• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 #include <cstdint>
16 #include <memory>
17 #include <vector>
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include "absl/memory/memory.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/kernels/internal/types.h"
24 #include "tensorflow/lite/kernels/test_util.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 
27 namespace tflite {
28 
29 namespace ops {
30 namespace builtin {
31 
32 TfLiteRegistration* Register_DENSIFY();
33 
34 }  // namespace builtin
35 }  // namespace ops
36 
37 namespace {
38 
39 using ::testing::ElementsAreArray;
40 
41 template <typename T>
42 class DensifyOpModel : public SingleOpModel {
43  public:
DensifyOpModel(const TensorData & input,const std::vector<T> & input_data,int version=1)44   DensifyOpModel(const TensorData& input, const std::vector<T>& input_data,
45                  int version = 1) {
46     input_ = AddConstSparseInput(input, input_data);
47     output_ = AddOutput({input.type, input.shape});
48 
49     SetBuiltinOp(BuiltinOperator_DENSIFY, BuiltinOptions_DensifyOptions,
50                  CreateDensifyOptions(builder_).Union());
51 
52     resolver_ = absl::make_unique<SingleOpResolver>(
53         BuiltinOperator_DENSIFY, ops::builtin::Register_DENSIFY(), version);
54 
55     BuildInterpreter({input.shape}, /*num_threads=*/-1,
56                      /*allow_fp32_relax_to_fp16=*/false,
57                      /*apply_delegate=*/false, /*allocate_and_delegate=*/true);
58   }
59 
GetInput()60   std::vector<T> GetInput() { return ExtractVector<T>(input_); }
GetOutput()61   std::vector<T> GetOutput() { return ExtractVector<T>(output_); }
62 
63  private:
64   int input_;
65   int output_;
66 };
67 
TEST(DensifyOpTest,Float)68 TEST(DensifyOpTest, Float) {
69   std::vector<float> dense_values = {6, 0, 9, 8, 0, 0, 0, 0, 5, 0, 0, 7};
70   std::vector<float> sparse_values = {6, 9, 8, 5, 7};
71   TensorData input = {};
72   input.type = TensorType_FLOAT32;
73   input.shape = {3, 4};
74   input.traversal_order = {0, 1};
75   input.format = {kTfLiteDimDense, kTfLiteDimSparseCSR};
76   DensifyOpModel<float> m(input, dense_values);
77   m.Invoke();
78   EXPECT_THAT(m.GetInput(), ElementsAreArray(sparse_values));
79   EXPECT_THAT(m.GetOutput(), ElementsAreArray(dense_values));
80 }
81 
TEST(DensifyOpTest,Float3D)82 TEST(DensifyOpTest, Float3D) {
83   std::vector<float> dense_values = {6, 0, 9, 8, 0, 0, 0, 0, 5, 0, 0, 7};
84   std::vector<float> sparse_values = {6, 9, 8, 5, 7};
85   TensorData input = {};
86   input.type = TensorType_FLOAT32;
87   input.shape = {3, 2, 2};
88   input.traversal_order = {0, 1, 2};
89   input.format = {kTfLiteDimDense, kTfLiteDimDense, kTfLiteDimSparseCSR};
90   DensifyOpModel<float> m(input, dense_values);
91   m.Invoke();
92   EXPECT_THAT(m.GetInput(), ElementsAreArray(sparse_values));
93   EXPECT_THAT(m.GetOutput(), ElementsAreArray(dense_values));
94 }
95 
TEST(DensifyOpTest,Int8)96 TEST(DensifyOpTest, Int8) {
97   std::vector<int8_t> dense_values = {6, 0, 9, 8, 0, 0, 0, 0, 5, 0, 0, 7};
98   std::vector<int8_t> sparse_values = {6, 9, 8, 5, 7};
99   TensorData input = {};
100   input.type = TensorType_INT8;
101   input.shape = {3, 4};
102   input.traversal_order = {0, 1};
103   input.format = {kTfLiteDimDense, kTfLiteDimSparseCSR};
104   DensifyOpModel<int8_t> m(input, dense_values);
105   m.Invoke();
106   EXPECT_THAT(m.GetInput(), ElementsAreArray(sparse_values));
107   EXPECT_THAT(m.GetOutput(), ElementsAreArray(dense_values));
108 }
109 
110 }  // namespace
111 }  // namespace tflite
112