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 #include "tensorflow/c/experimental/saved_model/core/revived_types/flat_tensor_function.h"
17
18 #include <memory>
19 #include <string>
20
21 #include "absl/types/span.h"
22 #include "tensorflow/c/eager/abstract_tensor_handle.h"
23 #include "tensorflow/c/eager/immediate_execution_operation.h"
24 #include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
25 #include "tensorflow/core/common_runtime/eager/context.h"
26 #include "tensorflow/core/framework/function.pb.h"
27 #include "tensorflow/core/platform/errors.h"
28 #include "tensorflow/core/platform/logging.h"
29 #include "tensorflow/core/platform/status.h"
30 #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
31 #include "tensorflow/core/protobuf/struct.pb.h"
32
33 namespace tensorflow {
34
FlatTensorFunction(const std::string & name,std::vector<ImmediateTensorHandlePtr> captures,ImmediateExecutionContext * ctx)35 FlatTensorFunction::FlatTensorFunction(
36 const std::string& name, std::vector<ImmediateTensorHandlePtr> captures,
37 ImmediateExecutionContext* ctx)
38 : name_(name), captures_(std::move(captures)), ctx_(ctx) {}
39
~FlatTensorFunction()40 FlatTensorFunction::~FlatTensorFunction() {
41 Status status = ctx_->RemoveFunction(name_);
42 if (!status.ok()) {
43 LOG(ERROR) << "Failed to remove functiondef " << name_ << ". "
44 << status.error_message();
45 }
46 }
47
Create(const FunctionDef * function_def,std::vector<ImmediateExecutionTensorHandle * > captures,ImmediateExecutionContext * ctx,std::unique_ptr<FlatTensorFunction> * out)48 Status FlatTensorFunction::Create(
49 const FunctionDef* function_def,
50 std::vector<ImmediateExecutionTensorHandle*> captures,
51 ImmediateExecutionContext* ctx, std::unique_ptr<FlatTensorFunction>* out) {
52 TF_RETURN_IF_ERROR(ctx->AddFunctionDef(*function_def));
53 std::vector<ImmediateTensorHandlePtr> owned_captures;
54 owned_captures.reserve(captures.size());
55 for (ImmediateExecutionTensorHandle* capture : captures) {
56 capture->Ref();
57 owned_captures.push_back(ImmediateTensorHandlePtr(capture));
58 }
59
60 out->reset(new FlatTensorFunction(function_def->signature().name(),
61 std::move(owned_captures), ctx));
62 return Status();
63 }
64
MakeCallOp(absl::Span<AbstractTensorHandle * const> inputs,ImmediateOpPtr * out) const65 Status FlatTensorFunction::MakeCallOp(
66 absl::Span<AbstractTensorHandle* const> inputs, ImmediateOpPtr* out) const {
67 out->reset(ctx_->CreateOperation());
68 // In eager mode, TF2 python executes functions by constructing an op with
69 // the name of the functiondef:
70 // https://github.com/tensorflow/tensorflow/blob/66668ec0ca432e2f38a575b814f45b6d299d01ed/tensorflow/python/eager/function.py#L545
71 // In graph mode, we create a PartitionedCallOp instead:
72 // https://github.com/tensorflow/tensorflow/blob/66668ec0ca432e2f38a575b814f45b6d299d01ed/tensorflow/python/eager/function.py#L573
73
74 // TODO(bmzhao): After discussing with Allen, we should execute this via a
75 // PartitionedCallOp for compatibility with "tooling that assumes functions in
76 // graphs are PartitionedCallOps".
77 TF_RETURN_IF_ERROR((*out)->Reset(name_.c_str(), nullptr));
78
79 // Adding the user-provided inputs to the function.
80 TF_RETURN_IF_ERROR((*out)->AddInputList(inputs));
81
82 absl::Span<AbstractTensorHandle* const> captures(
83 reinterpret_cast<AbstractTensorHandle* const*>(captures_.data()),
84 captures_.size());
85
86 // Adding the captures of the function.
87 TF_RETURN_IF_ERROR((*out)->AddInputList(captures));
88 return Status();
89 }
90
91 } // namespace tensorflow
92