Skip to content

Commit

Permalink
Add timvx op test && Fix timvx_gather op (#756)
Browse files Browse the repository at this point in the history
* Update CMakeLists.txt

* Add files via upload

* Update deconvolution.c
  • Loading branch information
BowShotDS authored Jun 21, 2021
1 parent 71de424 commit be9ffe3
Show file tree
Hide file tree
Showing 22 changed files with 3,438 additions and 18 deletions.
20 changes: 18 additions & 2 deletions source/device/tim-vx/op/timvx_gather.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

extern "C"
{
#include <malloc.h>
#include "operator/op.h"
#include "gather_param.h"
}
Expand All @@ -40,9 +41,24 @@ bool VXEngine::AddGatherNode(struct node* ir_node)

struct gather_param* param = (struct gather_param*)ir_node->op.param_mem;

auto gather = graph->CreateOperation<tim::vx::ops::Gather>(param->axis);
auto gather = graph->CreateOperation<tim::vx::ops::Gather>(3 - param->axis);
vx_node_map[ir_node->index] = gather;

auto Dims = (unsigned int*)input_tensor->dims;
tim::vx::ShapeType vx_shape;
vx_shape.push_back(param->indices_num);
tim::vx::Quantization none_quant(tim::vx::QuantType::NONE, 1, 0);
tim::vx::TensorSpec vx_spec(tim::vx::DataType::INT32, vx_shape,
tim::vx::TensorAttribute::CONSTANT, none_quant);

int* data_indices = (int*)malloc(param->indices_num * sizeof(int) ) ;
for (int i = 0; i < param->indices_num; i++)
data_indices[i] = i;
std::shared_ptr<tim::vx::Tensor> vx_tensor = this->graph->CreateTensor(vx_spec, data_indices);
this->vx_tensor_map[ir_node->input_tensors[0] + ir_graph->tensor_num] = vx_tensor;

(*gather)
.BindInputs({ this->vx_tensor_map[input_tensor->index] })
.BindInputs({ this->vx_tensor_map[input_tensor->index], vx_tensor })
.BindOutputs({ this->vx_tensor_map[output_tensor->index] });

return true;
Expand Down
2 changes: 1 addition & 1 deletion source/device/tim-vx/timvx_limit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const int timvx_supported_ops[] = {
// OP_EXPANDDIMS,
OP_FC,
OP_FLATTEN,
// OP_GATHER,
OP_GATHER,
// OP_GEMM,
// OP_GRU,
// OP_HARDSIGMOID,
Expand Down
49 changes: 34 additions & 15 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ FILE (COPY ${CMAKE_SOURCE_DIR}/source/api/c_api_ex.h DESTINATION ${CMAKE_CURRENT

if (TENGINE_ENABLE_TIM_VX)
function (tengine_timvx_op_test name file)
add_executable (${name} ${CMAKE_CURRENT_SOURCE_DIR}/${file} "${PROJECT_SOURCE_DIR}/tests/common/tengine_operations.c")
file(GLOB TENGINE_UTIL_SOURCE_FILES ${PROJECT_SOURCE_DIR}/tests/common/util/*.c)

add_executable (${name} ${CMAKE_CURRENT_SOURCE_DIR}/${file} "${TENGINE_UTIL_SOURCE_FILES}" "${PROJECT_SOURCE_DIR}/tests/common/tengine_operations.c")

target_link_libraries (${name} PRIVATE ${CMAKE_PROJECT_NAME})

Expand All @@ -15,6 +17,7 @@ if (TENGINE_ENABLE_TIM_VX)
target_include_directories (${name} PRIVATE "${PROJECT_BINARY_DIR}")
target_include_directories (${name} PRIVATE "${PROJECT_BINARY_DIR}/source")
target_include_directories (${name} PRIVATE "${PROJECT_SOURCE_DIR}/tests/common")
target_include_directories (${name} PRIVATE "${PROJECT_SOURCE_DIR}/tests/common/util")

if (${TENGINE_TARGET_PROCESSOR} MATCHES "ARM" AND (NOT ANDROID AND NOT OHOS) AND TENGINE_TARGET_PROCESSOR_32Bit)
target_compile_options (${name} PRIVATE "-mfp16-format=ieee")
Expand All @@ -23,20 +26,36 @@ if (TENGINE_ENABLE_TIM_VX)
add_test (${name} ${name})
endfunction()

tengine_timvx_op_test(test_timvx_op_clip op/test_timvx_op_clip.cpp)
tengine_timvx_op_test(test_timvx_op_convolution op/test_timvx_op_convolution.cpp)
tengine_timvx_op_test(test_timvx_op_dropout op/test_timvx_op_dropout.cpp)
tengine_timvx_op_test(test_timvx_op_elu op/test_timvx_op_elu.cpp)
tengine_timvx_op_test(test_timvx_op_hardswish op/test_timvx_op_hardswish.cpp)
tengine_timvx_op_test(test_timvx_op_leakyrelu op/test_timvx_op_leakyrelu.cpp)
tengine_timvx_op_test(test_timvx_op_mish op/test_timvx_op_mish.cpp)
tengine_timvx_op_test(test_timvx_op_pooling op/test_timvx_op_pooling.cpp)
tengine_timvx_op_test(test_timvx_op_prelu op/test_timvx_op_prelu.cpp)
tengine_timvx_op_test(test_timvx_op_relu op/test_timvx_op_relu.cpp)
# tengine_timvx_op_test(test_timvx_op_relu1 op/test_timvx_op_relu1.cpp)
tengine_timvx_op_test(test_timvx_op_sigmoid op/test_timvx_op_sigmoid.cpp)
tengine_timvx_op_test(test_timvx_op_tanh op/test_timvx_op_tanh.cpp)

tengine_timvx_op_test(test_timvx_op_clip op/test_timvx_op_clip.cpp)
tengine_timvx_op_test(test_timvx_op_concat op/test_timvx_op_concat.cpp)
tengine_timvx_op_test(test_timvx_op_convolution op/test_timvx_op_convolution.cpp)
tengine_timvx_op_test(test_timvx_op_deconv op/test_timvx_op_deconv.cpp)
tengine_timvx_op_test(test_timvx_op_dropout op/test_timvx_op_dropout.cpp)
tengine_timvx_op_test(test_timvx_op_eltwise_mul op/test_timvx_op_eltwise_mul.cpp)
tengine_timvx_op_test(test_timvx_op_eltwise_sum op/test_timvx_op_eltwise_sum.cpp)
tengine_timvx_op_test(test_timvx_op_elu op/test_timvx_op_elu.cpp)
tengine_timvx_op_test(test_timvx_op_fc op/test_timvx_op_fc.cpp)
tengine_timvx_op_test(test_timvx_op_flatten op/test_timvx_op_flatten.cpp)
tengine_timvx_op_test(test_timvx_op_gather op/test_timvx_op_gather.cpp)
tengine_timvx_op_test(test_timvx_op_hardswish op/test_timvx_op_hardswish.cpp)
tengine_timvx_op_test(test_timvx_op_interp op/test_timvx_op_interp.cpp)
tengine_timvx_op_test(test_timvx_op_leakyrelu op/test_timvx_op_leakyrelu.cpp)
tengine_timvx_op_test(test_timvx_op_mish op/test_timvx_op_mish.cpp)
tengine_timvx_op_test(test_timvx_op_permute op/test_timvx_op_permute.cpp)
tengine_timvx_op_test(test_timvx_op_pooling op/test_timvx_op_pooling.cpp)
tengine_timvx_op_test(test_timvx_op_prelu op/test_timvx_op_prelu.cpp)
tengine_timvx_op_test(test_timvx_op_relu op/test_timvx_op_relu.cpp)
# tengine_timvx_op_test(test_timvx_op_relu1 op/test_timvx_op_relu1.cpp)
tengine_timvx_op_test(test_timvx_op_reshape op/test_timvx_op_reshape.cpp)
tengine_timvx_op_test(test_timvx_op_resize op/test_timvx_op_resize.cpp)
tengine_timvx_op_test(test_timvx_op_sigmoid op/test_timvx_op_sigmoid.cpp)
tengine_timvx_op_test(test_timvx_op_slice op/test_timvx_op_slice.cpp)
tengine_timvx_op_test(test_timvx_op_softmax op/test_timvx_op_softmax.cpp)
tengine_timvx_op_test(test_timvx_op_split op/test_timvx_op_split.cpp)
tengine_timvx_op_test(test_timvx_op_tanh op/test_timvx_op_tanh.cpp)
tengine_timvx_op_test(test_timvx_op_upsampling op/test_timvx_op_upsampling.cpp)


# timvx model test, need opencv
FIND_PACKAGE(OpenCV QUIET)
IF (OpenCV_FOUND)
Expand Down
91 changes: 91 additions & 0 deletions tests/common/util/mathp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/*
* Copyright (c) 2021, OPEN AI LAB
* Author: [email protected]
* Revised: [email protected]
*/

#include "mathp.h"

#include <stdlib.h>


int imin(int a, int b)
{
return a <= b ? a : b;
}


int imax(int a, int b)
{
return a >= b ? a : b;
}


int min_abs(int a, int b)
{
return imin(abs(a), abs(b));
}


int max_abs(int a, int b)
{
return imax(abs(a), abs(b));
}


static int solve_gcd(int large, int small)
{
int val = large % small;
return 0 == val ? small : gcd(small, val);
}


int gcd(int a, int b)
{
if (0 == a || 0 == b)
return 0;

return solve_gcd(max_abs(a, b), min_abs(a, b));
}


int lcm(int a, int b)
{
if (0 == a || 0 == b)
return 0;

return abs(a * b) / solve_gcd(max_abs(a, b), min_abs(a, b));
}


int align(int value, int step)
{
const int mask = ~(abs(step) - 1);
return (value + step) & mask;
}


void* align_address(void* address, int step)
{
const size_t mask = ~(abs(step) - 1);
return (void*)((size_t)address & mask);
}
114 changes: 114 additions & 0 deletions tests/common/util/mathp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/*
* Copyright (c) 2021, OPEN AI LAB
* Author: [email protected]
* Revised: [email protected]
*/

#pragma once


/*!
* @brief Solve min value
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved min value
*/
int imin(int a, int b);


/*!
* @brief Solve max value
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved max value
*/
int imax(int a, int b);


/*!
* @brief Solve min absolute value
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved min absolute value
*/
int min_abs(int a, int b);


/*!
* @brief Solve max absolute value
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved max absolute value
*/
int max_abs(int a, int b);


/*!
* @brief Solve greatest common divisor
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved GCD
*/
int gcd(int a, int b);


/*!
* @brief Solve lowest common multiple
*
* @param [in] a: number a
* @param [in] b: number b
*
* @return The solved LCM
*/
int lcm(int a, int b);


/*!
* @brief Solve min aligned value with the step length
*
* @param [in] value: number which may not be aligned
* @param [in] step: align step
*
* @return The solved aligned value
*/
int align(int value, int step);


/*!
* @brief Get aligned pointer
*
* @param [in] value: pointer which may not be aligned
* @param [in] step: align step
*
* @return The solved aligned pointer
*/
void* align_address(void* address, int step);
Loading

0 comments on commit be9ffe3

Please sign in to comment.