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/restored_resource.h"
17 
18 #include "absl/types/span.h"
19 #include "tensorflow/c/eager/abstract_tensor_handle.h"
20 #include "tensorflow/c/eager/immediate_execution_context.h"
21 #include "tensorflow/c/eager/immediate_execution_operation.h"
22 #include "tensorflow/c/eager/immediate_execution_tensor_handle.h"
23 #include "tensorflow/c/experimental/saved_model/core/revived_types/tensorhandle_convertible.h"
24 #include "tensorflow/c/experimental/saved_model/core/revived_types/tf_concrete_function.h"
25 #include "tensorflow/core/platform/errors.h"
26 #include "tensorflow/core/platform/logging.h"
27 
28 namespace tensorflow {
29 
30 namespace {
31 
ExecuteNoArgDummyReturnFunction(TFConcreteFunction * func)32 Status ExecuteNoArgDummyReturnFunction(TFConcreteFunction* func) {
33   ImmediateOpPtr function_op;
34   TF_RETURN_IF_ERROR(func->MakeCallOp({}, &function_op));
35 
36   AbstractTensorHandle* dummy_output = nullptr;
37   int num_retvals = 1;
38   TF_RETURN_IF_ERROR(function_op->Execute(
39       absl::MakeSpan(&dummy_output, num_retvals), &num_retvals));
40   AbstractTensorHandlePtr owned_dummy_output(dummy_output);
41   return Status();
42 }
43 
44 }  // namespace
45 
RestoredResource(const std::string & device,TFConcreteFunction * create_resource,TFConcreteFunction * initialize,TFConcreteFunction * destroy_resource,ImmediateTensorHandlePtr resource_handle)46 RestoredResource::RestoredResource(const std::string& device,
47                                    TFConcreteFunction* create_resource,
48                                    TFConcreteFunction* initialize,
49                                    TFConcreteFunction* destroy_resource,
50                                    ImmediateTensorHandlePtr resource_handle)
51     : TensorHandleConvertible(std::move(resource_handle)),
52       device_(device),
53       create_resource_(create_resource),
54       initialize_(initialize),
55       destroy_resource_(destroy_resource) {}
56 
Initialize() const57 Status RestoredResource::Initialize() const {
58   return ExecuteNoArgDummyReturnFunction(initialize_);
59 }
60 
~RestoredResource()61 RestoredResource::~RestoredResource() {
62   // Note(bmzhao): SavedModels saved before
63   // https://github.com/tensorflow/tensorflow/commit/3c806101f57768e479f8646e7518bbdff1632ca3
64   // did not have their destroy_resource function saved, meaning they will
65   // leak resources.
66   if (destroy_resource_ != nullptr) {
67     Status status = ExecuteNoArgDummyReturnFunction(destroy_resource_);
68     if (!status.ok()) {
69       LOG(WARNING)
70           << "Failed executing destroy_resource function for RestoredResource: "
71           << status.error_message();
72     }
73   }
74 }
75 
76 }  // namespace tensorflow
77