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_LITE_DELEGATES_FLEX_TEST_UTIL_H_ 17 #define TENSORFLOW_LITE_DELEGATES_FLEX_TEST_UTIL_H_ 18 19 #include "tensorflow/c/c_api_internal.h" 20 #include "tensorflow/lite/kernels/test_util.h" 21 22 namespace tflite { 23 namespace flex { 24 namespace testing { 25 26 enum TfOpType { 27 kUnpack, 28 kIdentity, 29 kAdd, 30 kMul, 31 // Represents an op that does not exist in TensorFlow. 32 kNonExistent, 33 // Represents an valid TensorFlow op where the NodeDef is incompatible. 34 kIncompatibleNodeDef, 35 }; 36 37 // This class creates models with TF and TFLite ops. In order to use this class 38 // to test the Flex delegate, implement a function that calls 39 // interpreter->ModifyGraphWithDelegate. 40 class FlexModelTest : public ::testing::Test { 41 public: FlexModelTest()42 FlexModelTest() {} ~FlexModelTest()43 ~FlexModelTest() {} 44 45 bool Invoke(); 46 47 // Sets the (typed) tensor's values at the given index. 48 template <typename T> SetTypedValues(int tensor_index,const std::vector<T> & values)49 void SetTypedValues(int tensor_index, const std::vector<T>& values) { 50 memcpy(interpreter_->typed_tensor<T>(tensor_index), values.data(), 51 values.size() * sizeof(T)); 52 } 53 54 // Returns the (typed) tensor's values at the given index. 55 template <typename T> GetTypedValues(int tensor_index)56 std::vector<T> GetTypedValues(int tensor_index) { 57 const TfLiteTensor* t = interpreter_->tensor(tensor_index); 58 const T* tdata = interpreter_->typed_tensor<T>(tensor_index); 59 return std::vector<T>(tdata, tdata + t->bytes / sizeof(T)); 60 } 61 62 // Sets the tensor's values at the given index. SetValues(int tensor_index,const std::vector<float> & values)63 void SetValues(int tensor_index, const std::vector<float>& values) { 64 SetTypedValues<float>(tensor_index, values); 65 } 66 void SetStringValues(int tensor_index, const std::vector<string>& values); 67 68 // Returns the tensor's values at the given index. GetValues(int tensor_index)69 std::vector<float> GetValues(int tensor_index) { 70 return GetTypedValues<float>(tensor_index); 71 } 72 std::vector<string> GetStringValues(int tensor_index) const; 73 74 // Sets the tensor's shape at the given index. 75 void SetShape(int tensor_index, const std::vector<int>& values); 76 77 // Returns the tensor's shape at the given index. 78 std::vector<int> GetShape(int tensor_index); 79 80 // Returns the tensor's type at the given index. 81 TfLiteType GetType(int tensor_index); 82 error_reporter()83 const TestErrorReporter& error_reporter() const { return error_reporter_; } 84 85 // Adds `num_tensor` tensors to the model. `inputs` contains the indices of 86 // the input tensors and `outputs` contains the indices of the output 87 // tensors. All tensors are set to have `type` and `dims`. 88 void AddTensors(int num_tensors, const std::vector<int>& inputs, 89 const std::vector<int>& outputs, TfLiteType type, 90 const std::vector<int>& dims); 91 92 // Adds a TFLite Mul op. `inputs` contains the indices of the input tensors 93 // and `outputs` contains the indices of the output tensors. 94 void AddTfLiteMulOp(const std::vector<int>& inputs, 95 const std::vector<int>& outputs); 96 97 // Adds a TensorFlow op. `inputs` contains the indices of the 98 // input tensors and `outputs` contains the indices of the output tensors. 99 // This function is limited to the set of ops defined in TfOpType. 100 void AddTfOp(TfOpType op, const std::vector<int>& inputs, 101 const std::vector<int>& outputs); 102 103 protected: 104 std::unique_ptr<Interpreter> interpreter_; 105 TestErrorReporter error_reporter_; 106 std::vector<int> tf_ops_; 107 108 private: 109 // Helper method to add a TensorFlow op. tflite_names needs to start with 110 // "Flex" in order to work with the Flex delegate. 111 void AddTfOp(const char* tflite_name, const string& tf_name, 112 const string& nodedef_str, const std::vector<int>& inputs, 113 const std::vector<int>& outputs); 114 115 std::vector<std::vector<uint8_t>> flexbuffers_; 116 117 int next_op_index_ = 0; 118 }; 119 120 } // namespace testing 121 } // namespace flex 122 } // namespace tflite 123 124 #endif // TENSORFLOW_LITE_DELEGATES_FLEX_TEST_UTIL_H_ 125