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_VECTORIZATION_UTILS_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_VECTORIZATION_UTILS_H_ 18 19 #include "tensorflow/core/framework/function.pb.h" 20 #include "tensorflow/core/lib/core/errors.h" 21 #include "tensorflow/core/lib/core/status.h" 22 23 namespace tensorflow { 24 namespace grappler { 25 namespace vectorization_utils { 26 27 // Given a MapDefun node (`map_defun_node`) in a FunctionDef (`outer_scope`) 28 // that maps a function in lib across some input vector elements, 29 // `VectorizeMapDefun` attempts to create a vectorized version of `outer_scope` 30 // by "lifting" operations from the MapDefun function to the new function 31 // (`result`); that is, replacing operations in the MapDefun function with 32 // operations that produce the same vector output(s) as executing the original 33 // operations on elements of vector input(s) would. If all operations in the 34 // MapDefun function are successfully lifted, `result` has no MapDefun node 35 // altogether. However, if some operations cannot be lifted, and this 36 // vectorization only succeeds partially, a MapDefun node remains in `result` to 37 // be used for operations that were not lifted, and the modified MapDefun 38 // function is added to `lib`. The newly vectorized function `result` is also 39 // added to `lib`. 40 // 41 // Returns Status::OK() if the vectorization is completely or partially 42 // successful. Otherwise, returns an error, and sets `result` to nullptr. 43 // 44 // Example: 45 // If the input to the `VectorizeMapDefun` function is a MapDefun 46 // whose `map_defun_fn` performs the Cast operation, the vectorization will 47 // eliminate the MapDefun. This is because the Cast operation supports 48 // any tensor shape and can thus be lifted to `result`. 49 // 50 // Before: 51 // 52 // 53 // outer_scope +------+ 54 // +---------------+ Arg0 +---------+ 55 // | +---+--+ | 56 // | | | 57 // | map_defun_fn +---v--+ | 58 // | +-----------+ Arg0 +-----+ | 59 // | | +---+--+ | | 60 // | | | | | 61 // | | | | | 62 // | | +---v--+ | | 63 // | | | Cast | | | 64 // | | +---+--+ | | 65 // | | | | | 66 // | | +---v--+ | | 67 // | +-----------+ Ret0 +-----+ | 68 // | +---+--+ | 69 // | | | 70 // | +---v--+ | 71 // +---------------+ Ret0 +---------+ 72 // +------+ 73 // 74 // 75 // After: 76 // 77 // result +------+ 78 // +---------------+ Arg0 +---------+ 79 // | +---+--+ | 80 // | | | 81 // | +---v--+ | 82 // | | Cast | | 83 // | +---+--+ | 84 // | | | 85 // | +---v--+ | 86 // +---------------+ Ret0 +---------+ 87 // +------+ 88 // 89 Status VectorizeMapDefun(const FunctionDef& outer_scope, 90 const NodeDef& map_defun_node, FunctionDefLibrary* lib, 91 FunctionDef** result); 92 93 } // namespace vectorization_utils 94 } // namespace grappler 95 } // namespace tensorflow 96 97 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_VECTORIZATION_UTILS_H_ 98