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_GRAPPLER_OPTIMIZERS_DATA_MAP_VECTORIZATION_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_MAP_VECTORIZATION_H_ 18 19 #include "tensorflow/core/framework/attr_value.pb.h" 20 #include "tensorflow/core/grappler/optimizers/data/optimizer_base.h" 21 22 namespace tensorflow { 23 namespace grappler { 24 25 constexpr char kUseChooseFastest[] = "use_choose_fastest"; 26 27 // This optimizer rewrites dataset.map(map_fn, ...).batch(...) and 28 // dataset.apply(tf.data.experimental.map_and_batch(map_fn, ...)) patterns in an 29 // input pipeline. It vectorizes the map_fn, such that this segment can be 30 // rewritten as dataset.batch().map(vectorized_map_fn). This is more performant 31 // when the map_fn is cheap, because it amortizes the cost of running a map 32 // function over a larger batch. 33 // 34 // From: 35 // input --> map --> batch --> output 36 // (or map_and_batch) 37 // 38 // To: 39 // input --> batch --> map --> output 40 // 41 // If the "ChooseFastest" configuration is enabled, it adds a 42 // ChooseFastestBranch dataset node to pick between the original map->batch 43 // branch and the vectorized batch->map branch. 44 // 45 class MapVectorization : public TFDataOptimizerBase { 46 public: 47 MapVectorization() = default; 48 ~MapVectorization() override = default; 49 name()50 string name() const override { return "map_vectorization"; }; 51 UsesFunctionLibrary()52 bool UsesFunctionLibrary() const override { return false; } 53 Init(const tensorflow::RewriterConfig_CustomGraphOptimizer * config)54 Status Init( 55 const tensorflow::RewriterConfig_CustomGraphOptimizer* config) override { 56 if (!config) return Status::OK(); 57 58 const string& choose_fastest_param = 59 config->parameter_map().at(kUseChooseFastest).s(); 60 if (choose_fastest_param == "true") { 61 use_choose_fastest_ = true; 62 } else if (choose_fastest_param == "false") { 63 use_choose_fastest_ = false; 64 } else { 65 return errors::InvalidArgument("Received an invalid value for parameter ", 66 kUseChooseFastest, ": ", 67 choose_fastest_param); 68 } 69 return Status::OK(); 70 } 71 72 Status OptimizeAndCollectStats(Cluster* cluster, const GrapplerItem& item, 73 GraphDef* output, 74 OptimizationStats* stats) override; 75 76 void Feedback(Cluster* cluster, const GrapplerItem& item, 77 const GraphDef& optimize_output, double result) override; 78 79 private: 80 bool use_choose_fastest_ = false; 81 }; 82 83 } // namespace grappler 84 } // namespace tensorflow 85 86 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_MAP_VECTORIZATION_H_ 87