1 /* Copyright 2016 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/cc/framework/grad_op_registry.h" 17 18 namespace tensorflow { 19 namespace ops { 20 21 // static Global()22GradOpRegistry* GradOpRegistry::Global() { 23 static GradOpRegistry* grad_op_registry = new GradOpRegistry; 24 return grad_op_registry; 25 } 26 Register(const string & op,GradFunc func)27bool GradOpRegistry::Register(const string& op, GradFunc func) { 28 CHECK(registry_.insert({op, func}).second) << "Existing gradient for " << op; 29 return true; 30 } 31 Lookup(const string & op,GradFunc * func) const32Status GradOpRegistry::Lookup(const string& op, GradFunc* func) const { 33 auto iter = registry_.find(op); 34 if (iter == registry_.end()) { 35 const string error_msg = 36 "No gradient defined for op: " + op + 37 ". Please see " 38 "https://www.tensorflow.org/code/" 39 "tensorflow/cc/gradients/README.md" 40 " for instructions on how to add C++ gradients."; 41 return errors::NotFound(error_msg); 42 } 43 *func = iter->second; 44 return Status::OK(); 45 } 46 47 } // end namespace ops 48 } // namespace tensorflow 49