1 /* Copyright 2019 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/common_runtime/eager/eager_op_rewrite_registry.h"
16
17 namespace tensorflow {
18
Global()19 EagerOpRewriteRegistry* EagerOpRewriteRegistry::Global() {
20 static EagerOpRewriteRegistry* global_rewrite_registry =
21 new EagerOpRewriteRegistry;
22 return global_rewrite_registry;
23 }
24
Register(Phase phase,std::unique_ptr<EagerOpRewrite> pass)25 void EagerOpRewriteRegistry::Register(Phase phase,
26 std::unique_ptr<EagerOpRewrite> pass) {
27 if (!rewrites_[phase]) {
28 rewrites_[phase] = std::move(pass);
29 } else {
30 TF_CHECK_OK(errors::AlreadyExists(pass->GetDebugInfo().name,
31 " is already registered as"
32 " EagerOpRewrite for this phase in ",
33 pass->GetDebugInfo().file, ":",
34 pass->GetDebugInfo().line));
35 }
36 }
37
RunRewrite(Phase phase,EagerOperation * orig_op,std::unique_ptr<tensorflow::EagerOperation> * out_op)38 Status EagerOpRewriteRegistry::RunRewrite(
39 Phase phase, EagerOperation* orig_op,
40 std::unique_ptr<tensorflow::EagerOperation>* out_op) {
41 auto& rewrite = rewrites_[phase];
42 if (rewrite) {
43 TF_RETURN_IF_ERROR(rewrite->Run(orig_op, out_op));
44 }
45 return Status::OK();
46 }
47
48 } // namespace tensorflow
49