1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SIGNATURE_DEF_FUNCTION_H_
17 #define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SIGNATURE_DEF_FUNCTION_H_
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "absl/types/span.h"
23 #include "tensorflow/c/eager/immediate_execution_operation.h"
24 #include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
25 #include "tensorflow/c/experimental/saved_model/core/signature_def_function_metadata.h"
26 
27 namespace tensorflow {
28 
29 // See tensorflow/cc/experimental/saved_model/public/signature_def_function.h
30 // for SignatureDefFunction's intended user-facing semantics.
31 // This class is the "implementation" C++ part of the C++/C/C++ sandwich for
32 // a SignatureDefFunction.
33 // Note(bmzhao): Implementation-wise, SignatureDefFunctions are always saved as
34 // a "BareConcreteFunction", w/o a FunctionSpec, rather than a SavedFunction:
35 // https://github.com/tensorflow/tensorflow/blob/9bcefa44cd335c1db4a703a13da09f29ae1bbdb2/tensorflow/core/protobuf/saved_object_graph.proto#L60
36 // Additionally they are guaranteed to be children of the .signatures attribute
37 // of the root object, where the child object "name" is the signature_def key:
38 // https://github.com/tensorflow/tensorflow/blob/9bcefa44cd335c1db4a703a13da09f29ae1bbdb2/tensorflow/python/saved_model/signature_serialization.py#L181-L230
39 // One of the critical requirements of SignatureDef functions is that their
40 // inputs and outputs are "named". For example, a `.signatures` function:
41 // a. Requires users to pass: kwargs of all inputs:
42 // https://github.com/tensorflow/tensorflow/blob/26c4ee0c833e74f94d0102d8b005c41a28b44445/tensorflow/python/saved_model/signature_serialization.py#L119-L126
43 // b. Returns a dictionary of named outputs.
44 // https://github.com/tensorflow/tensorflow/blob/26c4ee0c833e74f94d0102d8b005c41a28b44445/tensorflow/python/saved_model/signature_serialization.py#L153-L161
45 // Since SignatureDefFunctions do not have FunctionSpecs, but guarantee the
46 // dictionary of inputs/outputs, we can parse these dictionaries' keys to obtain
47 // the input/output names of the SignatureDef:
48 // https://github.com/tensorflow/tensorflow/blob/9bcefa44cd335c1db4a703a13da09f29ae1bbdb2/tensorflow/core/protobuf/meta_graph.proto#L318-L321
49 class SignatureDefFunction {
50  public:
51   virtual ~SignatureDefFunction() = default;
52 
53   // Creates a "Call" Op used to execute the function.
54   virtual Status MakeCallOp(absl::Span<AbstractTensorHandle* const> inputs,
55                             ImmediateOpPtr* out) const = 0;
56 
57   virtual const SignatureDefFunctionMetadata& GetFunctionMetadata() const = 0;
58 };
59 
60 }  // namespace tensorflow
61 
62 #endif  // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SIGNATURE_DEF_FUNCTION_H_
63