xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tfrt/saved_model/saved_model_import_input.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 #include "tensorflow/core/tfrt/saved_model/saved_model_import_input.h"
16 
17 #include "absl/synchronization/mutex.h"
18 #include "tensorflow/compiler/mlir/tensorflow/translate/upgrade_graph.h"
19 #include "tensorflow/core/common_runtime/graph_constructor.h"
20 #include "tensorflow/core/util/dump_graph.h"
21 
22 namespace tensorflow {
23 namespace tfrt_stub {
24 
Create(const FallbackState & fallback_state,const MetaGraphDef * meta_graph_def,const GraphDebugInfo & debug_info,bool run_placer_grappler_on_nested_functions,bool enable_tfrt_gpu)25 StatusOr<TfrtSavedModelMLIRImportInput> TfrtSavedModelMLIRImportInput::Create(
26     const FallbackState& fallback_state, const MetaGraphDef* meta_graph_def,
27     const GraphDebugInfo& debug_info,
28     bool run_placer_grappler_on_nested_functions, bool enable_tfrt_gpu) {
29   DCHECK(meta_graph_def);
30 
31   TfrtGraphExecutionState::Options options;
32   options.run_placer_grappler_on_functions =
33       run_placer_grappler_on_nested_functions;
34   options.enable_tfrt_gpu = enable_tfrt_gpu;
35   TF_ASSIGN_OR_RETURN(
36       auto graph_execution_state,
37       TfrtGraphExecutionState::Create(options, meta_graph_def->graph_def(),
38                                       fallback_state));
39 
40   return TfrtSavedModelMLIRImportInput(meta_graph_def, debug_info,
41                                        std::move(graph_execution_state));
42 }
43 
TfrtSavedModelMLIRImportInput(const MetaGraphDef * meta_graph_def,const GraphDebugInfo & debug_info,std::unique_ptr<TfrtGraphExecutionState> graph_execution_state)44 TfrtSavedModelMLIRImportInput::TfrtSavedModelMLIRImportInput(
45     const MetaGraphDef* meta_graph_def, const GraphDebugInfo& debug_info,
46     std::unique_ptr<TfrtGraphExecutionState> graph_execution_state)
47     : SavedModelMLIRImportInput(meta_graph_def, debug_info),
48       graph_execution_state_(std::move(graph_execution_state)) {}
49 
GetSubGraph(absl::string_view name,GraphImportConfig & graph_import_config)50 StatusOr<const tensorflow::Graph*> TfrtSavedModelMLIRImportInput::GetSubGraph(
51     absl::string_view name, GraphImportConfig& graph_import_config) {
52   LOG(INFO) << "TFRT importing savedmodel signature: " << name;
53 
54   // Protects the members when muti-threads are calling this method to import
55   // signatures in parallel.
56   ABSL_CONST_INIT static absl::Mutex mu(absl::kConstInit);
57 
58   {
59     absl::MutexLock l(&mu);
60     auto iter = optimized_graphs_.find(name);
61     if (iter != optimized_graphs_.end()) return iter->second.get();
62   }
63 
64   TF_ASSIGN_OR_RETURN(
65       auto optimization_result,
66       graph_execution_state_->CreateOptimizedGraph(graph_import_config));
67 
68   absl::MutexLock l(&mu);
69   functionalization_duration_ += optimization_result.functionalization_duration;
70   grappler_duration_ += optimization_result.grappler_duration;
71 
72   const auto* optimized_graph_ptr = optimization_result.graph.get();
73   DCHECK(optimized_graph_ptr);
74   optimized_graphs_[name] = std::move(optimization_result.graph);
75   return optimized_graph_ptr;
76 }
77 
78 }  // namespace tfrt_stub
79 }  // namespace tensorflow
80