1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include <vector> 17 18#include <gtest/gtest.h> 19#include "tensorflow/contrib/lite/interpreter.h" 20#include "tensorflow/contrib/lite/kernels/register.h" 21#include "tensorflow/contrib/lite/kernels/test_util.h" 22#include "tensorflow/contrib/lite/model.h" 23 24namespace tflite { 25namespace { 26 27using ::testing::ElementsAre; 28 29class LSHProjectionOpModel : public SingleOpModel { 30 public: 31 LSHProjectionOpModel(LSHProjectionType type, 32 std::initializer_list<int> hash_shape, 33 std::initializer_list<int> input_shape, 34 std::initializer_list<int> weight_shape) { 35 hash_ = AddInput(TensorType_FLOAT32); 36 input_ = AddInput(TensorType_INT32); 37 if (weight_shape.size() > 0) { 38 weight_ = AddInput(TensorType_FLOAT32); 39 } 40 output_ = AddOutput(TensorType_INT32); 41 42 SetBuiltinOp(BuiltinOperator_LSH_PROJECTION, 43 BuiltinOptions_LSHProjectionOptions, 44 CreateLSHProjectionOptions(builder_, type).Union()); 45 if (weight_shape.size() > 0) { 46 BuildInterpreter({hash_shape, input_shape, weight_shape}); 47 } else { 48 BuildInterpreter({hash_shape, input_shape}); 49 } 50 51 output_size_ = 1; 52 for (int i : hash_shape) { 53 output_size_ *= i; 54 if (type == LSHProjectionType_SPARSE) { 55 break; 56 } 57 } 58 } 59 void SetInput(std::initializer_list<int> data) { 60 PopulateTensor(input_, data); 61 } 62 63 void SetHash(std::initializer_list<float> data) { 64 PopulateTensor(hash_, data); 65 } 66 67 void SetWeight(std::initializer_list<float> f) { PopulateTensor(weight_, f); } 68 69 std::vector<int> GetOutput() { return ExtractVector<int>(output_); } 70 71 private: 72 int input_; 73 int hash_; 74 int weight_; 75 int output_; 76 77 int output_size_; 78}; 79 80TEST(LSHProjectionOpTest2, Dense1DInputs) { 81 LSHProjectionOpModel m(LSHProjectionType_DENSE, {3, 2}, {5}, {5}); 82 83 m.SetInput({12345, 54321, 67890, 9876, -12345678}); 84 m.SetHash({0.123, 0.456, -0.321, 1.234, 5.678, -4.321}); 85 m.SetWeight({1.0, 1.0, 1.0, 1.0, 1.0}); 86 87 m.Invoke(); 88 89 EXPECT_THAT(m.GetOutput(), ElementsAre(0, 0, 0, 1, 0, 0)); 90} 91 92TEST(LSHProjectionOpTest2, Sparse1DInputs) { 93 LSHProjectionOpModel m(LSHProjectionType_SPARSE, {3, 2}, {5}, {}); 94 95 m.SetInput({12345, 54321, 67890, 9876, -12345678}); 96 m.SetHash({0.123, 0.456, -0.321, 1.234, 5.678, -4.321}); 97 98 m.Invoke(); 99 100 EXPECT_THAT(m.GetOutput(), ElementsAre(0 + 0, 4 + 1, 8 + 0)); 101} 102 103TEST(LSHProjectionOpTest2, Sparse3DInputs) { 104 LSHProjectionOpModel m(LSHProjectionType_SPARSE, {3, 2}, {5, 2, 2}, {5}); 105 106 m.SetInput({1234, 2345, 3456, 1234, 4567, 5678, 6789, 4567, 7891, 8912, 107 9123, 7890, -987, -876, -765, -987, -543, -432, -321, -543}); 108 m.SetHash({0.123, 0.456, -0.321, 1.234, 5.678, -4.321}); 109 m.SetWeight({0.12, 0.34, 0.56, 0.67, 0.78}); 110 111 m.Invoke(); 112 113 EXPECT_THAT(m.GetOutput(), ElementsAre(0 + 2, 4 + 1, 8 + 1)); 114} 115 116} // namespace 117} // namespace tflite 118 119int main(int argc, char** argv) { 120 ::tflite::LogToStderr(); 121 ::testing::InitGoogleTest(&argc, argv); 122 return RUN_ALL_TESTS(); 123} 124