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 #ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_MATMUL_BUILDER_H_
16 #define TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_MATMUL_BUILDER_H_
17 
18 #include <vector>
19 
20 #include "tensorflow/lite/delegates/hexagon/builders/op_builder.h"
21 
22 namespace tflite {
23 namespace delegates {
24 namespace hexagon {
25 
26 // Builder for FullyConnected op in Hexagon with weights as const.
27 class MatMulWithConstWeightsOpBuilder : public OpBuilder {
28  public:
MatMulWithConstWeightsOpBuilder(GraphBuilder * graph_builder,int op_type)29   explicit MatMulWithConstWeightsOpBuilder(GraphBuilder* graph_builder,
30                                            int op_type)
31       : OpBuilder(graph_builder, op_type) {}
32   TfLiteStatus PopulateSubGraph(const TfLiteIntArray* inputs,
33                                 const TfLiteIntArray* outputs,
34                                 TfLiteContext* context) override;
35 
36   TfLiteStatus RegisterOutputs(const TfLiteIntArray* outputs,
37                                TfLiteContext* context) override;
38 
39  private:
40   TensorID node_output_;
41   std::vector<int> weights_shape_, bias_shape_;
42   std::vector<float> transposed_weights_;
43   float weights_min_, weights_max_;
44 };
45 
46 // Builder for FullyConnected op in Hexagon with non const weights.
47 class MatMulOpBuilder : public OpBuilder {
48  public:
MatMulOpBuilder(GraphBuilder * graph_builder,int op_type)49   explicit MatMulOpBuilder(GraphBuilder* graph_builder, int op_type)
50       : OpBuilder(graph_builder, op_type) {}
51   TfLiteStatus PopulateSubGraph(const TfLiteIntArray* inputs,
52                                 const TfLiteIntArray* outputs,
53                                 TfLiteContext* context) override;
54 
55   TfLiteStatus RegisterOutputs(const TfLiteIntArray* outputs,
56                                TfLiteContext* context) override;
57 
58  private:
59   // Adds Fully connected op related ops to the graph.
60   TfLiteStatus AddFullyConnected(const TfLiteIntArray* inputs,
61                                  const TfLiteIntArray* outputs,
62                                  const TensorID weights_id,
63                                  const TensorID weights_min_id,
64                                  const TensorID weights_max_id,
65                                  TfLiteContext* context, OpBuilder* matmul_op);
66 
67   TensorID node_output_;
68   std::vector<int> weights_shape_, bias_shape_;
69   std::vector<float> transposed_weights_;
70   float weights_min_, weights_max_;
71 };
72 
73 }  // namespace hexagon
74 }  // namespace delegates
75 }  // namespace tflite
76 
77 #endif  // TENSORFLOW_LITE_DELEGATES_HEXAGON_BUILDERS_MATMUL_BUILDER_H_
78