Skip to content

Commit

Permalink
add custom op in lenet test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouheng.zheng authored and sunshinemyson committed Oct 28, 2022
1 parent fde6d79 commit 25c38d4
Show file tree
Hide file tree
Showing 7 changed files with 36,620 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory("benchmark_test")
if(${TIM_VX_ENABLE_CUSTOM_OP})
add_subdirectory("custom_op_test")
add_subdirectory("custom_lenet")
endif()

add_subdirectory("lenet")
Expand Down
13 changes: 13 additions & 0 deletions samples/custom_lenet/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cc_test(
name = "lenet_asymu8_cc",
copts = [
"-Werror", "-std=c++14"
],
srcs = [
"lenet_asymu8.cc",
"lenet_asymu8_weights.h"
],
deps = [
"//:tim-vx_interface"
],
)
14 changes: 14 additions & 0 deletions samples/custom_lenet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message("samples/custom_lenet")

set(TARGET_NAME "custom_lenet")

aux_source_directory(. ${TARGET_NAME}_SRCS)
add_executable(${TARGET_NAME} ${${TARGET_NAME}_SRCS})

target_link_libraries(${TARGET_NAME} PRIVATE -Wl,--whole-archive tim-vx)
target_include_directories(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
)
install(TARGETS ${TARGET_NAME} ${TARGET_NAME}
DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
36 changes: 36 additions & 0 deletions samples/custom_lenet/custom_softmax.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/****************************************************************************
*
* Copyright (c) 2021 Vivante Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include "custom_softmax.h"

namespace tim {
namespace vx {
namespace ops {

const char* CustomSoftmax::kernel_name_ = "xxxx_name123456";
int32_t CustomSoftmax::kernel_id_ = -1 * (++gobal_kernel_id_);


} // namespace ops
} // namespace vx
} // namespace tim
141 changes: 141 additions & 0 deletions samples/custom_lenet/custom_softmax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/****************************************************************************
*
* Copyright (c) 2021 Vivante Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/

#ifndef TIM_VX_OPS_CUSTOM_SOFTMAX_H_
#define TIM_VX_OPS_CUSTOM_SOFTMAX_H_

#include "tim/vx/ops/custom_base.h"

namespace tim {
namespace vx {
namespace ops {



class CustomSoftmax : public CustomOpBase {
public:
//scalar param for kernel function input
using ParamTuple = std::tuple< int, /* sf_size */
int, /* zp_in */
float /* scale_in */
>;
CustomSoftmax(Graph* graph, ParamTuple tuple_list, uint32_t input_num = 1, uint32_t output_num = 1)
: CustomOpBase(graph, input_num, output_num, CustomSoftmax::kernel_id_,CustomSoftmax::kernel_name_) {
tuple_list_.swap(tuple_list);
param_transform(tuple_list_, param_list_);

kernel_resource_ =
"__kernel void softmax_U8toF32_2D(\n\
__read_only image2d_t input,\n\
__write_only image2d_t output,\n\
int sf_size,\n\
int zp_in,\n\
float scale_in\n\
)\n\
{\n\
#define F_MAX(a,b) ((a)>(b)?(a):(b)) \n\
int2 coord_in = (int2)(0,0); \n\
float fMax = -99999999999.0; \n\
short sVal;\n\
for (int i = 0; i < sf_size; i++)\n\
{\n\
uchar val;\n\
coord_in.x = i;\n\
val = read_imageui(input, coord_in).x; \n\
float fval = (float)(val - zp_in) *scale_in; \n\
fMax = F_MAX(fMax, fval);\n\
write_imagef(output, coord_in, (float)fval); \n\
}\n\
float fProbSum = 0.0f;\n\
for (int i = 0; i < sf_size; i++)\n\
{\n\
float fval;\n\
\n\
coord_in.x = i;\n\
fval = read_imagef(output, coord_in).x; \n\
float fOut = (float)exp(fval - fMax); \n\
fProbSum += fOut; \n\
write_imagef(output, coord_in, fOut); \n\
}\n\
for (int i = 0; i < sf_size; i++)\n\
{\n\
coord_in.x = i;\n\
float fval = read_imagef(output, coord_in).x;\n\
float fOut =fval/fProbSum; \n\
write_imagef(output, coord_in, fOut); \n\
}\n\
\n\
}\n\
\n\
";
};

protected:
const char* kernel_softmax = "softmax_U8toF32_2D";
ParamTuple tuple_list_;
bool trans_a_;
bool trans_b_;
static const char* kernel_name_;
static int32_t kernel_id_;

//function for setup output
void SetupShapeInfor() override {
outputs_size_[0].push_back(inputs_size_[0][0]);
}

//function for kernel select and build option
void SetupParams(
std::vector<tim::vx::DataType> input_types,
std::string& build_option) override {
if(input_types.size()==0){
std::cout<<"something wrong"<<std::endl;
return;
}
func_name_ = kernel_softmax;
build_option = "";
}

//function for kernel local size and gobal size
void SetupEnqueue(uint32_t& dim, std::vector<size_t>& global_size,
std::vector<size_t>& local_size) {
dim = 1;
local_size[0] = 0;
local_size[1] = 0;
local_size[2] = 0;

global_size[0] = 1;
global_size[1] = 1;
global_size[2] = 1;
}

std::shared_ptr<Operation> Clone(
std::shared_ptr<Graph>& graph) const override {
return graph->CreateOperation<CustomSoftmax>(this->tuple_list_, this->input_num_, this->output_num_);
}
};

} // namespace ops
} // namespace vx
} // namespace tim
#endif
Loading

0 comments on commit 25c38d4

Please sign in to comment.