1 /* Copyright 2018 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_CORE_FRAMEWORK_DATASET_STATEFUL_OP_WHITELIST_H_ 17 #define TENSORFLOW_CORE_FRAMEWORK_DATASET_STATEFUL_OP_WHITELIST_H_ 18 19 #include <unordered_set> 20 #include "tensorflow/core/lib/core/status.h" 21 22 namespace tensorflow { 23 namespace data { 24 // Registry for stateful ops that need to be used in dataset functions. 25 // See below macro for usage details. 26 class WhitelistedStatefulOpRegistry { 27 public: Add(string op_name)28 Status Add(string op_name) { 29 op_names_.insert(std::move(op_name)); 30 return Status::OK(); 31 } 32 Contains(const string & op_name)33 bool Contains(const string& op_name) { return op_names_.count(op_name); } 34 Global()35 static WhitelistedStatefulOpRegistry* Global() { 36 static auto* reg = new WhitelistedStatefulOpRegistry; 37 return reg; 38 } 39 40 private: 41 WhitelistedStatefulOpRegistry() = default; 42 WhitelistedStatefulOpRegistry(WhitelistedStatefulOpRegistry const& copy) = 43 delete; 44 WhitelistedStatefulOpRegistry operator=( 45 WhitelistedStatefulOpRegistry const& copy) = delete; 46 47 std::unordered_set<string> op_names_; 48 }; 49 50 } // namespace data 51 52 // Use this macro to whitelist an op that is marked stateful but needs to be 53 // used inside a map_fn in an input pipeline. This is only needed if you wish 54 // to be able to checkpoint the state of the input pipeline. We currently 55 // do not allow stateful ops to be defined inside of map_fns since it is not 56 // possible to save their state. 57 // Note that the state of the whitelisted ops inside functions will not be 58 // saved during checkpointing, hence this should only be used if the op is 59 // marked stateful for reasons like to avoid constant folding during graph 60 // optimiztion but is not stateful. 61 // If possible, try to remove the stateful flag on the op first. 62 // Example usage: 63 // 64 // WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS("LegacyStatefulReader"); 65 // 66 #define WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS(name) \ 67 WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS_UNIQ_HELPER(__COUNTER__, name) 68 #define WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS_UNIQ_HELPER(ctr, name) \ 69 WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS_UNIQ(ctr, name) 70 #define WHITELIST_STATEFUL_OP_FOR_DATASET_FUNCTIONS_UNIQ(ctr, name) \ 71 static ::tensorflow::Status whitelist_op##ctr TF_ATTRIBUTE_UNUSED = \ 72 ::tensorflow::data::WhitelistedStatefulOpRegistry::Global()->Add(name) 73 74 } // namespace tensorflow 75 76 #endif // TENSORFLOW_CORE_FRAMEWORK_DATASET_STATEFUL_OP_WHITELIST_H_ 77