• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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_XNNPACK_SPLIT_TESTER_H_
17 #define TENSORFLOW_LITE_DELEGATES_XNNPACK_SPLIT_TESTER_H_
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 #include "tensorflow/lite/c/common.h"
24 #include "tensorflow/lite/interpreter.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 
27 namespace tflite {
28 namespace xnnpack {
29 
30 class SplitTester {
31  public:
32   SplitTester() = default;
33   SplitTester(const SplitTester&) = delete;
34   SplitTester& operator=(const SplitTester&) = delete;
35 
SplitDimension(int32_t split_dim)36   inline SplitTester& SplitDimension(int32_t split_dim) {
37     split_dim_ = split_dim;
38     return *this;
39   }
40 
InputShape(const std::vector<int32_t> & shape)41   inline SplitTester& InputShape(const std::vector<int32_t>& shape) {
42     for (auto it = shape.begin(); it != shape.end(); ++it) {
43       EXPECT_GT(*it, 0);
44     }
45     input_shape_ = std::vector<int32_t>(shape.begin(), shape.end());
46     return *this;
47   }
48 
SplitDimension()49   int32_t SplitDimension() const { return split_dim_; }
50 
NumSplits(int num_splits)51   inline SplitTester& NumSplits(int num_splits) {
52     num_splits_ = num_splits;
53     return *this;
54   }
55 
NumSplits()56   inline const int NumSplits() const { return num_splits_; }
57 
InputShape()58   inline const std::vector<int32_t>& InputShape() const { return input_shape_; }
59 
OutputShape()60   std::vector<int32_t> OutputShape() const {
61     std::vector<int32_t> output_shape = InputShape();
62     int32_t split_dim = SplitDimension();
63     split_dim += split_dim < 0 ? InputShape().size() : 0;
64     EXPECT_LE(0, split_dim);
65     EXPECT_EQ(0, output_shape[split_dim] % NumSplits());
66     output_shape[split_dim] /= NumSplits();
67     return output_shape;
68   }
69 
70   template <typename T>
71   void Test(Interpreter* delegate_interpreter,
72             Interpreter* default_interpreter) const;
73   void Test(TensorType tensor_type, TfLiteDelegate* delegate) const;
74 
75  private:
76   std::vector<char> CreateTfLiteModel(TensorType tensor_type) const;
77 
78   static int32_t ComputeSize(const std::vector<int32_t>& shape);
79 
80   std::vector<int32_t> input_shape_;
81   int32_t split_dim_;
82   int num_splits_;
83 };
84 
85 }  // namespace xnnpack
86 }  // namespace tflite
87 
88 #endif  // TENSORFLOW_LITE_DELEGATES_XNNPACK_SPLIT_TESTER_H_
89