tensorflow/tflite-micro: refactoring TFLM Kelvin kernel
This is the first version for refactoring TFLM Kelvin kernel.
Change-Id: Id61d215e4e6df8e3a09f4663cc5a7f23885f071f
diff --git a/tensorflow/lite/micro/kernels/kelvin/add.cc b/tensorflow/lite/micro/kernels/kelvin/add.cc
index b64c71f..9a647a0 100644
--- a/tensorflow/lite/micro/kernels/kelvin/add.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/add.cc
@@ -1,6 +1,18 @@
-// Copyright 2023 Google LLC
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include "tensorflow/lite/kernels/internal/reference/add.h"
@@ -68,7 +80,7 @@
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int32_t>(output));
} else {
- kelvin::opt::elementwise_add_s32(
+ kelvin::opt::ElementwiseAddS32(
tflite::micro::GetTensorData<int32_t>(input1),
tflite::micro::GetTensorData<int32_t>(input2),
tflite::micro::GetTensorData<int32_t>(output),
@@ -107,7 +119,7 @@
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int16_t>(output));
} else {
- kelvin::opt::elementwise_add_s16(
+ kelvin::opt::ElementwiseAddS16(
tflite::micro::GetTensorData<int16_t>(input1),
tflite::micro::GetTensorData<int16_t>(input2),
op_params.input1_offset, op_params.input1_multiplier,
@@ -150,7 +162,7 @@
tflite::micro::GetTensorShape(output),
tflite::micro::GetTensorData<int8_t>(output));
} else {
- kelvin::opt::elementwise_add_s8(
+ kelvin::opt::ElementwiseAddS8(
tflite::micro::GetTensorData<int8_t>(input1),
tflite::micro::GetTensorData<int8_t>(input2), op_params.input1_offset,
op_params.input1_multiplier, op_params.input1_shift,
diff --git a/tensorflow/lite/micro/kernels/kelvin/conv.cc b/tensorflow/lite/micro/kernels/kelvin/conv.cc
index 2e15dd7..d8fb8a1 100644
--- a/tensorflow/lite/micro/kernels/kelvin/conv.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/conv.cc
@@ -1,6 +1,18 @@
-// Copyright 2023 Google LLC
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include "tensorflow/lite/kernels/internal/reference/conv.h"
@@ -69,41 +81,39 @@
}
case kTfLiteInt16: {
const auto params_q = ConvParamsQuantized(params, data);
- bool opt =
- !(params_q.padding_values.width > 0 ||
- params_q.padding_values.height > 0 ||
- params_q.dilation_width_factor > 1 ||
- params_q.dilation_height_factor > 1);
+ bool opt = !(params_q.padding_values.width > 0 ||
+ params_q.padding_values.height > 0 ||
+ params_q.dilation_width_factor > 1 ||
+ params_q.dilation_height_factor > 1);
switch (bias->type) {
case kTfLiteInt32: {
- const auto fn = opt ? kelvin::opt::conv_per_channel_b32 : reference_integer_ops::ConvPerChannel<int32_t>;
- fn(
- params_q,
- data.per_channel_output_multiplier, data.per_channel_output_shift,
- tflite::micro::GetTensorShape(input),
- tflite::micro::GetTensorData<int16_t>(input),
- tflite::micro::GetTensorShape(filter),
- tflite::micro::GetTensorData<int8_t>(filter),
- tflite::micro::GetTensorShape(bias),
- tflite::micro::GetOptionalTensorData<std::int32_t>(bias),
- tflite::micro::GetTensorShape(output),
- tflite::micro::GetTensorData<int16_t>(output));
+ const auto fn = opt ? kelvin::opt::ConvS16B32
+ : reference_integer_ops::ConvPerChannel<int32_t>;
+ fn(params_q, data.per_channel_output_multiplier,
+ data.per_channel_output_shift,
+ tflite::micro::GetTensorShape(input),
+ tflite::micro::GetTensorData<int16_t>(input),
+ tflite::micro::GetTensorShape(filter),
+ tflite::micro::GetTensorData<int8_t>(filter),
+ tflite::micro::GetTensorShape(bias),
+ tflite::micro::GetOptionalTensorData<std::int32_t>(bias),
+ tflite::micro::GetTensorShape(output),
+ tflite::micro::GetTensorData<int16_t>(output));
break;
}
case kTfLiteInt64: {
- const auto fn = opt ? kelvin::opt::conv_per_channel_b64
+ const auto fn = opt ? kelvin::opt::ConvS16B64
: reference_integer_ops::ConvPerChannel<int64_t>;
- fn(
- params_q,
- data.per_channel_output_multiplier, data.per_channel_output_shift,
- tflite::micro::GetTensorShape(input),
- tflite::micro::GetTensorData<int16_t>(input),
- tflite::micro::GetTensorShape(filter),
- tflite::micro::GetTensorData<int8_t>(filter),
- tflite::micro::GetTensorShape(bias),
- tflite::micro::GetOptionalTensorData<std::int64_t>(bias),
- tflite::micro::GetTensorShape(output),
- tflite::micro::GetTensorData<int16_t>(output));
+ fn(params_q, data.per_channel_output_multiplier,
+ data.per_channel_output_shift,
+ tflite::micro::GetTensorShape(input),
+ tflite::micro::GetTensorData<int16_t>(input),
+ tflite::micro::GetTensorShape(filter),
+ tflite::micro::GetTensorData<int8_t>(filter),
+ tflite::micro::GetTensorShape(bias),
+ tflite::micro::GetOptionalTensorData<std::int64_t>(bias),
+ tflite::micro::GetTensorShape(output),
+ tflite::micro::GetTensorData<int16_t>(output));
break;
}
default:
@@ -136,7 +146,7 @@
}
case kTfLiteInt8: {
const auto params_q = ConvParamsQuantized(params, data);
- kelvin::opt::conv_per_channel_b8(
+ kelvin::opt::ConvS8(
params_q, data.per_channel_output_multiplier,
data.per_channel_output_shift,
tflite::micro::GetTensorShape(input),
diff --git a/tensorflow/lite/micro/kernels/kelvin/depthwise_conv.cc b/tensorflow/lite/micro/kernels/kelvin/depthwise_conv.cc
index 6e4a28d..f8d9307 100644
--- a/tensorflow/lite/micro/kernels/kelvin/depthwise_conv.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/depthwise_conv.cc
@@ -1,26 +1,27 @@
-/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-==============================================================================*/
-
-#include "tensorflow/lite/micro/kernels/depthwise_conv.h"
+#include "tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h"
#include "tensorflow/lite/c/builtin_op_data.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/portable_tensor_utils.h"
#include "tensorflow/lite/kernels/internal/reference/depthwiseconv_float.h"
-#include "tensorflow/lite/kernels/internal/reference/integer_ops/depthwise_conv.h"
#include "tensorflow/lite/kernels/kernel_util.h"
+#include "tensorflow/lite/micro/kernels/depthwise_conv.h"
#include "tensorflow/lite/micro/kernels/kernel_util.h"
#include "tensorflow/lite/micro/micro_log.h"
#include "tflm/opt/opt.h"
@@ -90,7 +91,7 @@
case kTfLiteInt8: {
tflite::DepthwiseParams dw_params =
DepthwiseConvParamsQuantized(params, data);
- kelvin::opt::DepthwiseConv2DKelvin(
+ kelvin::opt::DepthwiseConvS8(
dw_params, data.per_channel_output_multiplier,
data.per_channel_output_shift,
tflite::micro::GetTensorShape(input),
@@ -116,30 +117,7 @@
case kTfLiteInt8: {
tflite::DepthwiseParams dw_params =
DepthwiseConvParamsQuantized(params, data);
- tflite::RuntimeShape filter_shape =
- tflite::micro::GetTensorShape(filter);
- tflite::RuntimeShape input_shape =
- tflite::micro::GetTensorShape(input);
- if (dw_params.padding_type == PaddingType::kValid &&
- dw_params.stride_width == 1 && dw_params.stride_height == 1 &&
- filter_shape.Dims(1) == 1 && filter_shape.Dims(2) == 3 &&
- dw_params.dilation_height_factor == 1) {
- kelvin::opt::DepthwiseConv2DKelvinS16K3x1(
- tflite::micro::GetTensorData<int16_t>(input),
- tflite::micro::GetTensorData<int8_t>(filter),
- tflite::micro::GetOptionalTensorData<int64_t>(bias),
- filter_shape.Dims(3) /*channels*/,
- input_shape.Dims(2) /*frames*/,
- dw_params.dilation_width_factor /*dilation*/,
- data.per_channel_output_multiplier,
- data.per_channel_output_shift,
- dw_params.quantized_activation_min,
- dw_params.quantized_activation_max,
- tflite::micro::GetTensorData<int16_t>(output));
- break;
- }
-
- reference_integer_ops::DepthwiseConvPerChannel(
+ kelvin::opt::DepthwiseConvS16(
dw_params, data.per_channel_output_multiplier,
data.per_channel_output_shift,
tflite::micro::GetTensorShape(input),
diff --git a/tensorflow/lite/micro/kernels/kelvin/leaky_relu.cc b/tensorflow/lite/micro/kernels/kelvin/leaky_relu.cc
index 25a8853..2ca8a31 100644
--- a/tensorflow/lite/micro/kernels/kelvin/leaky_relu.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/leaky_relu.cc
@@ -1,6 +1,18 @@
-// Copyright 2023 Google LLC
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
#include "tensorflow/lite/kernels/internal/reference/leaky_relu.h"
@@ -41,7 +53,7 @@
return kTfLiteOk;
} break;
case kTfLiteInt8: {
- kelvin::opt::leaky_relu_s8(
+ kelvin::opt::LeakyReluS8(
tflite::micro::GetTensorData<int8_t>(input),
tflite::micro::GetTensorData<int8_t>(output),
MatchingFlatSize(tflite::micro::GetTensorShape(input),
@@ -52,7 +64,7 @@
return kTfLiteOk;
} break;
case kTfLiteInt16: {
- kelvin::opt::leaky_relu_s16(
+ kelvin::opt::LeakyReluS16(
tflite::micro::GetTensorData<int16_t>(input),
tflite::micro::GetTensorData<int16_t>(output),
MatchingFlatSize(tflite::micro::GetTensorShape(input),
diff --git a/tensorflow/lite/micro/kernels/kelvin/pooling.cc b/tensorflow/lite/micro/kernels/kelvin/pooling.cc
index 47f1362..d343a55 100644
--- a/tensorflow/lite/micro/kernels/kelvin/pooling.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/pooling.cc
@@ -1,17 +1,19 @@
-/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-==============================================================================*/
#include "tensorflow/lite/kernels/internal/reference/pooling.h"
#include "tensorflow/lite/c/builtin_op_data.h"
@@ -86,9 +88,9 @@
op_params.padding_values.width = data->padding.width;
op_params.quantized_activation_min = data->activation_min;
op_params.quantized_activation_max = data->activation_max;
- kelvin::opt::MaxPoolGeneric(op_params, tflite::micro::GetTensorShape(input),
- input->data.int8, tflite::micro::GetTensorShape(output),
- output->data.int8);
+ kelvin::opt::MaxPoolS8(
+ op_params, tflite::micro::GetTensorShape(input), input->data.int8,
+ tflite::micro::GetTensorShape(output), output->data.int8);
break;
case kTfLiteInt16:
MaxPoolingEvalQuantized<int16_t>(context, node, params, data, input,
diff --git a/tensorflow/lite/micro/kernels/kelvin/reshape.cc b/tensorflow/lite/micro/kernels/kelvin/reshape.cc
index 9c88385..76e3e52 100644
--- a/tensorflow/lite/micro/kernels/kelvin/reshape.cc
+++ b/tensorflow/lite/micro/kernels/kelvin/reshape.cc
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/kernel_util.h"
#include "tensorflow/lite/micro/kernels/kernel_util.h"
@@ -86,7 +102,7 @@
// Do nothing for in-place reshape.
if (input->data.raw != output->data.raw) {
// Otherwise perform reshape with copy.
- kelvin::opt::memcpy(output->data.raw, input->data.raw, input_bytes);
+ kelvin::opt::Memcpy(output->data.raw, input->data.raw, input_bytes);
}
return kTfLiteOk;
}