/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_TEST_UTIL_H_ #define TENSORFLOW_CONTRIB_LITE_KERNELS_TEST_UTIL_H_ #include #include #include #include "tensorflow/contrib/lite/interpreter.h" #include "tensorflow/contrib/lite/kernels/register.h" #include "tensorflow/contrib/lite/model.h" #include "tensorflow/contrib/lite/string_util.h" #include "tensorflow/contrib/lite/testing/util.h" #include "tensorflow/core/platform/logging.h" namespace tflite { // A gmock matcher that check that elements of a float vector match to a given // tolerance. std::vector<::testing::Matcher> ArrayFloatNear( const std::vector& values, float max_abs_error = 1e-5); template inline std::vector Quantize(const std::vector& data, float scale, int32_t zero_point) { std::vector q; for (float f : data) { q.push_back(std::max( std::numeric_limits::min(), std::min(std::numeric_limits::max(), static_cast(std::round(zero_point + (f / scale)))))); } return q; } template inline std::vector Dequantize(const std::vector& data, float scale, int32_t zero_point) { std::vector f; for (T q : data) { f.push_back(scale * (q - zero_point)); } return f; } // A test model that contains a single operator. All operator inputs and // output are external to the model, so the tests can directly access them. // Typical usage: // SingleOpModel m; // int a = m.AddInput({TensorType_FLOAT32, a_shape}); // int b = m.AddInput({TensorType_FLOAT32, b_shape}); // int c = m.AddOutput({TensorType_FLOAT32, {}}); // m.SetBuiltinOp(...); // m.BuildInterpreter({GetShape(a), GetShape(b)}); // m.PopulateTensor(a, {...}); // m.PopulateTensor(b, {...}); // m.Invoke(); // EXPECT_THAT(m.ExtractVector(c), ArrayFloatNear({...})); // // A helper struct to construct test tensors. This is particularly useful for // quantized tensor which must have their scale and zero_point defined before // the actual data is known. This mimics what happens in practice: quantization // parameters are calculate during training. struct TensorData { TensorType type; std::vector shape; float min; float max; float scale; int32_t zero_point; }; class SingleOpResolver : public OpResolver { public: SingleOpResolver(const BuiltinOperator op, TfLiteRegistration* registration) : op_(op), registration_(registration) {} TfLiteRegistration* FindOp(BuiltinOperator op) const override { if (op == op_) { return registration_; } return nullptr; } TfLiteRegistration* FindOp(const char* op) const override { return nullptr; } private: const BuiltinOperator op_; TfLiteRegistration* registration_; }; class SingleOpModel { public: SingleOpModel() {} ~SingleOpModel() {} // Copying or assignment is disallowed to simplify ownership semantics. SingleOpModel(const SingleOpModel&) = delete; SingleOpModel& operator=(const SingleOpModel&) = delete; // Add a TensorType input tensor and return its index. int AddInput(TensorType type) { return AddInput(TensorData{type}); } int AddInput(const TensorData& t); // Add a Tensor containing const data and return the tensor id. int AddConstInput(TensorType type, std::initializer_list data, std::initializer_list shape); // Add a null input tensor (optional input) and return kOptionalTensor. int AddNullInput(); // Add a TensorType output tensor and return its index. int AddOutput(TensorType type) { return AddOutput(TensorData{type}); } int AddOutput(const TensorData& t); template void QuantizeAndPopulate(int index, std::initializer_list data) { TfLiteTensor* t = interpreter_->tensor(index); auto q = Quantize(data, t->params.scale, t->params.zero_point); PopulateTensor(index, 0, q.data(), q.data() + q.size()); } const std::vector& GetShape(int id) { return tensor_data_.at(id).shape; } float GetScale(int id) { return tensor_data_.at(id).scale; } int32_t GetZeroPoint(int id) { return tensor_data_.at(id).zero_point; } // Define the operator in this model. void SetBuiltinOp(BuiltinOperator type, BuiltinOptions builtin_options_type, flatbuffers::Offset builtin_options); void SetCustomOp(const string& name, const std::vector& custom_option, const std::function& registeration); // Build the interpreter for this model. Also, resize and allocate all // tensors given the shapes of the inputs. void BuildInterpreter(std::vector> input_shapes); void Invoke(); void PopulateStringTensor(int index, const std::vector& content) { auto tensor = interpreter_->tensor(index); DynamicBuffer buf; for (const string& s : content) { buf.AddString(s.data(), s.length()); } buf.WriteToTensor(tensor); } // Populate the tensor given its index. template void PopulateTensor(int index, std::initializer_list data) { T* v = interpreter_->typed_tensor(index); CHECK(v) << "No tensor with index '" << index << "'."; for (T f : data) { *v = f; ++v; } } // Partially populate the tensor, starting at the given offset. template void PopulateTensor(int index, int offset, T* begin, T* end) { T* v = interpreter_->typed_tensor(index); memcpy(v + offset, begin, (end - begin) * sizeof(T)); } // Return a vector with the flattened contents of a tensor. template std::vector ExtractVector(int index) { T* v = interpreter_->typed_tensor(index); CHECK(v); return std::vector(v, v + GetTensorSize(index)); } std::vector GetTensorShape(int index) { std::vector result; TfLiteTensor* t = interpreter_->tensor(index); for (int i = 0; i < t->dims->size; ++i) { result.push_back(t->dims->data[i]); } return result; } void SetResolver(std::unique_ptr resolver) { resolver_ = std::move(resolver); } protected: int32_t GetTensorSize(int index) const; flatbuffers::FlatBufferBuilder builder_; std::unique_ptr interpreter_; std::unique_ptr resolver_; private: int AddTensor(TensorData t, std::initializer_list data); std::map tensor_data_; std::vector inputs_; std::vector outputs_; std::vector> tensors_; std::vector> opcodes_; std::vector> operators_; std::vector> buffers_; std::map> custom_registrations_; }; // Base class for single op unit tests. // The tests are parameterized to test multiple kernels for a single op. // The parameters are strings like "optimized" and "reference" to have better // readability in test reports. // // To use this class: // * Define a constant map from strings to TfLiteRegistration. // * Implement a test class that inherits SingleOpTest. // * Instantiate the test cases with SingleOpTest::GetKernelTags helper // function. // * Call GetRegistration to get the TfLiteRegistration to be used before // building the interpreter. class SingleOpTest : public ::testing::TestWithParam { public: static std::vector GetKernelTags( const std::map& kernel_map) { std::vector tags; for (auto it : kernel_map) { tags.push_back(it.first); } return tags; } protected: virtual const std::map& GetKernelMap() = 0; TfLiteRegistration* GetRegistration() { return GetKernelMap().at(GetParam()); } }; // Strings have a special implementation that is in test_util.cc template <> std::vector SingleOpModel::ExtractVector(int index); } // namespace tflite #endif // TENSORFLOW_CONTRIB_LITE_KERNELS_TEST_UTIL_H_