Skip to content

Commit

Permalink
svdf s8: Fix state data overwrite (#59)
Browse files Browse the repository at this point in the history
Adds unit test to verify fix as well.

Co-authored-by: Måns Nilsson <mans.nilsson@arm.com>
  • Loading branch information
felix-johnny and mansnils authored May 8, 2023
1 parent ac1db62 commit 61d1bb6
Show file tree
Hide file tree
Showing 14 changed files with 494 additions and 16 deletions.
20 changes: 10 additions & 10 deletions Source/NNSupportFunctions/arm_nn_vec_mat_mult_t_s8.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
* Title: arm_nn_vec_mat_mult_t_s8
* Description: s8 vector by matrix (transposed) multiplication
*
* $Date: 27 March 2023
* $Revision: V.5.4.0
* $Date: 5 May 2023
* $Revision: V.5.4.1
*
* Target : Arm(R) M-Profile Architecture
*
Expand Down Expand Up @@ -89,6 +89,12 @@ arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s8(const int8_t *lhs,
int32_t rhs_sum_0 = 0;
int32_t rhs_sum_1 = 0;
int32_t rhs_sum_2 = 0;
if (bias)
{
acc_0 = *bias++;
acc_1 = *bias++;
acc_2 = *bias++;
}

uint32_t col_cnt = (uint32_t)rhs_cols;

Expand Down Expand Up @@ -119,13 +125,6 @@ arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s8(const int8_t *lhs,
rhs += 3 * rhs_cols;

int32x4_t acc = {acc_0, acc_1, acc_2, 0};
mve_pred16_t p = vctp32q(3);
if (bias)
{
int32x4_t b = vldrwq_z_s32(bias, p);
acc = vaddq_x_s32(acc, b, p);
bias += 3;
}
const int32x4_t rhs_sum = {rhs_sum_0, rhs_sum_1, rhs_sum_2, 0};
acc += vdupq_n_s32(lhs_offset) * rhs_sum;

Expand All @@ -134,9 +133,10 @@ arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s8(const int8_t *lhs,
acc = vmaxq_s32(acc, vdupq_n_s32(activation_min));
acc = vminq_s32(acc, vdupq_n_s32(activation_max));

const mve_pred16_t p = vctp32q(3);
if (address_offset > 1L)
{
vstrbq_scatter_offset_s32(dst, address_offset_array, acc);
vstrbq_scatter_offset_p_s32(dst, address_offset_array, acc, p);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions Tests/UnitTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ add_subdirectory(TestCases/test_arm_max_pool_s8)
add_subdirectory(TestCases/test_arm_softmax_s16)
add_subdirectory(TestCases/test_arm_softmax_s8)
add_subdirectory(TestCases/test_arm_softmax_s8_s16)
add_subdirectory(TestCases/test_arm_svdf_s8)
add_subdirectory(TestCases/test_arm_svdf_state_s16_s8)
add_subdirectory(TestCases/test_arm_ds_cnn_l_s8)
add_subdirectory(TestCases/test_arm_ds_cnn_s_s8)
Expand Down
174 changes: 174 additions & 0 deletions Tests/UnitTest/TestCases/Common/svdf_s8_weights_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"version": 3,
"operator_codes": [
{
"deprecated_builtin_code": 27,
"version": 1
}
],
"subgraphs": [
{
"tensors": [
{
"shape": [
batches,
input_size
],
"type": "INT8",
"buffer": 0,
"name": "tensor_input",
"quantization": {
"scale": [
input_scale
],
"zero_point": [
input_zp
],
"quantized_dimension": 0
},
"is_variable": false
},
{
"shape": [
number_filters,
input_size
],
"type": "INT8",
"buffer": 1,
"name": "tensor_weight_1",
"quantization": {
"scale": [
w_1_scale
],
"zero_point": [
w_1_zp
],
"quantized_dimension": 0
},
"is_variable": false
},
{
"shape": [
number_filters,
memory_size
],
"type": "INT8",
"buffer": 2,
"name": "tensor_weight_2",
"quantization": {
"scale": [
w_2_scale
],
"zero_point": [
w_2_zp
],
"quantized_dimension": 0
},
"is_variable": false
},
{
"shape": [
number_units
],
"type": "INT32",
"buffer": 3,
"name": "tensor_bias",
"quantization": {
"scale": [
bias_scale
],
"zero_point": [
bias_zp
],
"quantized_dimension": 0
},
"is_variable": false
},
{
"shape": [
batches,
memory_sizeXnumber_filters
],
"type": "INT8",
"buffer": 4,
"name": "tensor_state",
"quantization": {
"scale": [
state_scale
],
"zero_point": [
state_zp
],
"quantized_dimension": 0
},
"is_variable": true
},
{
"shape": [
batches,
number_units
],
"type": "INT8",
"buffer": 5,
"name": "tensor_output",
"quantization": {
"scale": [
output_scale
],
"zero_point": [
output_zp
],
"quantized_dimension": 0
},
"is_variable": false
}
],
"inputs": [
0
],
"outputs": [
5
],
"operators": [
{
"opcode_index": 0,
"inputs": [
0,
1,
2,
3,
4
],
"outputs": [
5
],
"builtin_options_type": "SVDFOptions",
"builtin_options": {
"rank": rank_value,
"fused_activation_function": "RELU"
},
"custom_options_format": "FLEXBUFFERS"
}
]
}
],
"description": "CMSIS-NN unit test model",
"buffers": [
{},
{
"data": []
},
{
"data": []
},
{
"data": []
},
{
"data": []
},
{
"data": []
}
]
}
6 changes: 6 additions & 0 deletions Tests/UnitTest/TestCases/TestData/svdf_int8/biases_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#include <stdint.h>

const int32_t svdf_int8_biases[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
19 changes: 19 additions & 0 deletions Tests/UnitTest/TestCases/TestData/svdf_int8/config_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#define SVDF_INT8_MULTIPLIER_IN 1717987072
#define SVDF_INT8_MULTIPLIER_OUT 1099511552
#define SVDF_INT8_SHIFT_1 -3
#define SVDF_INT8_SHIFT_2 -11
#define SVDF_INT8_IN_ACTIVATION_MIN -32768
#define SVDF_INT8_IN_ACTIVATION_MAX 32767
#define SVDF_INT8_RANK 1
#define SVDF_INT8_FEATURE_BATCHES 12
#define SVDF_INT8_TIME_BATCHES 2
#define SVDF_INT8_INPUT_SIZE 20
#define SVDF_INT8_DST_SIZE 12
#define SVDF_INT8_OUT_ACTIVATION_MIN -128
#define SVDF_INT8_OUT_ACTIVATION_MAX 127
#define SVDF_INT8_INPUT_BATCHES 1
#define SVDF_INT8_INPUT_OFFSET 0
#define SVDF_INT8_OUTPUT_OFFSET 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#include <stdint.h>

const int8_t svdf_int8_input_sequence[40] = {-23, 123, 104, 88, -47, 92, -14, -14, -90, -113, -94, -46, 121, -125,
-100, -59, 69, 19, -80, 24, 86, 37, 8, 116, 113, -45, 116, 41,
-1, -103, 37, 103, 88, -86, 28, 30, -121, 4, -83, -109};
6 changes: 6 additions & 0 deletions Tests/UnitTest/TestCases/TestData/svdf_int8/state_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#include <stdint.h>

const int8_t svdf_int8_state[24] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
8 changes: 8 additions & 0 deletions Tests/UnitTest/TestCases/TestData/svdf_int8/test_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#include "biases_data.h"
#include "config_data.h"
#include "input_sequence_data.h"
#include "state_data.h"
#include "weights_feature_data.h"
#include "weights_time_data.h"
18 changes: 18 additions & 0 deletions Tests/UnitTest/TestCases/TestData/svdf_int8/weights_feature_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#include <stdint.h>

const int8_t svdf_int8_weights_feature[240] = {
55, 27, 100, 94, 12, -10, 99, 108, 30, -39, 5, 56, 13, -100, -15, -81, 71, -26, 9, 56,
-125, -19, -102, -2, -88, -78, -37, -4, -127, -39, -104, 45, 84, 97, -30, 109, 26, -122, 56, 46,
-122, 3, 80, -53, -66, -78, 22, -81, 40, 8, 15, -44, -72, -128, -55, 34, -95, 21, -65, 80,
94, -38, 33, 58, -62, 106, 111, 0, 75, -122, -116, 77, 56, 72, 113, 37, -88, 13, -6, 10,
-48, 29, -62, -98, 13, 78, -86, 6, 49, 35, -70, -6, 102, 55, 31, -90, 75, -98, 104, 25,
61, -95, 2, -74, 32, -93, -47, 102, -82, -48, -81, 55, -121, -63, 39, -50, 64, 46, 123, -126,
50, 28, -9, -31, -87, 47, -8, -65, 98, 103, -41, 22, 14, 75, -36, -36, 28, -89, -110, 107,
93, -71, 5, -54, 77, 105, 102, -115, -79, -31, -114, -47, -65, 44, -55, 61, 56, -35, -97, -12,
116, 30, -28, -80, -49, 93, 108, -23, 57, 125, 56, 61, -108, 109, 2, -80, -94, -95, -67, -4,
71, -64, 122, -127, 123, 86, -12, 27, 109, 93, -2, -30, -107, 117, -120, 41, -33, -7, -57, 78,
6, -31, 108, 79, 86, -16, 70, -93, -47, 93, -54, 60, -85, 116, -69, -28, -120, -29, 7, -79,
-89, -68, 25, 48, 19, 89, 50, 31, -105, -69, -26, -2, 116, -76, 62, -26, -9, 10, 102, 24};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated by generate_test_data.py using tensorflow version 2.10.0 (Keras version 2.10.0).
// Interpreter from tensorflow version 2.10.0 and revision v2.10.0-rc3-6-g359c3cdfc5f.
#pragma once
#include <stdint.h>

const int8_t svdf_int8_weights_time[24] = {30, 32, -33, 59, -53, -32, -45, -12, -119, 87, 34, -114,
-92, -45, -90, 114, -68, -116, -8, -10, 18, -55, 100, 1};
23 changes: 23 additions & 0 deletions Tests/UnitTest/TestCases/test_arm_svdf_s8/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Copyright (C) 2023 Arm Limited or its affiliates.
#
# 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.
#

add_cmsis_nn_unit_test_executable(test_arm_svdf_s8)

target_sources(test_arm_svdf_s8 PRIVATE
Unity/unity_test_arm_svdf_s8.c
Unity/TestRunner/unity_test_arm_svdf_s8_runner.c)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: Copyright 2023 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
* distributd 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 <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../test_arm_svdf_s8.c"
#include "unity.h"

#ifdef USING_FVP_CORSTONE_300
extern void uart_init(void);
#endif

/* This function is called from the autogenerated file.
* The name must be exactly like this
*/
void setUp(void)
{ /* This is run before EACH TEST */
#ifdef USING_FVP_CORSTONE_300
uart_init();
#endif
}

/* This function is called from the autogenerated file.
* The name must be exactly like this
*/
void tearDown(void) {}

void test_svdf_int8_arm_s8(void) { svdf_int8_arm_svdf_s8(); }
Loading

0 comments on commit 61d1bb6

Please sign in to comment.