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 #ifndef TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_ 17 #define TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_ 18 19 #include <unordered_map> 20 21 #include "tensorflow/cc/framework/ops.h" 22 #include "tensorflow/cc/framework/scope.h" 23 24 namespace tensorflow { 25 namespace ops { 26 27 /// GradFunc is the signature for all gradient functions in GradOpRegistry. 28 /// Implementations should add operations to compute the gradient outputs of 29 /// 'op' (returned in 'grad_outputs') using 'scope' and 'grad_inputs'. 30 typedef Status (*GradFunc)(const Scope& scope, const Operation& op, 31 const std::vector<Output>& grad_inputs, 32 std::vector<Output>* grad_outputs); 33 34 /// GradOpRegistry maintains a static registry of gradient functions. 35 /// Gradient functions are indexed in the registry by the forward op name (i.e. 36 /// "MatMul" -> MatMulGrad func). 37 class GradOpRegistry { 38 public: 39 /// Registers 'func' as the gradient function for 'op'. 40 /// Returns true if registration was successful, check fails otherwise. 41 bool Register(const string& op, GradFunc func); 42 43 /// Sets 'func' to the gradient function for 'op' and returns Status OK if 44 /// the gradient function for 'op' exists in the registry. 45 /// Note that 'func' can be null for ops that have registered no-gradient with 46 /// the registry. 47 /// Returns error status otherwise. 48 Status Lookup(const string& op, GradFunc* func) const; 49 50 /// Returns a pointer to the global gradient function registry. 51 static GradOpRegistry* Global(); 52 53 private: 54 std::unordered_map<string, GradFunc> registry_; 55 }; 56 57 } // namespace ops 58 59 // Macros used to define gradient functions for ops. 60 #define REGISTER_GRADIENT_OP(name, fn) \ 61 REGISTER_GRADIENT_OP_UNIQ_HELPER(__COUNTER__, name, fn) 62 63 #define REGISTER_NO_GRADIENT_OP(name) \ 64 REGISTER_GRADIENT_OP_UNIQ_HELPER(__COUNTER__, name, nullptr) 65 66 #define REGISTER_GRADIENT_OP_UNIQ_HELPER(ctr, name, fn) \ 67 REGISTER_GRADIENT_OP_UNIQ(ctr, name, fn) 68 69 #define REGISTER_GRADIENT_OP_UNIQ(ctr, name, fn) \ 70 static bool unused_ret_val_##ctr = \ 71 ::tensorflow::ops::GradOpRegistry::Global()->Register(name, fn) 72 73 } // namespace tensorflow 74 75 #endif // TENSORFLOW_CC_FRAMEWORK_GRAD_OP_REGISTRY_H_ 76