1 /* Copyright 2017 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 #ifndef TENSORFLOW_CORE_KERNELS_DATA_DATASET_UTILS_H_ 16 #define TENSORFLOW_CORE_KERNELS_DATA_DATASET_UTILS_H_ 17 18 #include "tensorflow/core/framework/dataset.h" 19 #include "tensorflow/core/framework/tensor.h" 20 #include "tensorflow/core/kernels/data/captured_function.h" 21 22 namespace tensorflow { 23 namespace data { 24 25 // This method is used to determine whether we can short-circuit the evaluation 26 // of the user-defined function `func`. Short-circuting is possible if every 27 // function output corresponds to one of its inputs (e.g. `f(x) = x`, `f(x,y) = 28 // (y,x)`, or `f(x) = (x,x)`). 29 // 30 // If short-circuiting is possible, the method stores the mapping from output 31 // indices to input indices in `indices`. Otherwise, `indices` will be empty. 32 // 33 // Returns non-ok status if analysis of the function fails. 34 // 35 // TODO(jsimsa): Extend this to support constants as well. 36 Status ComputeShortCircuitIndices(OpKernelConstruction* ctx, 37 const NameAttrList& func, 38 std::vector<int>* indices); 39 40 // Given a vector that maps output indices to input indices, return a vector 41 // that identifies for which output indices can we move the input (assuming 42 // output indices are processed left to right). 43 std::vector<bool> ComputeMoveVector(const std::vector<int>& indices); 44 45 Status MakeIteratorFromInputElement( 46 IteratorContext* ctx, const std::vector<Tensor>& input_element, 47 int64 thread_index, const InstantiatedCapturedFunction& inst_captured_func, 48 StringPiece prefix, std::unique_ptr<IteratorBase>* out_iterator); 49 50 // Returns Status::OK() if `expected` and `received` types match, 51 // errors::InvalidArgument otherwise. 52 Status VerifyTypesMatch(const DataTypeVector& expected, 53 const DataTypeVector& received); 54 55 // Returns Status::OK() if `expected` and `received` shapes are compatible, 56 // errors::InvalidArgument otherwise. 57 Status VerifyShapesCompatible(const std::vector<PartialTensorShape>& expected, 58 const std::vector<PartialTensorShape>& received); 59 60 // Helper class for reading data from a VariantTensorData object. 61 class VariantTensorDataReader : public IteratorStateReader { 62 public: 63 explicit VariantTensorDataReader(const VariantTensorData* data); 64 65 // Returns OK iff the initialization was successful. 66 Status ReadScalar(StringPiece key, int64* val) override; 67 Status ReadScalar(StringPiece key, string* val) override; 68 Status ReadTensor(StringPiece key, Tensor* val) override; 69 bool Contains(StringPiece key) override; 70 71 private: 72 template <typename T> 73 Status ReadScalarInternal(StringPiece key, T* val); 74 Status ReadTensorInternal(StringPiece key, Tensor* val); 75 76 std::map<string, size_t> map_; 77 const VariantTensorData* data_; // Not owned. 78 }; 79 80 // Helper class for writing data to a VariantTensorData object. 81 class VariantTensorDataWriter : public IteratorStateWriter { 82 public: 83 // Does not take ownership of data. VariantTensorDataWriter(VariantTensorData * data)84 explicit VariantTensorDataWriter(VariantTensorData* data) : data_(data) {} 85 Status WriteScalar(StringPiece key, const int64 val) override; 86 Status WriteScalar(StringPiece key, const string& val) override; 87 Status WriteTensor(StringPiece key, const Tensor& val) override; 88 89 // Writes the metadata to `data_`. 90 Status Flush(); 91 92 private: 93 template <typename T> 94 Status WriteScalarInternal(StringPiece key, const T& val); 95 Status WriteTensorInternal(StringPiece key, const Tensor& val); 96 97 VariantTensorData* data_; 98 std::vector<string> keys_; 99 }; 100 101 // Adds the functions in `to_add` to `base`. If a function with a matching 102 // signature already exists in `base`, replaces it with the function from 103 // `to_add`. 104 Status AddToFunctionLibrary(FunctionLibraryDefinition* base, 105 const FunctionLibraryDefinition& to_add); 106 Status AddToFunctionLibrary(FunctionLibraryDefinition* base, 107 const FunctionDefLibrary& to_add); 108 109 // Creates a runner that runs functions with limited parallelism. 110 std::function<void(std::function<void()>)> RunnerWithMaxParallelism( 111 std::function<void(std::function<void()>)> runner, int max_parallelism); 112 113 } // namespace data 114 } // namespace tensorflow 115 116 #endif // TENSORFLOW_CORE_KERNELS_DATA_DATASET_UTILS_H_ 117