• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 "tensorflow/lite/delegates/hexagon/builders/cast_builder.h"
16 
17 #include <stdint.h>
18 
19 #include <limits>
20 
21 #include "tensorflow/lite/c/builtin_op_data.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h"
24 #include "tensorflow/lite/kernels/kernel_util.h"
25 
26 namespace tflite {
27 namespace delegates {
28 namespace hexagon {
PopulateSubGraph(const TfLiteIntArray * inputs,const TfLiteIntArray * outputs,TfLiteContext * context)29 TfLiteStatus CastOpBuilder::PopulateSubGraph(const TfLiteIntArray* inputs,
30                                              const TfLiteIntArray* outputs,
31                                              TfLiteContext* context) {
32   // Should be only 1 tensor that is cast in-place.
33   if (inputs->size != 1 || outputs->size != 1) {
34     TF_LITE_KERNEL_LOG(context, "Cast supports a single tensor");
35     return kTfLiteError;
36   } else if (inputs->data[0] != outputs->data[0]) {
37     TF_LITE_KERNEL_LOG(context, "input & output should be same for Cast");
38     return kTfLiteError;
39   }
40 
41   int tensor_id = inputs->data[0];
42   const auto& tensor = context->tensors[tensor_id];
43   int batch_size, height_size, width_size, depth_size;
44   GetDims(&batch_size, &height_size, &width_size, &depth_size, tensor.dims);
45   float min_value = 0;
46   float max_value = 0;
47   if (tensor.quantization.type ==
48       TfLiteQuantizationType::kTfLiteAffineQuantization) {
49     // Casting doesn't require min/max, so populate only if available.
50     TF_LITE_ENSURE_STATUS(
51         ComputeMinAndMaxQuantValues(tensor, &min_value, &max_value));
52   }
53   auto* min_const = graph_builder_->AddConstNodeWithData(
54       kScalarShape, reinterpret_cast<char*>(&min_value), sizeof(min_value));
55   auto* max_const = graph_builder_->AddConstNodeWithData(
56       kScalarShape, reinterpret_cast<char*>(&max_value), sizeof(max_value));
57 
58   AddInput(graph_builder_->GetHexagonTensorId(tensor_id));
59   AddInput(TensorID(min_const->GetID(), 0));
60   AddInput(TensorID(max_const->GetID(), 0));
61   node_output_ = AddOutput(sizeof(uint8_t), 4,
62                            {batch_size, height_size, width_size, depth_size});
63   AddOutput(sizeof(float), 4, {1, 1, 1, 1});
64   AddOutput(sizeof(float), 4, {1, 1, 1, 1});
65 
66   return kTfLiteOk;
67 }
68 
RegisterOutputs(const TfLiteIntArray * outputs,TfLiteContext * context)69 TfLiteStatus CastOpBuilder::RegisterOutputs(const TfLiteIntArray* outputs,
70                                             TfLiteContext* context) {
71   // Should be only 1 output.
72   // Cast tensor already exists in the graph, so we need to overwrite it with
73   // the new TensorID.
74   if (!graph_builder_->AddTensorWithID(outputs->data[0], node_output_.first,
75                                        node_output_.second,
76                                        /*overwrite*/ true)) {
77     TF_LITE_KERNEL_LOG(context, "Could not register Cast output.");
78     return kTfLiteError;
79   }
80 
81   return kTfLiteOk;
82 }
83 
~CastOpBuilder()84 CastOpBuilder::~CastOpBuilder() {}
85 
CreateCastBuilder(GraphBuilder * graph_builder,int op_type)86 OpBuilder* CreateCastBuilder(GraphBuilder* graph_builder, int op_type) {
87   return new CastOpBuilder(graph_builder, op_type);
88 }
89 
90 }  // namespace hexagon
91 }  // namespace delegates
92 }  // namespace tflite
93