forked from ARM-software/CMSIS-NN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add int8 padding operator and unit tests
- Loading branch information
1 parent
5f8f1a9
commit 25eb94f
Showing
22 changed files
with
532 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# | ||
# SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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 | ||
# | ||
# 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. | ||
# | ||
|
||
file(GLOB SRC "./*_s8.c") | ||
target_sources(cmsis-nn PRIVATE ${SRC}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
/* | ||
* SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
/* ---------------------------------------------------------------------- | ||
* Project: CMSIS NN Library | ||
* Title: arm_pad_s8.c | ||
* Description: Pad a s8 vector | ||
* | ||
* $Date: 19 Sep 2024 | ||
* $Revision: V.1.0.0 | ||
* | ||
* Target : Arm(R) M-Profile Architecture | ||
* | ||
* -------------------------------------------------------------------- */ | ||
|
||
#include "arm_nn_types.h" | ||
#include "arm_nnfunctions.h" | ||
#include "arm_nnsupportfunctions.h" | ||
/** | ||
* @ingroup Public | ||
*/ | ||
|
||
/** | ||
* @addtogroup Pad | ||
* @{ | ||
*/ | ||
|
||
/* | ||
* Basic s8 pad function. | ||
* | ||
* Refer header file for details. | ||
* | ||
*/ | ||
|
||
arm_cmsis_nn_status arm_pad_s8(const int8_t *input, | ||
int8_t *output, | ||
const int8_t pad_value, | ||
const cmsis_nn_dims *input_size, | ||
const cmsis_nn_dims *pre_pad, | ||
const cmsis_nn_dims *post_pad) | ||
{ | ||
|
||
const cmsis_nn_dims output_size = {pre_pad->n + input_size->n + post_pad->n, | ||
pre_pad->h + input_size->h + post_pad->h, | ||
pre_pad->w + input_size->w + post_pad->w, | ||
pre_pad->c + input_size->c + post_pad->c}; | ||
|
||
const int32_t batch_block_size = output_size.h * output_size.w * output_size.c; | ||
const int32_t row_block_size = output_size.w * output_size.c; | ||
const int32_t col_block_size = output_size.c; | ||
|
||
arm_memset_s8(output, pad_value, batch_block_size * pre_pad->n); | ||
output += batch_block_size * pre_pad->n; | ||
for (int32_t b = 0; b < input_size->n; b++) | ||
{ | ||
|
||
arm_memset_s8(output, pad_value, row_block_size * pre_pad->h); | ||
output += row_block_size * pre_pad->h; | ||
for (int32_t y = 0; y < input_size->h; y++) | ||
{ | ||
|
||
arm_memset_s8(output, pad_value, col_block_size * pre_pad->w); | ||
output += col_block_size * pre_pad->w; | ||
if (input_size->c == output_size.c) | ||
{ | ||
arm_memcpy_s8(output, input, input_size->w * input_size->c); | ||
output += input_size->w * input_size->c; | ||
input += input_size->w * input_size->c; | ||
} | ||
else | ||
{ | ||
for (int32_t x = 0; x < input_size->w; x++) | ||
{ | ||
|
||
arm_memset_s8(output, pad_value, pre_pad->c); | ||
output += pre_pad->c; | ||
|
||
arm_memcpy_s8(output, input, input_size->c); | ||
output += input_size->c; | ||
input += input_size->c; | ||
|
||
arm_memset_s8(output, pad_value, post_pad->c); | ||
output += post_pad->c; | ||
} | ||
} | ||
|
||
arm_memset_s8(output, pad_value, col_block_size * post_pad->w); | ||
output += col_block_size * post_pad->w; | ||
} | ||
|
||
arm_memset_s8(output, pad_value, row_block_size * post_pad->h); | ||
output += row_block_size * post_pad->h; | ||
} | ||
arm_memset_s8(output, pad_value, batch_block_size * post_pad->n); | ||
|
||
return ARM_CMSIS_NN_SUCCESS; | ||
} | ||
|
||
/** | ||
* @} end of Pad group | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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 | ||
# | ||
# 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. | ||
# | ||
import Lib.op_utils | ||
import tensorflow as tf | ||
import math | ||
import numpy as np | ||
|
||
from tensorflow.lite.python.interpreter import Interpreter | ||
from tensorflow.lite.python.interpreter import OpResolverType | ||
import tf_keras as keras | ||
|
||
class Op_pad(Lib.op_utils.Op_type): | ||
|
||
def get_shapes(params): | ||
shapes = {} | ||
shapes["input_tensor"] = (params["input_n"], params["input_h"], params["input_w"], params["input_c"]) | ||
shapes["representational_dataset"] = shapes["input_tensor"] | ||
|
||
return shapes | ||
|
||
def generate_keras_model(shapes, params): | ||
|
||
model = keras.models.Sequential() | ||
model.add(keras.layers.InputLayer(input_shape=shapes["input_tensor"][1:])) | ||
|
||
if (params["pre_pad_n"] == params["post_pad_n"] == params["pre_pad_h"] == params["post_pad_h"] == 0): | ||
model.add(keras.layers.ZeroPadding2D(padding=((params["pre_pad_w"], params["post_pad_w"]), (params["pre_pad_c"], params["post_pad_c"])), data_format="channels_first")) | ||
elif (params["pre_pad_n"] == params["post_pad_n"] == params["pre_pad_c"] == params["post_pad_c"] == 0): | ||
model.add(keras.layers.ZeroPadding2D(padding=((params["pre_pad_h"], params["post_pad_h"]), (params["pre_pad_w"], params["post_pad_w"])), data_format="channels_last")) | ||
else: | ||
raise ValueError(f"Keras can only generate padding for (h,w) or (w,c), the others must be zero.") | ||
|
||
return model | ||
|
||
def generate_data_tflite(tflite_fname, params): | ||
tensors = {} | ||
effective_scales = {} | ||
scales = {} | ||
generated_params = {} | ||
|
||
generated_params["pad_value"] = -128 | ||
|
||
interpreter = Interpreter(str(tflite_fname), experimental_op_resolver_type=OpResolverType.BUILTIN_REF) | ||
interpreter.allocate_tensors() | ||
|
||
output_details = interpreter.get_output_details() | ||
output_n = output_details[0]['shape'][3] | ||
output_h = output_details[0]['shape'][2] | ||
output_w = output_details[0]['shape'][1] | ||
output_c = output_details[0]['shape'][0] | ||
|
||
generated_params["output_size"] = output_n * output_h * output_w * output_c; | ||
|
||
return Lib.op_utils.Generated_data(generated_params, tensors, scales, effective_scales) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.