diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 522883d328..65128f7fd3 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -72,6 +72,7 @@ add_compile_definitions(NO_DLIB) add_compile_options(-fPIC) if(SUPPORT_TRAIN) + set(BUILD_MINDDATA "full") if(PLATFORM_ARM64) set(RUNTIME_COMPONENT_NAME train-android-aarch64) elseif(PLATFORM_ARM32) diff --git a/mindspore/lite/include/model.h b/mindspore/lite/include/model.h index 91cea9c941..1f73e9e963 100644 --- a/mindspore/lite/include/model.h +++ b/mindspore/lite/include/model.h @@ -19,14 +19,14 @@ #include "include/lite_utils.h" namespace mindspore::lite { -class PrimitiveC; struct MS_API Model { struct Node { String name_; NodeType node_type_; - PrimitiveC *primitive_; + const void *primitive_; Uint32Vector input_indices_; Uint32Vector output_indices_; + int quant_type_; }; using NodePtrVector = std::vector; struct SubGraph { @@ -55,7 +55,7 @@ struct MS_API Model { /// \brief Free meta graph temporary buffer virtual void Free() = 0; - /// \brief Free all temporay buffer.EG: nodes in the model. + /// \brief Free all temporary buffer.EG: nodes in the model. virtual void Destroy() = 0; /// \brief Model destruct, free all memory diff --git a/mindspore/lite/include/version.h b/mindspore/lite/include/version.h index 857452bb6a..476fc20625 100644 --- a/mindspore/lite/include/version.h +++ b/mindspore/lite/include/version.h @@ -22,7 +22,7 @@ namespace mindspore { namespace lite { const int ms_version_major = 1; -const int ms_version_minor = 1; +const int ms_version_minor = 2; const int ms_version_revision = 0; /// \brief Global method to get a version string. diff --git a/mindspore/lite/micro/CMakeLists.txt b/mindspore/lite/micro/CMakeLists.txt index d49c00d88f..912acff2ce 100644 --- a/mindspore/lite/micro/CMakeLists.txt +++ b/mindspore/lite/micro/CMakeLists.txt @@ -9,16 +9,10 @@ include_directories(${CMAKE_BINARY_DIR}) include(${TOP_DIR}/cmake/utils.cmake) include(${TOP_DIR}/cmake/dependency_utils.cmake) include(${TOP_DIR}/cmake/dependency_securec.cmake) +include(${TOP_DIR}/cmake/external_libs/glog.cmake) include(${TOP_DIR}/cmake/external_libs/flatbuffers.cmake) -include(${TOP_DIR}/cmake/external_libs/cmsis.cmake) - -set(FBS_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/../schema/model.fbs - ${CMAKE_CURRENT_SOURCE_DIR}/../schema/ops.fbs - ${CMAKE_CURRENT_SOURCE_DIR}/../schema/model_v0.fbs - ${CMAKE_CURRENT_SOURCE_DIR}/../schema/ops_v0.fbs - ) +file(GLOB FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../schema/*.fbs) ms_build_flatbuffers_lite(FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../schema/ fbs_src @@ -50,6 +44,6 @@ if(ENABLE_ASAN) endif() add_subdirectory(coder) -if(${BUILD_TESTCASES}) +if(BUILD_TESTCASES) add_subdirectory(test) endif() diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index 7417c87d5a..e72274f2a5 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -5,6 +5,9 @@ set(CODER_SRC ${MICRO_DIR}/coder/graph.cc ${MICRO_DIR}/coder/session.cc ${MICRO_DIR}/coder/train.cc + ${MICRO_DIR}/coder/utils/coder_utils.cc + ${MICRO_DIR}/coder/utils/dir_utils.cc + ${MICRO_DIR}/coder/utils/type_cast.cc ) set(CODER_ALLOCATOR_SRC @@ -21,6 +24,11 @@ set(CODER_GENERATOR_SRC ${MICRO_DIR}/coder/generator/component/weight_component.cc ${MICRO_DIR}/coder/generator/component/cmake_component.cc ${MICRO_DIR}/coder/generator/component/train_component.cc + ${MICRO_DIR}/coder/generator/component/parallel_component.cc + ) + +set(MINDSPORE_CORE + ${TOP_DIR}/mindspore/core/gvar/logging_level.cc ) set(CODER_OPCODERS_SRC @@ -28,16 +36,20 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/op_coder.cc ${MICRO_DIR}/coder/opcoders/op_coder_builder.cc ${MICRO_DIR}/coder/opcoders/op_coder_register.cc + ${MICRO_DIR}/coder/opcoders/parallel.cc #### serializer ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc + ${MICRO_DIR}/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.cc #### base coder ${MICRO_DIR}/coder/opcoders/base/conv2d_base_coder.cc ${MICRO_DIR}/coder/opcoders/base/dtype_cast_coder.cc ${MICRO_DIR}/coder/opcoders/base/full_connection_base_coder.cc ${MICRO_DIR}/coder/opcoders/base/quant_dtype_cast_coder.cc ${MICRO_DIR}/coder/opcoders/base/reduce_base_coder.cc + ${MICRO_DIR}/coder/opcoders/base/resize_base_coder.cc ${MICRO_DIR}/coder/opcoders/base/softmax_base_coder.cc + ${MICRO_DIR}/coder/opcoders/base/detection_post_process_base_coder.cc #### cmsis int8 coder ${MICRO_DIR}/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc ${MICRO_DIR}/coder/opcoders/cmsis-nn/int8/conv2d_base_coder.cc @@ -55,6 +67,7 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/concat_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc @@ -64,21 +77,20 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/nnacl/fp32/gather_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/matmul_fp32_coder.cc - ${MICRO_DIR}/coder/opcoders/nnacl/fp32/nchw2nhwc_fp32_coder.cc - ${MICRO_DIR}/coder/opcoders/nnacl/fp32/nhwc2nchw_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/pad_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/power_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/reshape_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc - ${MICRO_DIR}/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/squeeze_dims_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/fp32/transpose_fp32_coder.cc #### nnacl int8 coder + ${MICRO_DIR}/coder/opcoders/nnacl/int8/activation_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/add_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/batchnorm_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/concat_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/fullconnection_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/matmul_int8_coder.cc @@ -87,40 +99,69 @@ set(CODER_OPCODERS_SRC ${MICRO_DIR}/coder/opcoders/nnacl/int8/conv2d_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/deconvolution_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/pooling_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/resize_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/reduce_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/reshape_int8_coder.cc ${MICRO_DIR}/coder/opcoders/nnacl/int8/softmax_int8_coder.cc - ) - -set(CODER_UTILS_SRC - ${MICRO_DIR}/coder/utils/coder_utils.cc - ${MICRO_DIR}/coder/utils/dir_utils.cc - ${MICRO_DIR}/coder/utils/type_cast.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/sub_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/relux_int8_coder.cc + ${MICRO_DIR}/coder/opcoders/nnacl/int8/div_int8_coder.cc + #### nnacl dequant coder + ${MICRO_DIR}/coder/opcoders/nnacl/dequant/de_quant.cc ) set(LITE_SRC ${LITE_DIR}/src/common/file_utils.cc ${LITE_DIR}/src/common/graph_util.cc ${LITE_DIR}/src/common/string_util.cc + ${LITE_DIR}/src/common/prim_util.cc + ${LITE_DIR}/src/common/tensor_util.cc ${LITE_DIR}/src/runtime/allocator.cc + ${LITE_DIR}/src/runtime/infer_manager.cc + ${LITE_DIR}/src/runtime/runtime_api.cc ${LITE_DIR}/src/lite_model.cc ${LITE_DIR}/src/tensorlist.cc ${LITE_DIR}/src/tensor.cc + ${LITE_DIR}/src/scheduler.cc + ${LITE_DIR}/src/inner_context.cc + ${LITE_DIR}/src/dequant.cc + ${LITE_DIR}/src/kernel_registry.cc + ${LITE_DIR}/src/lite_kernel.cc + ${LITE_DIR}/src/sub_graph_kernel.cc + ${LITE_DIR}/src/huffman_decode.cc + ${LITE_DIR}/src/executor.cc ${LITE_DIR}/src/common/log_adapter.cc - ### src/ops for parameter and infer shape - ${LITE_DIR}/src/ops/batch_norm.cc - ${LITE_DIR}/src/ops/conv2d.cc - ${LITE_DIR}/src/ops/primitive_c.cc - ${LITE_DIR}/src/ops/slice.cc - ${LITE_DIR}/src/ops/while.cc + ${LITE_DIR}/src/common/utils.cc ### populate operator parameter ${LITE_DIR}/src/ops/populate/conv2d_populate.cc + ${LITE_DIR}/src/ops/populate/arithmetic_populate.cc + ${LITE_DIR}/src/ops/populate/add_populate.cc + ${LITE_DIR}/src/ops/populate/concat_populate.cc + ${LITE_DIR}/src/ops/populate/conv2d_populate.cc + ${LITE_DIR}/src/ops/populate/detection_post_process_populate.cc + ${LITE_DIR}/src/ops/populate/depthwise_conv2d_populate.cc + ${LITE_DIR}/src/ops/populate/full_connection_populate.cc + ${LITE_DIR}/src/ops/populate/pooling_populate.cc + ${LITE_DIR}/src/ops/populate/quant_dtype_cast_populate.cc + ${LITE_DIR}/src/ops/populate/resize_populate.cc + ${LITE_DIR}/src/ops/populate/reshape_populate.cc + ${LITE_DIR}/src/ops/populate/batch_norm_populate.cc + ${LITE_DIR}/src/ops/populate/slice_populate.cc + ${LITE_DIR}/src/ops/populate/while_populate.cc + ${LITE_DIR}/src/ops/populate/matmul_populate.cc + ${LITE_DIR}/src/ops/populate/bias_add_populate.cc + ${LITE_DIR}/src/ops/populate/activation_populate.cc ### tools ${LITE_DIR}/tools/common/flag_parser.cc ) set(LITE_KERNEL_SRC ### nnacl + ${LITE_DIR}/nnacl/common_func.c ${LITE_DIR}/nnacl/base/minimal_filtering_generator.c + ${LITE_DIR}/nnacl/base/arithmetic_base.c + ${LITE_DIR}/nnacl/base/slice_base.c ${LITE_DIR}/nnacl/fp32/winograd_utils.c ${LITE_DIR}/nnacl/fp32/pack_fp32.c ${LITE_DIR}/nnacl/int8/quantize.c @@ -128,13 +169,138 @@ set(LITE_KERNEL_SRC ${LITE_DIR}/nnacl/int8/matmul_int8.c ${LITE_DIR}/nnacl/int8/fixed_point.c ${LITE_DIR}/nnacl/fp32/matmul_fp32.c + ${LITE_DIR}/nnacl/int8/arithmetic_int8.c + ${LITE_DIR}/nnacl/int8/add_int8.c + ${LITE_DIR}/nnacl/int8/concat_int8.c + ${LITE_DIR}/nnacl/int8/conv_int8.c ${LITE_DIR}/nnacl/int8/conv3x3_int8.c ${LITE_DIR}/nnacl/int8/conv1x1_int8.c ${LITE_DIR}/nnacl/base/conv1x1_base.c + ${LITE_DIR}/nnacl/int8/conv_depthwise_int8.c ${LITE_DIR}/nnacl/int8/deconv_int8.c ${LITE_DIR}/nnacl/int8/common_func_int8.c + ${LITE_DIR}/nnacl/int8/slice_int8.c + ${LITE_DIR}/nnacl/int8/batchnorm_int8.c + ${LITE_DIR}/nnacl/int8/sub_int8.c + ${LITE_DIR}/nnacl/int8/quant_dtype_cast_int8.c + ${LITE_DIR}/nnacl/int8/sigmoid_int8.c + ${LITE_DIR}/nnacl/int8/resize_int8.c + ### infer + ${LITE_DIR}/nnacl/infer/adam_infer.c + ${LITE_DIR}/nnacl/infer/add_sub_grad_infer.c + ${LITE_DIR}/nnacl/infer/addn_infer.c + ${LITE_DIR}/nnacl/infer/apply_momentum_infer.c + ${LITE_DIR}/nnacl/infer/argmin_max_infer.c + ${LITE_DIR}/nnacl/infer/arithmetic_compare_infer.c + ${LITE_DIR}/nnacl/infer/arithmetic_grad_infer.c + ${LITE_DIR}/nnacl/infer/arithmetic_infer.c + ${LITE_DIR}/nnacl/infer/assert_op_infer.c + ${LITE_DIR}/nnacl/infer/assign_add_infer.c + ${LITE_DIR}/nnacl/infer/assign_infer.c + ${LITE_DIR}/nnacl/infer/audio_spectrogram_infer.c + ${LITE_DIR}/nnacl/infer/batch_to_space_infer.c + ${LITE_DIR}/nnacl/infer/bias_grad_infer.c + ${LITE_DIR}/nnacl/infer/binary_cross_entropy_infer.c + ${LITE_DIR}/nnacl/infer/bn_grad_infer.c + ${LITE_DIR}/nnacl/infer/broadcast_to_infer.c + ${LITE_DIR}/nnacl/infer/cast_infer.c + ${LITE_DIR}/nnacl/infer/common_infer.c + ${LITE_DIR}/nnacl/infer/concat_infer.c + ${LITE_DIR}/nnacl/infer/constant_of_shape_infer.c + ${LITE_DIR}/nnacl/infer/conv2d_grad_filter_infer.c + ${LITE_DIR}/nnacl/infer/conv2d_grad_input_infer.c + ${LITE_DIR}/nnacl/infer/conv2d_infer.c + ${LITE_DIR}/nnacl/infer/crop_and_resize_infer.c + ${LITE_DIR}/nnacl/infer/crop_infer.c + ${LITE_DIR}/nnacl/infer/custom_extract_features_infer.c + ${LITE_DIR}/nnacl/infer/custom_normalize_infer.c + ${LITE_DIR}/nnacl/infer/custom_predict_infer.c + ${LITE_DIR}/nnacl/infer/deconv2d_infer.c + ${LITE_DIR}/nnacl/infer/dedepthwise_conv2d_infer.c + ${LITE_DIR}/nnacl/infer/depth_to_space_infer.c + ${LITE_DIR}/nnacl/infer/depthwise_conv2d_infer.c + ${LITE_DIR}/nnacl/infer/detection_post_process_infer.c + ${LITE_DIR}/nnacl/infer/dropout_grad_infer.c + ${LITE_DIR}/nnacl/infer/dropout_infer.c + ${LITE_DIR}/nnacl/infer/embedding_lookup_infer.c + ${LITE_DIR}/nnacl/infer/expand_dims_infer.c + ${LITE_DIR}/nnacl/infer/fft_imag_infer.c + ${LITE_DIR}/nnacl/infer/fft_real_infer.c + ${LITE_DIR}/nnacl/infer/fill_infer.c + ${LITE_DIR}/nnacl/infer/flatten_grad_infer.c + ${LITE_DIR}/nnacl/infer/flatten_infer.c + ${LITE_DIR}/nnacl/infer/full_connection_infer.c + ${LITE_DIR}/nnacl/infer/fused_batchnorm_infer.c + ${LITE_DIR}/nnacl/infer/gather_infer.c + ${LITE_DIR}/nnacl/infer/gather_nd_infer.c + ${LITE_DIR}/nnacl/infer/group_conv2d_grad_input_infer.c + ${LITE_DIR}/nnacl/infer/gru_infer.c + ${LITE_DIR}/nnacl/infer/hashtable_lookup_infer.c + ${LITE_DIR}/nnacl/infer/invert_permutation_infer.c + ${LITE_DIR}/nnacl/infer/layer_norm_infer.c + ${LITE_DIR}/nnacl/infer/lin_space_infer.c + ${LITE_DIR}/nnacl/infer/lsh_projection_infer.c + ${LITE_DIR}/nnacl/infer/lstm_infer.c + ${LITE_DIR}/nnacl/infer/matmul_infer.c + ${LITE_DIR}/nnacl/infer/maximum_grad_infer.c + ${LITE_DIR}/nnacl/infer/mean_infer.c + ${LITE_DIR}/nnacl/infer/merge_infer.c + ${LITE_DIR}/nnacl/infer/mfcc_infer.c + ${LITE_DIR}/nnacl/infer/non_max_suppression_infer.c + ${LITE_DIR}/nnacl/infer/one_hot_infer.c + ${LITE_DIR}/nnacl/infer/pad_infer.c + ${LITE_DIR}/nnacl/infer/partial_infer.c + ${LITE_DIR}/nnacl/infer/pooling_grad_infer.c + ${LITE_DIR}/nnacl/infer/pooling_infer.c + ${LITE_DIR}/nnacl/infer/power_infer.c + ${LITE_DIR}/nnacl/infer/prior_box_infer.c + ${LITE_DIR}/nnacl/infer/quant_dtype_cast_infer.c + ${LITE_DIR}/nnacl/infer/random_standard_normal_infer.c + ${LITE_DIR}/nnacl/infer/range_infer.c + ${LITE_DIR}/nnacl/infer/rank_infer.c + ${LITE_DIR}/nnacl/infer/reduce_infer.c + ${LITE_DIR}/nnacl/infer/reshape_infer.c + ${LITE_DIR}/nnacl/infer/resize_infer.c + ${LITE_DIR}/nnacl/infer/rfft_infer.c + ${LITE_DIR}/nnacl/infer/roi_pooling_infer.c + ${LITE_DIR}/nnacl/infer/scatter_nd_infer.c + ${LITE_DIR}/nnacl/infer/select_infer.c + ${LITE_DIR}/nnacl/infer/sgd_infer.c + ${LITE_DIR}/nnacl/infer/shape_infer.c + ${LITE_DIR}/nnacl/infer/size_infer.c + ${LITE_DIR}/nnacl/infer/skip_gram_infer.c + ${LITE_DIR}/nnacl/infer/slice_infer.c + ${LITE_DIR}/nnacl/infer/softmax_cross_entropy_infer.c + ${LITE_DIR}/nnacl/infer/softmax_infer.c + ${LITE_DIR}/nnacl/infer/space_to_batch_infer.c + ${LITE_DIR}/nnacl/infer/space_to_batch_nd_infer.c + ${LITE_DIR}/nnacl/infer/space_to_depth_infer.c + ${LITE_DIR}/nnacl/infer/sparse_softmax_cross_entropy_infer.c + ${LITE_DIR}/nnacl/infer/sparse_to_dense_infer.c + ${LITE_DIR}/nnacl/infer/split_infer.c + ${LITE_DIR}/nnacl/infer/squeeze_infer.c + ${LITE_DIR}/nnacl/infer/stack_infer.c + ${LITE_DIR}/nnacl/infer/strided_slice_grad_infer.c + ${LITE_DIR}/nnacl/infer/strided_slice_infer.c + ${LITE_DIR}/nnacl/infer/switch_infer.c + ${LITE_DIR}/nnacl/infer/tensorlist_fromtensor_infer.c + ${LITE_DIR}/nnacl/infer/tensorlist_getitem_infer.c + ${LITE_DIR}/nnacl/infer/tensorlist_reserve_infer.c + ${LITE_DIR}/nnacl/infer/tensorlist_setitem_infer.c + ${LITE_DIR}/nnacl/infer/tensorlist_stack_infer.c + ${LITE_DIR}/nnacl/infer/tile_infer.c + ${LITE_DIR}/nnacl/infer/topk_infer.c + ${LITE_DIR}/nnacl/infer/transpose_infer.c + ${LITE_DIR}/nnacl/infer/uniform_real_infer.c + ${LITE_DIR}/nnacl/infer/unique_infer.c + ${LITE_DIR}/nnacl/infer/unsorted_segment_sum_infer.c + ${LITE_DIR}/nnacl/infer/unsqueeze_infer.c + ${LITE_DIR}/nnacl/infer/unstack_infer.c + ${LITE_DIR}/nnacl/infer/where_infer.c + ${LITE_DIR}/nnacl/infer/while_infer.c + ${LITE_DIR}/nnacl/infer/splice_infer.c ) -list(APPEND FILE_SET ${CODER_SRC} ${CODER_UTILS_SRC} ${CODER_OPCODERS_SRC} ${CODER_GENERATOR_SRC} - ${CODER_ALLOCATOR_SRC} ${LITE_SRC} ${LITE_KERNEL_SRC}) +list(APPEND FILE_SET ${CODER_SRC} ${CODER_OPCODERS_SRC} ${CODER_GENERATOR_SRC} + ${CODER_ALLOCATOR_SRC} ${LITE_SRC} ${LITE_KERNEL_SRC} ${MINDSPORE_CORE}) diff --git a/mindspore/lite/micro/cmake/package_cmsis.cmake b/mindspore/lite/micro/cmake/package_cmsis.cmake new file mode 100644 index 0000000000..eaed65def9 --- /dev/null +++ b/mindspore/lite/micro/cmake/package_cmsis.cmake @@ -0,0 +1,21 @@ +set(CMSIS_DIR ${LITE_DIR}/micro/build/cmsis) +if(MICRO_CMSIS_X86) + message("build cmsis kernels") + include_directories(${CMSIS_DIR}/CMSIS/Core/Include) + include_directories(${CMSIS_DIR}/CMSIS/DSP/Include) + include_directories(${CMSIS_DIR}/CMSIS/NN/Include) + + file(REMOVE ${CMSIS_DIR}/CMSIS/NN/Source/NNSupportFunctions/arm_q7_to_q15_reordered_no_shift.c) + + file(GLOB CMSIS_OPS + ${CMSIS_DIR}/CMSIS/NN/Source/BasicMathFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/ActivationFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/ConcatenationFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/ConvolutionFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/FullyConnectedFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/NNSupportFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/PoolingFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/ReshapeFunctions/*.c + ${CMSIS_DIR}/CMSIS/NN/Source/SoftmaxFunctions/*.c + ) +endif() diff --git a/mindspore/lite/micro/cmake/package_micro_ops.cmake b/mindspore/lite/micro/cmake/package_micro_ops.cmake deleted file mode 100644 index 1a43baed2d..0000000000 --- a/mindspore/lite/micro/cmake/package_micro_ops.cmake +++ /dev/null @@ -1,32 +0,0 @@ -include_directories(${NNACL_DIR}/..) - -set(CMSIS_SRC ${NNACL_DIR}/../micro/build/cmsis) -if(MICRO_CMSIS_X86) - message("*****build cmsis x86 codes****") - include_directories(${CMSIS_SRC}/CMSIS/Core/Include) - include_directories(${CMSIS_SRC}/CMSIS/DSP/Include) - include_directories(${CMSIS_SRC}/CMSIS/NN/Include) - file(GLOB RUNTIME_KERNEL_CMSIS_SRC - ${CMSIS_SRC}/CMSIS/NN/Source/BasicMathFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/ActivationFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/ConcatenationFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/ConvolutionFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/FullyConnectedFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/NNSupportFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/PoolingFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/ReshapeFunctions/*.c - ${CMSIS_SRC}/CMSIS/NN/Source/SoftmaxFunctions/*.c - ) -endif() - -########################### files ########################### -file(GLOB RUNTIME_KERNEL_SRC - ${NNACL_DIR}/kernel/fp32/*.c - ${NNACL_DIR}/kernel/int8/*.c - ) -if(MICRO_CMSIS_X86) - set(RUNTIME_OPS ${RUNTIME_KERNEL_SRC} ${RUNTIME_TRAIN_SRC} ${RUNTIME_KERNEL_CMSIS_SRC}) -else() - set(RUNTIME_OPS ${RUNTIME_KERNEL_SRC} ${RUNTIME_TRAIN_SRC}) -endif() - diff --git a/mindspore/lite/micro/cmake/package_nnacl.cmake b/mindspore/lite/micro/cmake/package_nnacl.cmake new file mode 100644 index 0000000000..f3f26fc46e --- /dev/null +++ b/mindspore/lite/micro/cmake/package_nnacl.cmake @@ -0,0 +1,20 @@ +include_directories(${LITE_DIR}) +set(NNACL_DIR ${LITE_DIR}/nnacl) +file(GLOB KERNEL_SRC + ${NNACL_DIR}/*.c + ${NNACL_DIR}/base/*.c + ${NNACL_DIR}/fp32/*.c + ${NNACL_DIR}/int8/*.c +) + +if(MICRO_BUILD_ARM64) + file(GLOB ASSEMBLY_SRC ${NNACL_DIR}/assembly/arm64/*.S) + set_property(SOURCE ${ASSEMBLY_SRC} PROPERTY LANGUAGE C) +endif() + +if(MICRO_BUILD_ARM32A) + file(GLOB ASSEMBLY_SRC ${NNACL_DIR}/assembly/arm32/*.S) + set_property(SOURCE ${ASSEMBLY_SRC} PROPERTY LANGUAGE C) +endif() + +set(NNACL_OPS ${KERNEL_SRC} ${ASSEMBLY_SRC}) diff --git a/mindspore/lite/micro/cmake/package_wrapper.cmake b/mindspore/lite/micro/cmake/package_wrapper.cmake new file mode 100644 index 0000000000..fbaa20b567 --- /dev/null +++ b/mindspore/lite/micro/cmake/package_wrapper.cmake @@ -0,0 +1,25 @@ +include_directories(${LITE_DIR}/micro/coder/operator_library) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") +set(WRAPPER_DIR ${LITE_DIR}/micro/coder/operator_library/wrapper/) + +set(RUNTIME_SRC + ${LITE_DIR}/src/runtime/thread_pool.c + ) + +set(WRAPPER_SRC + ${WRAPPER_DIR}/base/detection_post_process_base_wrapper.c + ${WRAPPER_DIR}/fp32/matmul_fp32_wrapper.c + ${WRAPPER_DIR}/int8/matmul_int8_wrapper.c + ${WRAPPER_DIR}/int8/add_int8_wrapper.c + ${WRAPPER_DIR}/int8/concat_int8_wrapper.c + ${WRAPPER_DIR}/int8/convolution_int8_wrapper.c + ${WRAPPER_DIR}/int8/conv_init_int8_wrapper.c + ${WRAPPER_DIR}/int8/conv1x1_init_int8_wrapper.c + ${WRAPPER_DIR}/int8/conv1x1_run_int8_wrapper.c + ${WRAPPER_DIR}/int8/convolution_depthwise_int8_wrapper.c + ${WRAPPER_DIR}/int8/resize_int8_wrapper.c + ${WRAPPER_DIR}/int8/slice_int8_wrapper.c + ${WRAPPER_DIR}/int8/batchnorm_int8_wrapper.c + ) + +list(APPEND FILE_SET ${WRAPPER_SRC} ${RUNTIME_SRC}) diff --git a/mindspore/lite/micro/cmake/wrapper.cmake b/mindspore/lite/micro/cmake/wrapper.cmake deleted file mode 100644 index 5c2fd0c21f..0000000000 --- a/mindspore/lite/micro/cmake/wrapper.cmake +++ /dev/null @@ -1,12 +0,0 @@ -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") - -set(MICRO_WRAPPER_SRC - ${LITE_DIR}/src/runtime/thread_pool.c - ${MICRO_DIR}/wrapper/fp32/matmul_fp32_wrapper.c - ${MICRO_DIR}/wrapper/int8/matmul_int8_wrapper.c - ${MICRO_DIR}/wrapper/int8/conv_init_int8_wrapper.c - ${MICRO_DIR}/wrapper/int8/conv1x1_init_int8_wrapper.c - ${MICRO_DIR}/wrapper/int8/conv1x1_run_int8_wrapper.c - ) - -list(APPEND FILE_SET ${MICRO_WRAPPER_SRC}) \ No newline at end of file diff --git a/mindspore/lite/micro/coder/CMakeLists.txt b/mindspore/lite/micro/coder/CMakeLists.txt index 0cd7aefebf..97a05fe2e1 100644 --- a/mindspore/lite/micro/coder/CMakeLists.txt +++ b/mindspore/lite/micro/coder/CMakeLists.txt @@ -1,7 +1,10 @@ +add_definitions(-DUSE_GLOG) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections ") -set(MICRO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..) -set(LITE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..) set(3RD_DIR ${TOP_DIR}/third_party) +set(LITE_DIR ${TOP_DIR}/mindspore/lite) +set(MICRO_DIR ${LITE_DIR}/micro) + if(ENABLE_CONVERTER) set(CODEGEN_PATH ${CMAKE_BINARY_DIR}/micro/coder/codegen) else() @@ -13,17 +16,19 @@ include_directories(${3RD_DIR}) include_directories(${3RD_DIR}/flatbuffers/include) #include ms include_directories(${TOP_DIR}/) -include_directories(${LITE_DIR}) include_directories(${TOP_DIR}/mindspore/core/) - +include_directories(${LITE_DIR}) +include_directories(${MICRO_DIR}) #include coder -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) +include(${TOP_DIR}/cmake/external_libs/cmsis.cmake) include(${MICRO_DIR}/cmake/file_list.cmake) -include(${MICRO_DIR}/cmake/wrapper.cmake) +include(${MICRO_DIR}/cmake/package_wrapper.cmake) +add_subdirectory(operator_library) + add_executable(codegen main.cc ${FILE_SET}) add_dependencies(codegen fbs_src) add_dependencies(codegen fbs_inner_src) -target_link_libraries(codegen PRIVATE ${SECUREC_LIBRARY}) -if(NOT WIN32) - add_custom_command(TARGET codegen POST_BUILD COMMAND strip ${CODEGEN_PATH}) +target_link_libraries(codegen PRIVATE ${SECUREC_LIBRARY} mindspore::glog) +if(NOT WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release") + add_custom_command(TARGET codegen POST_BUILD COMMAND strip ${CODEGEN_PATH}) endif() diff --git a/mindspore/lite/micro/coder/allocator/allocator.cc b/mindspore/lite/micro/coder/allocator/allocator.cc index 71c4f5d72f..61674a3f77 100644 --- a/mindspore/lite/micro/coder/allocator/allocator.cc +++ b/mindspore/lite/micro/coder/allocator/allocator.cc @@ -22,11 +22,9 @@ namespace mindspore::lite::micro { void *MemoryAllocator::MallocWeightTensor(TypeId type_id, size_t size, MallocType type) { - static const std::map size_map = {{kNumberTypeFloat32, sizeof(float)}, - {kNumberTypeInt32, sizeof(int)}, - {kNumberTypeInt32, sizeof(int32_t)}, - {kNumberTypeInt16, sizeof(int16_t)}, - {kNumberTypeInt8, sizeof(int8_t)}}; + static const std::map size_map = { + {kNumberTypeFloat, sizeof(float)}, {kNumberTypeFloat32, sizeof(float)}, {kNumberTypeInt32, sizeof(int32_t)}, + {kNumberTypeInt16, sizeof(int16_t)}, {kNumberTypeInt8, sizeof(int8_t)}, {kNumberTypeUInt8, sizeof(uint8_t)}}; auto item = size_map.find(type_id); MS_CHECK_TRUE_RET_NULL(item != size_map.end(), "unsupported type idnex"); size_t type_size = item->second; diff --git a/mindspore/lite/micro/coder/allocator/allocator.h b/mindspore/lite/micro/coder/allocator/allocator.h index f4f6614ca0..660d48e121 100644 --- a/mindspore/lite/micro/coder/allocator/allocator.h +++ b/mindspore/lite/micro/coder/allocator/allocator.h @@ -73,7 +73,7 @@ class MemoryAllocator { if (type != kWorkspace) { return MallocWeightTensor(type_id, size, type); } - if (size == 0 && size >= UINT_MAX) { + if (size == 0 || size >= UINT_MAX) { return nullptr; } @@ -94,12 +94,12 @@ class MemoryAllocator { template std::string GetRuntimeAddr(T t, bool is_const = false) { if (!t) { - return "NULL"; + return ""; } std::string type_info = is_const ? "const " : ""; std::string type_name; if (std::type_index(typeid(T)) == std::type_index(typeid(Tensor *))) { - type_name = GetTensorDataType(reinterpret_cast(t)->data_type()) + " *"; + type_name = GetTensorDataType(reinterpret_cast(t)->data_type()) + "*"; } else { type_name = GetVariableTypeName(); } diff --git a/mindspore/lite/micro/coder/coder.cc b/mindspore/lite/micro/coder/coder.cc index 74d99b26f2..44315114cf 100644 --- a/mindspore/lite/micro/coder/coder.cc +++ b/mindspore/lite/micro/coder/coder.cc @@ -34,19 +34,20 @@ namespace mindspore::lite::micro { class CoderFlags : public virtual FlagParser { public: CoderFlags() { - AddFlag(&CoderFlags::is_weight_file_, "isWeightFile", "whether generating weight .net file, true| false", false); + AddFlag(&CoderFlags::is_weight_file_, "isWeightFile", "whether generating weight binary file, true| false", false); AddFlag(&CoderFlags::model_path_, "modelPath", "Input model path", ""); AddFlag(&CoderFlags::code_path_, "codePath", "Input code path", "."); AddFlag(&CoderFlags::code_module_name_, "moduleName", "Input code module name", ""); - AddFlag(&CoderFlags::target_, "target", "generateed code target, x86| ARM32M| ARM32A| ARM64", "x86"); - AddFlag(&CoderFlags::code_mode_, "codeMode", "generated code mode, Normal | Inference | Train", "Normal"); - AddFlag(&CoderFlags::debug_mode_, "debugMode", "dump perlayer's time cost and tensor, true | false", false); + AddFlag(&CoderFlags::target_, "target", "generated code target, x86| ARM32M| ARM32A| ARM64", "x86"); + AddFlag(&CoderFlags::code_mode_, "codeMode", "generated code mode, Inference | Train", "Inference"); + AddFlag(&CoderFlags::support_parallel_, "supportParallel", "whether support parallel launch, true | false", false); + AddFlag(&CoderFlags::debug_mode_, "debugMode", "dump the tensors data for debugging, true | false", false); } ~CoderFlags() override = default; - public: std::string model_path_; + bool support_parallel_{false}; bool is_weight_file_{false}; std::string code_module_name_; std::string code_path_; @@ -87,8 +88,7 @@ int Coder::Run(const std::string &model_path) { int Coder::Init(const CoderFlags &flags) const { static const std::map kTargetMap = { {"x86", kX86}, {"ARM32M", kARM32M}, {"ARM32A", kARM32A}, {"ARM64", kARM64}, {"All", kAllTargets}}; - static const std::map kCodeModeMap = { - {"Normal", Code_Normal}, {"Inference", Code_Inference}, {"Train", Code_Train}}; + static const std::map kCodeModeMap = {{"Inference", Inference}, {"Train", Train}}; Configurator *config = Configurator::GetInstance(); @@ -112,6 +112,11 @@ int Coder::Init(const CoderFlags &flags) const { return true; }); + parsers.emplace_back([&flags, config]() -> bool { + config->set_support_parallel(flags.support_parallel_); + return true; + }); + parsers.emplace_back([&flags, config]() -> bool { config->set_debug_mode(flags.debug_mode_); return true; diff --git a/mindspore/lite/micro/coder/coder_config.h b/mindspore/lite/micro/coder/coder_config.h index d1b89b6b36..949b420578 100644 --- a/mindspore/lite/micro/coder/coder_config.h +++ b/mindspore/lite/micro/coder/coder_config.h @@ -21,7 +21,7 @@ namespace mindspore::lite::micro { enum Target { kX86 = 0, kARM32M = 1, kARM32A = 2, kARM64 = 3, kAllTargets = 4, kTargetUnknown = 99 }; -enum CodeMode { Code_Normal = 0, Code_Inference = 1, Code_Train = 2, Code_Unknown = 99 }; +enum CodeMode { Inference = 0, Train = 1, Code_Unknown = 99 }; class Configurator { public: @@ -36,9 +36,6 @@ class Configurator { void set_code_path(const std::string &code_path) { code_path_ = code_path; } std::string code_path() const { return code_path_; } - void set_subgraph_(const std::string &subgraph) { sub_graph_ = subgraph; } - std::string sub_graph() { return sub_graph_; } - void set_target(Target target) { target_ = target; } Target target() const { return target_; } @@ -51,16 +48,19 @@ class Configurator { void set_is_weight_file(bool flag) { is_weight_file_ = flag; } bool is_weight_file() const { return is_weight_file_; } + void set_support_parallel(bool parallel) { support_parallel_ = parallel; } + bool support_parallel() const { return support_parallel_; } + private: Configurator() = default; ~Configurator() = default; - bool is_weight_file_{false}; std::string module_name_; std::string code_path_; - std::string sub_graph_; Target target_{kTargetUnknown}; CodeMode code_mode_{Code_Unknown}; + bool is_weight_file_{false}; + bool support_parallel_{false}; bool debug_mode_{false}; }; } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/context.cc b/mindspore/lite/micro/coder/context.cc index 065879d1cb..781312b2cc 100644 --- a/mindspore/lite/micro/coder/context.cc +++ b/mindspore/lite/micro/coder/context.cc @@ -14,9 +14,9 @@ * limitations under the License. */ -#include "micro/coder/context.h" -#include "micro/coder/coder_config.h" -#include "micro/coder/allocator/allocator.h" +#include "coder/context.h" +#include "coder/coder_config.h" +#include "coder/allocator/allocator.h" namespace mindspore::lite::micro { CoderContext::CoderContext() { diff --git a/mindspore/lite/micro/coder/generator/component/benchmark_component.cc b/mindspore/lite/micro/coder/generator/component/benchmark_component.cc index 71ab14bfdd..1b24efc4ed 100644 --- a/mindspore/lite/micro/coder/generator/component/benchmark_component.cc +++ b/mindspore/lite/micro/coder/generator/component/benchmark_component.cc @@ -108,7 +108,7 @@ void CodeBenchmarkSetBuffer(std::ofstream &ofs, const std::string &module_name) << "_SetBuffer(buffer);\n" " if (ret != RET_OK) {\n" " MICRO_ERROR(\"set inputs failed\");\n" - " return RET_ERROR;" + " return RET_ERROR;\n" " }\n"; } @@ -128,19 +128,6 @@ void CodeBenchmarkInitWeight(std::ofstream &ofs, const std::string &module_name) " weight_buffer = NULL;\n"; } -void CodeBenchmarkConfigThread(std::ofstream &ofs) { - ofs << " int thread_num = 4;\n" - " BindMode bind_mode = NO_BIND_MODE;\n" - " if (argc >= 6) {\n" - " thread_num = atoi(argv[4]);\n" - " bind_mode = atoi(argv[5]);\n" - " }\n" - " ret = ConfigThreadPool(THREAD_POOL_DEFAULT, thread_num, bind_mode);\n" - " if (ret != 0) {\n" - " MICRO_ERROR(\"create thread pool failed\");\n" - " }\n"; -} - void CodeBenchmarkInference(std::ofstream &ofs, const std::string &module_name) { ofs << " if (argc >= 4) {\n" << " " << module_name << "_WarmUp();\n" @@ -170,7 +157,6 @@ void CodeBenchmarkPrintOutputs(std::ofstream &ofs, const std::string &module_nam " PrintTensorData(tensor);\n" " }\n"; ofs << " printf(\"" << module_name << " inference success.\\n\");\n"; - ofs << " free(buffer);\n"; } /** diff --git a/mindspore/lite/micro/coder/generator/component/benchmark_component.h b/mindspore/lite/micro/coder/generator/component/benchmark_component.h index e73e916c2d..a19a5c9ef9 100644 --- a/mindspore/lite/micro/coder/generator/component/benchmark_component.h +++ b/mindspore/lite/micro/coder/generator/component/benchmark_component.h @@ -39,8 +39,6 @@ void CodeBenchmarkSetBuffer(std::ofstream &ofs, const std::string &module_name); void CodeBenchmarkInitWeight(std::ofstream &ofs, const std::string &module_name); -void CodeBenchmarkConfigThread(std::ofstream &ofs); - void CodeBenchmarkInference(std::ofstream &ofs, const std::string &module_name); void CodeBenchmarkPrintOutputs(std::ofstream &ofs, const std::string &module_name); diff --git a/mindspore/lite/micro/coder/generator/component/cmake_component.cc b/mindspore/lite/micro/coder/generator/component/cmake_component.cc index 58cd691d8c..9096f3872e 100644 --- a/mindspore/lite/micro/coder/generator/component/cmake_component.cc +++ b/mindspore/lite/micro/coder/generator/component/cmake_component.cc @@ -24,10 +24,9 @@ void CodeCMakeNetLibrary(std::ofstream &ofs, const std::string &module_name, con Target target) { ofs << "include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)\n"; if (target == kARM32M) { - ofs << "include_directories(${OP_HEADER_PATH}/cmsis)\n" - << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/NN/Include)\n" - << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/DSP/Include)\n" - << "include_directories(${OP_HEADER_PATH}/cmsis/CMSIS/Core/Include)\n"; + ofs << "include_directories(${OP_HEADER_PATH}/CMSIS/NN/Include)\n" + << "include_directories(${OP_HEADER_PATH}/CMSIS/DSP/Include)\n" + << "include_directories(${OP_HEADER_PATH}/CMSIS/Core/Include)\n"; } ofs << "set(OP_SRC\n"; for (const std::string &c_file : ctx->c_files()) { @@ -38,7 +37,7 @@ void CodeCMakeNetLibrary(std::ofstream &ofs, const std::string &module_name, con << ")\n"; std::set kernel_cmake_asm_set_files = ctx->asm_files(); - if (!kernel_cmake_asm_set_files.empty()) { + if (!kernel_cmake_asm_set_files.empty() && (target == kARM32A || target == kARM64)) { ofs << "set(ASSEMBLY_SRC\n"; for (const std::string &asm_file : kernel_cmake_asm_set_files) { ofs << " " << asm_file << ".o\n"; diff --git a/mindspore/lite/micro/coder/generator/component/common_component.cc b/mindspore/lite/micro/coder/generator/component/common_component.cc index fabf32e71f..feb585f21b 100644 --- a/mindspore/lite/micro/coder/generator/component/common_component.cc +++ b/mindspore/lite/micro/coder/generator/component/common_component.cc @@ -26,7 +26,7 @@ namespace mindspore::lite::micro { void CodeSourceFileInclude(std::ofstream &ofs, const std::string &weight_file, const std::string &header) { ofs << g_hwLicense << "#include \"microtensor.h\"\n" << "#include \"" << weight_file << "\"\n" - << "#include \"" << header << "\"\n"; + << "#include \"" << header << "\"\n\n"; } void CodeInputAndOutputState(std::ofstream &ofs, const std::string &module_name) { @@ -53,13 +53,13 @@ void PrintMicroTensors(std::ofstream &ofs, std::vector tensors, const MS_LOG(ERROR) << "nonexistent tensor"; break; } - ofs << " static int dim[] = {"; + ofs << " static int dim" << i << "[] = {"; for (size_t j = 0; j < tensor->shape().size(); ++j) { ofs << tensor->shape()[j] << ", "; } ofs << "};\n" << " " << name << "[" << i << "].ndim = " << tensor->shape().size() << ";\n" - << " " << name << "[" << i << "].dim = dim;\n" + << " " << name << "[" << i << "].dim = dim" << i << ";\n" << " " << name << "[" << i << "].type = " << EnumMicroTensorDataType(tensor->data_type()) << ";\n" << " " << name << "[" << i << "].format = " << std::to_string(tensor->format()) << ";\n" << " " << name << "[" << i << "].data =" << item->second << ";\n"; @@ -69,7 +69,6 @@ void PrintMicroTensors(std::ofstream &ofs, std::vector tensors, const void CodeInputAndOutputImplement(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx) { // input tensors - ofs << "\n// input tensors\n"; std::vector inputs = ctx->graph_inputs(); for (size_t i = 0; i < inputs.size(); ++i) { ofs << "static const unsigned char *" << ctx->input_name() + std::to_string(i) << " = 0;\n"; @@ -88,7 +87,6 @@ void CodeInputAndOutputImplement(std::ofstream &ofs, const std::string &module_n ofs << " return RET_OK;\n}\n"; // output tensors - ofs << "\n// output tensors\n"; std::vector outputs = ctx->graph_outputs(); size_t output_num = outputs.size(); std::string output_name = ctx->output_name(); @@ -158,7 +156,7 @@ void CodeManageResourceState(std::ofstream &ofs, const std::string &module_name) void CodeInitResourceImplement(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx) { - ofs << "int " << module_name << "deconv_GetBufferSize() {\n" + ofs << "int " << module_name << "_GetBufferSize() {\n" << " return " << ctx->total_buffer_size() << ";\n" << "}\n"; ofs << "int " << module_name << "_SetBuffer( void *buffer) {\n"; diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h index 4facccfec8..3e0166c6f1 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ #define MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ -static const char bench_cmake_lists_txt[] = +const char *bench_cmake_lists_txt = "cmake_minimum_required(VERSION 3.14)\n" "project(${PROJ_NAME})\n" "\n" @@ -55,9 +55,9 @@ static const char bench_cmake_lists_txt[] = "link_directories(${MODEL_LIB_PATH})\n" "include(benchmark.cmake)\n" "add_executable(${PROJ_NAME}_bench ${SRC_FILES})\n" - "target_link_libraries(${PROJ_NAME}_bench ${MODEL_LIB_NAME} -lm)\n"; + "target_link_libraries(${PROJ_NAME}_bench ${MODEL_LIB_NAME} -lm -pthread)\n"; -static const char src_cmake_lists_txt[] = +const char *src_cmake_lists_txt = "cmake_minimum_required(VERSION 3.14)\n" "project(${PROJ_NAME})\n" "\n" @@ -112,4 +112,4 @@ static const char src_cmake_lists_txt[] = "string(CONCAT library_name \"lib\" ${PROJ_NAME} \".a\")\n" "create_library()\n"; -#endif // MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_CMAKE_LISTS_CODE_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h index 995b80b972..9af8eb6043 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/debug_utils.h @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BEN_DEBUG_UTILS_H_ -#define MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BEN_DEBUG_UTILS_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ -static const char debug_utils_h[] = +const char *debug_utils_h = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -50,7 +50,7 @@ static const char debug_utils_h[] = "\n" "#endif // MINDSPORE_LITE_MICRO_MICRODEBUGUTIL_H_\n"; -static const char debug_utils_c[] = +const char *debug_utils_c = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -239,7 +239,7 @@ static const char debug_utils_c[] = "}\n" "\n" "void PrintTensor(MicroTensor *tensor, FILE *output_file, const char *is_input) {\n" - " if (output_file != NULL) {\n" + " if (output_file == NULL) {\n" " MICRO_ERROR(\"output file is NULL\");\n" " return;\n" " }\n" @@ -269,4 +269,4 @@ static const char debug_utils_c[] = " return retval;\n" "}\n"; -#endif // MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BEN_DEBUG_UTILS_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_DEBUG_UTILS_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/license.h b/mindspore/lite/micro/coder/generator/component/const_blocks/license.h index 6dcd967ca4..825f3b1721 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/license.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/license.h @@ -14,12 +14,12 @@ * limitations under the License. */ -#ifndef MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H -#define MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H +#ifndef MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H_ +#define MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H_ namespace mindspore::lite::micro { -const char g_hwLicense[] = +static const char *g_hwLicense = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -37,4 +37,4 @@ const char g_hwLicense[] = " */\n\n"; } // namespace mindspore::lite::micro -#endif // MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H +#endif // MINDSPORE_LITE_MICRO_GENERATOR_CONST_BLOCK_LICENSE_INFOS_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h index 9870e0b229..119b5da6ac 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/load_input.h @@ -14,9 +14,9 @@ * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ -#define MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ -static const char load_input_h[] = +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ +const char *load_input_h = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -43,7 +43,7 @@ static const char load_input_h[] = "\n" "#endif // MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_\n"; -static const char load_input_c[] = +const char *load_input_c = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -131,11 +131,11 @@ static const char load_input_c[] = " int size = 0;\n" " buffers[i] = ReadInputData(inputs_path[i], &size);\n" " if (size != inputs_size[i] || buffers[i] == NULL) {\n" - " printf(\"size mismatch, %s, %d, %d\\n\", inputs_path[i], size, inputs_size[i]);\n" + " printf(\"size mismatch, %s, input: %d, needed: %d\\n\", inputs_path[i], size, inputs_size[i]);\n" " return -1;\n" " }\n" " }\n" " return 0;\n" "}\n"; -#endif // MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_BENCH_LOAD_INPUT_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h index eacb7416ed..79eb4484dc 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/micro_tensor.h @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ -#define MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ -static const char micro_tensor_h[] = +const char *micro_tensor_h = "/**\n" " * Copyright 2021 Huawei Technologies Co., Ltd\n" " *\n" @@ -42,20 +42,8 @@ static const char micro_tensor_h[] = "#include \n" "#include \n" "\n" - "inline bool IsPrint() {\n" - " char *env = getenv(\"GLOG_v\");\n" - " if (env == NULL) {\n" - " return false;\n" - " }\n" - " return strcmp(env, \"1\") == 0;\n" - "}\n" - "\n" - "#define MICRO_INFO(content, args...) \\\n" - " { \\\n" - " if (IsPrint()) { \\\n" - " printf(\"[INFO] %s|%d: \" #content \"\\r\\n\", __func__, __LINE__, ##args); \\\n" - " } \\\n" - " }\n" + "#define MICRO_INFO(content, args...) \\\n" + " { printf(\"[INFO] %s|%d: \" #content \"\\r\\n\", __func__, __LINE__, ##args); }\n" "#define MICRO_ERROR(content, args...) \\\n" " { printf(\"[ERROR] %s|%d: \" #content \"\\r\\n\", __func__, __LINE__, ##args); }\n" "\n" @@ -115,4 +103,4 @@ static const char micro_tensor_h[] = "} GraphQuantArgs;\n" "\n" "#endif // MSMICRO_TENSOR_H\n"; -#endif // MICRO_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_MICRO_TENSOR_H_ diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h new file mode 100644 index 0000000000..959eacdca4 --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/thread_pool.h @@ -0,0 +1,99 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_THREAD_POOL_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_THREAD_POOL_H_ + +namespace mindspore::lite::micro { + +const char *thread_pool_h = + "/**\n" + " * Copyright 2021 Huawei Technologies Co., Ltd\n" + " *\n" + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + " * you may not use this file except in compliance with the License.\n" + " * You may obtain a copy of the License at\n" + " *\n" + " * http://www.apache.org/licenses/LICENSE-2.0\n" + " *\n" + " * Unless required by applicable law or agreed to in writing, software\n" + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + " * See the License for the specific language governing permissions and\n" + " * limitations under the License.\n" + " */\n" + "\n" + "#ifndef MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n" + "#define MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n" + "\n" + "#include \n" + "\n" + "#define MAX_TASK_NUM (2)\n" + "\n" + "/// \\brief BindMode defined for holding bind cpu strategy argument.\n" + "typedef enum {\n" + " NO_BIND_MODE = 0, /**< no bind */\n" + " HIGHER_MODE = 1, /**< bind higher cpu first */\n" + " MID_MODE = 2 /**< bind middle cpu first */\n" + "} BindMode;\n" + "\n" + "struct ThreadPool;\n" + "\n" + "struct ThreadPool *CreateThreadPool(int thread_num, int mode);\n" + "\n" + "/**\n" + " *\n" + " * @param session_index, support multi session\n" + " * @param job\n" + " * @param content\n" + " * @param task_num\n" + " */\n" + "int ParallelLaunch(struct ThreadPool *thread_pool, int (*job)(void *, int), void *content, int task_num);\n" + "\n" + "/**\n" + " * bind each thread to specified cpu core\n" + " * @param is_bind\n" + " * @param mode\n" + " */\n" + "int BindThreads(struct ThreadPool *thread_pool, bool is_bind, int mode);\n" + "\n" + "/**\n" + " * activate the thread pool\n" + " * @param thread_pool_id\n" + " */\n" + "void ActivateThreadPool(struct ThreadPool *thread_pool);\n" + "\n" + "/**\n" + " * deactivate the thread pool\n" + " * @param thread_pool_id\n" + " */\n" + "void DeactivateThreadPool(struct ThreadPool *thread_pool);\n" + "\n" + "/**\n" + " *\n" + " * @return current thread num\n" + " */\n" + "int GetCurrentThreadNum(struct ThreadPool *thread_pool);\n" + "\n" + "/**\n" + " * destroy thread pool, and release resource\n" + " */\n" + "void DestroyThreadPool(struct ThreadPool *thread_pool);\n" + "\n" + "#endif // MINDSPORE_LITE_SRC_RUNTIME_THREAD_POOL_H_\n"; +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_CONST_BLOCKS_THREAD_POOL_H_ diff --git a/mindspore/lite/micro/coder/generator/component/parallel_component.cc b/mindspore/lite/micro/coder/generator/component/parallel_component.cc new file mode 100644 index 0000000000..a4083498ab --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/parallel_component.cc @@ -0,0 +1,61 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/generator/component/parallel_component.h" +#include + +namespace mindspore::lite::micro { + +void CodeCreateThreadPool(std::ofstream &ofs, const std::string &module_name) { + ofs << " int thread_num = 4;\n" + " BindMode bind_mode = NO_BIND_MODE;\n" + " if (argc >= 6) {\n" + " thread_num = atoi(argv[4]);\n" + " bind_mode = atoi(argv[5]);\n" + " }\n" + " struct ThreadPool *thread_pool = CreateThreadPool(thread_num, bind_mode);\n" + " if (thread_pool == NULL) {\n" + " MICRO_ERROR(\"create thread pool failed\");\n" + " return RET_ERROR;\n" + " }\n" + << " ret = " << module_name << "_SetThreadPool(thread_pool);\n" + << " if (ret != RET_OK) {\n" + " MICRO_ERROR(\"set global thread pool failed\");\n" + " return RET_ERROR;\n" + " }\n" + " MICRO_INFO(\"config: ThreadNum: %d, BindMode: %d\", thread_num, bind_mode);\n"; +} + +void CodeDestroyThreadPool(std::ofstream &ofs) { ofs << " DestroyThreadPool(thread_pool);\n"; } + +void CodeSetGlobalThreadPoolState(std::ofstream &ofs, const std::string &module_name) { + ofs << "/*\n" + " * set global thread pool, which is created by user\n" + " */\n" + << "int " << module_name << "_SetThreadPool(struct ThreadPool *thread_pool);\n\n"; +} + +void CodeSetGlobalThreadPoolImplement(std::ofstream &ofs, const std::string &module_name) { + ofs << "struct ThreadPool *g_thread_pool = NULL;\n" + << "int " << module_name << "_SetThreadPool(struct ThreadPool *thread_pool) {\n" + << " if (thread_pool == NULL) {\n" + " return RET_ERROR;\n" + " }\n" + " g_thread_pool = thread_pool;\n" + " return RET_OK;\n" + "}\n"; +} +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/generator/component/parallel_component.h b/mindspore/lite/micro/coder/generator/component/parallel_component.h new file mode 100644 index 0000000000..f92cad26ec --- /dev/null +++ b/mindspore/lite/micro/coder/generator/component/parallel_component.h @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_GENERATOR_PARALLEL_COMPONENT_H_ +#define MINDSPORE_LITE_MICRO_CODER_GENERATOR_PARALLEL_COMPONENT_H_ + +#include +#include + +namespace mindspore::lite::micro { + +void CodeCreateThreadPool(std::ofstream &ofs, const std::string &module_name); + +void CodeDestroyThreadPool(std::ofstream &ofs); + +void CodeSetGlobalThreadPoolState(std::ofstream &ofs, const std::string &module_name); + +void CodeSetGlobalThreadPoolImplement(std::ofstream &ofs, const std::string &module_name); + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_GENERATOR_PARALLEL_COMPONENT_H_ diff --git a/mindspore/lite/micro/coder/generator/component/weight_component.cc b/mindspore/lite/micro/coder/generator/component/weight_component.cc index 6475e5f13c..28d2929c5b 100644 --- a/mindspore/lite/micro/coder/generator/component/weight_component.cc +++ b/mindspore/lite/micro/coder/generator/component/weight_component.cc @@ -74,10 +74,10 @@ void CodeModelParamsForNet(std::ofstream &hofs, std::ofstream &cofs, const std:: continue; } if (tensor->category() == Tensor::Category::CONST_TENSOR) { - hofs << "extern " << GetTensorDataType(tensor->data_type()) << name << " = [];\n"; - cofs << GetTensorDataType(tensor->data_type()) << name << " = [" << tensor->ElementsNum() << "];\n"; + hofs << "extern " << GetTensorDataType(tensor->data_type()) << name << "[];\n"; + cofs << GetTensorDataType(tensor->data_type()) << name << "[" << tensor->ElementsNum() << "];\n"; } else if (tensor->category() == Tensor::Category::VAR) { - hofs << "extern " << GetTensorDataType(tensor->data_type()) << " *" << name << ";\n"; + hofs << "extern " << GetTensorDataType(tensor->data_type()) << "*" << name << ";\n"; cofs << GetTensorDataType(tensor->data_type()) << "*" << name << " = NULL;\n"; } } @@ -87,7 +87,6 @@ void CodeModelParamsForNet(std::ofstream &hofs, std::ofstream &cofs, const std:: void CodeWeightInitFunc(std::ofstream &ofs, const std::string &module_name, const std::unique_ptr &ctx) { ofs << "int " << module_name << "_Init(void *weight_buffer, int weight_size) {\n" << " if (weight_buffer == NULL) {\n" - " MICRO_ERROR(\"weight buffer is NULL\");\n" << " return RET_ERROR;\n" << " }\n"; @@ -106,8 +105,9 @@ void CodeWeightInitFunc(std::ofstream &ofs, const std::string &module_name, cons if (tensor->category() != Tensor::Category::CONST_TENSOR) { continue; } - auto iter = ctx->tensors_map().find(tensor); - if (iter != ctx->tensors_map().end()) { + std::map ctx_tensor_map = ctx->tensors_map(); + auto iter = ctx_tensor_map.find(tensor); + if (iter != ctx_tensor_map.end()) { origins += " {" + name + ", " + std::to_string(tensor->Size()) + ", " + std::to_string(offset) + "},\n"; params_num++; } else { @@ -115,14 +115,14 @@ void CodeWeightInitFunc(std::ofstream &ofs, const std::string &module_name, cons params += " " + GetTensorDataType(data_type) + "*" + name + " = (weight_buffer + " + std::to_string(offset) + ");\n"; } + offset += tensor->Size(); } - ofs << " struct ModelParameter model_params[] = {\n" << origins << " };\n"; ofs << params << "\n"; + ofs << " struct ModelParameter model_params[] = {\n" << origins << " };\n"; ofs << "\n"; ofs << " for(int i = 0; i < " << params_num << "; ++i) {\n" << " if (model_params[i].offset + model_params[i].size > weight_size) {\n" - " MICRO_ERROR(\"buffer is invalid, size: %d, offset: %lu\", weight_size, model_params[i].offset);\n" " return RET_ERROR;\n" " }\n" << " memcpy(model_params[i].addr, (weight_buffer + model_params[i].offset), model_params[i].size);\n" diff --git a/mindspore/lite/micro/coder/generator/generator.cc b/mindspore/lite/micro/coder/generator/generator.cc index 7a8cf54d33..dce78c20db 100644 --- a/mindspore/lite/micro/coder/generator/generator.cc +++ b/mindspore/lite/micro/coder/generator/generator.cc @@ -24,8 +24,9 @@ #include "coder/generator/component/const_blocks/cmake_lists.h" #include "coder/generator/component/const_blocks/debug_utils.h" #include "coder/generator/component/const_blocks/load_input.h" +#include "coder/generator/component/const_blocks/thread_pool.h" #include "coder/generator/component/const_blocks/license.h" -#include "micro/coder/log.h" +#include "coder/log.h" namespace mindspore::lite::micro { int WriteContentToFile(const std::string &file, const std::string &content) { @@ -61,11 +62,13 @@ Generator::~Generator() { (void)umask(origin_umask_); } void Generator::CodeNetRunFunc(std::ofstream &ofs) { // generate net inference code ofs << "void " << config_->module_name() << "_Inference() {\n"; - if (config_->code_mode() == CodeMode::Code_Inference) { - ofs << "int thread_num = GetCurrentThreadNum(THREAD_POOL_DEFAULT);\n"; + if (config_->support_parallel()) { + ofs << " const int g_thread_num = GetCurrentThreadNum(g_thread_pool);\n"; + } else { + ofs << " const int g_thread_num = 1;\n"; } for (const auto &block : ctx_->code_blocks()) { - ofs << "\t{\n" << block << "\t}\n"; + ofs << " {\n" << block << " }\n"; } ofs << "}\n"; } @@ -98,7 +101,7 @@ int Generator::CodeSourceCMakeFile() { } int Generator::CodeStaticContent() { - const std::vector> static_blocks = { + std::vector> static_blocks = { {net_inc_file_path_ + "microtensor.h", micro_tensor_h}, {net_src_file_path_ + "CMakeLists.txt", src_cmake_lists_txt}, {net_main_file_path_ + "debug_utils.h", debug_utils_h}, @@ -106,12 +109,13 @@ int Generator::CodeStaticContent() { {net_main_file_path_ + "load_input.h", load_input_h}, {net_main_file_path_ + "load_input.c", load_input_c}, {net_main_file_path_ + "CMakeLists.txt", bench_cmake_lists_txt}}; + if (config_->support_parallel()) { + static_blocks.emplace_back(net_inc_file_path_ + "thread_pool.h", thread_pool_h); + } for (const auto &static_block : static_blocks) { std::string file_name = static_block.first; std::string content = static_block.second; - if (WriteContentToFile(file_name, content) != RET_OK) { - return RET_ERROR; - } + MS_CHECK_RET_CODE(WriteContentToFile(file_name, content), "write file failed"); } return RET_OK; } diff --git a/mindspore/lite/micro/coder/generator/inference/inference_generator.cc b/mindspore/lite/micro/coder/generator/inference/inference_generator.cc index cfa4c57830..5a29bd7582 100644 --- a/mindspore/lite/micro/coder/generator/inference/inference_generator.cc +++ b/mindspore/lite/micro/coder/generator/inference/inference_generator.cc @@ -18,6 +18,7 @@ #include #include #include "coder/generator/component/common_component.h" +#include "coder/generator/component/parallel_component.h" #include "coder/generator/component/benchmark_component.h" #include "coder/generator/component/const_blocks/license.h" @@ -28,14 +29,17 @@ int InferenceGenerator::CodeNetHFile() { MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); MS_LOG(INFO) << "write " << net_include_file; ofs << g_hwLicense; - if (config_->code_mode() == CodeMode::Code_Inference) { - ofs << "#include \"src/runtime/thread_pool.h\"\n"; + if (config_->support_parallel()) { + ofs << "#include \"thread_pool.h\"\n"; } ofs << "#include \"microtensor.h\"\n\n"; CodeInputAndOutputState(ofs, config_->module_name()); if (is_get_quant_args_) { CodeGraphQuantArgsState(ofs, config_->module_name()); } + if (config_->support_parallel()) { + CodeSetGlobalThreadPoolState(ofs, config_->module_name()); + } if (config_->is_weight_file()) { CodeInitWeightState(ofs, config_->module_name()); } @@ -50,6 +54,9 @@ int InferenceGenerator::CodeNetCFile() { MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); MS_LOG(INFO) << "write " << net_impl_file; CodeSourceFileInclude(ofs, net_weight_hfile_, net_inc_hfile_); + if (config_->support_parallel()) { + CodeSetGlobalThreadPoolImplement(ofs, config_->module_name()); + } CodeInputAndOutputImplement(ofs, config_->module_name(), ctx_); CodeInitResourceImplement(ofs, config_->module_name(), ctx_); CodeFreeResourceImplement(ofs, config_->module_name(), ctx_); @@ -78,12 +85,14 @@ int InferenceGenerator::CodeBenchmarkFile() { if (config_->is_weight_file()) { CodeBenchmarkInitWeight(ofs, config_->module_name()); } - if (config_->code_mode() == CodeMode::Code_Inference) { - CodeBenchmarkConfigThread(ofs); + if (config_->support_parallel()) { + CodeCreateThreadPool(ofs, config_->module_name()); } CodeBenchmarkInference(ofs, config_->module_name()); CodeBenchmarkPrintOutputs(ofs, config_->module_name()); - + if (config_->support_parallel()) { + CodeDestroyThreadPool(ofs); + } CodeBenchmarkFreeResourse(ofs, config_->module_name(), inputs_num); ofs.close(); return RET_OK; diff --git a/mindspore/lite/micro/coder/generator/inference/inference_generator.h b/mindspore/lite/micro/coder/generator/inference/inference_generator.h index 60f8092a93..78c8dca218 100644 --- a/mindspore/lite/micro/coder/generator/inference/inference_generator.h +++ b/mindspore/lite/micro/coder/generator/inference/inference_generator.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/generator/generator.h" +#include "coder/generator/generator.h" namespace mindspore::lite::micro { class InferenceGenerator : public Generator { diff --git a/mindspore/lite/micro/coder/generator/train/train_generator.cc b/mindspore/lite/micro/coder/generator/train/train_generator.cc index bfb7d87d0b..fb908c4d9d 100644 --- a/mindspore/lite/micro/coder/generator/train/train_generator.cc +++ b/mindspore/lite/micro/coder/generator/train/train_generator.cc @@ -39,7 +39,7 @@ int TrainGenerator::CodeNetHFile() { MS_CHECK_TRUE(!ofs.bad(), "filed to open file"); MS_LOG(INFO) << "write " << net_include_file; ofs << g_hwLicense; - if (config_->code_mode() == CodeMode::Code_Inference) { + if (config_->code_mode() == CodeMode::Inference) { ofs << "#include \"src/runtime/thread_pool.h\"\n"; } ofs << "#include \"microtensor.h\"\n\n"; diff --git a/mindspore/lite/micro/coder/generator/train/train_generator.h b/mindspore/lite/micro/coder/generator/train/train_generator.h index c18fd2b5b1..e22d4f7f91 100644 --- a/mindspore/lite/micro/coder/generator/train/train_generator.h +++ b/mindspore/lite/micro/coder/generator/train/train_generator.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/generator/generator.h" +#include "coder/generator/generator.h" namespace mindspore::lite::micro { class TrainGenerator : public Generator { diff --git a/mindspore/lite/micro/coder/graph.cc b/mindspore/lite/micro/coder/graph.cc index 27980c9e2b..bec25bc076 100644 --- a/mindspore/lite/micro/coder/graph.cc +++ b/mindspore/lite/micro/coder/graph.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "micro/coder/graph.h" +#include "coder/graph.h" #include #include #include @@ -23,7 +23,6 @@ #include #include "coder/log.h" #include "schema/inner/model_generated.h" -#include "src/ops/primitive_c.h" #include "securec/include/securec.h" namespace mindspore::lite::micro { @@ -92,8 +91,15 @@ int CoderGraph::ConvertTensors() { if (quant_params != nullptr) { for (int j = 0; j < static_cast(quant_params->size()); j++) { QuantArg quant_arg{}; + quant_arg.bitNum = quant_params->Get(j)->numBits(); quant_arg.scale = quant_params->Get(j)->scale(); quant_arg.zeroPoint = quant_params->Get(j)->zeroPoint(); + quant_arg.var_corr = quant_params->Get(j)->varCorr(); + quant_arg.mean_corr = quant_params->Get(j)->meanCorr(); + quant_arg.inited = quant_params->Get(j)->inited(); + quant_arg.roundType = quant_params->Get(j)->roundType(); + quant_arg.multiplier = quant_params->Get(j)->multiplier(); + quant_arg.dstDtype = quant_params->Get(j)->dstDtype(); dstTensor->AddQuantParam(quant_arg); } } diff --git a/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.cc index a1aa621a85..4b898da8ee 100644 --- a/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.cc @@ -14,12 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include #include #include "nnacl/fp32/winograd_utils.h" #include "nnacl/int8/quantize.h" -#include "micro/coder/log.h" +#include "coder/log.h" namespace { int MallocConvQuantParams(ConvQuantArg *quant_arg, size_t input_arg_num, size_t filter_arg_num, size_t output_arg_num) { @@ -37,8 +37,8 @@ int MallocConvQuantParams(ConvQuantArg *quant_arg, size_t input_arg_num, size_t } // namespace namespace mindspore::lite::micro { -string Conv2DBaseCoder::LayoutTransformFp32(schema::Format src_format, schema::Format dst_format) { - string ret; +std::string Conv2DBaseCoder::LayoutTransformFp32(schema::Format src_format, schema::Format dst_format) { + std::string ret; if (src_format == schema::Format_NHWC && dst_format == schema::Format_NC4HW4) { ret = "PackNHWCToNC4HW4Fp32"; } else if (src_format == schema::Format_NHWC && dst_format == schema::Format_NHWC4) { @@ -56,8 +56,8 @@ string Conv2DBaseCoder::LayoutTransformFp32(schema::Format src_format, schema::F return ret; } -string Conv2DBaseCoder::LayoutTransformInt8(schema::Format src_format, schema::Format dst_format) { - string ret; +std::string Conv2DBaseCoder::LayoutTransformInt8(schema::Format src_format, schema::Format dst_format) { + std::string ret; if (src_format == schema::Format_NHWC && dst_format == schema::Format_NHWC4) { ret = "PackNHWCToNHWC4Int8"; } else { @@ -67,8 +67,8 @@ string Conv2DBaseCoder::LayoutTransformInt8(schema::Format src_format, schema::F return ret; } -string Conv2DBaseCoder::LayoutTransform(TypeId data_type, schema::Format src_format, schema::Format dst_format) { - string ret; +std::string Conv2DBaseCoder::LayoutTransform(TypeId data_type, schema::Format src_format, schema::Format dst_format) { + std::string ret; switch (data_type) { case kNumberTypeInt8: ret = LayoutTransformInt8(src_format, dst_format); @@ -197,7 +197,7 @@ int Conv2DBaseCoder::SetQuantMultiplier() { return RET_OK; } -int Conv2DBaseCoder::CheckResizeValid() { +int Conv2DBaseCoder::CheckResizeValid() const { // ===============check in channel================= // int32_t filter_in_channel = filter_tensor_->Channel(); int32_t resize_in_channel = input_tensor_->Channel(); @@ -206,12 +206,39 @@ int Conv2DBaseCoder::CheckResizeValid() { return RET_OK; } +void Conv2DBaseCoder::SetRoundingAndMultipilerMode() { + auto input_quant_arg = input_tensor_->quant_params().front(); + int round_type = input_quant_arg.roundType; + switch (round_type) { + case 1: + conv_quant_arg_->round_mode_ = Rounding_Away_from_zero; + break; + case 2: + conv_quant_arg_->round_mode_ = Rounding_Up; + break; + default: + conv_quant_arg_->round_mode_ = Rounding_No; + } + int cal_multiplier_type = input_quant_arg.multiplier; + switch (cal_multiplier_type) { + case 0: + conv_quant_arg_->quant_multiplier_mode_ = Method_SinglePrecision; + break; + case 1: + conv_quant_arg_->quant_multiplier_mode_ = Method_DoublePrecision; + break; + default: + conv_quant_arg_->quant_multiplier_mode_ = Method_No; + } +} + int Conv2DBaseCoder::SetQuantParam() { MS_CHECK_RET_CODE(MallocQuantParam(), "Malloc quant param failed."); MS_CHECK_RET_CODE(SetInputTensorQuantParam(), "Set Input Tensor Quant Param Failed."); MS_CHECK_RET_CODE(SetFilterTensorQuantParam(), "Set Filter Tensor Quant Param Failed."); MS_CHECK_RET_CODE(SetOutputTensorQuantParam(), "Set Output Tensor Quant Param Failed."); MS_CHECK_RET_CODE(SetIfPerChannel(), "Set if per tensor channel failed."); + SetRoundingAndMultipilerMode(); MS_CHECK_RET_CODE(SetQuantMultiplier(), "Set Quant Multiplier Failed."); // now only consider per tensor for output MS_CHECK_PTR(conv_param_->conv_quant_arg_.out_act_min_); diff --git a/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.h index 4ae00fc6e5..982b28f7a7 100644 --- a/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/conv2d_base_coder.h @@ -21,13 +21,11 @@ #include #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "src/runtime/kernel/arm/base/layout_transform.h" #include "nnacl/conv_parameter.h" namespace mindspore::lite::micro { -using std::string; - class Conv2DBaseCoder : public OperatorCoder { public: Conv2DBaseCoder(const std::vector &in_tensors, const std::vector &out_tensors, @@ -47,10 +45,14 @@ class Conv2DBaseCoder : public OperatorCoder { free(conv_quant_arg_->input_quant_args_); free(conv_quant_arg_->filter_quant_args_); free(conv_quant_arg_->output_quant_args_); + conv_param_ = nullptr; + conv_quant_arg_ = nullptr; + filter_tensor_ = nullptr; + bias_tensor_ = nullptr; } protected: - int Init(); + virtual int Init(); int SetQuantParam(); @@ -62,19 +64,21 @@ class Conv2DBaseCoder : public OperatorCoder { int SetOutputTensorQuantParam(); + void SetRoundingAndMultipilerMode(); + int SetQuantMultiplier(); - int CheckResizeValid(); + int CheckResizeValid() const; int SetIfPerChannel(); int CheckLayout(lite::Tensor *input_tensor); - string LayoutTransformFp32(schema::Format src_format, schema::Format dst_format); + std::string LayoutTransformFp32(schema::Format src_format, schema::Format dst_format); - string LayoutTransformInt8(schema::Format src_format, schema::Format dst_format); + std::string LayoutTransformInt8(schema::Format src_format, schema::Format dst_format); - string LayoutTransform(TypeId data_type, schema::Format src_format, schema::Format dst_format); + std::string LayoutTransform(TypeId data_type, schema::Format src_format, schema::Format dst_format); ConvParameter *conv_param_{nullptr}; @@ -84,7 +88,7 @@ class Conv2DBaseCoder : public OperatorCoder { Tensor *bias_tensor_{nullptr}; - string convert_func_; + std::string convert_func_; }; } // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_BASE_CONV2D_BASE_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.cc new file mode 100644 index 0000000000..cd0684d2d9 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.cc @@ -0,0 +1,153 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/base/detection_post_process_base_coder.h" + +#include "nnacl/int8/quant_dtype_cast_int8.h" + +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "include/errorcode.h" + +namespace mindspore::lite::micro { + +int DetectionPostProcessBaseCoder::Prepare(CoderContext *const context) { + MS_CHECK_PTR(parameter_); + params_ = reinterpret_cast(parameter_); + params_->anchors_ = nullptr; + params_->decoded_boxes_ = nullptr; + params_->nms_candidate_ = nullptr; + params_->indexes_ = nullptr; + params_->scores_ = nullptr; + params_->all_class_indexes_ = nullptr; + params_->all_class_scores_ = nullptr; + params_->single_class_indexes_ = nullptr; + params_->selected_ = nullptr; + + Tensor *anchor_tensor = input_tensors_.at(2); + MS_CHECK_PTR(anchor_tensor); + if (anchor_tensor->data_type() == kNumberTypeInt8) { + QuantArg quant_param = anchor_tensor->quant_params().at(0); + auto anchor_int8 = reinterpret_cast(anchor_tensor->data_c()); + MS_CHECK_PTR(anchor_int8); + auto anchor_fp32 = static_cast( + allocator_->Malloc(kNumberTypeFloat, anchor_tensor->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(anchor_fp32); + DoDequantizeInt8ToFp32(anchor_int8, anchor_fp32, quant_param.scale, quant_param.zeroPoint, + anchor_tensor->ElementsNum()); + params_->anchors_ = anchor_fp32; + } else if (anchor_tensor->data_type() == kNumberTypeUInt8) { + QuantArg quant_param = anchor_tensor->quant_params().front(); + auto anchor_uint8 = reinterpret_cast(anchor_tensor->data_c()); + MS_CHECK_PTR(anchor_uint8); + auto anchor_fp32 = static_cast( + allocator_->Malloc(kNumberTypeFloat, anchor_tensor->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(anchor_fp32); + DoDequantizeUInt8ToFp32(anchor_uint8, anchor_fp32, quant_param.scale, quant_param.zeroPoint, + anchor_tensor->ElementsNum()); + params_->anchors_ = anchor_fp32; + } else if (anchor_tensor->data_type() == kNumberTypeFloat32 || anchor_tensor->data_type() == kNumberTypeFloat) { + params_->anchors_ = static_cast( + allocator_->Malloc(kNumberTypeFloat, anchor_tensor->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(params_->anchors_); + memcpy(params_->anchors_, anchor_tensor->data_c(), anchor_tensor->Size()); + } else { + MS_LOG(ERROR) << "unsupported anchor data type " << anchor_tensor->data_type(); + return RET_ERROR; + } + MS_CHECK_RET_CODE(AllocateBuffer(), "AllocateBuffer failed"); + MS_CHECK_RET_CODE(MallocInputsBuffer(), "malloc inputs buffer failed"); + return RET_OK; +} + +int DetectionPostProcessBaseCoder::AllocateBuffer() { + MS_CHECK_PTR(input_tensors_.at(0)); + MS_CHECK_PTR(input_tensors_.at(1)); + num_boxes_ = input_tensors_.at(0)->shape().at(1); + num_classes_with_bg_ = input_tensors_.at(1)->shape().at(2); + params_->decoded_boxes_ = allocator_->Malloc(kNumberTypeFloat, num_boxes_ * 4 * sizeof(float), kWorkspace); + MS_CHECK_PTR(params_->decoded_boxes_); + params_->nms_candidate_ = allocator_->Malloc(kNumberTypeUInt8, num_boxes_ * sizeof(uint8_t), kWorkspace); + MS_CHECK_PTR(params_->nms_candidate_); + params_->selected_ = allocator_->Malloc(kNumberTypeInt, num_boxes_ * sizeof(int), kWorkspace); + MS_CHECK_PTR(params_->selected_); + params_->single_class_indexes_ = allocator_->Malloc(kNumberTypeInt, num_boxes_ * sizeof(int), kWorkspace); + MS_CHECK_PTR(params_->single_class_indexes_); + + if (params_->use_regular_nms_) { + params_->scores_ = + allocator_->Malloc(kNumberTypeFloat, (num_boxes_ + params_->max_detections_) * sizeof(float), kWorkspace); + MS_CHECK_PTR(params_->scores_); + params_->indexes_ = + allocator_->Malloc(kNumberTypeInt, (num_boxes_ + params_->max_detections_) * sizeof(int), kWorkspace); + MS_CHECK_PTR(params_->indexes_); + params_->all_class_scores_ = + allocator_->Malloc(kNumberTypeFloat, (num_boxes_ + params_->max_detections_) * sizeof(float), kWorkspace); + MS_CHECK_PTR(params_->all_class_scores_); + params_->all_class_indexes_ = + allocator_->Malloc(kNumberTypeInt, (num_boxes_ + params_->max_detections_) * sizeof(int), kWorkspace); + MS_CHECK_PTR(params_->all_class_indexes_); + } else { + params_->scores_ = allocator_->Malloc(kNumberTypeFloat, num_boxes_ * sizeof(float), kWorkspace); + MS_CHECK_PTR(params_->scores_); + params_->indexes_ = + allocator_->Malloc(kNumberTypeFloat, num_boxes_ * params_->num_classes_ * sizeof(int), kWorkspace); + MS_CHECK_PTR(params_->indexes_); + } + return RET_OK; +} + +int DetectionPostProcessBaseCoder::DoCode(CoderContext *const context) { + Collect(context, {"nnacl/detection_post_process_parameter.h", "wrapper/base/detection_post_process_base_wrapper.h"}, + {"detection_post_process_fp32.c", "detection_post_process_base_wrapper.c"}); + + Serializer code; + MS_CHECK_RET_CODE(GetInputData(context, &code), "GetInputData failed"); + Tensor *output_boxes = output_tensors_.at(0); + Tensor *output_classes = output_tensors_.at(1); + Tensor *output_scores = output_tensors_.at(2); + Tensor *output_num = output_tensors_.at(3); + + code.CodeBaseStruct("DetectionPostProcessParameter", "params", params_->op_parameter_, params_->h_scale_, + params_->w_scale_, params_->x_scale_, params_->y_scale_, params_->nms_iou_threshold_, + params_->nms_score_threshold_, params_->max_detections_, params_->detections_per_class_, + params_->max_classes_per_detection_, params_->num_classes_, params_->use_regular_nms_, + params_->out_quantized_, params_->anchors_, params_->decoded_boxes_, params_->nms_candidate_, + params_->indexes_, params_->scores_, params_->all_class_indexes_, params_->all_class_scores_, + params_->single_class_indexes_, params_->selected_); + + code.CodeFunction("DecodeBoxes", num_boxes_, input_boxes_, params_->anchors_, "¶ms"); + + if (params_->use_regular_nms_) { + code.CodeFunction("DetectionPostProcessRegular", num_boxes_, num_classes_with_bg_, input_scores_, output_boxes, + output_classes, output_scores, output_num, "PartialArgSort", "¶ms"); + } else { + int task_id = 0; + int thread_num = 1; + code.CodeFunction("NmsMultiClassesFastCore", num_boxes_, num_classes_with_bg_, input_scores_, "PartialArgSort", + "¶ms", task_id, thread_num); + + code.CodeFunction("DetectionPostProcessFast", num_boxes_, num_classes_with_bg_, input_scores_, + "(float *)(params.decoded_boxes_)", output_boxes, output_classes, output_scores, output_num, + "PartialArgSort", "¶ms"); + } + + context->AppendCode(code.str()); + + return RET_OK; +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.h new file mode 100644 index 0000000000..dceaaf562e --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/base/detection_post_process_base_coder.h @@ -0,0 +1,54 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_BASE_DETECTION_POST_PROCESS_BASE_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_BASE_DETECTION_POST_PROCESS_BASE_CODER_H_ + +#include +#include +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/detection_post_process_parameter.h" +#include "coder/opcoders/serializers/serializer.h" + +namespace mindspore::lite::micro { + +class DetectionPostProcessBaseCoder : public OperatorCoder { + public: + DetectionPostProcessBaseCoder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~DetectionPostProcessBaseCoder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + protected: + int AllocateBuffer(); + virtual int GetInputData(CoderContext *const context, Serializer *const coder) = 0; + virtual int MallocInputsBuffer() = 0; + + int num_boxes_{0}; + int num_classes_with_bg_{0}; + float *input_boxes_{nullptr}; + float *input_scores_{nullptr}; + DetectionPostProcessParameter *params_{nullptr}; +}; +} // namespace mindspore::lite::micro +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_BASE_DETECTION_POST_PROCESS_BASE_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.cc b/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.cc index 409cbb4bbb..f962d75311 100644 --- a/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.cc @@ -15,7 +15,7 @@ */ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "micro/coder/opcoders/file_collector.h" #include "micro/coder/opcoders/base/dtype_cast_coder.h" #include "micro/coder/opcoders/serializers/serializer.h" diff --git a/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.h b/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.h index 7e14d21bd0..86087e61dc 100644 --- a/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/dtype_cast_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/int8/quant_dtype_cast_int8.h" namespace mindspore::lite::micro { diff --git a/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.cc index 18923e9c65..04c1869066 100644 --- a/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.cc @@ -14,10 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/base/full_connection_base_coder.h" +#include "coder/opcoders/base/full_connection_base_coder.h" namespace mindspore::lite::micro { -FullConnectionBaseCoder::~FullConnectionBaseCoder() { fc_param_ = nullptr; } +FullConnectionBaseCoder::~FullConnectionBaseCoder() { + fc_param_ = nullptr; + filter_tensor_ = nullptr; + bias_tensor_ = nullptr; +} int FullConnectionBaseCoder::Init() { this->fc_param_ = reinterpret_cast(parameter_); diff --git a/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.h index efff937f11..41a2f2e25f 100644 --- a/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/full_connection_base_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_BASE_FULLY_CONNECTED_BASE_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/matmul_parameter.h" namespace mindspore::lite::micro { @@ -29,7 +29,8 @@ class FullConnectionBaseCoder : public OperatorCoder { : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} ~FullConnectionBaseCoder() override; - int Init(); + + virtual int Init(); protected: MatMulParameter *fc_param_{nullptr}; diff --git a/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.cc b/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.cc index 5598638a27..707b39a534 100644 --- a/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.cc @@ -14,61 +14,72 @@ * limitations under the License. */ -#include -#include "micro/coder/opcoders/op_coder.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/base/quant_dtype_cast_coder.h" -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/op_coder.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/base/quant_dtype_cast_coder.h" +#include "coder/opcoders/serializers/serializer.h" +#include "coder/utils/type_cast.h" using mindspore::schema::PrimitiveType_QuantDTypeCast; namespace mindspore::lite::micro { - int QuantDTypeCastCoder::Prepare(CoderContext *const context) { - this->cast_param_ = reinterpret_cast(parameter_); - - if (cast_param_->srcT == kNumberTypeFloat32 && cast_param_->dstT == kNumberTypeInt8) { - if (input_tensor_->data_type() != kNumberTypeFloat32 || output_tensor_->data_type() != kNumberTypeInt8) { - MS_LOG(ERROR) << "cast_param_ data type and tensor data type do not match."; - return RET_ERROR; - } - inverse_ = false; - } else if (cast_param_->srcT == kNumberTypeInt8 && cast_param_->dstT == kNumberTypeFloat32) { - if (input_tensor_->data_type() != kNumberTypeInt8 || output_tensor_->data_type() != kNumberTypeFloat32) { - MS_LOG(ERROR) << "cast_param_ data type and tensor data type do not match."; - return RET_ERROR; - } - inverse_ = true; - } else { - MS_LOG(ERROR) << "cast_param_ data type not supported:" - << " src: " << cast_param_->srcT << " dst: " << cast_param_->dstT; - return RET_PARAM_INVALID; + auto *param = reinterpret_cast(parameter_); + if (input_tensor_->data_type() != static_cast(param->srcT) || + output_tensor_->data_type() != static_cast(param->dstT)) { + MS_LOG(ERROR) << "param data type not supported:" + << " src: " << param->srcT << " dst: " << param->dstT; + return RET_ERROR; } + src_dtype = static_cast(param->srcT); + dst_dtype = static_cast(param->dstT); return RET_OK; } int QuantDTypeCastCoder::DoCode(CoderContext *const context) { - // get quant params - QuantArg in_quant_arg = input_tensor_->quant_params().at(0); - - // single thread for now + if (input_tensor_->quant_params().empty() && output_tensor_->quant_params().empty()) { + MS_LOG(ERROR) << "QuantDTypeCast need quantization parameters which is not found."; + return RET_ERROR; + } + auto quant_arg = (!output_tensor_->quant_params().empty() && output_tensor_->quant_params().at(0).inited) + ? output_tensor_->quant_params().at(0) + : input_tensor_->quant_params().at(0); int num_unit_thread = input_tensor_->ElementsNum(); - // generate code .h .c Collect(context, {"nnacl/int8/quant_dtype_cast_int8.h"}, {"quant_dtype_cast_int8.c"}); - Serializer code; code.precision(kPrecision); - std::string function = inverse_ ? "DoDequantizeInt8ToFp32" : "DoQuantizeFp32ToInt8"; - code.CodeFunction(function, input_tensor_, output_tensor_, in_quant_arg.scale, in_quant_arg.zeroPoint, - num_unit_thread); - + if (src_dtype == TypeId::kNumberTypeInt8 && dst_dtype == TypeId::kNumberTypeFloat32) { + code.CodeFunction("DoDequantizeInt8ToFp32", input_tensor_, output_tensor_, quant_arg.scale, quant_arg.zeroPoint, + num_unit_thread); + } else if (src_dtype == TypeId::kNumberTypeFloat32 && dst_dtype == TypeId::kNumberTypeInt8) { + bool from_uint8_src = false; + if (quant_arg.dstDtype == TypeId::kNumberTypeUInt8) { + from_uint8_src = true; + } + code.CodeFunction("DoQuantizeFp32ToInt8", input_tensor_, output_tensor_, quant_arg.scale, quant_arg.zeroPoint, + num_unit_thread, from_uint8_src); + } else if (src_dtype == TypeId::kNumberTypeInt8 && dst_dtype == TypeId::kNumberTypeUInt8) { + code.CodeFunction("Int8ToUInt8", input_tensor_, output_tensor_, num_unit_thread); + } else if (src_dtype == TypeId::kNumberTypeUInt8 && dst_dtype == TypeId::kNumberTypeFloat32) { + code.CodeFunction("DoDequantizeUInt8ToFp32", input_tensor_, output_tensor_, quant_arg.scale, quant_arg.zeroPoint, + num_unit_thread); + } else if (src_dtype == TypeId::kNumberTypeFloat32 && dst_dtype == TypeId::kNumberTypeUInt8) { + code.CodeFunction("DoQuantizeFp32ToUInt8", input_tensor_, output_tensor_, quant_arg.scale, quant_arg.zeroPoint, + num_unit_thread); + } else if (src_dtype == TypeId::kNumberTypeUInt8 && dst_dtype == TypeId::kNumberTypeInt8) { + code.CodeFunction("UInt8ToInt8", input_tensor_, output_tensor_, num_unit_thread); + } else { + MS_LOG(INFO) << "unsupported type cast, src: " << EnumNameDataType(src_dtype) + << ", dst: " << EnumNameDataType(dst_dtype); + return RET_ERROR; + } context->AppendCode(code.str()); - return RET_OK; } REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_QuantDTypeCast, CPUOpCoderCreator) REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_QuantDTypeCast, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeUInt8, PrimitiveType_QuantDTypeCast, CPUOpCoderCreator) } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.h b/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.h index 7968c82563..276f6c967d 100644 --- a/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/quant_dtype_cast_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/int8/quant_dtype_cast_int8.h" namespace mindspore::lite::micro { @@ -36,10 +36,8 @@ class QuantDTypeCastCoder final : public OperatorCoder { int DoCode(CoderContext *const context) override; private: - QuantDTypeCastParameter *cast_param_{nullptr}; - std::vector inputs_; - std::vector outputs_; - bool inverse_{false}; + TypeId src_dtype{kTypeUnknown}; + TypeId dst_dtype{kTypeUnknown}; int thread_num_{0}; int thread_n_num_{0}; int thread_n_stride_{0}; diff --git a/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.cc index 862f096f9f..1224f7288d 100644 --- a/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.cc @@ -14,16 +14,16 @@ * limitations under the License. */ -#include "micro/coder/opcoders/base/reduce_base_coder.h" +#include "coder/opcoders/base/reduce_base_coder.h" #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro { namespace { constexpr size_t kInputNum = 1; constexpr size_t kOutputNum = 1; } // namespace -int ReduceBaseCoder::CheckInputsOutputs() { +int ReduceBaseCoder::CheckInputsOutputs() const { if (input_tensors_.size() < kInputNum) { MS_LOG(ERROR) << "Reduce inputs size should be at least " << kInputNum << " but got " << input_tensors_.size(); return RET_ERROR; diff --git a/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.h index 1b9ee3b5fe..b942c31bb9 100644 --- a/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/reduce_base_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/reduce_parameter.h" namespace mindspore::lite::micro { @@ -31,11 +31,10 @@ class ReduceBaseCoder : public OperatorCoder { ~ReduceBaseCoder() override = default; - int Init(); - virtual int ReSize(); + virtual int Init(); private: - int CheckInputsOutputs(); + int CheckInputsOutputs() const; int CheckParameters(); protected: @@ -54,6 +53,7 @@ class ReduceBaseCoder : public OperatorCoder { int outer_size_{0}; int inner_size_{0}; int axis_size_{0}; + virtual int ReSize(); }; } // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_REDUCE_BASE_CODER_H diff --git a/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.cc new file mode 100644 index 0000000000..737759a868 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.cc @@ -0,0 +1,104 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/base/resize_base_coder.h" +#include "coder/opcoders/op_coder.h" + +namespace mindspore::lite::micro { +constexpr int kMaxInputNum = 2; +constexpr int kOutputNum = 1; +constexpr int kSingleNum = 1; +constexpr int kDoubleNum = 2; +constexpr int kQuadrupleNum = 4; + +int ResizeBaseCoder::CheckParameters() { + auto parameter = reinterpret_cast(parameter_); + if (parameter == nullptr) { + MS_LOG(ERROR) << "cast ResizeParameter failed."; + return RET_NULL_PTR; + } + method_ = parameter->method_; + if (method_ != static_cast(schema::ResizeMethod_LINEAR) && + method_ != static_cast(schema::ResizeMethod_NEAREST)) { + MS_LOG(ERROR) << "Resize method should be bilinear or nearest_neighbor, but got " << method_; + return RET_INVALID_OP_ATTR; + } + if (this->input_tensors_.size() == kSingleNum) { + new_height_ = parameter->new_height_; + if (new_height_ < 1) { + MS_LOG(ERROR) << "Resize new_height should >= 1, but got " << new_height_; + return RET_INVALID_OP_ATTR; + } + new_width_ = parameter->new_width_; + if (new_width_ < 1) { + MS_LOG(ERROR) << "Resize new_width should >= 1, but got " << new_width_; + return RET_INVALID_OP_ATTR; + } + } else if (this->input_tensors_.size() == kDoubleNum) { + auto out_shape = this->input_tensors_.at(1)->data_c(); + if (out_shape == nullptr) { + MS_LOG(INFO) << "Out shape is not assigned"; + const_shape_ = false; + } else { + const_shape_ = true; + } + } + coordinate_transform_mode_ = parameter->coordinate_transform_mode_; + preserve_aspect_ratio_ = parameter->preserve_aspect_ratio_; + if (preserve_aspect_ratio_) { + MS_LOG(ERROR) << "Resize currently not support preserve_aspect_ratio true"; + return RET_ERROR; + } + return RET_OK; +} + +int ResizeBaseCoder::CheckInputsOuputs() { + if (input_tensors_.size() <= kQuadrupleNum) { + if (std::any_of(input_tensors_.begin(), input_tensors_.end(), [](const Tensor *t) { return t == nullptr; })) { + return RET_NULL_PTR; + } + } else { + MS_LOG(ERROR) << "Resize input num should be no more than" << kMaxInputNum << ", but got " << input_tensors_.size(); + return RET_ERROR; + } + if (output_tensors_.size() != kOutputNum) { + MS_LOG(ERROR) << "Resize output num should be " << kOutputNum << ", but got " << output_tensors_.size(); + return RET_ERROR; + } + auto output = output_tensors_.at(0); + if (output == nullptr) { + return RET_NULL_PTR; + } + return RET_OK; +} + +int ResizeBaseCoder::Init() { + auto ret = CheckParameters(); + if (ret != RET_OK) { + return ret; + } + ret = CheckInputsOuputs(); + if (ret != RET_OK) { + return ret; + } + auto input_shape = input_tensor_->shape(); + if (!input_shape.empty() && input_shape.size() != COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "Resize op support input rank 4, got " << input_shape.size(); + return RET_ERROR; + } + return RET_OK; +} +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.h new file mode 100644 index 0000000000..1d2ccdc05a --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/base/resize_base_coder.h @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_RESIZE_BASE_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_RESIZE_BASE_CODER_H_ + +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/resize_parameter.h" + +namespace mindspore::lite::micro { +class ResizeBaseCoder : public OperatorCoder { + public: + ResizeBaseCoder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~ResizeBaseCoder() override = default; + + int Init(); + + protected: + int method_{0}; + int new_height_{0}; + int new_width_{0}; + int coordinate_transform_mode_{0}; + bool preserve_aspect_ratio_{false}; + bool const_shape_{false}; + + private: + int CheckParameters(); + int CheckInputsOuputs(); +}; +} // namespace mindspore::lite::micro +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_RESIZE_BASE_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.cc b/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.cc index 6b4bd63694..f635c02f2b 100644 --- a/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.cc @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/base/softmax_base_coder.h" +#include "coder/opcoders/base/softmax_base_coder.h" #include #include diff --git a/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.h b/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.h index f446ae44f6..79941dfbfc 100644 --- a/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/base/softmax_base_coder.h @@ -19,14 +19,12 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/softmax_parameter.h" #include "nnacl/int8/quantize.h" namespace mindspore::lite::micro { -using std::string; - class SoftmaxBaseCoder : public OperatorCoder { public: SoftmaxBaseCoder(const std::vector &in_tensors, const std::vector &out_tensors, diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc index e8683ff368..7be14eeb1e 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/add_int8_coder.cc @@ -17,13 +17,13 @@ #include "coder/opcoders/cmsis-nn/int8/add_int8_coder.h" #include #include -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/serializers/serializer.h" #include "nnacl/arithmetic.h" #include "nnacl/int8/quantize.h" #include "coder/opcoders/file_collector.h" #include "coder/log.h" -using mindspore::schema::PrimitiveType_Add; +using mindspore::schema::PrimitiveType_AddFusion; namespace mindspore::lite::micro::cmsis { @@ -85,5 +85,5 @@ int AddInt8Coder::DoCode(CoderContext *const context) { context->AppendCode(code.str()); return RET_OK; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Add, CPUOpCoderCreator) +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_AddFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc index 2e9ff730b9..5349603e3a 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.cc @@ -15,14 +15,13 @@ */ #include "coder/opcoders/cmsis-nn/int8/conv2d_int8_coder.h" -#include #include #include -#include "coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.h" #include "coder/opcoders/serializers/serializer.h" #include "coder/opcoders/file_collector.h" +#include "src/common/prim_util.h" -using mindspore::schema::PrimitiveType_Conv2D; +using mindspore::schema::PrimitiveType_Conv2DFusion; namespace mindspore::lite::micro::cmsis { @@ -40,13 +39,11 @@ int Conv2DInt8Coder::Prepare(CoderContext *const context) { int Conv2DInt8Coder::DoCode(CoderContext *const context) { Serializer code; code.precision(kPrecision); - std::vector h_files; - std::vector c_files; + std::vector h_files; + std::vector c_files; h_files.emplace_back("CMSIS/NN/Include/arm_nnfunctions.h"); - string buffer_str = "NULL"; if (opt_ != Convolve_1x1_fast) { - buffer_str = allocator_->GetRuntimeAddr(buffer_); - code << " memset(" << buffer_str << ", 0, " << buffer_size_ << ");\n"; + code.CodeFunction("memset", buffer_, 0, buffer_size_); } code.CodeArray("output_shift", output_shift_, output_ch_); code.CodeArray("output_mult", output_mult_, output_ch_); @@ -57,7 +54,7 @@ int Conv2DInt8Coder::DoCode(CoderContext *const context) { code.CodeFunction("arm_convolve_s8", input_tensor_, input_x_, input_y_, input_ch_, input_batches_, filter_tensor_, output_ch_, kernel_x_, kernel_y_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, output_tensor_, "output_shift", "output_mult", out_offset_, input_offset_, out_activation_min_, - out_activation_max_, output_x_, output_y_, buffer_str); + out_activation_max_, output_x_, output_y_, buffer_); break; case Convolve_1_x_n: c_files = {"arm_convolve_1_x_n_s8.c", "arm_nn_mat_mul_core_1x_s8.c"}; @@ -65,7 +62,7 @@ int Conv2DInt8Coder::DoCode(CoderContext *const context) { code.CodeFunction("arm_convolve_1_x_n_s8", input_tensor_, input_x_, input_ch_, input_batches_, filter_tensor_, output_ch_, kernel_x_, pad_x_, stride_x_, bias_tensor_, output_tensor_, "output_shift", "output_mult", out_offset_, input_offset_, out_activation_min_, out_activation_max_, output_x_, - buffer_str); + buffer_); break; case Convolve_1x1_fast: c_files = {"arm_convolve_1x1_s8_fast.c", "arm_nn_mat_mult_nt_t_s8.c", "arm_nn_mat_mul_core_4x_s8.c", @@ -74,7 +71,7 @@ int Conv2DInt8Coder::DoCode(CoderContext *const context) { code.CodeFunction("arm_convolve_1x1_s8_fast", input_tensor_, input_x_, input_y_, input_ch_, input_batches_, filter_tensor_, output_ch_, pad_x_, pad_y_, stride_x_, stride_y_, bias_tensor_, output_tensor_, "output_shift", "output_mult", out_offset_, input_offset_, out_activation_min_, - out_activation_max_, output_x_, output_y_, buffer_str); + out_activation_max_, output_x_, output_y_, buffer_); break; default: MS_LOG(ERROR) << "opt enum value is not defined"; @@ -159,5 +156,20 @@ int Conv2DInt8Coder::InitTmpBuffer() { return RET_OK; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Conv2D, CPUOpCoderCreator) +std::unique_ptr CmsisConv2DInt8OpCoderCreator(const std::vector &in_tensors, + const std::vector &out_tensors, + const Model::Node *node, size_t node_index, + Target target) { + MS_CHECK_PTR_RET_NULL(node); + int pt = GetPrimitiveType(node->primitive_); + if (pt != schema::PrimitiveType::PrimitiveType_Conv2DFusion) { + MS_LOG(ERROR) << "unmatched primitive type " << PrimitiveTypeName(pt); + return nullptr; + } + std::unique_ptr coder = + std::make_unique(in_tensors, out_tensors, node, node_index, target); + return coder; +} + +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Conv2DFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc index 04322376fc..b6182aab92 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/dwconv_int8_coder.cc @@ -20,8 +20,6 @@ #include "coder/opcoders/file_collector.h" #include "coder/log.h" -using mindspore::schema::PrimitiveType_DepthwiseConv2D; - namespace mindspore::lite::micro::cmsis { int DWConvInt8Coder::Prepare(CoderContext *const context) { @@ -153,6 +151,4 @@ int DWConvInt8Coder::InitTmpBuffer() { return 0; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_DepthwiseConv2D, CPUOpCoderCreator) - } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h index a5a1d3340d..e0370dc4d7 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/fullconnection_int8_coder.h @@ -19,8 +19,8 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" -#include "micro/coder/opcoders/base/full_connection_base_coder.h" +#include "coder/opcoders/op_coder.h" +#include "coder/opcoders/base/full_connection_base_coder.h" #include "nnacl/int8/quantize.h" namespace mindspore::lite::micro::cmsis { class FullConnectionInt8Coder final : public FullConnectionBaseCoder { diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc index f999aa4f95..a0b68f92f7 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/mul_int8_coder.cc @@ -20,7 +20,7 @@ #include "nnacl/int8/quantize.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Mul; +using mindspore::schema::PrimitiveType_MulFusion; namespace mindspore::lite::micro::cmsis { @@ -69,5 +69,5 @@ int MulInt8Coder::DoCode(CoderContext *const context) { context->AppendCode(code.str()); return RET_OK; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Mul, CPUOpCoderCreator) +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_MulFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc index 342841ab00..5e786afe27 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/pooling_int8_coder.cc @@ -20,7 +20,8 @@ #include "coder/opcoders/serializers/serializer.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Pooling; +using mindspore::schema::PrimitiveType_AvgPoolFusion; +using mindspore::schema::PrimitiveType_MaxPoolFusion; namespace mindspore::lite::micro::cmsis { int PoolingInt8Coder::Prepare(CoderContext *const context) { @@ -39,14 +40,12 @@ int PoolingInt8Coder::Prepare(CoderContext *const context) { int PoolingInt8Coder::DoCode(CoderContext *const context) { // init struct PoolingParameters - std::string buffer_str = "NULL"; std::string pooling_func; std::vector cFiles; if (pooling_parameter_->pool_mode_ == PoolMode_AvgPool) { cFiles = {"arm_avgpool_s8.c"}; pooling_func = "arm_avgpool_s8"; - buffer_str = allocator_->GetRuntimeAddr(buffer_); } else if (pooling_parameter_->pool_mode_ == PoolMode_MaxPool) { cFiles = {"arm_max_pool_s8.c"}; pooling_func = "arm_max_pool_s8"; @@ -59,11 +58,9 @@ int PoolingInt8Coder::DoCode(CoderContext *const context) { Serializer code; code.precision(kPrecision); - code.CodeFunction(pooling_func, "&nn_context", "&pool_params", "&input_dims", input_tensor_, "&filter_dims", - "&output_dims", output_tensor_); code.CodeFunction(pooling_func, dim_src_height_, dim_src_width_, dim_dst_height_, dim_dst_width_, stride_height_, stride_width_, dim_kernel_height_, dim_kernel_width_, padding_height_, padding_width_, act_min_, - act_max_, ch_src_, input_tensor_, buffer_str, output_tensor_); + act_max_, ch_src_, input_tensor_, buffer_, output_tensor_); context->AppendCode(code.str()); return RET_OK; } @@ -97,6 +94,7 @@ int PoolingInt8Coder::SetParameters() { return RET_OK; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Pooling, CPUOpCoderCreator) +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_AvgPoolFusion, CPUOpCoderCreator) +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_MaxPoolFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc index 2083945a6d..8a47a8a1c6 100644 --- a/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/cmsis-nn/int8/softmax_int8_coder.cc @@ -19,7 +19,7 @@ #include "coder/opcoders/serializers/serializer.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_SoftMax; +using mindspore::schema::PrimitiveType_Softmax; namespace mindspore::lite::micro::cmsis { int SoftMaxInt8Coder::Prepare(CoderContext *const context) { @@ -76,6 +76,6 @@ int SoftMaxInt8Coder::DoCode(CoderContext *const context) { context->AppendCode(code.str()); return RET_OK; } -REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_SoftMax, CPUOpCoderCreator) +REG_OPERATOR_CODER(kARM32M, kNumberTypeInt8, PrimitiveType_Softmax, CPUOpCoderCreator) } // namespace mindspore::lite::micro::cmsis diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.cc b/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.cc new file mode 100644 index 0000000000..0e3b9a7475 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.cc @@ -0,0 +1,143 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/dequant/de_quant.h" +#include +#include +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" + +static constexpr int kPerTensor = 1; +static constexpr size_t kPerBatch = 3; +namespace mindspore::lite::micro::nnacl { + +void Dequant::set_de_quant_buffer_str(const std::string &dequant_buffer_str) { + de_quant_buffer_str_ = "(float *)(" + dequant_buffer_str + ")"; +} + +void Dequant::DequantRecordWorkspcae(size_t curr_workspace) { + de_quant_max_workspace_ = de_quant_max_workspace_ > curr_workspace ? de_quant_max_workspace_ : curr_workspace; +} + +bool Dequant::CheckDequantFlag(const Tensor *weight_tensor) { + if (weight_tensor == nullptr) { + return false; + } + return !weight_tensor->quant_params().empty() && weight_tensor->quant_params().front().inited && + weight_tensor->data_c() != nullptr; +} + +void Dequant::DeQuantFunctionPerChannel(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, + NNaclFp32Serializer *const de_quant_code) { + int quant_arg_dims = static_cast(quant_tensor->quant_params().size()); + int de_quant_nums = quant_tensor->ElementsNum(); + for (int i = 0; i < quant_arg_dims; ++i) { + auto de_quant_arg = de_quant_args.at(i); + std::string de_quant_arg_str = de_quant_arg_base_str + std::to_string(i); + de_quant_code->CodeStruct(de_quant_arg_str, de_quant_arg); + } + std::string de_quant_args_name = "de_quant_args"; + *de_quant_code << "const DeQuantArg *" << de_quant_args_name << "[" << quant_arg_dims << "] = {\n"; + for (int i = 0; i < quant_arg_dims - 1; ++i) { + *de_quant_code << "&" << de_quant_arg_base_str << std::to_string(i) << ", "; + } + *de_quant_code << "&" << de_quant_arg_base_str << std::to_string(quant_arg_dims - 1); + *de_quant_code << "};\n"; + size_t per_batch_size = quant_tensor->shape().at(0); + std::string quant_tensor_addr_str = "(int8_t *)(" + quant_tensor_addr_ + ")"; + de_quant_code->CodeFunction("DequantDataPerChannel", quant_tensor_addr_str, de_quant_args_name, de_quant_nums, + per_batch_size, de_quant_buffer_str_); +} + +void Dequant::DeQuantFunction(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, NNaclFp32Serializer *const de_quant_code) { + int quant_arg_dims = static_cast(quant_tensor->quant_params().size()); + int de_quant_nums = quant_tensor->ElementsNum(); + for (int i = 0; i < quant_arg_dims; ++i) { + auto de_quant_arg = de_quant_args.at(i); + std::string de_quant_arg_str = de_quant_arg_base_str + std::to_string(i); + de_quant_code->CodeStruct(de_quant_arg_str, de_quant_arg); + } + std::string de_quant_args_name = "de_quant_args"; + *de_quant_code << "const DeQuantArg *" << de_quant_args_name << "[" << quant_arg_dims << "] = {\n"; + for (int i = 0; i < quant_arg_dims - 1; ++i) { + *de_quant_code << "&" << de_quant_arg_base_str << std::to_string(i) << ", "; + } + *de_quant_code << "&" << de_quant_arg_base_str << std::to_string(quant_arg_dims - 1); + *de_quant_code << "};\n"; + auto channels = static_cast(quant_tensor->Batch()); + std::string quant_tensor_addr_str = "(int8_t *)(" + quant_tensor_addr_ + ")"; + de_quant_code->CodeFunction("DequantData", quant_tensor_addr_str, de_quant_args_name, de_quant_nums, channels, + de_quant_buffer_str_); +} + +void Dequant::DeQuantFunctionPerTensor(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, + NNaclFp32Serializer *const de_quant_code) { + size_t de_quant_nums = quant_tensor->ElementsNum(); + auto de_quant_arg = de_quant_args.at(0); + std::string de_quant_arg_str = de_quant_arg_base_str + std::to_string(0); + de_quant_code->CodeStruct(de_quant_arg_str, de_quant_arg); + std::string de_quant_args_name = "de_quant_args"; + *de_quant_code << "const DeQuantArg *" << de_quant_args_name << "[" << 1 << "] = {\n"; + *de_quant_code << "&" << de_quant_arg_base_str << std::to_string(0); + *de_quant_code << "};\n"; + std::string quant_tensor_addr_str = "(int8_t *)(" + quant_tensor_addr_ + ")"; + de_quant_code->CodeFunction("DequantDataPerTensor", quant_tensor_addr_str, de_quant_args_name, de_quant_nums, + de_quant_buffer_str_); +} + +std::string Dequant::GetMicroDeQuantFunction(const Tensor *quant_tensor, const std::string &quant_tensor_addr) { + std::string de_quant_block; + if (quant_tensor == nullptr || de_quant_buffer_str_.empty()) { + return de_quant_block; + } + quant_tensor_addr_ = quant_tensor_addr; + size_t de_quant_nums = quant_tensor->ElementsNum(); + size_t quant_arg_dims = quant_tensor->quant_params().size(); + DequantRecordWorkspcae(static_cast(de_quant_nums * sizeof(float))); + NNaclFp32Serializer de_quant_code; + de_quant_code << "{\n"; + size_t quant_tensor_dims = quant_tensor->shape().size(); + std::vector de_quant_args; + std::string de_quant_arg_base_str = "de_quant_arg_"; + for (size_t i = 0; i < quant_arg_dims; ++i) { + auto curr_quant_param = quant_tensor->quant_params().at(i); + DeQuantArg de_quant_arg = { + .scale = static_cast(curr_quant_param.scale), + .zeroPoint = curr_quant_param.zeroPoint, + .var_corr = curr_quant_param.var_corr, + .mean_corr = curr_quant_param.mean_corr, + // this clusters is meaningless which will be supported in future + .clusters = {}, + .clusters_nums = static_cast(curr_quant_param.clusters.size()), + .bitNum = quant_tensor->quant_params().at(i).bitNum, + }; + de_quant_args.emplace_back(de_quant_arg); + } + de_quant_code.CodeFunction("memset", de_quant_buffer_str_, 0, de_quant_nums * sizeof(float)); + if (quant_tensor_dims == kPerBatch && quant_arg_dims == static_cast(quant_tensor->shape().at(0))) { + DeQuantFunctionPerChannel(quant_tensor, de_quant_args, de_quant_arg_base_str, &de_quant_code); + } else if (quant_arg_dims != kPerTensor) { + DeQuantFunction(quant_tensor, de_quant_args, de_quant_arg_base_str, &de_quant_code); + } else { + DeQuantFunctionPerTensor(quant_tensor, de_quant_args, de_quant_arg_base_str, &de_quant_code); + } + de_quant_code << "}\n"; + de_quant_block = de_quant_code.str(); + return de_quant_block; +} +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.h b/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.h new file mode 100644 index 0000000000..bf86b7b490 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/dequant/de_quant.h @@ -0,0 +1,63 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_DEQUANT_DEQUANT_H_ +#define MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_DEQUANT_DEQUANT_H_ + +#include +#include +#include "src/tensor.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +namespace mindspore::lite::micro::nnacl { +class Dequant { + public: + Dequant(const Dequant &) = delete; + Dequant &operator=(const Dequant &) = delete; + static Dequant *GetInstance() { + static Dequant dequant; + return &dequant; + } + + void set_de_quant_buffer_str(const std::string &de_quant_buffer_str); + + const size_t de_quant_max_workspace() const { return de_quant_max_workspace_; } + + const std::string de_quant_buffer_str() const { return de_quant_buffer_str_; } + + bool CheckDequantFlag(const Tensor *quant_tensor); + + std::string GetMicroDeQuantFunction(const Tensor *quant_tensor, const std::string &quant_tensor_addr); + + private: + void DeQuantFunctionPerTensor(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, NNaclFp32Serializer *de_quant_code); + + void DeQuantFunction(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, NNaclFp32Serializer *de_quant_code); + + void DeQuantFunctionPerChannel(const Tensor *quant_tensor, const std::vector &de_quant_args, + const std::string &de_quant_arg_base_str, NNaclFp32Serializer *de_quant_code); + + Dequant() = default; + ~Dequant() = default; + void DequantRecordWorkspcae(size_t curr_workspace); + + std::string de_quant_buffer_str_; + std::string quant_tensor_addr_; + size_t de_quant_max_workspace_{0}; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MICRO_LITE_MICRO_CODER_OPCODERS_NNACL_DEQUANT_DEQUANT_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.cc index 3e617f7708..f560221ebe 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.cc @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/activation_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/activation_fp32_coder.h" #include #include "nnacl/fp32/activation_fp32.h" #include "nnacl/op_base.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Activation; @@ -34,9 +34,9 @@ int ActivationFP32Coder::DoCode(CoderContext *const context) { int count = MSMIN(stride, length - stride * task_id); if (activation_parameter->type_ == schema::ActivationType_SIGMOID) { - Collect(context, {"runtime/kernel/fp32/sigmoid.h"}, {"sigmoid.c"}); + Collect(context, {"runtime/kernel/fp32/sigmoid_fp32.h"}, {"sigmoid_fp32.c"}); } else { - Collect(context, {"nnacl/fp32/activation.h"}, {"activation.c"}); + Collect(context, {"nnacl/fp32/activation_fp32.h"}, {"activation_fp32.c"}); } NNaclFp32Serializer code; switch (activation_parameter->type_) { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.cc index c5a4132c0d..d780247d11 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/addn_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/addn_fp32_coder.h" #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_AddN; namespace mindspore::lite::micro::nnacl { @@ -28,15 +28,12 @@ int AddNFP32Coder::DoCode(CoderContext *const context) { int elements_num = input0->ElementsNum(); // Get Tensor Pointer - std::string input0_str = allocator_->GetRuntimeAddr(input0); - std::string input1_str = allocator_->GetRuntimeAddr(input1); - Collect(context, {"nnacl/kernel/fp32/add_fp32_slim.h"}, {"add_fp32_slim.c"}); + Collect(context, {"nnacl/kernel/fp32/add_fp32.h"}, {"add_fp32.c"}); NNaclFp32Serializer code; - code.CodeFunction("ElementAdd", input0_str, input1_str, output_tensor_, elements_num); + code.CodeFunction("ElementAdd", input0, input1, output_tensor_, elements_num); if (input_tensors_.size() > 2) { for (size_t i = 2; i < input_tensors_.size(); ++i) { - std::string input_str = allocator_->GetRuntimeAddr(input_tensors_.at(i)); - code.CodeFunction("ElementAdd", input_str, output_tensor_, elements_num); + code.CodeFunction("ElementAdd", input_tensors_.at(i), output_tensor_, elements_num); } } context->AppendCode(code.str()); diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc index 1c70ebd6f4..6f9a3047c7 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.cc @@ -61,7 +61,7 @@ int ArithmeticFP32Coder::Init(CoderContext *const context) { if (arithmetic_parameter_->in_elements_num0_ == 1 || arithmetic_parameter_->in_elements_num1_ == 1) { switch (arithmetic_parameter_->op_parameter_.type_) { - case PrimitiveType_Mul: + case PrimitiveType_MulFusion: switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: arithmetic_parameter_->broadcasting_ = false; @@ -80,7 +80,7 @@ int ArithmeticFP32Coder::Init(CoderContext *const context) { break; } break; - case PrimitiveType_Add: + case PrimitiveType_AddFusion: switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: arithmetic_parameter_->broadcasting_ = false; @@ -99,7 +99,7 @@ int ArithmeticFP32Coder::Init(CoderContext *const context) { break; } break; - case PrimitiveType_Sub: + case PrimitiveType_SubFusion: switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: arithmetic_parameter_->broadcasting_ = false; @@ -157,7 +157,7 @@ int ArithmeticFP32Coder::Prepare(CoderContext *const context) { } arithmetic_parameter_ = reinterpret_cast(parameter_); std::map> type_setters = { - {PrimitiveType_Mul, + {PrimitiveType_MulFusion, [this]() { switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: @@ -174,7 +174,7 @@ int ArithmeticFP32Coder::Prepare(CoderContext *const context) { break; } }}, - {PrimitiveType_Add, + {PrimitiveType_AddFusion, [this]() { switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: @@ -191,7 +191,7 @@ int ArithmeticFP32Coder::Prepare(CoderContext *const context) { break; } }}, - {PrimitiveType_Sub, + {PrimitiveType_SubFusion, [this]() { switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: @@ -205,7 +205,7 @@ int ArithmeticFP32Coder::Prepare(CoderContext *const context) { break; } }}, - {PrimitiveType_Div, + {PrimitiveType_DivFusion, [this]() { switch (arithmetic_parameter_->activation_type_) { case schema::ActivationType_RELU: @@ -275,15 +275,16 @@ int ArithmeticFP32Coder::DoCode(CoderContext *const context) { * this solution is not suitable for micro, for the size of package. * */ if (arithmetic_opt_run_ == "ElementOptSub" || arithmetic_run_ == "ElementSub") { - Collect(context, {"nnacl/kernel/fp32/sub.h"}, {"sub.c"}); + Collect(context, {"nnacl/fp32/sub_fp32.h"}, {"sub_fp32.c"}); } else if (arithmetic_opt_run_ == "ElementOptAdd" || arithmetic_run_ == "ElementAdd") { - Collect(context, {"nnacl/kernel/fp32/add_fp32_slim.h"}, {"add_fp32_slim.c"}); + Collect(context, {"nnacl/fp32/add_fp32.h"}, {"add_fp32.c"}); } else if (arithmetic_opt_run_ == "ElementOptMul" || arithmetic_run_ == "ElementMul") { - Collect(context, {"nnacl/kernel/fp32/mul.h"}, {"mul.c"}); + Collect(context, {"nnacl/fp32/mul_fp32.h"}, {"mul_fp32.c"}); } else if (arithmetic_run_ == "ElementAddRelu") { - Collect(context, {"nnacl/kernel/fp32/add_relu.h"}, {"add_relu.c"}); + Collect(context, {"nnacl/fp32/add_relu_fp32.h"}, {"add_relu_fp32.c"}); } else { - Collect(context, {"nnacl/arithmetic_common.h", "nnacl/fp32/arithmetic.h"}, {"arithmetic_common.c", "arithmetic.c"}); + Collect(context, {"nnacl/arithmetic_common.h", "nnacl/fp32/arithmetic_fp32.h"}, + {"arithmetic_common.c", "arithmetic_fp32.c"}); } if (arithmetic_parameter_->broadcasting_) { @@ -330,15 +331,15 @@ int ArithmeticFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt32, PrimitiveType_Add, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt32, PrimitiveType_AddFusion, CPUOpCoderCreator) -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Mul, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_MulFusion, CPUOpCoderCreator) -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Add, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_AddFusion, CPUOpCoderCreator) -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Sub, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_SubFusion, CPUOpCoderCreator) -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Div, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_DivFusion, CPUOpCoderCreator) REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_LogicalAnd, CPUOpCoderCreator) diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h index 7c2ed43242..a2f6948842 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_fp32_coder.h @@ -25,9 +25,9 @@ #define DEFAULT_ARITHMETIC_NDIMS 10 namespace mindspore::lite::micro::nnacl { -using mindspore::schema::PrimitiveType_Add; +using mindspore::schema::PrimitiveType_AddFusion; -using mindspore::schema::PrimitiveType_Div; +using mindspore::schema::PrimitiveType_DivFusion; using mindspore::schema::PrimitiveType_Equal; @@ -51,7 +51,7 @@ using mindspore::schema::PrimitiveType_Maximum; using mindspore::schema::PrimitiveType_Minimum; -using mindspore::schema::PrimitiveType_Mul; +using mindspore::schema::PrimitiveType_MulFusion; using mindspore::schema::PrimitiveType_NotEqual; @@ -59,7 +59,7 @@ using mindspore::schema::PrimitiveType_RealDiv; using mindspore::schema::PrimitiveType_SquaredDifference; -using mindspore::schema::PrimitiveType_Sub; +using mindspore::schema::PrimitiveType_SubFusion; using mindspore::schema::PrimitiveType_Eltwise; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h index 239173907e..2f0754d699 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/arithmetic_self_fp32_coder.h @@ -27,7 +27,7 @@ namespace mindspore::lite::micro::nnacl { using mindspore::schema::PrimitiveType_Abs; -using mindspore::schema::PrimitiveType_Add; +using mindspore::schema::PrimitiveType_AddFusion; using mindspore::schema::PrimitiveType_AddN; @@ -37,7 +37,7 @@ using mindspore::schema::PrimitiveType_Ceil; using mindspore::schema::PrimitiveType_Cos; -using mindspore::schema::PrimitiveType_Div; +using mindspore::schema::PrimitiveType_DivFusion; using mindspore::schema::PrimitiveType_Equal; @@ -67,7 +67,7 @@ using mindspore::schema::PrimitiveType_Maximum; using mindspore::schema::PrimitiveType_Minimum; -using mindspore::schema::PrimitiveType_Mul; +using mindspore::schema::PrimitiveType_MulFusion; using mindspore::schema::PrimitiveType_NotEqual; @@ -81,7 +81,7 @@ using mindspore::schema::PrimitiveType_Sqrt; using mindspore::schema::PrimitiveType_SquaredDifference; -using mindspore::schema::PrimitiveType_Sub; +using mindspore::schema::PrimitiveType_SubFusion; using mindspore::schema::PrimitiveType_Sin; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc index 8d7ac73fd7..fa97188414 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.cc @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/assign_add_fp32_coder.h" #include #include "schema/inner/ops_generated.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc index 4ddf38d561..2185714a14 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.cc @@ -17,7 +17,6 @@ #include #include #include "nnacl/fp32/batchnorm_fp32.h" -#include "src/ops/batch_norm.h" #include "nnacl/op_base.h" #include "coder/opcoders/file_collector.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" @@ -27,10 +26,7 @@ using mindspore::schema::PrimitiveType_BatchNorm; namespace mindspore::lite::micro::nnacl { int BatchnormFP32Coder::Init() { - auto bn_parameter = reinterpret_cast(parameter_); - auto bn_prim = reinterpret_cast(OperatorCoder::primitive()); - bn_parameter->epsilon_ = bn_prim->GetEpsilon(); - + auto bn_parameter = reinterpret_cast(OperatorCoder::parameter_); std::vector input_shapes = input_tensor_->shape(); if (input_shapes.empty()) { return RET_ERROR; @@ -41,7 +37,9 @@ int BatchnormFP32Coder::Init() { for (int i = 0; i < n_dim - 1; i++) { bn_parameter->unit_ *= input_shapes.at(i); } - bn_parameter->op_parameter_.thread_num_ = MSMIN(bn_parameter->op_parameter_.thread_num_, bn_parameter->unit_); + if (default_momentum_ < 0.0f) { + default_momentum_ = bn_parameter->momentum_; + } return RET_OK; } @@ -59,7 +57,7 @@ int BatchnormFP32Coder::DoCode(CoderContext *const context) { Collect(context, {"nnacl/fp32/batchnorm.h"}, {"nnacl/fp32/batchnorm.c"}); NNaclFp32Serializer code; code.CodeStruct("bn_parameter", *bn_parameter); - code.CodeFunction("BatchNorm", output_tensor_, input_tensor_, mean_tensor, var_tensor, task_id, "&bn_parameter"); + code.CodeFunction("BatchNormFp32", input_tensor_, mean_tensor, var_tensor, "&bn_parameter", task_id, output_tensor_); MS_LOG(INFO) << "BatchnormFP32Code has been called"; context->AppendCode(code.str()); return lite::RET_OK; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h index 9b77799a04..5d222c487f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/batchnorm_fp32_coder.h @@ -36,6 +36,12 @@ class BatchnormFP32Coder final : public OperatorCoder { private: int Init(); + + float default_momentum_{-1.0f}; + + float *mean_{nullptr}; + + float *variance_{nullptr}; }; } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.cc new file mode 100644 index 0000000000..5ff71219e6 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.cc @@ -0,0 +1,77 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/fp32/biasadd_fp32_coder.h" +#include +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" + +using mindspore::schema::PrimitiveType_BiasAdd; + +namespace mindspore::lite::micro::nnacl { + +int BiasAddFP32Coder::Prepare(CoderContext *context) { + arithmetic_parameter_ = reinterpret_cast(parameter_); + size_t data_size = input_tensors_.at(0)->ElementsNum(); + tile_in_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, data_size * sizeof(float), kWorkspace)); + tile_bias_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, data_size * sizeof(float), kWorkspace)); + return RET_OK; +} + +int BiasAddFP32Coder::DoCode(CoderContext *ctx) { + if (input_tensors_.size() < kBiasIndex) { + return RET_ERROR; + } + size_t data_size = input_tensor_->ElementsNum(); + std::string bias_str = allocator_->GetRuntimeAddr(input_tensors_.at(kWeightIndex)); + Collect(ctx, + {"nnacl/arithmetic.h", "nnacl/nnacl_utils.h", "nnacl/nnacl_common.h", "nnacl/base/arithmetic_base.h", + "nnacl/fp32/add_fp32.h", "nnacl/fp32/arithmetic_fp32.h"}, + {"arithmetic_base.c", "arithmetic_fp32.c", "add_fp32.c"}); + nnacl::NNaclFp32Serializer code; + std::vector dims = input_tensor_->shape(); + arithmetic_parameter_->broadcasting_ = false; + arithmetic_parameter_->ndim_ = dims.size(); + arithmetic_parameter_->activation_type_ = 0; + for (size_t i = 0; i < dims.size(); i++) { + arithmetic_parameter_->in_shape0_[i] = dims[i]; + } + arithmetic_parameter_->in_elements_num0_ = 0; + + for (size_t i = 0; i < dims.size(); i++) { + if (i == dims.size() - 1) { + arithmetic_parameter_->in_shape1_[i] = dims[dims.size() - 1]; + continue; + } + arithmetic_parameter_->in_shape1_[i] = 1; + } + arithmetic_parameter_->in_elements_num1_ = 0; + + for (size_t i = 0; i < dims.size(); i++) { + arithmetic_parameter_->out_shape_[i] = dims[i]; + } + arithmetic_parameter_->out_elements_num_ = 0; + // other rest elements is not sure + + code.CodeStruct("arith_param", *arithmetic_parameter_); + code.CodeFunction("BroadcastAdd", input_tensor_, bias_str, tile_in_, tile_bias_, output_tensor_, data_size, + "(ArithmeticParameter *)&arith_param"); + ctx->AppendCode(code.str()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_BiasAdd, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.h new file mode 100644 index 0000000000..60064f2af8 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/biasadd_fp32_coder.h @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_BIASADD_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_BIASADD_FP32_CODER_H_ + +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/arithmetic.h" + +namespace mindspore::lite::micro::nnacl { +class BiasAddFP32Coder final : public OperatorCoder { + public: + BiasAddFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~BiasAddFP32Coder() override = default; + + int Prepare(CoderContext *context) override; + + int DoCode(CoderContext *context) override; + + private: + ArithmeticParameter *arithmetic_parameter_{nullptr}; + float *tile_in_{nullptr}; + float *tile_bias_{nullptr}; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_BIASADD_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc index be9041267b..27e45cfc98 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.cc @@ -14,13 +14,12 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h" #include -#include "micro/coder/log.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -using mindspore::schema::PrimitiveType_DepthwiseConv2D; namespace mindspore::lite::micro::nnacl { int ConvolutionDepthwiseFP32Coder::Prepare(CoderContext *const context) { Conv2DBaseCoder::Init(); @@ -73,6 +72,4 @@ int ConvolutionDepthwiseFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_DepthwiseConv2D, - CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h index 6946459d42..f38a25b24a 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_DEPTHWISE_FP32_CODER_H_ #include -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include "src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc index 54a6e86efe..2efa19c302 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.cc @@ -14,17 +14,21 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/convolution_fp32_coder.h" #include #include #include -#include "micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/convolution_depthwise_fp32_coder.h" #include "nnacl/fp32/winograd_utils.h" #include "src/ops/populate/populate_register.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "src/common/prim_util.h" +#include "src/common/version_manager.h" +#include "coder/opcoders/nnacl/dequant/de_quant.h" -using mindspore::schema::PrimitiveType_Conv2D; +using mindspore::schema::PrimitiveType_Conv2DFusion; namespace mindspore::lite::micro::nnacl { int ConvolutionFP32Coder::InitTmpBuffer() { int in_channel = conv_param_->input_channel_; @@ -43,20 +47,16 @@ int ConvolutionFP32Coder::InitTmpBuffer() { } int ConvolutionFP32Coder::Prepare(CoderContext *const context) { - int ret = Conv2DBaseCoder::Init(); - MS_CHECK_RET_CODE(ret, "Conv2DBaseCoder::Init() failed."); - ret = InitWeightBias(context); - MS_CHECK_RET_CODE(ret, "Init weight bias failed."); + MS_CHECK_RET_CODE(Conv2DBaseCoder::Init(), "Conv2DBaseCoder::Init() failed."); + de_quant_flag_ = Dequant::GetInstance()->CheckDequantFlag(filter_tensor_); + MS_CHECK_RET_CODE(InitWeightBias(context), "Init weight bias failed."); return Resize(); } int ConvolutionFP32Coder::Resize() { - int ret = Conv2DBaseCoder::CheckResizeValid(); - MS_CHECK_RET_CODE(ret, "Resize is invalid."); - ret = Conv2DBaseCoder::Init(); - MS_CHECK_RET_CODE(ret, "init failed."); - ret = InitTmpBuffer(); - MS_CHECK_RET_CODE(ret, "init tmp buffer failed."); + MS_CHECK_RET_CODE(Conv2DBaseCoder::CheckResizeValid(), "Resize is invalid."); + MS_CHECK_RET_CODE(Conv2DBaseCoder::Init(), "init failed."); + MS_CHECK_RET_CODE(InitTmpBuffer(), "init tmp buffer failed."); return RET_OK; } @@ -71,36 +71,43 @@ int ConvolutionFP32Coder::InitWeightBias(CoderContext *const context) { const int oc_block = C8NUM; int oc_block_num = UP_DIV(out_channel, C8NUM); int pack_weight_size = oc_block_num * oc_block * in_channel * kernel_plane; - + pack_weight_size_ = pack_weight_size * sizeof(float); auto origin_weight = reinterpret_cast(filter_tensor_->MutableData()); MS_CHECK_PTR(origin_weight); - packed_weight_ = reinterpret_cast( - allocator_->Malloc(kNumberTypeFloat32, pack_weight_size * sizeof(float), kOnlinePackWeight)); + packed_weight_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, kOnlineSize, kOnlinePackWeight)); MS_CHECK_PTR(packed_weight_); auto out_channel_size = static_cast(out_channel); - NNaclFp32Serializer code; - code.CodeMallocExpression(packed_weight_, pack_weight_size * sizeof(float)); - code.CodeFunction("memset", packed_weight_, 0, pack_weight_size * sizeof(float)); - code.CodeFunction("RowMajor2Col8Major", filter_tensor_, packed_weight_, out_channel_size, in_channel * kernel_plane); + NNaclFp32Serializer init_code; + std::string ori_weight_addr = allocator_->GetRuntimeAddr(filter_tensor_); + std::string init_weight_str = ori_weight_addr; + if (de_quant_flag_) { + init_weight_str = Dequant::GetInstance()->de_quant_buffer_str(); + std::string de_quant_function = Dequant::GetInstance()->GetMicroDeQuantFunction(filter_tensor_, ori_weight_addr); + init_code << de_quant_function; + } + init_code.CodeMallocExpression(packed_weight_, pack_weight_size_); + init_code.CodeFunction("memset", packed_weight_, 0, pack_weight_size_); + init_code.CodeFunction("RowMajor2Col8Major", init_weight_str, packed_weight_, out_channel_size, + in_channel * kernel_plane); auto bias_data_size = static_cast(oc_block_num * oc_block * sizeof(float)); - bias_data_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, bias_data_size, kOnlinePackWeight)); + bias_data_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, kOnlineSize, kOnlinePackWeight)); MS_CHECK_PTR(bias_data_); if (input_tensors_.size() == kInputSize2) { - code.CodeMallocExpression(bias_data_, bias_data_size); - code.CodeFunction("memset", bias_data_, 0, bias_data_size); - code.CodeFunction("memcpy", bias_data_, bias_tensor_, out_channel_size * sizeof(float)); + init_code.CodeMallocExpression(bias_data_, bias_data_size); + init_code.CodeFunction("memset", bias_data_, 0, bias_data_size); + init_code.CodeFunction("memcpy", bias_data_, bias_tensor_, out_channel_size * sizeof(float)); } else { return RET_ERROR; } - context->AppendInitCode(code.str()); + context->AppendInitCode(init_code.str()); return RET_OK; } int ConvolutionFP32Coder::DoCode(CoderContext *const context) { { - std::vector asmFiles; + std::vector asmFiles; if (target_ == kARM32A) { asmFiles = {"MatmulFp32.S", "MatmulFp32Opt.S", @@ -112,9 +119,14 @@ int ConvolutionFP32Coder::DoCode(CoderContext *const context) { asmFiles = {"MatmulFp32.S", "MatmulFp32Opt.S", "PreSum4x16Int8Peroc.S", "MatVecMulFp32.S", "PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "IndirectGemmInt16to32_8x4.S", "MatmulInt8.S"}; } - Collect(context, - {"nnacl/kernel/fp32/conv_fp32_slim.h", "nnacl/fp32/matmul.h", "nnacl/conv_parameter.h", "nnacl/op_base.h"}, - {"common_func.c", "conv_fp32_slim.c", "matmul.c"}, asmFiles); + std::vector h_files = {"nnacl/fp32/conv_common_fp32.h", "nnacl/fp32/matmul.h", + "nnacl/conv_parameter.h", "nnacl/op_base.h"}; + std::vector c_files = {"common_func.c", "conv_common_fp32.c", "matmul.c"}; + if (de_quant_flag_) { + h_files.emplace_back("wrapper/fp32/dequant_int8_to_fp32_wrapper.h"); + c_files.emplace_back("dequant_int8_to_fp32_wrapper.c"); + } + Collect(context, h_files, c_files, asmFiles); } NNaclFp32Serializer code; // call the op function @@ -122,7 +134,7 @@ int ConvolutionFP32Coder::DoCode(CoderContext *const context) { code.CodeFunction("memset", col_major_input_, "0", col_major_input_size_); code.CodeStruct("conv_parameter", *conv_param_); int task_id = 0; - code.CodeFunction("ConvFp32Slim", input_tensor_, packed_input_, packed_weight_, bias_data_, col_major_input_, + code.CodeFunction("ConvFp32", input_tensor_, packed_input_, packed_weight_, bias_data_, col_major_input_, output_tensor_, task_id, "(ConvParameter *)&conv_parameter"); context->AppendCode(code.str()); @@ -135,18 +147,18 @@ std::unique_ptr CPUConvolutionFP32CoderCreator(const std::vector< Target target) { std::vector inputs = in_tensors; std::vector outputs = out_tensors; - auto primitive = node->primitive_; - if (!primitive) { + const void *primitive = node->primitive_; + if (primitive == nullptr) { return nullptr; } - OpParameter *parameter = - PopulateRegistry::GetInstance()->GetParameterCreator((schema::PrimitiveType(primitive->Type())))(primitive); - if (parameter == nullptr) { - MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " - << schema::EnumNamePrimitiveType((schema::PrimitiveType)(primitive->Type())); + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + ParameterGen paramGen = + PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(node->primitive_), schema_version); + if (paramGen == nullptr) { + MS_LOG(ERROR) << "parameter generator is null"; return nullptr; } - auto conv_param = reinterpret_cast(parameter); + auto conv_param = reinterpret_cast(paramGen(node->primitive_)); bool use_winograd = false; int out_unit = 0; int kernel_h = conv_param->kernel_h_; @@ -159,7 +171,7 @@ std::unique_ptr CPUConvolutionFP32CoderCreator(const std::vector< conv_param->output_channel_ = outputs.at(kOutputIndex)->Channel(); conv_param->op_parameter_.thread_num_ = 1; CheckIfUseWinograd(&use_winograd, &out_unit, conv_param); - free(parameter); + free(conv_param); // weight de quant std::unique_ptr coder; if (kernel_h == 1 && kernel_w == 1) { @@ -175,5 +187,32 @@ std::unique_ptr CPUConvolutionFP32CoderCreator(const std::vector< return coder; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Conv2D, CPUConvolutionFP32CoderCreator) +std::unique_ptr CPUConv2DFusionFP32CoderCreator(const std::vector &in_tensors, + const std::vector &out_tensors, + const Model::Node *node, size_t node_index, + Target target) { + const void *primitive = node->primitive_; + if (primitive == nullptr) { + return nullptr; + } + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + ParameterGen paramGen = + PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(node->primitive_), schema_version); + if (paramGen == nullptr) { + MS_LOG(ERROR) << "parameter generator is null"; + return nullptr; + } + auto conv_param = reinterpret_cast(paramGen(node->primitive_)); + std::unique_ptr coder; + if (conv_param->group_ == 1) { + coder = CPUConvolutionFP32CoderCreator(in_tensors, out_tensors, node, node_index, target); + } else if (conv_param->group_ == conv_param->input_channel_ && conv_param->group_ == conv_param->output_channel_) { + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + } else { + // GroupConv + } + return coder; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Conv2DFusion, CPUConv2DFusionFP32CoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h index eaabfb841e..42c2ee583b 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_fp32_coder.h @@ -14,14 +14,14 @@ * limitations under the License. */ -#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ -#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_CONVOLUTION_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_CONVOLUTION_FP32_CODER_H_ #include #include #include "nnacl/conv_parameter.h" -#include "micro/coder/opcoders/base/conv2d_base_coder.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" namespace mindspore::lite::micro::nnacl { class ConvolutionFP32Coder final : public Conv2DBaseCoder { @@ -51,12 +51,14 @@ class ConvolutionFP32Coder final : public Conv2DBaseCoder { size_t packed_input_size_{0}; - int thread_stride_{0}; + bool de_quant_flag_{false}; int thread_count_{0}; float *col_major_input_{nullptr}; size_t col_major_input_size_{0}; + + size_t pack_weight_size_{0}; }; } // namespace mindspore::lite::micro::nnacl -#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_FP32_CONVOLUTION_FP32_CODER_H_ +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_CONVOLUTION_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc index fbf6867252..69c973a24f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.cc @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h" #include #include "nnacl/base/minimal_filtering_generator.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" namespace mindspore::lite::micro::nnacl { const std::array InputTransFuncList = { @@ -222,10 +222,11 @@ int ConvolutionWinogradFP32Coder::DoCode(CoderContext *const context) { asmFiles = {"MatmulFp32.S", "MatmulFp32Opt.S", "PreSum4x16Int8Peroc.S", "MatVecMulFp32.S", "PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "IndirectGemmInt16to32_8x4.S", "MatmulInt8.S"}; } - Collect(context, {"nnacl/fp32/conv.h", "nnacl/common_func.h"}, - {"common_func.c", "conv_int8.c", "matmul_int8.c", "pack.c", "conv.c", "winograd_transform.c", - "common_func_fp32.c", "fixed_point.c", "winograd_utils.c", "minimal_filtering_generator.c"}, - asmFiles); + Collect( + context, {"nnacl/fp32/conv_winograd_fp32.h", "nnacl/common_func.h"}, + {"common_func.c", "conv_int8.c", "matmul_int8.c", "pack_fp32.c", "conv_winograd_fp32.c", "winograd_transform.c", + "common_func_fp32.c", "fixed_point.c", "winograd_utils.c", "minimal_filtering_generator.c"}, + asmFiles); NNaclFp32Serializer code; // call the op function diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h index 8bce785ad0..1e236ae622 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/convolution_winograd_fp32_coder.h @@ -20,7 +20,7 @@ #include #include #include -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include "nnacl/conv_parameter.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.cc index d9137398af..d84b208e48 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.cc @@ -22,6 +22,7 @@ #include "coder/opcoders/file_collector.h" #include "nnacl/fp32/matmul_fp32.h" #include "wrapper/fp32/matmul_fp32_wrapper.h" +#include "coder/opcoders/nnacl/dequant/de_quant.h" using mindspore::schema::PrimitiveType_MatMul; @@ -31,6 +32,13 @@ int MatMulFP32BaseCoder::ReSize() { ResizeParameter(); thread_count_ = MSMIN(thread_num_, UP_DIV(params_->col_align_, col_tile_)); thread_stride_ = UP_DIV(UP_DIV(params_->col_align_, col_tile_), thread_count_); + // can not call Malloc in DoCode,so move this runtime init to final resize + if (!params_->a_const_) { + MS_CHECK_RET_CODE(InitBufferA(), "InitBufferA failed"); + } + if (!params_->b_const_) { + MS_CHECK_RET_CODE(InitBufferB(), "InitBufferB failed"); + } return RET_OK; } @@ -45,17 +53,16 @@ int MatMulFP32BaseCoder::InitBiasData() { } void MatMulFP32BaseCoder::InitParameter() { + row_tile_ = C12NUM; if (target_ == kARM32A) { - row_tile_ = C12NUM; col_tile_ = C4NUM; } else { - row_tile_ = C12NUM; col_tile_ = C8NUM; } } void MatMulFP32BaseCoder::ResizeParameter() { - if (params_->row_ == 1 && !params_->b_const_) { + if (params_->row_ == 1) { vec_matmul_ = true; } params_->row_align_ = vec_matmul_ ? 1 : UP_ROUND(params_->row_, row_tile_); @@ -66,12 +73,11 @@ int MatMulFP32BaseCoder::InitBufferA() { if (a_pack_ptr_ != nullptr) { return RET_OK; } + a_pack_ptr_size_ = static_cast(params_->batch * params_->row_align_ * params_->deep_ * sizeof(float)); if (params_->a_const_) { a_pack_ptr_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, kOnlineSize, kOnlinePackWeight)); } else { - a_pack_ptr_size_ = static_cast(params_->batch * params_->row_align_ * params_->deep_ * sizeof(float)); - a_pack_ptr_ = - reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, a_pack_ptr_size_, kOfflinePackWeight)); + a_pack_ptr_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, a_pack_ptr_size_, kWorkspace)); } MS_CHECK_PTR(a_pack_ptr_); return RET_OK; @@ -81,12 +87,11 @@ int MatMulFP32BaseCoder::InitBufferB() { if (b_pack_ptr_ != nullptr) { return RET_OK; } + b_pack_ptr_size_ = static_cast(params_->batch * params_->col_align_ * params_->deep_ * sizeof(float)); if (params_->b_const_) { b_pack_ptr_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, kOnlineSize, kOnlinePackWeight)); } else { - b_pack_ptr_size_ = static_cast(params_->batch * params_->col_align_ * params_->deep_ * sizeof(float)); - b_pack_ptr_ = - reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, b_pack_ptr_size_, kOfflinePackWeight)); + b_pack_ptr_ = reinterpret_cast(allocator_->Malloc(kNumberTypeFloat32, b_pack_ptr_size_, kWorkspace)); } MS_CHECK_PTR(b_pack_ptr_); return RET_OK; @@ -108,12 +113,9 @@ int MatMulFP32BaseCoder::Init() { MS_CHECK_RET_CODE(InitBiasData(), "InitBiasData failed"); if (params_->a_const_) { MS_CHECK_RET_CODE(InitBufferA(), "InitBufferA failed"); - MS_CHECK_RET_CODE(InitMatrixA(reinterpret_cast(input_tensor_->data_c())), "InitMatrixA failed"); } - if (params_->b_const_) { MS_CHECK_RET_CODE(InitBufferB(), "InitBufferB failed"); - MS_CHECK_RET_CODE(InitMatrixB(reinterpret_cast(filter_tensor_->data_c())), "InitMatrixB failed"); } return RET_OK; } @@ -124,12 +126,17 @@ int MatMulFP32BaseCoder::DoCode(CoderContext *const context) { // generate code .h .c std::vector asm_files; if (target_ == kARM32A) { - asm_files = {"MatmulFp32.S", "MatmulFp32Opt.S"}; + asm_files = {"MatmulFp32.S", "MatmulFp32Opt.S", "MatmulFp32Opt12x4.S"}; } else if (target_ == kARM64) { - asm_files = {"arm64/MatmulFp32.S", "MatmulFp32Opt.S", "arm64/MatVecMulFp32.S"}; + asm_files = {"MatmulFp32.S", "MatmulFp32Opt.S", "MatVecMulFp32.S"}; + } + std::vector h_files = {"nnacl/fp32/matmul_fp32.h", "wrapper/fp32/matmul_fp32_wrapper.h"}; + std::vector c_files = {"matmul_fp32.c", "matmul_fp32_wrapper.c"}; + if (de_quant_flag_) { + h_files.emplace_back("wrapper/fp32/dequant_int8_to_fp32_wrapper.h"); + c_files.emplace_back("dequant_int8_to_fp32_wrapper.c"); } - Collect(context, {"nnacl/fp32/matmul.h", "adapter/fp32/matmul_fp32_adapter.h"}, {"matmul.c", "matmul_fp32_adapter.c"}, - asm_files); + Collect(context, h_files, c_files, asm_files); NNaclFp32Serializer code; NNaclFp32Serializer init_code; code.CodeStruct("mat_mul_parameter", *params_); @@ -137,9 +144,12 @@ int MatMulFP32BaseCoder::DoCode(CoderContext *const context) { // do bias packing to init if (bias_ptr_) { init_code.CodeMallocExpression(bias_ptr_, bias_pack_ptr_size_); - init_code.CodeFunction("memcpy", bias_ptr_, bias_tensor_->data_c(), bias_pack_ptr_size_); + init_code.CodeFunction("memcpy", bias_ptr_, bias_tensor_, bias_pack_ptr_size_); } + // Get Tensor Pointer + std::string a_str = allocator_->GetRuntimeAddr(input_tensor_); + std::string b_str = allocator_->GetRuntimeAddr(filter_tensor_); std::string c_str = allocator_->GetRuntimeAddr(output_tensor_); std::string a_pack_str = allocator_->GetRuntimeAddr(a_pack_ptr_); std::string b_pack_str = allocator_->GetRuntimeAddr(b_pack_ptr_); @@ -147,12 +157,28 @@ int MatMulFP32BaseCoder::DoCode(CoderContext *const context) { // do const value packing to init if (!params_->a_const_) { code.CodeFunction("InitMatrixA", input_tensor_, a_pack_ptr_, "&mat_mul_parameter", vec_matmul_); + init_code.CodeMallocExpression(b_pack_ptr_, b_pack_ptr_size_); + std::string b_src_str = b_str; + if (de_quant_flag_) { + // reuse to b_pack_str + b_src_str = Dequant::GetInstance()->de_quant_buffer_str(); + std::string de_quant_function = Dequant::GetInstance()->GetMicroDeQuantFunction(filter_tensor_, b_str); + init_code << de_quant_function; + } // b_pack_str has been memset, no need to memset - init_code.CodeFunction("InitMatrixB", filter_tensor_, b_pack_ptr_, "&mat_mul_parameter", vec_matmul_); + init_code.CodeFunction("InitMatrixB", b_src_str, b_pack_ptr_, "&mat_mul_parameter", vec_matmul_); } if (!params_->b_const_) { + init_code.CodeMallocExpression(a_pack_str, a_pack_ptr_size_); + std::string a_src_str = a_str; + if (de_quant_flag_) { + // reuse to a_pack_str + a_src_str = Dequant::GetInstance()->de_quant_buffer_str(); + std::string de_quant_function = Dequant::GetInstance()->GetMicroDeQuantFunction(input_tensor_, a_str); + init_code << de_quant_function; + } // a_pack_str has been memset, no need to memset - init_code.CodeFunction("InitMatrixA", input_tensor_, a_pack_ptr_, "&mat_mul_parameter", vec_matmul_); + init_code.CodeFunction("InitMatrixA", a_src_str, a_pack_ptr_, "&mat_mul_parameter", vec_matmul_); code.CodeFunction("InitMatrixB", filter_tensor_, b_pack_ptr_, "&mat_mul_parameter", vec_matmul_); } @@ -165,13 +191,13 @@ int MatMulFP32BaseCoder::DoCode(CoderContext *const context) { } code << "for (int i = 0; i < " << params_->batch << "; ++i) {\n"; if (vec_matmul_) { - code << "\t\tbatch_a_ptr = " << a_pack_str << " + i * " << params_->deep_ << ";\n"; - code << "\t\tbatch_b_ptr = " << b_pack_str << " + i * " << params_->deep_ * params_->col_ << ";\n"; - code << "\t\tbatch_c_ptr = " << c_str << " + i * " << params_->row_ * params_->col_ << ";\n"; + code << "\t\tfloat *batch_a_ptr = " << a_pack_str << " + i * " << params_->deep_ << ";\n"; + code << "\t\tfloat *batch_b_ptr = " << b_pack_str << " + i * " << params_->deep_ * params_->col_ << ";\n"; + code << "\t\tfloat *batch_c_ptr = " << c_str << " + i * " << params_->row_ * params_->col_ << ";\n"; } else { - code << "\t\tbatch_a_ptr = " << a_pack_str << " + i * " << params_->row_align_ * params_->deep_ << ";\n"; - code << "\t\tbatch_b_ptr = " << b_pack_str << " + i * " << params_->deep_ * params_->col_align_ << ";\n"; - code << "\tbatch_c_ptr = " << c_str << " + i * " << params_->row_ * params_->col_ << ";\n"; + code << "\t\tfloat *batch_a_ptr = " << a_pack_str << " + i * " << params_->row_align_ * params_->deep_ << ";\n"; + code << "\t\tfloat *batch_b_ptr = " << b_pack_str << " + i * " << params_->deep_ * params_->col_align_ << ";\n"; + code << "\t\tfloat *batch_c_ptr = " << c_str << " + i * " << params_->row_ * params_->col_ << ";\n"; } if (vec_matmul_) { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.h index 7d8a85fe53..088bee45a0 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_base_coder.h @@ -56,6 +56,7 @@ class MatMulFP32BaseCoder : public OperatorCoder { float *b_pack_ptr_ = nullptr; float *bias_ptr_{nullptr}; bool vec_matmul_{false}; + bool de_quant_flag_{false}; private: int col_tile_{0}; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_coder.cc index c58f4025cd..5f10e3fa8e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/matmul_fp32_coder.cc @@ -18,6 +18,7 @@ #include #include "coder/log.h" #include "coder/opcoders/file_collector.h" +#include "coder/opcoders/nnacl/dequant/de_quant.h" using mindspore::schema::PrimitiveType_MatMul; @@ -77,10 +78,12 @@ int MatMulFP32Coder::Prepare(CoderContext *const context) { params_->b_const_ = (filter_tensor_->data_c() != nullptr); MatMulFP32BaseCoder::InitParameter(); if (params_->a_const_) { - InitShapeA(); + de_quant_flag_ = Dequant::GetInstance()->CheckDequantFlag(input_tensor_); + MS_CHECK_RET_CODE(InitShapeA(), "MatMulFP32Coder init_shape_a failed"); } if (params_->b_const_) { - InitShapeB(); + de_quant_flag_ = Dequant::GetInstance()->CheckDequantFlag(filter_tensor_); + MS_CHECK_RET_CODE(InitShapeB(), "MatMulFP32Coder init_shape_b failed"); } MS_CHECK_RET_CODE(MatMulFP32BaseCoder::Init(), "MatMulFP32Coder init failed"); return ReSize(); diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.cc index ae51dc35b4..30fb30616f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.cc @@ -14,14 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/pad_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/pad_fp32_coder.h" #include #include -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Pad; +using mindspore::schema::PrimitiveType_PadFusion; namespace mindspore::lite::micro::nnacl { @@ -99,5 +99,5 @@ int PadFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Pad, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_PadFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc index 7ce3b2c343..d06f5eef3f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.cc @@ -21,7 +21,8 @@ #include "coder/log.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Pooling; +using mindspore::schema::PrimitiveType_AvgPoolFusion; +using mindspore::schema::PrimitiveType_MaxPoolFusion; namespace mindspore::lite::micro::nnacl { @@ -46,7 +47,7 @@ int PoolingFP32Coder::DoCode(CoderContext *const context) { float minf = -FLT_MAX; float maxf = FLT_MAX; if (pooling_parameter->pool_mode_ == PoolMode_MaxPool) { - Collect(context, {"nnacl/kernel/fp32/max_pooling_fp32_slim.h"}, {"max_pooling_fp32_slim.c"}); + Collect(context, {"nnacl/fp32/pooling_fp32.h"}, {"pooling_fp32.c"}); switch (pooling_parameter->act_type_) { case ActType_Relu: { minf = 0.f; @@ -63,14 +64,9 @@ int PoolingFP32Coder::DoCode(CoderContext *const context) { } } - if (thread_num_ > 1) { - code.CodeBaseStruct("PoolingFp32Args", "args", input_tensor_, output_tensor_, "&pooling_parameter", minf, maxf); - CODE_PARALLEL_FUNC("MaxPoolingFp32Run"); - } else { - code.CodeFunction("MaxPooling", input_tensor_, output_tensor_, "&pooling_parameter", task_id, minf, maxf); - } + code.CodeFunction("MaxPooling", input_tensor_, output_tensor_, "&pooling_parameter", task_id, minf, maxf); } else { - Collect(context, {"nnacl/fp32/pooling.h"}, {"pooling.c"}); + Collect(context, {"nnacl/fp32/pooling_fp32.h"}, {"pooling_fp32.c"}); switch (pooling_parameter->act_type_) { case ActType_Relu: { minf = 0.f; @@ -86,12 +82,7 @@ int PoolingFP32Coder::DoCode(CoderContext *const context) { break; } } - if (thread_num_ > 1) { - code.CodeBaseStruct("PoolingFp32Args", "args", input_tensor_, output_tensor_, "&pooling_parameter", minf, maxf); - CODE_PARALLEL_FUNC("AvgPoolingFp32Run"); - } else { - code.CodeFunction("AvgPooling", input_tensor_, output_tensor_, "&pooling_parameter", task_id, minf, maxf); - } + code.CodeFunction("AvgPooling", input_tensor_, output_tensor_, "&pooling_parameter", task_id, minf, maxf); } MS_LOG(INFO) << "PoolingFp32Code has been called"; @@ -99,5 +90,6 @@ int PoolingFP32Coder::DoCode(CoderContext *const context) { return lite::RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Pooling, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_AvgPoolFusion, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_MaxPoolFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h index 30429acbdd..ae66ea8a0f 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/pooling_fp32_coder.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_POOLFP32_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc index 46999a64f2..52222fe6c8 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/power_fp32_coder.cc @@ -20,7 +20,7 @@ #include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Power; +using mindspore::schema::PrimitiveType_PowFusion; namespace mindspore::lite::micro::nnacl { @@ -55,6 +55,6 @@ int PowerFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Power, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_PowFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc index 55c71f371c..fb005efd5a 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/reduce_fp32_coder.cc @@ -20,7 +20,7 @@ #include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Reduce; +using mindspore::schema::PrimitiveType_PowFusion; namespace mindspore::lite::micro::nnacl { int ReduceFP32Coder::Prepare(CoderContext *const context) { @@ -116,6 +116,6 @@ int ReduceFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Reduce, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_PowFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc index 4e901cb237..239ddf67ef 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/scale_fp32_coder.cc @@ -18,8 +18,9 @@ #include "coder/log.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" -using mindspore::schema::PrimitiveType_Scale; +using mindspore::schema::PrimitiveType_ScaleFusion; namespace mindspore::lite::micro::nnacl { ScaleFP32Coder::~ScaleFP32Coder() { @@ -131,34 +132,26 @@ int ScaleFP32Coder::DoCode(CoderContext *const context) { NNaclFp32Serializer code; code.CodeStruct("scale_parameter", *scale_param_); - if (thread_num_ > 1) { - code.CodeBaseStruct("ScaleFp32Args", "args", input_tensor_, output_tensor_, scale_tensor, offset_tensor, + switch (scale_param_->activation_type_) { + case schema::ActivationType_RELU6: + code.CodeFunction("DoScaleRelu6", input_tensor_, output_tensor_, scale_tensor, offset_tensor, kDefaultTaskId, "&scale_parameter"); - CODE_PARALLEL_FUNC("ScaleFp32Run"); - } else { - int task_id = 0; - switch (scale_param_->activation_type_) { - case schema::ActivationType_RELU6: - code.CodeFunction("DoScaleRelu6", input_tensor_, output_tensor_, scale_tensor, offset_tensor, task_id, - "&scale_parameter"); - break; - case schema::ActivationType_RELU: - code.CodeFunction("DoScaleRelu", input_tensor_, output_tensor_, scale_tensor, offset_tensor, task_id, - "&scale_parameter"); - break; - case schema::ActivationType_NO_ACTIVATION: - code.CodeFunction("DoScale", input_tensor_, output_tensor_, scale_tensor, offset_tensor, task_id, - "&scale_parameter"); - break; - default: - MS_LOG(ERROR) << "Scale does not support activation type " << scale_param_->activation_type_; - return RET_ERROR; - } + break; + case schema::ActivationType_RELU: + code.CodeFunction("DoScaleRelu", input_tensor_, output_tensor_, scale_tensor, offset_tensor, kDefaultTaskId, + "&scale_parameter"); + break; + case schema::ActivationType_NO_ACTIVATION: + code.CodeFunction("DoScale", input_tensor_, output_tensor_, scale_tensor, offset_tensor, kDefaultTaskId, + "&scale_parameter"); + break; + default: + MS_LOG(ERROR) << "Scale does not support activation type " << scale_param_->activation_type_; + return RET_ERROR; } - MS_LOG(INFO) << "ScaleFP32Code has been called"; context->AppendCode(code.str()); return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Scale, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_ScaleFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc deleted file mode 100644 index 15d31e4b41..0000000000 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.cc +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "coder/opcoders/nnacl/fp32/slice_fp32_coder.h" -#include -#include "nnacl/slice_parameter.h" -#include "src/ops/slice.h" -#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "coder/opcoders/file_collector.h" - -using mindspore::schema::PrimitiveType_Slice; -namespace mindspore::lite::micro::nnacl { -int SliceFP32Coder::Prepare(CoderContext *const context) { return RET_OK; } - -int SliceFP32Coder::DoCode(CoderContext *const context) { - // generate code .h .c - Collect(context, {"nnacl/slice_parameter.h", "nnacl/fp32/slice.h"}, {"slice.c"}); - - auto param = reinterpret_cast(parameter_); - auto primitive_slice = reinterpret_cast(OperatorCoder::primitive()); - std::vector begin = primitive_slice->GetPostProcessBegin(); - std::vector size = primitive_slice->GetPostProcessSize(); - std::vector input_shape = input_tensor_->shape(); - NNaclFp32Serializer code; - for (int i = 0; i < param->param_length_; i++) { - param->shape_[i] = input_shape.at(i); - } - - for (int i = 0; i < param->param_length_; i++) { - param->begin_[i] = begin.at(i); - } - - for (int i = 0; i < param->param_length_; i++) { - int tmp_size = size.at(i); - if (size.at(i) < 0) { - tmp_size = input_shape.at(i) - begin.at(i); - } - param->end_[i] = (begin.at(i) + tmp_size); - } - - for (int i = 0; i < param->param_length_; i++) { - if (size.at(i) < 0) { - param->size_[i] = (input_shape.at(i) - begin.at(i)); - continue; - } - param->size_[i] = size.at(i); - } - - code.CodeStruct("slice_parameter", *param); - - // call the op function - if (param->param_length_ < DIMENSION_4D) { - code.CodeFunction("PadSliceParameterTo4D", "&slice_parameter"); - } - code.CodeFunction("DoSliceNoParallel", input_tensor_, output_tensor_, "&slice_parameter"); - context->AppendCode(code.str()); - return RET_OK; -} - -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Slice, CPUOpCoderCreator) -} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h deleted file mode 100644 index 97acb64263..0000000000 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/slice_fp32_coder.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_SLICE_FP32_CODER_H_ -#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_SLICE_FP32_CODER_H_ - -#include -#include "coder/opcoders/op_coder.h" - -namespace mindspore::lite::micro::nnacl { -class SliceFP32Coder final : public OperatorCoder { - public: - SliceFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, - const Model::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} - - ~SliceFP32Coder() override = default; - - int Prepare(CoderContext *const context) override; - - int DoCode(CoderContext *const context) override; -}; -} // namespace mindspore::lite::micro::nnacl -#endif // MINDSPORE_LITE_MICRO_CODER_OPCOD ERS_SLICE_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc index b949590ebb..7ffd6e63e2 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.cc @@ -20,7 +20,7 @@ #include "schema/inner/ops_generated.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_SoftMax; +using mindspore::schema::PrimitiveType_Softmax; namespace mindspore::lite::micro::nnacl { @@ -48,7 +48,7 @@ int SoftMaxFP32Coder::Prepare(CoderContext *const context) { } int SoftMaxFP32Coder::DoCode(CoderContext *const context) { - Collect(context, {"nnacl/fp32/softmax.h"}, {"softmax.c"}); + Collect(context, {"nnacl/fp32/softmax_fp32.h"}, {"softmax_fp32.c", "exp_fp32.c"}); NNaclFp32Serializer code; code.CodeStruct("softmax_parameter", *softmax_param_); code.CodeFunction("memset", sum_data_, "0", sum_data_size_); @@ -58,6 +58,6 @@ int SoftMaxFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_SoftMax, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Softmax, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.h index 92414d6737..322d4d1c51 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/softmax_fp32_coder.h @@ -17,7 +17,7 @@ #define MINDSPORE_LITE_MICRO_CODER_SOFTMAX_CODER_H_ #include -#include "micro/coder/opcoders/base/softmax_base_coder.h" +#include "coder/opcoders/base/softmax_base_coder.h" namespace mindspore::lite::micro::nnacl { class SoftMaxFP32Coder final : public SoftmaxBaseCoder { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.cc new file mode 100644 index 0000000000..739287893f --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.cc @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/fp32/splice_fp32_coder.h" +#include +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "src/common/log_adapter.h" +#include "nnacl/splice_parameter.h" +using mindspore::schema::PrimitiveType_Splice; +namespace mindspore::lite::micro::nnacl { +int SpliceFP32Coder::DoCode(CoderContext *const context) { + auto splice_parameter = reinterpret_cast(parameter_); + // to make forward_indexes nullptr + splice_parameter->forward_indexes_ = nullptr; + std::vector src_shape = input_tensor_->shape(); + std::vector dst_shape = output_tensor_->shape(); + if (src_shape.size() != dst_shape.size() || src_shape.size() != kInputSize2 || dst_shape.size() != kInputSize2) { + MS_LOG(ERROR) << "SpliceFP32Coder src_shape size not equal to dst_shape"; + return RET_ERROR; + } + int src_row = src_shape.at(kInputIndex); + int dst_row = dst_shape.at(kInputIndex); + int src_col = src_shape.at(kBiasIndex); + int dst_col = dst_shape.at(kBiasIndex); + if (src_row != dst_row) { + MS_LOG(ERROR) << "SpliceFP32Coder src_row not equal to dst_row"; + return RET_ERROR; + } + if (src_col * splice_parameter->context_dim_ != dst_col) { + MS_LOG(ERROR) << "SpliceFP32Coder src_col not match to dst_col"; + return RET_ERROR; + } + Collect(context, {"nnacl/splice_parameter.h", "nnacl/fp32/splice_fp32.h"}, {"splice_fp32.c"}); + NNaclFp32Serializer code; + code.CodeStruct("splice_parameter", *splice_parameter); + code.CodeFunction("SpliceFp32", input_tensor_, src_row, src_col, "&splice_parameter", output_tensor_, dst_row, + dst_col); + context->AppendCode(code.str()); + MS_LOG(DEBUG) << "SpliceFP32Coder do_code ok"; + return RET_OK; +} +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Splice, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.h new file mode 100644 index 0000000000..a60b58fae6 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/splice_fp32_coder.h @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_SPLICE_FP32_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_SPLICE_FP32_CODER_H_ +#include +#include "coder/opcoders/op_coder.h" +namespace mindspore::lite::micro::nnacl { +class SpliceFP32Coder final : public OperatorCoder { + public: + SpliceFP32Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~SpliceFP32Coder() override = default; + + int Prepare(CoderContext *const context) override { return RET_OK; } + + int DoCode(CoderContext *const context) override; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_FP32_SPLICE_FP32_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc index 0376e42bac..48d071f599 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/tile_fp32_coder.cc @@ -20,7 +20,7 @@ #include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "coder/opcoders/file_collector.h" -using mindspore::schema::PrimitiveType_Tile; +using mindspore::schema::PrimitiveType_TileFusion; namespace mindspore::lite::micro::nnacl { void TileFP32Coder::ComputeStrides(const int *shape, int *strides, int ndim) const { @@ -63,6 +63,6 @@ int TileFP32Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_Tile, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeFloat32, PrimitiveType_TileFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.cc index efcab9f252..98e4dcca01 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.cc @@ -14,11 +14,11 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h" +#include "coder/opcoders/nnacl/fp32/transpose_fp32_coder.h" #include #include -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/file_collector.h" using mindspore::schema::PrimitiveType_Transpose; namespace mindspore::lite::micro::nnacl { @@ -83,8 +83,8 @@ int TransposeFp32Coder::DoCode(CoderContext *const context) { NNaclFp32Serializer code; code.CodeStruct("transpose_parameter", *transpose_parameter_); - code.CodeFunction("DoTransposeFp32", input_tensor_, output_tensor_, in_shape_, out_shape_, "&transpose_parameter", - task_id, num_unit_thread, dim_size_, position_); + code.CodeFunction("DoTransposeFp32", input_tensor_, output_tensor_, in_shape_, out_shape_, + "(TransposeParameter *)&transpose_parameter", task_id, num_unit_thread, dim_size_, position_); context->AppendCode(code.str()); return RET_OK; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h index e4a150bcc7..01ec048e5c 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/fp32/transpose_fp32_coder.h @@ -39,14 +39,14 @@ class TransposeFp32Coder final : public OperatorCoder { private: TransposeParameter *transpose_parameter_ = nullptr; - int thread_num_ = 1; - int thread_h_stride_ = 0; - int thread_h_num_ = 0; - int num_unit_ = 0; - int *in_shape_ = nullptr; - int *out_shape_ = nullptr; - int *dim_size_ = nullptr; - int *position_ = nullptr; + int thread_num_{1}; + int thread_h_stride_{0}; + int thread_h_num_{0}; + int num_unit_{0}; + int *in_shape_{nullptr}; + int *out_shape_{nullptr}; + int *dim_size_{nullptr}; + int *position_{nullptr}; }; } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/activation_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/activation_int8_coder.cc new file mode 100644 index 0000000000..ff7ba5d327 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/activation_int8_coder.cc @@ -0,0 +1,74 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/sigmoid_int8_coder.h" +#include "coder/opcoders/nnacl/int8/relux_int8_coder.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/activation_fp32.h" +#include "schema/model_generated.h" +#include "src/common/version_manager.h" + +using mindspore::schema::PrimitiveType_Activation; + +namespace mindspore::lite::micro::nnacl { + +std::unique_ptr CPUActivationINT8CoderCreator(const std::vector &in_tensors, + const std::vector &out_tensors, + const Model::Node *node, size_t node_index, + Target target) { + const void *primitive_c = node->primitive_; + if (primitive_c == nullptr) { + return nullptr; + } + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + ParameterGen parameter_gen = + PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(node->primitive_), schema_version); + if (parameter_gen == nullptr) { + MS_LOG(ERROR) << "parameter generator is nullptr"; + return nullptr; + } + OpParameter *parameter = parameter_gen(node->primitive_); + if (parameter == nullptr) { + MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " + << schema::EnumNamePrimitiveType((schema::PrimitiveType)GetPrimitiveType(node->primitive_)); + return nullptr; + } + auto type = (reinterpret_cast(parameter))->type_; + + std::unique_ptr coder; + switch (static_cast(type)) { + case schema::ActivationType_SIGMOID: + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + break; + case schema::ActivationType_RELU: + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + break; + case schema::ActivationType_RELU6: + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + break; + default: + break; + } + + if (coder == nullptr) { + MS_LOG(ERROR) << "create conv2d int8 coder failed"; + return nullptr; + } + return coder; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Activation, CPUActivationINT8CoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.cc index afe7085610..830be1a660 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.cc @@ -14,17 +14,18 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/add_int8_coder.h" +#include "coder/opcoders/nnacl/int8/add_int8_coder.h" #include #include #include "nnacl/int8/quantize.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" -#include "micro/coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" -using mindspore::schema::PrimitiveType_Add; +using mindspore::schema::PrimitiveType_AddFusion; -namespace mindspore::lite::micro { +namespace mindspore::lite::micro::nnacl { int AddInt8Coder::Prepare(CoderContext *const context) { input0 = input_tensors().at(0); @@ -38,26 +39,8 @@ int AddInt8Coder::Prepare(CoderContext *const context) { return RET_OK; } -int AddInt8Coder::DoCode(CoderContext *const context) { - Collect(context, {"wrapper/int8/conv1x1_init_int8.h"}, {"add_int8_wrapper.c", "add_int8.c", "thread_pool.c"}); - - nnacl::NNaclInt8Serializer code; - - code.CodeStruct("para", para_); - code.CodeStruct("arith_para", *arith_para_); - code.CodeBaseStruct("AddArgs", "args", "para", "arith_para", in_size_, out_size_, thread_num_s_, elements_num_, - support_opt_add_, input0, input1, output_tensor_); - - if (arith_para_->broadcasting_) { - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "AddBroadcastRun", "&args", thread_num_s_); - } else { - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "AddRun", "&args", thread_num_s_); - } - - return RET_OK; -} - int AddInt8Coder::Init() { + arith_para_ = reinterpret_cast(parameter_); para_.in0_args_.zp_ = input0->quant_params().front().zeroPoint * -1; para_.in1_args_.zp_ = input1->quant_params().front().zeroPoint * -1; para_.out_zp_ = output_tensor_->quant_params().front().zeroPoint; @@ -152,5 +135,32 @@ int AddInt8Coder::ReSize() { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Add, CPUOpCoderCreator) -} // namespace mindspore::lite::micro +int AddInt8Coder::DoCode(CoderContext *const context) { + Collect(context, {"wrapper/int8/add_int8_wrapper.h"}, + {"add_int8_wrapper.c", "add_int8.c", "arithmetic_base.c", "arithmetic_int8.c", "thread_pool.c"}); + + nnacl::NNaclInt8Serializer code; + + code.CodeStruct("para", para_); + code.CodeStruct("arith_para", *arith_para_); + code.CodeBaseStruct("AddInt8Args", kRunArgs, "¶", "&arith_para", in_size_, out_size_, gThreadNum, elements_num_, + support_opt_add_, input0, input1, output_tensor_); + if (support_parallel_) { + if (arith_para_->broadcasting_) { + code.CodeFunction(kParallelLaunch, gThreadPool, "AddBroadcastInt8Run", kRunArgsAddr, gThreadNum); + } else { + code.CodeFunction(kParallelLaunch, gThreadPool, "AddInt8Run", kRunArgsAddr, gThreadNum); + } + } else { + if (arith_para_->broadcasting_) { + code.CodeFunction("AddBroadcastInt8Run", kRunArgsAddr, kDefaultTaskId); + } else { + code.CodeFunction("AddInt8Run", kRunArgsAddr, kDefaultTaskId); + } + } + context->AppendCode(code.str()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_AddFusion, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.h index c1064a89a9..27eb507c8e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/add_int8_coder.h @@ -18,17 +18,15 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_ADD_INT8_CODER_H_ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/int8/add_int8.h" -namespace mindspore::lite::micro { -class AddInt8Coder : public OperatorCoder { +namespace mindspore::lite::micro::nnacl { +class AddInt8Coder final : public OperatorCoder { public: AddInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, const Model::Node *node, size_t node_index, Target target) - : OperatorCoder(in_tensors, out_tensors, node, node_index, target) { - arith_para_ = reinterpret_cast(parameter_); - } + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} ~AddInt8Coder() override = default; @@ -49,5 +47,5 @@ class AddInt8Coder : public OperatorCoder { int elements_num_{0}; bool support_opt_add_{false}; }; -} // namespace mindspore::lite::micro +} // namespace mindspore::lite::micro::nnacl #endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_ADD_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.cc new file mode 100644 index 0000000000..7f22f79ef4 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.cc @@ -0,0 +1,162 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/batchnorm_int8_coder.h" +#include +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" + +using mindspore::schema::PrimitiveType_BatchNorm; + +namespace mindspore::lite::micro::nnacl { + +int BatchNormInt8Coder::Prepare(CoderContext *const context) { + std::vector input_shapes = input_tensor_->shape(); + size_t n_dim = input_shapes.size(); + batchnorm_param_->channel_ = input_shapes[n_dim - 1]; + batchnorm_param_->units_ = 1; + for (size_t i = 0; i < n_dim - 1; i++) { + batchnorm_param_->units_ *= input_shapes[i]; + } + batchnorm_param_->op_parameter_.thread_num_ = + MSMIN(batchnorm_param_->op_parameter_.thread_num_, batchnorm_param_->channel_); + if (target_ == kARM32M) { + batchnorm_param_->unit_ = batchnorm_param_->units_; + } else { + batchnorm_param_->unit_ = UP_DIV(batchnorm_param_->units_, kMaxThreadNumSupported); + } + if (batchnorm_param_->fused_) { + MS_CHECK_RET_CODE(InitFusedConstTensor(), "InitFusedConstTensor failed"); + } else { + MS_CHECK_RET_CODE(InitConstTensor(), "InitConstTensor failed"); + } + + return RET_OK; +} +int BatchNormInt8Coder::DoCode(CoderContext *context) { + std::vector headers = {"nnacl/slice_parameter.h"}; + std::vector cFiles = {"batchnorm_int8.c"}; + NNaclInt8Serializer code; + + code.CodeStruct("param", *batchnorm_param_); + code.CodeFunction("BatchNormInt8", output_tensor_, input_tensor_, alpha_addr_, beta_addr_, kDefaultTaskId, "¶m"); + + Collect(context, headers, cFiles); + context->AppendCode(code.str()); + + return RET_OK; +} + +int BatchNormInt8Coder::InitConstTensor() { + MS_CHECK_TRUE(input_tensors_.size() >= kInputSize2, "input tensors number not match"); + Tensor *input = input_tensor_; + Tensor *mean = input_tensors_.at(1); + Tensor *variance = input_tensors_.at(2); + Tensor *output = output_tensor_; + + auto mean_ptr = reinterpret_cast(mean->MutableData()); + auto var_ptr = reinterpret_cast(variance->MutableData()); + + MS_CHECK_PTR(mean_ptr); + MS_CHECK_PTR(var_ptr); + + alpha_addr_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat, mean->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(alpha_addr_); + beta_addr_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat, variance->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(beta_addr_); + // compute alpha, beta; + auto eps = batchnorm_param_->epsilon_; + int32_t zp_in = input->quant_params().at(0).zeroPoint; + int32_t zp_mean = mean->quant_params().at(0).zeroPoint; + int32_t zp_var = variance->quant_params().at(0).zeroPoint; + int32_t zp_out = output->quant_params().at(0).zeroPoint; + auto s_in = static_cast(input->quant_params().at(0).scale); + auto s_mean = static_cast(mean->quant_params().at(0).scale); + auto s_var = static_cast(variance->quant_params().at(0).scale); + auto s_out = static_cast(output->quant_params().at(0).scale); + + for (int i = 0; i < batchnorm_param_->channel_; ++i) { + float tmp = s_out * sqrt(eps + s_var * (var_ptr[i] - zp_var)); + float tmp_a = s_in / tmp; + float tmp_b = zp_out - tmp_a * zp_in - (s_mean * (mean_ptr[i] - zp_mean)) / tmp; + alpha_addr_[i] = tmp_a; + beta_addr_[i] = tmp_b; + } + + return RET_OK; +} + +int BatchNormInt8Coder::InitFusedConstTensor() { + MS_CHECK_TRUE(input_tensors_.size() >= 5, "input tensors number not match"); + Tensor *input = input_tensors_.at(0); + Tensor *scale = input_tensors_.at(1); + Tensor *offset = input_tensors_.at(2); + Tensor *mean = input_tensors_.at(3); + Tensor *variance = input_tensors_.at(4); + Tensor *output = output_tensor_; + + auto scale_ptr = reinterpret_cast(scale->MutableData()); + auto offset_ptr = reinterpret_cast(offset->MutableData()); + auto mean_ptr = reinterpret_cast(mean->MutableData()); + auto var_ptr = reinterpret_cast(variance->MutableData()); + + MS_CHECK_PTR(scale_ptr); + MS_CHECK_PTR(offset_ptr); + MS_CHECK_PTR(mean_ptr); + MS_CHECK_PTR(var_ptr); + + alpha_addr_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat, mean->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(alpha_addr_); + beta_addr_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat, variance->ElementsNum() * sizeof(float), kOfflinePackWeight)); + MS_CHECK_PTR(beta_addr_); + // compute alpha, beta; + float eps = batchnorm_param_->epsilon_; + int32_t zp_in = input->quant_params().at(0).zeroPoint; + int32_t zp_scale = scale->quant_params().at(0).zeroPoint; + int32_t zp_offset = offset->quant_params().at(0).zeroPoint; + int32_t zp_mean = mean->quant_params().at(0).zeroPoint; + int32_t zp_var = variance->quant_params().at(0).zeroPoint; + int32_t zp_out = output->quant_params().at(0).zeroPoint; + auto s_in = static_cast(input->quant_params().at(0).scale); + auto s_scale = static_cast(scale->quant_params().at(0).scale); + auto s_offset = static_cast(offset->quant_params().at(0).scale); + auto s_mean = static_cast(mean->quant_params().at(0).scale); + auto s_var = static_cast(variance->quant_params().at(0).scale); + auto s_out = static_cast(output->quant_params().at(0).scale); + + float mul_12 = s_in * s_scale; + float mul_24 = s_scale * s_mean; + float div_36 = s_offset / s_out; + for (int i = 0; i < batchnorm_param_->channel_; ++i) { + float tmp = s_out * sqrt(eps + s_var * (var_ptr[i] - zp_var)); + float tmp_a = (mul_12 * (scale_ptr[i] - zp_scale)) / tmp; + float tmp_b = zp_out + div_36 * (offset_ptr[i] - zp_offset) - tmp_a * zp_in - + (mul_24 * (scale_ptr[i] - zp_scale) * (mean_ptr[i] - zp_mean)) / tmp; + alpha_addr_[i] = tmp_a; + beta_addr_[i] = tmp_b; + } + + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_BatchNorm, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.h new file mode 100644 index 0000000000..0fdd560824 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/batchnorm_int8_coder.h @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_BATCHNORM_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_BATCHNORM_INT8_CODER_H_ + +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/batchnorm_parameter.h" + +namespace mindspore::lite::micro::nnacl { +class BatchNormInt8Coder final : public OperatorCoder { + public: + BatchNormInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) { + batchnorm_param_ = reinterpret_cast(parameter_); + } + + ~BatchNormInt8Coder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *context) override; + + private: + int InitConstTensor(); + int InitFusedConstTensor(); + + float *alpha_addr_{nullptr}; + float *beta_addr_{nullptr}; + BatchNormParameter *batchnorm_param_; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_BATCHNORM_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc index 99c217c990..17a131fe18 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.cc @@ -21,6 +21,7 @@ #include "nnacl/int8/quantize.h" #include "coder/opcoders/file_collector.h" #include "coder/log.h" +#include "coder/opcoders/parallel.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" int MallocQuantArgForConcat(ConcatQuantArg *quant_arg, size_t input_num) { @@ -37,7 +38,6 @@ int ConcatInt8Coder::Prepare(CoderContext *const context) { concat_param_->input_shapes_ = nullptr; size_t input_num = input_tensors().size(); - MS_CHECK_PTR(input_data_); MS_CHECK_RET_CODE(MallocQuantArgForConcat(&concat_param_->quant_arg_, input_num), "Null pointer reference: quant_concat_parm_->in_quant_args_."); for (int i = 0; i < static_cast(input_num); i++) { @@ -60,7 +60,10 @@ int ConcatInt8Coder::Prepare(CoderContext *const context) { concat_param_->input_shapes_ = reinterpret_cast(malloc(sizeof(int *) * input_num)); MS_CHECK_PTR(concat_param_->input_shapes_); for (int i = 0; i < static_cast(input_num); i++) { - concat_param_->input_shapes_[i] = reinterpret_cast(input_tensors().at(i)->shape().data()); + auto in_shape = input_tensors_.at(i)->shape(); + concat_param_->input_shapes_[i] = reinterpret_cast(malloc(in_shape.size() * sizeof(int))); + MS_CHECK_PTR(concat_param_->input_shapes_[i]); + memcpy(reinterpret_cast(concat_param_->input_shapes_[i]), in_shape.data(), sizeof(int) * in_shape.size()); } before_axis_size = 1; @@ -70,7 +73,10 @@ int ConcatInt8Coder::Prepare(CoderContext *const context) { int64_t after_axis_size = 1; int output_dim = static_cast(output_tensor_->shape().size()); - concat_param_->output_shapes_ = output_tensor_->shape().data(); + concat_param_->output_shapes_ = reinterpret_cast(malloc(output_dim * sizeof(int))); + MS_CHECK_PTR(concat_param_->output_shapes_); + memcpy(reinterpret_cast(concat_param_->output_shapes_), output_tensor_->shape().data(), + sizeof(int) * output_dim); for (int i = axis_ + 1; i < output_dim; i++) { after_axis_size *= concat_param_->output_shapes_[i]; } @@ -84,7 +90,8 @@ int ConcatInt8Coder::DoCode(CoderContext *const context) { count_unit_ = thread_num_ > 1 ? UP_DIV(before_axis_size, thread_num_) : before_axis_size; concat_param_->count_unit_ = count_unit_; - Collect(context, {"nnacl/int8/concat_int8.h"}, {"concat_int8.c"}); + Collect(context, {"nnacl/int8/concat_int8.h", "wrapper/int8/concat_int8_wrapper.h"}, + {"concat_int8.c", "concat_int8_wrapper.c"}); NNaclInt8Serializer code; int in_tensor_count = input_tensors().size(); @@ -96,15 +103,12 @@ int ConcatInt8Coder::DoCode(CoderContext *const context) { } code.CodeStruct("concat_param", *concat_param_, in_tensor_count, input_tensor_->shape().size(), output_tensor_->shape().size()); - - if (thread_num_ > 1) { - code.CodeBaseStruct("ConcatInt8Args", "args", "input_data", output_tensor_, "&concat_param", axis_, - before_axis_size, count_unit_); - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "ConcatInt8Run", "&args", "thread_num"); + code.CodeBaseStruct("ConcatInt8Args", kRunArgs, "input_data", output_tensor_, "&concat_param", axis_, + before_axis_size, count_unit_); + if (support_parallel_) { + code.CodeFunction(kParallelLaunch, gThreadPool, "ConcatInt8Run", kRunArgsAddr, gThreadNum); } else { - int task_id = 0; - int64_t real_dst_count = MSMIN(before_axis_size - task_id * count_unit_, count_unit_); - code.CodeFunction("Int8Concat", "input_data", output_tensor_, "&concat_param", axis_, real_dst_count, task_id); + code.CodeFunction("ConcatInt8Run", kRunArgsAddr, kDefaultTaskId); } context->AppendCode(code.str()); return RET_OK; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.h index e71a1f5aaa..bc666683e8 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/concat_int8_coder.h @@ -19,7 +19,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "nnacl/int8/concat_int8.h" namespace mindspore::lite::micro::nnacl { diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.cc index 6b9c1c6608..2ec6685f85 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.cc @@ -14,14 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h" +#include "coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h" #include #include #include "securec/include/securec.h" #include "src/runtime/kernel/arm/base/convolution_base.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" namespace mindspore::lite::micro::nnacl { @@ -43,10 +43,10 @@ int Conv2D1x1Int8Coder::Prepare(CoderContext *const context) { int Conv2D1x1Int8Coder::DoCode(CoderContext *const context) { Collect(context, - {"nnacl/int8/conv1x1_int8.h", "nnacl/common_func.h", "wrapper/int8/conv1x1_init_int8.h", - "wrapper/int8/conv1x1_run_int8.h"}, - {"common_func.c", "pack.c", "conv1x1_int8.c", "matmul_int8.c", "fixed_point.c", "conv1x1_init_int8_wrapper.c", - "conv1x1_run_int8_wrapper.c", "thread_pool.c"}); + {"nnacl/int8/conv1x1_int8.h", "nnacl/common_func.h", "wrapper/int8/conv1x1_init_int8_wrapper.h", + "wrapper/int8/conv1x1_run_int8_wrapper.h"}, + {"common_func.c", "pack_int8.c", "conv1x1_int8.c", "matmul_int8.c", "fixed_point.c", + "conv1x1_init_int8_wrapper.c", "conv1x1_run_int8_wrapper.c", "thread_pool.c", "conv1x1_base.c"}); nnacl::NNaclInt8Serializer code; @@ -54,10 +54,10 @@ int Conv2D1x1Int8Coder::DoCode(CoderContext *const context) { code.CodeStruct("matmul_param", *matmul_param_); code.CodeBaseStruct("Conv1x1Args", "args", input_sum_, filter_zp_ptr_, left_shift_, right_shift_, multiplier_, - packed_weight_, bias_data_, packed_input_, nullptr, nullptr, 0, 0, "conv_param", "matmul_param", + packed_weight_, bias_data_, packed_input_, nullptr, nullptr, 0, 0, "&conv_param", "&matmul_param", matmul_func_, pre_trans_input_, support_optimize_, filter_peroc_); - code.CodeFunction("Conv1x1Run", input_tensor_, "args", "THREAD_POOL_DEFAULT", thread_num_s_, output_tensor_); + code.CodeFunction("Conv1x1Run", input_tensor_, "(Conv1x1Args *)&args", output_tensor_); context->AppendCode(code.str()); return RET_OK; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h index 0f1d9e144a..29185d6273 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h @@ -16,7 +16,7 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_Conv2D_1X1_INT8_CODER_H_ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_Conv2D_1X1_INT8_CODER_H_ -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include #include #include diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.cc index 30ccf2f976..21a3e4f81c 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.cc @@ -14,16 +14,14 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h" -#include +#include "coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h" #include #include "securec/include/securec.h" #include "nnacl/int8/conv3x3_int8.h" -#include "src/runtime/kernel/arm/base/convolution_base.h" -#include "src/runtime/kernel/arm/int8/convolution_3x3_int8.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "coder/opcoders/parallel.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" namespace mindspore::lite::micro::nnacl { void ProcessFilterUint8(int8_t *origin_weight, int16_t *dst_weight, ConvParameter *conv_param) { @@ -129,7 +127,7 @@ int Conv2D3x3Int8Coder::Prepare(CoderContext *const context) { } int Conv2D3x3Int8Coder::DoCode(CoderContext *const context) { - Collect(context, {"nnacl/int8/conv_int8.h"}, {"pack.c", "conv_int8.c", "fixed_point.c"}); + Collect(context, {"nnacl/int8/conv_int8.h"}, {"pack_int8.c", "conv_int8.c", "fixed_point.c"}); nnacl::NNaclInt8Serializer code; code.precision(kPrecision); // call the op function @@ -145,9 +143,9 @@ int Conv2D3x3Int8Coder::DoCode(CoderContext *const context) { code.CodeFunction("PackInputToC8Int8", input_tensor_, c8_input_, "&conv_param_"); // code operator func if (thread_num_ > 1) { - code.CodeBaseStruct("Conv3x3Int8Args", "args", c8_input_, transformed_filter_addr_, new_bias_addr_, output_tensor_, - tile_buffer_, block_unit_buffer_, tmp_dst_buffer_, tmp_out_, "&conv_param_"); - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "Conv3x3Int8Run", "&args", "thread_num"); + code.CodeBaseStruct("Conv3x3Int8Args", kRunArgs, c8_input_, transformed_filter_addr_, new_bias_addr_, + output_tensor_, tile_buffer_, block_unit_buffer_, tmp_dst_buffer_, tmp_out_, "&conv_param_"); + code.CodeFunction(kParallelLaunch, "THREAD_POOL_DEFAULT", "Conv3x3Int8Run", kRunArgsAddr, "thread_num"); } else { int task_id = 0; code.CodeFunction("Conv3x3Int8", c8_input_, transformed_filter_addr_, new_bias_addr_, output_tensor_, tile_buffer_, diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h index d32a7aa4c6..27641d7f94 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h @@ -16,7 +16,7 @@ #ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_Conv2D_3X3_INT8_CODER_H_ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_Conv2D_3X3_INT8_CODER_H_ -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include #include #include @@ -33,7 +33,10 @@ class Conv2D3x3Int8Coder final : public Conv2DBaseCoder { int DoCode(CoderContext *const context) override; - ~Conv2D3x3Int8Coder() override = default; + ~Conv2D3x3Int8Coder() override { + transformed_filter_addr_ = nullptr; + new_bias_addr_ = nullptr; + } private: int InitWeightBias(); diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_int8_coder.cc index 0c60ecc62d..0c75fff1cb 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/conv2d_int8_coder.cc @@ -14,21 +14,22 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/conv2d_int8_coder.h" +#include "coder/opcoders/nnacl/int8/conv2d_int8_coder.h" #include #include #include -#include #include "securec/include/securec.h" -#include "micro/coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h" -#include "micro/coder/log.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/nnacl/int8/conv2d_1x1_int8_coder.h" +#include "coder/opcoders/nnacl/int8/conv2d_3x3_int8_coder.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" #include "src/runtime/kernel/arm/base/convolution_base.h" -#include "src/runtime/kernel/arm/int8/convolution_int8.h" #include "src/ops/populate/populate_register.h" -#include "micro/coder/opcoders/file_collector.h" +#include "src/common/version_manager.h" +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" -using mindspore::schema::PrimitiveType_Conv2D; +using mindspore::schema::PrimitiveType_Conv2DFusion; namespace mindspore::lite::micro::nnacl { @@ -180,42 +181,32 @@ int Conv2DINT8Coder::Resize() { } int Conv2DINT8Coder::DoCode(CoderContext *const context) { - Collect(context, {"nnacl/int8/conv_int8.h", "nnacl/common_func.h", "nnacl/kernel/int8/conv_init_int8.h"}, - {"common_func.c", "pack.c", "conv_int8.c", "winograd_transform.c", "matmul_int8.c", "fixed_point.c", - "conv_init_int8_wrapper.c", "thread_pool.c"}); + std::vector asm_files; + if (target_ == kARM32A) { + asm_files = {"PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "MatmulInt8Neon32.S"}; + } else if (target_ == kARM64) { + asm_files = {"PreSum4x16Int8Peroc.S", "PreSum4x16Int8Pert.S", "MatmulInt8Neon64.S"}; + } + Collect(context, {"nnacl/int8/conv_int8.h", "nnacl/common_func.h", "wrapper/int8/convolution_int8_wrapper.h"}, + {"common_func.c", "pack_int8.c", "conv_int8.c", "winograd_transform.c", "matmul_int8.c", "fixed_point.c", + "convolution_int8_wrapper.c", "conv_init_int8_wrapper.c", "thread_pool.c"}, + asm_files); // call the op function nnacl::NNaclInt8Serializer code; code.precision(kPrecision); code.CodeFunction("memset", packed_input_, 0, packed_input_size_); code.CodeFunction("memset", input_sum_, 0, input_sum_size_); code.CodeFunction("memset", matmul_packed_input_, 0, matmul_packed_input_size_); - - conv_param_->op_parameter_.thread_num_ = thread_num_; - conv_param_->thread_num_ = thread_num_; - code.CodeStruct("conv_param_", *conv_param_); - - // code operator func - if (thread_num_ > 1) { - code.CodeFunction("memset", matmul_packed_input_, 0, matmul_packed_input_size_); - code.CodeBaseStruct("ConvOptInt8Args", "args", input_tensor_, packed_input_, matmul_packed_input_, packed_weight_, - bias_data_, output_tensor_, input_sum_, thread_num_s_, "(ConvParameter *)&conv_param_", - matmul_func_); - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "ConvInt8Run", "&args", "thread_num"); + code.CodeStruct("conv_param", *conv_param_); + + code.CodeBaseStruct("ConvolutionInt8Args", kRunArgs, input_tensor_, packed_input_, matmul_packed_input_, + packed_weight_, bias_data_, output_tensor_, filter_zp_ptr_, input_sum_, + "(ConvParameter *)&conv_param", matmul_func_, support_optimize_); + code.CodeFunction("CheckSupportOptimize", kRunArgsAddr); + if (support_parallel_) { + code.CodeFunction(kParallelLaunch, gThreadPool, "ConvolutionInt8Run", kRunArgsAddr, gThreadNum); } else { - if (target_ == kARM64) { - code << "if (GetSupportOptFlag()) {\n"; - code << "conv_param_.tile_num_ = " << 8 << ";\n"; - code << "} else {\n"; - code << "conv_param_.tile_num_ = " << 4 << ";\n"; - code << "}\n"; - code.CodeFunction("ConvInt8", input_tensor_, packed_input_, matmul_packed_input_, packed_weight_, bias_data_, - output_tensor_, filter_zp_ptr_, input_sum_, 0, "(ConvParameter *)&conv_param_", matmul_func_, - "GetSupportOptFlag()"); - } else { - code.CodeFunction("ConvInt8", input_tensor_, packed_input_, matmul_packed_input_, packed_weight_, bias_data_, - output_tensor_, filter_zp_ptr_, input_sum_, 0, "(ConvParameter *)&conv_param_", matmul_func_, - support_optimize_); - } + code.CodeFunction("ConvolutionInt8Run", kRunArgsAddr, kDefaultTaskId); } context->AppendCode(code.str()); return RET_OK; @@ -224,31 +215,30 @@ int Conv2DINT8Coder::DoCode(CoderContext *const context) { std::unique_ptr CPUConv2DINT8CoderCreator(const std::vector &in_tensors, const std::vector &out_tensors, const Model::Node *node, size_t node_index, Target target) { - PrimitiveC *primitive_c = node->primitive_; - if (!primitive_c) { + const void *primitive = node->primitive_; + if (primitive == nullptr) { return nullptr; } - OpParameter *parameter = - PopulateRegistry::GetInstance()->GetParameterCreator((schema::PrimitiveType(primitive_c->Type())))(primitive_c); - if (parameter == nullptr) { - MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " - << schema::EnumNamePrimitiveType((schema::PrimitiveType)(primitive_c->Type())); + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + ParameterGen paramGen = + PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(node->primitive_), schema_version); + if (paramGen == nullptr) { + MS_LOG(ERROR) << "parameter generator is null"; return nullptr; } - - auto *conv_param = reinterpret_cast(parameter); + auto conv_param = reinterpret_cast(paramGen(node->primitive_)); int kernel_h = conv_param->kernel_h_; int kernel_w = conv_param->kernel_w_; int stride_h = conv_param->stride_h_; int stride_w = conv_param->stride_w_; int dilation_h = conv_param->dilation_h_; int dilation_w = conv_param->dilation_w_; - free(parameter); + free(conv_param); std::unique_ptr coder; if (kernel_h == 3 && kernel_w == 3 && stride_h == 1 && stride_w == 1 && dilation_h == 1 && dilation_w == 1) { coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); } else if (kernel_h == 1 && kernel_w == 1) { - coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); + coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); } else { coder = CPUOpCoderCreator(in_tensors, out_tensors, node, node_index, target); } @@ -259,7 +249,7 @@ std::unique_ptr CPUConv2DINT8CoderCreator(const std::vector #include #include -#include "micro/coder/opcoders/base/conv2d_base_coder.h" +#include "coder/opcoders/base/conv2d_base_coder.h" #include "nnacl/conv_parameter.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" namespace mindspore::lite::micro::nnacl { class Conv2DINT8Coder final : public Conv2DBaseCoder { @@ -34,7 +34,14 @@ class Conv2DINT8Coder final : public Conv2DBaseCoder { int DoCode(CoderContext *const context) override; - ~Conv2DINT8Coder() override = default; + ~Conv2DINT8Coder() override { + packed_weight_ = nullptr; + bias_data_ = nullptr; + filter_zp_ptr_ = nullptr; + matmul_packed_input_ = nullptr; + packed_input_ = nullptr; + input_sum_ = nullptr; + } private: int InitWeightBias(CoderContext *ctx); @@ -63,7 +70,7 @@ class Conv2DINT8Coder final : public Conv2DBaseCoder { int32_t *input_sum_{nullptr}; int8_t *matmul_packed_input_{nullptr}; - string matmul_func_; + std::string matmul_func_; std::function pack_weight_init_{nullptr}; }; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.cc new file mode 100644 index 0000000000..b40c68782f --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.cc @@ -0,0 +1,110 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.h" +#include +#include "coder/log.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "nnacl/int8/conv_depthwise_int8.h" + +using mindspore::schema::PrimitiveType_DepthwiseConv2D; + +namespace mindspore::lite::micro { + +int ConvolutionDepthwiseINT8Coder::Prepare(CoderContext *const context) { + Conv2DBaseCoder::Init(); + // init sliding window param + MS_CHECK_RET_CODE(SetQuantParam(), "Set quant param failed."); + MS_CHECK_RET_CODE(InitWeightBias(context), "dwconvolution do init weightbais failed"); + MS_CHECK_RET_CODE(InitBuffer(context), "dwconvolution do init buffer failed"); + return RET_OK; +} + +int ConvolutionDepthwiseINT8Coder::InitBuffer(CoderContext *const context) { + // malloc pack input and output buffer + row_buffer_size_ = thread_num_ * conv_param_->output_w_ * conv_param_->output_channel_ * sizeof(int32_t); + row_buffer_ = reinterpret_cast(allocator_->Malloc(kNumberTypeInt32, row_buffer_size_, kWorkspace)); + MS_CHECK_PTR(row_buffer_); + return RET_OK; +} + +int ConvolutionDepthwiseINT8Coder::InitWeightBias(CoderContext *const context) { + // init weight, int8 -> int16 + int channel = filter_tensor_->Batch(); + int pack_weight_size = channel * filter_tensor_->Height() * filter_tensor_->Width(); + auto tmp_weight_data_size = static_cast(pack_weight_size * sizeof(int8_t)); + + nnacl::NNaclInt8Serializer code; + + int8_t *tmp_weight = reinterpret_cast(allocator_->Malloc(kNumberTypeInt8, kOnlineSize, kOnlinePackWeight)); + MS_CHECK_PTR(tmp_weight); + code.CodeMallocExpression(tmp_weight, tmp_weight_data_size); + code.CodeFunction("memset", tmp_weight, 0, tmp_weight_data_size); + code.CodeFunction("PackNCHWToNHWCInt8", filter_tensor_, tmp_weight, 1, + filter_tensor_->Height() * filter_tensor_->Width(), filter_tensor_->Batch()); + int weight_zp = conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_; + auto packed_weight_data_size = static_cast(pack_weight_size * sizeof(int16_t)); + packed_weight_ = reinterpret_cast(allocator_->Malloc(kNumberTypeInt16, kOnlineSize, kOnlinePackWeight)); + MS_CHECK_PTR(packed_weight_); + code.CodeMallocExpression(packed_weight_, packed_weight_data_size); + code << "for (int i = 0; i < " << filter_tensor_->ElementsNum() << "; i++) {\n"; + code << " " << allocator_->GetRuntimeAddr(packed_weight_) << "[i] = (int16_t)(" + << allocator_->GetRuntimeAddr(tmp_weight) << "[i] - " << weight_zp << ");\n"; + code << "}\n"; + + auto channel_data_size = static_cast(channel * sizeof(int32_t)); + bias_data_ = reinterpret_cast(allocator_->Malloc(kNumberTypeInt32, kOnlineSize, kOnlinePackWeight)); + MS_CHECK_PTR(bias_data_); + code.CodeMallocExpression(bias_data_, channel_data_size); + code.CodeFunction("memset", bias_data_, 0, channel_data_size); + // init bias + if (input_tensors_.size() == kInputSize2) { + code.CodeFunction("memcpy", bias_data_, bias_tensor_, bias_tensor_->ElementsNum() * sizeof(int32_t)); + } + context->AppendInitCode(code.str()); + return RET_OK; +} + +int ConvolutionDepthwiseINT8Coder::DoCode(CoderContext *const context) { + MS_CHECK_TRUE(conv_param_->input_channel_ == conv_param_->output_channel_, + "Only support input channel equals output channel."); + Collect( + context, + {"nnacl/int8/conv_depthwise_int8.h", "nnacl/int8/pack_int8.h", "wrapper/int8/convolution_depthwise_int8_wrapper.h"}, + {"conv_depthwise_int8.c", "fixed_point.c", "pack_int8.c", "conv_int8.c", "winograd_transform.c", + "convolution_depthwise_int8_wrapper.c"}, + {"ConvDwInt8Row.S", "ConvDwInt8PostAlign4.S", "ConvDwInt8PostAlign4PerChannel.S"}); + nnacl::NNaclInt8Serializer code; + code.precision(kPrecision); + // call the op function + code.CodeFunction("memset", row_buffer_, 0, row_buffer_size_); + code.CodeStruct("conv_param", *conv_param_); + code.CodeBaseStruct("ConvDepthwiseInt8Args", kRunArgs, output_tensor_, row_buffer_, input_tensor_, packed_weight_, + bias_data_, "&conv_param"); + if (support_parallel_) { + code.CodeFunction(kParallelLaunch, gThreadPool, "ConvDepthwiseInt8Run", kRunArgsAddr, "conv_param.thread_num_"); + } else { + code.CodeFunction("ConvDepthwiseInt8Run", kRunArgsAddr, kDefaultTaskId); + } + context->AppendCode(code.str()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_DepthwiseConv2D, + CPUOpCoderCreator) +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.h new file mode 100644 index 0000000000..6b42a73a85 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/convolution_depthwise_int8_coder.h @@ -0,0 +1,52 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODER_CONVOLUTION_DEPTHWISE_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODER_CONVOLUTION_DEPTHWISE_INT8_CODER_H_ + +#include +#include "coder/opcoders/base/conv2d_base_coder.h" +#include "src/runtime/kernel/arm/int8/convolution_depthwise_int8.h" + +namespace mindspore::lite::micro { +class ConvolutionDepthwiseINT8Coder : public Conv2DBaseCoder { + public: + ConvolutionDepthwiseINT8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : Conv2DBaseCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~ConvolutionDepthwiseINT8Coder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + private: + int InitBuffer(CoderContext *const context); + + int InitWeightBias(CoderContext *const context); + + int32_t *row_buffer_{nullptr}; + + size_t row_buffer_size_{0}; + + int16_t *packed_weight_{nullptr}; + + int32_t *bias_data_{nullptr}; +}; +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODER_CONVOLUTION_DEPTHWISE_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.cc index ea9242fcc8..f725eb75df 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,13 +14,13 @@ * limitations under the License. */ -#include "micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.h" +#include "coder/opcoders/nnacl/int8/deconvolution_int8_coder.h" #include #include "nnacl/int8/deconv_int8.h" -#include "micro/coder/opcoders/file_collector.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" -using mindspore::schema::PrimitiveType_DeConv2D; +using mindspore::schema::PrimitiveType_Conv2dTransposeFusion; namespace mindspore::lite::micro::nnacl { @@ -123,7 +123,7 @@ int DeconvolutionInt8Coder::InitRunBuf(CoderContext *const context) { } int DeconvolutionInt8Coder::DoCode(CoderContext *const context) { - Collect(context, {"nnacl/int8/deconv.h"}, {"int8/deconv.c", "pack.c", "quantization/fixed_point.c"}); + Collect(context, {"nnacl/int8/deconv.h"}, {"int8/deconv.c", "pack_int8.c", "quantization/fixed_point.c"}); nnacl::NNaclInt8Serializer code; code.CodeFunction("memset", input_ptr_, 0, input_ptr_size_); @@ -157,5 +157,6 @@ int DeconvolutionInt8Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_DeConv2D, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Conv2dTransposeFusion, + CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.h index 7b5839f3bf..f404e398ad 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/deconvolution_int8_coder.h @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.cc new file mode 100644 index 0000000000..237cf576c8 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.cc @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/detection_post_process_int8_coder.h" + +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "include/errorcode.h" + +using mindspore::schema::PrimitiveType_DetectionPostProcess; + +namespace mindspore::lite::micro::nnacl { +int DetectionPostProcessInt8Coder::MallocInputsBuffer() { + input_boxes_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat32, input_tensors_.at(0)->ElementsNum() * sizeof(float), kWorkspace)); + MS_CHECK_PTR(input_boxes_); + input_scores_ = reinterpret_cast( + allocator_->Malloc(kNumberTypeFloat32, input_tensors_.at(1)->ElementsNum() * sizeof(float), kWorkspace)); + MS_CHECK_PTR(input_boxes_); + return RET_OK; +} + +int DetectionPostProcessInt8Coder::GetInputData(CoderContext *const context, Serializer *const code) { + Tensor *boxes = input_tensors_.at(0); + MS_CHECK_PTR(boxes); + lite::QuantArg boxes_quant_param = boxes->quant_params().front(); + Tensor *scores = input_tensors_.at(1); + MS_CHECK_PTR(scores); + lite::QuantArg scores_quant_param = scores->quant_params().front(); + MS_CHECK_TRUE(boxes->data_type() == kNumberTypeInt8, "Input data type error"); + MS_CHECK_TRUE(scores->data_type() == kNumberTypeInt8, "Input data type error"); + + Collect(context, {"nnacl/int8/quant_dtype_cast_int8.h"}, {"quant_dtype_cast_int8.c"}); + code->CodeFunction("DoDequantizeInt8ToFp32", boxes, input_boxes_, boxes_quant_param.scale, + boxes_quant_param.zeroPoint, boxes->ElementsNum()); + code->CodeFunction("DoDequantizeInt8ToFp32", scores, input_scores_, scores_quant_param.scale, + scores_quant_param.zeroPoint, scores->ElementsNum()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_DetectionPostProcess, + CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.h new file mode 100644 index 0000000000..97f86a8ba2 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/detection_post_process_int8_coder.h @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_INT8_DETECTION_POST_PROCESS_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_INT8_DETECTION_POST_PROCESS_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/base/detection_post_process_base_coder.h" + +namespace mindspore::lite::micro::nnacl { +class DetectionPostProcessInt8Coder final : public DetectionPostProcessBaseCoder { + public: + DetectionPostProcessInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : DetectionPostProcessBaseCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~DetectionPostProcessInt8Coder() override = default; + + private: + int GetInputData(CoderContext *const context, Serializer *const code) override; + int MallocInputsBuffer() override; +}; +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_NNACL_INT8_DETECTION_POST_PROCESS_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.cc new file mode 100644 index 0000000000..cbcf4530ae --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.cc @@ -0,0 +1,78 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/div_int8_coder.h" +#include +#include +#include "include/errorcode.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" + +namespace mindspore::lite::micro::nnacl { + +int DivInt8Coder::Prepare(CoderContext *context) { + input0 = input_tensors_.at(0); + input1 = input_tensors_.at(1); + MS_ASSERT(input0); + MS_ASSERT(input1); + + broadcast_ = input0->ElementsNum() != input1->ElementsNum(); + + param_.in0_args_.scale_ = input0->quant_params().front().scale; + param_.in0_args_.zp_ = -input0->quant_params().front().zeroPoint; + param_.in1_args_.scale_ = input1->quant_params().front().scale; + param_.in1_args_.zp_ = -input1->quant_params().front().zeroPoint; + param_.out_args_.scale_ = output_tensor_->quant_params().front().scale; + param_.out_args_.zp_ = output_tensor_->quant_params().front().zeroPoint; + + const double real_multiplier = param_.in0_args_.scale_ / (param_.in1_args_.scale_ * param_.out_args_.scale_); + + QuantizeMultiplier(real_multiplier, ¶m_.output_multiplier_, ¶m_.output_shift_); + + param_.output_activation_min_ = std::numeric_limits::min(); + param_.output_activation_max_ = std::numeric_limits::max(); + + return RET_OK; +} + +int DivInt8Coder::DoCode(CoderContext *const context) { + NNaclInt8Serializer code; + int element_num = output_tensor_->ElementsNum(); + code.CodeStruct("param", param_); + if (broadcast_) { + ArithmeticParameter tile_para; + tile_para.ndim_ = output_tensor_->shape().size(); + for (size_t i = 0; i < tile_para.ndim_; i++) { + tile_para.in_shape0_[i] = input0->DimensionSize(i); + tile_para.in_shape1_[i] = input1->DimensionSize(i); + tile_para.out_shape_[i] = output_tensor_->DimensionSize(i); + } + tile0_data_ = static_cast(allocator_->Malloc(kNumberTypeInt8, output_tensor_->Size(), kWorkspace)); + tile1_data_ = static_cast(allocator_->Malloc(kNumberTypeInt8, output_tensor_->Size(), kWorkspace)); + MS_CHECK_PTR(tile0_data_); + MS_CHECK_PTR(tile1_data_); + code.CodeStruct("tile_para", tile_para); + code.CodeFunction("TileDimensionsInt8", input0, input1, tile0_data_, tile1_data_, "&tile_para"); + code.CodeFunction("DivInt8", tile0_data_, tile1_data_, output_tensor_, element_num, "¶m"); + } else { + code.CodeFunction("DivInt8", input0, input1, output_tensor_, element_num, "¶m"); + } + + return RET_OK; +} + +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.h new file mode 100644 index 0000000000..d648f040a7 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/div_int8_coder.h @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_DIV_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_DIV_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/int8/quantize.h" + +namespace mindspore::lite::micro::nnacl { + +class DivInt8Coder final : public OperatorCoder { + public: + DivInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~DivInt8Coder() override = default; + + int Prepare(CoderContext *context) override; + + int DoCode(CoderContext *const context) override; + + private: + DivQuantArg param_; + Tensor *input0{nullptr}; + Tensor *input1{nullptr}; + int8_t *tile0_data_{nullptr}; + int8_t *tile1_data_{nullptr}; + bool broadcast_{false}; +}; + +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_DIV_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/fullconnection_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/fullconnection_int8_coder.cc index ca6a03cf12..e1059b886e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/fullconnection_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/fullconnection_int8_coder.cc @@ -24,7 +24,16 @@ using mindspore::schema::PrimitiveType_FullConnection; namespace mindspore::lite::micro::nnacl { -FullConnectionInt8Coder ::~FullConnectionInt8Coder() { FreeQuantParam(); } +FullConnectionInt8Coder ::~FullConnectionInt8Coder() { + FreeQuantParam(); + filter_tensor_ = nullptr; + bias_tensor_ = nullptr; + pack_a_ptr_ = nullptr; + pack_b_ptr_ = nullptr; + input_sums_ = nullptr; + weight_bias_sums_ = nullptr; + bias_ptr_ = nullptr; +} int FullConnectionInt8Coder::MallocQuantParam() { filter_tensor_ = input_tensors_.at(kWeightIndex); diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc index 5b3a9cd1fe..ea788f58bd 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/pooling_int8_coder.cc @@ -15,16 +15,15 @@ */ #include "coder/opcoders/nnacl/int8/pooling_int8_coder.h" #include -#include #include #include "nnacl/int8/pooling_int8.h" #include "coder/log.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" #include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" -using std::string; - -using mindspore::schema::PrimitiveType_Pooling; +using mindspore::schema::PrimitiveType_AvgPoolFusion; +using mindspore::schema::PrimitiveType_MaxPoolFusion; namespace mindspore::lite::micro::nnacl { int PoolingInt8Coder::DoCode(CoderContext *const context) { @@ -58,25 +57,17 @@ int PoolingInt8Coder::DoCode(CoderContext *const context) { pooling_parameter->quant_args_ = quant_args; code.CodeStruct("pooling_parameter", *pooling_parameter); - if (thread_num_ > 1) { - code.CodeBaseStruct("PoolingInt8Args", "args", in_tensor, out_tensor, "(PoolingParameter *)&pooling_parameter"); - if (pooling_parameter->pool_mode_ == PoolMode_MaxPool) { - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "MaxPoolingInt8Run", "&args", "thread_num"); - } else { - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "AvgPoolingInt8Run", "&args", "thread_num"); - } + if (pooling_parameter->pool_mode_ == PoolMode_MaxPool) { + code.CodeFunction("MaxPoolingInt8", in_tensor, out_tensor, "(PoolingParameter *)&pooling_parameter", + kDefaultTaskId); } else { - int task_id = 0; - if (pooling_parameter->pool_mode_ == PoolMode_MaxPool) { - code.CodeFunction("MaxPoolingInt8", in_tensor, out_tensor, "(PoolingParameter *)&pooling_parameter", task_id); - } else { - code.CodeFunction("AvgPoolingInt8", in_tensor, out_tensor, "(PoolingParameter *)&pooling_parameter", task_id); - } + code.CodeFunction("AvgPoolingInt8", in_tensor, out_tensor, "(PoolingParameter *)&pooling_parameter", + kDefaultTaskId); } - MS_LOG(INFO) << "PoolingInt8Code has been called"; context->AppendCode(code.str()); return lite::RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Pooling, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_AvgPoolFusion, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_MaxPoolFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc index da7f81687e..c9550bb98e 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.cc @@ -20,7 +20,7 @@ #include "coder/log.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" -using mindspore::schema::PrimitiveType_Reduce; +using mindspore::schema::PrimitiveType_ReduceFusion; namespace mindspore::lite::micro::nnacl { int ReduceInt8Coder::CalculateQuantArgs() { QuantArg input_quant = input_tensor_->quant_params().at(0); @@ -230,6 +230,6 @@ int ReduceInt8Coder::DoCode(CoderContext *const context) { return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Reduce, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_ReduceFusion, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h index 3ab5919f2e..24fc456416 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reduce_int8_coder.h @@ -30,7 +30,8 @@ class ReduceInt8Coder final : public ReduceBaseCoder { const Model::Node *node, size_t node_index, Target target) : ReduceBaseCoder(in_tensors, out_tensors, node, node_index, target) {} - ~ReduceInt8Coder() override = default; + ~ReduceInt8Coder() override { begin_src_data_ = nullptr; } + int Prepare(CoderContext *const context) override; int DoCode(CoderContext *const context) override; diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.cc new file mode 100644 index 0000000000..7c72500d13 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.cc @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/relux_int8_coder.h" +#include "nnacl/fp32/activation_fp32.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "coder/log.h" +#include "include/errorcode.h" + +namespace mindspore::lite::micro::nnacl { + +int ReluxInt8Coder::Prepare(CoderContext *const context) { + MS_CHECK_PTR(parameter_); + type_ = (reinterpret_cast(parameter_))->type_; + + quant_arg_.input_arg.scale_ = input_tensor_->quant_params().front().scale; + quant_arg_.input_arg.zp_ = input_tensor_->quant_params().front().zeroPoint; + quant_arg_.output_arg.scale_ = output_tensor_->quant_params().front().scale; + quant_arg_.output_arg.zp_ = output_tensor_->quant_params().front().zeroPoint; + + const double multiplier = quant_arg_.input_arg.scale_ / quant_arg_.output_arg.scale_; + QuantizeRoundParameterWithDoublePrecision(multiplier, &quant_arg_.input_multiplier_, &quant_arg_.left_shift_, + &quant_arg_.right_shift_); + + return RET_OK; +} + +int ReluxInt8Coder::DoCode(CoderContext *const context) { + Collect(context, {"nnacl/int8/relux_int8.h"}, {"relux_int8.c"}); + + NNaclInt8Serializer code; + + int length = input_tensor_->ElementsNum(); + + code.CodeStruct("quant_arg", quant_arg_); + code.CodeFunction("ReluXInt8", input_tensor_, length, output_tensor_, "&quant_arg"); + + context->AppendCode(code.str()); + + return RET_OK; +} + +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.h new file mode 100644 index 0000000000..ff55311dfb --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/relux_int8_coder.h @@ -0,0 +1,81 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_RELUX_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_RELUX_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/int8/relux_int8.h" +#include "coder/log.h" +#include "include/errorcode.h" + +namespace mindspore::lite::micro::nnacl { + +class ReluxInt8Coder : public OperatorCoder { + public: + ReluxInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~ReluxInt8Coder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + protected: + ReluXQuantArg quant_arg_; + + private: + int type_; +}; + +class ReluInt8Coder final : public ReluxInt8Coder { + public: + ReluInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : ReluxInt8Coder(in_tensors, out_tensors, node, node_index, target) {} + + ~ReluInt8Coder() override = default; + + int Prepare(CoderContext *const context) override { + MS_CHECK_RET_CODE(ReluxInt8Coder::Prepare(context), "ReluxInt8Coder::Prepare failed"); + quant_arg_.quantized_output_min = quant_arg_.output_arg.zp_; + quant_arg_.quantized_output_max = CHAR_MAX; + return RET_OK; + }; +}; + +class Relu6Int8Coder final : public ReluxInt8Coder { + public: + Relu6Int8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : ReluxInt8Coder(in_tensors, out_tensors, node, node_index, target) {} + + ~Relu6Int8Coder() override = default; + + int Prepare(CoderContext *const context) override { + MS_CHECK_RET_CODE(ReluxInt8Coder::Prepare(context), "ReluxInt8Coder::Prepare failed"); + quant_arg_.quantized_output_min = QuantizeToInt8(0, quant_arg_.output_arg.scale_, quant_arg_.output_arg.zp_); + quant_arg_.quantized_output_max = QuantizeToInt8(6, quant_arg_.output_arg.scale_, quant_arg_.output_arg.zp_); + return RET_OK; + }; +}; + +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_RELUX_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc index 0df7a07986..eb83acf6be 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/reshape_int8_coder.cc @@ -43,13 +43,8 @@ int ReshapeInt8Coder::DoCode(CoderContext *const context) { INT8_MIN, INT8_MAX}; code.CodeStruct("reshape_quant_arg", reshape_quant_arg); + code.CodeFunction("Int8Reshape", input, output, elements_num, "reshape_quant_arg"); - if (thread_num_ > 1) { - code.CodeBaseStruct("ReshapeInt8Args", "args", input, output, elements_num, thread_num_s_, "reshape_quant_arg"); - CODE_PARALLEL_FUNC("ReshapeInt8Run"); - } else { - code.CodeFunction("Int8Reshape", input, output, elements_num, "reshape_quant_arg"); - } context->AppendCode(code.str()); return RET_OK; } diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.cc new file mode 100644 index 0000000000..066bcc8c1c --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.cc @@ -0,0 +1,109 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/resize_int8_coder.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" +#include "securec/include/securec.h" +#include "nnacl/int8/quantize.h" +#include "coder/opcoders/parallel.h" + +using mindspore::schema::PrimitiveType_Resize; + +namespace mindspore::lite::micro::nnacl { +ResizeInt8Coder::~ResizeInt8Coder() { + delete quant_out_; + quant_out_ = nullptr; + delete quant_in_; + quant_in_ = nullptr; + delete multiplier_; + multiplier_ = nullptr; +} + +int ResizeInt8Coder::Prepare(CoderContext *const context) { + MS_CHECK_RET_CODE(ResizeBaseCoder::Init(), "init resize base failed"); + quant_in_ = new (std::nothrow)::QuantArg; + quant_out_ = new (std::nothrow)::QuantArg; + multiplier_ = new (std::nothrow) QuantMulArg; + MS_CHECK_PTR(quant_in_); + MS_CHECK_PTR(quant_out_); + MS_CHECK_PTR(multiplier_); + quant_in_->zp_ = input_tensor_->quant_params().at(0).zeroPoint; + quant_in_->scale_ = input_tensor_->quant_params().at(0).scale; + quant_out_->zp_ = output_tensor_->quant_params().at(0).zeroPoint; + quant_out_->scale_ = output_tensor_->quant_params().at(0).scale; + + QuantizeRoundParameterWithDoublePrecision(quant_in_->scale_ / quant_out_->scale_, &multiplier_->multiplier_, + &multiplier_->left_shift_, &multiplier_->right_shift_); + return ReSize(); +} + +int ResizeInt8Coder::ReSize() { + if (method_ == schema::ResizeMethod_LINEAR) { + MS_LOG(ERROR) << "unsupported resize linear currently"; + return RET_ERROR; + } + return RET_OK; +} + +int ResizeInt8Coder::DoCode(CoderContext *const context) { + std::vector headers = {"nnacl/int8/resize_int8.h", "wrapper/int8/resize_int8_wrapper.h"}; + std::vector cFiles = {"resize_int8.c", "common_func.c", "resize_int8_wrapper.c"}; + Collect(context, headers, cFiles); + + nnacl::NNaclInt8Serializer code; + code.CodeArray("input_shape", input_tensor_->shape().data(), input_tensor_->shape().size(), false); + code.CodeArray("output_shape", output_tensor_->shape().data(), output_tensor_->shape().size(), false); + switch (method_) { + case static_cast(schema::ResizeMethod_LINEAR): { + MS_LOG(ERROR) << "unsupported: " << schema::EnumNameResizeMethod(static_cast(method_)); + break; + } + case static_cast(schema::ResizeMethod_NEAREST): { + bool same_zp = quant_in_->zp_ == quant_out_->zp_; + bool same_scale = abs(quant_out_->scale_ - quant_in_->scale_) < 1e-6; + bool align_corners = coordinate_transform_mode_ == schema::CoordinateTransformMode_ALIGN_CORNERS; + if (same_zp && same_scale) { + code.CodeBaseStruct("ResizeInt8Args", kRunArgs, input_tensor_, output_tensor_, "&input_shape", "&output_shape", + align_corners, thread_num_); + if (support_parallel_) { + code.CodeFunction(kParallelLaunch, gThreadPool, "ResizeInt8Run", kRunArgsAddr, gThreadNum); + } else { + code.CodeFunction("ResizeInt8Run", kRunArgsAddr, kDefaultTaskId); + } + } else { + MS_LOG(WARNING) << "unsupported parallel launch currently"; + code.CodeStruct("quant_in", *quant_in_); + code.CodeStruct("quant_out", *quant_out_); + code.CodeStruct("multiplier", *multiplier_); + code.CodeFunction("ResizeNearestNeighborInt8", input_tensor_, output_tensor_, "&input_shape", "&output_shape", + align_corners, "multiplier", "quant_in", "quant_out", 0, thread_num_); + } + break; + } + case schema::ResizeMethod_UNKNOWN: + default: { + MS_LOG(ERROR) << "Resize unknown method " << method_; + return RET_ERROR; + } + } + context->AppendCode(code.str()); + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Resize, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.h new file mode 100644 index 0000000000..ca849dfc58 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/resize_int8_coder.h @@ -0,0 +1,60 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_SLICE_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_SLICE_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/base/resize_base_coder.h" +#include "nnacl/op_base.h" + +namespace mindspore::lite::micro::nnacl { +class ResizeInt8Coder final : public ResizeBaseCoder { + public: + ResizeInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : ResizeBaseCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~ResizeInt8Coder(); + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + private: + int ReSize(); + int InitResizeBiLinear(); + int InitFloatResizeBiLinear(); + int InitResizeQuantArg(); + int CalRatio(); + int CalInterpolationRange(); + void FreeResizeBiLinear(); + int InitResizeFloatQuantArg(); + int CalFloatRatio(); + int CalFloatInterpolationRange(); + void FreeFloatResizeBiLinear(); + + ResizeParameter *param_{nullptr}; + ::QuantArg *quant_in_{nullptr}; + ::QuantArg *quant_out_{nullptr}; + QuantMulArg *multiplier_{nullptr}; + ResizeQuantArg resize_quant_arg_; + ResizeFloatScaleQuantArg resize_float_quant_arg_; +}; + +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_SLICE_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc new file mode 100644 index 0000000000..2593ea7154 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc @@ -0,0 +1,74 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/sigmoid_int8_coder.h" +#include +#include +#include "coder/log.h" +#include "include/errorcode.h" +#include "coder/opcoders/file_collector.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" + +namespace mindspore::lite::micro::nnacl { + +void CalculateTableList(int8_t *table, const float input_scale, const int32_t input_zp) { + int32_t min_value = std::numeric_limits::min(); + int32_t max_value = std::numeric_limits::max(); + const float output_scale = 1.0f / 256; + const int32_t output_zp = -128; + + for (int i = min_value; i < max_value; ++i) { + const float real_input_value = input_scale * (i - input_zp); + const float sigmoid_value = 1.0f / (1.0f + std::exp(-real_input_value)); + const int32_t quantized = std::round(sigmoid_value / output_scale) + output_zp; + auto out_value = static_cast(std::max(std::min(quantized, max_value), min_value)); + auto index = static_cast(i); + table[index] = out_value; + } +} + +int SigmodInt8Coder::Prepare(CoderContext *const context) { + size_t int8_range = 256; + table_list_ = static_cast(allocator_->Malloc(kNumberTypeInt8, int8_range, kOfflinePackWeight)); + MS_CHECK_PTR(table_list_); + + const float input_scale = input_tensor_->quant_params().at(0).scale; + const int32_t input_zp = input_tensor_->quant_params().at(0).zeroPoint; + const float output_scale = output_tensor_->quant_params().at(0).scale; + const int32_t output_zp = output_tensor_->quant_params().at(0).zeroPoint; + if (output_scale != (1.0f / 256) || output_zp != -128) { + MS_LOG(ERROR) << "Output scale is : " << output_scale << ", should be 1/256. Output zp is : " << output_zp + << ", should be -128."; + return RET_ERROR; + } + CalculateTableList(table_list_, input_scale, input_zp); + return RET_OK; +} + +int SigmodInt8Coder::DoCode(CoderContext *const context) { + Collect(context, {"nnacl/int8/sigmoid_int8.h"}, {"sigmoid_int8.c"}); + + NNaclInt8Serializer code; + + int length = input_tensor_->ElementsNum(); + code.CodeFunction("SigmoidInt8", input_tensor_, length, output_tensor_, table_list_); + + context->AppendCode(code.str()); + + return RET_OK; +} + +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.h new file mode 100644 index 0000000000..f6d015a197 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.h @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_SIGMOID_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_SIGMOID_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/op_coder.h" + +namespace mindspore::lite::micro::nnacl { + +class SigmodInt8Coder final : public OperatorCoder { + public: + SigmodInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~SigmodInt8Coder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + private: + int8_t *table_list_{nullptr}; +}; + +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_SIGMOID_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc index 40a1bfc528..a75cb60a82 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/softmax_int8_coder.cc @@ -24,8 +24,9 @@ #include "coder/log.h" #include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" #include "coder/opcoders/file_collector.h" +#include "coder/opcoders/parallel.h" -using mindspore::schema::PrimitiveType_SoftMax; +using mindspore::schema::PrimitiveType_Softmax; namespace mindspore::lite::micro::nnacl { int SoftMaxInt8Coder::Prepare(CoderContext *const context) { @@ -64,15 +65,10 @@ int SoftMaxInt8Coder::Prepare(CoderContext *const context) { int SoftMaxInt8Coder::DoCode(CoderContext *const context) { int outter_size = 1; - int inner_size = 1; for (int i = 0; i < softmax_param_->axis_; i++) { outter_size *= softmax_param_->input_shape_[i]; } MS_CHECK_TRUE(softmax_param_->n_dim_ < 5, "n_dim should be less than the length of maximum value of input_shape"); - for (int i = softmax_param_->axis_; i < softmax_param_->n_dim_; i++) { - inner_size *= softmax_param_->input_shape_[i]; - } - Collect(context, {"nnacl/int8/softmax_int8.h"}, {"softmax_int8.c", "fixed_point.c"}); NNaclInt8Serializer code; @@ -84,22 +80,14 @@ int SoftMaxInt8Coder::DoCode(CoderContext *const context) { code.CodeFunction("memset", exp_data_, 0, exp_data_size_); code.CodeFunction("memset", sum_data_, 0, sum_data_size_); - if (thread_num_ > 1) { - code.CodeBaseStruct("SoftmaxInt8Args", "args", input_tensor_, output_tensor_, outter_size, inner_size, exp_data_, - sum_data_, thread_num_s_, "quant_args", "(SoftmaxParameter *)&softmax_param"); - code.CodeFunction("ParallelLaunch", "THREAD_POOL_DEFAULT", "SoftmaxInt8Run", "&args", "thread_num"); - } else { - int task_id = 0; - MS_CHECK_TRUE(thread_num_ > 0, "thread_num_ <= 0"); - int stride = UP_DIV(outter_size, thread_num_); - int count = MSMIN(stride, outter_size - stride * task_id); - code.CodeFunction("SoftmaxInt8", input_tensor_, output_tensor_, count, exp_data_, sum_data_, "quant_args", - "(SoftmaxParameter *)&softmax_parameter"); - } + MS_CHECK_TRUE(thread_num_ > 0, "thread_num_ <= 0"); + int stride = UP_DIV(outter_size, thread_num_); + int count = MSMIN(stride, outter_size - stride * kDefaultTaskId); + code.CodeFunction("SoftmaxInt8", input_tensor_, output_tensor_, count, exp_data_, sum_data_, "quant_args", + "(SoftmaxParameter *)&softmax_parameter"); context->AppendCode(code.str()); - return RET_OK; } -REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_SoftMax, CPUOpCoderCreator) +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_Softmax, CPUOpCoderCreator) } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.cc new file mode 100644 index 0000000000..fb9b7f9c14 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.cc @@ -0,0 +1,105 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/nnacl/int8/sub_int8_coder.h" +#include +#include +#include "include/errorcode.h" +#include "coder/log.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/file_collector.h" + +using mindspore::schema::PrimitiveType_SubFusion; + +namespace mindspore::lite::micro::nnacl { + +int SubInt8Coder::Prepare(CoderContext *const context) { + input0 = input_tensors_.at(0); + input1 = input_tensors_.at(1); + MS_ASSERT(input0); + MS_ASSERT(input1); + + broadcast_ = input0->ElementsNum() != input1->ElementsNum(); + + param_.in0_args_.scale_ = input0->quant_params().front().scale; + param_.in0_args_.zp_ = -input0->quant_params().front().zeroPoint; + param_.in1_args_.scale_ = input1->quant_params().front().scale; + param_.in1_args_.zp_ = -input1->quant_params().front().zeroPoint; + param_.out_args_.scale_ = output_tensor_->quant_params().front().scale; + param_.out_args_.zp_ = output_tensor_->quant_params().front().zeroPoint; + + const int left_shift = 20; + const double twice_max_input_scale = 2 * std::max(param_.in0_args_.scale_, param_.in1_args_.scale_); + const double real_input0_multiplier = param_.in0_args_.scale_ / twice_max_input_scale; + const double real_input1_multiplier = param_.in1_args_.scale_ / twice_max_input_scale; + const double real_output_multiplier = twice_max_input_scale / ((1 << left_shift) * param_.out_args_.scale_); + + QuantizeMultiplierSmallerThanOne(real_input0_multiplier, ¶m_.input0_multiplier_, ¶m_.input0_shift_); + QuantizeMultiplierSmallerThanOne(real_input1_multiplier, ¶m_.input1_multiplier_, ¶m_.input1_shift_); + QuantizeMultiplierSmallerThanOne(real_output_multiplier, ¶m_.output_multiplier_, ¶m_.output_shift_); + + param_.output_activation_min_ = std::numeric_limits::min(); + param_.output_activation_max_ = std::numeric_limits::max(); + + int left_shift0 = -param_.input0_shift_ > 0 ? -param_.input0_shift_ : 0; + param_.right_shift0_ = -param_.input0_shift_ > 0 ? 0 : param_.input0_shift_; + + int left_shift1 = -param_.input1_shift_ > 0 ? -param_.input1_shift_ : 0; + param_.right_shift1_ = -param_.input1_shift_ > 0 ? 0 : param_.input1_shift_; + + param_.left_shift_out_ = -param_.output_shift_ > 0 ? -param_.output_shift_ : 0; + param_.right_shift_out_ = -param_.output_shift_ > 0 ? 0 : param_.output_shift_; + + param_.left_shift_result0_ = (1 << left_shift) * ((1 << left_shift0)); + param_.left_shift_result1_ = (1 << left_shift) * ((1 << left_shift1)); + + MS_CHECK_TRUE(left_shift + left_shift0 == left_shift, "shift not match"); + MS_CHECK_TRUE(left_shift + left_shift1 == left_shift, "shift not match"); + + return RET_OK; +} + +int SubInt8Coder::DoCode(CoderContext *const context) { + NNaclInt8Serializer code; + // Todo: Parallel run wrapper + auto element_num = output_tensor_->ElementsNum(); + code.CodeStruct("param", param_); + if (broadcast_) { + ArithmeticParameter tile_para; + tile_para.ndim_ = output_tensor_->shape().size(); + for (size_t i = 0; i < tile_para.ndim_; i++) { + tile_para.in_shape0_[i] = input0->DimensionSize(i); + tile_para.in_shape1_[i] = input1->DimensionSize(i); + tile_para.out_shape_[i] = output_tensor_->DimensionSize(i); + } + tile0_data_ = static_cast(allocator_->Malloc(kNumberTypeInt8, output_tensor_->Size(), kWorkspace)); + MS_CHECK_PTR(tile0_data_); + tile1_data_ = static_cast(allocator_->Malloc(kNumberTypeInt8, output_tensor_->Size(), kWorkspace)); + MS_CHECK_PTR(tile1_data_); + + code.CodeStruct("tile_para", tile_para); + + code.CodeFunction("TileDimensionsInt8", input0, input1, tile0_data_, tile1_data_, "&tile_para"); + code.CodeFunction("SubInt8", tile0_data_, tile1_data_, output_tensor_, element_num, "¶m"); + } else { + code.CodeFunction("SubInt8", input0, input1, output_tensor_, element_num, "¶m"); + } + + return RET_OK; +} + +REG_OPERATOR_CODER(kAllTargets, kNumberTypeInt8, PrimitiveType_SubFusion, CPUOpCoderCreator) +} // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.h b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.h new file mode 100644 index 0000000000..aab1adf59f --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sub_int8_coder.h @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_SUB_INT8_CODER_H_ +#define MINDSPORE_LITE_MICRO_CODER_SUB_INT8_CODER_H_ + +#include +#include +#include +#include "coder/opcoders/op_coder.h" +#include "nnacl/int8/quantize.h" + +namespace mindspore::lite::micro::nnacl { + +class SubInt8Coder final : public OperatorCoder { + public: + SubInt8Coder(const std::vector &in_tensors, const std::vector &out_tensors, + const Model::Node *node, size_t node_index, Target target) + : OperatorCoder(in_tensors, out_tensors, node, node_index, target) {} + + ~SubInt8Coder() override = default; + + int Prepare(CoderContext *const context) override; + + int DoCode(CoderContext *const context) override; + + private: + SubQuantArg param_; + Tensor *input0{nullptr}; + Tensor *input1{nullptr}; + int8_t *tile0_data_{nullptr}; + int8_t *tile1_data_{nullptr}; + bool broadcast_{false}; +}; + +} // namespace mindspore::lite::micro::nnacl +#endif // MINDSPORE_LITE_MICRO_CODER_SUB_INT8_CODER_H_ diff --git a/mindspore/lite/micro/coder/opcoders/op_coder.cc b/mindspore/lite/micro/coder/opcoders/op_coder.cc index bc4820e400..1c956788e7 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/op_coder.cc @@ -15,7 +15,9 @@ */ #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" +#include "coder/opcoders/parallel.h" + namespace mindspore::lite::micro { OperatorCoder::~OperatorCoder() { @@ -46,12 +48,7 @@ void OperatorCoder::set_parameter(OpParameter *parameter) { this->parameter_ = p size_t OperatorCoder::node_index() const { return node_index_; } void OperatorCoder::set_thread_num(int thread_num) { - if (thread_num == 4) { - this->thread_num_ = thread_num; - this->thread_num_s_ = "thread_num"; - return; - } else { - return; - } + thread_num_ = thread_num; + support_parallel_ = thread_num_ == kMaxThreadNumSupported; } } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/op_coder.h b/mindspore/lite/micro/coder/opcoders/op_coder.h index 1560b79ddc..3c53e4a74c 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder.h +++ b/mindspore/lite/micro/coder/opcoders/op_coder.h @@ -32,23 +32,21 @@ namespace mindspore::lite::micro { constexpr int kPrecision = 19; -#define CODE_PARALLEL_FUNC(func) code << "ParallelLaunch(THREAD_POOL_DEFAULT, " << func << ", &args, thread_num);\n" - class OperatorCoder { public: OperatorCoder(const std::vector &in_tensors, const std::vector &out_tensors, const Model::Node *node, size_t node_index, Target target) : input_tensors_(in_tensors), output_tensors_(out_tensors), - node_(node), target_(target), + node_(node), node_index_(node_index) { allocator_ = MemoryAllocator::GetInstance(); // vectors checked not empty in OpCoderBuilder::build input_tensor_ = input_tensors_.at(kInputIndex); output_tensor_ = output_tensors_.at(kOutputIndex); } - std::string ID() const { return node_->name_; } + std::string name() const { return node_->name_; } void set_input_tensor_indices(const std::vector &input_indices); void set_output_tensor_indices(const std::vector &output_indices); @@ -67,7 +65,6 @@ class OperatorCoder { size_t node_index() const; void set_parameter(OpParameter *parameter); - const PrimitiveC *primitive() const { return node_->primitive_; } const Model::Node *node() const { return this->node_; } @@ -87,8 +84,8 @@ class OperatorCoder { protected: std::vector input_tensors_; std::vector output_tensors_; - const Model::Node *node_{nullptr}; Target target_{kTargetUnknown}; + const Model::Node *node_{nullptr}; Tensor *input_tensor_{nullptr}; Tensor *output_tensor_{nullptr}; @@ -96,7 +93,7 @@ class OperatorCoder { MemoryAllocator *allocator_{nullptr}; - std::string thread_num_s_{"1"}; + bool support_parallel_{false}; int thread_num_{1}; private: @@ -114,6 +111,10 @@ template std::unique_ptr CPUOpCoderCreator(const std::vector &in_tensors, const std::vector &out_tensors, const Model::Node *node, size_t node_index, Target target) { + if (node == nullptr) { + MS_LOG(ERROR) << "node is null"; + return nullptr; + } std::unique_ptr coder = std::make_unique(in_tensors, out_tensors, node, node_index, target); return coder; } diff --git a/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc b/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc index 106d6ddfaf..b5704ea940 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc +++ b/mindspore/lite/micro/coder/opcoders/op_coder_builder.cc @@ -13,20 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/op_coder_builder.h" +#include "coder/opcoders/op_coder_builder.h" #include #include #include "micro/coder/allocator/allocator.h" +#include "src/common/prim_util.h" +#include "src/common/version_manager.h" #include "src/ops/populate/populate_register.h" +#include "coder/opcoders/parallel.h" namespace mindspore::lite::micro { -constexpr int kMAX_THREAD_NUM_SUPPORT = 4; std::unique_ptr OpCoderBuilder::build() { - if (node_->primitive_ == nullptr) { - return nullptr; - } - auto primitive_type = static_cast(node_->primitive_->Type()); + MS_CHECK_PTR_RET_NULL(node_->primitive_); + int primitive_type = GetPrimitiveType(node_->primitive_); CoderKey coder_key(target_, data_type_, primitive_type); CoderCreatorFunc creator_func = OpCoderFactory::GetInstance()->FindOpCoder(coder_key); if (creator_func == nullptr) { @@ -52,16 +52,22 @@ std::unique_ptr OpCoderBuilder::build() { << " code_target: " << target_ << " data_type: " << EnumNameDataType(data_type_); return op_coder; } - OpParameter *parameter = - PopulateRegistry::GetInstance()->GetParameterCreator((schema::PrimitiveType(primitive_type)))(node_->primitive_); + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + ParameterGen paramGen = + PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(node_->primitive_), schema_version); + if (paramGen == nullptr) { + MS_LOG(ERROR) << "parameter generator is null"; + return nullptr; + } + OpParameter *parameter = paramGen(node_->primitive_); if (parameter == nullptr) { MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " - << schema::EnumNamePrimitiveType((schema::PrimitiveType)(primitive_type)); + << PrimitiveTypeName(GetPrimitiveType(node_->primitive_)); return nullptr; } op_coder->set_input_tensor_indices(input_indices_); op_coder->set_output_tensor_indices(output_indices_); - int thread_num = this->mode_ == CodeMode::Code_Inference ? kMAX_THREAD_NUM_SUPPORT : 1; + int thread_num = support_parallel_ ? kMaxThreadNumSupported : 1; op_coder->set_thread_num(thread_num); parameter->thread_num_ = thread_num; op_coder->set_parameter(parameter); @@ -108,6 +114,11 @@ OpCoderBuilder &OpCoderBuilder::target(Target target) { return *this; } +OpCoderBuilder &OpCoderBuilder::support_parallel(bool parallel) { + support_parallel_ = parallel; + return *this; +} + void OpCoderBuilder::Reset() {} } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/op_coder_builder.h b/mindspore/lite/micro/coder/opcoders/op_coder_builder.h index c4596f8fa2..bab3e96fb2 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder_builder.h +++ b/mindspore/lite/micro/coder/opcoders/op_coder_builder.h @@ -18,7 +18,7 @@ #include #include -#include "micro/coder/opcoders/op_coder.h" +#include "coder/opcoders/op_coder.h" #include "micro/coder/allocator/allocator.h" namespace mindspore::lite::micro { @@ -43,6 +43,8 @@ class OpCoderBuilder { OpCoderBuilder &target(Target target); + OpCoderBuilder &support_parallel(bool parallel); + void Reset(); private: @@ -56,13 +58,15 @@ class OpCoderBuilder { Target target_{kTargetUnknown}; - TypeId data_type_ = kTypeUnknown; + TypeId data_type_{kTypeUnknown}; - CodeMode mode_ = Code_Normal; + CodeMode mode_{Code_Unknown}; std::vector input_indices_; std::vector output_indices_; + + bool support_parallel_{false}; }; } // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/op_coder_register.h b/mindspore/lite/micro/coder/opcoders/op_coder_register.h index 08942c2786..4e22ffdb31 100644 --- a/mindspore/lite/micro/coder/opcoders/op_coder_register.h +++ b/mindspore/lite/micro/coder/opcoders/op_coder_register.h @@ -33,8 +33,7 @@ class CoderKey { public: CoderKey() = delete; - CoderKey(Target target, TypeId data_type, schema::PrimitiveType op_type) - : target_(target), data_type_(data_type), op_type_(op_type) {} + CoderKey(Target target, TypeId data_type, int op_type) : target_(target), data_type_(data_type), op_type_(op_type) {} CoderKey AllKey() const { CoderKey key(kAllTargets, data_type_, op_type_); @@ -48,7 +47,7 @@ class CoderKey { private: Target target_ = kTargetUnknown; TypeId data_type_ = kTypeUnknown; - schema::PrimitiveType op_type_ = schema::PrimitiveType_NONE; + int op_type_ = schema::PrimitiveType_NONE; }; class OpCoderFactory { diff --git a/mindspore/lite/micro/coder/opcoders/parallel.cc b/mindspore/lite/micro/coder/opcoders/parallel.cc new file mode 100644 index 0000000000..fbd6bb8619 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/parallel.cc @@ -0,0 +1,33 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "coder/opcoders/parallel.h" + +namespace mindspore::lite::micro { + +// ParallelLaunch is defined in thread_pool +const char *kParallelLaunch = "ParallelLaunch"; + +// g_thread_pool and g_thread_num are global variable, +// assign g_thread_pool by CreateThreadPool, +// and g_thread_num is equal to GetCurrentThreadNum +const char *gThreadNum = "g_thread_num"; +const char *gThreadPool = "g_thread_pool"; + +// args represents the parameters required for operator to run +const char *kRunArgs = "args"; +const char *kRunArgsAddr = "&args"; + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/parallel.h b/mindspore/lite/micro/coder/opcoders/parallel.h new file mode 100644 index 0000000000..0813607848 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/parallel.h @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPCODERS_PARALLEL_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPCODERS_PARALLEL_H_ + +namespace mindspore::lite::micro { + +constexpr int kDefaultTaskId = 0; + +constexpr int kMaxThreadNumSupported = 4; + +// ParallelLaunch is defined in thread_pool +extern const char *kParallelLaunch; + +// g_thread_pool and g_thread_num are global variable, +// assign g_thread_pool by CreateThreadPool, +// and g_thread_num is equal to GetCurrentThreadNum +extern const char *gThreadNum; +extern const char *gThreadPool; + +// args represents the parameters required for operator to run +extern const char *kRunArgs; +extern const char *kRunArgsAddr; + +} // namespace mindspore::lite::micro + +#endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_PARALLEL_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc index e03c7e355a..35378625e5 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.cc @@ -14,9 +14,9 @@ * limitations under the License. */ -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h" #include "src/common/log_adapter.h" -#include "micro/coder/log.h" +#include "coder/log.h" #include "nnacl/pooling_parameter.h" namespace mindspore::lite::micro::nnacl { @@ -56,7 +56,7 @@ void NNaclFp32Serializer::CodeStruct(const std::string &name, const SoftmaxParam } void NNaclFp32Serializer::CodeStruct(const std::string &name, const ConvParameter &conv_parameter) { - CodeBaseStruct("ConvParameter", name, conv_parameter.op_parameter_, "{NULL}", conv_parameter.kernel_h_, + CodeBaseStruct("ConvParameter", name, conv_parameter.op_parameter_, "{}", conv_parameter.kernel_h_, conv_parameter.kernel_w_, conv_parameter.stride_h_, conv_parameter.stride_w_, conv_parameter.dilation_h_, conv_parameter.dilation_w_, conv_parameter.pad_u_, conv_parameter.pad_d_, conv_parameter.pad_l_, conv_parameter.pad_r_, conv_parameter.group_, conv_parameter.tile_num_, @@ -101,4 +101,14 @@ void NNaclFp32Serializer::CodeStruct(const std::string &name, const TransposePar transpose_parameter.data_size_); } +void NNaclFp32Serializer::CodeStruct(const std::string &name, const DeQuantArg &de_quant_arg) { + // this clusters is meaningless which will be supported in future + CodeBaseStruct("DeQuantArg", name, de_quant_arg.scale, de_quant_arg.zeroPoint, de_quant_arg.var_corr, + de_quant_arg.mean_corr, "NULL", de_quant_arg.clusters_nums, de_quant_arg.bitNum); +} +void NNaclFp32Serializer::CodeStruct(const std::string &name, const SpliceParameter &splice_parameter) { + CodeArray("splice_context", splice_parameter.context_, splice_parameter.context_dim_, false); + CodeBaseStruct("SpliceParameter", name, splice_parameter.op_parameter_, splice_parameter.context_dim_, + splice_parameter.forward_indexes_dim_, "splice_context", nullptr, splice_parameter.output_dim_); +} } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h index 7128853050..edc6f28ff2 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_fp32_serializer.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_FP32_SERIALIZER_H_ #include #include -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/serializers/serializer.h" #include "nnacl/batchnorm_parameter.h" #include "nnacl/fp32/arithmetic_fp32.h" #include "nnacl/conv_parameter.h" @@ -29,12 +29,14 @@ #include "nnacl/fp32/transpose_fp32.h" #include "nnacl/pooling_parameter.h" #include "nnacl/softmax_parameter.h" +#include "nnacl/splice_parameter.h" +#include "wrapper/fp32/dequant_int8_to_fp32_wrapper.h" namespace mindspore::lite::micro::nnacl { class NNaclFp32Serializer : public Serializer { public: NNaclFp32Serializer() = default; - ~NNaclFp32Serializer() = default; + ~NNaclFp32Serializer() override = default; void CodeStruct(const std::string &name, const PoolingParameter &pooling_parameter); void CodeStruct(const std::string &name, const SoftmaxParameter &softmax_parameter); void CodeStruct(const std::string &name, const BatchNormParameter &batch_norm_parameter); @@ -45,6 +47,8 @@ class NNaclFp32Serializer : public Serializer { void CodeStruct(const std::string &name, const SliceParameter &slice_parameter); void CodeStruct(const std::string &name, const TileParameter &tile_parameter); void CodeStruct(const std::string &name, const TransposeParameter &transpose_parameter); + void CodeStruct(const std::string &name, const DeQuantArg &de_quant_arg); + void CodeStruct(const std::string &name, const SpliceParameter &splice_parameter); }; } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc index fc0e3093fc..3836781337 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.cc @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h" #include #include "src/common/log_adapter.h" -#include "micro/coder/log.h" +#include "coder/opcoders/parallel.h" +#include "coder/log.h" namespace mindspore::lite::micro::nnacl { @@ -49,15 +50,15 @@ void NNaclInt8Serializer::CodeStruct(const std::string &name, const ConvParamete quant_arg_w, quant_arg_out, real_multiplier, left_shift, right_shift, quant_multiplier, out_act_min, out_act_max, quant_arg.input_arg_num_, quant_arg.filter_arg_num_, quant_arg.output_arg_num_, quant_arg.per_channel_); - - CodeBaseStruct( - "ConvParameter", name, conv_parameter.op_parameter_, conv_quant_arg, conv_parameter.kernel_h_, - conv_parameter.kernel_w_, conv_parameter.stride_h_, conv_parameter.stride_w_, conv_parameter.dilation_h_, - conv_parameter.dilation_w_, conv_parameter.pad_u_, conv_parameter.pad_d_, conv_parameter.pad_l_, - conv_parameter.pad_r_, conv_parameter.group_, conv_parameter.tile_num_, conv_parameter.input_batch_, - conv_parameter.input_h_, conv_parameter.input_w_, conv_parameter.input_channel_, conv_parameter.output_batch_, - conv_parameter.output_h_, conv_parameter.output_w_, conv_parameter.output_channel_, conv_parameter.thread_num_, - conv_parameter.input_unit_, conv_parameter.output_unit_, conv_parameter.pad_mode_, conv_parameter.act_type_); + code << "int thread_num = MSMIN(" << gThreadNum << ", " << conv_parameter.output_h_ << ");\n"; + CodeBaseStruct("ConvParameter", name, conv_parameter.op_parameter_, conv_quant_arg, conv_parameter.kernel_h_, + conv_parameter.kernel_w_, conv_parameter.stride_h_, conv_parameter.stride_w_, + conv_parameter.dilation_h_, conv_parameter.dilation_w_, conv_parameter.pad_u_, conv_parameter.pad_d_, + conv_parameter.pad_l_, conv_parameter.pad_r_, conv_parameter.group_, conv_parameter.tile_num_, + conv_parameter.input_batch_, conv_parameter.input_h_, conv_parameter.input_w_, + conv_parameter.input_channel_, conv_parameter.output_batch_, conv_parameter.output_h_, + conv_parameter.output_w_, conv_parameter.output_channel_, "thread_num", conv_parameter.input_unit_, + conv_parameter.output_unit_, conv_parameter.pad_mode_, conv_parameter.act_type_); } void NNaclInt8Serializer::CodeStruct(const std::string &name, const MatMulParameter &matmul_parameter) { @@ -107,14 +108,14 @@ void NNaclInt8Serializer::CodeStruct(const std::string &name, const PoolingParam << " &" << out_quant_name << "};\n"; CodeBaseStruct("PoolingParameter", name, pooling_parameter.op_parameter_, pooling_parameter.pool_mode_, - pooling_parameter.round_mode_, pooling_parameter.act_type_, pooling_parameter.avg_mode_, - pooling_parameter.global_, pooling_parameter.window_w_, pooling_parameter.window_h_, - pooling_parameter.stride_w_, pooling_parameter.stride_h_, pooling_parameter.input_w_, - pooling_parameter.input_h_, pooling_parameter.input_batch_, pooling_parameter.input_channel_, - pooling_parameter.output_w_, pooling_parameter.output_h_, pooling_parameter.output_batch_, - pooling_parameter.output_channel_, pooling_parameter.pad_u_, pooling_parameter.pad_d_, - pooling_parameter.pad_l_, pooling_parameter.pad_r_, pooling_parameter.op_parameter_.thread_num_, - quant_name, pooling_parameter.quantize_); + pooling_parameter.round_mode_, pooling_parameter.pad_mode_, pooling_parameter.act_type_, + pooling_parameter.avg_mode_, pooling_parameter.global_, pooling_parameter.window_w_, + pooling_parameter.window_h_, pooling_parameter.stride_w_, pooling_parameter.stride_h_, + pooling_parameter.input_w_, pooling_parameter.input_h_, pooling_parameter.input_batch_, + pooling_parameter.input_channel_, pooling_parameter.output_w_, pooling_parameter.output_h_, + pooling_parameter.output_batch_, pooling_parameter.output_channel_, pooling_parameter.pad_u_, + pooling_parameter.pad_d_, pooling_parameter.pad_l_, pooling_parameter.pad_r_, + pooling_parameter.op_parameter_.thread_num_, quant_name, pooling_parameter.quantize_); } void NNaclInt8Serializer::CodeStruct(const std::string &name, const SoftmaxParameter &softmax_parameter) { @@ -122,6 +123,18 @@ void NNaclInt8Serializer::CodeStruct(const std::string &name, const SoftmaxParam ToString(softmax_parameter.input_shape_), softmax_parameter.element_size_, softmax_parameter.n_dim_); } +void NNaclInt8Serializer::CodeStruct(const std::string &name, const SliceParameter &slice_parameter) { + CodeBaseStruct("SliceParameter", name, slice_parameter.op_parameter_, ToString(slice_parameter.shape_), + ToString(slice_parameter.begin_), ToString(slice_parameter.end_), ToString(slice_parameter.size_), + slice_parameter.quant_arg_, slice_parameter.param_length_); +} + +void NNaclInt8Serializer::CodeStruct(const std::string &name, const BatchNormParameter &batchnorm_parameter) { + CodeBaseStruct("BatchNormParameter", name, batchnorm_parameter.op_parameter_, batchnorm_parameter.epsilon_, + batchnorm_parameter.momentum_, batchnorm_parameter.unit_, batchnorm_parameter.units_, + batchnorm_parameter.channel_, batchnorm_parameter.fused_); +} + void NNaclInt8Serializer::CodeStruct(const std::string &name, const SoftmaxQuantArg &softmax_quant_parameter) { CodeBaseStruct("SoftmaxQuantArg", name, softmax_quant_parameter.in_quant_args_, softmax_quant_parameter.out_quant_arg_, softmax_quant_parameter.output_activation_min_, @@ -160,6 +173,14 @@ void NNaclInt8Serializer::CodeStruct(const std::string &name, const ConcatParame concat_parameter.after_axis_size, concat_parameter.count_unit_); } +void NNaclInt8Serializer::CodeStruct(const std::string &name, const ::QuantArg &quant_arg) { + CodeBaseStruct("QuantArg", name, quant_arg.scale_, quant_arg.zp_); +} + +void NNaclInt8Serializer::CodeStruct(const std::string &name, const ::QuantMulArg &quant_mul_arg) { + CodeBaseStruct("QuantMulArg", name, quant_mul_arg.multiplier_, quant_mul_arg.left_shift_, quant_mul_arg.right_shift_); +} + void NNaclInt8Serializer::CodeStruct(const std::string &name, const ReduceQuantArg &reduce_quant_arg) { CodeBaseStruct( "ReduceQuantArg", name, reduce_quant_arg.in_scale_, reduce_quant_arg.in_zp_, reduce_quant_arg.out_scale_, @@ -180,4 +201,25 @@ void NNaclInt8Serializer::CodeStruct(const std::string &name, const MatmulQuantA matmul_quant_arg.right_shift, matmul_quant_arg.quant_multiplier); } +void NNaclInt8Serializer::CodeStruct(const std::string &name, const SubQuantArg &sub_quant_arg) { + CodeBaseStruct("SubQuantArg", name, sub_quant_arg.in0_args_, sub_quant_arg.in1_args_, sub_quant_arg.out_args_, + sub_quant_arg.output_activation_min_, sub_quant_arg.output_activation_max_, + sub_quant_arg.input0_multiplier_, sub_quant_arg.input1_multiplier_, sub_quant_arg.output_multiplier_, + sub_quant_arg.input0_shift_, sub_quant_arg.input1_shift_, sub_quant_arg.output_shift_, + sub_quant_arg.left_shift_result0_, sub_quant_arg.left_shift_result1_, sub_quant_arg.right_shift0_, + sub_quant_arg.right_shift1_, sub_quant_arg.left_shift_out_, sub_quant_arg.right_shift_out_); +} + +void NNaclInt8Serializer::CodeStruct(const std::string &name, const DivQuantArg &div_quant_arg) { + CodeBaseStruct("DivQuantArg", name, div_quant_arg.in0_args_, div_quant_arg.in1_args_, div_quant_arg.out_args_, + div_quant_arg.output_activation_min_, div_quant_arg.output_activation_max_, + div_quant_arg.output_multiplier_, div_quant_arg.output_shift_); +} + +void NNaclInt8Serializer::CodeStruct(const std::string &name, const ReluXQuantArg &relu_quant_arg) { + CodeBaseStruct("ReluXQuantArg", name, relu_quant_arg.input_arg, relu_quant_arg.output_arg, + relu_quant_arg.input_multiplier_, relu_quant_arg.left_shift_, relu_quant_arg.right_shift_, + relu_quant_arg.quantized_output_min, relu_quant_arg.quantized_output_max); +} + } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h index a3c0550edb..daffc55380 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_int8_serializer.h @@ -19,7 +19,8 @@ #include #include "nnacl/pooling_parameter.h" #include "nnacl/softmax_parameter.h" -#include "micro/coder/opcoders/serializers/serializer.h" +#include "coder/opcoders/serializers/serializer.h" +#include "nnacl/op_base.h" #include "nnacl/int8/add_int8.h" #include "nnacl/int8/arithmetic_int8.h" #include "nnacl/conv_parameter.h" @@ -27,6 +28,9 @@ #include "nnacl/int8/concat_int8.h" #include "nnacl/int8/quantize.h" #include "nnacl/reshape_parameter.h" +#include "nnacl/slice_parameter.h" +#include "nnacl/batchnorm_parameter.h" +#include "nnacl/int8/relux_int8.h" namespace mindspore::lite::micro::nnacl { @@ -40,12 +44,19 @@ class NNaclInt8Serializer : public Serializer { void CodeStruct(const std::string &name, const ArithmeticParameter &arithmetic_parameter); void CodeStruct(const std::string &name, const PoolingParameter &pooling_parameter); void CodeStruct(const std::string &name, const SoftmaxParameter &softmax_parameter); + void CodeStruct(const std::string &name, const SliceParameter &slice_parameter); + void CodeStruct(const std::string &name, const BatchNormParameter &batchnorm_parameter); void CodeStruct(const std::string &name, const SoftmaxQuantArg &softmax_quant_parameter); void CodeStruct(const std::string &name, const ConcatParameter &concat_parameter, int input_tensors, int in_shape, int out_shape); + void CodeStruct(const std::string &name, const ::QuantArg &quant_arg); + void CodeStruct(const std::string &name, const ::QuantMulArg &quant_mul_arg); void CodeStruct(const std::string &name, const ReduceQuantArg &reduce_quant_arg); void CodeStruct(const std::string &name, const ReshapeQuantArg &reshape_quant_arg); void CodeStruct(const std::string &name, const MatmulQuantArg &matmul_quant_arg); + void CodeStruct(const std::string &name, const SubQuantArg &sub_quant_arg); + void CodeStruct(const std::string &name, const DivQuantArg &div_quant_arg); + void CodeStruct(const std::string &name, const ReluXQuantArg &relu_quant_arg); }; } // namespace mindspore::lite::micro::nnacl diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.cc b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.cc new file mode 100644 index 0000000000..9e80249759 --- /dev/null +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.cc @@ -0,0 +1,90 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 +#include +#include "nnacl/pooling_parameter.h" +#include "nnacl/slice_parameter.h" +#include "nnacl/softmax_parameter.h" +#include "nnacl/int8/add_int8.h" +#include "nnacl/int8/quantize.h" +#include "coder/opcoders/parallel.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h" + +namespace mindspore::lite::micro { + +std::ostream &operator<<(std::ostream &code, const ::QuantArg &quant_arg) { + code << "{" << static_cast(quant_arg.scale_) << ", " << quant_arg.zp_ << "}"; + return code; +} + +std::ostream &operator<<(std::ostream &code, const OpParameter &tile) { + code << "{ \"\"" + << ", " << tile.type_ << ", " << gThreadNum << "}"; + return code; +} + +std::ostream &operator<<(std::ostream &code, const AddQuantQrgs &args) { + code << "{" << args.zp_ << ", " << args.left_shift_ << ", " << args.right_shift_ << ", " << args.multiplier_ << "}"; + return code; +} + +std::ostream &operator<<(std::ostream &code, const SliceQuantArg &arg) { + code << "{" << arg.in_args_ << ", " << arg.out_args_ << ", " << arg.output_activation_min_ << ", " + << arg.output_activation_max_ << "}"; + return code; +} + +std::ostream &operator<<(std::ostream &code, PoolMode pool_mode) { + code << "(PoolMode)" + << "(" << static_cast(pool_mode) << ")"; + return code; +} + +std::ostream &operator<<(std::ostream &code, RoundMode round_mode) { + code << "(RoundMode)" + << "(" << static_cast(round_mode) << ")"; + return code; +} + +std::ostream &operator<<(std::ostream &code, RoundingMode rounding_mode) { + code << "(RoundingMode)" + << "(" << static_cast(rounding_mode) << ")"; + return code; +} + +std::ostream &operator<<(std::ostream &code, PadMode pad_mode) { + code << "(PadMode)" + << "(" << static_cast(pad_mode) << ")"; + return code; +} + +std::ostream &operator<<(std::ostream &code, ActType act_type) { + code << "(ActType)" + << "(" << static_cast(act_type) << ")"; + return code; +} + +std::ostream &operator<<(std::ostream &code, DataOrder data_order) { + if (data_order == RowMajor) { + code << "RowMajor"; + } else { + code << "ColMajor"; + } + return code; +} + +} // namespace mindspore::lite::micro diff --git a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h index 3d59511609..6f6f1559dd 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h @@ -19,65 +19,31 @@ #include #include #include "nnacl/pooling_parameter.h" +#include "nnacl/slice_parameter.h" #include "nnacl/softmax_parameter.h" #include "nnacl/int8/add_int8.h" #include "nnacl/int8/quantize.h" namespace mindspore::lite::micro { -inline std::ostream &operator<<(std::ostream &code, const ::QuantArg &quant_arg) { - code << "{" << static_cast(quant_arg.scale_) << ", " << quant_arg.zp_ << "}"; - return code; -} +std::ostream &operator<<(std::ostream &code, const ::QuantArg &quant_arg); -inline std::ostream &operator<<(std::ostream &code, const OpParameter &tile) { - code << "{ \"\"" - << ", " << tile.type_ << ", " << tile.thread_num_ << "}"; - return code; -} +std::ostream &operator<<(std::ostream &code, const OpParameter &tile); -inline std::ostream &operator<<(std::ostream &code, const AddQuantQrgs &args) { - code << "{" << args.zp_ << ", " << args.left_shift_ << ", " << args.right_shift_ << ", " << args.multiplier_ << "}"; - return code; -} +std::ostream &operator<<(std::ostream &code, const AddQuantQrgs &args); -inline std::ostream &operator<<(std::ostream &code, PoolMode pool_mode) { - code << "(PoolMode)" - << "(" << static_cast(pool_mode) << ")"; - return code; -} +std::ostream &operator<<(std::ostream &code, const SliceQuantArg &arg); -inline std::ostream &operator<<(std::ostream &code, RoundMode round_mode) { - code << "(RoundMode)" - << "(" << static_cast(round_mode) << ")"; - return code; -} +std::ostream &operator<<(std::ostream &code, PoolMode pool_mode); -inline std::ostream &operator<<(std::ostream &code, RoundingMode rounding_mode) { - code << "(RoundingMode)" - << "(" << static_cast(rounding_mode) << ")"; - return code; -} +std::ostream &operator<<(std::ostream &code, RoundMode round_mode); -inline std::ostream &operator<<(std::ostream &code, PadMode pad_mode) { - code << "(PadMode)" - << "(" << static_cast(pad_mode) << ")"; - return code; -} +std::ostream &operator<<(std::ostream &code, RoundingMode rounding_mode); -inline std::ostream &operator<<(std::ostream &code, ActType act_type) { - code << "(ActType)" - << "(" << static_cast(act_type) << ")"; - return code; -} +std::ostream &operator<<(std::ostream &code, PadMode pad_mode); -inline std::ostream &operator<<(std::ostream &code, DataOrder data_order) { - if (data_order == RowMajor) { - code << "RowMajor"; - } else { - code << "ColMajor"; - } - return code; -} +std::ostream &operator<<(std::ostream &code, ActType act_type); + +std::ostream &operator<<(std::ostream &code, DataOrder data_order); } // namespace mindspore::lite::micro #endif // MINDSPORE_LITE_MICRO_CODER_OPCODERS_SERIALIZERS_NNACL_STREAM_UTILS_H_ diff --git a/mindspore/lite/micro/coder/opcoders/serializers/serializer.h b/mindspore/lite/micro/coder/opcoders/serializers/serializer.h index dd844da5f3..6a5401c1d2 100644 --- a/mindspore/lite/micro/coder/opcoders/serializers/serializer.h +++ b/mindspore/lite/micro/coder/opcoders/serializers/serializer.h @@ -20,8 +20,8 @@ #include #include #include -#include "micro/coder/allocator/allocator.h" -#include "micro/coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h" +#include "coder/allocator/allocator.h" +#include "coder/opcoders/serializers/nnacl_serializer/nnacl_stream_utils.h" namespace mindspore::lite::micro { @@ -121,6 +121,10 @@ class Serializer { template void CodeMallocExpression(T t, size_t size) { + if (size == 0) { + MS_LOG(ERROR) << "CodeMallocExpression size is zero"; + exit(1); + } GenCode(t); code << " = malloc(" << size << ");\n"; code << "if ("; diff --git a/mindspore/lite/micro/coder/operator_library/CMakeLists.txt b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt new file mode 100644 index 0000000000..c86d5f7fd3 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/CMakeLists.txt @@ -0,0 +1,49 @@ +option(MICRO_CMSIS_X86 "build for CMSIS x86" ON) +option(ENABLE_ASAN "enable asan" OFF) +option(CMAKE_BUILD_TYPE "debug or release" Debug) + +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + set(DEBUG_MODE "Debug") +endif() + +set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}") +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + set(CMAKE_C_FLAGS "-DDebug -g -fvisibility=default ${CMAKE_C_FLAGS}") +else() + set(CMAKE_C_FLAGS "-fPIC -fPIE -Werror -O3 -fstack-protector-strong -fomit-frame-pointer ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS_Release "${CMAKE_C_FLAGS_Release} -O3 -ffunction-sections -fdata-sections") + string(REPLACE "-g" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") +endif() + +if(ENABLE_ASAN) + set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} ${OPTION_CXX_FLAGS} -lasan") +endif() + +set(MICRO_CMAKE_PATH ${MICRO_DIR}/cmake) +set(OPERATOR_LIBRARY_PATH ${CMAKE_BINARY_DIR}/operator_library) +set(HEADER_PATH "${OPERATOR_LIBRARY_PATH}/include") +set(LIB_PATH "${OPERATOR_LIBRARY_PATH}/lib/x86") + +message("===========>start to pack operators' head file") +file(REMOVE_RECURSE ${OPERATOR_LIBRARY_PATH}) +file(MAKE_DIRECTORY ${OPERATOR_LIBRARY_PATH}) +file(INSTALL ${LITE_DIR}/nnacl DESTINATION ${HEADER_PATH} FILES_MATCHING PATTERN "*.h") +file(INSTALL ${MICRO_DIR}/coder/operator_library/wrapper DESTINATION ${HEADER_PATH} FILES_MATCHING PATTERN "*.h") +file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/Core/Include DESTINATION ${HEADER_PATH}/CMSIS/Core) +file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/DSP/Include DESTINATION ${HEADER_PATH}/CMSIS/DSP) +file(INSTALL ${CMAKE_BINARY_DIR}/cmsis/CMSIS/NN/Include DESTINATION ${HEADER_PATH}/CMSIS/NN) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/assembly) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp16) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp16_grad) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/fp32_grad) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/intrinsics) +file(REMOVE_RECURSE ${HEADER_PATH}/nnacl/optimize) + +include(${MICRO_CMAKE_PATH}/package_android.cmake) +include(${MICRO_CMAKE_PATH}/package_nnacl.cmake) +include(${MICRO_CMAKE_PATH}/package_cmsis.cmake) +include(${MICRO_CMAKE_PATH}/package_wrapper.cmake) + +# generate static library +add_library(ops STATIC ${NNACL_OPS} ${CMSIS_OPS} ${WRAPPER_SRC} ${RUNTIME_SRC}) +install(TARGETS ops ARCHIVE DESTINATION ${LIB_PATH}) diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.c new file mode 100644 index 0000000000..1b77b1c8df --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.c @@ -0,0 +1,71 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/base/detection_post_process_base_wrapper.h" +#include + +static inline void swap_index(int *arr, int lhs, int rhs) { + int temp = arr[lhs]; + arr[lhs] = arr[rhs]; + arr[rhs] = temp; +} + +static inline bool compare(int i, int j, const float *scores) { + if (scores[i] == scores[j]) { + return i < j; + } + return scores[i] > scores[j]; +} + +static void heapify(const float *scores, int *indexes, int n, int i) { + while (i < n) { + int cur = i; + int l = 2 * i + 1; + int r = 2 * i + 2; + if (r < n && compare(indexes[cur], indexes[r], scores)) { + cur = r; + } + if (l < n && compare(indexes[cur], indexes[l], scores)) { + cur = l; + } + if (cur != i) { + swap_index(indexes, i, cur); + i = cur; + } else { + break; + } + } +} + +void PartialArgSort(const float *scores, int *indexes, int num_to_sort, int num_values) { + // make heap + int start_index = num_to_sort / 2 - 1; + for (int i = start_index; i >= 0; i--) { + heapify(scores, indexes, num_to_sort, i); + } + // compare the rest elements with heap top + for (int i = num_to_sort; i < num_values; ++i) { + if (!compare(indexes[0], indexes[i], scores)) { + swap_index(indexes, i, 0); + heapify(scores, indexes, num_to_sort, 0); + } + } + // heap sort + for (int cur_length = num_to_sort - 1; cur_length > 0; cur_length--) { + swap_index(indexes, 0, cur_length); + heapify(scores, indexes, cur_length, 0); + } +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.h new file mode 100644 index 0000000000..badec6c043 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/base/detection_post_process_base_wrapper.h @@ -0,0 +1,24 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_BASE_DETECTION_POST_PROCESS_BASE_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_BASE_DETECTION_POST_PROCESS_BASE_WRAPPER_H_ + +#include + +void PartialArgSort(const float *scores, int *indexes, int num_to_sort, int num_values); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_BASE_DETECTION_POST_PROCESS_BASE_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.c new file mode 100644 index 0000000000..e9c6a01f54 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.c @@ -0,0 +1,69 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/fp32/dequant_int8_to_fp32_wrapper.h" +#include +#include +void DequantDataPerChannel(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, + size_t per_batch_size, float *de_quant_dst) { + size_t matrix_size = de_quant_nums / per_batch_size; + for (int i = 0; i < per_batch_size; i++) { + const DeQuantArg *de_quant_arg = de_quant_args[i]; + float scale = de_quant_arg->scale; + int32_t zero_point = de_quant_arg->zeroPoint; + for (int j = 0; j < matrix_size; j++) { + de_quant_dst[i * matrix_size + j] = (quant_src[i * matrix_size + j] - zero_point) * scale; + } + } +} + +void DequantData(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, size_t channels, + float *de_quant_dst) { + size_t per_channel_size = de_quant_nums / channels; + for (size_t i = 0; i < channels; i++) { + const DeQuantArg *de_quant_arg = de_quant_args[i]; + float scale = de_quant_arg->scale; + int32_t zero_point = de_quant_arg->zeroPoint; + float var_corr = de_quant_arg->var_corr; + float mean_corr = de_quant_arg->mean_corr; + if (var_corr < 0 || var_corr > 10) { + var_corr = 1; + } + for (size_t j = 0; j < per_channel_size; j++) { + float dequant_data = (quant_src[per_channel_size * i + j] - zero_point) * scale; + de_quant_dst[per_channel_size * i + j] = dequant_data * var_corr + mean_corr; + } + } +} + +void DequantDataPerTensor(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, + float *de_quant_dst) { + const DeQuantArg *de_quant_arg = de_quant_args[0]; + float *quant_clusters = de_quant_arg->clusters; + float scale = de_quant_arg->scale; + int32_t zero_point = de_quant_arg->zeroPoint; + for (int j = 0; j < de_quant_nums; j++) { + int8_t quant_data = quant_src[j]; + if (quant_clusters) { + if (quant_data > INT8_MAX || quant_data < INT8_MIN) { + return; + } + de_quant_dst[j] = quant_clusters[quant_data - INT8_MIN]; + } else { + de_quant_dst[j] = (quant_data - zero_point) * scale; + } + } +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.h new file mode 100644 index 0000000000..ad581e0537 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/dequant_int8_to_fp32_wrapper.h @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_DEQUANT_TO_INT8_FP32_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_DEQUANT_TO_INT8_FP32_WRAPPER_H_ +#include +#include +typedef struct DeQuantArg { + float scale; + int32_t zeroPoint; + float var_corr; + float mean_corr; + float *clusters; + int clusters_nums; + int bitNum; +} DeQuantArg; + +#ifdef __cplusplus +extern "C" { +#endif + +void DequantDataPerChannel(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, + size_t per_batch_size, float *de_quant_dst); + +void DequantData(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, size_t channels, + float *de_quant_dst); + +void DequantDataPerTensor(const int8_t *quant_src, const DeQuantArg **de_quant_args, size_t de_quant_nums, + float *de_quant_dst); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_DEQUANT_TO_INT8_FP32_WRAPPER_H_ diff --git a/mindspore/lite/micro/wrapper/fp32/matmul_fp32_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/matmul_fp32_wrapper.c similarity index 100% rename from mindspore/lite/micro/wrapper/fp32/matmul_fp32_wrapper.c rename to mindspore/lite/micro/coder/operator_library/wrapper/fp32/matmul_fp32_wrapper.c diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/fp32/matmul_fp32_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/matmul_fp32_wrapper.h new file mode 100644 index 0000000000..89fba5e3a4 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/fp32/matmul_fp32_wrapper.h @@ -0,0 +1,33 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_MATMUL_FP32_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_MATMUL_FP32_WRAPPER_H_ +#include +#include "nnacl/fp32/matmul_fp32.h" +#ifdef __cplusplus +extern "C" { +#endif + +void InitMatrixA(const float *src_ptr, float *dst_ptr, const MatMulParameter *params_, bool is_vector_a); + +void InitMatrixB(const float *src_ptr, float *dst_ptr, const MatMulParameter *params_, bool is_vector_a); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_FP32_MATMUL_FP32_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.c new file mode 100644 index 0000000000..c5653e5a39 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.c @@ -0,0 +1,69 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/add_int8_wrapper.h" +#include "nnacl/errorcode.h" + +int AddBroadcastInt8Run(void *cdata, int task_id) { + AddInt8Args *args = (AddInt8Args *)(cdata); + int stride = UP_DIV(args->out_size_, args->thread_count_); + int real_out_count = MSMIN(stride, args->out_size_ - stride * task_id); + if (real_out_count <= 0) { + return NNACL_OK; + } + int8_t *cur_in0 = NULL; + int8_t *cur_in1 = NULL; + int8_t *cur_out = NULL; + for (int i = 0; i < real_out_count; i++) { + if (args->arith_para_->in_elements_num0_ == args->arith_para_->out_elements_num_) { + cur_in0 = args->input0_data_ + task_id * stride * args->in_size_ + i * args->in_size_; + cur_in1 = args->input1_data_; + cur_out = args->output_data_ + task_id * stride * args->in_size_ + i * args->in_size_; + } else { + cur_in0 = args->input0_data_; + cur_in1 = args->input1_data_ + task_id * stride * args->in_size_ + i * args->in_size_; + cur_out = args->output_data_ + task_id * stride * args->in_size_ + i * args->in_size_; + } + AddInt8(cur_in0, cur_in1, cur_out, args->in_size_, args->para_); + } + return NNACL_OK; +} + +int AddInt8Run(void *cdata, int task_id) { + AddInt8Args *args = (AddInt8Args *)(cdata); + /* no need broadcast */ + int stride = UP_DIV(args->elements_num_, args->thread_count_); + int rest_count = args->elements_num_ - task_id * stride; + int real_count = MSMIN(stride, rest_count); + if (real_count <= 0) { + return NNACL_OK; + } + int8_t *cur_in0 = args->input0_data_ + stride * task_id; + int8_t *cur_in1 = args->input1_data_ + stride * task_id; + int8_t *cur_out = args->output_data_ + stride * task_id; + if (args->support_opt_add_) { + int8_t *ptr_in = args->arith_para_->in_elements_num0_ == 1 ? cur_in1 : cur_in0; + int8_t element_in = args->arith_para_->in_elements_num0_ == 1 ? args->input0_data_[0] : args->input1_data_[0]; + AddQuantQrgs *ptr_args = + args->arith_para_->in_elements_num0_ == 1 ? &args->para_->in1_args_ : &args->para_->in0_args_; + AddQuantQrgs *ele_args = + args->arith_para_->in_elements_num0_ == 1 ? &args->para_->in0_args_ : &args->para_->in1_args_; + AddOptInt8(ptr_in, element_in, cur_out, rest_count, args->para_, ptr_args, ele_args); + } else { + AddInt8(cur_in0, cur_in1, cur_out, rest_count, args->para_); + } + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.h new file mode 100644 index 0000000000..791ee2cf57 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/add_int8_wrapper.h @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ +#include +#include "nnacl/int8/matmul_int8.h" +#include "nnacl/int8/add_int8.h" +#include "nnacl/arithmetic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + AddQuantParameter *para_; + ArithmeticParameter *arith_para_; + int in_size_; + int out_size_; + int thread_count_; + int elements_num_; + bool support_opt_add_; + int8_t *input0_data_; + int8_t *input1_data_; + int8_t *output_data_; +} AddInt8Args; + +int AddBroadcastInt8Run(void *cdata, int task_id); + +int AddInt8Run(void *cdata, int task_id); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.c new file mode 100644 index 0000000000..93048138b6 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.c @@ -0,0 +1,25 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/batchnorm_int8_wrapper.h" +#include "nnacl/int8/batchnorm_int8.h" +#include "nnacl/errorcode.h" + +int BatchNormInt8Run(void *cdata, int task_id) { + BatchNormArgs *args = (BatchNormArgs *)(cdata); + BatchNormInt8(args->out_addr_, args->in_addr_, args->alpha_addr_, args->beta_addr_, task_id, args->batchnorm_param_); + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.h new file mode 100644 index 0000000000..a3b3c87637 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/batchnorm_int8_wrapper.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_BATCHNORM_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_BATCHNORM_INT8_WRAPPER_H_ + +#include +#include "nnacl/batchnorm_parameter.h" +typedef struct BatchNormArgs { + int8_t *in_addr_; + int8_t *out_addr_; + float *alpha_addr_; + float *beta_addr_; + BatchNormParameter *batchnorm_param_; +} BatchNormArgs; + +int BatchNormInt8Run(void *cdata, int task_id); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_BATCHNORM_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.c new file mode 100644 index 0000000000..b81643113e --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.c @@ -0,0 +1,27 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/concat_int8_wrapper.h" + +int ConcatInt8Run(void *cdata, int task_id) { + ConcatInt8Args *args = (ConcatInt8Args *)cdata; + int64_t real_dst_count = MSMIN(args->before_axis_size_ - task_id * args->count_unit_, args->count_unit_); + if (real_dst_count <= 0) { + return NNACL_OK; + } + Int8Concat(args->inputs_, args->output_, args->para_, args->axis_, real_dst_count, task_id); + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.h new file mode 100644 index 0000000000..f7c8fc4e20 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/concat_int8_wrapper.h @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_INT8_CONCAT_WRAPPER_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_INT8_CONCAT_WRAPPER_INT8_WRAPPER_H_ + +#include "nnacl/errorcode.h" +#include "nnacl/concat_parameter.h" +#include "nnacl/int8/concat_int8.h" + +typedef struct { + int8_t **inputs_; + int8_t *output_; + ConcatParameter *para_; + int axis_; + int64_t before_axis_size_; + int64_t count_unit_; +} ConcatInt8Args; + +int ConcatInt8Run(void *cdata, int task_id); + +#endif // MINDSPORE_LITE_MICRO_INT8_CONCAT_WRAPPER_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.c new file mode 100644 index 0000000000..5f30d4bd10 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.c @@ -0,0 +1,90 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/conv1x1_init_int8_wrapper.h" +#include +#include "nnacl/int8/matmul_int8.h" +#include "nnacl/errorcode.h" + +int Conv1x1Init(int8_t *src_weight, int32_t *src_bias, int32_t *filter_zps, int32_t input_channel, + int32_t output_channel, int32_t input_zp, bool support_optimize, bool filter_peroc, + int8_t **packed_weight, int32_t **bias_data) { + if (packed_weight == NULL || bias_data == NULL) { + return NNACL_ERR; + } +#ifdef ENABLE_ARM32 + /* InitWeightBiasArm32 */ + /* weight */ + size_t size = UP_ROUND(input_channel, C16NUM) * UP_ROUND(output_channel, C2NUM) * sizeof(int8_t); + int8_t *packed_weight_ = (int8_t *)(malloc(size)); + if (packed_weight_ == NULL) { + return NNACL_ERR; + } + memset(packed_weight_, 0, size); + RowMajor2Row2x16MajorInt8(src_weight, packed_weight_, output_channel, input_channel); + /* bias */ + size = UP_ROUND(output_channel, C2NUM); + int32_t *bias_data_ = (int32_t *)malloc(size * sizeof(int32_t)); + if (bias_data_ == NULL) { + free(packed_weight_); + return NNACL_ERR; + } + memset(bias_data_, 0, size * sizeof(int32_t)); + if (src_bias != NULL) { + memcpy(bias_data_, src_bias, output_channel * sizeof(int32_t)); + } +#else + /* InitWeightBias */ + /* weight */ + size_t size = support_optimize ? UP_ROUND(input_channel, C4NUM) * UP_ROUND(output_channel, C16NUM) * sizeof(int8_t) + : UP_ROUND(input_channel, C16NUM) * UP_ROUND(output_channel, C4NUM) * sizeof(int8_t); + int8_t *packed_weight_ = (int8_t *)(malloc(size)); + if (packed_weight_ == NULL) { + return NNACL_ERR; + } + memset(packed_weight_, 0, size); + if (support_optimize) { + RowMajor2Row4x16MajorInt8(src_weight, packed_weight_, output_channel, input_channel); + } else { + RowMajor2Row16x4MajorInt8(src_weight, packed_weight_, output_channel, input_channel); + } + /* bias */ + size = support_optimize ? UP_ROUND(output_channel, C16NUM) : UP_ROUND(output_channel, C4NUM); + int32_t *bias_data_ = (int32_t *)malloc(size * sizeof(int32_t)); + if (bias_data_ == NULL) { + free(packed_weight_); + return NNACL_ERR; + } + memset(bias_data_, 0, size * sizeof(int32_t)); + if (src_bias != NULL) { + memcpy(bias_data_, src_bias, output_channel * sizeof(int32_t)); + } +#endif + /* InitBiasByzp */ + /* bias = bias - v2 x zp1 + zp1 x zp2 */ + for (int oc = 0; oc < output_channel; oc++) { + int32_t weight_sum_value = 0; + int32_t filter_zp = (filter_peroc) ? filter_zps[oc] : filter_zps[0]; + for (int ic = 0; ic < input_channel; ic++) { + weight_sum_value += src_weight[oc * input_channel + ic]; + } + bias_data_[oc] += filter_zp * input_zp * input_channel - weight_sum_value * input_zp; + } + + *packed_weight = packed_weight_; + *bias_data = bias_data_; + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.h new file mode 100644 index 0000000000..72380ba892 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_init_int8_wrapper.h @@ -0,0 +1,28 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_INIT_INT8_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_INIT_INT8_H_ + +#include +#include +#include "nnacl/conv_parameter.h" + +int Conv1x1Init(int8_t *src_weight, int32_t *src_bias, int32_t *filter_zps, int32_t input_channel, + int32_t output_channel, int32_t input_zp, bool support_optimize, bool filter_peroc, + int8_t **packed_weight, int32_t **bias_data); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_INIT_INT8_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.c new file mode 100644 index 0000000000..4560c4d49f --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.c @@ -0,0 +1,225 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/conv1x1_run_int8_wrapper.h" +#include "nnacl/base/conv1x1_base.h" +#include "nnacl/int8/matmul_int8.h" +#include "nnacl/int8/pack_int8.h" +#include "nnacl/int8/conv1x1_int8.h" +#include "nnacl/errorcode.h" + +void Pre1x1Trans(Conv1x1Args *args, int8_t *src_input, int8_t *src_output) { + args->output_ptr_ = src_output; + if (args->pre_trans_input_) { + Conv1x1InputPack(src_input, args->input_ptr_, args->conv_param_, sizeof(int8_t)); + } else { + args->input_ptr_ = src_input; + } +} + +int OcOptPre(void *cdata, int task_id) { + Conv1x1Args *args = (Conv1x1Args *)(cdata); + int cur_stride = args->thread_stride_hw_ * C4NUM; + int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; + int cur_hw = MSMIN(cur_stride, res_stride); + if (cur_hw <= 0) { + return NNACL_OK; + } + int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; + int8_t *hw_packed_in = args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_4_; + int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; + + if (args->filter_peroc_) { + PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, 1); + } else { + PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, + args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_); + } + return NNACL_OK; +} + +int RunArm64OptOc(void *cdata, int task_id) { + Conv1x1Args *args = (Conv1x1Args *)(cdata); + int stride = args->thread_stride_oc_ * C16NUM; + int cur_stride = task_id * stride; + int res_stride = args->matmul_param_->col_ - cur_stride; + int cur_oc = MSMIN(stride, res_stride); + if (cur_oc <= 0) { + return NNACL_OK; + } + + bool filter_peroc = args->filter_peroc_; + int32_t *cur_left_shift = + filter_peroc ? args->left_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.left_shift_; + int32_t *cur_right_shift = + filter_peroc ? args->right_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.right_shift_; + int32_t *cur_multiplier = + filter_peroc ? args->multiplier_ + cur_stride : args->conv_param_->conv_quant_arg_.quant_multiplier_; + int32_t *cur_zp = filter_peroc ? args->filter_zp_ptr_ + cur_stride : args->filter_zp_ptr_; + + Conv1x1Int8Opt(args->packed_input_, args->packed_weight_ + cur_stride * args->matmul_param_->deep_4_, + args->output_ptr_ + cur_stride, args->input_sum_, args->bias_data_ + cur_stride, + args->matmul_param_->row_, cur_oc, args->matmul_param_->deep_4_, cur_left_shift, cur_right_shift, + cur_multiplier, args->conv_param_, args->matmul_func_, cur_zp); + return NNACL_OK; +} + +int RunArmOc(void *cdata, int task_id) { + Conv1x1Args *args = (Conv1x1Args *)(cdata); +#ifdef ENABLE_ARM32 + int col_tile = C2NUM; +#else + int col_tile = C4NUM; +#endif + int stride = args->thread_stride_oc_ * col_tile; + int cur_stride = task_id * stride; + int res_stride = args->matmul_param_->col_ - cur_stride; + int cur_oc = MSMIN(stride, res_stride); + if (cur_oc <= 0) { + return NNACL_OK; + } + + bool filter_peroc = args->filter_peroc_; + int32_t *cur_left_shift = + filter_peroc ? args->left_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.left_shift_; + int32_t *cur_right_shift = + filter_peroc ? args->right_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.right_shift_; + int32_t *cur_multiplier = + filter_peroc ? args->multiplier_ + cur_stride : args->conv_param_->conv_quant_arg_.quant_multiplier_; + int32_t *cur_zp = filter_peroc ? args->filter_zp_ptr_ + cur_stride : args->filter_zp_ptr_; + + Conv1x1Int8(args->packed_input_, args->packed_weight_ + cur_stride * args->matmul_param_->deep_16_, + args->output_ptr_ + cur_stride, args->input_sum_, args->bias_data_ + cur_stride, + args->matmul_param_->row_, cur_oc, args->matmul_param_->deep_16_, cur_left_shift, cur_right_shift, + cur_multiplier, args->conv_param_, cur_zp); + return NNACL_OK; +} + +int RunArm64OptHw(void *cdata, int task_id) { + Conv1x1Args *args = (Conv1x1Args *)(cdata); + int cur_stride = args->thread_stride_hw_ * C4NUM; + int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; + int cur_hw = MSMIN(cur_stride, res_stride); + if (cur_hw <= 0) { + return NNACL_OK; + } + int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; + int8_t *hw_out = args->output_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->output_channel_; + int8_t *hw_packed_in = args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_4_; + int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; + + if (args->filter_peroc_) { + PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, 1); + } else { + PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, + args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_); + } + + Conv1x1Int8Opt(hw_packed_in, args->packed_weight_, hw_out, hw_input_sum, args->bias_data_, cur_hw, + args->matmul_param_->col_, args->matmul_param_->deep_4_, args->left_shift_, args->right_shift_, + args->multiplier_, args->conv_param_, args->matmul_func_, args->filter_zp_ptr_); + return NNACL_OK; +} + +int RunArmHw(void *cdata, int task_id) { + Conv1x1Args *args = (Conv1x1Args *)(cdata); + int cur_stride = args->thread_stride_hw_ * C4NUM; + int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; + int cur_hw = MSMIN(cur_stride, res_stride); + if (cur_hw <= 0) { + return NNACL_OK; + } + + int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; + int8_t *hw_out = args->output_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->output_channel_; + int8_t *hw_packed_in = + args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_16_; + int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; + + RowMajor2Row16x4MajorInt8(hw_in, hw_packed_in, cur_hw, args->matmul_param_->deep_); + + if (args->filter_peroc_) { + PackInputSum16x4PerLayer(hw_packed_in, hw_input_sum, 1, UP_ROUND(cur_hw, C4NUM), args->matmul_param_->deep_16_); + } else { + PackInputSum16x4PerLayer(hw_packed_in, hw_input_sum, args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_, + UP_ROUND(cur_hw, C4NUM), args->matmul_param_->deep_16_); + } + + Conv1x1Int8(hw_packed_in, args->packed_weight_, hw_out, hw_input_sum, args->bias_data_, cur_hw, + args->matmul_param_->col_, args->matmul_param_->deep_16_, args->left_shift_, args->right_shift_, + args->multiplier_, args->conv_param_, args->filter_zp_ptr_); + return NNACL_OK; +} + +void Conv1x1Run(int8_t *src_in, Conv1x1Args *args, int8_t *src_out) { + int row_pack_count = C4NUM; + int col_pack_count; + +#ifdef ENABLE_ARM32 + col_pack_count = C2NUM; +#else + if (args->support_optimize_) { + col_pack_count = C16NUM; + } else { + col_pack_count = C4NUM; + } +#endif + int thread_num = 1; + int hw_thread_count = UP_DIV(args->matmul_param_->row_, row_pack_count); + int oc_thread_count = UP_DIV(args->matmul_param_->col_, col_pack_count); + size_t thread_count_hw = MSMIN(thread_num, hw_thread_count); + args->thread_stride_hw_ = UP_DIV(hw_thread_count, thread_count_hw); + size_t thread_count_oc = MSMIN(thread_num, oc_thread_count); + args->thread_stride_oc_ = UP_DIV(oc_thread_count, thread_count_oc); + bool parallel_by_oc = oc_thread_count > thread_num; + + for (int batch_index = 0; batch_index < args->conv_param_->input_batch_; batch_index++) { + Pre1x1Trans(args, + src_in + batch_index * args->conv_param_->input_h_ * args->conv_param_->input_w_ * + args->conv_param_->input_channel_, + src_out + batch_index * args->matmul_param_->row_ * args->matmul_param_->col_); + if (parallel_by_oc) { + /* input transpose and input sum */ + if (args->support_optimize_) { + OcOptPre(args, 0); + } else { + RowMajor2Row16x4MajorInt8(args->input_ptr_, args->packed_input_, args->matmul_param_->row_, + args->matmul_param_->deep_); + if (args->filter_peroc_) { + PackInputSum16x4PerLayer(args->packed_input_, args->input_sum_, 1, args->matmul_param_->row_4_, + args->matmul_param_->deep_16_); + } else { + PackInputSum16x4PerLayer(args->packed_input_, args->input_sum_, + args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_, + args->matmul_param_->row_4_, args->matmul_param_->deep_16_); + } + } + /* matmul parallel by oc */ + if (args->support_optimize_) { + RunArm64OptOc(args, 0); + } else { + RunArmOc(args, 0); + } + } else { + /* matmul parallel by hw */ + if (args->support_optimize_) { + RunArm64OptHw(args, 0); + } else { + RunArmHw(args, 0); + } + } + } +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.h new file mode 100644 index 0000000000..310bec30dd --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv1x1_run_int8_wrapper.h @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_RUN_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_RUN_H_ + +#include +#include +#include "nnacl/conv_parameter.h" +#include "nnacl/matmul_parameter.h" + +typedef struct { + int32_t *input_sum_; /* per-oc */ + int32_t *filter_zp_ptr_; /* per-oc up round */ + int32_t *left_shift_; /* per-oc up round */ + int32_t *right_shift_; /* per-oc up round */ + int32_t *multiplier_; /* per-oc up round */ + int8_t *packed_weight_; + int32_t *bias_data_; + int8_t *packed_input_; + int8_t *input_ptr_; + int8_t *output_ptr_; + size_t thread_stride_hw_; + size_t thread_stride_oc_; + ConvParameter *conv_param_; + MatMulParameter *matmul_param_; + MATMUL_OPT_DP_FUNC matmul_func_; + bool pre_trans_input_; + bool support_optimize_; + bool filter_peroc_; +} Conv1x1Args; + +void Conv1x1Run(int8_t *src_in, Conv1x1Args *args, int8_t *src_out); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV1X1_RUN_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.c new file mode 100644 index 0000000000..9947c56140 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.c @@ -0,0 +1,88 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/conv_init_int8_wrapper.h" +#include +#include "nnacl/op_base.h" +#include "nnacl/int8/matmul_int8.h" +#include "nnacl/errorcode.h" + +int ConvInit(int8_t *origin_weight, const int32_t *ori_bias, const int32_t *filter_quant_zps, int kernel_h, + int kernel_w, int input_channel, int output_channel, int32_t input_zp, bool filter_peroc, + bool support_optimize, int8_t **packed_weight, int32_t **bias_data) { + int8_t *packed_weight_ = NULL; + int32_t *bias_data_ = NULL; + int kernel_plane = kernel_h * kernel_w; + int up_round_deep; + int up_round_oc; +#ifdef ENABLE_ARM32 + up_round_oc = UP_ROUND(output_channel, C2NUM); + up_round_deep = UP_ROUND(kernel_plane * input_channel, C16NUM); +#else + if (support_optimize) { + up_round_oc = UP_ROUND(output_channel, C8NUM); + up_round_deep = UP_ROUND(kernel_plane * input_channel, C4NUM); + } else { + up_round_oc = UP_ROUND(output_channel, C4NUM); + up_round_deep = UP_ROUND(kernel_plane * input_channel, C16NUM); + } +#endif + int pack_weight_size = up_round_oc * up_round_deep; + size_t bias_size = up_round_oc * sizeof(int32_t); + + // init weight + packed_weight_ = (int8_t *)(malloc(pack_weight_size)); + if (packed_weight_ == NULL) { + return NNACL_ERR; + } + memset(packed_weight_, 0, pack_weight_size); +#ifdef ENABLE_ARM32 + RowMajor2Row2x16MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); +#else + if (support_optimize) { + RowMajor2Row8x4MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); + } else { + RowMajor2Row16x4MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); + } +#endif + + // init bias + bias_data_ = (int32_t *)(malloc(bias_size)); + if (bias_data_ == NULL) { + free(packed_weight_); + return NNACL_ERR; + } + memset(bias_data_, 0, bias_size); + if (ori_bias != NULL) { + memcpy(bias_data_, ori_bias, output_channel * sizeof(int32_t)); + } + + for (int oc = 0; oc < output_channel; oc++) { + int32_t filter_zp = filter_quant_zps[0]; + if (filter_peroc) { + filter_zp = filter_quant_zps[oc]; + } + int32_t weight_sum_value = up_round_deep * filter_zp; + for (int i = 0; i < kernel_plane * input_channel; i++) { + weight_sum_value += origin_weight[oc * kernel_plane * input_channel + i] - filter_zp; + } + bias_data_[oc] += filter_zp * input_zp * up_round_deep - weight_sum_value * input_zp; + } + + *packed_weight = packed_weight_; + *bias_data = bias_data_; + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.h new file mode 100644 index 0000000000..819cad60f1 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/conv_init_int8_wrapper.h @@ -0,0 +1,26 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV_INIT_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV_INIT_H_ + +#include +#include + +int ConvInit(int8_t *origin_weight, const int32_t *ori_bias, const int32_t *filter_quant_zps, int kernel_h, + int kernel_w, int input_channel, int output_channel, int32_t input_zp, bool filter_peroc, + bool support_optimize, int8_t **packed_weight, int32_t **bias_data); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_CONV_INIT_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.c new file mode 100644 index 0000000000..3e5cf2a410 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.c @@ -0,0 +1,25 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/convolution_depthwise_int8_wrapper.h" + +int ConvDepthwiseInt8Run(void *cdata, int task_id) { + ConvDepthwiseInt8Args *args = (ConvDepthwiseInt8Args *)cdata; + int32_t *buffer = args->row_buffer_ + args->conv_param_->output_w_ * args->conv_param_->output_channel_ * task_id; + ConvDwInt8(args->output_data_, buffer, args->input_data_, args->weight_data_, args->bias_data_, args->conv_param_, + task_id); + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.h new file mode 100644 index 0000000000..6df27d027f --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_depthwise_int8_wrapper.h @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_DEPTHWISE_WRAPPER_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_DEPTHWISE_WRAPPER_INT8_WRAPPER_H_ + +#include "nnacl/errorcode.h" +#include "nnacl/conv_parameter.h" +#include "nnacl/int8/conv_depthwise_int8.h" + +typedef struct { + int8_t *output_data_; + int32_t *row_buffer_; + const int8_t *input_data_; + const int16_t *weight_data_; + const int32_t *bias_data_; + const ConvParameter *conv_param_; +} ConvDepthwiseInt8Args; + +int ConvDepthwiseInt8Run(void *cdata, int task_id); + +#endif // MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_DEPTHWISE_WRAPPER_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.c new file mode 100644 index 0000000000..3f916829bf --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.c @@ -0,0 +1,43 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/convolution_int8_wrapper.h" + +void CheckSupportOptimize(const ConvolutionInt8Args *args) { + int tile_num = 8; +#ifdef ENABLE_ARM32 + tile_num = 4; + args->is_optimize_ = false; +#endif +#ifdef ENABLE_ARM64 + if (mindspore::lite::IsSupportSDot()) { + matmul_func_ = MatMulRInt8_optimize_handler; + args->is_optimize_ = true; + } else { + tile_num = 4; + args->is_optimize_ = false; + } +#endif + args->conv_param_->tile_num_ = tile_num; +} + +int ConvolutionInt8Run(void *cdata, int task_id) { + ConvolutionInt8Args *args = (ConvolutionInt8Args *)cdata; + ConvInt8(args->input_data_, args->packed_input_, args->matmul_input_, args->packed_weight_, args->bias_data_, + args->output_data_, args->filter_zp_, args->input_sum_, task_id, args->conv_param_, args->matmul_func_, + args->is_optimize_); + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.h new file mode 100644 index 0000000000..ec19d41aa1 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/convolution_int8_wrapper.h @@ -0,0 +1,43 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_WRAPPER_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_WRAPPER_INT8_WRAPPER_H_ + +#include "nnacl/errorcode.h" +#include "nnacl/conv_parameter.h" +#include "nnacl/matmul_parameter.h" +#include "nnacl/int8/conv_int8.h" + +typedef struct { + int8_t *input_data_; + int8_t *packed_input_; + int8_t *matmul_input_; + int8_t *packed_weight_; + const int32_t *bias_data_; + int8_t *output_data_; + int32_t *filter_zp_; + int32_t *input_sum_; + ConvParameter *conv_param_; + MATMUL_OPT_R_FUNC matmul_func_; + bool is_optimize_; +} ConvolutionInt8Args; + +void CheckSupportOptimize(const ConvolutionInt8Args *args); + +int ConvolutionInt8Run(void *cdata, int task_id); + +#endif // MINDSPORE_LITE_MICRO_INT8_CONVOLUTION_WRAPPER_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/wrapper/int8/matmul_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/matmul_int8_wrapper.c similarity index 100% rename from mindspore/lite/micro/wrapper/int8/matmul_int8_wrapper.c rename to mindspore/lite/micro/coder/operator_library/wrapper/int8/matmul_int8_wrapper.c diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/matmul_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/matmul_int8_wrapper.h new file mode 100644 index 0000000000..0c9f48cb48 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/matmul_int8_wrapper.h @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ +#include +#include "nnacl/int8/matmul_int8.h" +#ifdef __cplusplus +extern "C" { +#endif + +void InitInt8MatrixA(int8_t *src_ptr, int32_t *input_sums, int8_t *dst_ptr, int batch, int row, int deep, int input_zp, + const int *weight_zp, bool a_transpose); + +void InitInt8MatrixB(int8_t *src_ptr, int32_t *weight_bias_sums_batch_, int8_t *dst_ptr, int batch, int deep, int col, + int col_4, int deep_16, int input_zp, int *weight_zp, const int *bias_ptr, bool b_transpose); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.c new file mode 100644 index 0000000000..58dcbbf28d --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.c @@ -0,0 +1,25 @@ +/* + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/resize_int8_wrapper.h" +#include "nnacl/errorcode.h" + +int ResizeInt8Run(void *cdata, int task_id) { + ResizeInt8Args *args = (ResizeInt8Args *)cdata; + ResizeNearestNeighborInt8Simple(args->input_data_, args->output_data_, args->input_shape_, args->output_shape_, + args->align_corners_, task_id, args->thread_num_); + return NNACL_OK; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.h new file mode 100644 index 0000000000..6721109e17 --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/resize_int8_wrapper.h @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_WRAPPER_INT8_RESIZE_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_WRAPPER_INT8_RESIZE_INT8_WRAPPER_H_ + +#include "nnacl/int8/resize_int8.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + const int8_t *input_data_; + int8_t *output_data_; + const int *input_shape_; + const int *output_shape_; + const bool align_corners_; + int thread_num_; +} ResizeInt8Args; + +int ResizeInt8Run(void *cdata, int task_id); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_MICRO_WRAPPER_INT8_RESIZE_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.c b/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.c new file mode 100644 index 0000000000..191908d29c --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.c @@ -0,0 +1,24 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "wrapper/int8/slice_int8_wrapper.h" +#include "nnacl/int8/slice_int8.h" + +int SliceInt8Run(void *cdata, int task_id) { + SliceArgs *args = (SliceArgs *)(cdata); + int ret = SliceInt8(args->input_data_, args->output_data_, args->param_, task_id); + return ret; +} diff --git a/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.h b/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.h new file mode 100644 index 0000000000..0ecbc875cf --- /dev/null +++ b/mindspore/lite/micro/coder/operator_library/wrapper/int8/slice_int8_wrapper.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_SLICE_INT8_WRAPPER_H_ +#define MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_SLICE_INT8_WRAPPER_H_ + +#include +#include "nnacl/slice_parameter.h" + +typedef struct SliceArgs { + int8_t *input_data_; + int8_t *output_data_; + SliceParameter *param_; +} SliceArgs; + +int SliceInt8Run(void *cdata, int task_id); + +#endif // MINDSPORE_LITE_MICRO_CODER_OPERATOR_LIBRARY_WRAPPER_INT8_SLICE_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/coder/session.cc b/mindspore/lite/micro/coder/session.cc index 093cc11759..f4fdc5084e 100644 --- a/mindspore/lite/micro/coder/session.cc +++ b/mindspore/lite/micro/coder/session.cc @@ -27,8 +27,13 @@ #include "coder/opcoders/op_coder_builder.h" #include "coder/utils/coder_utils.h" #include "coder/log.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/version_manager.h" +#include "src/runtime/infer_manager.h" +#include "src/scheduler.h" #include "include/errorcode.h" #include "src/common/file_utils.h" +#include "coder/opcoders/nnacl/dequant/de_quant.h" namespace mindspore::lite::micro { @@ -57,21 +62,28 @@ int CoderSession::InferShape() { outputs.push_back(all_tensors.at(curr_node->output_indices_.at(j))); } - PrimitiveC *primitive = curr_node->primitive_; - if (primitive == nullptr) { - MS_LOG(ERROR) << "Op " << curr_node->name_ << " should exist in model!"; + auto primitive = curr_node->primitive_; + MS_CHECK_PTR(primitive); + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + auto parame_gen = PopulateRegistry::GetInstance()->GetParameterCreator(GetPrimitiveType(primitive), schema_version); + if (parame_gen == nullptr) { + MS_LOG(ERROR) << "parameter generator is nullptr."; + return RET_NULL_PTR; + } + auto parameter = parame_gen(primitive); + if (parameter == nullptr) { + MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " << PrimitiveTypeName(GetPrimitiveType(primitive)); return RET_ERROR; } - primitive->set_infer_flag(true); - int ret = primitive->InferShape(inputs, outputs); + parameter->infer_flag_ = true; + auto ret = KernelInferShape(inputs, &outputs, parameter); if (ret == RET_INFER_INVALID) { MS_LOG(INFO) << "InferShape shouldn't be done before runtime, name: " << curr_node->name_ - << ", type: " << schema::EnumNamePrimitiveType(static_cast(primitive->Type())) - << "flag set to false."; - primitive->set_infer_flag(false); + << ", type: " << PrimitiveTypeName(GetPrimitiveType(primitive)) << "flag set to false."; + parameter->infer_flag_ = false; } else if (ret != RET_OK) { - MS_LOG(ERROR) << "InferShape failed, name: " << curr_node->name_ << ", type: " - << schema::EnumNamePrimitiveType(static_cast(primitive->Type())); + MS_LOG(ERROR) << "InferShape failed, name: " << curr_node->name_ + << ", type: " << PrimitiveTypeName(GetPrimitiveType(primitive)); return RET_ERROR; } } @@ -81,7 +93,11 @@ int CoderSession::InferShape() { void CoderSession::EndCode() { context_->set_tensor_map(allocator_->tensors_map()); context_->set_saved_weights(allocator_->saved_weights()); - context_->set_total_buffer_size(allocator_->total_buffer_size()); + size_t de_quant_max_workspace_size = nnacl::Dequant::GetInstance()->de_quant_max_workspace(); + size_t final_total_size = allocator_->total_buffer_size() > de_quant_max_workspace_size + ? allocator_->total_buffer_size() + : de_quant_max_workspace_size; + context_->set_total_buffer_size(final_total_size); context_->set_graph_inputs(coder_graph_->input_tensors()); context_->set_graph_outputs(coder_graph_->output_tensors()); Configurator *config = Configurator::GetInstance(); @@ -90,7 +106,7 @@ void CoderSession::EndCode() { blocks = AddDumpDataInfo(context_->code_blocks(), op_coders_); context_->set_code_blocks(blocks); } - if (config->code_mode() == Code_Train) { + if (config->code_mode() == Train) { Train::TransformGraphForTrain(context_.get(), op_coders_); } } @@ -104,15 +120,17 @@ int CoderSession::Run() { // 2. prepare, init model parameters for (const auto &op_coder : op_coders_) { MS_CHECK_PTR(op_coder); + MS_LOG(DEBUG) << "prepare: " << op_coder->name(); ret = op_coder->Prepare(context_.get()); - MS_CHECK_RET_CODE(ret, "prepare coder " << op_coder->ID() << " failed"); + MS_CHECK_RET_CODE(ret, "prepare coder " << op_coder->name() << " failed"); allocator_->enable_is_next(); } // 3. docode, write operator code for (const auto &op_coder : op_coders_) { MS_CHECK_PTR(op_coder); + MS_LOG(DEBUG) << "code: " << op_coder->name(); ret = op_coder->DoCode(this->context_.get()); - MS_CHECK_RET_CODE(ret, "do coder " << op_coder->ID() << " failed"); + MS_CHECK_RET_CODE(ret, "do coder " << op_coder->name() << " failed"); } this->EndCode(); @@ -126,12 +144,11 @@ int CoderSession::GenerateCode() { Configurator *config = Configurator::GetInstance(); CodeMode code_mode = config->code_mode(); switch (code_mode) { - case Code_Normal: - case Code_Inference: + case Inference: MS_LOG(INFO) << "generate code for Inference"; generator = std::make_shared(std::move(context_)); break; - case Code_Train: + case Train: MS_LOG(INFO) << "generate code for Train"; generator = std::make_shared(std::move(context_)); break; @@ -240,6 +257,7 @@ int CoderSession::CreateOpCoders() { Configurator *config = Configurator::GetInstance(); Target code_target = config->target(); CodeMode code_mode = config->code_mode(); + bool support_parallel = config->support_parallel(); uint32_t nodes_size = model->all_nodes_.size(); OpCoderBuilder builder; for (uint32_t i = 0; i < nodes_size; ++i) { @@ -293,6 +311,7 @@ int CoderSession::CreateOpCoders() { .outputs(outputs) .node(node) .target(code_target) + .support_parallel(support_parallel) .data_type(tensor_data_type) .mode(code_mode) .input_indices(input_indices) diff --git a/mindspore/lite/micro/coder/train.cc b/mindspore/lite/micro/coder/train.cc index b2bfb07993..f566e2c4a4 100644 --- a/mindspore/lite/micro/coder/train.cc +++ b/mindspore/lite/micro/coder/train.cc @@ -17,10 +17,13 @@ #include "coder/train.h" #include #include -#include +#include #include #include #include +#include +#include "schema/ops_generated.h" +#include "src/common/prim_util.h" namespace mindspore::lite::micro { @@ -53,17 +56,17 @@ std::set FindInferenceOpcoders(OperatorCoder *edge) { } int Train::TransformGraphForTrain(CoderContext *context, const std::vector> &op_coders) { - const std::set loss_types = {schema::PrimitiveType_SoftmaxCrossEntropy, - schema::PrimitiveType_SparseSoftmaxCrossEntropy, - schema::PrimitiveType_BinaryCrossEntropy, - schema::PrimitiveType_SmoothL1Loss, - schema::PrimitiveType_SmoothL1LossGrad, - schema::PrimitiveType_SigmoidCrossEntropyWithLogits, - schema::PrimitiveType_SigmoidCrossEntropyWithLogitsGrad}; + const std::array loss_types = {schema::PrimitiveType_SparseSoftmaxCrossEntropy, + schema::PrimitiveType_BinaryCrossEntropy, + schema::PrimitiveType_SmoothL1Loss, + schema::PrimitiveType_SmoothL1LossGrad, + schema::PrimitiveType_SigmoidCrossEntropyWithLogits, + schema::PrimitiveType_SigmoidCrossEntropyWithLogitsGrad}; OperatorCoder *loss_op = nullptr; for (const auto &opcoder : op_coders) { - auto primitive_type = static_cast(opcoder->primitive()->Type()); - auto item = loss_types.find(primitive_type); + const Model::Node *node = opcoder->node(); + int primitive_type = GetPrimitiveType(node->primitive_); + auto item = std::find(loss_types.begin(), loss_types.end(), primitive_type); if (item != loss_types.end()) { loss_op = opcoder.get(); break; diff --git a/mindspore/lite/micro/coder/utils/coder_utils.cc b/mindspore/lite/micro/coder/utils/coder_utils.cc index 55a92e49c0..08d31b6a26 100644 --- a/mindspore/lite/micro/coder/utils/coder_utils.cc +++ b/mindspore/lite/micro/coder/utils/coder_utils.cc @@ -38,7 +38,7 @@ void TensorDataToFile(const lite::Tensor *tensor, std::ofstream &ofs) { } int len = tensor->ElementsNum(); for (int i = 0; i < len; ++i) { - ofs << data[i] << ", "; + ofs << std::to_string(data[i]) << ", "; if (i % NUM == NUM - 1) { ofs << "\n"; } @@ -59,6 +59,7 @@ void PrintTensorData(const lite::Tensor *tensor, std::ofstream &ofs) { case kNumberTypeInt: case kNumberTypeInt32: TensorDataToFile(tensor, ofs); + break; case kNumberTypeInt64: TensorDataToFile(tensor, ofs); break; @@ -113,7 +114,7 @@ std::vector AddDumpDataInfo(const std::vector &blocks, for (size_t i = 0; i < num; ++i) { auto &opcoder = opcoders.at(i); std::string code = blocks.at(i); - std::string name = opcoder->ID(); + std::string name = opcoder->name(); code += " {\n"; code += " FILE *output_file = fopen(\"./" + name + ".ir\", \"w\");\n"; code += " fprintf(output_file, \"Node:" + name + "\\n\");\n"; diff --git a/mindspore/lite/micro/coder/utils/type_cast.cc b/mindspore/lite/micro/coder/utils/type_cast.cc index 7be96be52a..54dd193a44 100644 --- a/mindspore/lite/micro/coder/utils/type_cast.cc +++ b/mindspore/lite/micro/coder/utils/type_cast.cc @@ -28,6 +28,16 @@ std::string EnumNameDataType(TypeId type) { return "kNumberTypeInt16"; case kNumberTypeInt32: return "kNumberTypeInt32"; + case kNumberTypeInt64: + return "kNumberTypeInt64"; + case kNumberTypeUInt: + return "kNumberTypeUInt"; + case kNumberTypeUInt8: + return "kNumberTypeUInt8"; + case kNumberTypeUInt16: + return "kNumberTypeUInt16"; + case kNumberTypeUInt32: + return "kNumberTypeUInt32"; case kNumberTypeFloat: case kNumberTypeFloat32: return "kNumberTypeFloat32"; @@ -48,16 +58,18 @@ std::string GetTensorDataType(TypeId type) { case kNumberTypeFloat32: return "float "; case kNumberTypeInt8: - return "int8"; + return "int8_t "; + case kNumberTypeInt16: + return "int16_t "; case kNumberTypeInt: case kNumberTypeInt32: - return "int32_t"; + return "int32_t "; case kNumberTypeUInt8: - return "uint8_t"; + return "uint8_t "; case kNumberTypeUInt32: - return "uint32_t"; + return "uint32_t "; case kNumberTypeInt64: - return "int64_t"; + return "int64_t "; default: MS_LOG(ERROR) << "unsupported data type: " << EnumNameDataType(type); return ""; diff --git a/mindspore/lite/micro/coder/utils/type_cast.h b/mindspore/lite/micro/coder/utils/type_cast.h index 44b599fcb0..ab805d9f08 100644 --- a/mindspore/lite/micro/coder/utils/type_cast.h +++ b/mindspore/lite/micro/coder/utils/type_cast.h @@ -47,13 +47,16 @@ std::string GetVariableTypeName() { {std::type_index(typeid(int32_t)), "int32_t"}, {std::type_index(typeid(int16_t)), "int16_t"}, {std::type_index(typeid(int8_t)), "int8_t"}, + {std::type_index(typeid(uint8_t)), "uint8_t"}, {std::type_index(typeid(float)), "float"}, {std::type_index(typeid(double)), "double"}, {std::type_index(typeid(::QuantArg)), "QuantArg"}, + {std::type_index(typeid(void *)), "void *"}, {std::type_index(typeid(int *)), "int *"}, {std::type_index(typeid(int32_t *)), "int32_t *"}, {std::type_index(typeid(int16_t *)), "int16_t *"}, {std::type_index(typeid(int8_t *)), "int8_t *"}, + {std::type_index(typeid(uint8_t *)), "uint8_t *"}, {std::type_index(typeid(float *)), "float *"}}; auto item = types_name.find(std::type_index(typeid(T))); if (item != types_name.end()) { diff --git a/mindspore/lite/micro/example/micro_speech/Softmax-3.out b/mindspore/lite/micro/example/micro_speech/Softmax-3.out index 740c985cf6..f8a74a8ca6 100644 --- a/mindspore/lite/micro/example/micro_speech/Softmax-3.out +++ b/mindspore/lite/micro/example/micro_speech/Softmax-3.out @@ -1,11 +1,11 @@ Node:Softmax-3 -input Tensor:micro_speech_B+0 +input Tensor: (int8_t *)(micro_speech_B+0) input 1, 4, input type:DT_INT8, format:NHWC, elementSize: 4 input Data: - -50, -5, 121, -4 -output Tensor:micro_speech_B+8 +-50, -5, 121, -4, +output Tensor: (int8_t *)(micro_speech_B+8) output 1, 4, output type:DT_INT8, format:NHWC, elementSize: 4 output Data: - -128, -128, 127, -128 +-128, -128, 127, -128, diff --git a/mindspore/lite/micro/example/mobilenetv2_quant/1_224_224_3.bin b/mindspore/lite/micro/example/mobilenetv2_quant/1_224_224_3.bin deleted file mode 100644 index 1c035cd0e0..0000000000 Binary files a/mindspore/lite/micro/example/mobilenetv2_quant/1_224_224_3.bin and /dev/null differ diff --git a/mindspore/lite/micro/example/mobilenetv2_quant/Reshape-64.out b/mindspore/lite/micro/example/mobilenetv2_quant/Reshape-64.out index cc05ebffa8..49a744194c 100644 --- a/mindspore/lite/micro/example/mobilenetv2_quant/Reshape-64.out +++ b/mindspore/lite/micro/example/mobilenetv2_quant/Reshape-64.out @@ -1,55 +1,55 @@ Node:Reshape-64 -input Tensor:mobilenetv2_quant_B+1280 +input Tensor: (int8_t *)(mobilenetv2_quant_B+1280) input 1, 1, 1, 1001, input type:DT_INT8, format:NHWC, elementSize: 1001 input Data: - -77, -76, -60, -50, -50, -40, -53, -41, -60, -67, -77, -72, -65, -73, -62, -67, -72, -57, -71, -55, -74, -69, -79, -72, -63, -75, -55, -65, -62, -69, -68, -59, -59, -79, -64, -48, -63, -55, -71, -67, -56, -64, -64, -56, -71, - -59, -72, -36, -81, -63, -73, -61, -84, -64, -68, -75, -61, -77, -71, -64, -61, -46, -82, -73, -83, -85, -62, -75, -68, -54, -61, -56, -45, -60, -50, -68, -53, -72, -52, -67, -49, -73, -57, -71, -79, -51, -61, -72, -81, -60, - -60, -68, -79, -56, -60, -48, -79, -69, -59, -71, -54, -58, -67, -75, -76, -82, -75, -75, -41, -45, -49, -60, -37, -53, -36, -49, -65, -76, -58, -73, -66, -58, -75, -76, -77, -78, -60, -74, -66, -76, -60, -72, -62, -58, -64, - -65, -66, -54, -60, -74, -69, -75, -58, -71, -60, -77, -65, -72, -78, -67, -69, -77, -77, -87, -68, -75, -63, -76, -73, -69, -61, -93, -70, -64, -76, -61, -73, -67, -76, -77, -75, -73, -66, -59, -71, -76, -71, -83, -75, -55, - -67, -69, -75, -69, -66, -58, -63, -74, -72, -77, -72, -59, -69, -74, -65, -76, -61, -64, -80, -83, -58, -64, -61, -65, -49, -65, -77, -72, -66, -59, -65, -70, -69, -69, -72, -91, -77, -80, -76, -72, -53, -82, -76, -80, -76, - -63, -78, -92, -79, -82, -53, -58, -70, -72, -78, -63, -72, -56, -81, -77, -70, -70, -71, -51, -73, -90, -65, -68, -78, -59, -70, -72, -61, -80, -69, -73, -81, -81, -70, -74, -76, -56, -62, -91, -71, -64, -69, -71, -63, -84, - -80, -74, -87, -75, -80, -71, -82, -71, -74, -73, -76, -64, -55, -67, -71, -68, -56, -90, -76, -82, -71, -81, -77, -78, -70, -71, -66, -50, -71, -75, -69, -67, -36, -60, -79, -58, -45, -64, -55, -41, -66, -28, -60, -64, -55, - -63, -49, -53, -49, -55, -65, -60, -75, -69, -76, -63, -74, -69, -44, -49, -65, -62, -59, -78, -66, -76, -69, -76, -76, -75, -80, -68, -80, -86, -86, -68, -75, -77, -77, -79, -76, -84, -85, -72, -67, -71, -89, -70, -69, -75, - -70, -85, -65, -62, -59, -81, -73, -56, -54, -79, -85, -70, -77, -72, -62, -74, -75, -87, -87, -69, -80, -75, -71, -79, -80, -85, -64, -66, -83, -84, -66, -60, -56, -75, -67, -85, -61, -60, -62, -79, -57, -73, -62, -54, -72, - -85, -74, -92, -71, -82, -55, -86, -69, -57, -65, -71, -86, -59, -54, -44, -69, -73, -35, -76, -85, -89, -57, -53, -61, -55, -50, -63, -81, -71, -67, -45, -70, -65, -44, -89, -82, -36, -44, -96, -68, -89, -71, -57, -65, -65, - -75, -98, -89, -78, -68, -74, -48, -78, -59, -55, -62, -54, -73, -53, -37, -80, -79, -79, -76, -64, -70, -68, -82, -57, -73, -53, -58, -88, -96, -59, -52, -58, -59, -67, -58, -59, -58, -76, -86, -36, -56, -42, -72, -70, -78, - -67, -69, -63, -59, -63, -68, -78, -56, -61, -68, -66, -56, -45, -59, -61, -88, -70, -88, -72, -75, -72, -73, -85, -60, -82, -62, -74, -85, -64, -75, -55, -75, -85, -73, -56, -67, -43, -66, -83, -65, -70, -79, -66, -70, -57, - -24, -77, -71, -66, -66, -86, -57, -81, -77, -87, -44, -85, -67, -55, -76, -76, -78, -75, -74, -83, -49, -61, -70, -25, -52, -74, -77, -64, -72, -73, -73, -53, -83, -69, -68, -22, -79, -80, -73, -64, -61, -58, -61, -79, -62, - -98, -55, -91, -52, -74, -74, -82, -56, -70, -70, -73, -60, -53, -84, -76, -51, -51, -78, -62, -82, -48, -68, -57, -58, -59, -73, -71, -68, -92, -72, -67, -72, -38, -69, -62, -32, -63, -63, -37, -65, -76, -84, -28, -88, -67, - -58, -58, -73, -61, -66, -91, -64, -76, -57, -74, -55, -46, -78, -71, -43, -22, -37, -32, -70, -72, -43, -51, -68, -69, -71, -83, -66, -82, -65, -94, -68, -72, -83, -76, -89, -63, -85, -69, -86, -81, -64, -72, -80, -64, -62, - -54, -94, -84, -44, -79, -65, -71, -72, -59, -62, -87, -62, -76, -93, -62, -69, -66, -84, -66, -64, -62, -46, -50, -85, -78, -79, -54, -70, -69, -57, -53, -80, -85, -65, -68, -80, -62, -32, -71, -76, -58, -76, -44, -88, -60, - -68, -58, -57, -49, -53, -75, -63, -98, -72, -53, -82, -50, -63, -72, -53, -68, -49, -57, -45, -63, -92, -73, -53, -74, -82, -64, -69, -57, -53, -72, -43, -56, -92, -71, -62, -91, -74, -64, -75, -84, -65, -58, -69, -91, -63, - -66, -70, -90, -31, -59, -50, -58, -65, -73, -43, -72, -60, -58, -64, -74, -85, -80, -68, -64, -53, -60, -74, -83, -85, -86, -48, -67, -58, -37, -78, -27, -60, -59, -69, -73, -51, -87, -66, -85, -75, -34, -35, -71, -86, -66, - -91, -62, -58, -79, -84, -70, -57, -89, -85, -34, -49, -84, -77, -75, -90, -50, -51, -65, -72, -52, -89, -78, -60, -81, -84, -65, -69, -67, -58, -67, -75, -57, -52, -68, -54, -16, -51, -49, -77, -64, -65, -56, -55, -38, -48, - -62, -63, -79, -80, -51, -72, -73, -57, -71, -59, -86, -86, -67, -86, -63, -67, -54, -83, -47, -70, -76, -77, -68, -89, -70, -12, -76, -70, -59, -44, -67, -16, -80, -89, -67, -69, -90, -76, -31, -49, -70, -89, -57, -58, -31, - -48, -59, -86, -83, -84, -31, -40, -88, -30, -43, -84, -59, -40, -68, -59, -80, -85, -67, -69, -73, -65, -57, -78, -80, -96, -82, -82, -71, -87, -76, -67, -85, -79, -74, -72, -87, -78, -58, -36, -64, -75, -97, -75, -90, -77, - -76, -56, -63, -72, -57, -56, -62, -64, -57, -60, -60, -68, -71, -84, -43, -71, -86, -65, -79, -84, -81, -100, -48, -63, -48, -68, -84, -15, -47, -49, -62, -65, -61, -49, -46, -71, -45, -46, -80, -48, -39, -50, -71, -67, -63, - -65, -74, -62, -72, -72, -88, -82, -69, -85, -73, -31 -output Tensor:mobilenetv2_quant_B+0 +-77, -76, -60, -50, -50, -40, -53, -41, -60, -67, -77, -72, -65, -73, -62, -67, -72, -57, -71, -55, -74, -69, -79, -72, -63, -75, -55, -65, -62, -69, -68, -59, -59, -79, -64, -48, -63, -55, -71, -67, -56, -64, -64, -56, -71, +-59, -72, -36, -81, -63, -73, -61, -84, -64, -68, -75, -61, -77, -71, -64, -61, -46, -82, -73, -83, -85, -62, -75, -68, -54, -61, -56, -45, -60, -50, -68, -53, -72, -52, -67, -49, -73, -57, -71, -79, -51, -61, -72, -81, -60, +-60, -68, -79, -56, -60, -48, -79, -69, -59, -71, -54, -58, -67, -75, -76, -82, -75, -75, -41, -45, -49, -60, -37, -53, -36, -49, -65, -76, -58, -73, -66, -58, -75, -76, -77, -78, -60, -74, -66, -76, -60, -72, -62, -58, -64, +-65, -66, -54, -60, -74, -69, -75, -58, -71, -60, -77, -65, -72, -78, -67, -69, -77, -77, -87, -68, -75, -63, -76, -73, -69, -61, -93, -70, -64, -76, -61, -73, -67, -76, -77, -75, -73, -66, -59, -71, -76, -71, -83, -75, -55, +-67, -69, -75, -69, -66, -58, -63, -74, -72, -77, -72, -59, -69, -74, -65, -76, -61, -64, -80, -83, -58, -64, -61, -65, -49, -65, -77, -72, -66, -59, -65, -70, -69, -69, -72, -91, -77, -80, -76, -72, -53, -82, -76, -80, -76, +-63, -78, -92, -79, -82, -53, -58, -70, -72, -78, -63, -72, -56, -81, -77, -70, -70, -71, -51, -73, -90, -65, -68, -78, -59, -70, -72, -61, -80, -69, -73, -81, -81, -70, -74, -76, -56, -62, -91, -71, -64, -69, -71, -63, -84, +-80, -74, -87, -75, -80, -71, -82, -71, -74, -73, -76, -64, -55, -67, -71, -68, -56, -90, -76, -82, -71, -81, -77, -78, -70, -71, -66, -50, -71, -75, -69, -67, -36, -60, -79, -58, -45, -64, -55, -41, -66, -28, -60, -64, -55, +-63, -49, -53, -49, -55, -65, -60, -75, -69, -76, -63, -74, -69, -44, -49, -65, -62, -59, -78, -66, -76, -69, -76, -76, -75, -80, -68, -80, -86, -86, -68, -75, -77, -77, -79, -76, -84, -85, -72, -67, -71, -89, -70, -69, -75, +-70, -85, -65, -62, -59, -81, -73, -56, -54, -79, -85, -70, -77, -72, -62, -74, -75, -87, -87, -69, -80, -75, -71, -79, -80, -85, -64, -66, -83, -84, -66, -60, -56, -75, -67, -85, -61, -60, -62, -79, -57, -73, -62, -54, -72, +-85, -74, -92, -71, -82, -55, -86, -69, -57, -65, -71, -86, -59, -54, -44, -69, -73, -35, -76, -85, -89, -57, -53, -61, -55, -50, -63, -81, -71, -67, -45, -70, -65, -44, -89, -82, -36, -44, -96, -68, -89, -71, -57, -65, -65, +-75, -98, -89, -78, -68, -74, -48, -78, -59, -55, -62, -54, -73, -53, -37, -80, -79, -79, -76, -64, -70, -68, -82, -57, -73, -53, -58, -88, -96, -59, -52, -58, -59, -67, -58, -59, -58, -76, -86, -36, -56, -42, -72, -70, -78, +-67, -69, -63, -59, -63, -68, -78, -56, -61, -68, -66, -56, -45, -59, -61, -88, -70, -88, -72, -75, -72, -73, -85, -60, -82, -62, -74, -85, -64, -75, -55, -75, -85, -73, -56, -67, -43, -66, -83, -65, -70, -79, -66, -70, -57, +-24, -77, -71, -66, -66, -86, -57, -81, -77, -87, -44, -85, -67, -55, -76, -76, -78, -75, -74, -83, -49, -61, -70, -25, -52, -74, -77, -64, -72, -73, -73, -53, -83, -69, -68, -22, -79, -80, -73, -64, -61, -58, -61, -79, -62, +-98, -55, -91, -52, -74, -74, -82, -56, -70, -70, -73, -60, -53, -84, -76, -51, -51, -78, -62, -82, -48, -68, -57, -58, -59, -73, -71, -68, -92, -72, -67, -72, -38, -69, -62, -32, -63, -63, -37, -65, -76, -84, -28, -88, -67, +-58, -58, -73, -61, -66, -91, -64, -76, -57, -74, -55, -46, -78, -71, -43, -22, -37, -32, -70, -72, -43, -51, -68, -69, -71, -83, -66, -82, -65, -94, -68, -72, -83, -76, -89, -63, -85, -69, -86, -81, -64, -72, -80, -64, -62, +-54, -94, -84, -44, -79, -65, -71, -72, -59, -62, -87, -62, -76, -93, -62, -69, -66, -84, -66, -64, -62, -46, -50, -85, -78, -79, -54, -70, -69, -57, -53, -80, -85, -65, -68, -80, -62, -32, -71, -76, -58, -76, -44, -88, -60, +-68, -58, -57, -49, -53, -75, -63, -98, -72, -53, -82, -50, -63, -72, -53, -68, -49, -57, -45, -63, -92, -73, -53, -74, -82, -64, -69, -57, -53, -72, -43, -56, -92, -71, -62, -91, -74, -64, -75, -84, -65, -58, -69, -91, -63, +-66, -70, -90, -31, -59, -50, -58, -65, -73, -43, -72, -60, -58, -64, -74, -85, -80, -68, -64, -53, -60, -74, -83, -85, -86, -48, -67, -58, -37, -78, -27, -60, -59, -69, -73, -51, -87, -66, -85, -75, -34, -35, -71, -86, -66, +-91, -62, -58, -79, -84, -70, -57, -89, -85, -34, -49, -84, -77, -75, -90, -50, -51, -65, -72, -52, -89, -78, -60, -81, -84, -65, -69, -67, -58, -67, -75, -57, -52, -68, -54, -16, -51, -49, -77, -64, -65, -56, -55, -38, -48, +-62, -63, -79, -80, -51, -72, -73, -57, -71, -59, -86, -86, -67, -86, -63, -67, -54, -83, -47, -70, -76, -77, -68, -89, -70, -12, -76, -70, -59, -44, -67, -16, -80, -89, -67, -69, -90, -76, -31, -49, -70, -89, -57, -58, -31, +-48, -59, -86, -83, -84, -31, -40, -88, -30, -43, -84, -59, -40, -68, -59, -80, -85, -67, -69, -73, -65, -57, -78, -80, -96, -82, -82, -71, -87, -76, -67, -85, -79, -74, -72, -87, -78, -58, -36, -64, -75, -97, -75, -90, -77, +-76, -56, -63, -72, -57, -56, -62, -64, -57, -60, -60, -68, -71, -84, -43, -71, -86, -65, -79, -84, -81, -100, -48, -63, -48, -68, -84, -15, -47, -49, -62, -65, -61, -49, -46, -71, -45, -46, -80, -48, -39, -50, -71, -67, -63, +-65, -74, -62, -72, -72, -88, -82, -69, -85, -73, -31, +output Tensor: (int8_t *)(mobilenetv2_quant_B+0) output 1, 1001, output type:DT_INT8, format:NHWC, elementSize: 1001 output Data: - -77, -76, -60, -50, -50, -40, -53, -41, -60, -67, -77, -72, -65, -73, -62, -67, -72, -57, -71, -55, -74, -69, -79, -72, -63, -75, -55, -65, -62, -69, -68, -59, -59, -79, -64, -48, -63, -55, -71, -67, -56, -64, -64, -56, -71, - -59, -72, -36, -81, -63, -73, -61, -84, -64, -68, -75, -61, -77, -71, -64, -61, -46, -82, -73, -83, -85, -62, -75, -68, -54, -61, -56, -45, -60, -50, -68, -53, -72, -52, -67, -49, -73, -57, -71, -79, -51, -61, -72, -81, -60, - -60, -68, -79, -56, -60, -48, -79, -69, -59, -71, -54, -58, -67, -75, -76, -82, -75, -75, -41, -45, -49, -60, -37, -53, -36, -49, -65, -76, -58, -73, -66, -58, -75, -76, -77, -78, -60, -74, -66, -76, -60, -72, -62, -58, -64, - -65, -66, -54, -60, -74, -69, -75, -58, -71, -60, -77, -65, -72, -78, -67, -69, -77, -77, -87, -68, -75, -63, -76, -73, -69, -61, -93, -70, -64, -76, -61, -73, -67, -76, -77, -75, -73, -66, -59, -71, -76, -71, -83, -75, -55, - -67, -69, -75, -69, -66, -58, -63, -74, -72, -77, -72, -59, -69, -74, -65, -76, -61, -64, -80, -83, -58, -64, -61, -65, -49, -65, -77, -72, -66, -59, -65, -70, -69, -69, -72, -91, -77, -80, -76, -72, -53, -82, -76, -80, -76, - -63, -78, -92, -79, -82, -53, -58, -70, -72, -78, -63, -72, -56, -81, -77, -70, -70, -71, -51, -73, -90, -65, -68, -78, -59, -70, -72, -61, -80, -69, -73, -81, -81, -70, -74, -76, -56, -62, -91, -71, -64, -69, -71, -63, -84, - -80, -74, -87, -75, -80, -71, -82, -71, -74, -73, -76, -64, -55, -67, -71, -68, -56, -90, -76, -82, -71, -81, -77, -78, -70, -71, -66, -50, -71, -75, -69, -67, -36, -60, -79, -58, -45, -64, -55, -41, -66, -28, -60, -64, -55, - -63, -49, -53, -49, -55, -65, -60, -75, -69, -76, -63, -74, -69, -44, -49, -65, -62, -59, -78, -66, -76, -69, -76, -76, -75, -80, -68, -80, -86, -86, -68, -75, -77, -77, -79, -76, -84, -85, -72, -67, -71, -89, -70, -69, -75, - -70, -85, -65, -62, -59, -81, -73, -56, -54, -79, -85, -70, -77, -72, -62, -74, -75, -87, -87, -69, -80, -75, -71, -79, -80, -85, -64, -66, -83, -84, -66, -60, -56, -75, -67, -85, -61, -60, -62, -79, -57, -73, -62, -54, -72, - -85, -74, -92, -71, -82, -55, -86, -69, -57, -65, -71, -86, -59, -54, -44, -69, -73, -35, -76, -85, -89, -57, -53, -61, -55, -50, -63, -81, -71, -67, -45, -70, -65, -44, -89, -82, -36, -44, -96, -68, -89, -71, -57, -65, -65, - -75, -98, -89, -78, -68, -74, -48, -78, -59, -55, -62, -54, -73, -53, -37, -80, -79, -79, -76, -64, -70, -68, -82, -57, -73, -53, -58, -88, -96, -59, -52, -58, -59, -67, -58, -59, -58, -76, -86, -36, -56, -42, -72, -70, -78, - -67, -69, -63, -59, -63, -68, -78, -56, -61, -68, -66, -56, -45, -59, -61, -88, -70, -88, -72, -75, -72, -73, -85, -60, -82, -62, -74, -85, -64, -75, -55, -75, -85, -73, -56, -67, -43, -66, -83, -65, -70, -79, -66, -70, -57, - -24, -77, -71, -66, -66, -86, -57, -81, -77, -87, -44, -85, -67, -55, -76, -76, -78, -75, -74, -83, -49, -61, -70, -25, -52, -74, -77, -64, -72, -73, -73, -53, -83, -69, -68, -22, -79, -80, -73, -64, -61, -58, -61, -79, -62, - -98, -55, -91, -52, -74, -74, -82, -56, -70, -70, -73, -60, -53, -84, -76, -51, -51, -78, -62, -82, -48, -68, -57, -58, -59, -73, -71, -68, -92, -72, -67, -72, -38, -69, -62, -32, -63, -63, -37, -65, -76, -84, -28, -88, -67, - -58, -58, -73, -61, -66, -91, -64, -76, -57, -74, -55, -46, -78, -71, -43, -22, -37, -32, -70, -72, -43, -51, -68, -69, -71, -83, -66, -82, -65, -94, -68, -72, -83, -76, -89, -63, -85, -69, -86, -81, -64, -72, -80, -64, -62, - -54, -94, -84, -44, -79, -65, -71, -72, -59, -62, -87, -62, -76, -93, -62, -69, -66, -84, -66, -64, -62, -46, -50, -85, -78, -79, -54, -70, -69, -57, -53, -80, -85, -65, -68, -80, -62, -32, -71, -76, -58, -76, -44, -88, -60, - -68, -58, -57, -49, -53, -75, -63, -98, -72, -53, -82, -50, -63, -72, -53, -68, -49, -57, -45, -63, -92, -73, -53, -74, -82, -64, -69, -57, -53, -72, -43, -56, -92, -71, -62, -91, -74, -64, -75, -84, -65, -58, -69, -91, -63, - -66, -70, -90, -31, -59, -50, -58, -65, -73, -43, -72, -60, -58, -64, -74, -85, -80, -68, -64, -53, -60, -74, -83, -85, -86, -48, -67, -58, -37, -78, -27, -60, -59, -69, -73, -51, -87, -66, -85, -75, -34, -35, -71, -86, -66, - -91, -62, -58, -79, -84, -70, -57, -89, -85, -34, -49, -84, -77, -75, -90, -50, -51, -65, -72, -52, -89, -78, -60, -81, -84, -65, -69, -67, -58, -67, -75, -57, -52, -68, -54, -16, -51, -49, -77, -64, -65, -56, -55, -38, -48, - -62, -63, -79, -80, -51, -72, -73, -57, -71, -59, -86, -86, -67, -86, -63, -67, -54, -83, -47, -70, -76, -77, -68, -89, -70, -12, -76, -70, -59, -44, -67, -16, -80, -89, -67, -69, -90, -76, -31, -49, -70, -89, -57, -58, -31, - -48, -59, -86, -83, -84, -31, -40, -88, -30, -43, -84, -59, -40, -68, -59, -80, -85, -67, -69, -73, -65, -57, -78, -80, -96, -82, -82, -71, -87, -76, -67, -85, -79, -74, -72, -87, -78, -58, -36, -64, -75, -97, -75, -90, -77, - -76, -56, -63, -72, -57, -56, -62, -64, -57, -60, -60, -68, -71, -84, -43, -71, -86, -65, -79, -84, -81, -100, -48, -63, -48, -68, -84, -15, -47, -49, -62, -65, -61, -49, -46, -71, -45, -46, -80, -48, -39, -50, -71, -67, -63, - -65, -74, -62, -72, -72, -88, -82, -69, -85, -73, -31 +-77, -76, -60, -50, -50, -40, -53, -41, -60, -67, -77, -72, -65, -73, -62, -67, -72, -57, -71, -55, -74, -69, -79, -72, -63, -75, -55, -65, -62, -69, -68, -59, -59, -79, -64, -48, -63, -55, -71, -67, -56, -64, -64, -56, -71, +-59, -72, -36, -81, -63, -73, -61, -84, -64, -68, -75, -61, -77, -71, -64, -61, -46, -82, -73, -83, -85, -62, -75, -68, -54, -61, -56, -45, -60, -50, -68, -53, -72, -52, -67, -49, -73, -57, -71, -79, -51, -61, -72, -81, -60, +-60, -68, -79, -56, -60, -48, -79, -69, -59, -71, -54, -58, -67, -75, -76, -82, -75, -75, -41, -45, -49, -60, -37, -53, -36, -49, -65, -76, -58, -73, -66, -58, -75, -76, -77, -78, -60, -74, -66, -76, -60, -72, -62, -58, -64, +-65, -66, -54, -60, -74, -69, -75, -58, -71, -60, -77, -65, -72, -78, -67, -69, -77, -77, -87, -68, -75, -63, -76, -73, -69, -61, -93, -70, -64, -76, -61, -73, -67, -76, -77, -75, -73, -66, -59, -71, -76, -71, -83, -75, -55, +-67, -69, -75, -69, -66, -58, -63, -74, -72, -77, -72, -59, -69, -74, -65, -76, -61, -64, -80, -83, -58, -64, -61, -65, -49, -65, -77, -72, -66, -59, -65, -70, -69, -69, -72, -91, -77, -80, -76, -72, -53, -82, -76, -80, -76, +-63, -78, -92, -79, -82, -53, -58, -70, -72, -78, -63, -72, -56, -81, -77, -70, -70, -71, -51, -73, -90, -65, -68, -78, -59, -70, -72, -61, -80, -69, -73, -81, -81, -70, -74, -76, -56, -62, -91, -71, -64, -69, -71, -63, -84, +-80, -74, -87, -75, -80, -71, -82, -71, -74, -73, -76, -64, -55, -67, -71, -68, -56, -90, -76, -82, -71, -81, -77, -78, -70, -71, -66, -50, -71, -75, -69, -67, -36, -60, -79, -58, -45, -64, -55, -41, -66, -28, -60, -64, -55, +-63, -49, -53, -49, -55, -65, -60, -75, -69, -76, -63, -74, -69, -44, -49, -65, -62, -59, -78, -66, -76, -69, -76, -76, -75, -80, -68, -80, -86, -86, -68, -75, -77, -77, -79, -76, -84, -85, -72, -67, -71, -89, -70, -69, -75, +-70, -85, -65, -62, -59, -81, -73, -56, -54, -79, -85, -70, -77, -72, -62, -74, -75, -87, -87, -69, -80, -75, -71, -79, -80, -85, -64, -66, -83, -84, -66, -60, -56, -75, -67, -85, -61, -60, -62, -79, -57, -73, -62, -54, -72, +-85, -74, -92, -71, -82, -55, -86, -69, -57, -65, -71, -86, -59, -54, -44, -69, -73, -35, -76, -85, -89, -57, -53, -61, -55, -50, -63, -81, -71, -67, -45, -70, -65, -44, -89, -82, -36, -44, -96, -68, -89, -71, -57, -65, -65, +-75, -98, -89, -78, -68, -74, -48, -78, -59, -55, -62, -54, -73, -53, -37, -80, -79, -79, -76, -64, -70, -68, -82, -57, -73, -53, -58, -88, -96, -59, -52, -58, -59, -67, -58, -59, -58, -76, -86, -36, -56, -42, -72, -70, -78, +-67, -69, -63, -59, -63, -68, -78, -56, -61, -68, -66, -56, -45, -59, -61, -88, -70, -88, -72, -75, -72, -73, -85, -60, -82, -62, -74, -85, -64, -75, -55, -75, -85, -73, -56, -67, -43, -66, -83, -65, -70, -79, -66, -70, -57, +-24, -77, -71, -66, -66, -86, -57, -81, -77, -87, -44, -85, -67, -55, -76, -76, -78, -75, -74, -83, -49, -61, -70, -25, -52, -74, -77, -64, -72, -73, -73, -53, -83, -69, -68, -22, -79, -80, -73, -64, -61, -58, -61, -79, -62, +-98, -55, -91, -52, -74, -74, -82, -56, -70, -70, -73, -60, -53, -84, -76, -51, -51, -78, -62, -82, -48, -68, -57, -58, -59, -73, -71, -68, -92, -72, -67, -72, -38, -69, -62, -32, -63, -63, -37, -65, -76, -84, -28, -88, -67, +-58, -58, -73, -61, -66, -91, -64, -76, -57, -74, -55, -46, -78, -71, -43, -22, -37, -32, -70, -72, -43, -51, -68, -69, -71, -83, -66, -82, -65, -94, -68, -72, -83, -76, -89, -63, -85, -69, -86, -81, -64, -72, -80, -64, -62, +-54, -94, -84, -44, -79, -65, -71, -72, -59, -62, -87, -62, -76, -93, -62, -69, -66, -84, -66, -64, -62, -46, -50, -85, -78, -79, -54, -70, -69, -57, -53, -80, -85, -65, -68, -80, -62, -32, -71, -76, -58, -76, -44, -88, -60, +-68, -58, -57, -49, -53, -75, -63, -98, -72, -53, -82, -50, -63, -72, -53, -68, -49, -57, -45, -63, -92, -73, -53, -74, -82, -64, -69, -57, -53, -72, -43, -56, -92, -71, -62, -91, -74, -64, -75, -84, -65, -58, -69, -91, -63, +-66, -70, -90, -31, -59, -50, -58, -65, -73, -43, -72, -60, -58, -64, -74, -85, -80, -68, -64, -53, -60, -74, -83, -85, -86, -48, -67, -58, -37, -78, -27, -60, -59, -69, -73, -51, -87, -66, -85, -75, -34, -35, -71, -86, -66, +-91, -62, -58, -79, -84, -70, -57, -89, -85, -34, -49, -84, -77, -75, -90, -50, -51, -65, -72, -52, -89, -78, -60, -81, -84, -65, -69, -67, -58, -67, -75, -57, -52, -68, -54, -16, -51, -49, -77, -64, -65, -56, -55, -38, -48, +-62, -63, -79, -80, -51, -72, -73, -57, -71, -59, -86, -86, -67, -86, -63, -67, -54, -83, -47, -70, -76, -77, -68, -89, -70, -12, -76, -70, -59, -44, -67, -16, -80, -89, -67, -69, -90, -76, -31, -49, -70, -89, -57, -58, -31, +-48, -59, -86, -83, -84, -31, -40, -88, -30, -43, -84, -59, -40, -68, -59, -80, -85, -67, -69, -73, -65, -57, -78, -80, -96, -82, -82, -71, -87, -76, -67, -85, -79, -74, -72, -87, -78, -58, -36, -64, -75, -97, -75, -90, -77, +-76, -56, -63, -72, -57, -56, -62, -64, -57, -60, -60, -68, -71, -84, -43, -71, -86, -65, -79, -84, -81, -100, -48, -63, -48, -68, -84, -15, -47, -49, -62, -65, -61, -49, -46, -71, -45, -46, -80, -48, -39, -50, -71, -67, -63, +-65, -74, -62, -72, -72, -88, -82, -69, -85, -73, -31, diff --git a/mindspore/lite/micro/example/mobilenetv2_quant/input_1_224_224_3_uint8.bin b/mindspore/lite/micro/example/mobilenetv2_quant/input_1_224_224_3_uint8.bin new file mode 100644 index 0000000000..c451e38634 Binary files /dev/null and b/mindspore/lite/micro/example/mobilenetv2_quant/input_1_224_224_3_uint8.bin differ diff --git a/mindspore/lite/micro/test/CMakeLists.txt b/mindspore/lite/micro/test/CMakeLists.txt index 0862bd70fb..6d1e9c976c 100644 --- a/mindspore/lite/micro/test/CMakeLists.txt +++ b/mindspore/lite/micro/test/CMakeLists.txt @@ -1,14 +1,15 @@ +add_definitions(-DUSE_GLOG) string(REPLACE "/test" "" MICRO_DIR ${CMAKE_CURRENT_SOURCE_DIR}) string(REPLACE " -fvisibility=hidden " " -fvisibility=default " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") string(REPLACE " -fvisibility=hidden " " -fvisibility=default " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -set(LITE_DIR ${MICRO_DIR}/..) -set(NNACL_DIR ${LITE_DIR}/nnacl) -set(3RD_DIR ${TOP_DIR}/third_party) +set(3RD_DIR ${TOP_DIR}/third_party) +set(LITE_DIR ${TOP_DIR}/mindspore/lite) set(BUILD_LITE "on") -include(${MICRO_DIR}/../../../cmake/external_libs/gtest.cmake) + +include(${TOP_DIR}/cmake/external_libs/gtest.cmake) include(${MICRO_DIR}/cmake/file_list.cmake) -include(${MICRO_DIR}/cmake/wrapper.cmake) +include(${MICRO_DIR}/cmake/package_wrapper.cmake) include_directories(${TOP_DIR}) include_directories(${TOP_DIR}/mindspore/core/) @@ -19,4 +20,4 @@ include_directories(${3RD_DIR}) add_executable(micro_test code_gen_test.cc ${FILE_SET}) add_dependencies(micro_test fbs_src) add_dependencies(micro_test fbs_inner_src) -target_link_libraries(micro_test dl mindspore::gtest ${SECUREC_LIBRARY}) +target_link_libraries(micro_test dl mindspore::gtest ${SECUREC_LIBRARY} mindspore::glog) diff --git a/mindspore/lite/micro/wrapper/fp32/matmul_fp32_wrapper.h b/mindspore/lite/micro/wrapper/fp32/matmul_fp32_wrapper.h deleted file mode 100644 index 5852bcb0c4..0000000000 --- a/mindspore/lite/micro/wrapper/fp32/matmul_fp32_wrapper.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_ADAPTER_FP32_MATMUL_FP32_WRAPPER_H_ -#define MINDSPORE_LITE_MICRO_ADAPTER_FP32_MATMUL_FP32_WRAPPER_H_ -#include -#include "nnacl/fp32/matmul_fp32.h" -#ifdef __cplusplus -extern "C" { -#endif - -void InitMatrixA(const float *src_ptr, float *dst_ptr, const MatMulParameter *params_, bool is_vector_a); - -void InitMatrixB(const float *src_ptr, float *dst_ptr, const MatMulParameter *params_, bool is_vector_a); - -#ifdef __cplusplus -} -#endif - -#endif // MINDSPORE_LITE_MICRO_ADAPTER_FP32_MATMUL_FP32_WRAPPER_H_ diff --git a/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.c b/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.c deleted file mode 100644 index a179c668d5..0000000000 --- a/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.c +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "wrapper/int8/add_int8_wrapper.h" -#include "nnacl/errorcode.h" - -int AddBroadcastRun(void *cdata, int task_id) { - AddArgs *args = (AddArgs *)(cdata); - int stride = UP_DIV(args->out_size_, args->thread_count_); - int real_out_count = MSMIN(stride, args->out_size_ - stride * task_id); - if (real_out_count <= 0) { - return NNACL_OK; - } - int8_t *cur_in0 = NULL; - int8_t *cur_in1 = NULL; - int8_t *cur_out = NULL; - for (int i = 0; i < real_out_count; i++) { - if (args->arith_para_->in_elements_num0_ == args->arith_para_->out_elements_num_) { - cur_in0 = args->input0_data_ + task_id * stride * args->in_size_ + i * args->in_size_; - cur_in1 = args->input1_data_; - cur_out = args->output_data_ + task_id * stride * args->in_size_ + i * args->in_size_; - } else { - cur_in0 = args->input0_data_; - cur_in1 = args->input1_data_ + task_id * stride * args->in_size_ + i * args->in_size_; - cur_out = args->output_data_ + task_id * stride * args->in_size_ + i * args->in_size_; - } - AddInt8(cur_in0, cur_in1, cur_out, args->in_size_, &args->para_); - } - return NNACL_OK; -} - -int AddRun(void *cdata, int task_id) { - AddArgs *args = (AddArgs *)(cdata); - /* no need broadcast */ - int stride = UP_DIV(args->elements_num_, args->thread_count_); - int rest_count = args->elements_num_ - task_id * stride; - int real_count = MSMIN(stride, rest_count); - if (real_count <= 0) { - return NNACL_OK; - } - int8_t *cur_in0 = args->input0_data_ + stride * task_id; - int8_t *cur_in1 = args->input1_data_ + stride * task_id; - int8_t *cur_out = args->output_data_ + stride * task_id; - if (args->support_opt_add_) { - int8_t *ptr_in = args->arith_para_->in_elements_num0_ == 1 ? cur_in1 : cur_in0; - int8_t element_in = args->arith_para_->in_elements_num0_ == 1 ? args->input0_data_[0] : args->input1_data_[0]; - AddQuantQrgs *ptr_args = - args->arith_para_->in_elements_num0_ == 1 ? &args->para_.in1_args_ : &args->para_.in0_args_; - AddQuantQrgs *ele_args = - args->arith_para_->in_elements_num0_ == 1 ? &args->para_.in0_args_ : &args->para_.in1_args_; - AddOptInt8(ptr_in, element_in, cur_out, rest_count, &args->para_, ptr_args, ele_args); - } else { - AddInt8(cur_in0, cur_in1, cur_out, rest_count, &args->para_); - } - return NNACL_OK; -} diff --git a/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.h b/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.h deleted file mode 100644 index c23e32e3ae..0000000000 --- a/mindspore/lite/micro/wrapper/int8/add_int8_wrapper.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ -#define MINDSPORE_LITE_MICRO_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ -#include -#include "nnacl/int8/matmul_int8.h" -#include "src/runtime/thread_pool.h" -#include "nnacl/int8/add_int8.h" -#include "nnacl/arithmetic.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - AddQuantParameter para_; - ArithmeticParameter *arith_para_; - int in_size_; - int out_size_; - int thread_count_; - int elements_num_; - bool support_opt_add_; - int8_t *input0_data_; - int8_t *input1_data_; - int8_t *output_data_; -} AddArgs; - -int AddBroadcastRun(void *cdata, int task_id); - -int AddRun(void *cdata, int task_id); - -#ifdef __cplusplus -} -#endif - -#endif // MINDSPORE_LITE_MICRO_WRAPPER_INT8_ADD_INT8_WRAPPER_H_ diff --git a/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.c b/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.c deleted file mode 100644 index 109532eb48..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "wrapper/int8/conv1x1_init_int8_wrapper.h" -#include -#include "nnacl/int8/matmul_int8.h" -#include "nnacl/errorcode.h" - -int Conv1x1Init(int8_t *src_weight, int32_t *src_bias, int32_t *filter_zps, int32_t input_channel, - int32_t output_channel, int32_t input_zp, bool support_optimize, bool filter_peroc, - int8_t **packed_weight, int32_t **bias_data) { - if (packed_weight == NULL || bias_data == NULL) { - return NNACL_ERR; - } -#ifdef ENABLE_ARM32 - /* InitWeightBiasArm32 */ - /* weight */ - size_t size = UP_ROUND(input_channel, C16NUM) * UP_ROUND(output_channel, C2NUM) * sizeof(int8_t); - int8_t *packed_weight_ = (int8_t *)(malloc(size)); - if (packed_weight_ == NULL) { - return NNACL_ERR; - } - memset(packed_weight_, 0, size); - RowMajor2Row2x16MajorInt8(src_weight, packed_weight_, output_channel, input_channel); - /* bias */ - size = UP_ROUND(output_channel, C2NUM); - int32_t *bias_data_ = (int32_t *)malloc(size * sizeof(int32_t)); - if (bias_data_ == NULL) { - free(packed_weight_); - return NNACL_ERR; - } - memset(bias_data_, 0, size * sizeof(int32_t)); - if (src_bias != NULL) { - memcpy(bias_data_, src_bias, output_channel * sizeof(int32_t)); - } -#else - /* InitWeightBias */ - /* weight */ - size_t size = support_optimize ? UP_ROUND(input_channel, C4NUM) * UP_ROUND(output_channel, C16NUM) * sizeof(int8_t) - : UP_ROUND(input_channel, C16NUM) * UP_ROUND(output_channel, C4NUM) * sizeof(int8_t); - int8_t *packed_weight_ = (int8_t *)(malloc(size)); - if (packed_weight_ == NULL) { - return NNACL_ERR; - } - memset(packed_weight_, 0, size); - if (support_optimize) { - RowMajor2Row4x16MajorInt8(src_weight, packed_weight_, output_channel, input_channel); - } else { - RowMajor2Row16x4MajorInt8(src_weight, packed_weight_, output_channel, input_channel); - } - /* bias */ - size = support_optimize ? UP_ROUND(output_channel, C16NUM) : UP_ROUND(output_channel, C4NUM); - int32_t *bias_data_ = (int32_t *)malloc(size * sizeof(int32_t)); - if (bias_data_ == NULL) { - free(packed_weight_); - return NNACL_ERR; - } - memset(bias_data_, 0, size * sizeof(int32_t)); - if (src_bias != NULL) { - memcpy(bias_data_, src_bias, output_channel * sizeof(int32_t)); - } -#endif - /* InitBiasByzp */ - /* bias = bias - v2 x zp1 + zp1 x zp2 */ - for (int oc = 0; oc < output_channel; oc++) { - int32_t weight_sum_value = 0; - int32_t filter_zp = (filter_peroc) ? filter_zps[oc] : filter_zps[0]; - for (int ic = 0; ic < input_channel; ic++) { - weight_sum_value += src_weight[oc * input_channel + ic]; - } - bias_data_[oc] += filter_zp * input_zp * input_channel - weight_sum_value * input_zp; - } - - *packed_weight = packed_weight_; - *bias_data = bias_data_; - return NNACL_OK; -} diff --git a/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.h b/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.h deleted file mode 100644 index 462574366d..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv1x1_init_int8_wrapper.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_INT8_CONV1X1_INIT_INT8_H_ -#define MINDSPORE_LITE_MICRO_INT8_CONV1X1_INIT_INT8_H_ - -#include -#include -#include "nnacl/conv_parameter.h" - -int Conv1x1Init(int8_t *src_weight, int32_t *src_bias, int32_t *filter_zps, int32_t input_channel, - int32_t output_channel, int32_t input_zp, bool support_optimize, bool filter_peroc, - int8_t **packed_weight, int32_t **bias_data); - -#endif // MINDSPORE_LITE_MICRO_INT8_CONV1X1_INIT_INT8_H_ diff --git a/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.c b/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.c deleted file mode 100644 index ddaa1ecee8..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.c +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "wrapper/int8/conv1x1_run_int8_wrapper.h" -#include "nnacl/base/conv1x1_base.h" -#include "nnacl/int8/matmul_int8.h" -#include "nnacl/int8/pack_int8.h" -#include "nnacl/int8/conv1x1_int8.h" -#include "nnacl/errorcode.h" - -void Pre1x1Trans(Conv1x1Args *args, int8_t *src_input, int8_t *src_output) { - args->output_ptr_ = src_output; - if (args->pre_trans_input_) { - Conv1x1InputPack(src_input, args->input_ptr_, args->conv_param_, sizeof(int8_t)); - } else { - args->input_ptr_ = src_input; - } -} - -int OcOptPre(void *cdata, int task_id) { - Conv1x1Args *args = (Conv1x1Args *)(cdata); - int cur_stride = args->thread_stride_hw_ * C4NUM; - int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; - int cur_hw = MSMIN(cur_stride, res_stride); - if (cur_hw <= 0) { - return NNACL_OK; - } - int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; - int8_t *hw_packed_in = args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_4_; - int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; - - if (args->filter_peroc_) { - PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, 1); - } else { - PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, - args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_); - } - return NNACL_OK; -} - -int RunArm64OptOc(void *cdata, int task_id) { - Conv1x1Args *args = (Conv1x1Args *)(cdata); - int stride = args->thread_stride_oc_ * C16NUM; - int cur_stride = task_id * stride; - int res_stride = args->matmul_param_->col_ - cur_stride; - int cur_oc = MSMIN(stride, res_stride); - if (cur_oc <= 0) { - return NNACL_OK; - } - - bool filter_peroc = args->filter_peroc_; - int32_t *cur_left_shift = - filter_peroc ? args->left_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.left_shift_; - int32_t *cur_right_shift = - filter_peroc ? args->right_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.right_shift_; - int32_t *cur_multiplier = - filter_peroc ? args->multiplier_ + cur_stride : args->conv_param_->conv_quant_arg_.quant_multiplier_; - int32_t *cur_zp = filter_peroc ? args->filter_zp_ptr_ + cur_stride : args->filter_zp_ptr_; - - Conv1x1Int8Opt(args->packed_input_, args->packed_weight_ + cur_stride * args->matmul_param_->deep_4_, - args->output_ptr_ + cur_stride, args->input_sum_, args->bias_data_ + cur_stride, - args->matmul_param_->row_, cur_oc, args->matmul_param_->deep_4_, cur_left_shift, cur_right_shift, - cur_multiplier, args->conv_param_, args->matmul_func_, cur_zp); - return NNACL_OK; -} - -int RunArmOc(void *cdata, int task_id) { - Conv1x1Args *args = (Conv1x1Args *)(cdata); -#ifdef ENABLE_ARM32 - int col_tile = C2NUM; -#else - int col_tile = C4NUM; -#endif - int stride = args->thread_stride_oc_ * col_tile; - int cur_stride = task_id * stride; - int res_stride = args->matmul_param_->col_ - cur_stride; - int cur_oc = MSMIN(stride, res_stride); - if (cur_oc <= 0) { - return NNACL_OK; - } - - bool filter_peroc = args->filter_peroc_; - int32_t *cur_left_shift = - filter_peroc ? args->left_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.left_shift_; - int32_t *cur_right_shift = - filter_peroc ? args->right_shift_ + cur_stride : args->conv_param_->conv_quant_arg_.right_shift_; - int32_t *cur_multiplier = - filter_peroc ? args->multiplier_ + cur_stride : args->conv_param_->conv_quant_arg_.quant_multiplier_; - int32_t *cur_zp = filter_peroc ? args->filter_zp_ptr_ + cur_stride : args->filter_zp_ptr_; - - Conv1x1Int8(args->packed_input_, args->packed_weight_ + cur_stride * args->matmul_param_->deep_16_, - args->output_ptr_ + cur_stride, args->input_sum_, args->bias_data_ + cur_stride, - args->matmul_param_->row_, cur_oc, args->matmul_param_->deep_16_, cur_left_shift, cur_right_shift, - cur_multiplier, args->conv_param_, cur_zp); - return NNACL_OK; -} - -int RunArm64OptHw(void *cdata, int task_id) { - Conv1x1Args *args = (Conv1x1Args *)(cdata); - int cur_stride = args->thread_stride_hw_ * C4NUM; - int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; - int cur_hw = MSMIN(cur_stride, res_stride); - if (cur_hw <= 0) { - return NNACL_OK; - } - int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; - int8_t *hw_out = args->output_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->output_channel_; - int8_t *hw_packed_in = args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_4_; - int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; - - if (args->filter_peroc_) { - PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, 1); - } else { - PackInput4x4AndInputSumPert(hw_in, hw_packed_in, hw_input_sum, args->matmul_param_->deep_, cur_hw, - args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_); - } - - Conv1x1Int8Opt(hw_packed_in, args->packed_weight_, hw_out, hw_input_sum, args->bias_data_, cur_hw, - args->matmul_param_->col_, args->matmul_param_->deep_4_, args->left_shift_, args->right_shift_, - args->multiplier_, args->conv_param_, args->matmul_func_, args->filter_zp_ptr_); - return NNACL_OK; -} - -int RunArmHw(void *cdata, int task_id) { - Conv1x1Args *args = (Conv1x1Args *)(cdata); - int cur_stride = args->thread_stride_hw_ * C4NUM; - int res_stride = args->matmul_param_->row_ - task_id * args->thread_stride_hw_ * C4NUM; - int cur_hw = MSMIN(cur_stride, res_stride); - if (cur_hw <= 0) { - return NNACL_OK; - } - - int8_t *hw_in = args->input_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->input_channel_; - int8_t *hw_out = args->output_ptr_ + task_id * args->thread_stride_hw_ * C4NUM * args->conv_param_->output_channel_; - int8_t *hw_packed_in = - args->packed_input_ + task_id * args->thread_stride_hw_ * C4NUM * args->matmul_param_->deep_16_; - int32_t *hw_input_sum = args->input_sum_ + task_id * args->thread_stride_hw_ * C4NUM; - - RowMajor2Row16x4MajorInt8(hw_in, hw_packed_in, cur_hw, args->matmul_param_->deep_); - - if (args->filter_peroc_) { - PackInputSum16x4PerLayer(hw_packed_in, hw_input_sum, 1, UP_ROUND(cur_hw, C4NUM), args->matmul_param_->deep_16_); - } else { - PackInputSum16x4PerLayer(hw_packed_in, hw_input_sum, args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_, - UP_ROUND(cur_hw, C4NUM), args->matmul_param_->deep_16_); - } - - Conv1x1Int8(hw_packed_in, args->packed_weight_, hw_out, hw_input_sum, args->bias_data_, cur_hw, - args->matmul_param_->col_, args->matmul_param_->deep_16_, args->left_shift_, args->right_shift_, - args->multiplier_, args->conv_param_, args->filter_zp_ptr_); - return NNACL_OK; -} - -void Conv1x1Run(int8_t *src_in, Conv1x1Args *args, struct ThreadPool *thread_pool, int thread_num, int8_t *src_out) { - int row_pack_count = C4NUM; - int col_pack_count; - -#ifdef ENABLE_ARM32 - col_pack_count = C2NUM; -#else - if (args->support_optimize_) { - col_pack_count = C16NUM; - } else { - col_pack_count = C4NUM; - } -#endif - int hw_thread_count = UP_DIV(args->matmul_param_->row_, row_pack_count); - int oc_thread_count = UP_DIV(args->matmul_param_->col_, col_pack_count); - size_t thread_count_hw = MSMIN(thread_num, hw_thread_count); - args->thread_stride_hw_ = UP_DIV(hw_thread_count, thread_count_hw); - size_t thread_count_oc = MSMIN(thread_num, oc_thread_count); - args->thread_stride_oc_ = UP_DIV(oc_thread_count, thread_count_oc); - bool parallel_by_oc = oc_thread_count > thread_num; - - for (int batch_index = 0; batch_index < args->conv_param_->input_batch_; batch_index++) { - Pre1x1Trans(args, - src_in + batch_index * args->conv_param_->input_h_ * args->conv_param_->input_w_ * - args->conv_param_->input_channel_, - src_out + batch_index * args->matmul_param_->row_ * args->matmul_param_->col_); - if (parallel_by_oc) { - /* input transpose and input sum */ - if (args->support_optimize_) { - ParallelLaunch(thread_pool, OcOptPre, args, thread_count_hw); - } else { - RowMajor2Row16x4MajorInt8(args->input_ptr_, args->packed_input_, args->matmul_param_->row_, - args->matmul_param_->deep_); - if (args->filter_peroc_) { - PackInputSum16x4PerLayer(args->packed_input_, args->input_sum_, 1, args->matmul_param_->row_4_, - args->matmul_param_->deep_16_); - } else { - PackInputSum16x4PerLayer(args->packed_input_, args->input_sum_, - args->conv_param_->conv_quant_arg_.filter_quant_args_[0].zp_, - args->matmul_param_->row_4_, args->matmul_param_->deep_16_); - } - } - /* matmul parallel by oc */ - if (args->support_optimize_) { - ParallelLaunch(thread_pool, RunArm64OptOc, args, thread_count_oc); - } else { - ParallelLaunch(thread_pool, RunArmOc, args, thread_count_oc); - } - } else { - /* matmul parallel by hw */ - if (args->support_optimize_) { - ParallelLaunch(thread_pool, RunArm64OptHw, args, thread_count_hw); - } else { - ParallelLaunch(thread_pool, RunArmHw, args, thread_count_hw); - } - } - } -} diff --git a/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.h b/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.h deleted file mode 100644 index 10c2366009..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv1x1_run_int8_wrapper.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_INT8_CONV1X1_RUN_H_ -#define MINDSPORE_LITE_MICRO_INT8_CONV1X1_RUN_H_ - -#include -#include -#include "nnacl/conv_parameter.h" -#include "nnacl/matmul_parameter.h" -#include "src/runtime/thread_pool.h" - -typedef struct { - int32_t *input_sum_; /* per-oc */ - int32_t *filter_zp_ptr_; /* per-oc up round */ - int32_t *left_shift_; /* per-oc up round */ - int32_t *right_shift_; /* per-oc up round */ - int32_t *multiplier_; /* per-oc up round */ - int8_t *packed_weight_; - int32_t *bias_data_; - int8_t *packed_input_; - int8_t *input_ptr_; - int8_t *output_ptr_; - size_t thread_stride_hw_; - size_t thread_stride_oc_; - ConvParameter *conv_param_; - MatMulParameter *matmul_param_; - MATMUL_OPT_DP_FUNC matmul_func_; - bool pre_trans_input_; - bool support_optimize_; - bool filter_peroc_; -} Conv1x1Args; - -void Conv1x1Run(int8_t *src_in, Conv1x1Args *args, struct ThreadPool *thread_pool, int thread_num, int8_t *src_out); - -#endif // MINDSPORE_LITE_MICRO_INT8_CONV1X1_RUN_H_ diff --git a/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.c b/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.c deleted file mode 100644 index 71e8db0fee..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "wrapper/int8/conv_init_int8_wrapper.h" -#include -#include "nnacl/op_base.h" -#include "nnacl/int8/matmul_int8.h" -#include "nnacl/errorcode.h" - -int ConvInit(int8_t *origin_weight, const int32_t *ori_bias, const int32_t *filter_quant_zps, int kernel_h, - int kernel_w, int input_channel, int output_channel, int32_t input_zp, bool filter_peroc, - bool support_optimize, int8_t **packed_weight, int32_t **bias_data) { - int8_t *packed_weight_ = NULL; - int32_t *bias_data_ = NULL; - int kernel_plane = kernel_h * kernel_w; - int up_round_deep; - int up_round_oc; -#ifdef ENABLE_ARM32 - up_round_oc = UP_ROUND(output_channel, C2NUM); - up_round_deep = UP_ROUND(kernel_plane * input_channel, C16NUM); -#else - if (support_optimize) { - up_round_oc = UP_ROUND(output_channel, C8NUM); - up_round_deep = UP_ROUND(kernel_plane * input_channel, C4NUM); - } else { - up_round_oc = UP_ROUND(output_channel, C4NUM); - up_round_deep = UP_ROUND(kernel_plane * input_channel, C16NUM); - } -#endif - int pack_weight_size = up_round_oc * up_round_deep; - size_t bias_size = up_round_oc * sizeof(int32_t); - - // init weight - packed_weight_ = (int8_t *)(malloc(pack_weight_size)); - if (packed_weight_ == NULL) { - return NNACL_ERR; - } - memset(packed_weight_, 0, pack_weight_size); -#ifdef ENABLE_ARM32 - RowMajor2Row2x16MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); -#else - if (support_optimize) { - RowMajor2Row8x4MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); - } else { - RowMajor2Row16x4MajorInt8(origin_weight, packed_weight_, output_channel, input_channel * kernel_plane); - } -#endif - - // init bias - bias_data_ = (int32_t *)(malloc(bias_size)); - if (bias_data_ == NULL) { - free(packed_weight_); - return NNACL_ERR; - } - memset(bias_data_, 0, bias_size); - if (ori_bias != NULL) { - memcpy(bias_data_, ori_bias, output_channel * sizeof(int32_t)); - } - - for (int oc = 0; oc < output_channel; oc++) { - int32_t filter_zp = filter_quant_zps[0]; - if (filter_peroc) { - filter_zp = filter_quant_zps[oc]; - } - int32_t weight_sum_value = up_round_deep * filter_zp; - for (int i = 0; i < kernel_plane * input_channel; i++) { - weight_sum_value += origin_weight[oc * kernel_plane * input_channel + i] - filter_zp; - } - bias_data_[oc] += filter_zp * input_zp * up_round_deep - weight_sum_value * input_zp; - } - - *packed_weight = packed_weight_; - *bias_data = bias_data_; - return NNACL_OK; -} diff --git a/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.h b/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.h deleted file mode 100644 index 0eff8d7a3f..0000000000 --- a/mindspore/lite/micro/wrapper/int8/conv_init_int8_wrapper.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef MINDSPORE_LITE_MICRO_INT8_CONV_INIT_H_ -#define MINDSPORE_LITE_MICRO_INT8_CONV_INIT_H_ - -#include -#include - -int ConvInit(int8_t *origin_weight, const int32_t *ori_bias, const int32_t *filter_quant_zps, int kernel_h, - int kernel_w, int input_channel, int output_channel, int32_t input_zp, bool filter_peroc, - bool support_optimize, int8_t **packed_weight, int32_t **bias_data); - -#endif // MINDSPORE_LITE_MICRO_INT8_CONV_INIT_H_ diff --git a/mindspore/lite/micro/wrapper/int8/matmul_int8_wrapper.h b/mindspore/lite/micro/wrapper/int8/matmul_int8_wrapper.h deleted file mode 100644 index 27a64007a6..0000000000 --- a/mindspore/lite/micro/wrapper/int8/matmul_int8_wrapper.h +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_MICRO_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ -#define MINDSPORE_LITE_MICRO_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ -#include -#include "nnacl/int8/matmul_int8.h" -#ifdef __cplusplus -extern "C" { -#endif - -void InitInt8MatrixA(int8_t *src_ptr, int32_t *input_sums, int8_t *dst_ptr, int batch, int row, int deep, int input_zp, - const int *weight_zp, bool a_transpose); - -void InitInt8MatrixB(int8_t *src_ptr, int32_t *weight_bias_sums_batch_, int8_t *dst_ptr, int batch, int deep, int col, - int col_4, int deep_16, int input_zp, int *weight_zp, const int *bias_ptr, bool b_transpose); - -#ifdef __cplusplus -} -#endif - -#endif // MINDSPORE_LITE_MICRO_WRAPPER_INT8_MATMUL_INT8_WRAPPER_H_ diff --git a/mindspore/lite/nnacl/CMakeLists.txt b/mindspore/lite/nnacl/CMakeLists.txt index fcc6558ed7..3f7da15e73 100644 --- a/mindspore/lite/nnacl/CMakeLists.txt +++ b/mindspore/lite/nnacl/CMakeLists.txt @@ -21,6 +21,7 @@ file(GLOB KERNEL_SRC ${NNACL_DIR}/*.c ${NNACL_DIR}/fp32/*.c ${NNACL_DIR}/int8/*.c + ${NNACL_DIR}/infer/*.c ${NNACL_DIR}/base/*.c ) diff --git a/mindspore/lite/nnacl/arithmetic.h b/mindspore/lite/nnacl/arithmetic.h index d4f6a11c4c..198a2b6fce 100644 --- a/mindspore/lite/nnacl/arithmetic.h +++ b/mindspore/lite/nnacl/arithmetic.h @@ -40,6 +40,7 @@ typedef struct ArithmeticParameter { int multiples0_[10]; int multiples1_[10]; + int eltwise_mode_; // eltwise need } ArithmeticParameter; #endif // MINDSPORE_LITE_NNACL_ARTITHMETIC_H_ diff --git a/mindspore/lite/nnacl/base/tile_base.h b/mindspore/lite/nnacl/base/tile_base.h index 679ea3d486..7a499a3aa7 100644 --- a/mindspore/lite/nnacl/base/tile_base.h +++ b/mindspore/lite/nnacl/base/tile_base.h @@ -24,6 +24,8 @@ typedef struct TileParameter { OpParameter op_parameter_; int multiples_[5]; int dims_[5]; + size_t dims_size_; + size_t multiples_size_; // shape correlative int in_shape_[5]; diff --git a/mindspore/lite/nnacl/concat_parameter.h b/mindspore/lite/nnacl/concat_parameter.h index 8b22e93468..35386464e5 100644 --- a/mindspore/lite/nnacl/concat_parameter.h +++ b/mindspore/lite/nnacl/concat_parameter.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_NNACL_CONCAT_PARAMETER_H_ #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" typedef struct ConcatParameter { OpParameter op_parameter_; diff --git a/mindspore/lite/nnacl/conv_parameter.h b/mindspore/lite/nnacl/conv_parameter.h index 174240b894..95934df573 100644 --- a/mindspore/lite/nnacl/conv_parameter.h +++ b/mindspore/lite/nnacl/conv_parameter.h @@ -21,7 +21,7 @@ #include #endif #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" typedef struct ConvParameter { OpParameter op_parameter_; @@ -51,6 +51,9 @@ typedef struct ConvParameter { int output_unit_; PadMode pad_mode_; ActType act_type_; + int channel_multiplie_; + int output_padding_w; + int output_padding_h; } ConvParameter; typedef struct SlidingWindowParam { diff --git a/mindspore/lite/nnacl/crop_parameter.h b/mindspore/lite/nnacl/crop_parameter.h index 4ab5c58303..6730a0be6e 100644 --- a/mindspore/lite/nnacl/crop_parameter.h +++ b/mindspore/lite/nnacl/crop_parameter.h @@ -18,7 +18,7 @@ #define MINDSPORE_LITE_NNACL_CROP_PARAMETER_H_ #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" typedef struct CropParameter { OpParameter op_parameter_; diff --git a/mindspore/lite/nnacl/errorcode.h b/mindspore/lite/nnacl/errorcode.h index 50d7d76bce..18a50290cc 100644 --- a/mindspore/lite/nnacl/errorcode.h +++ b/mindspore/lite/nnacl/errorcode.h @@ -22,6 +22,8 @@ typedef enum ErrorCodeCommonEnum { NNACL_ERR = 1, NNACL_NULL_PTR, NNACL_PARAM_INVALID, + NNACL_INFER_INVALID, + NNACL_INPUT_TENSOR_ERROR, NNACL_COMMON_END = 9999 } ErrorCodeCommonEnum; diff --git a/mindspore/lite/nnacl/fp16/activation_fp16.h b/mindspore/lite/nnacl/fp16/activation_fp16.h index 3e8a2309f5..6fdb3220a1 100644 --- a/mindspore/lite/nnacl/fp16/activation_fp16.h +++ b/mindspore/lite/nnacl/fp16/activation_fp16.h @@ -21,7 +21,7 @@ #endif #include #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/fixed_point.h" +#include "nnacl/int8/fixed_point.h" #ifdef __cplusplus extern "C" { diff --git a/mindspore/lite/nnacl/fp16/lstm_fp16.c b/mindspore/lite/nnacl/fp16/lstm_fp16.c index 8ffd6e174d..ac8071600e 100644 --- a/mindspore/lite/nnacl/fp16/lstm_fp16.c +++ b/mindspore/lite/nnacl/fp16/lstm_fp16.c @@ -93,39 +93,38 @@ int ElementOptMulAccFp16(const float16_t *input0, const float16_t input1, float1 void UpdataStateFp16(float16_t *cell_state, float16_t *forget_gate, const float16_t *input_gate, const float16_t *cell_gate, float16_t *state_buffer, int batch, int hidden_size, - float16_t smooth) { - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { // smooth * old_cell_state + float16_t zoneout) { + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { // zoneout * old_cell_state memcpy(state_buffer, cell_state, batch * hidden_size * sizeof(float16_t)); ArithmeticParameter parameter; parameter.in_elements_num0_ = batch * hidden_size; parameter.in_elements_num1_ = 1; - ElementOptMulFp16(state_buffer, &smooth, state_buffer, batch * hidden_size, ¶meter); + ElementOptMulFp16(state_buffer, &zoneout, state_buffer, batch * hidden_size, ¶meter); } ElementMulFp16(forget_gate, cell_state, cell_state, batch * hidden_size); ElementMulAccFp16(input_gate, cell_gate, cell_state, batch * hidden_size); - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { // (1 - smooth) * new_cell_state - ElementOptMulAccFp16(cell_state, 1 - smooth, state_buffer, batch * hidden_size); + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { // (1 - zoneout) * new_cell_state + ElementOptMulAccFp16(cell_state, 1 - zoneout, state_buffer, batch * hidden_size); } } void UpdataOutputFp16(const float16_t *cell_state, float16_t *output_gate, float16_t *hidden_state, - float16_t *state_buffer_in, int batch, int hidden_size, float16_t smooth) { - float16_t *state_buffer = state_buffer_in + batch * hidden_size; - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { + float16_t *state_buffer, int batch, int hidden_size, float16_t zoneout) { + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { memcpy(state_buffer, hidden_state, batch * hidden_size * sizeof(float16_t)); ArithmeticParameter parameter; parameter.in_elements_num0_ = batch * hidden_size; parameter.in_elements_num1_ = 1; - ElementOptMulFp16(state_buffer, &smooth, state_buffer, batch * hidden_size, ¶meter); + ElementOptMulFp16(state_buffer, &zoneout, state_buffer, batch * hidden_size, ¶meter); } TanhFp16(cell_state, hidden_state, batch * hidden_size); ElementMulFp16(hidden_state, output_gate, hidden_state, batch * hidden_size); - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { - ElementOptMulAccFp16(hidden_state, 1 - smooth, state_buffer, batch * hidden_size); + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { + ElementOptMulAccFp16(hidden_state, 1 - zoneout, state_buffer, batch * hidden_size); } } @@ -151,7 +150,7 @@ void UpdateLstmGateFp16(float16_t *gate_buffer, const float16_t *input, const fl void LstmStepUnitFp16(float16_t *output, const float16_t *input, const float16_t *input_weight, const float16_t *state_weight, const float16_t *bias, float16_t *hidden_state, - float16_t *cell_state, float16_t *gate_buffer, float16_t *state_buffer, + float16_t *cell_state, float16_t *gate_buffer, float16_t *state_buffer[2], float16_t *matmul_buffer[2], const LstmParameter *lstm_param) { bool is_vec = lstm_param->batch_ == 1; // input * weight @@ -192,26 +191,28 @@ void LstmStepUnitFp16(float16_t *output, const float16_t *input, const float16_t // update cell_gate TanhFp16(cell_gate, cell_gate, lstm_param->batch_ * lstm_param->hidden_size_); // update cell state - UpdataStateFp16(cell_state, forget_gate, input_gate, cell_gate, state_buffer, lstm_param->batch_, - lstm_param->hidden_size_, lstm_param->smooth_); + UpdataStateFp16(cell_state, forget_gate, input_gate, cell_gate, state_buffer[0], lstm_param->batch_, + lstm_param->hidden_size_, lstm_param->zoneout_cell_); // update output_gate SigmoidFp16(output_gate, output_gate, lstm_param->batch_ * lstm_param->hidden_size_); // update output - UpdataOutputFp16(cell_state, output_gate, hidden_state, state_buffer, lstm_param->batch_, lstm_param->hidden_size_, - lstm_param->smooth_); + UpdataOutputFp16(cell_state, output_gate, hidden_state, state_buffer[1], lstm_param->batch_, lstm_param->hidden_size_, + lstm_param->zoneout_hidden_); memcpy(output, hidden_state, lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float16_t)); - if (!(lstm_param->smooth_ >= -FLT_EPSILON && lstm_param->smooth_ <= FLT_EPSILON)) { - memcpy(cell_state, state_buffer, lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float16_t)); - memcpy(hidden_state, state_buffer + lstm_param->batch_ * lstm_param->hidden_size_, - lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float16_t)); + if (!(lstm_param->zoneout_cell_ >= -FLT_EPSILON && lstm_param->zoneout_cell_ <= FLT_EPSILON)) { + memcpy(cell_state, state_buffer[0], lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float16_t)); + } + + if (!(lstm_param->zoneout_hidden_ >= -FLT_EPSILON && lstm_param->zoneout_hidden_ <= FLT_EPSILON)) { + memcpy(hidden_state, state_buffer[1], lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float16_t)); } } void LstmFp16(float16_t *output, const float16_t *input, const float16_t *weight_i, const float16_t *weight_h, const float16_t *bias, float16_t *hidden_state, float16_t *cell_state, float16_t *gate_buffer, - float16_t *state_buffer, float16_t *matmul_buffer[2], const LstmParameter *lstm_param) { + float16_t *state_buffer[2], float16_t *matmul_buffer[2], const LstmParameter *lstm_param) { // forward for (int t = 0; t < lstm_param->seq_len_; t++) { const float16_t *input_ptr = input + t * lstm_param->input_step_; diff --git a/mindspore/lite/nnacl/fp16/lstm_fp16.h b/mindspore/lite/nnacl/fp16/lstm_fp16.h index b5bf7ad2a9..e3aae4326b 100644 --- a/mindspore/lite/nnacl/fp16/lstm_fp16.h +++ b/mindspore/lite/nnacl/fp16/lstm_fp16.h @@ -37,7 +37,7 @@ int ElementOptMulAccFp16(const float16_t *input0, const float16_t input1, float1 void LstmFp16(float16_t *output, const float16_t *input, const float16_t *weight_i, const float16_t *weight_h, const float16_t *bias, float16_t *hidden_state, float16_t *cell_state, float16_t *gate_buffer, - float16_t *state_buffer, float16_t *matmul_buffer[2], const LstmParameter *lstm_param); + float16_t *state_buffer[2], float16_t *matmul_buffer[2], const LstmParameter *lstm_param); #ifdef __cplusplus } #endif diff --git a/mindspore/lite/nnacl/fp16_grad/activation_grad.c b/mindspore/lite/nnacl/fp16_grad/activation_grad.c index a9406e0262..da48f5253b 100644 --- a/mindspore/lite/nnacl/fp16_grad/activation_grad.c +++ b/mindspore/lite/nnacl/fp16_grad/activation_grad.c @@ -53,20 +53,3 @@ int Fp16SigmoidGrad(const float16_t *src0, const float16_t *src1, size_t length, } return NNACL_OK; } - -int Fp16LogGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst) { - int i = 0; -#ifdef ENABLE_NEON - float16x8_t log_10 = vdupq_n_f16(log(10)); - for (; i < length - 4; i += 4) { - float16x8_t src0_4 = vld1q_f16(src0 + i); - float16x8_t src1_4 = vld1q_f16(src1 + i); - float16x8_t dst_4 = vmulq_f16(src0_4, vrecpeq_f16(vmulq_f16(src1_4, log_10))); - vst1q_f16(dst + i, dst_4); - } -#endif - for (; i < length; i++) { - dst[i] = src0[i] * 1.0f / (src1[i] * log(10)); - } - return NNACL_OK; -} diff --git a/mindspore/lite/nnacl/fp16_grad/activation_grad.h b/mindspore/lite/nnacl/fp16_grad/activation_grad.h index 985708bb15..79c53584b7 100644 --- a/mindspore/lite/nnacl/fp16_grad/activation_grad.h +++ b/mindspore/lite/nnacl/fp16_grad/activation_grad.h @@ -21,7 +21,7 @@ #endif #include #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/fixed_point.h" +#include "nnacl/int8/fixed_point.h" typedef struct ActivationGradParameterFp16 { OpParameter op_parameter; @@ -34,7 +34,6 @@ extern "C" { int Fp16ReluGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst); int Fp16SigmoidGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst); -int Fp16LogGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst); #ifdef __cplusplus } diff --git a/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.c b/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.c new file mode 100644 index 0000000000..7cd7b4d7e5 --- /dev/null +++ b/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.c @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 +#include "nnacl/op_base.h" +#include "nnacl/fp16_grad/arithmetic_self_grad.h" +#include "nnacl/errorcode.h" + +int Fp16LogGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst) { + int i = 0; +#ifdef ENABLE_NEON + float16x8_t log_10 = vdupq_n_f16(log(10)); + for (; i < length - 4; i += 4) { + float16x8_t src0_4 = vld1q_f16(src0 + i); + float16x8_t src1_4 = vld1q_f16(src1 + i); + float16x8_t dst_4 = vmulq_f16(src0_4, vrecpeq_f16(vmulq_f16(src1_4, log_10))); + vst1q_f16(dst + i, dst_4); + } +#endif + for (; i < length; i++) { + dst[i] = src0[i] * 1.0f / (src1[i] * log(10)); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.h b/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.h new file mode 100644 index 0000000000..3d894581ac --- /dev/null +++ b/mindspore/lite/nnacl/fp16_grad/arithmetic_self_grad.h @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FP16_GRAD_ARITHMETHIC_SELF_GRAD_H_ +#define MINDSPORE_LITE_NNACL_FP16_GRAD_ARITHMETHIC_SELF_GRAD_H_ + +#ifdef ENABLE_NEON +#include +#endif +#include +#include "nnacl/op_base.h" + +typedef struct ArithmeticSelfGradParameterFp16 { + OpParameter op_parameter; + int type_; +} ArithmeticSelfGradParameterFp16; +#ifdef __cplusplus +extern "C" { +#endif + +int Fp16LogGrad(const float16_t *src0, const float16_t *src1, size_t length, float16_t *dst); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_NNACL_FP16_GRAD_ARITHMETHIC_SELF_GRAD_H_ diff --git a/mindspore/lite/nnacl/fp32/activation_fp32.h b/mindspore/lite/nnacl/fp32/activation_fp32.h index afae986998..c682ec2b3b 100644 --- a/mindspore/lite/nnacl/fp32/activation_fp32.h +++ b/mindspore/lite/nnacl/fp32/activation_fp32.h @@ -18,7 +18,7 @@ #include #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/fixed_point.h" +#include "nnacl/int8/fixed_point.h" typedef struct ActivationParameter { OpParameter op_parameter_; diff --git a/mindspore/lite/nnacl/fp32/layer_norm_fp32.c b/mindspore/lite/nnacl/fp32/layer_norm_fp32.c index d99f303f37..c05fda2ea1 100644 --- a/mindspore/lite/nnacl/fp32/layer_norm_fp32.c +++ b/mindspore/lite/nnacl/fp32/layer_norm_fp32.c @@ -87,12 +87,10 @@ int LayerNorm(const float *src_data, const float *gamma_data, const float *beta_ LayerNormGammaAndBeta(dst_param, src_param, gamma_data, beta_data, param->params_inner_size_, mean, deno); } } else { - int x = i / param->norm_outer_size_; - const float *src_param = src_norm + x * param->params_inner_size_; - float *dst_param = dst_norm + x * param->params_inner_size_; - const float *gamma = gamma_data + x * param->params_inner_size_; - const float *beta = beta_data + x * param->params_inner_size_; - LayerNormGammaAndBeta(dst_param, src_param, gamma, beta, param->norm_inner_size_, mean, deno); + int x = i / param->params_outer_size_; + const float *gamma = gamma_data + x * param->norm_inner_size_; + const float *beta = beta_data + x * param->norm_inner_size_; + LayerNormGammaAndBeta(dst_norm, src_norm, gamma, beta, param->norm_inner_size_, mean, deno); } } return NNACL_OK; diff --git a/mindspore/lite/nnacl/fp32/lstm_fp32.c b/mindspore/lite/nnacl/fp32/lstm_fp32.c index 276375c863..35627feb91 100644 --- a/mindspore/lite/nnacl/fp32/lstm_fp32.c +++ b/mindspore/lite/nnacl/fp32/lstm_fp32.c @@ -117,39 +117,38 @@ int ElementOptMulAcc(const float *input0, const float input1, float *output, con } void UpdataState(float *cell_state, const float *forget_gate, const float *input_gate, const float *cell_gate, - float *state_buffer, int batch, int hidden_size, const float smooth) { - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { // smooth * old_cell_state + float *state_buffer, int batch, int hidden_size, const float zoneout) { + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { // zoneout * old_cell_state memcpy(state_buffer, cell_state, batch * hidden_size * sizeof(float)); ArithmeticParameter parameter; parameter.in_elements_num0_ = batch * hidden_size; parameter.in_elements_num1_ = 1; - ElementOptMul(state_buffer, &smooth, state_buffer, batch * hidden_size, ¶meter); + ElementOptMul(state_buffer, &zoneout, state_buffer, batch * hidden_size, ¶meter); } ElementMul(forget_gate, cell_state, cell_state, batch * hidden_size); ElementMulAcc(input_gate, cell_gate, cell_state, batch * hidden_size); - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { // (1 - smooth) * new_cell_state - ElementOptMulAcc(cell_state, 1 - smooth, state_buffer, batch * hidden_size); + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { // (1 - zoneout) * new_cell_state + ElementOptMulAcc(cell_state, 1 - zoneout, state_buffer, batch * hidden_size); } } -void UpdataOutput(const float *cell_state, const float *output_gate, float *hidden_state, float *state_buffer_in, - int batch, int hidden_size, const float smooth) { - float *state_buffer = state_buffer_in + batch * hidden_size; - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { +void UpdataOutput(const float *cell_state, const float *output_gate, float *hidden_state, float *state_buffer, + int batch, int hidden_size, const float zoneout) { + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { memcpy(state_buffer, hidden_state, batch * hidden_size * sizeof(float)); ArithmeticParameter parameter; parameter.in_elements_num0_ = batch * hidden_size; parameter.in_elements_num1_ = 1; - ElementOptMul(state_buffer, &smooth, state_buffer, batch * hidden_size, ¶meter); + ElementOptMul(state_buffer, &zoneout, state_buffer, batch * hidden_size, ¶meter); } Tanh(cell_state, batch * hidden_size, hidden_state); ElementMul(hidden_state, output_gate, hidden_state, batch * hidden_size); - if (!(smooth >= -FLT_EPSILON && smooth <= FLT_EPSILON)) { - ElementOptMulAcc(hidden_state, 1 - smooth, state_buffer, batch * hidden_size); + if (!(zoneout >= -FLT_EPSILON && zoneout <= FLT_EPSILON)) { + ElementOptMulAcc(hidden_state, 1 - zoneout, state_buffer, batch * hidden_size); } } @@ -164,7 +163,7 @@ void UpdateLstmGate(float *gate_buffer, const float *input, const float *weight, } void LstmStepUnit(float *output, const float *input, const float *input_weight, const float *state_weight, - const float *bias, float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer, + const float *bias, float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer[2], float *matmul_buffer[2], const LstmParameter *lstm_param) { bool is_vec = lstm_param->batch_ == 1; // input * weight @@ -205,25 +204,27 @@ void LstmStepUnit(float *output, const float *input, const float *input_weight, // update cell_gate Tanh(cell_gate, lstm_param->batch_ * lstm_param->hidden_size_, cell_gate); // update cell state - UpdataState(cell_state, forget_gate, input_gate, cell_gate, state_buffer, lstm_param->batch_, - lstm_param->hidden_size_, lstm_param->smooth_); + UpdataState(cell_state, forget_gate, input_gate, cell_gate, state_buffer[0], lstm_param->batch_, + lstm_param->hidden_size_, lstm_param->zoneout_cell_); // update output_gate Sigmoid(output_gate, lstm_param->batch_ * lstm_param->hidden_size_, output_gate); // update output - UpdataOutput(cell_state, output_gate, hidden_state, state_buffer, lstm_param->batch_, lstm_param->hidden_size_, - lstm_param->smooth_); + UpdataOutput(cell_state, output_gate, hidden_state, state_buffer[1], lstm_param->batch_, lstm_param->hidden_size_, + lstm_param->zoneout_hidden_); memcpy(output, hidden_state, lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float)); - if (!(lstm_param->smooth_ >= -FLT_EPSILON && lstm_param->smooth_ <= FLT_EPSILON)) { - memcpy(cell_state, state_buffer, lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float)); - memcpy(hidden_state, state_buffer + lstm_param->batch_ * lstm_param->hidden_size_, - lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float)); + if (!(lstm_param->zoneout_cell_ >= -FLT_EPSILON && lstm_param->zoneout_cell_ <= FLT_EPSILON)) { + memcpy(cell_state, state_buffer[0], lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float)); + } + + if (!(lstm_param->zoneout_hidden_ >= -FLT_EPSILON && lstm_param->zoneout_hidden_ <= FLT_EPSILON)) { + memcpy(hidden_state, state_buffer[1], lstm_param->batch_ * lstm_param->hidden_size_ * sizeof(float)); } } void Lstm(float *output, const float *input, const float *weight_i, const float *weight_h, const float *bias, - float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer, float *matmul_buffer[2], + float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer[2], float *matmul_buffer[2], const LstmParameter *lstm_param) { // forward for (int t = 0; t < lstm_param->seq_len_; t++) { diff --git a/mindspore/lite/nnacl/fp32/lstm_fp32.h b/mindspore/lite/nnacl/fp32/lstm_fp32.h index 2bc060dd8c..ef5ff9f954 100644 --- a/mindspore/lite/nnacl/fp32/lstm_fp32.h +++ b/mindspore/lite/nnacl/fp32/lstm_fp32.h @@ -32,7 +32,7 @@ void ElementMulAcc(const float *input0, const float *input1, float *output, int int ElementOptMulAcc(const float *input0, const float input1, float *output, const int element_size); void Lstm(float *output, const float *input, const float *weight_i, const float *weight_h, const float *bias, - float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer, float *matmul_buffer[2], + float *hidden_state, float *cell_state, float *gate_buffer, float *state_buffer[2], float *matmul_buffer[2], const LstmParameter *lstm_param); #ifdef __cplusplus } diff --git a/mindspore/lite/nnacl/fp32/pooling_fp32.h b/mindspore/lite/nnacl/fp32/pooling_fp32.h index 96c712bd77..71033e5072 100644 --- a/mindspore/lite/nnacl/fp32/pooling_fp32.h +++ b/mindspore/lite/nnacl/fp32/pooling_fp32.h @@ -22,7 +22,7 @@ #endif #include "nnacl/op_base.h" #include "nnacl/pooling_parameter.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" #ifdef __cplusplus extern "C" { diff --git a/mindspore/lite/nnacl/fp32/splice_fp32.c b/mindspore/lite/nnacl/fp32/splice_fp32.c new file mode 100644 index 0000000000..0682925169 --- /dev/null +++ b/mindspore/lite/nnacl/fp32/splice_fp32.c @@ -0,0 +1,30 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/fp32/splice_fp32.h" +void SpliceFp32(const float *src_data, int src_row, int src_col, const SpliceParameter *splice_parameter, + float *dst_data, int dst_row, int dst_col) { + for (int r = 0; r < dst_row; ++r) { + for (int off = 0; off < splice_parameter->context_dim_; ++off) { + int r_off = r + splice_parameter->context_[off]; + r_off = MSMAX(r_off, 0); + r_off = MSMIN(r_off, src_row - 1); + const float *tmp_src_data = src_data + r_off * src_col * sizeof(float); + float *tmp_dst_data = dst_data + r * dst_col * sizeof(float); + memcpy(tmp_dst_data + off * src_col, tmp_src_data, src_col * sizeof(float)); + } + } +} diff --git a/mindspore/lite/nnacl/fp32/splice_fp32.h b/mindspore/lite/nnacl/fp32/splice_fp32.h new file mode 100644 index 0000000000..42db661670 --- /dev/null +++ b/mindspore/lite/nnacl/fp32/splice_fp32.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_NNACL_FP32_SPLICE_FP32_H_ +#define MINDSPORE_LITE_NNACL_FP32_SPLICE_FP32_H_ +#include +#include "nnacl/splice_parameter.h" +#ifdef __cplusplus +extern "C" { +#endif + +void SpliceFp32(const float *src_data, int src_row, int src_col, const SpliceParameter *splice_parameter, + float *dst_data, int dst_row, int dst_col); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FP32_SPLICE_FP32_H_ diff --git a/mindspore/lite/nnacl/fp32_grad/batch_norm.h b/mindspore/lite/nnacl/fp32_grad/batch_norm.h index ca64c6a5e6..9931819696 100644 --- a/mindspore/lite/nnacl/fp32_grad/batch_norm.h +++ b/mindspore/lite/nnacl/fp32_grad/batch_norm.h @@ -22,7 +22,6 @@ typedef struct BNGradParameter { OpParameter op_parameter_; float epsilon_; - float momentum_; } BNGradParameter; #ifdef __cplusplus diff --git a/mindspore/lite/nnacl/fp32_grad/softmax_grad.h b/mindspore/lite/nnacl/fp32_grad/softmax_grad.h index 06cd9cc733..85f4717b64 100644 --- a/mindspore/lite/nnacl/fp32_grad/softmax_grad.h +++ b/mindspore/lite/nnacl/fp32_grad/softmax_grad.h @@ -35,7 +35,7 @@ typedef struct SoftmaxCrossEntropyParameter { // other parameter int32_t batch_size_; unsigned int number_of_classes_; - int is_grad; + bool is_grad; } SoftmaxCrossEntropyParameter; void SoftmaxGrad(const float *input_ptr, const float *yt_ptr, float *output_ptr, float *sum_data, float *sum_mul, diff --git a/mindspore/lite/nnacl/gather_parameter.h b/mindspore/lite/nnacl/gather_parameter.h index d300970417..0a2d907b6d 100644 --- a/mindspore/lite/nnacl/gather_parameter.h +++ b/mindspore/lite/nnacl/gather_parameter.h @@ -23,7 +23,7 @@ typedef struct GatherParameter { // Primitive parameter OpParameter op_parameter_; int axis_; - int batchDims_; + int quant_type_; } GatherParameter; #endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_ARM_NNACL_GATHER_PARAMETER_H_ diff --git a/mindspore/lite/nnacl/infer/adam_infer.c b/mindspore/lite/nnacl/infer/adam_infer.c new file mode 100644 index 0000000000..b8543ef7f0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/adam_infer.c @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/adam_infer.h" + +int AdamInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (10 != inputs_size) { + return NNACL_ERR; + } + + if (GetElementNum(inputs[0]) != GetElementNum(inputs[1]) || GetElementNum(inputs[0]) != GetElementNum(inputs[2]) || + GetElementNum(inputs[0]) != GetElementNum(inputs[9]) || GetElementNum(inputs[3]) != 1 || + GetElementNum(inputs[4]) != 1 || GetElementNum(inputs[5]) != 1 || GetElementNum(inputs[6]) != 1 || + GetElementNum(inputs[7]) != 1 || GetElementNum(inputs[8]) != 1) { + return NNACL_ERR; + } + if (outputs_size != 0) { + TensorC *out = outputs[0]; + SetDataTypeFormat(out, inputs[0]); + out->shape_size_ = 1; + out->shape_[0] = 1; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/adam_infer.h b/mindspore/lite/nnacl/infer/adam_infer.h new file mode 100644 index 0000000000..f4ec666813 --- /dev/null +++ b/mindspore/lite/nnacl/infer/adam_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ADAM_INFER_H +#define MINDSPORE_LITE_NNACL_ADAM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AdamInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ADAM_INFER_H diff --git a/mindspore/lite/nnacl/infer/add_sub_grad_infer.c b/mindspore/lite/nnacl/infer/add_sub_grad_infer.c new file mode 100644 index 0000000000..69d5580778 --- /dev/null +++ b/mindspore/lite/nnacl/infer/add_sub_grad_infer.c @@ -0,0 +1,63 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/add_sub_grad_infer.h" +#include "nnacl/infer/arithmetic_grad_infer.h" + +int AddSubGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *dy = inputs[0]; + const TensorC *x1 = inputs[1]; + const TensorC *x2 = inputs[2]; + TensorC *dx1 = outputs[0]; + TensorC *dx2 = outputs[1]; + + ArithmeticGradParameter *param = (ArithmeticGradParameter *)parameter; + + int in_shape0[MAX_SHAPE_SIZE]; + size_t in_shape0_size = 0; + ShapeSet(in_shape0, &in_shape0_size, x1->shape_, x1->shape_size_); + int in_shape1[MAX_SHAPE_SIZE]; + size_t in_shape1_size = 0; + ShapeSet(in_shape1, &in_shape1_size, x2->shape_, x2->shape_size_); + int outShape[MAX_SHAPE_SIZE]; + size_t outShape_size = 0; + ShapeSet(outShape, &outShape_size, dy->shape_, dy->shape_size_); + + param->ndim_ = outShape_size; + param->x1_shape_size_ = param->ndim_; + param->x2_shape_size_ = param->ndim_; + param->dy_shape_size_ = param->ndim_; + int fill_dim_num0 = outShape_size - in_shape0_size; + int fill_dim_num1 = outShape_size - in_shape1_size; + int j0 = 0; + int j1 = 0; + for (unsigned int i = 0; i < outShape_size; i++) { + param->x1_shape_[i] = (i < fill_dim_num0) ? 1 : in_shape0[j0++]; + param->x2_shape_[i] = (i < fill_dim_num1) ? 1 : in_shape1[j1++]; + param->dy_shape_[i] = outShape[i]; + } + + SetShapeTensor(dx1, x1); + SetShapeTensor(dx2, x2); + dx1->data_type_ = dy->data_type_; + dx2->data_type_ = dy->data_type_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/add_sub_grad_infer.h b/mindspore/lite/nnacl/infer/add_sub_grad_infer.h new file mode 100644 index 0000000000..4d3b959b42 --- /dev/null +++ b/mindspore/lite/nnacl/infer/add_sub_grad_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ADD_SUB_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_ADD_SUB_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AddSubGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ADD_SUB_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/addn_infer.c b/mindspore/lite/nnacl/infer/addn_infer.c new file mode 100644 index 0000000000..e571d06e9c --- /dev/null +++ b/mindspore/lite/nnacl/infer/addn_infer.c @@ -0,0 +1,70 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/addn_infer.h" + +int AddnInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + if (inputs_size < 2) { + return NNACL_ERR; + } + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + size_t max_dims = input->shape_size_; + size_t max_dims_idx = 0; + + // determine max_dims + for (size_t i = 1; i < inputs_size; ++i) { + if (inputs[i]->shape_size_ > max_dims) { + max_dims = inputs[i]->shape_size_; + max_dims_idx = i; + } + } + ShapeSet(output->shape_, &output->shape_size_, inputs[max_dims_idx]->shape_, inputs[max_dims_idx]->shape_size_); + + // make sure all elements have the same size or 1 (broadcasting) in all dimensions + for (size_t i = 1; i < inputs_size; ++i) { + if ((inputs[i]->shape_size_ != max_dims) && (GetElementNum(inputs[i]) != GetElementNum(inputs[max_dims_idx]))) { + return NNACL_ERR; + } + if (inputs[i]->data_type_ != inputs[0]->data_type_) { + return NNACL_ERR; + } + } + + for (size_t d = 0; d < input->shape_size_; ++d) { + size_t max_dim = 0; + for (size_t i = 0; i < inputs_size; ++i) { + size_t shift = max_dims - inputs[i]->shape_size_; + size_t dim = (i < shift) ? 1 : inputs[i]->shape_[d]; + if (dim > max_dim) { + max_dim = dim; + } + } + output->shape_[d] = max_dim; // set the biggest dimension in the output tensor + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/addn_infer.h b/mindspore/lite/nnacl/infer/addn_infer.h new file mode 100644 index 0000000000..76f34944e8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/addn_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ADDN_INFER_H +#define MINDSPORE_LITE_NNACL_ADDN_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AddnInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ADDN_INFER_H diff --git a/mindspore/lite/nnacl/infer/apply_momentum_infer.c b/mindspore/lite/nnacl/infer/apply_momentum_infer.c new file mode 100644 index 0000000000..f12207c59d --- /dev/null +++ b/mindspore/lite/nnacl/infer/apply_momentum_infer.c @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/apply_momentum_infer.h" + +int ApplyMomentumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size != 5) { + return NNACL_INPUT_TENSOR_ERROR; + } + + if (GetElementNum(inputs[0]) != GetElementNum(inputs[1]) || GetElementNum(inputs[0]) != GetElementNum(inputs[3]) || + GetElementNum(inputs[2]) != 1 || GetElementNum(inputs[4]) != 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + if (outputs_size != 0) { + TensorC *out = outputs[0]; + if (out == NULL) { + return NNACL_NULL_PTR; + } + out->data_type_ = inputs[0]->data_type_; + out->format_ = inputs[0]->format_; + out->shape_size_ = 1; + out->shape_[0] = 1; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/apply_momentum_infer.h b/mindspore/lite/nnacl/infer/apply_momentum_infer.h new file mode 100644 index 0000000000..a377b3a5e0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/apply_momentum_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H +#define MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ApplyMomentumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_APPLY_MOMENTUM_INFER_H diff --git a/mindspore/lite/nnacl/infer/argmin_max_infer.c b/mindspore/lite/nnacl/infer/argmin_max_infer.c new file mode 100644 index 0000000000..362023f73c --- /dev/null +++ b/mindspore/lite/nnacl/infer/argmin_max_infer.c @@ -0,0 +1,70 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/argmin_max_infer.h" + +int ArgMinMaxInferShape(const TensorC *const *inputs, const size_t inputs_size, TensorC **outputs, + const size_t outputs_size, OpParameter *parameter) { + int check_ret_1 = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + int check_ret_2 = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 2); + if (check_ret_1 != NNACL_OK && check_ret_2 != NNACL_OK) { + return NNACL_ERR; + } + ArgMinMaxParameter *param = (ArgMinMaxParameter *)parameter; + const TensorC *input = inputs[0]; + TensorC *output_1 = NULL; + TensorC *output_2 = NULL; + if (outputs_size == 2) { + output_1 = outputs[0]; + output_2 = outputs[1]; + } else if (param->out_value_) { + output_2 = outputs[0]; + } else { + output_1 = outputs[0]; + } + + if (output_1 != NULL) { + output_1->format_ = input->format_; + output_1->data_type_ = kNumberTypeInt32; + } + if (output_2 != NULL) { + SetDataTypeFormat(output_2, input); + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_); + size_t input_shape_size = input->shape_size_; + int axis = param->axis_ < 0 ? param->axis_ + (int)input_shape_size : param->axis_; + if (axis >= input_shape_size || axis < 0) { + return NNACL_PARAM_INVALID; + } + if (param->topk_ == 1 && !param->keep_dims_) { + ShapeErase(output_shape, &output_shape_size, axis); + } else { + output_shape[axis] = param->topk_; + } + + if (output_1 != NULL) { + SetShapeArray(output_1, output_shape, output_shape_size); + } + if (output_2 != NULL) { + SetShapeArray(output_2, output_shape, output_shape_size); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/argmin_max_infer.h b/mindspore/lite/nnacl/infer/argmin_max_infer.h new file mode 100644 index 0000000000..42726b3c57 --- /dev/null +++ b/mindspore/lite/nnacl/infer/argmin_max_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ARGMAX_INFER_H +#define MINDSPORE_LITE_NNACL_ARGMAX_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/arg_min_max_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ArgMinMaxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ARGMAX_INFER_H diff --git a/mindspore/lite/nnacl/infer/arithmetic_compare_infer.c b/mindspore/lite/nnacl/infer/arithmetic_compare_infer.c new file mode 100644 index 0000000000..9d3c812285 --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_compare_infer.c @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/arithmetic_compare_infer.h" + +int ArithmeticCompareInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int res = ArithmeticInferShape(inputs, inputs_size, outputs, outputs_size, parameter); + TensorC *output = outputs[0]; + if (output == NULL) { + return NNACL_NULL_PTR; + } + output->data_type_ = kNumberTypeBool; + return res; +} diff --git a/mindspore/lite/nnacl/infer/arithmetic_compare_infer.h b/mindspore/lite/nnacl/infer/arithmetic_compare_infer.h new file mode 100644 index 0000000000..2934cdce95 --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_compare_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H +#define MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H + +#include "nnacl/infer/arithmetic_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ArithmeticCompareInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_COMPARE_INFER_H diff --git a/mindspore/lite/nnacl/infer/arithmetic_grad_infer.c b/mindspore/lite/nnacl/infer/arithmetic_grad_infer.c new file mode 100644 index 0000000000..591732a7c6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_grad_infer.c @@ -0,0 +1,97 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/arithmetic_grad_infer.h" + +/* + * the Arithmetic Grad op include AddGrad, SubGrad, MulGrad, DivGrad, MaximumGrad, MinimumGrad + * according to the arithmetic_fp32.h now + * the MaximumGrad, MinimumGrad run through MaximumGradInfershape + * the AddGrad, SubGrad run through AddSubGradInfershape + * the others run through this function + * */ +int ArithmeticGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *dy = inputs[0]; + const TensorC *x1 = inputs[1]; + const TensorC *x2 = inputs[2]; + TensorC *dx1 = outputs[0]; + TensorC *dx2 = outputs[1]; + + ArithmeticGradParameter *param = (ArithmeticGradParameter *)parameter; + + int in_shape0[MAX_SHAPE_SIZE]; + size_t in_shape0_size = 0; + ShapeSet(in_shape0, &in_shape0_size, x1->shape_, x1->shape_size_); + int in_shape1[MAX_SHAPE_SIZE]; + size_t in_shape1_size = 0; + ShapeSet(in_shape1, &in_shape1_size, x2->shape_, x2->shape_size_); + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, dy->shape_, dy->shape_size_); + + if (GetElementNum(dx1) < GetElementNum(dx2)) { + param->ndim_ = in_shape1_size; + param->x1_shape_size_ = param->ndim_; + param->x2_shape_size_ = param->ndim_; + param->dy_shape_size_ = param->ndim_; + int fill_dim_num = in_shape1_size - in_shape0_size; // This will not work for batch! + int j = 0; + for (unsigned int i = 0; i < in_shape1_size; i++) { + if (i < fill_dim_num) { + param->x2_shape_[i] = 1; + } else { + param->x2_shape_[i] = in_shape0[j++]; + } + param->x1_shape_[i] = in_shape1[i]; + param->dy_shape_[i] = out_shape[i]; + } + } else if (GetElementNum(dx2) < GetElementNum(dx1)) { + param->ndim_ = in_shape0_size; + param->x1_shape_size_ = param->ndim_; + param->x2_shape_size_ = param->ndim_; + param->dy_shape_size_ = param->ndim_; + param->broadcasting_ = true; + int j = 0; + int fill_dim_num = in_shape0_size - in_shape1_size; + for (unsigned int i = 0; i < in_shape0_size; i++) { + if (i < fill_dim_num) { + param->x2_shape_[i] = 1; + } else { + param->x2_shape_[i] = in_shape1[j++]; + } + param->x1_shape_[i] = in_shape0[i]; + param->dy_shape_[i] = out_shape[i]; + } + } else { + param->broadcasting_ = false; + for (unsigned int i = 0; i < in_shape0_size; i++) { + param->x2_shape_[i] = in_shape1[i]; + param->x1_shape_[i] = in_shape0[i]; + param->dy_shape_[i] = out_shape[i]; + } + } + + SetShapeTensor(dx1, x1); + SetShapeTensor(dx2, x2); + dx1->data_type_ = dy->data_type_; + dx2->data_type_ = dy->data_type_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/arithmetic_grad_infer.h b/mindspore/lite/nnacl/infer/arithmetic_grad_infer.h new file mode 100644 index 0000000000..04323116ae --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_grad_infer.h @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ArithmeticGradParameter { + OpParameter op_parameter_; + int type_; + bool broadcasting_; // default false + int ndim_; + // std::vector dy_shape_; + int dy_shape_[MAX_SHAPE_SIZE]; + size_t dy_shape_size_; + int x1_shape_[MAX_SHAPE_SIZE]; + size_t x1_shape_size_; + int x2_shape_[MAX_SHAPE_SIZE]; + size_t x2_shape_size_; +} ArithmeticGradParameter; + +int ArithmeticGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/arithmetic_infer.c b/mindspore/lite/nnacl/infer/arithmetic_infer.c new file mode 100644 index 0000000000..466813b8a1 --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_infer.c @@ -0,0 +1,117 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/arithmetic_infer.h" + +int ArithmeticInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + ArithmeticParameter *param = (ArithmeticParameter *)parameter; + param->broadcasting_ = false; + + const TensorC *input0 = inputs[0]; + const TensorC *input1 = inputs[1]; + TensorC *output = outputs[0]; + + const int *input_shape0 = input0->shape_; + size_t input_shape0_size = input0->shape_size_; + const int *input_shape1 = input1->shape_; + size_t input_shape1_size = input1->shape_size_; + output->format_ = input0->format_; + output->data_type_ = input0->data_type_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input_shape0_size > 10 || input_shape1_size > 10) { + return NNACL_ERR; + } + int in_shape0_[10]; + int in_shape1_[10]; + int out_shape_[10]; + + int ndim_ = input_shape0_size; + if (input_shape0_size < input_shape1_size) { + ndim_ = input_shape1_size; + int fill_dim_num = input_shape1_size - input_shape0_size; + int j = 0; + for (size_t i = 0; i < input_shape1_size; i++) { + if (i < fill_dim_num) { + in_shape0_[i] = 1; + } else { + in_shape0_[i] = input_shape0[j++]; + } + in_shape1_[i] = input_shape1[i]; + } + // format = input0->format(); + } else if (input_shape0_size > input_shape1_size) { + ndim_ = input_shape0_size; + int fill_dim_num = input_shape0_size - input_shape1_size; + int j = 0; + for (size_t i = 0; i < input_shape0_size; i++) { + if (i < fill_dim_num) { + in_shape1_[i] = 1; + } else { + in_shape1_[i] = input_shape1[j++]; + } + in_shape0_[i] = input_shape0[i]; + } + } else { + for (size_t i = 0; i < input_shape0_size; i++) { + in_shape1_[i] = input_shape1[i]; + in_shape0_[i] = input_shape0[i]; + } + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (int i = 0; i < ndim_; i++) { + if (in_shape0_[i] != in_shape1_[i]) { + if (in_shape0_[i] == 1) { + out_shape_[i] = in_shape1_[i]; + } else if (in_shape1_[i] == 1) { + out_shape_[i] = in_shape0_[i]; + } else { + return NNACL_ERR; + } + param->broadcasting_ = true; + } else { + out_shape_[i] = in_shape0_[i]; + } + output_shape[output_shape_size] = out_shape_[i]; + output_shape_size++; + } + + SetShapeArray(output, output_shape, output_shape_size); + + param->ndim_ = ndim_; + memcpy(param->in_shape0_, in_shape0_, ndim_ * sizeof(int)); + memcpy(param->in_shape1_, in_shape1_, ndim_ * sizeof(int)); + memcpy(param->out_shape_, out_shape_, ndim_ * sizeof(int)); + + param->in_elements_num0_ = 1; + param->in_elements_num1_ = 1; + param->out_elements_num_ = 1; + for (int i = 0; i < ndim_; i++) { + param->in_elements_num0_ *= param->in_shape0_[i]; + param->in_elements_num1_ *= param->in_shape1_[i]; + param->out_elements_num_ *= param->out_shape_[i]; + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/arithmetic_infer.h b/mindspore/lite/nnacl/infer/arithmetic_infer.h new file mode 100644 index 0000000000..c7ee565643 --- /dev/null +++ b/mindspore/lite/nnacl/infer/arithmetic_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H +#define MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/arithmetic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ArithmeticInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outpus_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ARITHMETIC_INFER_H diff --git a/mindspore/lite/nnacl/infer/assert_op_infer.c b/mindspore/lite/nnacl/infer/assert_op_infer.c new file mode 100644 index 0000000000..5fabc13637 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assert_op_infer.c @@ -0,0 +1,22 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/assert_op_infer.h" + +int AssertOpInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/assert_op_infer.h b/mindspore/lite/nnacl/infer/assert_op_infer.h new file mode 100644 index 0000000000..4e03466f11 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assert_op_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H +#define MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AssertOpInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ASSERT_OP_INFER_H diff --git a/mindspore/lite/nnacl/infer/assign_add_infer.c b/mindspore/lite/nnacl/infer/assign_add_infer.c new file mode 100644 index 0000000000..807c9fd7b6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assign_add_infer.c @@ -0,0 +1,34 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/assign_add_infer.h" + +int AssignAddInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *x = inputs[0]; + const TensorC *y = inputs[1]; + TensorC *out = outputs[0]; + if (x->data_type_ != y->data_type_) { + return NNACL_ERR; + } + SetDataTypeFormat(out, x); + SetShapeTensor(out, x); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/assign_add_infer.h b/mindspore/lite/nnacl/infer/assign_add_infer.h new file mode 100644 index 0000000000..0290e88b57 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assign_add_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H +#define MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AssignAddInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ASSIGN_ADD_INFER_H diff --git a/mindspore/lite/nnacl/infer/assign_infer.c b/mindspore/lite/nnacl/infer/assign_infer.c new file mode 100644 index 0000000000..fcdf7a0ef5 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assign_infer.c @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/assign_infer.h" + +int AssignInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + + if (GetElementNum(inputs[0]) != GetElementNum(inputs[1])) { + return NNACL_ERR; + } + + if (outputs_size != 0) { + TensorC *out = outputs[0]; + SetDataTypeFormat(out, inputs[0]); + out->shape_size_ = 1; + out->shape_[0] = 1; + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/assign_infer.h b/mindspore/lite/nnacl/infer/assign_infer.h new file mode 100644 index 0000000000..fe276b79e3 --- /dev/null +++ b/mindspore/lite/nnacl/infer/assign_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ASSIGN_INFER_H +#define MINDSPORE_LITE_NNACL_ASSIGN_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int AssignInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ASSIGN_INFER_H diff --git a/mindspore/lite/nnacl/infer/audio_spectrogram_infer.c b/mindspore/lite/nnacl/infer/audio_spectrogram_infer.c new file mode 100644 index 0000000000..6f566f89f0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/audio_spectrogram_infer.c @@ -0,0 +1,71 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/audio_spectrogram_infer.h" + +int Log2Ceil(uint32_t length) { + if (length == 0) { + return -1; + } + int floor = 0; + for (int i = 4; i >= 0; --i) { + const int shift = (1 << i); + uint32_t tmp = length >> shift; + if (tmp != 0) { + length = tmp; + floor += shift; + } + } + return length == (length & ~(length - 1)) ? floor : floor + 1; +} + +uint32_t GetFftLength(uint32_t length) { + int shift = Log2Ceil(length); + return 1 << shift; +} + +int AudioSpectrogramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->shape_size_ != 2) { + return NNACL_ERR; + } + AudioSpectrogramParameter *param = (AudioSpectrogramParameter *)parameter; + if (param->window_size_ < 2) { + return NNACL_ERR; + } + if (param->stride_ < 1) { + return NNACL_ERR; + } + int output_shape[3]; + output_shape[0] = input->shape_[1]; + int sample_sub_window = input->shape_[0] - param->window_size_; + output_shape[1] = sample_sub_window < 0 ? 0 : 1 + sample_sub_window / param->stride_; + // compute fft length + int fft_length = GetFftLength(param->window_size_); + output_shape[2] = fft_length / 2 + 1; + SetShapeArray(output, output_shape, 3); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/audio_spectrogram_infer.h b/mindspore/lite/nnacl/infer/audio_spectrogram_infer.h new file mode 100644 index 0000000000..030883c8b6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/audio_spectrogram_infer.h @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H +#define MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct AudioSpectrogramParameter { + OpParameter op_parameter_; + int window_size_; + int stride_; +} AudioSpectrogramParameter; + +int AudioSpectrogramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_AUDIO_SPECTROGRAM_INFER_H diff --git a/mindspore/lite/nnacl/infer/batch_to_space_infer.c b/mindspore/lite/nnacl/infer/batch_to_space_infer.c new file mode 100644 index 0000000000..b7910a7257 --- /dev/null +++ b/mindspore/lite/nnacl/infer/batch_to_space_infer.c @@ -0,0 +1,137 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/batch_to_space_infer.h" + +int SetOutputShapeFromParam(const TensorC *const *inputs, TensorC **outputs, OpParameter *parameter) { + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, inputs[0]->shape_, inputs[0]->shape_size_); + + if (input_shape_size != 4) { + return NNACL_PARAM_INVALID; + } + + BatchToSpaceParameter *param = (BatchToSpaceParameter *)parameter; + int32_t *block_shape = param->block_shape_; + int32_t *crops = param->crops_; + int mul_block_shape = 1; + + for (size_t i = 0; i < 2; ++i) { + if (block_shape[i] <= 0) { + return NNACL_PARAM_INVALID; + } + if (input_shape[kNHWC_N] % block_shape[i]) { + return NNACL_ERR; + } + mul_block_shape *= block_shape[i]; + } + + if (input_shape[kNHWC_N] < mul_block_shape) { + return NNACL_PARAM_INVALID; + } + for (size_t i = 0; i < 4; ++i) { + if (crops[i] < 0) { + return NNACL_PARAM_INVALID; + } + } + int32_t output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input_shape_size; + output_shape[kNHWC_N] = input_shape[kNHWC_N] / mul_block_shape; + output_shape[kNHWC_H] = input_shape[kNHWC_H] * block_shape[0] - crops[0] - crops[1]; + output_shape[kNHWC_W] = input_shape[kNHWC_W] * block_shape[1] - crops[2] - crops[3]; + output_shape[kNHWC_C] = input_shape[kNHWC_C]; + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} + +int SetOutputShapeFromInput(const TensorC *const *inputs, TensorC **outputs) { + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, inputs[0]->shape_, inputs[0]->shape_size_); + if (input_shape_size != 4) { + return NNACL_PARAM_INVALID; + } + int *block_shape = (int *)(inputs[1]->data_); + int *crops = (int *)(inputs[2]->data_); + if (GetElementNum(inputs[1]) != 2) { + return NNACL_PARAM_INVALID; + } + if (GetElementNum(inputs[2]) != 4) { + return NNACL_PARAM_INVALID; + } + int mul_block_shape_ = 1; + + for (size_t i = 0; i < 2; ++i) { + if (block_shape[i] <= 0) { + return NNACL_PARAM_INVALID; + } + if (input_shape[kNHWC_N] % block_shape[i]) { + return 1; + } + mul_block_shape_ *= block_shape[i]; + } + + if (input_shape[kNHWC_N] < mul_block_shape_) { + return NNACL_PARAM_INVALID; + } + for (size_t i = 0; i < 4; ++i) { + if (crops[i] < 0) { + return NNACL_PARAM_INVALID; + } + } + int32_t output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input_shape_size; + output_shape[kNHWC_N] = input_shape[kNHWC_N] / mul_block_shape_; + output_shape[kNHWC_H] = input_shape[kNHWC_H] * block_shape[0] - crops[0] - crops[1]; + output_shape[kNHWC_W] = input_shape[kNHWC_W] * block_shape[1] - crops[2] - crops[3]; + output_shape[kNHWC_C] = input_shape[kNHWC_C]; + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} + +int BatchToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (outputs_size != 1 || (inputs_size != 1 && inputs_size != 3)) { + return NNACL_PARAM_INVALID; + } + + const TensorC *input = inputs[0]; + if (input->format_ != Format_NHWC) { + return NNACL_ERR; + } + SetDataTypeFormat(outputs[0], input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + if (inputs_size == 1) { + int ret = SetOutputShapeFromParam(inputs, outputs, parameter); + return ret; + } + if (inputs_size == 3) { + if (inputs[0]->data_ == NULL) { + return NNACL_INFER_INVALID; + } + if (inputs[1]->data_ == NULL || inputs[2]->data_ == NULL) { + return NNACL_ERR; + } + int ret = SetOutputShapeFromInput(inputs, outputs); + return ret; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/batch_to_space_infer.h b/mindspore/lite/nnacl/infer/batch_to_space_infer.h new file mode 100644 index 0000000000..261a1f76bf --- /dev/null +++ b/mindspore/lite/nnacl/infer/batch_to_space_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H +#define MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/batch_to_space.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int BatchToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BATCH_TO_SPACE_INFER_H diff --git a/mindspore/lite/nnacl/infer/bias_grad_infer.c b/mindspore/lite/nnacl/infer/bias_grad_infer.c new file mode 100644 index 0000000000..7cf1d678e7 --- /dev/null +++ b/mindspore/lite/nnacl/infer/bias_grad_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/bias_grad_infer.h" + +int BiasGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *in0 = inputs[0]; + TensorC *out = outputs[0]; + + int inshape[MAX_SHAPE_SIZE]; + size_t inshape_size = 0; + ShapeSet(inshape, &inshape_size, in0->shape_, in0->shape_size_); + int ndim = inshape_size; + for (int i = 0; i < ndim - 1; i++) { + inshape[i] = 1; + } + SetDataTypeFormat(out, in0); + SetShapeArray(out, inshape, inshape_size); + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/bias_grad_infer.h b/mindspore/lite/nnacl/infer/bias_grad_infer.h new file mode 100644 index 0000000000..2b40694d09 --- /dev/null +++ b/mindspore/lite/nnacl/infer/bias_grad_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int BiasGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BIAS_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.c b/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.c new file mode 100644 index 0000000000..55a6342138 --- /dev/null +++ b/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.c @@ -0,0 +1,33 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/binary_cross_entropy_infer.h" + +int BinaryCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + const TensorC *x = inputs[0]; + TensorC *out = outputs[0]; + SetDataTypeFormat(out, x); + BinaryCrossEntropyParameter *param = (BinaryCrossEntropyParameter *)parameter; + int reduction = param->reduction; + if (reduction == 1 || reduction == 2) { + out->shape_size_ = 1; + out->shape_[0] = 1; + } else { + SetShapeTensor(out, x); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.h b/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.h new file mode 100644 index 0000000000..6727303255 --- /dev/null +++ b/mindspore/lite/nnacl/infer/binary_cross_entropy_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H +#define MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32_grad/binary_cross_entropy.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int BinaryCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_INFER_H diff --git a/mindspore/lite/nnacl/infer/bn_grad_infer.c b/mindspore/lite/nnacl/infer/bn_grad_infer.c new file mode 100644 index 0000000000..9d4a921ae1 --- /dev/null +++ b/mindspore/lite/nnacl/infer/bn_grad_infer.c @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/bn_grad_infer.h" + +int BnGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 6, 3); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *in = inputs[1]; + const TensorC *scale = inputs[2]; + if (in->shape_size_ != 4) { + return NNACL_INPUT_TENSOR_ERROR; + } + + SetShapeTensor(outputs[0], in); + SetDataTypeFormat(outputs[0], in); + SetShapeTensor(outputs[1], scale); + SetDataTypeFormat(outputs[1], scale); + SetShapeTensor(outputs[2], scale); + SetDataTypeFormat(outputs[2], scale); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/bn_grad_infer.h b/mindspore/lite/nnacl/infer/bn_grad_infer.h new file mode 100644 index 0000000000..a28f5b2f55 --- /dev/null +++ b/mindspore/lite/nnacl/infer/bn_grad_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int BnGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BN_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/broadcast_to_infer.c b/mindspore/lite/nnacl/infer/broadcast_to_infer.c new file mode 100644 index 0000000000..0580e3b301 --- /dev/null +++ b/mindspore/lite/nnacl/infer/broadcast_to_infer.c @@ -0,0 +1,68 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/broadcast_to_infer.h" + +int BroadcastToInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size != 1 && inputs_size != 2) { + return NNACL_ERR; + } + if (outputs_size != 1) { + return NNACL_ERR; + } + + const TensorC *input = inputs[0]; + SetDataTypeFormat(outputs[0], input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + BroadcastToParameter *param = (BroadcastToParameter *)parameter; + int32_t dst_shape[MAX_SHAPE_SIZE]; + size_t dst_shape_size = param->shape_size_; + for (size_t i = 0; i < dst_shape_size; i++) { + dst_shape[i] = param->shape_[i]; + } + for (size_t i = 0; i < dst_shape_size; ++i) { + if (dst_shape[i] == -1) { + dst_shape[i] = inputs[0]->shape_[i]; + } + } + const int *input_shape = input->shape_; + size_t input_shape_size = input->shape_size_; + int shape[MAX_SHAPE_SIZE]; + size_t shape_size = dst_shape_size; + int input_shape_index = input_shape_size - 1; + if (input_shape_size > dst_shape_size) { + return NNACL_ERR; + } + + for (int i = dst_shape_size - 1; i >= 0; --i) { + if (dst_shape[i] < 0) { + return NNACL_ERR; + } + if (input_shape_index >= 0) { + int dim = input_shape[input_shape_index]; + if (dim != dst_shape[i] && dim != 1) { + return NNACL_ERR; + } + } + shape[i] = dst_shape[i]; + --input_shape_index; + } + SetShapeArray(outputs[0], shape, shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/broadcast_to_infer.h b/mindspore/lite/nnacl/infer/broadcast_to_infer.h new file mode 100644 index 0000000000..a7b8630a7a --- /dev/null +++ b/mindspore/lite/nnacl/infer/broadcast_to_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H +#define MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/broadcast_to_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int BroadcastToInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outpus_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BROADCAST_TO_INFER_H diff --git a/mindspore/lite/nnacl/infer/cast_infer.c b/mindspore/lite/nnacl/infer/cast_infer.c new file mode 100644 index 0000000000..8b84d95b1b --- /dev/null +++ b/mindspore/lite/nnacl/infer/cast_infer.c @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/cast_infer.h" + +int CastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + output->format_ = input->format_; + const TensorC *dst_type = inputs[1]; + output->data_type_ = *((int *)dst_type->data_); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->data_type_ != kNumberTypeBool && input->data_type_ != kNumberTypeUInt8 && + input->data_type_ != kNumberTypeInt8 && input->data_type_ != kNumberTypeInt32 && + input->data_type_ != kNumberTypeFloat32 && input->data_type_ != kNumberTypeFloat16) { + return NNACL_INPUT_TENSOR_ERROR; + } + + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/cast_infer.h b/mindspore/lite/nnacl/infer/cast_infer.h new file mode 100644 index 0000000000..6c669c7ca0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/cast_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CAST_INFER_H +#define MINDSPORE_LITE_NNACL_CAST_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int CastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CAST_INFER_H diff --git a/mindspore/lite/nnacl/infer/common_infer.c b/mindspore/lite/nnacl/infer/common_infer.c new file mode 100644 index 0000000000..8ec609136e --- /dev/null +++ b/mindspore/lite/nnacl/infer/common_infer.c @@ -0,0 +1,455 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use tensor 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 "nnacl/infer/common_infer.h" +#include +#include + +int FreeTensorListData(TensorListC *tensor_list) { + // del each tensor in tensors_ and clear tensors_ + if (tensor_list->element_num_ == 0) { + return NNACL_OK; + } + for (int i = 0; i < tensor_list->element_num_; ++i) { + tensor_list->tensors_[i] = NULL; + } + return NNACL_OK; +} + +int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, vvector *tensor_shape) { + // This function will create a new tensors_ + // Your must to set shape(param2: tensor_shape) and data_type_(tensors_data_type_ = param1: dtype) of each tensor in + // tensors_. After that, you need to call function:MallocData to malloc data buf of each tensor in tensors_. + + if (tensor_list->element_num_ == 0) { + return NNACL_OK; + } + if (((size_t)(tensor_list->element_num_)) != tensor_shape->size_) { + return NNACL_ERR; + } + tensor_list->tensors_data_type_ = dtype; + tensor_list->tensors_ = (TensorC **)malloc(tensor_list->element_num_ * sizeof(TensorC *)); // free in infer_manager + if (tensor_list->tensors_ == NULL) { + return NNACL_NULL_PTR; + } + memset(tensor_list->tensors_, 0, tensor_list->element_num_ * sizeof(TensorC *)); + for (int i = 0; i < tensor_list->element_num_; ++i) { + TensorC *tensor_ptr = (TensorC *)malloc(sizeof(TensorC)); + if (tensor_ptr == NULL) { + return NNACL_ERR; + } + memset(tensor_ptr, 0, sizeof(TensorC)); + tensor_ptr->format_ = Format_NHWC; + tensor_ptr->data_type_ = dtype; + ShapeSet(tensor_ptr->shape_, &(tensor_ptr->shape_size_), tensor_shape->shape_[i], tensor_shape->shape_size_[i]); + tensor_list->tensors_[i] = tensor_ptr; + } + return NNACL_OK; +} + +int TensorListMergeShape(int *element_shape, size_t *element_shape_size, const int *tmp, size_t tmp_size) { + if (*element_shape_size >= 255 || element_shape[0] == -1) { + ShapeSet(element_shape, element_shape_size, tmp, tmp_size); + return NNACL_OK; + } + if (*element_shape_size != tmp_size) { + return NNACL_ERR; + } + for (size_t j = 0; j < tmp_size; ++j) { + if (element_shape[j] >= 0 && tmp[j] >= 0 && element_shape[j] != tmp[j]) { + return NNACL_ERR; + } + element_shape[j] = element_shape[j] >= 0 ? element_shape[j] : tmp[j]; + } + return NNACL_OK; +} + +bool TensorListIsFullyDefined(int *shape, size_t shape_size) { + for (size_t i = 0; i < shape_size; ++i) { + if (shape[i] < 0) { + return false; + } + } + return true; +} + +int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + for (size_t i = 0; i < inputs_size; i++) { + if (inputs[i] == NULL) { + return NNACL_NULL_PTR; + } + } + for (size_t i = 0; i < outputs_size; i++) { + if (outputs[i] == NULL) { + return NNACL_NULL_PTR; + } + } + if (parameter == NULL) { + return NNACL_NULL_PTR; + } + return NNACL_OK; +} + +int CheckAugmentNullSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + if (inputs_size != inputs_size_obj || outputs_size != outputs_size_obj) { + return NNACL_INPUT_TENSOR_ERROR; + } + return NNACL_OK; +} + +int CheckAugmentNullSizeInputTwo(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter, size_t inputs_size_obj_0, + size_t inputs_size_obj_1, size_t outputs_size_obj) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + if ((inputs_size != inputs_size_obj_0 && inputs_size != inputs_size_obj_1) || outputs_size != outputs_size_obj) { + return NNACL_INPUT_TENSOR_ERROR; + } + return NNACL_OK; +} + +int CheckAugmentNullInputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t inputs_size_obj) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + if (inputs_size != inputs_size_obj) { + return NNACL_INPUT_TENSOR_ERROR; + } + return NNACL_OK; +} + +int CheckAugmentNullOutputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t outputs_size_obj) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + if (outputs_size != outputs_size_obj) { + return NNACL_INPUT_TENSOR_ERROR; + } + return NNACL_OK; +} + +int SetShapeTensor(TensorC *dst, const TensorC *src) { + for (size_t i = 0; i < src->shape_size_; i++) { + dst->shape_[i] = src->shape_[i]; + } + dst->shape_size_ = src->shape_size_; + return NNACL_OK; +} + +int SetShapeArray(TensorC *dst, int *src, size_t src_size) { + for (size_t i = 0; i < src_size; i++) { + dst->shape_[i] = src[i]; + } + dst->shape_size_ = src_size; + return NNACL_OK; +} + +void SetDataTypeFormat(TensorC *dst, const TensorC *src) { + dst->format_ = src->format_; + dst->data_type_ = src->data_type_; +} + +int GetBatch(const TensorC *tensor) { + if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) { + return -1; + } + switch (tensor->format_) { + case Format_NHWC: + case Format_NHWC4: + case Format_NCHW: + case Format_NC4HW4: + case Format_KCHW: + case Format_KHWC: + case Format_NC: + case Format_NC4: + return tensor->shape_[0]; + case Format_HWCK: + case Format_CHWK: + return tensor->shape_[3]; + case Format_HWKC: + return tensor->shape_[2]; + case Format_CKHW: + return tensor->shape_[1]; + default: + return -1; + } +} +int GetHeight(const TensorC *tensor) { + if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) { + return -1; + } + switch (tensor->format_) { + case Format_NCHW: + case Format_KCHW: + case Format_CKHW: + return tensor->shape_[2]; + case Format_NHWC: + case Format_NHWC4: + case Format_NC4HW4: + case Format_KHWC: + case Format_CHWK: + return tensor->shape_[1]; + case Format_HWCK: + case Format_HWKC: + case Format_HW: + case Format_HW4: + return tensor->shape_[0]; + default: + return -1; + } +} +int GetWidth(const TensorC *tensor) { + if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) { + return -1; + } + switch (tensor->format_) { + case Format_NCHW: + case Format_KCHW: + case Format_CKHW: + return tensor->shape_[3]; + case Format_KHWC: + case Format_NHWC: + case Format_NHWC4: + case Format_NC4HW4: + case Format_CHWK: + return tensor->shape_[2]; + case Format_HWCK: + case Format_HWKC: + case Format_HW: + case Format_HW4: + return tensor->shape_[1]; + default: + return -1; + } +} +int GetChannel(const TensorC *tensor) { + if (tensor->shape_size_ != 4 && tensor->shape_size_ != 2) { + return -1; + } + switch (tensor->format_) { + case Format_NCHW: + case Format_KCHW: + case Format_NC: + case Format_NC4: + return tensor->shape_[1]; + case Format_HWCK: + return tensor->shape_[2]; + case Format_HWKC: + case Format_NHWC: + case Format_NHWC4: + case Format_NC4HW4: + case Format_KHWC: + return tensor->shape_[3]; + case Format_CKHW: + case Format_CHWK: + return tensor->shape_[0]; + default: + return -1; + } +} + +int GetElementNum(const TensorC *tensor) { + if (tensor->shape_size_ == 0) { + return 1; // scalar mode + } + int res = 1; + for (size_t i = 0; i < tensor->shape_size_; i++) { + res = res * tensor->shape_[i]; + } + return res; +} +int GetDimensionSize(const TensorC *tensor, const size_t index) { + int dim_size = -1; + if (index < tensor->shape_size_) { + dim_size = tensor->shape_[index]; + } + return dim_size; +} + +int ShapeSet(int *dst_shape, size_t *dst_shape_size, const int *src_shape, size_t src_shape_size) { + for (size_t i = 0; i < src_shape_size; i++) { + dst_shape[i] = src_shape[i]; + } + *dst_shape_size = src_shape_size; + return NNACL_OK; +} + +int ShapePush(int *shape, size_t *shape_size, int value) { + shape[*shape_size] = value; + *shape_size = *shape_size + 1; + return NNACL_OK; +} + +int ShapeInsert(int *shape, size_t *shape_size, int index, int value) { + if (index < 0 || index > *shape_size) { + return NNACL_ERR; + } + for (int i = *shape_size; i > index; i--) { + shape[i] = shape[i - 1]; + } + shape[index] = value; + *shape_size = *shape_size + 1; + return NNACL_OK; +} + +int ShapeErase(int *shape, size_t *shape_size, int index) { + if (index < 0 && index >= *shape_size) { + return NNACL_ERR; + } + + for (int i = index; i < *shape_size - 1; i++) { + shape[i] = shape[i + 1]; + } + *shape_size = *shape_size - 1; + return NNACL_OK; +} + +bool ShapeEqual(const int *shape0, size_t shape0_size, const int *shape1, size_t shape1_size) { + if (shape0_size != shape1_size) { + return false; + } + for (int i = 0; i < shape0_size; i++) { + if (shape0[i] != shape1[i]) { + return false; + } + } + return true; +} + +void iswap(int *a, int *b) { + int tmp = *a; + *a = *b; + *b = tmp; +} + +int imin(int a, int b) { return a > b ? b : a; } + +int imax(int a, int b) { return a < b ? b : a; } + +// input == output completely refer to +// 1. zeros_like +int CommonInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (parameter == NULL || inputs[0] == NULL || outputs[0] == NULL) { + return NNACL_NULL_PTR; + } + SetDataTypeFormat(outputs[0], inputs[0]); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(outputs[0], inputs[0]); + return NNACL_OK; +} + +int FftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + output->data_type_ = kNumberTypeFloat32; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_); + input_shape_size--; + SetShapeArray(output, input_shape, input_shape_size); + return NNACL_OK; +} + +int VectorCInit(VectorC *vc, size_t per_malloc_size) { + vc->data_ = (int *)malloc(per_malloc_size * sizeof(int)); + if (vc->data_ == NULL) { + return NNACL_ERR; + } + vc->size_ = 0; + vc->max_size_ = per_malloc_size; + vc->per_malloc_size_ = per_malloc_size; + return NNACL_OK; +} + +void VectorCSet(VectorC *vc, const int *src_shape, size_t src_shape_size) { + if (src_shape_size == 0) { + vc->size_ = 0; + } else { + free(vc->data_); + vc->max_size_ = (src_shape_size / vc->per_malloc_size_ + 1) * vc->per_malloc_size_; + vc->data_ = (int *)malloc(sizeof(int) * vc->max_size_); + for (size_t i = 0; i < src_shape_size; i++) { + vc->data_[i] = src_shape[i]; + } + vc->size_ = src_shape_size; + } +} + +void VectorCPush(VectorC *vc, int value) { + if (vc->size_ + 1 > vc->max_size_) { + int *tmp = (int *)malloc(vc->per_malloc_size_ * sizeof(int) + vc->max_size_ * sizeof(int)); + memcpy(tmp, vc->data_, vc->size_ * sizeof(int)); + free(vc->data_); + vc->data_ = tmp; + vc->max_size_ = vc->max_size_ + vc->per_malloc_size_; + } + vc->data_[vc->size_] = value; + vc->size_++; +} + +void VectorCInsert(VectorC *vc, int index, int value) { + if (vc->size_ + 1 > vc->max_size_) { + int *tmp = (int *)malloc(vc->per_malloc_size_ * sizeof(int) + vc->max_size_ * sizeof(int)); + memcpy(tmp, vc->data_, vc->size_ * sizeof(int)); + free(vc->data_); + vc->data_ = tmp; + vc->max_size_ = vc->max_size_ + vc->per_malloc_size_; + } + memmove(vc->data_ + index + 1, vc->data_ + index, (vc->size_ - index) * sizeof(int)); + vc->data_[index] = value; + vc->size_++; +} + +void VectorCErase(VectorC *vc, int index) { + memmove(vc->data_ + index, vc->data_ + index + 1, (vc->size_ - index - 1) * sizeof(int)); + vc->size_--; +} + +bool VectorCEqual(VectorC *vc1, VectorC *vc2) { + if (vc1->size_ != vc2->size_) { + return false; + } + for (size_t i = 0; i < vc1->size_; i++) { + if (vc1->data_[i] != vc2->data_[i]) { + return false; + } + } + return true; +} + +void VectorCFree(VectorC *vc) { + free(vc->data_); + vc->data_ = NULL; +} diff --git a/mindspore/lite/nnacl/infer/common_infer.h b/mindspore/lite/nnacl/infer/common_infer.h new file mode 100644 index 0000000000..792dd98470 --- /dev/null +++ b/mindspore/lite/nnacl/infer/common_infer.h @@ -0,0 +1,209 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_NNACL_COMMON_H_ +#define MINDSPORE_LITE_NNACL_COMMON_H_ + +#include +#include "nnacl/errorcode.h" +#include "nnacl/op_base.h" +#include "nnacl/tensor_c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define kNCHW_N 0 +#define kNCHW_C 1 +#define kNCHW_H 2 +#define kNCHW_W 3 + +typedef enum FormatC { + Format_NCHW = 0, + Format_NHWC = 1, + Format_NHWC4 = 2, + Format_HWKC = 3, + Format_HWCK = 4, + Format_KCHW = 5, + Format_CKHW = 6, + Format_KHWC = 7, + Format_CHWK = 8, + Format_HW = 9, + Format_HW4 = 10, + Format_NC = 11, + Format_NC4 = 12, + Format_NC4HW4 = 100, + Format_NUM_OF_FORMAT = 101, + Format_MIN = Format_NCHW, + Format_MAX = Format_NUM_OF_FORMAT +} FormatC; + +typedef enum TypeIdC { + kTypeUnknown = 0, + kMetaTypeBegin = kTypeUnknown, + kMetaTypeType, // Type + kMetaTypeAnything, + kMetaTypeObject, + kMetaTypeTypeType, // TypeType + kMetaTypeProblem, + kMetaTypeExternal, + kMetaTypeNone, + kMetaTypeNull, + kMetaTypeEllipsis, + kMetaTypeEnd, + // + // Object types + // + kObjectTypeBegin = kMetaTypeEnd, + kObjectTypeNumber, + kObjectTypeString, + kObjectTypeList, + kObjectTypeTuple, + kObjectTypeSlice, + kObjectTypeKeyword, + kObjectTypeTensorType, + kObjectTypeRowTensorType, + kObjectTypeSparseTensorType, + kObjectTypeUndeterminedType, + kObjectTypeClass, + kObjectTypeDictionary, + kObjectTypeFunction, + kObjectTypeJTagged, + kObjectTypeSymbolicKeyType, + kObjectTypeEnvType, + kObjectTypeRefKey, + kObjectTypeRef, + kObjectTypeEnd, + // + // Number Types + // + kNumberTypeBegin = kObjectTypeEnd, + kNumberTypeBool, + kNumberTypeInt, + kNumberTypeInt8, + kNumberTypeInt16, + kNumberTypeInt32, + kNumberTypeInt64, + kNumberTypeUInt, + kNumberTypeUInt8, + kNumberTypeUInt16, + kNumberTypeUInt32, + kNumberTypeUInt64, + kNumberTypeFloat, + kNumberTypeFloat16, + kNumberTypeFloat32, + kNumberTypeFloat64, + kNumberTypeComplex64, + kNumberTypeEnd +} TypeIdC; + +enum NNACLLshProjectionType { + LshProjectionType_UNKNOWN = 0, + LshProjectionType_SPARSE = 1, + LshProjectionType_DENSE = 2, + LshProjectionType_MIN = LshProjectionType_UNKNOWN, + LshProjectionType_MAX = LshProjectionType_DENSE +}; + +enum NNACLQuantType { + QuantType_QUANT_NONE = 0, + QuantType_AwareTraining = 1, + QuantType_WeightQuant = 2, + QuantType_PostTraining = 3, + QuantType_MIN = QuantType_QUANT_NONE, + QuantType_MAX = QuantType_PostTraining +}; + +typedef struct vvector { + int **shape_; // value of shapes + int *shape_size_; // size of shape + size_t size_; // number of shapes +} vvector; + +typedef struct TensorListC { + int data_type_; + int format_; + + TensorC **tensors_; + size_t element_num_; + int tensors_data_type_; // element_data_type_, keep same as c++ + int element_shape_[MAX_SHAPE_SIZE]; + size_t element_shape_size_; + int max_elements_num_; +} TensorListC; + +typedef struct VectorC { + int *data_; + size_t size_; + size_t max_size_; + size_t per_malloc_size_; +} VectorC; + +int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, vvector *tensor_shape); +int TensorListMergeShape(int *element_shape, size_t *element_shape_size, const int *tmp, size_t tmp_size); +bool TensorListIsFullyDefined(int *shape, size_t shape_size); + +int GetBatch(const TensorC *tensor); +int GetHeight(const TensorC *tensor); +int GetWidth(const TensorC *tensor); +int GetChannel(const TensorC *tensor); +int GetElementNum(const TensorC *tensor); +int GetDimensionSize(const TensorC *tensor, const size_t index); + +int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); +int CheckAugmentNullSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj); +int CheckAugmentNullSizeInputTwo(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter, size_t inputs_size_obj_0, + size_t inputs_size_obj_1, size_t outputs_size_obj); +int CheckAugmentNullInputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t inputs_size_obj); +int CheckAugmentNullOutputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter, size_t outputs_size_obj); +void SetDataTypeFormat(TensorC *dst, const TensorC *src); + +int SetShapeTensor(TensorC *dst, const TensorC *src); +int SetShapeArray(TensorC *dst, int *src, size_t src_size); +int ShapeSet(int *dst_shape, size_t *dst_shape_size, const int *src_shape, size_t src_shape_size); +int ShapePush(int *shape, size_t *shape_size, int value); +int ShapeInsert(int *shape, size_t *shape_size, int index, int value); +int ShapeErase(int *shape, size_t *shape_size, int index); +bool ShapeEqual(const int *shape0, size_t shape0_size, const int *shape1, size_t shape1_size); + +void iswap(int *a, int *b); + +int imin(int a, int b); +int imax(int a, int b); + +int CommonInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); +int FftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +int VectorCInit(VectorC *vc, size_t per_malloc_size); +void VectorCSet(VectorC *vc, const int *src_shape, size_t src_shape_size); +void VectorCPush(VectorC *vc, int value); +void VectorCInsert(VectorC *vc, int index, int value); +void VectorCErase(VectorC *vc, int index); +bool VectorCEqual(VectorC *vc1, VectorC *vc2); +void VectorCFree(VectorC *vc); + +#ifdef __cplusplus +} +#endif + +#endif // MINDSPORE_LITE_NNACL_COMMON__H_ diff --git a/mindspore/lite/nnacl/infer/concat_infer.c b/mindspore/lite/nnacl/infer/concat_infer.c new file mode 100644 index 0000000000..330801c16b --- /dev/null +++ b/mindspore/lite/nnacl/infer/concat_infer.c @@ -0,0 +1,73 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/concat_infer.h" + +int ConcatInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input0 = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input0); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const int *input0_shape = inputs[0]->shape_; + size_t input0_shape_size = inputs[0]->shape_size_; + + ConcatParameter *param = (ConcatParameter *)parameter; + int axis = param->axis_ < 0 ? param->axis_ + input0_shape_size : param->axis_; + if (axis < 0 || axis >= input0_shape_size) { + return NNACL_ERR; + } + int input0_shape_without_axis[MAX_SHAPE_SIZE]; + size_t input0_shape_without_axis_size = 0; + ShapeSet(input0_shape_without_axis, &input0_shape_without_axis_size, input0_shape, input0_shape_size); + ShapeErase(input0_shape_without_axis, &input0_shape_without_axis_size, axis); + int output_axis_dim = input0_shape[axis]; + for (size_t i = 1; i < inputs_size; ++i) { + int shape_tmp[MAX_SHAPE_SIZE]; + size_t shape_tmp_size = 0; + ShapeSet(shape_tmp, &shape_tmp_size, inputs[i]->shape_, inputs[i]->shape_size_); + if (shape_tmp_size != input0_shape_size) { + return NNACL_PARAM_INVALID; + } + if ((inputs[i]->data_type_ != output->data_type_) && + !((inputs[i]->data_type_ == kNumberTypeFloat16 && output->data_type_ == kNumberTypeFloat32) || + (inputs[i]->data_type_ == kNumberTypeFloat32 && output->data_type_ == kNumberTypeFloat16))) { + return NNACL_PARAM_INVALID; + } + int axis_tmp = shape_tmp[axis]; + ShapeErase(shape_tmp, &shape_tmp_size, axis); + if (!ShapeEqual(input0_shape_without_axis, input0_shape_without_axis_size, shape_tmp, shape_tmp_size)) { + return NNACL_ERR; + } + output_axis_dim += axis_tmp; + } + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input0_shape_size; + for (size_t i = 0; i < input0_shape_size; i++) { + output_shape[i] = input0_shape[i]; + } + output_shape[axis] = output_axis_dim; + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/concat_infer.h b/mindspore/lite/nnacl/infer/concat_infer.h new file mode 100644 index 0000000000..08f3b8ff78 --- /dev/null +++ b/mindspore/lite/nnacl/infer/concat_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CONCAT_INFER_H +#define MINDSPORE_LITE_NNACL_CONCAT_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/concat_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ConcatInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CONCAT_INFER_H diff --git a/mindspore/lite/nnacl/infer/constant_of_shape_infer.c b/mindspore/lite/nnacl/infer/constant_of_shape_infer.c new file mode 100644 index 0000000000..49bd00995e --- /dev/null +++ b/mindspore/lite/nnacl/infer/constant_of_shape_infer.c @@ -0,0 +1,64 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/constant_of_shape_infer.h" + +int ConstantOfShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *in_tensor = inputs[0]; + TensorC *out_tensor = outputs[0]; + ConstantOfShapeParameter *param = (ConstantOfShapeParameter *)parameter; + out_tensor->data_type_ = (TypeIdC)(param->data_type_); + out_tensor->format_ = in_tensor->format_; + if (!parameter->infer_flag_ || in_tensor->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int size = GetElementNum(in_tensor); + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = size; + switch (in_tensor->data_type_) { + case kNumberTypeInt32: { + int32_t *in_data = (int32_t *)(in_tensor->data_); + for (int i = 0; i < size; ++i) { + out_shape[i] = in_data[i]; + if (out_shape[i] <= 0) { + return NNACL_ERR; + } + } + break; + } + case kNumberTypeInt64: { + int64_t *in_data = (int64_t *)(in_tensor->data_); + for (int i = 0; i < size; ++i) { + out_shape[i] = in_data[i]; + if (out_shape[i] <= 0) { + return NNACL_ERR; + } + } + break; + } + default: + return NNACL_INFER_INVALID; + } + + SetShapeArray(out_tensor, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/constant_of_shape_infer.h b/mindspore/lite/nnacl/infer/constant_of_shape_infer.h new file mode 100644 index 0000000000..2c51287201 --- /dev/null +++ b/mindspore/lite/nnacl/infer/constant_of_shape_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H +#define MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/constant_of_shape.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ConstantOfShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CONSTANT_OF_SHAPE_INFER_H diff --git a/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.c b/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.c new file mode 100644 index 0000000000..6f2bfeeb8e --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.c @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/conv2d_grad_filter_infer.h" + +int Conv2dGradFilterInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size < 2 || outputs_size != 1) { + return NNACL_ERR; + } + SetDataTypeFormat(outputs[0], inputs[0]); + + size_t filter_shape_size_ = 4; + int filter_shape_[MAX_SHAPE_SIZE]; + const int nchw2nhwc[4] = {0, 2, 3, 1}; + for (size_t i = 0; i < filter_shape_size_; i++) { + filter_shape_[i] = *((int *)(inputs[2]->data_) + nchw2nhwc[i]); + } + + SetShapeArray(outputs[0], filter_shape_, filter_shape_size_); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.h b/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.h new file mode 100644 index 0000000000..2fa82c41de --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_grad_filter_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H +#define MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int Conv2dGradFilterInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CONV2D_GRAD_FILTER_INFER_H diff --git a/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.c b/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.c new file mode 100644 index 0000000000..5aa6f929a4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.c @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/conv2d_grad_input_infer.h" + +int Conv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size < 2 || outputs_size != 1) { + return NNACL_ERR; + } + const TensorC *in0 = inputs[0]; + TensorC *out = outputs[0]; + + if (in0 == NULL || out == NULL) { + return NNACL_NULL_PTR; + } + SetDataTypeFormat(out, in0); + + size_t shape_size_ = in0->shape_size_; + int shape_[MAX_SHAPE_SIZE]; + const int nchw2nhwc[4] = {0, 2, 3, 1}; + for (int i = 0; i < shape_size_; i++) { + shape_[i] = *((int *)(inputs[2]->data_) + nchw2nhwc[i]); + } + SetShapeArray(out, shape_, shape_size_); + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.h b/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.h new file mode 100644 index 0000000000..4ea80be53f --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_grad_input_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H +#define MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int Conv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CONV2D_GRAD_INPUT_INFER_H diff --git a/mindspore/lite/nnacl/infer/conv2d_infer.c b/mindspore/lite/nnacl/infer/conv2d_infer.c new file mode 100644 index 0000000000..5a64063a3c --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_infer.c @@ -0,0 +1,99 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/conv2d_infer.h" + +void ConvInferShape(int input_h, int input_w, int *output_h, int *output_w, ConvParameter *param) { + int kernel_w = param->kernel_w_; + int kernel_h = param->kernel_h_; + int stride_w = param->stride_w_; + int stride_h = param->stride_h_; + int dilate_w = param->dilation_w_; + int dilate_h = param->dilation_h_; + + if (param->pad_mode_ == Pad_same) { // maybe error + *output_w = ceil((float)(input_w) / (float)(stride_w)); + *output_h = ceil((float)(input_h) / (float)(stride_h)); + int pad_h_all = ((*output_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - input_h); + int pad_w_all = ((*output_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - input_w); + if (pad_h_all < 0) { + param->pad_u_ = param->pad_d_ = 0; + } else { + param->pad_u_ = pad_h_all / 2; + param->pad_d_ = pad_h_all - param->pad_u_; + } + if (pad_w_all < 0) { + param->pad_l_ = param->pad_r_ = 0; + } else { + param->pad_l_ = pad_w_all / 2; + param->pad_r_ = pad_w_all - param->pad_l_; + } + } else { + *output_w = ceil(((float)(input_w) + param->pad_l_ + param->pad_r_ - ((float)(kernel_w)-1) * (float)(dilate_w)) / + (float)(stride_w)); + *output_h = ceil(((float)(input_h) + param->pad_u_ + param->pad_d_ - ((float)(kernel_h)-1) * (float)(dilate_h)) / + (float)(stride_h)); + } +} + +int Conv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input_tensor = inputs[0]; + const TensorC *weight_tensor = inputs[1]; + TensorC *out_tensor = outputs[0]; + + out_tensor->format_ = input_tensor->format_; + out_tensor->data_type_ = input_tensor->data_type_; + ConvParameter *param = (ConvParameter *)parameter; + if (param->group_ == 0) { + param->group_ = weight_tensor->shape_[0]; + } + param->output_channel_ = weight_tensor->shape_[0]; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + const int *in_shape = input_tensor->shape_; + if (input_tensor->shape_size_ == 0) { + return NNACL_INFER_INVALID; + } + int input_h = in_shape[1]; + int input_w = in_shape[2]; + int output_w = 0, output_h = 0; + + ConvInferShape(input_h, input_w, &output_h, &output_w, param); + + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, input_tensor->shape_, input_tensor->shape_size_); + out_shape[1] = output_h >= 0 ? output_h : 1; + out_shape[2] = output_w >= 0 ? output_w : 1; + out_shape[3] = GetBatch(weight_tensor); + SetShapeArray(out_tensor, out_shape, out_shape_size); + + param->input_batch_ = in_shape[0]; + param->input_h_ = in_shape[1]; + param->input_w_ = in_shape[2]; + param->input_channel_ = in_shape[3]; + param->output_batch_ = out_shape[0]; + param->output_h_ = out_shape[1]; + param->output_w_ = out_shape[2]; + param->output_channel_ = out_shape[3]; + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/conv2d_infer.h b/mindspore/lite/nnacl/infer/conv2d_infer.h new file mode 100644 index 0000000000..ee0d291b6a --- /dev/null +++ b/mindspore/lite/nnacl/infer/conv2d_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CONV2D_INFER_H +#define MINDSPORE_LITE_NNACL_CONV2D_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int Conv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CONV2D_INFER_H diff --git a/mindspore/lite/nnacl/infer/crop_and_resize_infer.c b/mindspore/lite/nnacl/infer/crop_and_resize_infer.c new file mode 100644 index 0000000000..fdbdbd9e53 --- /dev/null +++ b/mindspore/lite/nnacl/infer/crop_and_resize_infer.c @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/crop_and_resize_infer.h" + +int CropAndResizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 4); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + if (input->shape_size_ != 0 && input->shape_size_ != 4) { + return NNACL_ERR; + } + + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + if (inputs[1]->data_ != NULL) { + const TensorC *boxes_tensor = inputs[1]; + ShapePush(output_shape, &output_shape_size, boxes_tensor->shape_[0]); + } else { + ShapePush(output_shape, &output_shape_size, GetBatch(input)); + } + + const TensorC *shape_tensor = inputs[3]; + int32_t *data = (int32_t *)(shape_tensor->data_); + if (data == NULL) { + return NNACL_INFER_INVALID; + } + ShapePush(output_shape, &output_shape_size, data[0]); + ShapePush(output_shape, &output_shape_size, data[1]); + ShapePush(output_shape, &output_shape_size, GetChannel(input)); + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/crop_and_resize_infer.h b/mindspore/lite/nnacl/infer/crop_and_resize_infer.h new file mode 100644 index 0000000000..0d0858839a --- /dev/null +++ b/mindspore/lite/nnacl/infer/crop_and_resize_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CROP_AND_RESIZE_INFER_H +#define MINDSPORE_LITE_NNACL_CROP_AND_RESIZE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int CropAndResizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CROP_AND_RESIZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/crop_infer.c b/mindspore/lite/nnacl/infer/crop_infer.c new file mode 100644 index 0000000000..f815d30773 --- /dev/null +++ b/mindspore/lite/nnacl/infer/crop_infer.c @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/crop_infer.h" + +int CropInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + SetDataTypeFormat(outputs[0], inputs[0]); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(outputs[0], inputs[1]); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/crop_infer.h b/mindspore/lite/nnacl/infer/crop_infer.h new file mode 100644 index 0000000000..dd6de645f3 --- /dev/null +++ b/mindspore/lite/nnacl/infer/crop_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CROP_INFER_H +#define MINDSPORE_LITE_NNACL_CROP_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/crop_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int CropInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CROP_INFER_H diff --git a/mindspore/lite/nnacl/infer/custom_extract_features_infer.c b/mindspore/lite/nnacl/infer/custom_extract_features_infer.c new file mode 100644 index 0000000000..4fa4a09b0d --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_extract_features_infer.c @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/custom_extract_features_infer.h" + +int CustomExtractFeaturesInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output0 = outputs[0]; + TensorC *output1 = outputs[1]; + + output0->data_type_ = kNumberTypeInt32; + output0->format_ = input->format_; + output1->data_type_ = kNumberTypeFloat32; + output1->format_ = input->format_; + + if (input->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int string_num = *((const int32_t *)(input->data_)); + + int res = (string_num == 0 ? 1 : string_num); + output0->shape_size_ = 1; + output0->shape_[0] = res; + output1->shape_size_ = 1; + output1->shape_[0] = res; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/custom_extract_features_infer.h b/mindspore/lite/nnacl/infer/custom_extract_features_infer.h new file mode 100644 index 0000000000..af518e60ce --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_extract_features_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H +#define MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int CustomExtractFeaturesInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CUSTOM_EXTRACT_FEATURES_INFER_H diff --git a/mindspore/lite/nnacl/infer/custom_normalize_infer.c b/mindspore/lite/nnacl/infer/custom_normalize_infer.c new file mode 100644 index 0000000000..5ed8b9f323 --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_normalize_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/custom_normalize_infer.h" + +int CustomNormalizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + + if (input->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int string_num = *((const int32_t *)(input->data_)); // also look custom_extract_features + + output->shape_size_ = 1; + output->shape_[0] = (string_num == 0 ? 1 : string_num); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/custom_normalize_infer.h b/mindspore/lite/nnacl/infer/custom_normalize_infer.h new file mode 100644 index 0000000000..6fe40cfc51 --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_normalize_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H +#define MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int CustomNormalizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CUSTOM_NORMALIZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/custom_predict_infer.c b/mindspore/lite/nnacl/infer/custom_predict_infer.c new file mode 100644 index 0000000000..bd119033cd --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_predict_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/custom_predict_infer.h" + +int CustomPredictInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output0 = outputs[0]; + TensorC *output1 = outputs[1]; + + CustomPredictParameter *param = (CustomPredictParameter *)parameter; + output0->shape_size_ = 1; + output0->shape_[0] = param->output_num; + output0->data_type_ = kNumberTypeInt32; + output0->format_ = input->format_; + output1->shape_size_ = 1; + output1->shape_[0] = param->output_num; + output1->data_type_ = kNumberTypeFloat32; + output1->format_ = input->format_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/custom_predict_infer.h b/mindspore/lite/nnacl/infer/custom_predict_infer.h new file mode 100644 index 0000000000..4df7628e5e --- /dev/null +++ b/mindspore/lite/nnacl/infer/custom_predict_infer.h @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H +#define MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct CustomPredictParameter { + OpParameter op_parameter_; + int output_num; +} CustomPredictParameter; + +int CustomPredictInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_CUSTOM_PREDICT_INFER_H diff --git a/mindspore/lite/nnacl/infer/deconv2d_infer.c b/mindspore/lite/nnacl/infer/deconv2d_infer.c new file mode 100644 index 0000000000..2f37906834 --- /dev/null +++ b/mindspore/lite/nnacl/infer/deconv2d_infer.c @@ -0,0 +1,97 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/deconv2d_infer.h" + +int Deconv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + const TensorC *weight = inputs[1]; + TensorC *output = outputs[0]; + output->format_ = input->format_; + output->data_type_ = input->data_type_; + + ConvParameter *param = (ConvParameter *)parameter; + if (param->group_ == 0) { + param->group_ = weight->shape_[0]; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int32_t input_h = GetHeight(input); + int32_t input_w = GetWidth(input); + + int32_t output_n = GetBatch(input); + int32_t output_h = 0; + int32_t output_w = 0; + int32_t output_c = GetChannel(weight); + if (param->group_ == GetChannel(input) && param->group_ == GetBatch(weight) && 1 == GetChannel(weight)) { + output_c = GetBatch(weight); /* depthwise */ + } + + int kernel_w = param->kernel_w_; + int kernel_h = param->kernel_h_; + int stride_w = param->stride_w_; + int stride_h = param->stride_h_; + int dilate_w = param->dilation_w_; + int dilate_h = param->dilation_h_; + int pad_mode = param->pad_mode_; + if (pad_mode == Pad_pad) { + output_h = (input_h - 1) * stride_h + ((kernel_h - 1) * dilate_h + 1) - param->pad_u_ - param->pad_d_; + output_w = (input_w - 1) * stride_w + ((kernel_w - 1) * dilate_w + 1) - param->pad_l_ - param->pad_r_; + } else if (pad_mode == Pad_same) { + output_h = input_h * stride_h; + output_w = input_w * stride_w; + } else if (pad_mode == Pad_valid) { + output_h = (input_h - 1) * stride_h + kernel_h; + output_w = (input_w - 1) * stride_w + kernel_w; + } else { + return NNACL_ERR; + } + + output_h += param->output_padding_h; + output_w += param->output_padding_w; + + output->shape_size_ = 4; + output->shape_[0] = output_n; + output->shape_[1] = output_h; + output->shape_[2] = output_w; + output->shape_[3] = output_c; + + if (pad_mode == Pad_same) { + param->pad_u_ = ((input_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - output_h) / 2; + param->pad_l_ = ((input_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - output_w) / 2; + } else if (pad_mode == Pad_valid) { + param->pad_u_ = 0; + param->pad_l_ = 0; + } + + const int *in_shape = input->shape_; + param->input_batch_ = in_shape[0]; + param->input_h_ = in_shape[1]; + param->input_w_ = in_shape[2]; + param->input_channel_ = in_shape[3]; + param->output_batch_ = output_n; + param->output_h_ = output_h; + param->output_w_ = output_w; + param->output_channel_ = output_c; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/deconv2d_infer.h b/mindspore/lite/nnacl/infer/deconv2d_infer.h new file mode 100644 index 0000000000..0563a9c6e9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/deconv2d_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DECONV2D_INFER_H +#define MINDSPORE_LITE_NNACL_DECONV2D_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int Deconv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DECONV2D_INFER_H diff --git a/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.c b/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.c new file mode 100644 index 0000000000..a62c311a20 --- /dev/null +++ b/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.c @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/dedepthwise_conv2d_infer.h" + +int DeDepthwiseConv2DInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int input_h = input->shape_[1]; + int input_w = input->shape_[2]; + int input_channel = input->shape_[3]; + int output_w = 0, output_h = 0; + + ConvParameter *param = (ConvParameter *)parameter; + output_h = param->stride_h_ * (input_h - 1) + param->kernel_h_ - param->pad_u_ - param->pad_d_; + output_w = param->stride_w_ * (input_w - 1) + param->kernel_w_ - param->pad_l_ - param->pad_r_; + if ((output_h + param->pad_u_ + param->pad_d_ - param->kernel_h_) % param->stride_h_ != 0) { + output_h += (output_h + param->pad_l_ + param->pad_r_ - param->kernel_h_) % param->stride_h_; + } + if ((output_w + param->pad_l_ + param->pad_r_ - param->kernel_w_) % param->stride_w_ != 0) { + output_w += (output_w + param->pad_l_ + param->pad_r_ - param->kernel_w_) % param->stride_w_; + } + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_); + out_shape[1] = output_h; + out_shape[2] = output_w; + if (param->channel_multiplie_ != 1) { + return NNACL_ERR; + } + out_shape[3] = input_channel; // in_channel * out_channel + + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.h b/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.h new file mode 100644 index 0000000000..59f295e141 --- /dev/null +++ b/mindspore/lite/nnacl/infer/dedepthwise_conv2d_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H +#define MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DeDepthwiseConv2DInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DEDEPTHWISE_CONV2D_INFER_H diff --git a/mindspore/lite/nnacl/infer/depth_to_space_infer.c b/mindspore/lite/nnacl/infer/depth_to_space_infer.c new file mode 100644 index 0000000000..81fdb94006 --- /dev/null +++ b/mindspore/lite/nnacl/infer/depth_to_space_infer.c @@ -0,0 +1,54 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/depth_to_space_infer.h" + +int DepthToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + if (input->format_ != Format_NHWC) { + return NNACL_ERR; + } + SetDataTypeFormat(outputs[0], input); + DepthToSpaceParameter *param = (DepthToSpaceParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_); + if (input_shape_size != 4) { + return NNACL_PARAM_INVALID; + } + + int32_t block_size = param->block_size_; + if (input_shape[kNHWC_C] % (block_size * block_size) != 0 || input_shape[kNHWC_C] == 0) { + return NNACL_PARAM_INVALID; + } + int32_t output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input_shape_size; + output_shape[kNHWC_N] = input_shape[kNHWC_N]; + output_shape[kNHWC_H] = input_shape[kNHWC_H] * block_size; + output_shape[kNHWC_W] = input_shape[kNHWC_W] * block_size; + output_shape[kNHWC_C] = input_shape[kNHWC_C] / (block_size * block_size); + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/depth_to_space_infer.h b/mindspore/lite/nnacl/infer/depth_to_space_infer.h new file mode 100644 index 0000000000..be114f56e8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/depth_to_space_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H +#define MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/depth_to_space_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DepthToSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DEPTHTOSPACE_INFER_H diff --git a/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.c b/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.c new file mode 100644 index 0000000000..9a60c6006a --- /dev/null +++ b/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.c @@ -0,0 +1,71 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/depthwise_conv2d_infer.h" + +int DepthwiseConv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + ConvParameter *param = (ConvParameter *)parameter; + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int input_h = input->shape_[1]; + int input_w = input->shape_[2]; + int input_channel = input->shape_[3]; + int output_w = 0, output_h = 0; + param->input_channel_ = input_channel; + + if (param->pad_mode_ == Pad_same) { + output_h = ceil((float)(input_h) / (float)(param->stride_h_)); + output_w = ceil((float)(input_w) / (float)(param->stride_w_)); + int pad_h_all = ((output_h - 1) * param->stride_h_ + (param->kernel_h_ - 1) * param->dilation_h_ + 1 - input_h); + int pad_w_all = ((output_w - 1) * param->stride_w_ + (param->kernel_w_ - 1) * param->dilation_w_ + 1 - input_w); + if (pad_h_all > 0) { + param->pad_u_ = pad_h_all / 2; + param->pad_d_ = pad_h_all - param->pad_u_; + } + if (pad_w_all > 0) { + param->pad_l_ = pad_w_all / 2; + param->pad_r_ = pad_w_all - param->pad_l_; + } + } else { + output_h = ceil(((float)(input_h) + param->pad_u_ + param->pad_d_ - + ((float)(param->kernel_h_) - 1) * (float)(param->dilation_h_)) / + (float)(param->stride_h_)); + output_w = ceil(((float)(input_w) + param->pad_l_ + param->pad_r_ - + ((float)(param->kernel_w_) - 1) * (float)(param->dilation_w_)) / + (float)(param->stride_w_)); + } + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_); + out_shape[1] = output_h; + out_shape[2] = output_w; + if (param->channel_multiplie_ != 1) { + return NNACL_ERR; + } + out_shape[3] = input_channel; // in_channel * out_channel + SetShapeArray(output, out_shape, out_shape_size); + return 0; +} diff --git a/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.h b/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.h new file mode 100644 index 0000000000..799279a1c7 --- /dev/null +++ b/mindspore/lite/nnacl/infer/depthwise_conv2d_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H +#define MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DepthwiseConv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DEPTHWISE_CONV2D_INFER_H diff --git a/mindspore/lite/nnacl/infer/detection_post_process_infer.c b/mindspore/lite/nnacl/infer/detection_post_process_infer.c new file mode 100644 index 0000000000..4a5a883507 --- /dev/null +++ b/mindspore/lite/nnacl/infer/detection_post_process_infer.c @@ -0,0 +1,76 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/detection_post_process_infer.h" + +int DetectionPostProcessInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 4); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *boxes = inputs[0]; + const TensorC *scores = inputs[1]; + const TensorC *anchors = inputs[2]; + + DetectionPostProcessParameter *param = (DetectionPostProcessParameter *)parameter; + if (scores->shape_[2] < param->num_classes_) { + return NNACL_ERR; + } + if (scores->shape_[2] - param->num_classes_ > 1) { + return NNACL_ERR; + } + if (boxes->shape_[1] != scores->shape_[1]) { + return NNACL_ERR; + } + if (boxes->shape_[1] != anchors->shape_[0]) { + return NNACL_ERR; + } + + TensorC *detected_boxes = outputs[0]; + TensorC *detected_classes = outputs[1]; + TensorC *detected_scores = outputs[2]; + TensorC *num_det = outputs[3]; + + detected_boxes->format_ = boxes->format_; + detected_boxes->data_type_ = kNumberTypeFloat32; + detected_classes->format_ = boxes->format_; + detected_classes->data_type_ = kNumberTypeFloat32; + detected_scores->format_ = boxes->format_; + detected_scores->data_type_ = kNumberTypeFloat32; + num_det->format_ = boxes->format_; + num_det->data_type_ = kNumberTypeFloat32; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + const int max_detections = param->max_detections_; + const int max_classes_per_detection = param->max_classes_per_detection_; + const int num_detected_boxes = (int)(max_detections * max_classes_per_detection); + detected_boxes->shape_size_ = 3; + detected_boxes->shape_[0] = 1; + detected_boxes->shape_[1] = num_detected_boxes; + detected_boxes->shape_[2] = 4; + detected_classes->shape_size_ = 2; + detected_classes->shape_[0] = 1; + detected_classes->shape_[1] = num_detected_boxes; + detected_scores->shape_size_ = 2; + detected_scores->shape_[0] = 1; + detected_scores->shape_[1] = num_detected_boxes; + num_det->shape_size_ = 1; + num_det->shape_[0] = 1; + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/detection_post_process_infer.h b/mindspore/lite/nnacl/infer/detection_post_process_infer.h new file mode 100644 index 0000000000..f5ac10500f --- /dev/null +++ b/mindspore/lite/nnacl/infer/detection_post_process_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H +#define MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/detection_post_process_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DetectionPostProcessInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DETECTION_POST_PROCESS_INFER_H diff --git a/mindspore/lite/nnacl/infer/dropout_grad_infer.c b/mindspore/lite/nnacl/infer/dropout_grad_infer.c new file mode 100644 index 0000000000..b759ae93ed --- /dev/null +++ b/mindspore/lite/nnacl/infer/dropout_grad_infer.c @@ -0,0 +1,33 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/dropout_grad_infer.h" + +int DropoutGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/dropout_grad_infer.h b/mindspore/lite/nnacl/infer/dropout_grad_infer.h new file mode 100644 index 0000000000..b88bfe11da --- /dev/null +++ b/mindspore/lite/nnacl/infer/dropout_grad_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DropoutGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DROPOUT_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/dropout_infer.c b/mindspore/lite/nnacl/infer/dropout_infer.c new file mode 100644 index 0000000000..c5ca932d9c --- /dev/null +++ b/mindspore/lite/nnacl/infer/dropout_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/dropout_infer.h" + +int DropoutInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output0 = outputs[0]; + SetDataTypeFormat(output0, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(output0, input); + if (outputs_size > 1) { + TensorC *output1 = outputs[1]; + SetDataTypeFormat(output1, input); + SetShapeTensor(output1, input); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/dropout_infer.h b/mindspore/lite/nnacl/infer/dropout_infer.h new file mode 100644 index 0000000000..9e13f939c4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/dropout_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_DROPOUT_INFER_H +#define MINDSPORE_LITE_NNACL_DROPOUT_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int DropoutInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_DROPOUT_INFER_H diff --git a/mindspore/lite/nnacl/infer/embedding_lookup_infer.c b/mindspore/lite/nnacl/infer/embedding_lookup_infer.c new file mode 100644 index 0000000000..4d58ebbcbb --- /dev/null +++ b/mindspore/lite/nnacl/infer/embedding_lookup_infer.c @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/embedding_lookup_infer.h" + +int EmbeddingLookupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size < 2 || outputs_size != 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + const TensorC *params_ = inputs[0]; + const TensorC *ids = inputs[inputs_size - 1]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, params_); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int embedding_shape[MAX_SHAPE_SIZE]; + size_t embedding_shape_size = 0; + ShapeSet(embedding_shape, &embedding_shape_size, params_->shape_, params_->shape_size_); + ShapeErase(embedding_shape, &embedding_shape_size, 0); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapeSet(output_shape, &output_shape_size, ids->shape_, ids->shape_size_); + for (size_t i = 0; i < embedding_shape_size; ++i) { + ShapePush(output_shape, &output_shape_size, embedding_shape[i]); + } + for (size_t i = 1; i < inputs_size - 1; ++i) { + int embedding_shape_t[MAX_SHAPE_SIZE]; + size_t embedding_shape_t_size = 0; + ShapeSet(embedding_shape_t, &embedding_shape_t_size, inputs[i]->shape_, inputs[i]->shape_size_); + ShapeErase(embedding_shape_t, &embedding_shape_t_size, 0); + bool t_equal = ShapeEqual(embedding_shape_t, embedding_shape_t_size, embedding_shape, embedding_shape_size); + if (!t_equal) { + return NNACL_INPUT_TENSOR_ERROR; + } + } + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/embedding_lookup_infer.h b/mindspore/lite/nnacl/infer/embedding_lookup_infer.h new file mode 100644 index 0000000000..642cf2e65a --- /dev/null +++ b/mindspore/lite/nnacl/infer/embedding_lookup_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H +#define MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int EmbeddingLookupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_EMBEDDING_LOOKUP_INFER_H diff --git a/mindspore/lite/nnacl/infer/expand_dims_infer.c b/mindspore/lite/nnacl/infer/expand_dims_infer.c new file mode 100644 index 0000000000..08d1691eac --- /dev/null +++ b/mindspore/lite/nnacl/infer/expand_dims_infer.c @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/expand_dims_infer.h" + +int ExpandDimsInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int dim = ((int32_t *)(inputs[1]->data_))[0]; + if (dim < 0) { + dim += input->shape_size_ + 1; + } + if (dim > (int)(input->shape_size_)) { + return NNACL_INPUT_TENSOR_ERROR; + } + + ShapeSet(output->shape_, &(output->shape_size_), input->shape_, input->shape_size_); + ShapeInsert(output->shape_, &(output->shape_size_), dim, 1); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/expand_dims_infer.h b/mindspore/lite/nnacl/infer/expand_dims_infer.h new file mode 100644 index 0000000000..9005d75d13 --- /dev/null +++ b/mindspore/lite/nnacl/infer/expand_dims_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_EXPAND_DIMS_INFER_H +#define MINDSPORE_LITE_NNACL_EXPAND_DIMS_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ExpandDimsInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_EXPAND_DIMS_INFER_H diff --git a/mindspore/lite/nnacl/infer/fft_imag_infer.c b/mindspore/lite/nnacl/infer/fft_imag_infer.c new file mode 100644 index 0000000000..81bf648e7a --- /dev/null +++ b/mindspore/lite/nnacl/infer/fft_imag_infer.c @@ -0,0 +1,22 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/fft_imag_infer.h" + +int FftImagInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + return FftInferShape(inputs, inputs_size, outputs, outputs_size, parameter); +} diff --git a/mindspore/lite/nnacl/infer/fft_imag_infer.h b/mindspore/lite/nnacl/infer/fft_imag_infer.h new file mode 100644 index 0000000000..df816e6397 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fft_imag_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FFT_IMAG_INFER_H +#define MINDSPORE_LITE_NNACL_FFT_IMAG_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FftImagInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FFT_IMAG_INFER_H diff --git a/mindspore/lite/nnacl/infer/fft_real_infer.c b/mindspore/lite/nnacl/infer/fft_real_infer.c new file mode 100644 index 0000000000..fcd4cc1a50 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fft_real_infer.c @@ -0,0 +1,22 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/fft_real_infer.h" + +int FftRealInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + return FftInferShape(inputs, inputs_size, outputs, outputs_size, parameter); +} diff --git a/mindspore/lite/nnacl/infer/fft_real_infer.h b/mindspore/lite/nnacl/infer/fft_real_infer.h new file mode 100644 index 0000000000..b3410ead4d --- /dev/null +++ b/mindspore/lite/nnacl/infer/fft_real_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FFT_REAL_INFER_H +#define MINDSPORE_LITE_NNACL_FFT_REAL_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FftRealInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FFT_REAL_INFER_H diff --git a/mindspore/lite/nnacl/infer/fill_infer.c b/mindspore/lite/nnacl/infer/fill_infer.c new file mode 100644 index 0000000000..8f68e035e6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fill_infer.c @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/fill_infer.h" + +int FillInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + const TensorC *dst_shape_tensor = inputs[1]; + const int32_t *dst_shape = (int32_t *)(dst_shape_tensor->data_); + size_t num_dims = 1; + for (size_t i = 0; i < dst_shape_tensor->shape_size_; ++i) { + num_dims *= dst_shape_tensor->shape_[i]; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (size_t i = 0; i < num_dims; i++) { + ShapePush(output_shape, &output_shape_size, dst_shape[i]); + } + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/fill_infer.h b/mindspore/lite/nnacl/infer/fill_infer.h new file mode 100644 index 0000000000..535a7d84a3 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fill_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FILL_INFER_H +#define MINDSPORE_LITE_NNACL_FILL_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FillInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FILL_INFER_H diff --git a/mindspore/lite/nnacl/infer/flatten_grad_infer.c b/mindspore/lite/nnacl/infer/flatten_grad_infer.c new file mode 100644 index 0000000000..96d96f59ec --- /dev/null +++ b/mindspore/lite/nnacl/infer/flatten_grad_infer.c @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/flatten_grad_infer.h" + +int FlattenGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int output_shape[2]; + size_t output_shape_size = 2; + output_shape[0] = input->shape_[0]; + output_shape[1] = 1; + for (size_t i = 1; i < input->shape_size_; i++) { + output_shape[1] *= input->shape_[i]; + } + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/flatten_grad_infer.h b/mindspore/lite/nnacl/infer/flatten_grad_infer.h new file mode 100644 index 0000000000..532ebe591d --- /dev/null +++ b/mindspore/lite/nnacl/infer/flatten_grad_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FLATTEN_GRAD_INFER_INFER_H +#define MINDSPORE_LITE_NNACL_FLATTEN_GRAD_INFER_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FlattenGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FLATTEN_GRAD_INFER_INFER_H diff --git a/mindspore/lite/nnacl/infer/flatten_infer.c b/mindspore/lite/nnacl/infer/flatten_infer.c new file mode 100644 index 0000000000..217ce0c62e --- /dev/null +++ b/mindspore/lite/nnacl/infer/flatten_infer.c @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/flatten_infer.h" + +int FlattenInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_); + int output_shape[2]; + output_shape[0] = input_shape[0]; + output_shape[1] = 1; + for (size_t i = 1; i < input_shape_size; i++) { + output_shape[1] *= input_shape[i]; + } + SetShapeArray(output, output_shape, 2); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/flatten_infer.h b/mindspore/lite/nnacl/infer/flatten_infer.h new file mode 100644 index 0000000000..f71e25829d --- /dev/null +++ b/mindspore/lite/nnacl/infer/flatten_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FLATTEN_INFER_H +#define MINDSPORE_LITE_NNACL_FLATTEN_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FlattenInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FLATTEN_INFER_H diff --git a/mindspore/lite/nnacl/infer/full_connection_infer.c b/mindspore/lite/nnacl/infer/full_connection_infer.c new file mode 100644 index 0000000000..6e3e9c2382 --- /dev/null +++ b/mindspore/lite/nnacl/infer/full_connection_infer.c @@ -0,0 +1,74 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/full_connection_infer.h" + +int FullConnectionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input0 = inputs[0]; + const TensorC *input1 = inputs[1]; + TensorC *output = outputs[0]; + MatMulParameter *param = (MatMulParameter *)parameter; + SetDataTypeFormat(output, input0); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if ((param->has_bias_ && inputs_size != 3) || (!param->has_bias_ && inputs_size != 2)) { + return NNACL_INPUT_TENSOR_ERROR; + } + if (param->use_axis_ && (param->axis_ < 1 || param->axis_ > (int)(input0->shape_size_))) { + return NNACL_ERR; + } + int new_k = 1; + if (param->use_axis_) { + for (size_t i = param->axis_; i < input0->shape_size_; ++i) { + new_k *= input0->shape_[i]; + } + if (new_k != input1->shape_[1]) { + return NNACL_INPUT_TENSOR_ERROR; + } + } else { + new_k = input1->shape_[1]; + } + if (param->has_bias_) { + if (inputs[2]->shape_[0] != input1->shape_[0]) { + return NNACL_INPUT_TENSOR_ERROR; + } + } + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, inputs[0]->shape_, inputs[0]->shape_size_); + if (param->use_axis_) { + out_shape_size = param->axis_ + 1; + out_shape[param->axis_] = input1->shape_[0]; + } else { + int total = 1; + for (size_t i = 0; i < input0->shape_size_; ++i) { + total *= input0->shape_[i]; + } + out_shape_size = 2; + int batch_size = total / new_k; + out_shape[0] = batch_size; + out_shape[1] = input1->shape_[0]; + } + SetShapeArray(output, out_shape, out_shape_size); + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/full_connection_infer.h b/mindspore/lite/nnacl/infer/full_connection_infer.h new file mode 100644 index 0000000000..dc3ef3cfa8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/full_connection_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FULL_CONNECTION_INFER_H +#define MINDSPORE_LITE_NNACL_FULL_CONNECTION_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/matmul_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FullConnectionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FULL_CONNECTION_INFER_H diff --git a/mindspore/lite/nnacl/infer/fused_batchnorm_infer.c b/mindspore/lite/nnacl/infer/fused_batchnorm_infer.c new file mode 100644 index 0000000000..d3428bf440 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fused_batchnorm_infer.c @@ -0,0 +1,34 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/fused_batchnorm_infer.h" + +int FusedBatchNormInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + for (size_t i = 0; i < inputs_size; i++) { + if (outputs_size <= i) { + break; + } + SetShapeTensor(outputs[i], inputs[i]); + SetDataTypeFormat(outputs[i], inputs[i]); + } + if (outputs_size > 5) { + SetDataTypeFormat(outputs[5], inputs[0]); + outputs[5]->shape_size_ = 1; + outputs[5]->shape_[0] = 1; + } + return 0; +} diff --git a/mindspore/lite/nnacl/infer/fused_batchnorm_infer.h b/mindspore/lite/nnacl/infer/fused_batchnorm_infer.h new file mode 100644 index 0000000000..a90de7f459 --- /dev/null +++ b/mindspore/lite/nnacl/infer/fused_batchnorm_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_FUSED_BATCHNORM_INFER_H +#define MINDSPORE_LITE_NNACL_FUSED_BATCHNORM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int FusedBatchNormInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_FUSED_BATCHNORM_INFER_H diff --git a/mindspore/lite/nnacl/infer/gather_infer.c b/mindspore/lite/nnacl/infer/gather_infer.c new file mode 100644 index 0000000000..4bd9fb5a9f --- /dev/null +++ b/mindspore/lite/nnacl/infer/gather_infer.c @@ -0,0 +1,60 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/gather_infer.h" + +int GatherInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size < 2 || outputs_size != 1) { + return NNACL_ERR; + } + const TensorC *input = inputs[0]; + const TensorC *indices = inputs[1]; + TensorC *output = outputs[0]; + output->data_type_ = input->data_type_; + GatherParameter *param = (GatherParameter *)parameter; + if (param->quant_type_ == QuantType_WeightQuant) { + output->data_type_ = kNumberTypeFloat32; + } + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int axis = *((int *)inputs[2]->data_); + if (axis < 0) { + axis += input->shape_size_; + } + int indices_shape[MAX_SHAPE_SIZE]; + size_t indices_shape_size = 0; + ShapeSet(indices_shape, &indices_shape_size, indices->shape_, indices->shape_size_); + int indices_rank = indices_shape_size; + int in_shape[MAX_SHAPE_SIZE]; + size_t in_shape_size = 0; + ShapeSet(in_shape, &in_shape_size, input->shape_, input->shape_size_); + int in_rank = in_shape_size; + if (in_rank < axis + 1) { + return NNACL_ERR; + } + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, in_shape, in_shape_size); + ShapeErase(out_shape, &out_shape_size, axis); + for (int i = indices_rank - 1; i >= 0; --i) { + ShapeInsert(out_shape, &out_shape_size, axis, indices_shape[i]); + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/gather_infer.h b/mindspore/lite/nnacl/infer/gather_infer.h new file mode 100644 index 0000000000..b83028addb --- /dev/null +++ b/mindspore/lite/nnacl/infer/gather_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_GATHER_INFER_H +#define MINDSPORE_LITE_NNACL_GATHER_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/gather_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int GatherInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_GATHER_INFER_H diff --git a/mindspore/lite/nnacl/infer/gather_nd_infer.c b/mindspore/lite/nnacl/infer/gather_nd_infer.c new file mode 100644 index 0000000000..98ac806526 --- /dev/null +++ b/mindspore/lite/nnacl/infer/gather_nd_infer.c @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/gather_nd_infer.h" + +int GatherNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + const TensorC *indices = inputs[1]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int in_rank = input->shape_size_; + int indices_rank = indices->shape_size_; + if (indices->shape_[indices_rank - 1] > in_rank) { + return NNACL_OK; + } + int i = 0; + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + for (i = 0; i < indices_rank - 1; ++i) { + ShapePush(out_shape, &out_shape_size, indices->shape_[i]); + } + for (i = indices->shape_[indices_rank - 1]; i < in_rank; ++i) { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/gather_nd_infer.h b/mindspore/lite/nnacl/infer/gather_nd_infer.h new file mode 100644 index 0000000000..69c804f1d0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/gather_nd_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_GATHER_ND_INFER_H +#define MINDSPORE_LITE_NNACL_GATHER_ND_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/gatherNd_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int GatherNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_GATHER_ND_INFER_H diff --git a/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.c b/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.c new file mode 100644 index 0000000000..012eb49808 --- /dev/null +++ b/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.c @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/group_conv2d_grad_input_infer.h" + +int GroupConv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + if (inputs_size < 2 || outputs_size != 1) { + return NNACL_ERR; + } + + const TensorC *in0 = inputs[0]; + TensorC *out = outputs[0]; + + SetDataTypeFormat(out, in0); + + size_t shape_size_ = in0->shape_size_; + int shape_[MAX_SHAPE_SIZE]; + for (int i = 0; i < shape_size_; i++) { + shape_[i] = in0->shape_[i]; + } + SetShapeArray(out, shape_, shape_size_); + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.h b/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.h new file mode 100644 index 0000000000..672924a092 --- /dev/null +++ b/mindspore/lite/nnacl/infer/group_conv2d_grad_input_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_GROUP_CONV2D_GRAD_INPUT_INFER_H +#define MINDSPORE_LITE_NNACL_GROUP_CONV2D_GRAD_INPUT_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/conv_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int GroupConv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_GROUP_CONV2D_GRAD_INPUT_INFER_H diff --git a/mindspore/lite/nnacl/infer/gru_infer.c b/mindspore/lite/nnacl/infer/gru_infer.c new file mode 100644 index 0000000000..61bd5c1eeb --- /dev/null +++ b/mindspore/lite/nnacl/infer/gru_infer.c @@ -0,0 +1,82 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/gru_infer.h" + +int GruInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if ((inputs_size != 5 && inputs_size != 6) || outputs_size != 2) { + return NNACL_INPUT_TENSOR_ERROR; + } + const TensorC *input = inputs[0]; + const TensorC *weight_gate = inputs[1]; + const TensorC *weight_recurrence = inputs[2]; + const TensorC *bias = inputs[3]; + TensorC *output = outputs[0]; + for (int i = 0; i < 2; i++) { + SetDataTypeFormat(outputs[i], input); + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const int *in_shape = input->shape_; // seq_len, batch, input_size + const int *w_gate_shape = weight_gate->shape_; // num_direction, hidden_size * 3, input_size + const int *w_recu_shape = weight_recurrence->shape_; // num_direction, hidden_size * 3, hidden_size + const int *bias_shape = bias->shape_; // num_direction, hidden_size * 6 + if (input->shape_size_ != 3 || weight_gate->shape_size_ != 3 || weight_recurrence->shape_size_ != 3) { + return NNACL_ERR; + } + if (w_gate_shape[1] != w_recu_shape[1] || w_recu_shape[1] * 2 != bias_shape[1]) { + return NNACL_ERR; + } + if (inputs_size == 6) { + const int *seq_len_shape = inputs[5]->shape_; + if (seq_len_shape[0] > 1) { + return NNACL_ERR; + } + if (inputs[5]->shape_size_ != 1 && seq_len_shape[0] != in_shape[1]) { + return NNACL_ERR; + } + } + + int hidden_size = w_gate_shape[1] / 3; + // set output + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, in_shape, input->shape_size_); + out_shape[2] = hidden_size; + + GruParameter *param = (GruParameter *)parameter; + if (param->bidirectional_) { + ShapeInsert(out_shape, &out_shape_size, 1, 2); + } else { + ShapeInsert(out_shape, &out_shape_size, 1, 1); + } + SetShapeArray(output, out_shape, out_shape_size); + // set hidden state + int state_shape[MAX_SHAPE_SIZE]; + size_t state_shape_size = 0; + ShapeSet(state_shape, &state_shape_size, in_shape, input->shape_size_); + state_shape[0] = param->bidirectional_ ? 2 : 1; + state_shape[2] = hidden_size; + SetShapeArray(outputs[1], state_shape, state_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/gru_infer.h b/mindspore/lite/nnacl/infer/gru_infer.h new file mode 100644 index 0000000000..448c49ca94 --- /dev/null +++ b/mindspore/lite/nnacl/infer/gru_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_GRU_INFER_H +#define MINDSPORE_LITE_NNACL_GRU_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/gru_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int GruInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_GRU_INFER_H diff --git a/mindspore/lite/nnacl/infer/hashtable_lookup_infer.c b/mindspore/lite/nnacl/infer/hashtable_lookup_infer.c new file mode 100644 index 0000000000..a01525c4e9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/hashtable_lookup_infer.c @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/hashtable_lookup_infer.h" + +int HashtableLoopupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + const TensorC *values = inputs[2]; + TensorC *output = outputs[0]; + TensorC *hits = outputs[1]; + + output->data_type_ = values->data_type_; + output->format_ = input->format_; + hits->shape_size_ = 1; + hits->shape_[0] = GetDimensionSize(input, 0); + hits->data_type_ = kNumberTypeUInt8; + hits->format_ = input->format_; + + if (input->data_ == NULL) { + return NNACL_INFER_INVALID; + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/hashtable_lookup_infer.h b/mindspore/lite/nnacl/infer/hashtable_lookup_infer.h new file mode 100644 index 0000000000..304e97a3e2 --- /dev/null +++ b/mindspore/lite/nnacl/infer/hashtable_lookup_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_HASHTABLE_LOOKUP_INFER_H +#define MINDSPORE_LITE_NNACL_HASHTABLE_LOOKUP_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int HashtableLoopupInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_HASHTABLE_LOOKUP_INFER_H diff --git a/mindspore/lite/nnacl/infer/invert_permutation_infer.c b/mindspore/lite/nnacl/infer/invert_permutation_infer.c new file mode 100644 index 0000000000..d7448eecb1 --- /dev/null +++ b/mindspore/lite/nnacl/infer/invert_permutation_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/invert_permutation_infer.h" + +int InvertPermutationInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->data_type_ != kNumberTypeInt32) { + return NNACL_ERR; + } + if (input->shape_size_ != 1) { + return NNACL_ERR; + } + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/invert_permutation_infer.h b/mindspore/lite/nnacl/infer/invert_permutation_infer.h new file mode 100644 index 0000000000..fb2f71a9bb --- /dev/null +++ b/mindspore/lite/nnacl/infer/invert_permutation_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_INVERT_PERMUTATION_INFER_H +#define MINDSPORE_LITE_NNACL_INVERT_PERMUTATION_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int InvertPermutationInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_INVERT_PERMUTATION_INFER_H diff --git a/mindspore/lite/nnacl/infer/layer_norm_infer.c b/mindspore/lite/nnacl/infer/layer_norm_infer.c new file mode 100644 index 0000000000..bfa9e51d67 --- /dev/null +++ b/mindspore/lite/nnacl/infer/layer_norm_infer.c @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/layer_norm_infer.h" + +int LayerNormInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 1, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + LayerNormParameter *param = (LayerNormParameter *)parameter; + if (!param->op_parameter_.infer_flag_) { + return NNACL_INFER_INVALID; + } + + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/layer_norm_infer.h b/mindspore/lite/nnacl/infer/layer_norm_infer.h new file mode 100644 index 0000000000..bbc87f7db6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/layer_norm_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_LAYER_NORM_INFER_H +#define MINDSPORE_LITE_NNACL_LAYER_NORM_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/layer_norm_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int LayerNormInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_LAYER_NORM_INFER_H diff --git a/mindspore/lite/nnacl/infer/lin_space_infer.c b/mindspore/lite/nnacl/infer/lin_space_infer.c new file mode 100644 index 0000000000..5a3d1c3bf6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/lin_space_infer.c @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/lin_space_infer.h" + +int LinSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + output->data_type_ = input->data_type_; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int *num = (int *)(inputs[2]->data_); + if (num == NULL) { + return NNACL_INFER_INVALID; + } + output->shape_size_ = 1; + output->shape_[0] = num[0]; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/lin_space_infer.h b/mindspore/lite/nnacl/infer/lin_space_infer.h new file mode 100644 index 0000000000..0568040914 --- /dev/null +++ b/mindspore/lite/nnacl/infer/lin_space_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_LIN_SPACE_INFER_H +#define MINDSPORE_LITE_NNACL_LIN_SPACE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int LinSpaceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_LIN_SPACE_INFER_H diff --git a/mindspore/lite/nnacl/infer/lsh_projection_infer.c b/mindspore/lite/nnacl/infer/lsh_projection_infer.c new file mode 100644 index 0000000000..b5e170874a --- /dev/null +++ b/mindspore/lite/nnacl/infer/lsh_projection_infer.c @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/lsh_projection_infer.h" + +int LshProjectionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *in_hash = inputs[0]; + if (in_hash->shape_size_ != 2 || GetDimensionSize(in_hash, 1) > 32) { + return NNACL_ERR; + } + TensorC *out_tensor = outputs[0]; + out_tensor->data_type_ = kNumberTypeInt32; + out_tensor->format_ = Format_NHWC; + + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + LshProjectionParameter *param = (LshProjectionParameter *)parameter; + switch (param->lsh_type_) { + case LshProjectionType_SPARSE: + ShapePush(out_shape, &out_shape_size, GetDimensionSize(in_hash, 0)); + break; + case LshProjectionType_DENSE: + ShapePush(out_shape, &out_shape_size, GetDimensionSize(in_hash, 0) * GetDimensionSize(in_hash, 1)); + break; + default: + return NNACL_ERR; + } + SetShapeArray(out_tensor, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/lsh_projection_infer.h b/mindspore/lite/nnacl/infer/lsh_projection_infer.h new file mode 100644 index 0000000000..ffba1443f8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/lsh_projection_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_LSH_PROJECTION_INFER_H +#define MINDSPORE_LITE_NNACL_LSH_PROJECTION_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/lsh_projection_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int LshProjectionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_LSH_PROJECTION_INFER_H diff --git a/mindspore/lite/nnacl/infer/lstm_infer.c b/mindspore/lite/nnacl/infer/lstm_infer.c new file mode 100644 index 0000000000..2f30704260 --- /dev/null +++ b/mindspore/lite/nnacl/infer/lstm_infer.c @@ -0,0 +1,62 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/lstm_infer.h" + +int LstmInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 6, 3); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + const TensorC *weight_i = inputs[1]; + TensorC *output = outputs[0]; + for (int i = 0; i < 3; i++) { + SetDataTypeFormat(outputs[i], input); + } + + LstmParameter *param = (LstmParameter *)parameter; + if (!param->op_parameter_.infer_flag_) { + return NNACL_INFER_INVALID; + } + + if (input->shape_size_ != 3 || weight_i->shape_size_ != 3) { + return NNACL_ERR; + } + + // int hidden_size = w_shape[1] / 4; + int hidden_size = weight_i->shape_[1] / 4; + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_); + out_shape[2] = hidden_size; + if (param->bidirectional_) { + ShapeInsert(out_shape, &out_shape_size, 1, 2); + } else { + ShapeInsert(out_shape, &out_shape_size, 1, 1); + } + SetShapeArray(output, out_shape, out_shape_size); + int state_shape[MAX_SHAPE_SIZE]; + size_t state_shape_size = 0; + ShapeSet(state_shape, &state_shape_size, input->shape_, input->shape_size_); + state_shape[0] = param->bidirectional_ ? 2 : 1; + state_shape[2] = hidden_size; + SetShapeArray(outputs[1], state_shape, state_shape_size); + SetShapeArray(outputs[2], state_shape, state_shape_size); + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/lstm_infer.h b/mindspore/lite/nnacl/infer/lstm_infer.h new file mode 100644 index 0000000000..ea51f01b28 --- /dev/null +++ b/mindspore/lite/nnacl/infer/lstm_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_LSTM_INFER_H +#define MINDSPORE_LITE_NNACL_LSTM_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/lstm_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int LstmInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_LSTM_INFER_H diff --git a/mindspore/lite/nnacl/infer/matmul_infer.c b/mindspore/lite/nnacl/infer/matmul_infer.c new file mode 100644 index 0000000000..aff275b15f --- /dev/null +++ b/mindspore/lite/nnacl/infer/matmul_infer.c @@ -0,0 +1,83 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/matmul_infer.h" + +int MatmulInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + TensorC *input0 = (TensorC *)inputs[0]; + TensorC *input1 = (TensorC *)inputs[1]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input0); + MatMulParameter *param = (MatMulParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int a_shape[MAX_SHAPE_SIZE]; + size_t a_shape_size = 0; + ShapeSet(a_shape, &a_shape_size, input0->shape_, input0->shape_size_); + int b_shape[MAX_SHAPE_SIZE]; + size_t b_shape_size = 0; + ShapeSet(b_shape, &b_shape_size, input1->shape_, input1->shape_size_); + + if (a_shape_size == 4 && a_shape[2] == 1 && a_shape[3] == 1) { + a_shape_size = 2; + SetShapeArray(input0, a_shape, a_shape_size); + } + + bool del_start = false; + bool del_end = false; + if (a_shape_size == 1) { + ShapeInsert(a_shape, &a_shape_size, 0, 1); + SetShapeArray(input0, a_shape, a_shape_size); + del_start = true; + } + if (b_shape_size == 1) { + ShapePush(b_shape, &b_shape_size, 1); + SetShapeArray(input1, b_shape, b_shape_size); + del_end = true; + } + for (size_t i = 0; i < (a_shape_size - 2) && i < (b_shape_size - 2); ++i) { + if (a_shape[a_shape_size - 3 - i] != b_shape[b_shape_size - 3 - i]) { + return NNACL_INPUT_TENSOR_ERROR; + } + } + + if (param->a_transpose_) { + iswap(&a_shape[a_shape_size - 1], &a_shape[a_shape_size - 2]); + } + if (param->b_transpose_) { + iswap(&b_shape[b_shape_size - 1], &b_shape[b_shape_size - 2]); + } + int c_shape[MAX_SHAPE_SIZE]; + size_t c_shape_size = 0; + ShapeSet(c_shape, &c_shape_size, a_shape, a_shape_size); + c_shape[c_shape_size - 1] = b_shape[b_shape_size - 1]; + if (del_start) { + ShapeErase(c_shape, &c_shape_size, 0); + } + if (del_end) { + c_shape_size--; + } + SetShapeArray(output, c_shape, c_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/matmul_infer.h b/mindspore/lite/nnacl/infer/matmul_infer.h new file mode 100644 index 0000000000..9091f4e0f4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/matmul_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_MATMUL_INFER_H +#define MINDSPORE_LITE_NNACL_MATMUL_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/matmul_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int MatmulInferShape(const TensorC *const *const inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_MATMUL_INFER_H diff --git a/mindspore/lite/nnacl/infer/maximum_grad_infer.c b/mindspore/lite/nnacl/infer/maximum_grad_infer.c new file mode 100644 index 0000000000..f35561795f --- /dev/null +++ b/mindspore/lite/nnacl/infer/maximum_grad_infer.c @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/maximum_grad_infer.h" + +int MaximumGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *x1 = inputs[0]; + const TensorC *x2 = inputs[1]; + const TensorC *dy = inputs[2]; + TensorC *dx1 = outputs[0]; + TensorC *dx2 = outputs[1]; + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + MaximumGradParameter *param = (MaximumGradParameter *)parameter; + param->ndim_ = dy->shape_size_; + param->x1_shape_size_ = param->ndim_; + param->x2_shape_size_ = param->ndim_; + param->dy_shape_size_ = param->ndim_; + int fillDimNum0 = dy->shape_size_ - x1->shape_size_; + int fillDimNum1 = dy->shape_size_ - x2->shape_size_; + int j0 = 0; + int j1 = 0; + for (unsigned int i = 0; i < dy->shape_size_; i++) { + param->x1_shape_[i] = (i < fillDimNum0) ? 1 : x1->shape_[j0++]; + param->x2_shape_[i] = (i < fillDimNum1) ? 1 : x2->shape_[j1++]; + param->dy_shape_[i] = dy->shape_[i]; + } + + SetShapeTensor(dx1, x1); + SetShapeTensor(dx2, x2); + SetDataTypeFormat(dx1, dy); + SetDataTypeFormat(dx2, dy); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/maximum_grad_infer.h b/mindspore/lite/nnacl/infer/maximum_grad_infer.h new file mode 100644 index 0000000000..e76c5e9350 --- /dev/null +++ b/mindspore/lite/nnacl/infer/maximum_grad_infer.h @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_MAXIMUM_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_MAXIMUM_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct MaximumGradParameter { + OpParameter op_parameter_; + int ndim_; + int x1_shape_[MAX_SHAPE_SIZE]; + size_t x1_shape_size_; + int x2_shape_[MAX_SHAPE_SIZE]; + size_t x2_shape_size_; + int dy_shape_[MAX_SHAPE_SIZE]; + size_t dy_shape_size_; +} MaximumGradParameter; + +int MaximumGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_MAXIMUM_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/mean_infer.c b/mindspore/lite/nnacl/infer/mean_infer.c new file mode 100644 index 0000000000..1b22283b80 --- /dev/null +++ b/mindspore/lite/nnacl/infer/mean_infer.c @@ -0,0 +1,67 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/mean_infer.h" + +int MeanInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + ReduceParameter *param = (ReduceParameter *)parameter; + bool keep_dims = (bool)(param->keep_dims_); + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + int *axes = param->axes_; + int num_axes = param->num_axes_; + // reduce on all axes + if (num_axes == 0) { + if (keep_dims) { + for (size_t i = 0; i < input->shape_size_; i++) { + ShapePush(out_shape, &out_shape_size, 1); + } + } + SetShapeArray(output, out_shape, out_shape_size); + output->data_type_ = input->data_type_; + return NNACL_OK; + } + // reduce on selected axes + for (size_t i = 0; i < input->shape_size_; i++) { + bool reduce_axis = false; + for (size_t idx = 0; idx < num_axes; ++idx) { + if (((size_t)(axes[idx])) == i) { + reduce_axis = true; + break; + } + } + if (reduce_axis) { + if (keep_dims) { + ShapePush(out_shape, &out_shape_size, 1); + } + } else { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/mean_infer.h b/mindspore/lite/nnacl/infer/mean_infer.h new file mode 100644 index 0000000000..ab83182eb8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/mean_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_MEAN_INFER_H +#define MINDSPORE_LITE_NNACL_MEAN_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/reduce_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int MeanInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_MEAN_INFER_H diff --git a/mindspore/lite/nnacl/infer/merge_infer.c b/mindspore/lite/nnacl/infer/merge_infer.c new file mode 100644 index 0000000000..5af32daf81 --- /dev/null +++ b/mindspore/lite/nnacl/infer/merge_infer.c @@ -0,0 +1,93 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/merge_infer.h" +#include + +int MergeAbleToInfer(const TensorC *const *inputs, size_t inputs_size) { + for (size_t i = 0; i < inputs_size; i++) { + if (inputs[i]->shape_size_ == 0) { + return HasZeroShape; + } + if (inputs[i]->data_ == NULL) { + return NotAble; + } + } + return Able; +} + +int MergeInfer(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size) { + for (size_t i = 0; i < inputs_size; i++) { + SetDataTypeFormat(outputs[i], inputs[i]); + if (((TensorListC *)inputs[i])->data_type_ == kObjectTypeTensorType) { + TensorListC *input_tensorlist = (TensorListC *)inputs[i]; + TensorListC *output_tensorlist = (TensorListC *)outputs[i]; + ShapeSet(output_tensorlist->element_shape_, &output_tensorlist->element_shape_size_, + input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); + output_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + + output_tensorlist->element_num_ = input_tensorlist->element_num_; + for (size_t j = 0; j < output_tensorlist->element_num_; j++) { + memcpy(output_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + } + } else { + SetShapeTensor(outputs[i], inputs[i]); + } + } + return NNACL_OK; +} + +int MergeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size != 2 * outputs_size) { + return NNACL_ERR; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + for (size_t i = 0; i < outputs_size; ++i) { + outputs[i]->data_type_ = inputs[i]->data_type_; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const TensorC *const *left_part_inputs = inputs; + size_t left_part_inputs_size = inputs_size / 2; + + const TensorC *const *right_part_inputs = inputs + left_part_inputs_size; + size_t right_part_inputs_size = inputs_size / 2; + + if (MergeAbleToInfer(left_part_inputs, left_part_inputs_size) == Able) { + return MergeInfer(left_part_inputs, left_part_inputs_size, outputs, outputs_size); + } + + if (MergeAbleToInfer(right_part_inputs, right_part_inputs_size) == Able) { + return MergeInfer(right_part_inputs, right_part_inputs_size, outputs, outputs_size); + } + + if (MergeAbleToInfer(left_part_inputs, left_part_inputs_size) == HasZeroShape && + MergeAbleToInfer(right_part_inputs, right_part_inputs_size) == HasZeroShape) { + return MergeInfer(left_part_inputs, left_part_inputs_size, outputs, outputs_size); + } + + return NNACL_INFER_INVALID; +} diff --git a/mindspore/lite/nnacl/infer/merge_infer.h b/mindspore/lite/nnacl/infer/merge_infer.h new file mode 100644 index 0000000000..57b40e07df --- /dev/null +++ b/mindspore/lite/nnacl/infer/merge_infer.h @@ -0,0 +1,34 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_MERGE_INFER_H +#define MINDSPORE_LITE_NNACL_MERGE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum InferStatus { Able, NotAble, HasZeroShape }; + +int MergeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_MERGE_INFER_H diff --git a/mindspore/lite/nnacl/infer/mfcc_infer.c b/mindspore/lite/nnacl/infer/mfcc_infer.c new file mode 100644 index 0000000000..f47e849b36 --- /dev/null +++ b/mindspore/lite/nnacl/infer/mfcc_infer.c @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/mfcc_infer.h" + +int MfccInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->shape_size_ != 3) { + return NNACL_ERR; + } + if (GetElementNum(inputs[1]) != 1) { + return NNACL_ERR; + } + output->shape_size_ = 3; + output->shape_[0] = input->shape_[0]; + output->shape_[1] = input->shape_[1]; + MfccParameter *param = (MfccParameter *)parameter; + output->shape_[2] = param->dct_coeff_num_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/mfcc_infer.h b/mindspore/lite/nnacl/infer/mfcc_infer.h new file mode 100644 index 0000000000..358deb46a9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/mfcc_infer.h @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_MFCC_INFER_H +#define MINDSPORE_LITE_NNACL_MFCC_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct MfccParameter { + OpParameter op_parameter_; + int dct_coeff_num_; +} MfccParameter; + +int MfccInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_MFCC_INFER_H diff --git a/mindspore/lite/nnacl/infer/non_max_suppression_infer.c b/mindspore/lite/nnacl/infer/non_max_suppression_infer.c new file mode 100644 index 0000000000..740e958a87 --- /dev/null +++ b/mindspore/lite/nnacl/infer/non_max_suppression_infer.c @@ -0,0 +1,30 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/non_max_suppression_infer.h" + +int NonMaxSuppressionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + output->data_type_ = kNumberTypeInt32; + output->format_ = input->format_; + return NNACL_INFER_INVALID; +} diff --git a/mindspore/lite/nnacl/infer/non_max_suppression_infer.h b/mindspore/lite/nnacl/infer/non_max_suppression_infer.h new file mode 100644 index 0000000000..bb0cc24d1a --- /dev/null +++ b/mindspore/lite/nnacl/infer/non_max_suppression_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_NON_MAX_SUPPRESSION_INFER_H +#define MINDSPORE_LITE_NNACL_NON_MAX_SUPPRESSION_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int NonMaxSuppressionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_NON_MAX_SUPPRESSION_INFER_H diff --git a/mindspore/lite/nnacl/infer/one_hot_infer.c b/mindspore/lite/nnacl/infer/one_hot_infer.c new file mode 100644 index 0000000000..aec2a7d161 --- /dev/null +++ b/mindspore/lite/nnacl/infer/one_hot_infer.c @@ -0,0 +1,54 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/one_hot_infer.h" + +int OneHotInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size != 4 && inputs_size != 3) { + return NNACL_INPUT_TENSOR_ERROR; + } + + const TensorC *input = inputs[0]; + const TensorC *depth_tensor = inputs[1]; + const TensorC *on_value = inputs[2]; + TensorC *output = outputs[0]; + const int *depth = (int *)(depth_tensor->data_); + if (depth == NULL) { + return NNACL_NULL_PTR; + } + SetDataTypeFormat(output, on_value); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + OneHotParameter *param = (OneHotParameter *)parameter; + int axis = param->axis_; + int input_rank = (int)(input->shape_size_); + if (axis < 0) { + axis += input_rank + 1; + } + ShapeSet(output->shape_, &(output->shape_size_), input->shape_, input->shape_size_); + int res_insert = ShapeInsert(output->shape_, &output->shape_size_, axis, *depth); + if (res_insert == NNACL_ERR) { + return NNACL_ERR; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/one_hot_infer.h b/mindspore/lite/nnacl/infer/one_hot_infer.h new file mode 100644 index 0000000000..3e0305e158 --- /dev/null +++ b/mindspore/lite/nnacl/infer/one_hot_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ONE_HOT_INFER_H +#define MINDSPORE_LITE_NNACL_ONE_HOT_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/one_hot_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int OneHotInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ONE_HOT_INFER_H diff --git a/mindspore/lite/nnacl/infer/pad_infer.c b/mindspore/lite/nnacl/infer/pad_infer.c new file mode 100644 index 0000000000..c2cd67d8ce --- /dev/null +++ b/mindspore/lite/nnacl/infer/pad_infer.c @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/pad_infer.h" + +int PadInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + PadParameter *param = (PadParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const TensorC *paddings = inputs[1]; + int size = GetElementNum(paddings); + if (size > MAX_PAD_SIZE) { + return NNACL_PARAM_INVALID; + } + if (paddings->data_ == NULL) { + return NNACL_INFER_INVALID; + } + param->padding_length = size; + for (int i = 0; i < size; ++i) { + param->paddings_[i] = ((int *)paddings->data_)[i]; + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + if (input->shape_size_ > 4) { + return NNACL_INPUT_TENSOR_ERROR; + } + for (size_t i = 0; i < input->shape_size_; i++) { + int shape = input->shape_[i] + param->paddings_[2 * i] + param->paddings_[2 * i + 1]; + ShapePush(output_shape, &output_shape_size, shape); + } + + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/pad_infer.h b/mindspore/lite/nnacl/infer/pad_infer.h new file mode 100644 index 0000000000..b97bea4b52 --- /dev/null +++ b/mindspore/lite/nnacl/infer/pad_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_PAD_INFER_H +#define MINDSPORE_LITE_NNACL_PAD_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/pad_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PadInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_PAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/partial_infer.c b/mindspore/lite/nnacl/infer/partial_infer.c new file mode 100644 index 0000000000..5fa89a3b8e --- /dev/null +++ b/mindspore/lite/nnacl/infer/partial_infer.c @@ -0,0 +1,22 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/partial_infer.h" + +int PartialInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/partial_infer.h b/mindspore/lite/nnacl/infer/partial_infer.h new file mode 100644 index 0000000000..7d9adbe8ca --- /dev/null +++ b/mindspore/lite/nnacl/infer/partial_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_PARTIAL_INFER_H +#define MINDSPORE_LITE_NNACL_PARTIAL_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PartialInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_PARTIAL_INFER_H diff --git a/mindspore/lite/nnacl/infer/pooling_grad_infer.c b/mindspore/lite/nnacl/infer/pooling_grad_infer.c new file mode 100644 index 0000000000..c5b0d3e2b9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/pooling_grad_infer.c @@ -0,0 +1,59 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/pooling_grad_infer.h" +#include + +int PoolingGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + int input_h = input->shape_[1]; + int input_w = input->shape_[2]; + + PoolingParameter *param = (PoolingParameter *)parameter; + int window_h = param->window_h_; + int window_w = param->window_w_; + if (param->global_) { + window_h = input_h; + window_w = input_w; + } + + if (param->pad_mode_ == Pad_same) { + int output_w = ceil((float)(input_w) / (float)(param->stride_w_)); + int output_h = ceil((float)(input_h) / (float)(param->stride_h_)); + int pad_h_all = ((output_h - 1) * param->stride_h_ + (window_h - 1) + 1 - input_h); + int pad_w_all = ((output_w - 1) * param->stride_w_ + (window_w - 1) + 1 - input_w); + if (pad_h_all < 0) { + param->pad_u_ = param->pad_d_ = 0; + } else { + param->pad_u_ = pad_h_all / 2; + param->pad_d_ = pad_h_all - param->pad_u_; + } + if (pad_w_all < 0) { + param->pad_l_ = param->pad_r_ = 0; + } else { + param->pad_l_ = pad_w_all / 2; + param->pad_r_ = pad_w_all - param->pad_l_; + } + } + SetDataTypeFormat(outputs[0], input); + SetShapeTensor(outputs[0], input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/pooling_grad_infer.h b/mindspore/lite/nnacl/infer/pooling_grad_infer.h new file mode 100644 index 0000000000..d8104f35e8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/pooling_grad_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_POOLING_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_POOLING_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/pooling_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PoolingGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_POOLING_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/pooling_infer.c b/mindspore/lite/nnacl/infer/pooling_infer.c new file mode 100644 index 0000000000..d301854f96 --- /dev/null +++ b/mindspore/lite/nnacl/infer/pooling_infer.c @@ -0,0 +1,80 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/pooling_infer.h" +#include + +int PoolingInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + PoolingParameter *param = (PoolingParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int input_h = input->shape_[1]; + int input_w = input->shape_[2]; + + int window_h = param->window_h_; + int window_w = param->window_w_; + if (param->global_) { + window_h = input_h; + window_w = input_w; + } + int output_h = 0; + int output_w = 0; + if (param->pad_mode_ == Pad_same) { // maybe error + output_w = ceil((float)(input_w) / (float)(param->stride_w_)); + output_h = ceil((float)(input_h) / (float)(param->stride_h_)); + int pad_h_all = ((output_h - 1) * param->stride_h_ + (window_h - 1) + 1 - input_h); + int pad_w_all = ((output_w - 1) * param->stride_w_ + (window_w - 1) + 1 - input_w); + if (pad_h_all < 0) { + param->pad_u_ = param->pad_d_ = 0; + } else { + param->pad_u_ = pad_h_all / 2; + param->pad_d_ = pad_h_all - param->pad_u_; + } + if (pad_w_all < 0) { + param->pad_l_ = param->pad_r_ = 0; + } else { + param->pad_l_ = pad_w_all / 2; + param->pad_r_ = pad_w_all - param->pad_l_; + } + } else { + int round_mode = (RoundMode)param->round_mode_; + if (round_mode == RoundMode_Floor) { + output_h = floor((float)(input_h + param->pad_u_ + param->pad_d_ - window_h) / param->stride_h_) + 1; + output_w = floor((float)(input_w + param->pad_l_ + param->pad_r_ - window_w) / param->stride_w_) + 1; + } else if (round_mode == RoundMode_Ceil) { + output_h = ceil((float)(input_h + param->pad_u_ + param->pad_d_ - window_h) / param->stride_h_) + 1; + output_w = ceil((float)(input_w + param->pad_l_ + param->pad_r_ - window_w) / param->stride_w_) + 1; + } else { + return NNACL_ERR; + } + } + int input_shape[MAX_SHAPE_SIZE]; + size_t input_shape_size = 0; + ShapeSet(input_shape, &input_shape_size, input->shape_, input->shape_size_); + input_shape[1] = output_h > 0 ? output_h : 1; + input_shape[2] = output_w > 0 ? output_w : 1; + SetShapeArray(output, input_shape, input_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/pooling_infer.h b/mindspore/lite/nnacl/infer/pooling_infer.h new file mode 100644 index 0000000000..1f30eeaebb --- /dev/null +++ b/mindspore/lite/nnacl/infer/pooling_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_POOLING_INFER_H +#define MINDSPORE_LITE_NNACL_POOLING_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/pooling_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PoolingInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_POOLING_INFER_H diff --git a/mindspore/lite/nnacl/infer/power_infer.c b/mindspore/lite/nnacl/infer/power_infer.c new file mode 100644 index 0000000000..aa45b38167 --- /dev/null +++ b/mindspore/lite/nnacl/infer/power_infer.c @@ -0,0 +1,51 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/power_infer.h" + +int PowerInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *x_tensor = inputs[0]; + TensorC *exp_tensor = NULL; + if (inputs_size == 2) { + exp_tensor = (TensorC *)inputs[1]; + PowerParameter *param = (PowerParameter *)parameter; + float *exp_data = (float *)(exp_tensor->data_); + if (exp_data == NULL) { + return NNACL_INFER_INVALID; + } + param->power_ = *exp_data; + } + TensorC *output_tensor = outputs[0]; + + SetDataTypeFormat(output_tensor, x_tensor); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (exp_tensor != NULL) { + bool exp_x_equal = ShapeEqual(exp_tensor->shape_, exp_tensor->shape_size_, x_tensor->shape_, x_tensor->shape_size_); + if (!exp_x_equal && GetElementNum(exp_tensor) != 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + } + + SetShapeTensor(output_tensor, x_tensor); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/power_infer.h b/mindspore/lite/nnacl/infer/power_infer.h new file mode 100644 index 0000000000..10823b6ee4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/power_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_POWER_INFER_H +#define MINDSPORE_LITE_NNACL_POWER_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/power_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PowerInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_POWER_INFER_H diff --git a/mindspore/lite/nnacl/infer/prior_box_infer.c b/mindspore/lite/nnacl/infer/prior_box_infer.c new file mode 100644 index 0000000000..cba8185204 --- /dev/null +++ b/mindspore/lite/nnacl/infer/prior_box_infer.c @@ -0,0 +1,74 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/prior_box_infer.h" +#include + +int PriorBoxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + output->data_type_ = kNumberTypeFloat32; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + float different_aspect_ratios[MAX_SHAPE_SIZE * 2 + 1]; // NOTE: flip double the number + different_aspect_ratios[0] = 1.0; + size_t different_aspect_ratios_size = 1; + + PriorBoxParameter *param = (PriorBoxParameter *)parameter; + float *aspect_ratios = param->aspect_ratios; + size_t aspect_ratios_size = param->aspect_ratios_size; + for (size_t i = 0; i < aspect_ratios_size; i++) { + float ratio = aspect_ratios[i]; + bool exist = false; + for (size_t j = 0; j < different_aspect_ratios_size; j++) { + if (fabsf(ratio - different_aspect_ratios[j]) < 1e-6) { + exist = true; + break; + } + } + if (!exist) { + different_aspect_ratios[different_aspect_ratios_size] = ratio; + different_aspect_ratios_size++; + if (param->flip) { + different_aspect_ratios[different_aspect_ratios_size] = 1.0f / ratio; + different_aspect_ratios_size++; + } + } + } + + size_t min_sizes_size = param->min_sizes_size; + size_t max_sizes_size = param->max_sizes_size; + int32_t num_priors_box = min_sizes_size * different_aspect_ratios_size + max_sizes_size; + int kPriorBoxPoints = 4; + int kPriorBoxN = 1; + int kPriorBoxW = 1; + int kPriorBoxC = 2; + + int32_t h = GetHeight(input) * GetWidth(input) * num_priors_box * kPriorBoxPoints; + output->shape_size_ = 4; + output->shape_[0] = kPriorBoxN; + output->shape_[1] = h; + output->shape_[2] = kPriorBoxW; + output->shape_[3] = kPriorBoxC; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/prior_box_infer.h b/mindspore/lite/nnacl/infer/prior_box_infer.h new file mode 100644 index 0000000000..1803485263 --- /dev/null +++ b/mindspore/lite/nnacl/infer/prior_box_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_PRIOR_BOX_INFER_H +#define MINDSPORE_LITE_NNACL_PRIOR_BOX_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/prior_box_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int PriorBoxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_PRIOR_BOX_INFER_H diff --git a/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.c b/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.c new file mode 100644 index 0000000000..c904a066ce --- /dev/null +++ b/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/quant_dtype_cast_infer.h" + +int QuantDtypeCastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + QuantDtypeCastParameter *param = (QuantDtypeCastParameter *)parameter; + if (input->data_type_ != param->srcT_) { + return NNACL_ERR; + } + output->data_type_ = param->dstT_; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.h b/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.h new file mode 100644 index 0000000000..b1fb1ca101 --- /dev/null +++ b/mindspore/lite/nnacl/infer/quant_dtype_cast_infer.h @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_QUANT_DTYPE_CAST_INFER_H +#define MINDSPORE_LITE_NNACL_QUANT_DTYPE_CAST_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct QuantDtypeCastParameter { + OpParameter op_parameter_; + int srcT_; + int dstT_; +} QuantDtypeCastParameter; + +int QuantDtypeCastInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_QUANT_DTYPE_CAST_INFER_H diff --git a/mindspore/lite/nnacl/infer/random_standard_normal_infer.c b/mindspore/lite/nnacl/infer/random_standard_normal_infer.c new file mode 100644 index 0000000000..d3b175fc06 --- /dev/null +++ b/mindspore/lite/nnacl/infer/random_standard_normal_infer.c @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/random_standard_normal_infer.h" + +int RandomStandardNormalInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int32_t *input_data = (int32_t *)(inputs[0]->data_); + if (input_data == NULL) { + return NNACL_INFER_INVALID; + } + int input_num = GetElementNum(inputs[0]); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (int i = 0; i < input_num; i++) { + ShapePush(output_shape, &output_shape_size, input_data[i]); + } + SetShapeArray(outputs[0], output_shape, output_shape_size); + outputs[0]->data_type_ = kNumberTypeFloat32; + outputs[0]->format_ = Format_NHWC; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/random_standard_normal_infer.h b/mindspore/lite/nnacl/infer/random_standard_normal_infer.h new file mode 100644 index 0000000000..d91ba863ce --- /dev/null +++ b/mindspore/lite/nnacl/infer/random_standard_normal_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RANDOM_STANDARD_NORMAL_INFER_H +#define MINDSPORE_LITE_NNACL_RANDOM_STANDARD_NORMAL_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int RandomStandardNormalInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RANDOM_STANDARD_NORMAL_INFER_H diff --git a/mindspore/lite/nnacl/infer/range_infer.c b/mindspore/lite/nnacl/infer/range_infer.c new file mode 100644 index 0000000000..51b06ba2a8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/range_infer.c @@ -0,0 +1,74 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/range_infer.h" +#include + +int RangeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + if (input == NULL || output == NULL) { + return NNACL_NULL_PTR; + } + + if (inputs_size == 3) { + output->data_type_ = input->data_type_; + } else { + output->data_type_ = kNumberTypeInt32; + } + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int shape_size = 0; + if (inputs_size == 3) { + if ((inputs[0]->data_ == NULL) || (inputs[1]->data_ == NULL) || (inputs[2]->data_ == NULL)) { + return NNACL_INFER_INVALID; + } + switch (inputs[0]->data_type_) { + case kNumberTypeInt: + case kNumberTypeInt32: { + int start = *(int *)(inputs[0]->data_); + int limit = *(int *)(inputs[1]->data_); + int delta = *(int *)(inputs[2]->data_); + shape_size = imax((int)(ceil((float)(limit - start) / delta)), 0); + } break; + case kNumberTypeFloat32: + case kNumberTypeFloat: { + float start = *(float *)(inputs[0]->data_); + float limit = *(float *)(inputs[1]->data_); + float delta = *(float *)(inputs[2]->data_); + shape_size = imax((int)(ceil((float)(limit - start) / delta)), 0); + } break; + default: { + return NNACL_ERR; + } + } + } else { + RangeParameter *param = (RangeParameter *)parameter; + shape_size = ceil((float)(param->limit_ - param->start_) / param->delta_); + } + + output->shape_size_ = 1; + output->shape_[0] = shape_size; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/range_infer.h b/mindspore/lite/nnacl/infer/range_infer.h new file mode 100644 index 0000000000..c52e8cc406 --- /dev/null +++ b/mindspore/lite/nnacl/infer/range_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RANGE_INFER_H +#define MINDSPORE_LITE_NNACL_RANGE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/range_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int RangeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RANGE_INFER_H diff --git a/mindspore/lite/nnacl/infer/rank_infer.c b/mindspore/lite/nnacl/infer/rank_infer.c new file mode 100644 index 0000000000..56c53920c8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/rank_infer.c @@ -0,0 +1,34 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/rank_infer.h" + +int RankInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + output->shape_size_ = 1; + output->shape_[0] = 1; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/rank_infer.h b/mindspore/lite/nnacl/infer/rank_infer.h new file mode 100644 index 0000000000..ce162ed35b --- /dev/null +++ b/mindspore/lite/nnacl/infer/rank_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RANK_INFER_H +#define MINDSPORE_LITE_NNACL_RANK_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int RankInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RANK_INFER_H diff --git a/mindspore/lite/nnacl/infer/reduce_infer.c b/mindspore/lite/nnacl/infer/reduce_infer.c new file mode 100644 index 0000000000..7d1d744e8b --- /dev/null +++ b/mindspore/lite/nnacl/infer/reduce_infer.c @@ -0,0 +1,101 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/reduce_infer.h" + +int ReduceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullOutputSize(inputs, inputs_size, outputs, outputs_size, parameter, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size < 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + ReduceParameter *param = (ReduceParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + bool keep_dims = param->keep_dims_; + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + // get axes from input tensor + const TensorC *axes_input = inputs[1]; + int *axes = (int *)axes_input->data_; + if (axes == NULL) { + return NNACL_NULL_PTR; + } + size_t num_axes; + if (axes_input->shape_size_ == 1) { + num_axes = axes_input->shape_[0]; + } else if (axes_input->shape_size_ == 0) { + num_axes = 1; + } else { + return NNACL_ERR; + } + + int rank = (int)(input->shape_size_); + int actual_axes[MAX_SHAPE_SIZE]; + size_t actual_axes_size = 0; + ShapeSet(actual_axes, &actual_axes_size, axes, num_axes); + + if (param->reduce_to_end_) { + if (num_axes != 1) { + return NNACL_ERR; + } + + int begin_axis; + begin_axis = axes[0] < 0 ? axes[0] + rank : axes[0]; + for (size_t i = begin_axis + 1; i < rank; ++i) { + ShapePush(actual_axes, &actual_axes_size, i); + } + num_axes = rank - begin_axis; + keep_dims = false; + } + // reduce on all axes + if (num_axes == 0) { + if (keep_dims) { + for (size_t i = 0; i < input->shape_size_; i++) { + ShapePush(out_shape, &out_shape_size, 1); + } + } + SetShapeArray(output, out_shape, out_shape_size); + output->data_type_ = input->data_type_; + return NNACL_OK; + } + // reduce on selected axes + for (size_t i = 0; i < input->shape_size_; i++) { + bool reduce_axis = false; + for (size_t idx = 0; idx < num_axes; ++idx) { + if ((size_t)(actual_axes[idx]) == i || (size_t)(actual_axes[idx] + input->shape_size_) == i) { + reduce_axis = true; + break; + } + } + if (reduce_axis) { + if (keep_dims) { + ShapePush(out_shape, &out_shape_size, 1); + } + } else { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/reduce_infer.h b/mindspore/lite/nnacl/infer/reduce_infer.h new file mode 100644 index 0000000000..8bec1eb2ba --- /dev/null +++ b/mindspore/lite/nnacl/infer/reduce_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_REDUCE_INFER_H +#define MINDSPORE_LITE_NNACL_REDUCE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/reduce_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ReduceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_REDUCE_INFER_H diff --git a/mindspore/lite/nnacl/infer/reshape_infer.c b/mindspore/lite/nnacl/infer/reshape_infer.c new file mode 100644 index 0000000000..81516fac1d --- /dev/null +++ b/mindspore/lite/nnacl/infer/reshape_infer.c @@ -0,0 +1,176 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/reshape_infer.h" + +void CalShape(int *data, const TensorC *const *inputs, int *out_shape, size_t *out_shape_size, int shape_size) { + int input_count = GetElementNum(inputs[0]); + int index = 0; + int size = 1; + for (int i = 0; i < shape_size; i++) { + if ((int)(data[i]) == -1) { + index = i; + } else if ((int)(data[i]) == 0) { + size *= inputs[0]->shape_[i]; + } else { + size *= data[i]; + } + ShapePush(out_shape, out_shape_size, data[i]); + } + if ((int)(data[index]) == -1) { + out_shape[index] = input_count / size; + } +} + +int CalNewShape(const TensorC *in_tensor, int *out_shape, size_t out_shape_size) { + size_t in_shape_size = 1; + for (size_t i = 0; i < in_tensor->shape_size_; i++) { + in_shape_size *= in_tensor->shape_[i]; + } + int64_t infer_index = -1; + size_t out_shape_size_new = 1; + for (size_t i = 0; i < out_shape_size; i++) { + if (out_shape[i] == -1) { + if (infer_index == -1) { + infer_index = i; + } else { + return NNACL_ERR; + } + } else if (out_shape[i] < 0) { + return NNACL_ERR; + } else if (out_shape[i] == 0) { + if (GetElementNum(in_tensor) != 0) { + out_shape[i] = in_tensor->shape_[i]; + out_shape_size_new *= out_shape[i]; + } else { + out_shape_size_new = 0; + break; + } + } else { + out_shape_size_new *= out_shape[i]; + } + } + if (infer_index == -1 && out_shape_size_new != in_shape_size) { + return NNACL_ERR; + } + if (infer_index != -1) { + out_shape[infer_index] = in_shape_size / out_shape_size_new; + } + return NNACL_OK; +} + +int CalShapeByType(const TensorC *const *inputs, size_t shape_size, int *out_shape, size_t *out_shape_size) { + const TensorC *shape_tensor = inputs[1]; + switch (shape_tensor->data_type_) { + case kNumberTypeInt8: { + int8_t *data = (int8_t *)(shape_tensor->data_); + int *data_int = (int *)malloc(sizeof(int) * shape_size); + for (size_t i = 0; i < shape_size; i++) { + data_int[i] = data[i]; + } + CalShape(data_int, inputs, out_shape, out_shape_size, shape_size); + free(data_int); + } break; + case kNumberTypeInt32: { + int32_t *data = (int32_t *)(shape_tensor->data_); + int *data_int = (int *)malloc(sizeof(int) * shape_size); + for (size_t i = 0; i < shape_size; i++) { + data_int[i] = data[i]; + } + CalShape(data_int, inputs, out_shape, out_shape_size, shape_size); + free(data_int); + } break; + case kNumberTypeInt64: { + int64_t *data = (int64_t *)(shape_tensor->data_); + int *data_int = (int *)malloc(sizeof(int) * shape_size); + for (size_t i = 0; i < shape_size; i++) { + data_int[i] = data[i]; + } + CalShape(data_int, inputs, out_shape, out_shape_size, shape_size); + free(data_int); + } break; + case kNumberTypeFloat: { + float *data = (float *)(shape_tensor->data_); + int *data_int = (int *)malloc(sizeof(int) * shape_size); + for (size_t i = 0; i < shape_size; i++) { + data_int[i] = data[i]; + } + CalShape(data_int, inputs, out_shape, out_shape_size, shape_size); + free(data_int); + } break; + case kNumberTypeUInt32: { + uint32_t *data = (uint32_t *)(shape_tensor->data_); + int *data_int = (int *)malloc(sizeof(int) * shape_size); + for (size_t i = 0; i < shape_size; i++) { + data_int[i] = data[i]; + } + CalShape(data_int, inputs, out_shape, out_shape_size, shape_size); + free(data_int); + } break; + default: { + return NNACL_ERR; + } + } + return NNACL_OK; +} + +int ReshapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + ReshapeParameter *param = (ReshapeParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + if (inputs_size == 2) { + const TensorC *shape_tensor = inputs[1]; + if (GetElementNum(input) == 1 && input->shape_size_ == 0) { + if (shape_tensor->data_ == NULL || (shape_tensor->shape_size_ == 1 && shape_tensor->shape_[0] == 0)) { + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; + } + } + + if (shape_tensor->data_ == NULL) { + return NNACL_INFER_INVALID; + } + size_t shape_size = GetElementNum(shape_tensor); + int calRet = CalShapeByType(inputs, shape_size, out_shape, &out_shape_size); + if (calRet != NNACL_OK) { + return calRet; + } + } else if (inputs_size == 1) { + for (size_t i = 0; i < param->shape_dim_; ++i) { + ShapePush(out_shape, &out_shape_size, param->shape_[i]); + } + } else { + return NNACL_ERR; + } + int ret = CalNewShape(inputs[0], out_shape, out_shape_size); + if (ret != NNACL_OK) { + return ret; + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/reshape_infer.h b/mindspore/lite/nnacl/infer/reshape_infer.h new file mode 100644 index 0000000000..adc01b9dac --- /dev/null +++ b/mindspore/lite/nnacl/infer/reshape_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RESHAPE_INFER_H +#define MINDSPORE_LITE_NNACL_RESHAPE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/reshape_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ReshapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RESHAPE_INFER_H diff --git a/mindspore/lite/nnacl/infer/resize_infer.c b/mindspore/lite/nnacl/infer/resize_infer.c new file mode 100644 index 0000000000..71c077af70 --- /dev/null +++ b/mindspore/lite/nnacl/infer/resize_infer.c @@ -0,0 +1,137 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/resize_infer.h" + +int CalculateNewHeightAndWidth(const TensorC *const *inputs, size_t inputs_size, ResizeParameter *param) { + const TensorC *input = inputs[0]; + if (inputs_size == 2) { + const TensorC *shape_tensor = inputs[1]; + if (shape_tensor->data_ == NULL) { + return NNACL_INFER_INVALID; + } + size_t shape_size = GetElementNum(shape_tensor); + switch (shape_size) { + case 4: { + if (shape_tensor->data_type_ == kNumberTypeInt32) { + int32_t *data = (int32_t *)(shape_tensor->data_); + if (data == NULL) { + return NNACL_INFER_INVALID; + } + switch (shape_tensor->format_) { + case Format_NCHW: + param->new_height_ = data[2]; + param->new_width_ = data[3]; + break; + case Format_NHWC: + param->new_height_ = data[1]; + param->new_width_ = data[2]; + break; + default: + return NNACL_INFER_INVALID; + } + } else if (shape_tensor->data_type_ == kNumberTypeFloat32) { + float *data = (float *)(shape_tensor->data_); + if (data == NULL) { + return NNACL_INFER_INVALID; + } + switch (shape_tensor->format_) { + case Format_NCHW: + param->new_height_ = data[2] * GetHeight(input); + param->new_width_ = data[3] * GetWidth(input); + break; + case Format_NHWC: + param->new_height_ = data[1] * GetHeight(input); + param->new_width_ = data[2] * GetWidth(input); + break; + default: + return NNACL_INFER_INVALID; + } + } + break; + } + case 2: { + int32_t *data = (int32_t *)(shape_tensor->data_); + if (data == NULL) { + return NNACL_INFER_INVALID; + } + param->new_height_ = data[0]; + param->new_width_ = data[1]; + break; + } + case 1: { + // caffe zoom_factor + int scale; + if (shape_tensor->data_type_ == kNumberTypeInt32) { + int *data = (int *)(shape_tensor->data_); + if (data == NULL) { + return NNACL_INFER_INVALID; + } + scale = data[0]; + } else { + return NNACL_ERR; + } + param->new_height_ = GetHeight(input) + (GetHeight(input) - 1) * (scale - 1); + param->new_width_ = GetWidth(input) + (GetWidth(input) - 1) * (scale - 1); + break; + } + default: { + return NNACL_ERR; + } + } + } else if (inputs_size == 1) { + } else if (inputs_size == 4) { + if (inputs[3]->data_ == NULL) { + return NNACL_INFER_INVALID; + } + param->new_height_ = ((int *)(inputs[3]->data_))[0]; + param->new_width_ = ((int *)(inputs[3]->data_))[1]; + } else { + return NNACL_ERR; + } + return NNACL_OK; +} + +int ResizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + if (input->shape_size_ != 0 && input->shape_size_ != 4) { + return NNACL_ERR; + } + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + ResizeParameter *param = (ResizeParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapePush(output_shape, &output_shape_size, GetBatch(input)); + int ret = CalculateNewHeightAndWidth(inputs, inputs_size, param); + if (ret == NNACL_OK) { + ShapePush(output_shape, &output_shape_size, param->new_height_); + ShapePush(output_shape, &output_shape_size, param->new_width_); + ShapePush(output_shape, &output_shape_size, GetChannel(input)); + SetShapeArray(output, output_shape, output_shape_size); + } + return ret; +} diff --git a/mindspore/lite/nnacl/infer/resize_infer.h b/mindspore/lite/nnacl/infer/resize_infer.h new file mode 100644 index 0000000000..50ad390ab6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/resize_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RESIZE_INFER_H +#define MINDSPORE_LITE_NNACL_RESIZE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/resize_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ResizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RESIZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/rfft_infer.c b/mindspore/lite/nnacl/infer/rfft_infer.c new file mode 100644 index 0000000000..093ff691de --- /dev/null +++ b/mindspore/lite/nnacl/infer/rfft_infer.c @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/rfft_infer.h" +int RfftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + output->data_type_ = kNumberTypeComplex64; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + ShapeSet(output->shape_, &(output->shape_size_), input->shape_, input->shape_size_); + RfftParameter *param = (RfftParameter *)parameter; + output->shape_[input->shape_size_ - 1] = param->fft_length_ / 2 + 1; + ShapePush(output->shape_, &(output->shape_size_), 2); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/rfft_infer.h b/mindspore/lite/nnacl/infer/rfft_infer.h new file mode 100644 index 0000000000..c430cb342b --- /dev/null +++ b/mindspore/lite/nnacl/infer/rfft_infer.h @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_RFFT_INFER_H +#define MINDSPORE_LITE_NNACL_RFFT_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct RfftParameter { + OpParameter op_parameter_; + int fft_length_; +} RfftParameter; + +int RfftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_RFFT_INFER_H diff --git a/mindspore/lite/nnacl/infer/roi_pooling_infer.c b/mindspore/lite/nnacl/infer/roi_pooling_infer.c new file mode 100644 index 0000000000..488364771d --- /dev/null +++ b/mindspore/lite/nnacl/infer/roi_pooling_infer.c @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/roi_pooling_infer.h" + +int ROIPoolingInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (inputs_size != 2) { + return NNACL_INPUT_TENSOR_ERROR; + } + const TensorC *input = inputs[0]; + const TensorC *roi = inputs[1]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + ROIPoolingParameter *param = (ROIPoolingParameter *)parameter; + output->shape_size_ = 4; + output->shape_[0] = roi->shape_[0]; + output->shape_[1] = param->pooledH_; + output->shape_[2] = param->pooledW_; + output->shape_[3] = GetChannel(input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/roi_pooling_infer.h b/mindspore/lite/nnacl/infer/roi_pooling_infer.h new file mode 100644 index 0000000000..7fb99468c0 --- /dev/null +++ b/mindspore/lite/nnacl/infer/roi_pooling_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_ROI_POOLING_INFER_H +#define MINDSPORE_LITE_NNACL_ROI_POOLING_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/roi_pooling_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ROIPoolingInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_ROI_POOLING_INFER_H diff --git a/mindspore/lite/nnacl/infer/scatter_nd_infer.c b/mindspore/lite/nnacl/infer/scatter_nd_infer.c new file mode 100644 index 0000000000..f40edf5320 --- /dev/null +++ b/mindspore/lite/nnacl/infer/scatter_nd_infer.c @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/scatter_nd_infer.h" + +int ScatterNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *shape = inputs[0]; + if (shape->data_ == NULL) { + return NNACL_INFER_INVALID; + } + const TensorC *update = inputs[2]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, update); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int *shape_data = (int *)(shape->data_); + SetShapeArray(output, shape_data, GetElementNum(shape)); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/scatter_nd_infer.h b/mindspore/lite/nnacl/infer/scatter_nd_infer.h new file mode 100644 index 0000000000..5ee5acdaad --- /dev/null +++ b/mindspore/lite/nnacl/infer/scatter_nd_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SCATTER_ND_INFER_H +#define MINDSPORE_LITE_NNACL_SCATTER_ND_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ScatterNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SCATTER_ND_INFER_H diff --git a/mindspore/lite/nnacl/infer/select_infer.c b/mindspore/lite/nnacl/infer/select_infer.c new file mode 100644 index 0000000000..311af22f7d --- /dev/null +++ b/mindspore/lite/nnacl/infer/select_infer.c @@ -0,0 +1,53 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/select_infer.h" +#include + +int SelectInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = + CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 2 * outputs_size + 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + for (size_t i = 0; i < outputs_size; i++) { + const TensorC *input = inputs[i + 1]; + TensorC *output = outputs[i]; + SetDataTypeFormat(output, input); + if (input->data_type_ == kObjectTypeTensorType) { + TensorListC *input_tensorlist = (TensorListC *)(input); + TensorListC *output_tensorlist = (TensorListC *)(output); + output_tensorlist->element_shape_size_ = input_tensorlist->element_shape_size_; + for (size_t j = 0; j < input_tensorlist->element_shape_size_; j++) { + output_tensorlist->element_shape_[j] = input_tensorlist->element_shape_[j]; + } + output_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + output_tensorlist->element_num_ = input_tensorlist->element_num_; + + for (size_t j = 0; j < output_tensorlist->element_num_; j++) { + memcpy(output_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + } + } else { + SetShapeTensor(output, input); + } + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/select_infer.h b/mindspore/lite/nnacl/infer/select_infer.h new file mode 100644 index 0000000000..1b95ebf830 --- /dev/null +++ b/mindspore/lite/nnacl/infer/select_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SELECT_INFER_H +#define MINDSPORE_LITE_NNACL_SELECT_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SelectInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SELECT_INFER_H diff --git a/mindspore/lite/nnacl/infer/sgd_infer.c b/mindspore/lite/nnacl/infer/sgd_infer.c new file mode 100644 index 0000000000..71da6844d2 --- /dev/null +++ b/mindspore/lite/nnacl/infer/sgd_infer.c @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/sgd_infer.h" + +int SgdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullInputSize(inputs, inputs_size, outputs, outputs_size, parameter, 6); + if (check_ret != NNACL_OK) { + return check_ret; + } + + if (GetElementNum(inputs[0]) != GetElementNum(inputs[1]) || GetElementNum(inputs[0]) != GetElementNum(inputs[3]) || + GetElementNum(inputs[2]) != 1 || GetElementNum(inputs[4]) != 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + if (outputs_size != 0) { + TensorC *out = outputs[0]; + SetDataTypeFormat(out, inputs[0]); + out->shape_size_ = 1; + out->shape_[0] = 1; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/sgd_infer.h b/mindspore/lite/nnacl/infer/sgd_infer.h new file mode 100644 index 0000000000..8d47efdcda --- /dev/null +++ b/mindspore/lite/nnacl/infer/sgd_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SGD_INFER_H +#define MINDSPORE_LITE_NNACL_SGD_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SgdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SGD_INFER_H diff --git a/mindspore/lite/nnacl/infer/shape_infer.c b/mindspore/lite/nnacl/infer/shape_infer.c new file mode 100644 index 0000000000..b2110b4a8c --- /dev/null +++ b/mindspore/lite/nnacl/infer/shape_infer.c @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/shape_infer.h" + +int ShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *in_tensor = inputs[0]; + TensorC *out_tensor = outputs[0]; + + out_tensor->data_type_ = kNumberTypeInt32; + out_tensor->format_ = in_tensor->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + out_tensor->shape_size_ = 1; + out_tensor->shape_[0] = (int)(in_tensor->shape_size_); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/shape_infer.h b/mindspore/lite/nnacl/infer/shape_infer.h new file mode 100644 index 0000000000..30be218bc6 --- /dev/null +++ b/mindspore/lite/nnacl/infer/shape_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SHAPE_INFER_H +#define MINDSPORE_LITE_NNACL_SHAPE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int ShapeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SHAPE_INFER_H diff --git a/mindspore/lite/nnacl/infer/size_infer.c b/mindspore/lite/nnacl/infer/size_infer.c new file mode 100644 index 0000000000..f110ce0014 --- /dev/null +++ b/mindspore/lite/nnacl/infer/size_infer.c @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/size_infer.h" + +int SizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *in_tensor = inputs[0]; + TensorC *out_tensor = outputs[0]; + out_tensor->data_type_ = kNumberTypeInt32; + out_tensor->format_ = in_tensor->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + out_tensor->shape_size_ = 1; + out_tensor->shape_[0] = 1; + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/size_infer.h b/mindspore/lite/nnacl/infer/size_infer.h new file mode 100644 index 0000000000..b299c0e1ad --- /dev/null +++ b/mindspore/lite/nnacl/infer/size_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SIZE_INFER_H +#define MINDSPORE_LITE_NNACL_SIZE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SizeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SIZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/skip_gram_infer.c b/mindspore/lite/nnacl/infer/skip_gram_infer.c new file mode 100644 index 0000000000..5517abfd80 --- /dev/null +++ b/mindspore/lite/nnacl/infer/skip_gram_infer.c @@ -0,0 +1,34 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/skip_gram_infer.h" + +int SkipGramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (input->data_ == NULL) { + return NNACL_INFER_INVALID; + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/skip_gram_infer.h b/mindspore/lite/nnacl/infer/skip_gram_infer.h new file mode 100644 index 0000000000..6b54fc1c9a --- /dev/null +++ b/mindspore/lite/nnacl/infer/skip_gram_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SKIP_GRAM_INFER_H +#define MINDSPORE_LITE_NNACL_SKIP_GRAM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SkipGramInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SKIP_GRAM_INFER_H diff --git a/mindspore/lite/nnacl/infer/slice_infer.c b/mindspore/lite/nnacl/infer/slice_infer.c new file mode 100644 index 0000000000..390902d83f --- /dev/null +++ b/mindspore/lite/nnacl/infer/slice_infer.c @@ -0,0 +1,81 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/slice_infer.h" + +int SliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size < 1 || outputs_size != 1) { + return NNACL_PARAM_INVALID; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + SliceParameter *param = (SliceParameter *)parameter; + param->param_length_ = input->shape_size_; + output->shape_size_ = input->shape_size_; + + /* init begin parameter */ + size_t slice_begin_size = GetElementNum(inputs[1]); + int *begin_ptr = (int *)(inputs[1]->data_); + if (slice_begin_size != param->param_length_ || begin_ptr == NULL) { + return NNACL_INFER_INVALID; + } + for (int i = 0; i < slice_begin_size; i++) { + param->begin_[i] = begin_ptr[i]; + } + + /* init size parameter */ + size_t slice_size_size = GetElementNum(inputs[2]); + int *size_ptr = (int *)(inputs[2]->data_); + if (slice_size_size != param->param_length_ || size_ptr == NULL) { + return NNACL_INFER_INVALID; + } + for (int i = 0; i < slice_size_size; i++) { + param->size_[i] = size_ptr[i]; + } + + /* infer output shape information */ + int begin[MAX_SHAPE_SIZE]; + int size[MAX_SHAPE_SIZE]; + for (size_t i = 0; i < param->param_length_; ++i) { + begin[param->axis_[i]] = param->begin_[i]; + size[param->axis_[i]] = param->size_[i]; + } + + for (size_t i = 0; i < param->param_length_; ++i) { + if (size[i] < 0 && size[i] != -1) { + return NNACL_PARAM_INVALID; + } + if (begin[i] < 0) { + return NNACL_PARAM_INVALID; + } + if (input->shape_[i] <= begin[i]) { + return NNACL_PARAM_INVALID; + } + if (size[i] > (input->shape_[i] - begin[i])) { + return NNACL_PARAM_INVALID; + } + + output->shape_[i] = size[i] < 0 ? input->shape_[i] - begin[i] : size[i]; + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/slice_infer.h b/mindspore/lite/nnacl/infer/slice_infer.h new file mode 100644 index 0000000000..0aa3f79ce3 --- /dev/null +++ b/mindspore/lite/nnacl/infer/slice_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SLICE_INFER_H +#define MINDSPORE_LITE_NNACL_SLICE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/slice_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SLICE_INFER_H diff --git a/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.c b/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.c new file mode 100644 index 0000000000..5b78c4d102 --- /dev/null +++ b/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.c @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/softmax_cross_entropy_infer.h" + +int SoftmaxCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (1 > outputs_size) { + return NNACL_INPUT_TENSOR_ERROR; + } + const TensorC *in0 = inputs[0]; + TensorC *out = outputs[0]; + + out->shape_size_ = 2; + out->shape_[0] = in0->shape_[0]; + out->shape_[1] = 1; + SetDataTypeFormat(out, in0); + + if (1 < outputs_size) { + TensorC *grads = outputs[1]; + SetShapeTensor(grads, in0); + SetDataTypeFormat(grads, in0); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.h b/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.h new file mode 100644 index 0000000000..b66aa8d7ef --- /dev/null +++ b/mindspore/lite/nnacl/infer/softmax_cross_entropy_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SOFTMAX_CROSS_ENTROPY_INFER_H +#define MINDSPORE_LITE_NNACL_SOFTMAX_CROSS_ENTROPY_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SoftmaxCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SOFTMAX_ENTROPY_INFER_H diff --git a/mindspore/lite/nnacl/infer/softmax_infer.c b/mindspore/lite/nnacl/infer/softmax_infer.c new file mode 100644 index 0000000000..39fd8ff999 --- /dev/null +++ b/mindspore/lite/nnacl/infer/softmax_infer.c @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/softmax_infer.h" + +int SoftMaxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + output->data_type_ = input->data_type_; + output->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->shape_size_ > 5) { + return NNACL_ERR; + } + SetShapeTensor(output, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/softmax_infer.h b/mindspore/lite/nnacl/infer/softmax_infer.h new file mode 100644 index 0000000000..ba22743fea --- /dev/null +++ b/mindspore/lite/nnacl/infer/softmax_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SOFTMAX_INFER_H +#define MINDSPORE_LITE_NNACL_SOFTMAX_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SoftMaxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SOFTMAX_INFER_H diff --git a/mindspore/lite/nnacl/infer/space_to_batch_infer.c b/mindspore/lite/nnacl/infer/space_to_batch_infer.c new file mode 100644 index 0000000000..0eb146052b --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_batch_infer.c @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/space_to_batch_infer.h" + +int SpaceToBatchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + if (input->format_ != Format_NHWC) { + return NNACL_ERR; + } + SetDataTypeFormat(outputs[0], input); + SpaceToBatchParameter *param = (SpaceToBatchParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->shape_size_ != 4) { + return NNACL_ERR; + } + + int *block_shape = param->block_sizes_; + size_t block_shape_size = param->m_; + int *paddings = param->paddings_; + int padding_left = 0; + int padding_right = 0; + int block_w = 1; + if (block_shape_size == 2) { + padding_left = paddings[2]; + padding_right = paddings[3]; + block_w = block_shape[1]; + } + + outputs[0]->shape_[kNHWC_N] = input->shape_[kNHWC_N] * (block_shape[0] * block_w); + outputs[0]->shape_[kNHWC_H] = (input->shape_[kNHWC_H] + paddings[0] + paddings[1]) / block_shape[0]; + outputs[0]->shape_[kNHWC_W] = (input->shape_[kNHWC_W] + padding_left + padding_right) / block_w; + outputs[0]->shape_[kNHWC_C] = input->shape_[kNHWC_C]; + outputs[0]->shape_size_ = input->shape_size_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/space_to_batch_infer.h b/mindspore/lite/nnacl/infer/space_to_batch_infer.h new file mode 100644 index 0000000000..e6e8743222 --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_batch_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_INFER_H +#define MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/space_to_batch_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SpaceToBatchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_INFER_H diff --git a/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.c b/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.c new file mode 100644 index 0000000000..0cc350a96c --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.c @@ -0,0 +1,132 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/space_to_batch_nd_infer.h" +#include + +int SpaceSetOutputShapeFromParam(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + const TensorC *input = inputs[0]; + if (input->shape_size_ != 4) { + return NNACL_ERR; + } + SpaceToBatchParameter *param = (SpaceToBatchParameter *)parameter; + int *block_shape = param->block_sizes_; + size_t block_shape_size = param->m_; + int *padding = param->paddings_; + int padding_left = 0; + int padding_right = 0; + int block_w = 1; + if (block_shape_size == 2) { + padding_left = padding[2]; + padding_right = padding[3]; + block_w = block_shape[1]; + } + if (block_shape[0] * block_w > INT_MAX / input->shape_[kNHWC_N]) { + return NNACL_ERR; + } + outputs[0]->shape_[kNHWC_N] = input->shape_[kNHWC_N] * block_shape[0] * block_w; + if (padding[0] + padding[1] > INT_MAX - input->shape_[kNHWC_H]) { + return NNACL_ERR; + } + outputs[0]->shape_[kNHWC_H] = (input->shape_[kNHWC_H] + padding[0] + padding[1]) / block_shape[0]; + if (padding_left + padding_right > INT_MAX - input->shape_[kNHWC_W]) { + return NNACL_ERR; + } + outputs[0]->shape_[kNHWC_W] = (input->shape_[kNHWC_W] + padding_left + padding_right) / block_w; + if (input->shape_size_ > 3) { + outputs[0]->shape_[kNHWC_C] = input->shape_[kNHWC_C]; + } + outputs[0]->shape_size_ = input->shape_size_; + return NNACL_OK; +} + +int SpaceSetOutputShapeFromInput(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + const TensorC *input = inputs[0]; + if (input->shape_size_ != 4) { + return NNACL_ERR; + } + if (GetElementNum(inputs[2]) != 4) { + return NNACL_ERR; + } + int *block_shape = (int *)(inputs[1]->data_); + int *padding = (int *)(inputs[2]->data_); + int padding_left = 0; + int padding_right = 0; + int block_w = 1; + if (GetElementNum(inputs[1]) == 2) { + padding_left = padding[2]; + padding_right = padding[3]; + block_w = block_shape[1]; + } + int32_t output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input->shape_size_; + if (block_shape[0] * block_w > INT_MAX / input->shape_[kNHWC_N]) { + return NNACL_ERR; + } + output_shape[kNHWC_N] = input->shape_[kNHWC_N] * block_shape[0] * block_w; + if (padding[0] + padding[1] > INT_MAX - input->shape_[kNHWC_H]) { + return NNACL_ERR; + } + output_shape[kNHWC_H] = (input->shape_[kNHWC_H] + padding[0] + padding[1]) / block_shape[0]; + if (padding_left + padding_right > INT_MAX - input->shape_[kNHWC_W]) { + return NNACL_ERR; + } + output_shape[kNHWC_W] = (input->shape_[kNHWC_W] + padding_left + padding_right) / block_w; + if (input->shape_size_ > 3) { + output_shape[kNHWC_C] = input->shape_[kNHWC_C]; + } + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} + +int SpaceToBatchNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (outputs_size != 1 || (inputs_size != 1 && inputs_size != 3)) { + return 1; + } + + const TensorC *input = inputs[0]; + if (input->format_ != Format_NHWC) { + return NNACL_ERR; + } + outputs[0]->data_type_ = input->data_type_; + outputs[0]->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + if (inputs_size == 1) { + int ret = SpaceSetOutputShapeFromParam(inputs, inputs_size, outputs, outputs_size, parameter); + if (ret != NNACL_OK) { + return ret; + } + } + if (inputs_size == 3) { + if (inputs[0]->data_ == NULL) { + return NNACL_INFER_INVALID; + } + if (inputs[1]->data_ == NULL || inputs[2]->data_ == NULL) { + return NNACL_ERR; + } + int ret = SpaceSetOutputShapeFromInput(inputs, inputs_size, outputs, outputs_size, parameter); + if (ret != NNACL_OK) { + return ret; + } + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.h b/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.h new file mode 100644 index 0000000000..c8bc25e2c4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_batch_nd_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_ND_INFER_H +#define MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_ND_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/space_to_batch_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SpaceToBatchNdInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPACE_TO_BATCH_ND_INFER_H diff --git a/mindspore/lite/nnacl/infer/space_to_depth_infer.c b/mindspore/lite/nnacl/infer/space_to_depth_infer.c new file mode 100644 index 0000000000..7baaaaade9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_depth_infer.c @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/space_to_depth_infer.h" +#include + +int SpaceToDepthInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (outputs_size != 1 || inputs_size != 1) { + return NNACL_ERR; + } + + const TensorC *input = inputs[0]; + if (input->format_ != Format_NHWC) { + return NNACL_ERR; + } + SetDataTypeFormat(outputs[0], input); + SpaceToDepthParameter *param = (SpaceToDepthParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + if (input->shape_size_ != 4) { + return NNACL_ERR; + } + + int32_t block_size = param->block_size_; + if (block_size == 0) { + return NNACL_ERR; + } + if (input->shape_[kNHWC_H] % block_size != 0 || input->shape_[kNHWC_H] == 0 || + input->shape_[kNHWC_W] % block_size != 0 || input->shape_[kNHWC_W] == 0) { + return NNACL_ERR; + } + outputs[0]->shape_[kNHWC_N] = input->shape_[kNHWC_N]; + outputs[0]->shape_[kNHWC_H] = input->shape_[kNHWC_H] / block_size; + outputs[0]->shape_[kNHWC_W] = input->shape_[kNHWC_W] / block_size; + if (block_size * block_size > INT_MAX / input->shape_[kNHWC_C]) { + return NNACL_ERR; + } + outputs[0]->shape_[kNHWC_C] = input->shape_[kNHWC_C] * (block_size * block_size); + outputs[0]->shape_size_ = input->shape_size_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/space_to_depth_infer.h b/mindspore/lite/nnacl/infer/space_to_depth_infer.h new file mode 100644 index 0000000000..65dfefdd2e --- /dev/null +++ b/mindspore/lite/nnacl/infer/space_to_depth_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPACE_TO_DEPTH_INFER_H +#define MINDSPORE_LITE_NNACL_SPACE_TO_DEPTH_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/space_to_depth_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SpaceToDepthInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPACE_TO_DEPTH_INFER_H diff --git a/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.c b/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.c new file mode 100644 index 0000000000..5d8b7991b7 --- /dev/null +++ b/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/sparse_softmax_cross_entropy_infer.h" + +int SparseSoftmaxCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *in0 = inputs[0]; + TensorC *out = outputs[0]; + + SparseSoftmaxCrossEntropyParameter *param = (SparseSoftmaxCrossEntropyParameter *)parameter; + if (param->is_grad_ != 0) { + SetShapeTensor(out, in0); + SetDataTypeFormat(out, in0); + } else { + out->shape_size_ = 1; + out->shape_[0] = 1; + SetDataTypeFormat(out, in0); + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.h b/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.h new file mode 100644 index 0000000000..56322e3533 --- /dev/null +++ b/mindspore/lite/nnacl/infer/sparse_softmax_cross_entropy_infer.h @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPARSE_SOFTMAX_CROSS_ENTROPY_INFER_H +#define MINDSPORE_LITE_NNACL_SPARSE_SOFTMAX_CROSS_ENTROPY_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SparseSoftmaxCrossEntropyParameter { + OpParameter op_parameter_; + bool is_grad_; +} SparseSoftmaxCrossEntropyParameter; + +int SparseSoftmaxCrossEntropyInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPARSE_SOFTMAX_CROSS_ENTROPY_INFER_H diff --git a/mindspore/lite/nnacl/infer/sparse_to_dense_infer.c b/mindspore/lite/nnacl/infer/sparse_to_dense_infer.c new file mode 100644 index 0000000000..486df3ef19 --- /dev/null +++ b/mindspore/lite/nnacl/infer/sparse_to_dense_infer.c @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/sparse_to_dense_infer.h" + +int SparseToDenseInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + TensorC *output = outputs[0]; + const TensorC *input1 = inputs[1]; + const TensorC *input2 = inputs[2]; + SetDataTypeFormat(output, input2); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int *input1_data = (int *)(input1->data_); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (int i = 0; i < GetElementNum(input1); i++) { + ShapePush(output_shape, &output_shape_size, input1_data[i]); + } + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/sparse_to_dense_infer.h b/mindspore/lite/nnacl/infer/sparse_to_dense_infer.h new file mode 100644 index 0000000000..1e274247e2 --- /dev/null +++ b/mindspore/lite/nnacl/infer/sparse_to_dense_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPACE_TO_DENSE_INFER_H +#define MINDSPORE_LITE_NNACL_SPACE_TO_DENSE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SparseToDenseInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPACE_TO_DENSE_INFER_H diff --git a/mindspore/lite/nnacl/infer/splice_infer.c b/mindspore/lite/nnacl/infer/splice_infer.c new file mode 100644 index 0000000000..74468f743b --- /dev/null +++ b/mindspore/lite/nnacl/infer/splice_infer.c @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/splice_infer.h" + +int SpliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret == NNACL_NULL_PTR) { + return NNACL_NULL_PTR; + } + if (inputs_size != 1) { + return NNACL_ERR; + } + if (outputs_size != 1) { + return NNACL_ERR; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + size_t max_dims = input->shape_size_; + size_t max_dims_idx = 0; + + // determine max_dims + for (size_t i = 1; i < inputs_size; ++i) { + if (inputs[i]->shape_size_ > max_dims) { + max_dims = inputs[i]->shape_size_; + max_dims_idx = i; + } + } + SpliceParameter *param = (SpliceParameter *)parameter; + if (param == NULL) { + return NNACL_NULL_PTR; + } + int context_size = param->context_dim_; + int out_dim = param->output_dim_; + ShapeSet(output->shape_, &output->shape_size_, inputs[max_dims_idx]->shape_, inputs[max_dims_idx]->shape_size_); + output->shape_[1] = input->shape_[1] - context_size + 1; + output->shape_[2] = out_dim; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/splice_infer.h b/mindspore/lite/nnacl/infer/splice_infer.h new file mode 100644 index 0000000000..bb7584cc19 --- /dev/null +++ b/mindspore/lite/nnacl/infer/splice_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_NNACL_INFER_SPLICE_INFER_H_ +#define MINDSPORE_LITE_NNACL_INFER_SPLICE_INFER_H_ +#include "nnacl/infer/common_infer.h" +#include "nnacl/splice_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SpliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_INFER_SPLICE_INFER_H_ diff --git a/mindspore/lite/nnacl/infer/split_infer.c b/mindspore/lite/nnacl/infer/split_infer.c new file mode 100644 index 0000000000..8a4f139325 --- /dev/null +++ b/mindspore/lite/nnacl/infer/split_infer.c @@ -0,0 +1,77 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/split_infer.h" + +int SplitInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + if (inputs_size < 1) { + return NNACL_ERR; + } + if (outputs_size == 0) { + return NNACL_ERR; + } + for (size_t i = 0; i < outputs_size; i++) { + SetDataTypeFormat(outputs[i], input); + } + + SplitParameter *param = (SplitParameter *)parameter; + + size_t num_split_ = param->num_split_ == 0 ? (int)(outputs_size) : param->num_split_; + param->num_split_ = num_split_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + size_t split_dim = param->split_dim_ < 0 ? input->shape_size_ + param->split_dim_ : param->split_dim_; + if (split_dim > input->shape_size_) { + return NNACL_ERR; + } + if ((int)(outputs_size) != num_split_) { + return NNACL_ERR; + } + if (param->split_count_ == 0) { + if (input->shape_[split_dim] % num_split_ != 0) { + return NNACL_ERR; + } + for (int i = 0; i < num_split_; ++i) { + param->split_sizes_[i] = input->shape_[split_dim] / num_split_; + } + } + for (int i = 0; i < num_split_; ++i) { + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_); + int split_dim_i = input->shape_[split_dim]; + if (i == num_split_ - 1 && param->split_sizes_[i] == -1) { + for (size_t j = 0; j < param->num_split_ - 1; ++j) { + split_dim_i -= param->split_sizes_[j]; + } + param->split_sizes_[i] = split_dim_i; + } else { + split_dim_i = param->split_sizes_[i]; + } + output_shape[split_dim] = split_dim_i; + SetShapeArray(outputs[i], output_shape, output_shape_size); + SetDataTypeFormat(outputs[i], input); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/split_infer.h b/mindspore/lite/nnacl/infer/split_infer.h new file mode 100644 index 0000000000..7745fd26cb --- /dev/null +++ b/mindspore/lite/nnacl/infer/split_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SPLIT_INFER_H +#define MINDSPORE_LITE_NNACL_SPLIT_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/split_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SplitInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SPLIT_INFER_H diff --git a/mindspore/lite/nnacl/infer/squeeze_infer.c b/mindspore/lite/nnacl/infer/squeeze_infer.c new file mode 100644 index 0000000000..2ed2f52eaa --- /dev/null +++ b/mindspore/lite/nnacl/infer/squeeze_infer.c @@ -0,0 +1,59 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/squeeze_infer.h" + +int SqueezeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + SqueezeParameter *param = (SqueezeParameter *)parameter; + SetDataTypeFormat(outputs[0], input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + + for (size_t i = 0; i < param->axis_size_; i++) { + param->axis_[i] = param->axis_[i] >= 0 ? param->axis_[i] : param->axis_[i] + input->shape_size_; + } + + if (param->axis_size_ == 0) { + for (size_t i = 0; i < input->shape_size_; i++) { + if (input->shape_[i] != 1) { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + } + } else { + size_t axisIdx = 0; + for (size_t i = 0; i < input->shape_size_; i++) { + if (axisIdx < param->axis_size_ && param->axis_[axisIdx] == (int)(i)) { + if (input->shape_[i] != 1) return NNACL_PARAM_INVALID; + axisIdx++; + continue; + } else { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + } + } + SetShapeArray(outputs[0], out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/squeeze_infer.h b/mindspore/lite/nnacl/infer/squeeze_infer.h new file mode 100644 index 0000000000..9b7409ab28 --- /dev/null +++ b/mindspore/lite/nnacl/infer/squeeze_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SQUEEZE_INFER_H +#define MINDSPORE_LITE_NNACL_SQUEEZE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/squeeze_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SqueezeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SQUEEZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/stack_infer.c b/mindspore/lite/nnacl/infer/stack_infer.c new file mode 100644 index 0000000000..c135a2f8e9 --- /dev/null +++ b/mindspore/lite/nnacl/infer/stack_infer.c @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/stack_infer.h" + +int StackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (outputs_size != 1) { + return NNACL_PARAM_INVALID; + } + if (inputs_size < 1) { + return NNACL_PARAM_INVALID; + } + const TensorC *input = inputs[0]; + SetDataTypeFormat(outputs[0], input); + StackParameter *param = (StackParameter *)parameter; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int32_t output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_); + int axis = param->axis_ < 0 ? param->axis_ + input->shape_size_ + 1 : param->axis_; + if (axis < 0 || axis > input->shape_size_) { + return NNACL_PARAM_INVALID; + } + + for (size_t i = 1; i < inputs_size; ++i) { + if (inputs[i]->shape_size_ != input->shape_size_) { + return NNACL_PARAM_INVALID; + } + for (size_t j = 0; j < input->shape_size_; ++j) { + if (inputs[i]->shape_[j] != input->shape_[j]) { + return NNACL_PARAM_INVALID; + } + } + if (inputs[i]->data_type_ != input->data_type_) { + return NNACL_PARAM_INVALID; + } + } + ShapeInsert(output_shape, &output_shape_size, axis, inputs_size); + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/stack_infer.h b/mindspore/lite/nnacl/infer/stack_infer.h new file mode 100644 index 0000000000..40e47158e5 --- /dev/null +++ b/mindspore/lite/nnacl/infer/stack_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_STACK_INFER_H +#define MINDSPORE_LITE_NNACL_STACK_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/stack_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int StackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_STACK_INFER_H diff --git a/mindspore/lite/nnacl/infer/strided_slice_grad_infer.c b/mindspore/lite/nnacl/infer/strided_slice_grad_infer.c new file mode 100644 index 0000000000..6de2337a29 --- /dev/null +++ b/mindspore/lite/nnacl/infer/strided_slice_grad_infer.c @@ -0,0 +1,140 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/strided_slice_grad_infer.h" + +bool StridedSliceCheckInputs(const TensorC *const *inputs, size_t inputs_size) { + for (size_t i = 1; i < inputs_size; ++i) { + if (inputs[i]->data_ == NULL) { + return false; + } + } + + return true; // note: the original code is ndim_ <= in_shape_size +} + +int StridedSliceGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 5, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + SetDataTypeFormat(outputs[0], input); + bool inferflag = parameter->infer_flag_; + + int in_shape_[MAX_SHAPE_SIZE]; + size_t in_shape_size = 0; + if (inferflag) { + ShapeSet(in_shape_, &in_shape_size, input->shape_, input->shape_size_); + } + int begins_[MAX_SHAPE_SIZE]; + size_t begins_size = 0; + int ends_[MAX_SHAPE_SIZE]; + size_t ends_size = 0; + int strides_[MAX_SHAPE_SIZE]; + size_t strides_size = 0; + + if (!StridedSliceCheckInputs(inputs, inputs_size)) { + return NNACL_INFER_INVALID; + } + + // input order: dy, shapex, begins, ends, strides. + const TensorC *begin_tensor = inputs[2]; + int *begin_data = (int *)(begin_tensor->data_); + int *end_data = (int *)(inputs[3]->data_); + int *stride_data = (int *)(inputs[4]->data_); + if (begin_data == NULL || end_data == NULL || stride_data == NULL) { + return NNACL_ERR; + } + size_t ndim_ = GetElementNum(begin_tensor); + for (int i = 0; i < ndim_; ++i) { + ShapePush(begins_, &begins_size, begin_data[i]); + ShapePush(ends_, &ends_size, end_data[i]); + ShapePush(strides_, &strides_size, stride_data[i]); + } + + // set all mask to original input shape + uint32_t begins_mask_[MAX_SHAPE_SIZE]; + uint32_t ends_mask_[MAX_SHAPE_SIZE]; + uint32_t ellipsis_mask_[MAX_SHAPE_SIZE]; + uint32_t new_axis_mask_[MAX_SHAPE_SIZE]; + + StridedSliceParameter *param = (StridedSliceParameter *)parameter; + for (size_t i = 0; i < ndim_; i++) { + begins_mask_[i] = (bool)(param->begins_mask_) & (1 << i); + ends_mask_[i] = (bool)(param->ends_mask_) & (1 << i); + ellipsis_mask_[i] = (bool)(param->ellipsisMask_) & (1 << i); + new_axis_mask_[i] = (bool)(param->newAxisMask_) & (1 << i); + } + + // ApplyNewAxisMask(); + for (size_t i = 0; i < ndim_; i++) { + if (new_axis_mask_[i]) { + ndim_ += 1; + ShapeInsert(in_shape_, &in_shape_size, i, 1); + begins_[i] = 0; + ends_[i] = 1; + strides_[i] = 1; + + ShapePush(begins_, &begins_size, 0); + ShapePush(ends_, &ends_size, in_shape_[ndim_ - 1]); + ShapePush(strides_, &strides_size, 1); + + begins_mask_[i] = false; + ends_mask_[i] = false; + ellipsis_mask_[i] = false; + } + } + // ApplyBeginMask(); + for (size_t i = 0; i < ndim_; i++) { + if (begins_mask_[i]) { + begins_[i] = 0; + } + } + // ApplyEndMask(); + for (size_t i = 0; i < ndim_; i++) { + if (ends_mask_[i]) { + ends_[i] = in_shape_[i]; + } + } + // ApplyEllipsisMask(); + for (size_t i = 0; i < ndim_; i++) { + if (ellipsis_mask_[i]) { + begins_[i] = 0; + ends_[i] = in_shape_[i]; + break; + } + } + + if (!inferflag) { + return NNACL_OK; + } + + size_t output_size = inputs[1]->shape_[0]; + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + if (inputs[1]->data_ == NULL) { + return NNACL_ERR; + } + + for (int i = 0; i < output_size; i++) { + ShapePush(output_shape, &output_shape_size, ((int *)(inputs[1]->data_))[i]); + } + SetShapeArray(outputs[0], output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/strided_slice_grad_infer.h b/mindspore/lite/nnacl/infer/strided_slice_grad_infer.h new file mode 100644 index 0000000000..9e4ed8ea56 --- /dev/null +++ b/mindspore/lite/nnacl/infer/strided_slice_grad_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_STRIDED_SLICE_GRAD_INFER_H +#define MINDSPORE_LITE_NNACL_STRIDED_SLICE_GRAD_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/strided_slice_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int StridedSliceGradInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_STRIDED_SLICE_GRAD_INFER_H diff --git a/mindspore/lite/nnacl/infer/strided_slice_infer.c b/mindspore/lite/nnacl/infer/strided_slice_infer.c new file mode 100644 index 0000000000..ad37aa247b --- /dev/null +++ b/mindspore/lite/nnacl/infer/strided_slice_infer.c @@ -0,0 +1,328 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/strided_slice_infer.h" + +const size_t kStridedSliceOutputNum = 1; +const size_t kStridedSliceInputNum = 1; +const size_t kStridedSliceMultiInputNumMin = 3; +const size_t kStridedSliceMultiInputNumMax = 5; + +bool CheckInputs(const TensorC *const *inputs, size_t inputs_size) { + for (size_t i = 1; i < inputs_size; ++i) { + if (inputs[i]->data_ == NULL) { + return false; + } + } + return true; +} + +int HandleAxesCheckNull(const TensorC *input_tensor, const TensorC *begin_tensor, int *begin_data, + const TensorC *end_tensor, int *end_data) { + if (input_tensor == NULL || begin_tensor == NULL || end_tensor == NULL || begin_data == NULL || end_data == NULL) { + return NNACL_NULL_PTR; + } + return NNACL_OK; +} + +int HandleAxesInputExist(const TensorC *const *inputs, int *ndim_, int *in_shape_, int *begins_, int *strides_, + int *ends_) { + const TensorC *input_tensor = inputs[0]; + const TensorC *begin_tensor = inputs[1]; + int *begin_data = (int *)(begin_tensor->data_); + const TensorC *end_tensor = inputs[2]; + int *end_data = (int *)(end_tensor->data_); + + int handle_check_ret = HandleAxesCheckNull(input_tensor, begin_tensor, begin_data, end_tensor, end_data); + if (handle_check_ret != NNACL_OK) { + return handle_check_ret; + } + + // when input contains axes, begins, ends, strides will be expand to the same length as input rank + *ndim_ = (int)(input_tensor->shape_size_); + int begin_ndim = GetElementNum(begin_tensor); + + int *axes_data = NULL; + const TensorC *axes_tensor = inputs[3]; + if (GetElementNum(axes_tensor) != 0) { + if (GetElementNum(axes_tensor) != begin_ndim) { + return NNACL_ERR; + } + axes_data = (int *)(axes_tensor->data_); + if (axes_data == NULL) { + return NNACL_NULL_PTR; + } + } + + int *stride_data = NULL; + const TensorC *stride_tensor = inputs[4]; + if (GetElementNum(stride_tensor) != 0) { + if (GetElementNum(stride_tensor) != begin_ndim) { + return NNACL_ERR; + } + stride_data = (int *)(stride_tensor->data_); + if (stride_data == NULL) { + return NNACL_ERR; + } + } + + int axes[MAX_SHAPE_SIZE]; + if (axes_data == NULL) { + for (int i = 0; i < begin_ndim; ++i) { + axes[i] = i; + } + } else { + for (size_t i = 0; i < begin_ndim; i++) { + axes[i] = axes_data[i]; + } + for (int i = 0; i < begin_ndim; ++i) { + if (axes[i] < 0) { + axes[i] += *ndim_; + } + } + } + + for (size_t i = 0; i < *ndim_; i++) { + in_shape_[i] = 0; + begins_[i] = 0; + strides_[i] = 0; + } + for (size_t i = 0; i < *ndim_; ++i) { + in_shape_[i] = input_tensor->shape_[i]; + } + for (size_t i = 0; i < *ndim_; ++i) { + int axes_it = 0; + for (size_t j = 0; j < begin_ndim; j++) { + if (axes[j] == i) { + axes_it = j; + break; + } else { + axes_it++; + } + } + if (axes_it != begin_ndim) { + int axis = axes_it; + // begins or ends exceed limit will be set to limit + begins_[i] = imax(imin(begin_data[axis], input_tensor->shape_[i] - 1), -input_tensor->shape_[i]); + ends_[i] = imax(imin(end_data[axis], input_tensor->shape_[i]), -input_tensor->shape_[i] - 1); + strides_[i] = stride_data[axis]; + } else { + begins_[i] = 0; + ends_[i] = input_tensor->shape_[i]; + strides_[i] = 1; + } + } + return NNACL_OK; +} + +// note: begin, end, stride length are equal, but may less than rank of input +int StridedSliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (outputs_size != kStridedSliceOutputNum) { + return NNACL_PARAM_INVALID; + } + if (inputs_size != kStridedSliceInputNum && + !(inputs_size <= kStridedSliceMultiInputNumMax && inputs_size >= kStridedSliceMultiInputNumMin)) { + return NNACL_PARAM_INVALID; + } + if (parameter == NULL || outputs[0] == NULL || inputs[0] == NULL) { + return NNACL_NULL_PTR; + } + const TensorC *input = inputs[0]; + SetDataTypeFormat(outputs[0], inputs[0]); + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int in_shape_[MAX_SHAPE_SIZE]; + int begins_[MAX_SHAPE_SIZE]; + int ends_[MAX_SHAPE_SIZE]; + size_t in_shape_size_ = 0; + if (parameter->infer_flag_) { + ShapeSet(in_shape_, &in_shape_size_, input->shape_, input->shape_size_); + } + size_t begins_size_ = 0; + size_t ends_size_ = 0; + int strides_[MAX_SHAPE_SIZE]; + size_t strides_size_ = 0; + int begins_mask_[MAX_SHAPE_SIZE]; + int ends_mask_[MAX_SHAPE_SIZE]; + int ellipsis_mask_[MAX_SHAPE_SIZE]; + size_t ellipsis_mask_size_ = 0; + int new_axis_mask_[MAX_SHAPE_SIZE]; + size_t new_axis_mask_size_ = 0; + int shrink_axis_mask_[MAX_SHAPE_SIZE]; + size_t shrink_axis_mask_size_ = 0; + + StridedSliceParameter *param = (StridedSliceParameter *)parameter; + param->num_axes_ = in_shape_size_; + param->in_shape_length_ = in_shape_size_; + + int ndim_ = 0; + if (inputs_size == kStridedSliceInputNum) { + ndim_ = (int)(param->num_axes_); + + for (int i = 0; i < ndim_; i++) { + ShapePush(begins_, &begins_size_, param->begins_[i]); + ShapePush(ends_, &ends_size_, param->ends_[i]); + ShapePush(strides_, &strides_size_, param->strides_[i]); + } + } + if (!CheckInputs(inputs, inputs_size)) { + return NNACL_INFER_INVALID; + } + if (inputs_size == 4) { + const TensorC *begin_tensor = inputs[1]; + int *begin_data = (int *)(begin_tensor->data_); + const TensorC *end_tensor = inputs[2]; + int *end_data = (int *)(end_tensor->data_); + const TensorC *stride_tensor = inputs[3]; + int *stride_data = (int *)(stride_tensor->data_); + if (begin_data == NULL || end_data == NULL || stride_data == NULL) { + return NNACL_ERR; + } + ndim_ = GetElementNum(begin_tensor); + for (int i = 0; i < ndim_; ++i) { + ShapePush(begins_, &begins_size_, begin_data[i]); + ShapePush(ends_, &ends_size_, end_data[i]); + ShapePush(strides_, &strides_size_, stride_data[i]); + } + } + if (inputs_size == 5) { + int ret = HandleAxesInputExist(inputs, &ndim_, in_shape_, begins_, strides_, ends_); + if (ret != NNACL_OK) { + return ret; + } + } + + // set all mask to original input shape + ellipsis_mask_size_ = ndim_; + new_axis_mask_size_ = ndim_; + shrink_axis_mask_size_ = ndim_; + begins_size_ = ndim_; + ends_size_ = ndim_; + strides_size_ = ndim_; + + // convert bit to vector + for (int i = 0; i < ndim_; i++) { + begins_mask_[i] = (uint32_t)(param->begins_mask_) & (1 << i); + ends_mask_[i] = (uint32_t)(param->ends_mask_) & (1 << i); + ellipsis_mask_[i] = (uint32_t)(param->ellipsisMask_) & (1 << i); + new_axis_mask_[i] = (uint32_t)(param->newAxisMask_) & (1 << i); + shrink_axis_mask_[i] = (uint32_t)(param->shrinkAxisMask_) & (1 << i); + } + + // ApplyNewAxisMask(); + for (size_t i = 0; i < new_axis_mask_size_; i++) { + if (new_axis_mask_[i]) { + ndim_ += 1; + ShapeInsert(in_shape_, &in_shape_size_, i, 1); + begins_[i] = 0; + ends_[i] = 1; + strides_[i] = 1; + + ShapePush(begins_, &begins_size_, 0); + ShapePush(ends_, &ends_size_, in_shape_[ndim_ - 1]); + ShapePush(strides_, &strides_size_, 1); + + begins_mask_[i] = false; + ends_mask_[i] = false; + ellipsis_mask_[i] = false; + shrink_axis_mask_[i] = false; + } + } + // ApplyBeginMask(); + for (int i = 0; i < ndim_; i++) { + if (begins_mask_[i]) { + begins_[i] = 0; + } + } + // ApplyEndMask(); + for (int i = 0; i < ndim_; i++) { + if (ends_mask_[i]) { + ends_[i] = in_shape_[i]; + } + } + // ApplyEllipsisMask(); + for (size_t i = 0; i < ellipsis_mask_size_; i++) { + if (ellipsis_mask_[i]) { + begins_[i] = 0; + ends_[i] = in_shape_[i]; + break; + } + } + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapeSet(output_shape, &output_shape_size, in_shape_, in_shape_size_); + + // TransIndexToPositive(); + for (int i = 0; i < (int)(begins_size_); ++i) { + if (begins_[i] < 0) { + begins_[i] += in_shape_[i]; + } + if (ends_[i] < 0) { + ends_[i] += in_shape_[i]; + } + } + + for (int i = 0; i < ndim_; i++) { + if (strides_[i] == 0) { + return NNACL_ERR; + } + output_shape[i] = (ends_[i] - begins_[i] + strides_[i] + (strides_[i] < 0 ? 1 : -1)) / strides_[i]; + } + + // ApplyShrinkMask + int old_out_shape[MAX_SHAPE_SIZE]; + size_t old_out_shape_size = 0; + ShapeSet(old_out_shape, &old_out_shape_size, output_shape, output_shape_size); + output_shape_size = 0; + for (size_t i = 0; i < shrink_axis_mask_size_; i++) { + if (shrink_axis_mask_[i]) { + ends_[i] = begins_[i] + 1; + strides_[i] = 1; + } else { + ShapePush(output_shape, &output_shape_size, old_out_shape[i]); + } + } + for (size_t i = shrink_axis_mask_size_; i < old_out_shape_size; i++) { + ShapePush(output_shape, &output_shape_size, old_out_shape[i]); + } + + SetShapeArray(outputs[0], output_shape, output_shape_size); + + for (int i = 0; i < ndim_; i++) { + param->begins_[i] = begins_[i]; + param->ends_[i] = ends_[i]; + param->in_shape_[i] = in_shape_[i]; + param->strides_[i] = strides_[i]; + } + + for (int i = ndim_; i < param->in_shape_length_; i++) { + param->begins_[i] = 0; + param->ends_[i] = in_shape_[i]; + param->in_shape_[i] = in_shape_[i]; + param->strides_[i] = 1; + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/strided_slice_infer.h b/mindspore/lite/nnacl/infer/strided_slice_infer.h new file mode 100644 index 0000000000..1c9792eb43 --- /dev/null +++ b/mindspore/lite/nnacl/infer/strided_slice_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_STRIDED_SLICE_INFER_H +#define MINDSPORE_LITE_NNACL_STRIDED_SLICE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/strided_slice_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int StridedSliceInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_STRIDED_SLICE_INFER_H diff --git a/mindspore/lite/nnacl/infer/switch_infer.c b/mindspore/lite/nnacl/infer/switch_infer.c new file mode 100644 index 0000000000..b51af00b92 --- /dev/null +++ b/mindspore/lite/nnacl/infer/switch_infer.c @@ -0,0 +1,103 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/switch_infer.h" +#include + +int SwitchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (2 * (inputs_size - 1) != outputs_size) { + return NNACL_ERR; + } + + for (size_t i = 0; i < outputs_size / 2; i++) { + const TensorC *input = inputs[i + 1]; + TensorC *output_true = outputs[i]; + TensorC *output_false = outputs[i + outputs_size / 2]; + + SetDataTypeFormat(output_false, input); + SetDataTypeFormat(output_true, input); + + if (input->data_type_ == kObjectTypeTensorType) { + TensorListC *input_tensorlist = (TensorListC *)(input); + TensorListC *output_true_tensorlist = (TensorListC *)(output_true); + TensorListC *output_false_tensorlist = (TensorListC *)(output_false); + + ShapeSet(output_true_tensorlist->element_shape_, &output_true_tensorlist->element_shape_size_, + input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); + ShapeSet(output_false_tensorlist->element_shape_, &output_false_tensorlist->element_shape_size_, + input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); + output_true_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_false_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_true_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + output_false_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + + // note: need delete below? + for (size_t j = 0; j < output_false_tensorlist->element_num_; j++) { + memcpy(output_true_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + memcpy(output_false_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + } + + } else { + } + } + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + for (size_t i = 0; i < outputs_size / 2; i++) { + const TensorC *input = inputs[i + 1]; + TensorC *output_true = outputs[i]; + TensorC *output_false = outputs[i + outputs_size / 2]; + + SetDataTypeFormat(output_false, input); + SetDataTypeFormat(output_true, input); + + if (input->data_type_ == kObjectTypeTensorType) { + TensorListC *input_tensorlist = (TensorListC *)(input); + TensorListC *output_true_tensorlist = (TensorListC *)(output_true); + TensorListC *output_false_tensorlist = (TensorListC *)(output_false); + + ShapeSet(output_true_tensorlist->element_shape_, &output_true_tensorlist->element_shape_size_, + input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); + ShapeSet(output_false_tensorlist->element_shape_, &output_false_tensorlist->element_shape_size_, + input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); + output_true_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_false_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; + output_true_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + output_false_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; + + output_false_tensorlist->element_num_ = input_tensorlist->element_num_; + output_true_tensorlist->element_num_ = input_tensorlist->element_num_; + + for (size_t j = 0; j < output_false_tensorlist->element_num_; j++) { + memcpy(output_true_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + memcpy(output_false_tensorlist->tensors_[j], input_tensorlist->tensors_[j], sizeof(TensorC)); + } + + } else { + SetShapeTensor(output_true, input); + SetShapeTensor(output_false, input); + } + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/switch_infer.h b/mindspore/lite/nnacl/infer/switch_infer.h new file mode 100644 index 0000000000..673d1efa63 --- /dev/null +++ b/mindspore/lite/nnacl/infer/switch_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_SWITCH_INFER_H +#define MINDSPORE_LITE_NNACL_SWITCH_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/softmax_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int SwitchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_SWITCH_INFER_H diff --git a/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c new file mode 100644 index 0000000000..694491f819 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c @@ -0,0 +1,75 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tensorlist_fromtensor_infer.h" + +int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + const TensorC *input0 = inputs[0]; + + if (input0->shape_size_ < 1) { + return NNACL_ERR; + } + int dim0 = input0->shape_[0]; + if (dim0 < 0) { + return NNACL_ERR; + } + const TensorC *input1 = inputs[1]; + if (input1->data_ == NULL) { + return NNACL_NULL_PTR; + } + int *ele_shape_ptr = (int *)(input1->data_); + TensorListC *output = (TensorListC *)(outputs[0]); + + vvector *tensor_shape = (vvector *)malloc(sizeof(vvector)); + if (tensor_shape == NULL) { + return NNACL_NULL_PTR; + } + tensor_shape->size_ = dim0; + tensor_shape->shape_ = (int **)malloc(tensor_shape->size_ * sizeof(int *)); + if (tensor_shape->shape_ == NULL) { + free(tensor_shape); + return NNACL_NULL_PTR; + } + tensor_shape->shape_size_ = (int *)malloc(tensor_shape->size_ * sizeof(int)); + if (tensor_shape->shape_size_ == NULL) { + free(tensor_shape->shape_); + free(tensor_shape); + return NNACL_NULL_PTR; + } + + for (size_t i = 0; i < dim0; i++) { + tensor_shape->shape_[i] = (int *)(input0->shape_ + 1); + tensor_shape->shape_size_[i] = input0->shape_size_ - 1; + } + + ShapeSet(output->element_shape_, &(output->element_shape_size_), ele_shape_ptr, GetElementNum(input1)); + output->element_num_ = dim0; + output->data_type_ = kObjectTypeTensorType; + output->format_ = Format_NHWC; + MallocTensorListData(output, input0->data_type_, tensor_shape); + free(tensor_shape->shape_); + free(tensor_shape->shape_size_); + free(tensor_shape); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.h b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.h new file mode 100644 index 0000000000..9ac106cc22 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSORLIST_FROMTENSOR_INFER_H +#define MINDSPORE_LITE_NNACL_TENSORLIST_FROMTENSOR_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TENSORLIST_FROMTENSOR_INFER_H diff --git a/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c new file mode 100644 index 0000000000..9c84fc65fe --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c @@ -0,0 +1,80 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tensorlist_getitem_infer.h" + +int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + TensorListC *input0 = (TensorListC *)(inputs[0]); + const TensorC *get_index = inputs[1]; + if (GetElementNum(get_index) != 1) { + return NNACL_ERR; + } + if (get_index->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int index = ((int *)(get_index->data_))[0]; + if (index < 0 || index > (input0->element_num_ - 1)) { + return NNACL_ERR; + } + TensorC *tensor_index = input0->tensors_[index]; + TensorC *output = outputs[0]; + if (tensor_index->data_type_ != kTypeUnknown) { + output->data_type_ = tensor_index->data_type_; + ShapeSet(output->shape_, &(output->shape_size_), tensor_index->shape_, tensor_index->shape_size_); + } else { + const TensorC *input2 = inputs[2]; + if (input2->data_ == NULL) { + return NNACL_NULL_PTR; + } + int *ele_shape_data = (int *)(input2->data_); + int element_shape[MAX_SHAPE_SIZE]; + size_t element_shape_size = 0; + for (int i = 0; i < GetElementNum(input2); ++i) { + ShapePush(element_shape, &element_shape_size, ele_shape_data[i]); + } + int status = + TensorListMergeShape(element_shape, &element_shape_size, input0->element_shape_, input0->element_shape_size_); + if (status != NNACL_OK) { + return NNACL_ERR; + } + if (!TensorListIsFullyDefined(element_shape, element_shape_size)) { + for (int i = 0; i < input0->element_num_; ++i) { + TensorC *input = input0->tensors_[i]; + if (input->data_type_ != kTypeUnknown) { + status = TensorListMergeShape(element_shape, &element_shape_size, input->shape_, input->shape_size_); + if (status != NNACL_OK) { + return NNACL_ERR; + } + } + } + } + if (!TensorListIsFullyDefined(element_shape, element_shape_size)) { // the pre is the same judge condition + return NNACL_ERR; + } + output->data_type_ = input0->tensors_data_type_; + SetShapeArray(output, element_shape, element_shape_size); + } + output->format_ = input0->tensors_[index]->format_; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.h b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.h new file mode 100644 index 0000000000..663a626a04 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSORLIST_GETITEM_INFER_H +#define MINDSPORE_LITE_NNACL_TENSORLIST_GETITEM_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/tensorlist_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TENSORLIST_GETITEM_INFER_H diff --git a/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c new file mode 100644 index 0000000000..cf6adc1a24 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c @@ -0,0 +1,79 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tensorlist_reserve_infer.h" + +int TensorListReserveInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input0 = inputs[0]; + int ele_shape_type = input0->data_type_; + if (ele_shape_type != kNumberTypeInt && ele_shape_type != kNumberTypeInt32) { + return NNACL_ERR; + } + if (input0->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int *ele_shape_ptr = (int *)(input0->data_); + + const TensorC *input1 = inputs[1]; + int num_ele_type = input1->data_type_; + if (num_ele_type != kNumberTypeInt && ele_shape_type != kNumberTypeInt32) { + return NNACL_ERR; + } + if (GetElementNum(input1) != 1) { + return NNACL_ERR; + } + if (input1->data_ == NULL) { + return NNACL_INFER_INVALID; + } + int num_elements = ((int *)(input1->data_))[0]; + TensorListC *output = (TensorListC *)(outputs[0]); + output->data_type_ = kObjectTypeTensorType; + output->format_ = Format_NHWC; + ShapeSet(output->element_shape_, &(output->element_shape_size_), ele_shape_ptr, GetElementNum(input0)); + output->element_num_ = num_elements; + + vvector *tmp_shape = (vvector *)malloc(sizeof(vvector)); + if (tmp_shape == NULL) { + return NNACL_NULL_PTR; + } + tmp_shape->size_ = num_elements; + tmp_shape->shape_ = (int **)malloc(tmp_shape->size_ * sizeof(int *)); + if (tmp_shape->shape_ == NULL) { + free(tmp_shape); + return NNACL_NULL_PTR; + } + tmp_shape->shape_size_ = (int *)malloc(tmp_shape->size_ * sizeof(int)); + if (tmp_shape->shape_size_ == NULL) { + free(tmp_shape->shape_); + free(tmp_shape); + return NNACL_NULL_PTR; + } + + for (size_t i = 0; i < num_elements; i++) { + tmp_shape->shape_size_[i] = 0; + tmp_shape->shape_[i] = NULL; + } + MallocTensorListData(output, kTypeUnknown, tmp_shape); + free(tmp_shape->shape_size_); + free(tmp_shape->shape_); + free(tmp_shape); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.h b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.h new file mode 100644 index 0000000000..4cd2c453e2 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSORLIST_RESERVE_INFER_H +#define MINDSPORE_LITE_NNACL_TENSORLIST_RESERVE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TensorListReserveInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TENSORLIST_RESERVE_INFER_H diff --git a/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.c b/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.c new file mode 100644 index 0000000000..e7ee7d310e --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.c @@ -0,0 +1,128 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tensorlist_setitem_infer.h" + +int PreJudge(const TensorC *get_index, TensorListC *input0, const TensorC *value_tensor) { + if (get_index->data_ == NULL || value_tensor->data_ == NULL) { + return NNACL_INFER_INVALID; + } + + if (get_index->data_type_ != kNumberTypeInt && get_index->data_type_ != kNumberTypeInt32) { + return NNACL_ERR; + } + if (GetElementNum(get_index) != 1) { + return NNACL_ERR; + } + if (get_index->data_ == NULL) { + return NNACL_NULL_PTR; + } + return NNACL_OK; +} + +int TensorListSetItemInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + TensorListC *input0 = (TensorListC *)(inputs[0]); + const TensorC *get_index = inputs[1]; + const TensorC *value_tensor = inputs[2]; + TensorListC *output0 = (TensorListC *)(outputs[0]); + output0->data_type_ = input0->data_type_; + output0->format_ = input0->format_; + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int judge_ret = PreJudge(get_index, input0, value_tensor); + if (judge_ret != NNACL_OK) { + return judge_ret; + } + + int index = ((int *)(get_index->data_))[0]; + if (index < 0 || (index >= ((int)(input0->element_num_)) && index != 0)) { + return NNACL_ERR; + } + + output0->max_elements_num_ = input0->max_elements_num_; + + if (input0->element_num_ == 0 && input0->element_shape_size_ == 0 && index == 0) { + ShapeSet(input0->element_shape_, &(input0->element_shape_size_), value_tensor->shape_, value_tensor->shape_size_); + ShapeSet(output0->element_shape_, &(output0->element_shape_size_), value_tensor->shape_, value_tensor->shape_size_); + } else { + ShapeSet(output0->element_shape_, &(output0->element_shape_size_), input0->element_shape_, + input0->element_shape_size_); + } + + vvector *out_shape = (vvector *)malloc(sizeof(vvector)); + if (out_shape == NULL) { + return NNACL_NULL_PTR; + } + out_shape->size_ = 0; + out_shape->shape_ = (int **)malloc((input0->element_num_ + 1) * sizeof(int *)); + if (out_shape->shape_ == NULL) { + free(out_shape); + return NNACL_NULL_PTR; + } + out_shape->shape_size_ = (int *)malloc((input0->element_num_ + 1) * sizeof(int)); + if (out_shape->shape_size_ == NULL) { + free(out_shape->shape_); + free(out_shape); + return NNACL_NULL_PTR; + } + + if (index == 0 && input0->element_num_ == 0) { // uninitialized tensorlist + out_shape->shape_[out_shape->size_] = (int *)(value_tensor->shape_); + out_shape->shape_size_[out_shape->size_] = value_tensor->shape_size_; + out_shape->size_++; + output0->element_num_ = 1; + } else { + output0->element_num_ = input0->element_num_; + for (int i = 0; i < input0->element_num_; ++i) { + TensorC *src_ptr = input0->tensors_[i]; + if (src_ptr == NULL) { + free(out_shape->shape_); + free(out_shape->shape_size_); + free(out_shape); + return NNACL_ERR; + } + if (src_ptr->data_type_ != kTypeUnknown) { + out_shape->shape_[out_shape->size_] = src_ptr->shape_; + out_shape->shape_size_[out_shape->size_] = src_ptr->shape_size_; + out_shape->size_++; + } else { + out_shape->shape_[out_shape->size_] = NULL; + out_shape->shape_size_[out_shape->size_] = 0; + out_shape->size_++; + } + } + } + + if (input0->tensors_data_type_ == kTypeUnknown) { + input0->tensors_data_type_ = value_tensor->data_type_; + } + + out_shape->shape_[index] = (int *)(value_tensor->shape_); + out_shape->shape_size_[index] = value_tensor->shape_size_; + MallocTensorListData(output0, input0->tensors_data_type_, out_shape); + free(out_shape->shape_); + free(out_shape->shape_size_); + free(out_shape); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.h b/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.h new file mode 100644 index 0000000000..d7b6b20d10 --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_setitem_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSORLIST_SETITEM_INFER_H +#define MINDSPORE_LITE_NNACL_TENSORLIST_SETITEM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TensorListSetItemInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TENSORLIST_SETITEM_INFER_H diff --git a/mindspore/lite/nnacl/infer/tensorlist_stack_infer.c b/mindspore/lite/nnacl/infer/tensorlist_stack_infer.c new file mode 100644 index 0000000000..0881cb67ff --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_stack_infer.c @@ -0,0 +1,68 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tensorlist_stack_infer.h" + +int TensorListStackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + TensorListC *input0 = (TensorListC *)(inputs[0]); + if (input0->element_num_ == 0) { + return NNACL_ERR; + } + const TensorC *ele_shape = inputs[1]; // element shape + if (ele_shape->data_ == NULL) { + return NNACL_NULL_PTR; + } + int *ele_shape_ptr = (int *)(ele_shape->data_); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (int i = 0; i < GetElementNum(ele_shape); ++i) { + ShapePush(output_shape, &output_shape_size, ele_shape_ptr[i]); + } + + int status = + TensorListMergeShape(output_shape, &output_shape_size, input0->element_shape_, input0->element_shape_size_); + if (status == NNACL_ERR) { + return NNACL_ERR; + } + if (!TensorListIsFullyDefined(output_shape, output_shape_size)) { + return NNACL_ERR; + } + if (!TensorListIsFullyDefined(input0->element_shape_, input0->element_shape_size_)) { + for (int i = 0; i < input0->element_num_; ++i) { + TensorC *tensor_ele = input0->tensors_[i]; + if (tensor_ele->data_type_ != kTypeUnknown) { + status = TensorListMergeShape(output_shape, &output_shape_size, tensor_ele->shape_, tensor_ele->shape_size_); + if (status == NNACL_ERR) { + return NNACL_ERR; + } + } + } + } + TensorC *output = outputs[0]; + output->data_type_ = input0->tensors_data_type_; + output->format_ = input0->format_; + ShapeInsert(output_shape, &output_shape_size, 0, input0->element_num_); + SetShapeArray(output, output_shape, output_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tensorlist_stack_infer.h b/mindspore/lite/nnacl/infer/tensorlist_stack_infer.h new file mode 100644 index 0000000000..38d6ce0cfd --- /dev/null +++ b/mindspore/lite/nnacl/infer/tensorlist_stack_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSORLIST_STACK_INFER_H +#define MINDSPORE_LITE_NNACL_TENSORLIST_STACK_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TensorListStackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TENSORLIST_STACK_INFER_H diff --git a/mindspore/lite/nnacl/infer/tile_infer.c b/mindspore/lite/nnacl/infer/tile_infer.c new file mode 100644 index 0000000000..c4a6e1954b --- /dev/null +++ b/mindspore/lite/nnacl/infer/tile_infer.c @@ -0,0 +1,109 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/tile_infer.h" +#include + +void TileParamCaffe2Tflite(TileParameter *param, size_t out_shape_size) { + if (param->dims_size_ != 0) { + int multiples_size_tmp[5] = {0}; + for (size_t i = 0; i < out_shape_size; i++) { + multiples_size_tmp[i] = 1; + } + for (size_t i = 0; i < param->dims_size_; i++) { + multiples_size_tmp[param->dims_[i]] = param->multiples_[i]; + } + for (size_t i = 0; i < 5; i++) { + param->multiples_[i] = multiples_size_tmp[i]; + } + } +} + +int TileInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + TileParameter *param = (TileParameter *)parameter; + + size_t multiples_size = 0; + if (inputs_size != 2) { + return NNACL_ERR; + } + int data_num = GetElementNum(inputs[1]); + if (data_num > (int)(input->shape_size_)) { + return NNACL_INPUT_TENSOR_ERROR; + } + multiples_size = data_num; + int *input1_data = inputs[1]->data_; + if (input1_data == NULL) { + return NNACL_INFER_INVALID; + } + for (size_t i = 0; i < data_num; i++) { + param->multiples_[i] = input1_data[i]; + } + +#ifdef SUPPORT_TRAIN + const size_t in_dims = input->shape_size_; + const size_t delta_dims = in_dims - multiples_size; + + size_t i = 0; + for (; i < delta_dims; ++i) { + int tmp = input->shape_[i]; + ShapePush(out_shape, &out_shape_size, tmp); + } + for (; i < in_dims; ++i) { + int tmp = input->shape_[i] * (param->multiples_[i - delta_dims]); + ShapePush(out_shape, &out_shape_size, tmp); + } +#else + int *dims = param->dims_; + size_t dims_size = param->dims_size_; + if (dims_size == 0) { + for (int dim = 0; dim < GetElementNum(inputs[1]); ++dim) { + ShapePush(dims, &dims_size, dim); + } + param->dims_size_ = dims_size; + } + if (multiples_size != dims_size) { + return NNACL_ERR; + } + for (size_t i = 0; i < input->shape_size_; ++i) { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + for (size_t i = 0; i < dims_size; ++i) { + if (input->shape_[dims[i]] != 0 && param->multiples_[i] > INT_MAX / input->shape_[dims[i]]) { + return NNACL_ERR; + } + out_shape[dims[i]] = input->shape_[dims[i]] * (param->multiples_[i]); + } + // change caffe param format to tflite + TileParamCaffe2Tflite(param, out_shape_size); +#endif + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/tile_infer.h b/mindspore/lite/nnacl/infer/tile_infer.h new file mode 100644 index 0000000000..f5200949da --- /dev/null +++ b/mindspore/lite/nnacl/infer/tile_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TILE_INFER_H +#define MINDSPORE_LITE_NNACL_TILE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/base/tile_base.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TileInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TILE_INFER_H diff --git a/mindspore/lite/nnacl/infer/topk_infer.c b/mindspore/lite/nnacl/infer/topk_infer.c new file mode 100644 index 0000000000..8b851e50e5 --- /dev/null +++ b/mindspore/lite/nnacl/infer/topk_infer.c @@ -0,0 +1,50 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/topk_infer.h" + +int TopKInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 1, 2, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + if (input->shape_size_ == 4 && input->format_ != Format_NHWC) { + return NNACL_ERR; + } + TensorC *output0 = outputs[0]; + TensorC *output1 = outputs[1]; + SetDataTypeFormat(output0, input); + output1->data_type_ = kNumberTypeInt32; + output1->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + TopkParameter *param = (TopkParameter *)parameter; + const TensorC *input_k_tensor = inputs[1]; + param->k_ = ((int32_t *)input_k_tensor->data_)[0]; + + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + ShapeSet(out_shape, &out_shape_size, input->shape_, input->shape_size_); + out_shape[out_shape_size - 1] = param->k_; + + SetShapeArray(output0, out_shape, out_shape_size); + SetShapeArray(output1, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/topk_infer.h b/mindspore/lite/nnacl/infer/topk_infer.h new file mode 100644 index 0000000000..791cabdf8f --- /dev/null +++ b/mindspore/lite/nnacl/infer/topk_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TOPK_INFER_H +#define MINDSPORE_LITE_NNACL_TOPK_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/fp32/topk_fp32.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TopKInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TOPK_INFER_H diff --git a/mindspore/lite/nnacl/infer/transpose_infer.c b/mindspore/lite/nnacl/infer/transpose_infer.c new file mode 100644 index 0000000000..2780fcc38a --- /dev/null +++ b/mindspore/lite/nnacl/infer/transpose_infer.c @@ -0,0 +1,83 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/transpose_infer.h" + +bool CheckPermTransFormat(const int *perm, const int *perm_transformat, const size_t size) { + for (size_t i = 0; i < size; ++i) { + if (perm[i] != perm_transformat[i]) { + return false; + } + } + return true; +} + +int TransposeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const TensorC *perm_tensor = inputs[1]; + const int32_t *perm_data = (int32_t *)perm_tensor->data_; + const size_t perms_num = (size_t)perm_tensor->shape_[0]; + if (perm_tensor->shape_size_ == 0) { + return NNACL_INFER_INVALID; + } + int perm[MAX_SHAPE_SIZE]; + size_t perm_size = 0; + for (size_t i = 0; i < perms_num; i++) { + ShapePush(perm, &perm_size, perm_data[i]); + } + int out_shape[MAX_SHAPE_SIZE]; + if (input->shape_size_ != 4 && perms_num == 4) { + for (size_t i = 0; i < input->shape_size_; ++i) { + out_shape[i] = input->shape_[i]; + } + SetShapeArray(output, out_shape, input->shape_size_); + return NNACL_OK; + } + const int nchw2nhwc[4] = {0, 2, 3, 1}; + const int nhwc2nchw[4] = {0, 3, 1, 2}; + if (perms_num == 4) { + if (input->format_ == Format_NCHW && CheckPermTransFormat(perm, nchw2nhwc, perms_num)) { + output->format_ = Format_NHWC; + } else if (input->format_ == Format_NHWC && CheckPermTransFormat(perm, nhwc2nchw, perms_num)) { + output->format_ = Format_NCHW; + } + } + output->shape_size_ = perm_size; + for (size_t i = 0; i < perm_size; ++i) { + out_shape[i] = input->shape_[perm[i]]; + } + if (perm_size == 0) { + size_t shape_size = input->shape_size_; + output->shape_size_ = shape_size; + for (size_t i = 0; i < shape_size; ++i) { + out_shape[shape_size - i - 1] = input->shape_[i]; + } + } + SetShapeArray(output, out_shape, output->shape_size_); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/transpose_infer.h b/mindspore/lite/nnacl/infer/transpose_infer.h new file mode 100644 index 0000000000..4a8cb4aec8 --- /dev/null +++ b/mindspore/lite/nnacl/infer/transpose_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TRANSPOSE_INFER_H +#define MINDSPORE_LITE_NNACL_TRANSPOSE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/transpose.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int TransposeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_TRANSPOSE_INFER_H diff --git a/mindspore/lite/nnacl/infer/uniform_real_infer.c b/mindspore/lite/nnacl/infer/uniform_real_infer.c new file mode 100644 index 0000000000..b9a65caed4 --- /dev/null +++ b/mindspore/lite/nnacl/infer/uniform_real_infer.c @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/uniform_real_infer.h" + +int UniformRealInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int32_t *input_data = (int32_t *)(inputs[0]->data_); + if (input_data == NULL) { + return NNACL_INFER_INVALID; + } + int input_num = GetElementNum(inputs[0]); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = input_num; + for (int i = 0; i < input_num; i++) { + output_shape[i] = input_data[i]; + } + SetShapeArray(outputs[0], output_shape, output_shape_size); + outputs[0]->data_type_ = kNumberTypeFloat32; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/uniform_real_infer.h b/mindspore/lite/nnacl/infer/uniform_real_infer.h new file mode 100644 index 0000000000..ceef8dec71 --- /dev/null +++ b/mindspore/lite/nnacl/infer/uniform_real_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_UNIFORM_REAL_INFER_H +#define MINDSPORE_LITE_NNACL_UNIFORM_REAL_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int UniformRealInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_UNIFORM_REAL_INFER_H diff --git a/mindspore/lite/nnacl/infer/unique_infer.c b/mindspore/lite/nnacl/infer/unique_infer.c new file mode 100644 index 0000000000..46721dc046 --- /dev/null +++ b/mindspore/lite/nnacl/infer/unique_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/unique_infer.h" + +int UniqueInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 2); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output0 = outputs[0]; + TensorC *output1 = outputs[1]; + + SetDataTypeFormat(output0, input); + output1->data_type_ = kNumberTypeInt32; + output1->format_ = input->format_; + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + SetShapeTensor(output0, input); + SetShapeTensor(output1, input); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/unique_infer.h b/mindspore/lite/nnacl/infer/unique_infer.h new file mode 100644 index 0000000000..ec8b8d434d --- /dev/null +++ b/mindspore/lite/nnacl/infer/unique_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_UNIQUE_INFER_H +#define MINDSPORE_LITE_NNACL_UNIQUE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int UniqueInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_UNIQUE_INFER_H diff --git a/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.c b/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.c new file mode 100644 index 0000000000..ea382d139a --- /dev/null +++ b/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.c @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/unsorted_segment_sum_infer.h" + +int UnsortedSegmentSumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 3, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + + TensorC *out = outputs[0]; + const TensorC *x = inputs[0]; + const TensorC *segment_id = inputs[1]; + int num_segments = *(int *)(inputs[2]->data_); + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + ShapePush(output_shape, &output_shape_size, num_segments); + for (int index = segment_id->shape_size_; index < (int)(x->shape_size_); index++) { + ShapePush(output_shape, &output_shape_size, x->shape_[index]); + } + SetShapeArray(out, output_shape, output_shape_size); + SetDataTypeFormat(out, x); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.h b/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.h new file mode 100644 index 0000000000..3945e2907b --- /dev/null +++ b/mindspore/lite/nnacl/infer/unsorted_segment_sum_infer.h @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_UNSORTED_SEGMENT_SUM_INFER_H +#define MINDSPORE_LITE_NNACL_UNSORTED_SEGMENT_SUM_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct UnsortedSegmentSumParameter { + OpParameter op_parameter_; + int segments_num_; +} UnsortedSegmentSumParameter; + +int UnsortedSegmentSumInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, + size_t outputs_size, OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_UNSORTED_SEGMENT_SUM_INFER_H diff --git a/mindspore/lite/nnacl/infer/unsqueeze_infer.c b/mindspore/lite/nnacl/infer/unsqueeze_infer.c new file mode 100644 index 0000000000..9e8f6b4f35 --- /dev/null +++ b/mindspore/lite/nnacl/infer/unsqueeze_infer.c @@ -0,0 +1,63 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/unsqueeze_infer.h" + +int UnsqueezeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + UnSqueezeParameter *param = (UnSqueezeParameter *)parameter; + int in_rank = input->shape_size_; + int dim_rank = param->num_dim_; + int out_shape[MAX_SHAPE_SIZE]; + size_t out_shape_size = 0; + if (dim_rank == 0) { + for (size_t i = 0; i < input->shape_size_; i++) { + if (input->shape_[i] != 1) { + ShapePush(out_shape, &out_shape_size, input->shape_[i]); + } + } + } else { + int sz = in_rank + dim_rank; + size_t in_itr = 0; + size_t ax_itr = 0; + for (size_t i = 0; i < sz; i++) { + if (ax_itr < dim_rank && param->dims_[ax_itr] == (int)(i)) { + ShapePush(out_shape, &out_shape_size, 1); + ax_itr++; + } else if (ax_itr < dim_rank && param->dims_[ax_itr] + sz == i) { + ShapePush(out_shape, &out_shape_size, 1); + ax_itr++; + } else { + ShapePush(out_shape, &out_shape_size, input->shape_[in_itr]); + in_itr++; + } + } + } + SetShapeArray(output, out_shape, out_shape_size); + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/unsqueeze_infer.h b/mindspore/lite/nnacl/infer/unsqueeze_infer.h new file mode 100644 index 0000000000..72db2bcc19 --- /dev/null +++ b/mindspore/lite/nnacl/infer/unsqueeze_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_UNSQUEEZE_INFER_H +#define MINDSPORE_LITE_NNACL_UNSQUEEZE_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/unsqueeze_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int UnsqueezeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_UNSQUEEZE_INFER_H diff --git a/mindspore/lite/nnacl/infer/unstack_infer.c b/mindspore/lite/nnacl/infer/unstack_infer.c new file mode 100644 index 0000000000..4e8971a740 --- /dev/null +++ b/mindspore/lite/nnacl/infer/unstack_infer.c @@ -0,0 +1,52 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/unstack_infer.h" + +int UnstackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + const TensorC *input = inputs[0]; + UnstackParameter *param = (UnstackParameter *)parameter; + int axis = param->axis_ < 0 ? param->axis_ + input->shape_size_ : param->axis_; + if (axis < 0 || axis >= input->shape_size_) { + return NNACL_PARAM_INVALID; + } + for (size_t i = 0; i < outputs_size; i++) { + SetDataTypeFormat(outputs[i], input); + } + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + int output_shape[MAX_SHAPE_SIZE]; + size_t output_shape_size = 0; + for (size_t i = 0; i < input->shape_size_; ++i) { + if (i != axis) { + ShapePush(output_shape, &output_shape_size, input->shape_[i]); + } + } + for (size_t i = 0; i < outputs_size; i++) { + if (outputs[i] == NULL) { + return NNACL_NULL_PTR; + } + SetShapeArray(outputs[i], output_shape, output_shape_size); + } + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/unstack_infer.h b/mindspore/lite/nnacl/infer/unstack_infer.h new file mode 100644 index 0000000000..787e369f01 --- /dev/null +++ b/mindspore/lite/nnacl/infer/unstack_infer.h @@ -0,0 +1,32 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_UNSTACK_INFER_H +#define MINDSPORE_LITE_NNACL_UNSTACK_INFER_H + +#include "nnacl/infer/common_infer.h" +#include "nnacl/unstack_parameter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int UnstackInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_UNSTACK_INFER_H diff --git a/mindspore/lite/nnacl/infer/where_infer.c b/mindspore/lite/nnacl/infer/where_infer.c new file mode 100644 index 0000000000..88e84a1c84 --- /dev/null +++ b/mindspore/lite/nnacl/infer/where_infer.c @@ -0,0 +1,75 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/where_infer.h" + +int WhereInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); + if (check_ret != NNACL_OK) { + return check_ret; + } + + const TensorC *input = inputs[0]; + TensorC *output = outputs[0]; + + // Need to dynamically allocate at runtime. + if (inputs_size == 1) { + return NNACL_INFER_INVALID; + } + + if (inputs_size < 3 || outputs_size != 1) { + return NNACL_INPUT_TENSOR_ERROR; + } + + SetDataTypeFormat(output, input); + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + const TensorC *input0 = inputs[0]; + const TensorC *input1 = inputs[1]; + const TensorC *input2 = inputs[2]; + int num = GetElementNum(input0); + int num1 = GetElementNum(input1); + int num2 = GetElementNum(input2); + int nummax = num > num1 ? num : (num1 > num2 ? num1 : num2); + int axisout = 0; + size_t temp = 0; + for (size_t j = 0; j < input0->shape_size_; j++) { + if (input0->shape_[j] == input1->shape_[j] && input0->shape_[j] != input2->shape_[j]) { + axisout = j; + break; + } + if (input0->shape_[j] == input2->shape_[j] && input0->shape_[j] != input1->shape_[j]) { + axisout = j; + break; + } + if (input1->shape_[j] == input2->shape_[j] && input0->shape_[j] != input1->shape_[j]) { + axisout = j; + break; + } + temp += 1; + if (temp == input0->shape_size_) { + SetShapeTensor(output, input); + output->data_type_ = input->data_type_; + return NNACL_OK; + } + } + ShapeSet(output->shape_, &output->shape_size_, input0->shape_, input0->shape_size_); + output->shape_[axisout] = nummax; + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/where_infer.h b/mindspore/lite/nnacl/infer/where_infer.h new file mode 100644 index 0000000000..182a8b45ce --- /dev/null +++ b/mindspore/lite/nnacl/infer/where_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_WHERE_INFER_H +#define MINDSPORE_LITE_NNACL_WHERE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int WhereInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_WHERE_INFER_H diff --git a/mindspore/lite/nnacl/infer/while_infer.c b/mindspore/lite/nnacl/infer/while_infer.c new file mode 100644 index 0000000000..1e0de40e13 --- /dev/null +++ b/mindspore/lite/nnacl/infer/while_infer.c @@ -0,0 +1,30 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/infer/while_infer.h" + +int WhileInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter) { + if (inputs_size != outputs_size) { + return NNACL_ERR; + } + for (size_t i = 0; i < inputs_size; i++) { + SetDataTypeFormat(outputs[i], inputs[i]); + SetShapeTensor(outputs[i], inputs[i]); + } + + return NNACL_OK; +} diff --git a/mindspore/lite/nnacl/infer/while_infer.h b/mindspore/lite/nnacl/infer/while_infer.h new file mode 100644 index 0000000000..10616d5b19 --- /dev/null +++ b/mindspore/lite/nnacl/infer/while_infer.h @@ -0,0 +1,31 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_WHILE_INFER_H +#define MINDSPORE_LITE_NNACL_WHILE_INFER_H + +#include "nnacl/infer/common_infer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int WhileInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, + OpParameter *parameter); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_WHILE_INFER_H diff --git a/mindspore/lite/nnacl/int8/layer_norm_int8.c b/mindspore/lite/nnacl/int8/layer_norm_int8.c index 4502ee2981..79448af47f 100644 --- a/mindspore/lite/nnacl/int8/layer_norm_int8.c +++ b/mindspore/lite/nnacl/int8/layer_norm_int8.c @@ -62,12 +62,10 @@ int LayerNormInt8(const int8_t *src_data, const float *gamma_data, const float * deno); } } else { - int x = i / param->norm_outer_size_; - const int8_t *src_param = src_norm + x * param->params_inner_size_; - int8_t *dst_param = dst_norm + x * param->params_inner_size_; - const float *gamma = gamma_data + x * param->params_inner_size_; - const float *beta = beta_data + x * param->params_inner_size_; - LayerNormGammaAndBetaInt8(dst_param, src_param, gamma, beta, quant, param->norm_inner_size_, mean, deno); + int x = i / param->params_outer_size_; + const float *gamma = gamma_data + x * param->norm_inner_size_; + const float *beta = beta_data + x * param->norm_inner_size_; + LayerNormGammaAndBetaInt8(dst_norm, src_norm, gamma, beta, quant, param->norm_inner_size_, mean, deno); } } return NNACL_OK; diff --git a/mindspore/lite/nnacl/int8/reduce_int8.c b/mindspore/lite/nnacl/int8/reduce_int8.c index a2fa720448..e40e45be40 100644 --- a/mindspore/lite/nnacl/int8/reduce_int8.c +++ b/mindspore/lite/nnacl/int8/reduce_int8.c @@ -137,10 +137,7 @@ int ReduceMeanHW(int n, int plane, int count, int c, int8_t *in_data, int8_t *ou quant_arg.multiplier_), quant_arg.right_shift_); mean += bias; - mean = MSMIN(mean, INT8_MAX); - mean = MSMAX(mean, INT8_MIN); - out_ptr[0] = mean; - out_ptr++; + *out_ptr++ = MSMAX(MSMIN(mean, INT8_MAX), INT8_MIN); } } return NNACL_OK; @@ -243,13 +240,7 @@ int ReduceMeanLastAxis(const int outer_size, const int inner_size, const int axi } mean = mean_scaled + quant->out_zp_; - if (mean > INT8_MAX) { - *inner_dst = INT8_MAX; - } else if (mean < INT8_MIN) { - *inner_dst = INT8_MIN; - } else { - *inner_dst = (int8_t)mean; - } + *inner_dst = MSMAX(MSMIN(mean, INT8_MAX), INT8_MIN); } } return NNACL_OK; diff --git a/mindspore/lite/nnacl/int8/splice_int8.c b/mindspore/lite/nnacl/int8/splice_int8.c new file mode 100644 index 0000000000..3ee891a25b --- /dev/null +++ b/mindspore/lite/nnacl/int8/splice_int8.c @@ -0,0 +1,30 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "nnacl/int8/splice_int8.h" +void SpliceInt8(const int8_t *src_data, int src_row, int src_col, const SpliceParameter *splice_parameter, + int8_t *dst_data, int dst_row, int dst_col) { + for (int r = 0; r < dst_row; ++r) { + for (int off = 0; off < splice_parameter->context_dim_; ++off) { + int r_off = r + splice_parameter->context_[off]; + r_off = MSMAX(r_off, 0); + r_off = MSMIN(r_off, src_row - 1); + const int8_t *tmp_src_data = src_data + r_off * src_col * sizeof(int8_t); + int8_t *tmp_dst_data = dst_data + r * dst_col * sizeof(int8_t); + memcpy(tmp_dst_data + off * src_col, tmp_src_data, src_col * sizeof(int8_t)); + } + } +} diff --git a/mindspore/lite/nnacl/int8/splice_int8.h b/mindspore/lite/nnacl/int8/splice_int8.h new file mode 100644 index 0000000000..326aea4a0d --- /dev/null +++ b/mindspore/lite/nnacl/int8/splice_int8.h @@ -0,0 +1,30 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_INT8_SPLICE_INT8_H_ +#define MINDSPORE_LITE_NNACL_INT8_SPLICE_INT8_H_ +#include +#include "nnacl/splice_parameter.h" +#ifdef __cplusplus +extern "C" { +#endif + +void SpliceInt8(const int8_t *src_data, int src_row, int src_col, const SpliceParameter *splice_parameter, + int8_t *dst_data, int dst_row, int dst_col); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_INT8_SPLICE_INT8_H_ diff --git a/mindspore/lite/nnacl/l2_norm_parameter.h b/mindspore/lite/nnacl/l2_norm_parameter.h index 615f8273d9..4343ef3f5e 100644 --- a/mindspore/lite/nnacl/l2_norm_parameter.h +++ b/mindspore/lite/nnacl/l2_norm_parameter.h @@ -17,7 +17,7 @@ #define MINDSPORE_LITE_NNACL_L2NORM_PARAMETER_H_ #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" typedef struct L2NormParameter { // Primitive parameter diff --git a/mindspore/lite/nnacl/layer_norm_parameter.h b/mindspore/lite/nnacl/layer_norm_parameter.h index 4b8b077e9d..928662d9d7 100644 --- a/mindspore/lite/nnacl/layer_norm_parameter.h +++ b/mindspore/lite/nnacl/layer_norm_parameter.h @@ -17,13 +17,15 @@ #define MINDSPORE_LITE_NNACL_LAYER_NORM_PARAMETER_H_ #include "nnacl/op_base.h" -#include "mindspore/lite/nnacl/int8/quantize.h" +#include "nnacl/int8/quantize.h" enum ElementwiseMode { ELEMENTWISE_NOT = 0, ELEMENTWISE_PER_CHANNEL = 1, ELEMENTWISE_PER_NUM = 2 }; typedef struct LayerNormParameter { // Primitive parameter OpParameter op_parameter_; float epsilon_; + enum ElementwiseMode elementwise_mode_; + bool elementwise_affine_; int begin_norm_axis_; int begin_params_axis_; // shape correlative diff --git a/mindspore/lite/nnacl/lstm_parameter.h b/mindspore/lite/nnacl/lstm_parameter.h index 8a04e3a81a..98d78727db 100644 --- a/mindspore/lite/nnacl/lstm_parameter.h +++ b/mindspore/lite/nnacl/lstm_parameter.h @@ -30,10 +30,8 @@ typedef struct LstmParameter { int input_step_; int output_step_; bool bidirectional_; - // smooth factor for hidden/cell state calculation: - // output_hidden = old_hidden * smooth + new_hidden * (1 - smooth) - // output_cell = old_cell * smooth + new_cell * (1 - smooth) - float smooth_; + float zoneout_cell_; + float zoneout_hidden_; int col_align_; int row_align_; } LstmParameter; diff --git a/mindspore/lite/nnacl/matmul_parameter.h b/mindspore/lite/nnacl/matmul_parameter.h index 4e6ff89aa1..8c1376043b 100644 --- a/mindspore/lite/nnacl/matmul_parameter.h +++ b/mindspore/lite/nnacl/matmul_parameter.h @@ -59,6 +59,8 @@ typedef struct MatMulParameter { bool a_const_; bool b_const_; ActType act_type_; + bool use_axis_; + int axis_; } MatMulParameter; typedef struct MatmulQuantParameter { diff --git a/mindspore/lite/nnacl/nnacl_common.h b/mindspore/lite/nnacl/nnacl_common.h index 365257c958..3e02fe8991 100644 --- a/mindspore/lite/nnacl/nnacl_common.h +++ b/mindspore/lite/nnacl/nnacl_common.h @@ -23,7 +23,7 @@ extern "C" { #endif -inline void ComputeStrides(const int *shape, int *strides, const int ndim) { +static inline void ComputeStrides(const int *shape, int *strides, const int ndim) { int stride = 1; for (int i = ndim - 1; i >= 0; i--) { strides[i] = stride; diff --git a/mindspore/lite/nnacl/op_base.h b/mindspore/lite/nnacl/op_base.h index 8ce19188b0..a6dadef202 100644 --- a/mindspore/lite/nnacl/op_base.h +++ b/mindspore/lite/nnacl/op_base.h @@ -59,6 +59,7 @@ #define kNHWC_C 3 #define kInputSize1 2 #define kInputSize2 3 +#define MAX_AXIS_SIZE 6 #define MAX_LEN 256 typedef enum LiteDataType { @@ -76,6 +77,7 @@ typedef enum DataOrder { typedef struct OpParameter { char name_[100]; + bool infer_flag_; int type_; int thread_num_; } OpParameter; @@ -92,7 +94,7 @@ typedef struct QuantMulArg { } QuantMulArg; typedef enum ActType { ActType_No, ActType_Relu, ActType_Sigmod, ActType_Relu6, ActType_Prelu } ActType; -typedef enum PadMode { Pad_No, Pad_Same, Pad_Valid } PadMode; +typedef enum PadMode { Pad_pad, Pad_same, Pad_valid } PadMode; typedef enum RoundingMode { Rounding_No, Rounding_Away_from_zero, Rounding_Up } RoundingMode; typedef enum CalFixedMultiplierMode { Method_No, diff --git a/mindspore/lite/nnacl/slice_parameter.h b/mindspore/lite/nnacl/slice_parameter.h index e535ed6371..d3627965e5 100644 --- a/mindspore/lite/nnacl/slice_parameter.h +++ b/mindspore/lite/nnacl/slice_parameter.h @@ -35,6 +35,7 @@ typedef struct SliceParameter { int32_t begin_[COMM_SHAPE_SIZE]; int32_t end_[COMM_SHAPE_SIZE]; int32_t size_[COMM_SHAPE_SIZE]; + int32_t axis_[COMM_SHAPE_SIZE]; // other parameter SliceQuantArg quant_arg_; diff --git a/mindspore/lite/nnacl/splice_parameter.h b/mindspore/lite/nnacl/splice_parameter.h new file mode 100644 index 0000000000..d9cc2a45e2 --- /dev/null +++ b/mindspore/lite/nnacl/splice_parameter.h @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_NNACL_SPLICE_PARAMETER_H_ +#define MINDSPORE_LITE_NNACL_SPLICE_PARAMETER_H_ +#include "nnacl/op_base.h" +typedef struct SpliceParameter { + OpParameter op_parameter_; + int context_dim_; + int forward_indexes_dim_; + int *context_; + int *forward_indexes_; + int output_dim_; +} SpliceParameter; +#endif // MINDSPORE_LITE_NNACL_SPLICE_PARAMETER_H_ diff --git a/mindspore/lite/nnacl/squeeze_parameter.h b/mindspore/lite/nnacl/squeeze_parameter.h index 44cadaae2c..77a419aa12 100644 --- a/mindspore/lite/nnacl/squeeze_parameter.h +++ b/mindspore/lite/nnacl/squeeze_parameter.h @@ -16,12 +16,31 @@ #ifndef MINDSPORE_LITE_NNACL_SQUEEZE_PARAMETER_H_ #define MINDSPORE_LITE_NNACL_SQUEEZE_PARAMETER_H_ - #include "nnacl/op_base.h" +#include "nnacl/int8/quantize.h" + +#define SQUEEZE_OFFSET_MAX_SIZE 4 typedef struct SqueezeQuantArg { QuantArg *in_quant_args_; QuantArg *out_quant_args_; } SqueezeQuantArg; +typedef struct SqueezeParameter { + // primitive parameter + OpParameter op_parameter_; + int axis_[8]; + size_t axis_size_; + + // shape correlative + const int *in_shape_; + const int *out_shape_; + int offset_size_; + int64_t offset_[SQUEEZE_OFFSET_MAX_SIZE]; + int64_t in_offset_[SQUEEZE_OFFSET_MAX_SIZE]; + int input_dim_; + // other parameter + SqueezeQuantArg quant_arg; +} SqueezeParameter; + #endif // MINDSPORE_LITE_NNACL_SQUEEZE_PARAMETER_H_ diff --git a/mindspore/lite/nnacl/strided_slice_parameter.h b/mindspore/lite/nnacl/strided_slice_parameter.h index 64384cf582..91fa2d6571 100644 --- a/mindspore/lite/nnacl/strided_slice_parameter.h +++ b/mindspore/lite/nnacl/strided_slice_parameter.h @@ -33,6 +33,11 @@ typedef struct StridedSliceParameter { // other parameter int num_axes_; LiteDataType data_type; + int begins_mask_; + int ends_mask_; + int ellipsisMask_; + int newAxisMask_; + int shrinkAxisMask_; } StridedSliceParameter; #endif // MINDSPORE_LITE_NNACL_STRIDED_SLICE_PARAMETER_H_ diff --git a/mindspore/lite/nnacl/tensor_c.h b/mindspore/lite/nnacl/tensor_c.h new file mode 100644 index 0000000000..7a3d44b3da --- /dev/null +++ b/mindspore/lite/nnacl/tensor_c.h @@ -0,0 +1,28 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_TENSOR_C_H_ +#define MINDSPORE_LITE_NNACL_TENSOR_C_H_ +#include "nnacl/op_base.h" + +typedef struct TensorC { + int data_type_; + int format_; + void *data_; + size_t shape_size_; + int shape_[MAX_SHAPE_SIZE]; +} TensorC; + +#endif // MINDSPORE_LITE_NNACL_TENSOR_C_H_ diff --git a/mindspore/lite/nnacl/tensorlist_parameter.h b/mindspore/lite/nnacl/tensorlist_parameter.h index 30a9a4e3b3..0cf8156913 100644 --- a/mindspore/lite/nnacl/tensorlist_parameter.h +++ b/mindspore/lite/nnacl/tensorlist_parameter.h @@ -18,13 +18,12 @@ #define MINDSPORE_LITE_NNACL_TENSORLIST_PARAMETER_H_ #include "nnacl/op_base.h" -#include "ir/dtype/type_id.h" typedef struct TensorListParameter { // primitive parameter OpParameter op_parameter_; - mindspore::TypeId shape_type_; - mindspore::TypeId element_dtype_; + int shape_type_; + int element_dtype_; // other parameter int num_element_; diff --git a/mindspore/lite/nnacl/transpose.h b/mindspore/lite/nnacl/transpose.h index 06087526b2..c22e6e70e1 100644 --- a/mindspore/lite/nnacl/transpose.h +++ b/mindspore/lite/nnacl/transpose.h @@ -25,6 +25,7 @@ typedef struct TransposeParameter { // primitive parameter OpParameter op_parameter_; int perm_[MAX_SHAPE_SIZE]; + size_t perm_size_; bool conjugate_; // shape correlative diff --git a/mindspore/lite/nnacl/unsqueeze_parameter.h b/mindspore/lite/nnacl/unsqueeze_parameter.h index 137dc2ac51..e543d27209 100644 --- a/mindspore/lite/nnacl/unsqueeze_parameter.h +++ b/mindspore/lite/nnacl/unsqueeze_parameter.h @@ -32,6 +32,7 @@ typedef struct UnSqueezeParameter { // primitive parameter OpParameter op_parameter_; int dims_[COMM_SHAPE_SIZE]; + int num_dim_; // shape correlative const int *in_shape_; diff --git a/mindspore/lite/schema/model.fbs b/mindspore/lite/schema/model.fbs index ef8906952a..7a4a14a0c9 100644 --- a/mindspore/lite/schema/model.fbs +++ b/mindspore/lite/schema/model.fbs @@ -19,7 +19,7 @@ include "ops.fbs"; namespace mindspore.schema; // This corresponds to the version. -file_identifier "MSL1"; +file_identifier "MSL2"; // File extension of any written files. file_extension "ms"; @@ -60,226 +60,6 @@ table Tensor { enableHuffmanCode: bool = false; } -union PrimitiveType { - Concat, - SoftMax, - Activation, - Conv2D, - FusedBatchNorm, - BatchNorm, - BiasAdd, - Pooling, - ROIPooling, - DepthwiseConv2D, - DeDepthwiseConv2D, - Resize, - DetectionPostProcess, - FullConnection, - Mean, // DEPRECATED - DeConv2D, - Scale, - Reshape, - Eltwise, - NetOutput, - Add, - Sub, - MatMul, - StridedSlice, - Power, - Slice, - Stack, - Mul, - RealDiv, - Pad, - Maximum, - Minimum, - PReLU, - LeakyReLU, - ArgMax, - ArgMin, - Exp, - Crop, - Range, - Rsqrt, - ExpandDims, - Tile, - Cast, - Shape, - Nchw2Nhwc, // DEPRECATED - Nhwc2Nchw, // DEPRECATED - QuantDTypeCast, - Split, - Permute, // DEPRECATED - FakeQuantWithMinMaxVars, - Equal, - Less, - Greater, - NotEqual, - LessEqual, - GreaterEqual, - Min, - Floor, - Abs, - Neg, - Cos, - Sin, - Sqrt, - Square, - Constant, - Log, - Tan, - Atan, - Asin, - Clip, - Transpose, - Squeeze, - Unsqueeze, - Upsample, - Dropout, - Broadcast, - BroadcastTo, - Lrn, - ZerosLike, - TopK, - SpaceToDepth, - SpaceToBatch, - SparseToDense, - ReverseSequence, - Rank, - Gather, - GatherNd, - Fill, - Elu, - DepthToSpace, - BatchToSpace, - AddN, - Ceil, - EmbeddingLookup, - EmbeddingLookupSparse, - FloorDiv, - FloorMod, - L2Norm, - LocalResponseNormalization, - MatrixDiag, - Reduce, - Reverse, - Round, - Select, - Scatter, - ScatterND, - ConstantOfShape, - Unique, - Unstack, - LogicalAnd, - LogicalOr, - LogicalXor, - LogicalNot, - OnnxInt8Quantize, - OnnxInt8Dequantize, - FakeQuantWithMinMax, - FakeQuantWithMinMaxPerChannel, - BatchNormFold, - MulFold, - AddFold, - SquaredDifference, - Flatten, - FlattenGrad, - TupleGetItem, - Div, - Where, - OneHot, - Lstm, - Conv2DGradFilter, - Conv2DGradInput, - PoolingGrad, - BNGrad, - Assign, - ApplyMomentum, - BiasGrad, - SoftmaxCrossEntropy, - AddGrad, - SubGrad, - MulGrad, - DivGrad, - PowerGrad, - ActivationGrad, - PriorBox, - SpaceToBatchND, - Depend, - Return, - MakeTuple, - ToFormat, - Proposal, - Custom, - BlackBox, - NegGrad, - LogGrad, - BatchToSpaceND, - LshProjection, - HashtableLookup, - SkipGram, - DeConv2DGradFilter, - CustomPredict, - CustomNormalize, - CustomExtractFeatures, - AudioSpectrogram, - Mfcc, - Rfft, - FftReal, - FftImag, - Sgd, - Adam, - GroupConv2DGradInput, - Loop, - NonMaxSuppression, - InstanceNorm, - Identity, - LayerNorm, - While, - ControlDepend, - UnsortedSegmentSum, - AssignAdd, - OnesLike, - BinaryCrossEntropyGrad, - BinaryCrossEntropy, - LpNormalization, - DropoutGrad, - MaximumGrad, - MinimumGrad, - Switch, - Partial, - TensorListFromTensor, - TensorListStack, - TensorListGetItem, - TensorListSetItem, - TensorListReserve, - All, - Assert, - Adder, - SparseSoftmaxCrossEntropy, - SmoothL1Loss, - SmoothL1LossGrad, - SigmoidCrossEntropyWithLogits, - SigmoidCrossEntropyWithLogitsGrad, - Reciprocal, - Merge, - Mod, - If, - GeLU, - Gru, - NonZero, - InvertPermutation, - Size, - RandomStandardNormal, - CropAndResize, - Erf, - StridedSliceGrad, - IsFinite, - LinSpace, - UniformReal, - AbsGrad -} - enum QuantType: int { QUANT_NONE, AwareTraining, diff --git a/mindspore/lite/schema/model_v0.fbs b/mindspore/lite/schema/model_v0.fbs index 9ac325cf35..2c7b0dda67 100644 --- a/mindspore/lite/schema/model_v0.fbs +++ b/mindspore/lite/schema/model_v0.fbs @@ -35,6 +35,8 @@ table QuantParam { varCorr: float = 1; meanCorr: float = 0; dstDtype: int = 32; + roundType: int = 1; + multiplier: int = 1; // calculate fixed point multiplier method } table Tensor { @@ -256,10 +258,6 @@ union PrimitiveType { Reciprocal, Merge, Mod, - If, - GeLU, - Gru, - NonZero, } enum QuantType: int { diff --git a/mindspore/lite/schema/ops.fbs b/mindspore/lite/schema/ops.fbs index ff3c43eab4..ad77ac2526 100644 --- a/mindspore/lite/schema/ops.fbs +++ b/mindspore/lite/schema/ops.fbs @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,1234 +13,1014 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +include "ops_types.fbs"; namespace mindspore.schema; -enum ResizeMethod: byte { - UNKNOWN = -1, - LINEAR = 0, - NEAREST = 1, - CUBIC = 2 -} - -enum CoordinateTransformMode: byte { - COMMON = 0, - HALF_PIXEL = 1, - PYTORCH_HALF_PIXEL = 2, - TF_HALF_PIXEL = 3, - TF_CROP_AND_RESIZE = 4, - ALIGN_CORNERS = 5, - ASYMMETRIC = 6, - ALIGN_CORNERS_WITH_HALF_PIEXL = 7 -} - -enum NearestMode : byte { - NORMAL = 0, - ROUND_HALF_DOWN = 1, - ROUND_HALF_UP = 2, - FLOOR = 3, - CEIL = 4 -} - -enum Format : int { - NCHW = 0, - NHWC, - NHWC4, - HWKC, - HWCK, - KCHW, - CKHW, - KHWC, - CHWK, - HW, - HW4, - NC, - NC4, - NC4HW4 = 100, - NUM_OF_FORMAT -} - -enum ActivationType : byte { - NO_ACTIVATION = 0, - RELU = 1, - SIGMOID = 2, - RELU6 = 3, - ELU = 4, - LEAKY_RELU = 5, - ABS = 6, - RELU1 = 7, - SOFTSIGN = 8, - SOFTPLUS = 9, - TANH = 10, - SELU = 11, - HSWISH = 12, - HSIGMOID = 13, - THRESHOLDRELU = 14, - LINEAR = 15, - HARD_TANH = 16, - SIGN = 17, - SWISH = 18, - UNKNOWN = 19 -} -enum ActivationGradType : byte { - NO_ACTIVATION = 0, - RELU = 1, - SIGMOID = 2, - RELU6 = 3, - ELU = 4, - LEAKY_RELU = 5, - ABS = 6, - RELU1 = 7, - SOFTSIGN = 8, - SOFTPLUS = 9, - TANH = 10, - SELU = 11, - HSWISH = 12, - HSIGMOID = 13, - THRESHOLDRELU = 14, - LINEAR = 15, - UNKNOWN = 16, - LOG = 17 -} -enum ReduceType : byte { - REDUCE_MAX = 0, - REDUCE_MEAN = 1, - REDUCE_ALL = 2, - REDUCE_ANY = 3, - REDUCE_LOG_SUM_EXP = 4, - REDUCE_PROD = 5, - REDUCE_SUM = 6, - UNKNOWN = 7 -} - -enum PoolMode : byte { - MAX_POOLING = 0, - MEAN_POOLING = 1, -} - -enum EltwiseMode : byte { - PROD = 0, - SUM = 1, - MAXIMUM = 2, - UNKNOWN = 3 -} - -enum PadMode : byte { - NOTSET = 0, - SAME_UPPER = 1, - VALID = 2, - CAFFE = 4, - SAME_LOWER = 5 -} - -enum RoundMode : byte { - FLOOR = 0, - CEIL = 1 -} - -enum PaddingMode : byte { - CONSTANT = 0, - REFLECT = 1, - SYMMETRIC = 2, - MODE_RESERVED = 3 -} - -enum LshProjectionType : byte { - UNKNOWN = 0, - SPARSE = 1, - DENSE = 2 -} - -table Pad { - paddings: [int]; - paddingMode: PaddingMode; - constantValue: float; +union PrimitiveType { + Abs, + Activation, + ActivationGrad, + Adam, + AddFusion, + AdderFusion, + AddGrad, + AddN, + All, + ApplyMomentum, + ArgMaxFusion, + ArgMinFusion, + Assert, + Assign, + AssignAdd, + AudioSpectrogram, + AvgPoolFusion, + AvgPoolGrad, + BatchNorm, + BatchNormGrad, + BatchToSpace, + BatchToSpaceND, + BiasAdd, + BinaryCrossEntropy, + BinaryCrossEntropyGrad, + BiasAddGrad, + BroadcastTo, + Cast, + Ceil, + Clip, + Concat, + ControlDepend, + Conv2DBackpropFilterFusion, + Conv2DBackpropInputFusion, + Conv2DFusion, + Conv2dTransposeFusion, + Cos, + ConstantOfShape, + Crop, + CustomExtractFeatures, + CustomNormalize, + CustomPredict, + DeConv2DGradFilter, + Depend, + DepthToSpace, + DetectionPostProcess, + DivFusion, + DivGrad, + Dropout, + DropoutGrad, + Elu, + Eltwise, + Equal, + EmbeddingLookupFusion, + ExpFusion, + ExpandDims, + FakeQuantWithMinMaxVars, + FakeQuantWithMinMaxVarsPerChannel, + FftReal, + FftImag, + Flatten, + FlattenGrad, + Floor, + FloorDiv, + FloorMod, + Fill, + FullConnection, + FusedBatchNorm, + Gather, + GatherNd, + Greater, + GreaterEqual, + HashtableLookup, + InstanceNorm, + LayerNormFusion, + LeakyRelu, + Less, + LessEqual, + Log, + LogGrad, + LogicalAnd, + LogicalNot, + LogicalOr, + LpNormalization, + LRN, + LshProjection, + LSTM, + L2NormalizeFusion, + MatMul, + Maximum, + MaximumGrad, + MaxPoolFusion, + MaxPoolGrad, + Merge, + Mfcc, + Minimum, + MinimumGrad, + Mod, + MulFusion, + MulGrad, + Neg, + NegGrad, + NotEqual, + NonMaxSuppression, + OneHot, + OnesLike, + PadFusion, + PartialFusion, + PowerGrad, + PowFusion, + PriorBox, + PReLUFusion, + QuantDTypeCast, + Rank, + Range, + Reciprocal, + RealDiv, + ReduceFusion, + Reshape, + Resize, + ReverseSequence, + ReverseV2, + Rfft, + ROIPooling, + Round, + Rsqrt, + ScaleFusion, + ScatterNd, + SGD, + Shape, + SigmoidCrossEntropyWithLogits, + SigmoidCrossEntropyWithLogitsGrad, + Sin, + SkipGram, + SliceFusion, + SmoothL1Loss, + SmoothL1LossGrad, + Softmax, + SoftmaxCrossEntropyWithLogits, + SpaceToBatch, + SpaceToBatchND, + SpaceToDepth, + SparseSoftmaxCrossEntropy, + SparseToDense, + Split, + Sqrt, + Squeeze, + Square, + SquaredDifference, + Stack, + StridedSlice, + SubFusion, + SubGrad, + Switch, + TensorListFromTensor, + TensorListGetItem, + TensorListReserve, + TensorListSetItem, + TensorListStack, + TileFusion, + TopKFusion, + Transpose, + Unique, + UnsortedSegmentSum, + Unsqueeze, + Unstack, + While, + Where, + ZerosLike, + Select, + If, + GRU, + NonZero, + InvertPermutation, + Size, + RandomStandardNormal, + CropAndResize, + Erf, + StridedSliceGrad, + IsFinite, + LinSpace, + UniformReal, + AbsGrad, } -table Maximum { -} - -table Minimum { -} - -table Flatten { -} -table FlattenGrad { -} -table Concat { - axis: int; - n: int; // DEPRECATED -} - -table SoftMax { - axis: int = -1; +table Abs { } table Activation { - type: ActivationType = 0; - alpha: float = 0.2; - min_val: float = -1.0; - max_val: float = 1.0; -} -table ActivationGrad { - type: ActivationType = 0; - alpha: float = 0.2; -} - - -table Conv2D { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - activationType: ActivationType = 0; -} - -table Adder { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table Conv2DGradFilter { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - filter_shape: [int]; // DEPRECATED - activationType: ActivationType = 0; -} - -table Conv2DGradInput { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - input_shape: [int]; // DEPRECATED - activationType: ActivationType = 0; -} - -table GroupConv2DGradInput { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - input_shape: [int]; - activationType: ActivationType = 0; -} - -table FusedBatchNorm { - epsilon: float = 0.00001; // eg. epsilon=0.001 - momentum: float = 0.9; - spatial: int = 1; -} - -table BatchNorm { - epsilon: float = 0.00001; // eg. epsilon=0.001 -} - -table BiasGrad { + activation_type: ActivationType = 0; + alpha: float; + min_val: float; + max_val: float; } - -table SoftmaxCrossEntropy { +table ActivationGrad { + activation_type: ActivationType; + alpha: float; } -table SparseSoftmaxCrossEntropy { - isGrad: bool; +table Adam { + use_locking: bool; + use_nesterov: bool; } -table make_tuple { +table AddFusion { + activation_type: ActivationType = 0; } - -table PoolingGrad { +table AdderFusion { format: Format = 0; - poolingMode: PoolMode; - global: bool = false; - windowW: int; - windowH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - roundMode: RoundMode; -} -table Shape { -} - -table ConstantOfShape{ - dataType: int; - value: [float]; -} - -table Nchw2Nhwc { // DEPRECATED - -} - -table Nhwc2Nchw { // DEPRECATED - + kernel_size: [long]; + stride: [long]; + dilation: [long]; + pad_mode: PadMode; + pad_list: [long]; + group: long; + in_channel: long; + out_channel: long; + activation_type: ActivationType = 0; } -table FakeQuantWithMinMaxVars { - narrowRange: bool; - numBits: int; -} - -table BiasAdd { - axis: [int]; // DEPRECATED +table AddGrad { } -table ROIPooling { - pooledH: int; - pooledW: int; - scale: float; +table AddN { } -table Pooling { - format: Format = 0; - poolingMode: PoolMode; - global: bool = false; - windowW: int; - windowH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - roundMode: RoundMode; - activationType: ActivationType = 0; - avgMode: int = 0; -} - -table DepthwiseConv2D { - format: Format = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - activationType: ActivationType = 0; -} - -table DeDepthwiseConv2D { - format: Format = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - activationType: ActivationType = 0; +table All { + keep_dims: long; } - -table Resize { - format: Format = 0; - method: ResizeMethod; - newHeight: long; - newWidth: long; - alignCorners: bool = false; // DEPRECATED IN FUTURE: use 'coordinateTransformMode' instead. - preserveAspectRatio: bool = false; - coordinateTransformMode : CoordinateTransformMode; - cubicCoeff : float; - excludeOutside : int; - extrapolationValue : float = 0; - nearestMode : NearestMode; +table ApplyMomentum { + use_nesterov: bool; + use_locking: bool; + gradient_scale: float; } -table DetectionPostProcess { - format: Format = 0; - inputSize: int; - hScale: float; - wScale: float; - xScale: float; - yScale: float; - NmsIouThreshold: float; - NmsScoreThreshold: float; - MaxDetections: long; - DetectionsPerClass: long; - MaxClassesPerDetection: long; - NumClasses: long; - UseRegularNms: bool; - OutQuantized: bool; +table ArgMaxFusion { + axis: long; + top_k: long = 1; + keep_dims: bool; + out_max_value: bool; } -table FullConnection { - hasBias: bool; - axis: int; - useAxis: bool; - activationType: ActivationType = 0; +table ArgMinFusion { + axis: long; + top_k: long; + keep_dims: bool; + out_max_value: bool; } -// Mean(input_tensor, axis, keep_dims) -table Mean { // DEPRECATED - axis: [int]; - keepDims: bool = false; +table Assert { + summarize: long; } -table DeConv2D { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - activationType: ActivationType = 0; - outputPaddingW: int; - outputPaddingH: int; +table Assign { } -table DeConv2DGradFilter { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; // DEPRECATED - activationType: ActivationType = 0; -} - -table BNGrad { - eps: float; - momentum: float; -} - -table Scale { - axis: int; - activationType: ActivationType = 0; +table AssignAdd { } -table Eltwise { - mode: EltwiseMode; +table AudioSpectrogram { + window_size: long; + stride: long; + mag_square: bool; } -table Add { - activationType: ActivationType = 0; +table AvgPoolFusion { + kernel_size: [long]; + strides: [long]; + pad: [long]; + pad_mode: PadMode; + round_mode: RoundMode; + format: Format; + global: bool; + activation_type: ActivationType = 0; } -table Sub { - activationType: ActivationType = 0; +table AvgPoolGrad { + kernel_size: [long]; + strides: [long]; + pad_mode: PadMode; + format: Format; } -table Mul { - activationType: ActivationType = 0; +table BatchNorm { + epsilon: float; + format: Format; + is_training: bool; } -table Div { - activationType: ActivationType = 0; +table BatchNormGrad { + epsilon: float; } -table AddGrad { +table BatchToSpace { + block_size: [long]; + crops: Vec2D; } -table SubGrad { +table BatchToSpaceND { + block_shape: [long]; + crops: Vec2D; } -table MulGrad { +table BiasAdd { + format: Format; } -table DivGrad { -} -table RealDiv { +table BinaryCrossEntropy { + reduction: Reduction; } -table Rsqrt { +table BinaryCrossEntropyGrad { + reduction: Reduction = 1; } -table Equal { +table BiasAddGrad { } -table Less { +table BroadcastTo { + shape: [long]; } -table Greater { +table Cast { } -table NotEqual { +table Ceil { } -table LessEqual { +table Clip { + max: float; + min: float; } -table GreaterEqual { +table Concat { + axis: long; } -table Min { +table ControlDepend { + depend_mode: long; } -table Slice { +table Conv2DBackpropFilterFusion { format: Format = 0; - axes: [int]; - begin: [int]; - size: [int]; -} - -table Floor { -} - -table Abs { -} - -table Neg { -} - -table NegGrad { -} - -table Exp { - base : float = -1.0; - scale : float = 1.0; - shift : float = 0.0; + kernel_size: [long]; + stride: [long]; + dilation: [long]; + pad_mode: PadMode; + pad_list: [long]; + mode: long; + group: long; + in_channel: long; + out_channel: long; + activation_type: ActivationType = 0; +} + +table Conv2DBackpropInputFusion { + format: Format = 0; + kernel_size: [long]; + stride: [long]; + dilation: [long]; + pad_mode: PadMode; + pad: [long]; + pad_list: [long]; + mode: long; + group: long; + in_channel: long; + out_channel: long; + activation_type: ActivationType = 0; +} + +table Conv2DFusion { + format: Format = 0; + kernel_size: [long]; + stride: [long]; + dilation: [long]; + pad_mode: PadMode; + pad_list: [long]; + mode: long; + group: long; + in_channel: long; + out_channel: long; + activation_type: ActivationType = 0; +} + +table Conv2dTransposeFusion { + format: Format = 0; + kernel_size: [long]; + stride: [long]; + dilation: [long]; + pad_mode: PadMode; + pad: [long]; + pad_list: [long]; + mode: long; + group: long; + in_channel: long; + out_channel: long; + activation_type: ActivationType = 0; } table Cos { } -table Sin { -} - -table Sqrt { +table ConstantOfShape { + data_type: long; + value: [float]; } -table Square { +table Crop { + axis: long; + offsets: [long]; } -table Ceil { +table CustomExtractFeatures { } -table Log { +table CustomNormalize { } -table LogGrad { +table CustomPredict { + output_num: long; + weight_threshold: float; } -table Tan { +table DeConv2DGradFilter { + in_channel: long; + out_channel: long; + kernel_size: [long]; + pad_mode: PadMode; + pad_list: [long]; + stride: [long]; + dilation: [long]; + group: long; + format: Format; + activation_type: ActivationType; } -table Atan { +table Depend { } -table Asin { +table DepthToSpace { + block_size: long; + format: Format = 0; } -table Reshape { +table DetectionPostProcess { format: Format = 0; - shape: [long]; + input_size: long; + scale: [float]; + nms_iou_threshold: float; + nms_score_threshold: float; + max_detections: long; + detections_per_class: long; + max_classes_per_detection: long; + num_classes: long; + use_regular_nms: bool; + out_quantized: bool; } -table Power { - power: float; - scale: float; - shift: float; -} -table PowerGrad { - power: float; - scale: float; - shift: float; -} -table ArgMax { - axis: int; - outMaxValue: bool; - topK: int = 1; - keepDims: bool; - axisType: int; +table DivFusion { + activation_type: ActivationType = 0; } -table ArgMin { - axis: int; - outMaxValue: bool; - topK: int = 1; - keepDims: bool; - axisType: int; +table DivGrad { } -table NetOutput { +table Dropout { + keep_prob: float = 0.5; } -table MatMul { - broadcast : bool = false; // DEPRECATED - transposeA : bool = false; - transposeB : bool = false; +table DropoutGrad { + keep_prob: float; } -table PReLU { - channelShared : bool = false; - slope: [float]; +table Elu { + alpha: float; } -table LeakyReLU { - negativeSlope: float; +table Eltwise { + mode: EltwiseMode; } -table StridedSlice { - beginMask: int; - endMask: int; - ellipsisMask: int; - newAxisMask: int; - shrinkAxisMask: int; - begin: [int]; - end: [int]; - stride: [int]; - isScale: [int]; +table Equal { } -table Stack { - axis: int; - n: int; - isScale: [int]; +table EmbeddingLookupFusion { + max_norm: float; } -table Range { - dType: int; - start: int; - limit: int; - delta: int = 1; +table ExpFusion { + base: float = -1; + scale: float = 1.0; + shift: float = 0.0; } table ExpandDims { - dim: int; -} - -table Tile { - multiples: [int]; - dims: [int]; } -table Cast { - srcT: int; - dstT: int; -} - -table QuantDTypeCast { - srcT: int; - dstT: int; -} - -table Split { - numberSplit: int; - sizeSplits: [int]; - splitDim: int; -} - -table Crop { - axis : long; - offsets : [long]; -} - -table Permute { // DEPRECATED - order: [long]; -} - -table Clip { - max: float; - min: float; +table FakeQuantWithMinMaxVars { + num_bits: long; + narrow_range: bool; } -table Constant { +table FakeQuantWithMinMaxVarsPerChannel { + num_bits: long; + narrow_range: bool; } - -table Elu { - alpha: float = 1.0; +table FftReal { } -table Broadcast { +table FftImag { } -table BroadcastTo { - dst_shape: [int]; +table Flatten { } -table Lrn { - alpha: float = 0.0001; - beta: float = 0.75; - bias: float = 1.0; - size: int; +table FlattenGrad { } -enum ReduceMode : byte { - ReduceMean = 0, - ReduceMax = 1, - ReduceMin = 2, - ReduceProd = 3, - ReduceSum = 4, - ReduceSumSquare = 5, - ReduceASum = 6, - ReduceAll = 7 +table Floor { } -table Reduce { - axes: [int]; - keepDims: int; - mode: ReduceMode; - reduceToEnd: bool = false; - coeff: float = 1.0; +table FloorDiv { } -table Transpose { - perm: [int]; - conjugate: bool = false; // DEPRECATED +table FloorMod { } -table Squeeze { - axis: [int]; +table Fill { } -table Unsqueeze { - axis: [int]; +table FullConnection { + has_bias: bool; + use_axis: bool; + axis: long; + activation_type: ActivationType = 0; } -table Upsample { - mode: string; - scales: [float]; +table FusedBatchNorm { + epsilon: float = 0.0001; + momentum: float = 0.9; + mode: long; } -table Dropout { - ratio : float = 0.5; +table Gather { } -table LocalResponseNormalization { - depth_radius: int; - bias: float; - alpha: float; - beta: float; +table GatherNd { } -table ZerosLike { +table Greater { } -table TopK { - k : int; - sorted : bool = true; +table GreaterEqual { } -table SpaceToDepth { - blockSize : int; - format: Format = 0; +table HashtableLookup { } -table SpaceToBatch { - blockShape : [int]; - paddings : [int]; +table InstanceNorm { + epsilon: float; } -table SparseToDense { - validateIndices: bool; +table LayerNormFusion { + begin_norm_axis: long; + epsilon: float = 0.00001; + elementwise_affine: bool; + begin_params_axis: long; } -table ReverseSequence { - seqAxis: int; - batchAxis: int; +table LeakyRelu { + negative_slope: float; } -table Rank { +table Less { } - -table Gather { - axis: int; - batchDims: int; +table LessEqual { } -table GatherNd { - batchDims: int; // DEPRECATED +table Log { } -table Fill { - dims: [int]; +table LogGrad { } -table DepthToSpace { - blockSize: int; - format: Format = 0; +table LogicalAnd { } - -table BatchToSpace { - blockShape: [int]; - crops: [int]; +table LogicalNot { } -table BatchToSpaceND { - blockShape: [int]; - crops: [int]; +table LogicalOr { } -table AddN { - N: int; // DEPRECATED +table LpNormalization { + axis: long; + p: long; } - -table EmbeddingLookup { - maxNorm: float = 0.0; +table LRN { + depth_radius: long; + bias: float; + alpha: float; + beta: float; + norm_region: string; } -table EmbeddingLookupSparse { - spIds: [int]; - spWeights: [float]; - //combiner: Combiner=0; - maxNortm: float; +table LshProjection { + type: LshProjectionType; } -table FloorDiv { +table LSTM { + bidirectional: bool; + has_bias: bool; + input_size: long; + hidden_size: long; + num_layers: long; + num_directions: long; + dropout: float; + zoneout_cell: float = 0; + zoneout_hidden: float = 0; } -table FloorMod { +table L2NormalizeFusion { + axis: [long]; + epsilon: float; + activation_type: ActivationType = 0; } -table Mod { +table MatMul { + transpose_a: bool = false; + transpose_b: bool = false; } -table L2Norm { - axis: [int]; - epsilon: float; - activationType: ActivationType = 0; +table Maximum { } -table LogicalAnd { +table MaximumGrad { + grad_x: bool; + grad_y: bool; } -table LogicalOr { +table MaxPoolFusion { + kernel_size: [long]; + strides: [long]; + pad: [long]; + pad_mode: PadMode; + round_mode: RoundMode; + format: Format; + global: bool; + activation_type: ActivationType = 0; } -table LogicalXor { +table MaxPoolGrad { + kernel_size: [long]; + strides: [long]; + pad_mode: PadMode; + format: Format; } -table LogicalNot { +table Merge { } -table MatrixDiag { - k: int; - numRows: int; - numCols: int; - paddingValue: float; +table Mfcc { + freq_upper_limit: float; + freq_lower_limit: float; + filter_bank_channel_num: long; + dct_coeff_num: long; } -table Select { +table Minimum { } -table TfReduce { - type: ReduceType = 7; +table MinimumGrad { + grad_x: bool; + grad_y: bool; } -table Reverse { - axis: [int]; +table Mod { } -table Round { +table MulFusion { + activation_type: ActivationType = 0; } -table Scatter { +table MulGrad { } -table ScatterND { +table Neg { } -table Unique { - outType: int; // DEPRECATED +table NegGrad { } -table Unstack { - num: int; // deprecated - axis: int; +table NotEqual { } -table OnnxInt8Quantize { +table NonMaxSuppression { + center_point_box: long; } -table OnnxInt8Dequantize { +table OneHot { + axis: long; } -table FakeQuantWithMinMax { +table OnesLike { } -table FakeQuantWithMinMaxPerChannel { +table PadFusion { + paddings: Vec2D; + padding_mode: PaddingMode; + constant_value: float; } -table BatchNormFold { +table PartialFusion { + sub_graph_index: long; } -table MulFold { +table PowerGrad { + power: float; + scale: float; + shift: float; } -table AddFold { +table PowFusion { + scale: float = 1; + shift: float = 0; } -table SquaredDifference { +table PriorBox { + min_sizes: [long]; + max_sizes: [long]; + aspect_ratios: [float]; + variances: [float]; + image_size_w: long; + image_size_h: long; + step_w: float; + step_h: float; + clip: bool; + flip: bool; + offset: float; } -table TupleGetItem { +table PReLUFusion { + channel_shared: bool; } -table ApplyMomentum { - gradientScale: float; - useNesterov: bool; +table Rank { } -table Sgd { - weightDecay: float; - dampening: float; - useNesterov: bool; +table Range { + d_type: long; + start: long; + limit: long; + delta: long = 1; } -table Adam { - useNesterov: bool; +table Reciprocal { } -table Assign { +table RealDiv { } -table AssignAdd { +table ReduceFusion { + keep_dims: bool; + mode: ReduceMode; + reduce_to_end: bool; + coeff: float; } -table Where{ - condition: [bool]; +table Reshape { } -table OneHot { - axis: int; +table Resize { + format: Format = 0; + method: ResizeMethod; + new_height: long; + new_width: long; + preserve_aspect_ratio: bool = false; + coordinate_transform_mode: CoordinateTransformMode; + cubic_coeff: float; + exclude_outside: long; + extrapolation_value: float; + nearest_mode: NearestMode; } -table Lstm{ - bidirection: bool = false; - smooth: float = 0.0; +table ReverseSequence { + seq_dim: long; + batch_dim: long; } -table Gru{ - bidirection: bool = false; +table ReverseV2 { + axis: [long]; } -table PriorBox { - min_sizes: [int]; - max_sizes: [int]; - aspect_ratios: [float]; - variances: [float]; - image_size_w: int; - image_size_h: int; - step_w: float; - step_h: float; - clip: bool = true; - flip: bool = true; - offset: float; +table Rfft { + fft_length: long; } -table SpaceToBatchND { - blockShape : [int]; - paddings : [int]; +table ROIPooling { + pooled_h: long; + pooled_w: long; + scale: float; } -table MakeTuple { +table Round { } -table ToFormat { - srcT: int; - dstT: int; +table Rsqrt { } - -table Depend { +table QuantDTypeCast { + src_t: long; + dst_t: long; } -table ControlDepend { +table ScaleFusion { + axis: long; + activation_type: ActivationType = 0; } -table Return { +table ScatterNd { } -table Proposal { - feat_stride : float; - base_size : float; - min_size : float; - ratio : [float]; - scale : [float]; - pre_nms_topn : int; - post_nms_topn : int; - nms_thresh : float; +table SGD { + nesterov: bool; + dampening: float; + weight_decay: float; } -table Custom { - custom : [ubyte]; +table Shape { } - -table BlackBox { - id : string; - size : int; - address : [ubyte]; +table SigmoidCrossEntropyWithLogits { } -table LshProjection { - type : LshProjectionType; +table SigmoidCrossEntropyWithLogitsGrad { } -table HashtableLookup { +table Sin { } table SkipGram { - includeAllGrams : bool; - maxSkipSize : int; - ngramSize : int; + include_all_grams: bool; + max_skip_size: long; + ngram_size: long; } -table CustomPredict { - outputNum : int; - weightThreshold : float; +table SliceFusion { + axes: [long]; } -table CustomNormalize { -} - -table CustomExtractFeatures { +table SmoothL1Loss { + beta: float; } -table AudioSpectrogram { - windowSize : int; - stride : int; - magSquare : bool; +table SmoothL1LossGrad { + beta: float; } -table Mfcc { - freqUpperLimit : float; - freqLowerLimit : float; - filterBankChannelNum : int; - dctCoeffNum : int; +table Softmax { + axis: [long]; } -table Rfft { - fftLength : int; +table SoftmaxCrossEntropyWithLogits { } -table FftReal { +table SpaceToBatch { + block_size: [long]; + paddings: Vec2D; } -table FftImag { +table SpaceToBatchND { + block_shape: [long]; + paddings: Vec2D; } -table DropoutGrad { - ratio : float = 0.5; +table SpaceToDepth { + block_size: long; + format: Format; } -table MaximumGrad { +table SparseSoftmaxCrossEntropy { + grad: bool; } -table MinimumGrad { +table SparseToDense { } -table NonMaxSuppression { - centerPointBox : int = 0; +table Split { + output_num: long; + size_splits: [long]; + axis: long; } -table InstanceNorm { - epsilon : float = 0.00001; +table Sqrt { } -table Loop { - subGraphIndex : int; +table Squeeze { + axis: [long]; } -table Identity { +table Square { } -table LayerNorm { - begin_norm_axis : int; - begin_params_axis : int; - epsilon : float = 0.00001; +table SquaredDifference { } -table While { - condSubgraphIndex : int; - bodySubgraphIndex : int; +table Stack { + axis: long; } -table If { +table StridedSlice { + begin_mask: long; + end_mask: long; + ellipsis_mask: long; + new_axis_mask: long; + shrink_axis_mask: long; } -table UnsortedSegmentSum { - numSegments : int; +table SubFusion { + activation_type: ActivationType = 0; } -table OnesLike { - +table SubGrad { } -table BinaryCrossEntropy { - reduction : int = 1; +table Switch { } -table BinaryCrossEntropyGrad { - reduction : int = 1; +table TensorListFromTensor { + element_dtype: long; + shape_type: long; } -table LpNormalization { - axis : int; - p : int; +table TensorListGetItem { + element_dtype: long; } -table Switch { +table TensorListReserve { + element_dtype: long; + shape_type: long; } -table Partial { - subGraphIndex : int; +table TensorListSetItem { + element_dtype: long; } -table TensorListFromTensor { - elementDType : int; - shapeType : int; +table TensorListStack { + num_elements: long; + element_dtype: long; } -table TensorListStack { - numElements : int; - elementDType : int; +table TileFusion { + dims: [long]; } -table TensorListGetItem { - elementDType : int; +table TopKFusion { + sorted: bool = true; + axis: long; + largest: long; } -table TensorListSetItem { - elementDType : int; +table Transpose { } -table TensorListReserve { - elementDType : int; - shapeType : int; +table Unique { } -table All { - keepDims : int; +table UnsortedSegmentSum { } -table Assert { - summarize : int; +table Unsqueeze { + axis: [long]; } -table SmoothL1Loss { - beta : float; +table Unstack { + axis: long = 0; } -table SmoothL1LossGrad { - beta : float; +table While { + cond_subgraph_index: long; + body_subgraph_index: long; } -table SigmoidCrossEntropyWithLogits { +table Where { } -table SigmoidCrossEntropyWithLogitsGrad { +table ZerosLike { } -table Reciprocal { +table Select { } -table Merge { +table If { } -table GeLU { - approximate : bool = false; +table GRU { + bidirectional: bool = false; } table NonZero { @@ -1260,14 +1040,14 @@ table RandomStandardNormal { table CropAndResize { method : ResizeMethod; extrapolation_value : float; -} +} table StridedSliceGrad { - beginMask: int; - endMask: int; - ellipsisMask: int; - newAxisMask: int; - shrinkAxisMask: int; + begin_mask: int; + end_mask: int; + ellipsis_mask: int; + new_axis_mask: int; + shrink_axis_mask: int; begin: [int]; end: [int]; stride: [int]; @@ -1288,5 +1068,4 @@ table UniformReal { seed2 : int; } table AbsGrad { - transpose_a :bool; -} \ No newline at end of file +} diff --git a/mindspore/lite/schema/ops_types.fbs b/mindspore/lite/schema/ops_types.fbs new file mode 100644 index 0000000000..8d642dbb28 --- /dev/null +++ b/mindspore/lite/schema/ops_types.fbs @@ -0,0 +1,141 @@ +/** + * Copyright 2019 Huawei Technologies Co., Ltd + * + * 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. + */ + +namespace mindspore.schema; + +enum ResizeMethod: byte { + UNKNOWN = -1, + LINEAR = 0, + NEAREST = 1, + CUBIC = 2 +} + +enum CoordinateTransformMode: byte { + ASYMMETRIC = 0, + ALIGN_CORNERS = 1, + HALF_PIXEL = 2 +} + +enum NearestMode : byte { + NORMAL = 0, + ROUND_HALF_DOWN = 1, + ROUND_HALF_UP = 2, + FLOOR = 3, + CEIL = 4 +} + +enum Format : int { + NCHW = 0, + NHWC, + NHWC4, + HWKC, + HWCK, + KCHW, + CKHW, + KHWC, + CHWK, + HW, + HW4, + NC, + NC4, + NC4HW4, + NUM_OF_FORMAT +} + +enum ActivationType : byte { + NO_ACTIVATION = 0, + RELU = 1, + SIGMOID = 2, + RELU6 = 3, + ELU = 4, + LEAKY_RELU = 5, + ABS = 6, + RELU1 = 7, + SOFTSIGN = 8, + SOFTPLUS = 9, + TANH = 10, + SELU = 11, + HSWISH = 12, + HSIGMOID = 13, + THRESHOLDRELU = 14, + LINEAR = 15, + HARD_TANH = 16, + SIGN = 17, + SWISH = 18, + GELU = 19, + UNKNOWN = 20 +} + +enum ReduceMode : byte { + ReduceMean = 0, + ReduceMax = 1, + ReduceMin = 2, + ReduceProd = 3, + ReduceSum = 4, + ReduceSumSquare = 5, + ReduceASum = 6, + ReduceAll = 7 +} + +enum PoolMode : byte { + MAX_POOLING = 0, + MEAN_POOLING = 1, +} + +enum EltwiseMode : byte { + PROD = 0, + SUM = 1, + MAXIMUM = 2, + UNKNOWN = 3 +} + +enum PadMode : byte { + PAD = 0, + SAME = 1, + VALID = 2, +} + +enum RoundMode : byte { + FLOOR = 0, + CEIL = 1 +} + +enum PaddingMode : byte { + CONSTANT = 0, + REFLECT = 1, + SYMMETRIC = 2, + MODE_RESERVED = 3 +} + +enum LshProjectionType : byte { + UNKNOWN = 0, + SPARSE = 1, + DENSE = 2 +} + +enum Reduction : byte { + REDUCTION_SUM = 0, + MEAN = 1, + NONE = 2 +} + +table Vec { + data: [long]; +} + +table Vec2D { + data: [Vec]; +} diff --git a/mindspore/lite/schema/ops_v0.fbs b/mindspore/lite/schema/ops_v0.fbs index c013aa5345..5df2576b0b 100644 --- a/mindspore/lite/schema/ops_v0.fbs +++ b/mindspore/lite/schema/ops_v0.fbs @@ -17,7 +17,7 @@ namespace mindspore.schema.v0; enum ResizeMethod: byte { - UNKNOW = -1, + UNKNOWN = -1, LINEAR = 0, NEAREST = 1, CUBIC = 2 @@ -80,27 +80,9 @@ enum ActivationType : byte { HARD_TANH = 16, SIGN = 17, SWISH = 18, - UNKNOW = 19 -} -enum ActivationGradType : byte { - NO_ACTIVATION = 0, - RELU = 1, - SIGMOID = 2, - RELU6 = 3, - ELU = 4, - LEAKY_RELU = 5, - ABS = 6, - RELU1 = 7, - SOFTSIGN = 8, - SOFTPLUS = 9, - TANH = 10, - SELU = 11, - HSWISH = 12, - HSIGMOID = 13, - THRESHOLDRELU = 14, - LINEAR = 15, - UNKNOW = 16 + UNKNOWN = 19 } + enum ReduceType : byte { REDUCE_MAX = 0, REDUCE_MEAN = 1, @@ -109,7 +91,7 @@ enum ReduceType : byte { REDUCE_LOG_SUM_EXP = 4, REDUCE_PROD = 5, REDUCE_SUM = 6, - UNKNOW = 7 + UNKNOWN = 7 } enum PoolMode : byte { @@ -121,7 +103,7 @@ enum EltwiseMode : byte { PROD = 0, SUM = 1, MAXIMUM = 2, - UNKNOW = 3 + UNKNOWN = 3 } enum PadMode : byte { @@ -1007,11 +989,6 @@ table OneHot { table Lstm{ bidirection: bool = false; - smooth: float = 0.0; -} - -table Gru{ - bidirection: bool = false; } table PriorBox { @@ -1156,9 +1133,6 @@ table While { bodySubgraphIndex : int; } -table If { -} - table UnsortedSegmentSum { numSegments : int; } @@ -1239,10 +1213,3 @@ table Reciprocal { table Merge { } - -table GeLU { - approximate : bool = false; -} - -table NonZero { -} diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 23c63879e8..09f8139f67 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -35,9 +35,12 @@ set(LITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/common/graph_util.cc ${CMAKE_CURRENT_SOURCE_DIR}/common/log_adapter.cc ${CMAKE_CURRENT_SOURCE_DIR}/common/string_util.cc + ${CMAKE_CURRENT_SOURCE_DIR}/common/prim_util.cc + ${CMAKE_CURRENT_SOURCE_DIR}/common/tensor_util.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/allocator.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.c + ${CMAKE_CURRENT_SOURCE_DIR}/runtime/infer_manager.cc ${CMAKE_CURRENT_SOURCE_DIR}/tensor.cc ${CMAKE_CURRENT_SOURCE_DIR}/tensorlist.cc ${CMAKE_CURRENT_SOURCE_DIR}/executor.cc @@ -104,6 +107,12 @@ if(SUPPORT_TRAIN) ${CMAKE_CURRENT_SOURCE_DIR}/train/accuracy_monitor.cc ${CMAKE_CURRENT_SOURCE_DIR}/train/classification_train_accuracy_monitor.cc ) + if(ENABLE_V0) + set(LITE_SRC + ${LITE_SRC} + ${CMAKE_CURRENT_SOURCE_DIR}/train/train_populate_parameter_v0.cc + ) + endif() endif() if(ENABLE_MINDRT) diff --git a/mindspore/lite/src/common/common.h b/mindspore/lite/src/common/common.h index 58ecbfc3da..dd8540f03b 100644 --- a/mindspore/lite/src/common/common.h +++ b/mindspore/lite/src/common/common.h @@ -32,7 +32,7 @@ enum CHWK_SHAPE { CHWK_C = 0, CHWK_H = 1, CHWK_W = 2, CHWK_K = 3 }; enum KHWC_SHAPE { KHWC_K = 0, KHWC_H = 1, KHWC_W = 2, KHWC_C = 3 }; enum CHW_SHAPE { CHW_C = 0, CHW_H = 1, CHW_W = 2 }; enum HWC_SHAPE { HWC_H = 0, HWC_W = 1, HWC_C = 2 }; -enum SCHEMA_VERSION { SCHEMA_INVALID = -1, SCHEMA_CUR = 0, SCHEMA_V0 = 1 }; +enum SCHEMA_VERSION : int { SCHEMA_INVALID = -1, SCHEMA_CUR = 0, SCHEMA_V0 = 1 }; static constexpr int kNCHWDimNumber = 4; static constexpr int kNHWCDimNumber = 4; diff --git a/mindspore/lite/src/common/graph_util.cc b/mindspore/lite/src/common/graph_util.cc index 59c8015e2b..d9c1e526dd 100644 --- a/mindspore/lite/src/common/graph_util.cc +++ b/mindspore/lite/src/common/graph_util.cc @@ -20,7 +20,11 @@ #include "src/common/graph_util.h" #include "src/common/utils.h" #include "src/common/log_adapter.h" +#include "src/common/version_manager.h" #include "include/errorcode.h" +#ifdef ENABLE_V0 +#include "schema/model_v0_generated.h" +#endif namespace mindspore { namespace lite { @@ -82,10 +86,17 @@ std::vector GetLinkedPostNodeIdx(const lite::Model *model, const size_t return post_node_idxes; } -bool IsPackedOp(schema::PrimitiveType op_type) { - static std::vector packed_ops = { - schema::PrimitiveType_Conv2D, schema::PrimitiveType_DeConv2D, schema::PrimitiveType_DepthwiseConv2D, - schema::PrimitiveType_DeDepthwiseConv2D, schema::PrimitiveType_MatMul}; +bool IsPackedOp(int op_type) { +#ifdef ENABLE_V0 + static std::vector v0_packed_ops = { + schema::v0::PrimitiveType_Conv2D, schema::v0::PrimitiveType_DeConv2D, schema::v0::PrimitiveType_DepthwiseConv2D, + schema::v0::PrimitiveType_DeDepthwiseConv2D, schema::v0::PrimitiveType_MatMul}; + if (VersionManager::GetInstance()->CheckV0Schema()) { + return IsContain(v0_packed_ops, op_type); + } +#endif + static std::vector packed_ops = {schema::PrimitiveType_Conv2DFusion, schema::PrimitiveType_Conv2dTransposeFusion, + schema::PrimitiveType_MatMul}; return IsContain(packed_ops, op_type); } } // namespace lite diff --git a/mindspore/lite/src/common/graph_util.h b/mindspore/lite/src/common/graph_util.h index a158d751fa..76b6256c5d 100644 --- a/mindspore/lite/src/common/graph_util.h +++ b/mindspore/lite/src/common/graph_util.h @@ -37,7 +37,7 @@ std::vector GetGraphOutputNodes(const lite::Model *model); std::vector GetLinkedPostNodeIdx(const lite::Model *model, size_t tensor_idx); -bool IsPackedOp(schema::PrimitiveType op_type); +bool IsPackedOp(int op_type); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/common/log_util.h b/mindspore/lite/src/common/log_util.h new file mode 100644 index 0000000000..2a87d11e2a --- /dev/null +++ b/mindspore/lite/src/common/log_util.h @@ -0,0 +1,31 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_COMMON_LOG_UTIL_H_ +#define MINDSPORE_LITE_SRC_COMMON_LOG_UTIL_H_ + +#include "src/common/log_adapter.h" +#include "include/errorcode.h" + +#define MSLITE_CHECK_PTR(ptr) \ + do { \ + if ((ptr) == nullptr) { \ + MS_LOG(ERROR) << ": The pointer[" << #ptr << "] is null."; \ + return mindspore::lite::RET_ERROR; \ + } \ + } while (0) + +#endif // MINDSPORE_LITE_SRC_COMMON_LOG_UTIL_H_ diff --git a/mindspore/lite/src/common/prim_inner.h b/mindspore/lite/src/common/prim_inner.h new file mode 100644 index 0000000000..65120615e9 --- /dev/null +++ b/mindspore/lite/src/common/prim_inner.h @@ -0,0 +1,36 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_COMMON_PRIM_INNER_H_ +#define MINDSPORE_LITE_SRC_COMMON_PRIM_INNER_H_ +#include + +namespace mindspore { +namespace lite { +enum PRIM_INNER_TYPE : int { + PRIM_TO_FORMAT = 10000, + PRIM_RETURN = 10001, + PRIM_MAKE_TUPLE = 10002, + PRIM_TUPLE_GET_ITEM = 10003, + PRIM_LOOP = 10004, + PRIM_CONSTANT = 10005, + PRIM_OPENCL_FUSION_ELTWISE = 10006, +}; + +} // namespace lite +} // namespace mindspore + +#endif // MINDSPORE_LITE_SRC_COMMON_PRIM_INNER_H_ diff --git a/mindspore/lite/src/common/prim_util.cc b/mindspore/lite/src/common/prim_util.cc new file mode 100644 index 0000000000..e8cad919a6 --- /dev/null +++ b/mindspore/lite/src/common/prim_util.cc @@ -0,0 +1,122 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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 "src/common/prim_util.h" +#include "src/common/version_manager.h" +#include "schema/model_generated.h" +#ifdef ENABLE_V0 +#include "schema/model_v0_generated.h" +#endif + +namespace mindspore { +namespace lite { +int GetPrimitiveType(const void *primitive) { + if (primitive == nullptr) { + return -1; + } +#ifdef ENABLE_V0 + if (VersionManager::GetInstance()->GetSchemaVersion() == SCHEMA_V0) { + return static_cast(primitive)->value_type(); + } +#endif + return static_cast(primitive)->value_type(); +} + +const char *PrimitiveTypeName(int type) { +#ifdef ENABLE_V0 + if (VersionManager::GetInstance()->GetSchemaVersion() == SCHEMA_V0) { + return schema::v0::EnumNamePrimitiveType(static_cast(type)); + } +#endif + return schema::EnumNamePrimitiveType(static_cast(type)); +} + +const char *PrimitiveCurVersionTypeName(int type) { + return schema::EnumNamePrimitiveType(static_cast(type)); +} + +int GenPrimVersionKey(int primitive_type, int schema_version) { return primitive_type * 1000 + schema_version; } + +bool IsPartialNode(const void *primitive) { + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + if (schema_version == SCHEMA_CUR) { + return reinterpret_cast(primitive)->value_type() == schema::PrimitiveType_PartialFusion; + } +#ifdef ENABLE_V0 + if (schema_version == SCHEMA_V0) { + return reinterpret_cast(primitive)->value_type() == + schema::v0::PrimitiveType_Partial; + } +#endif + return false; +} + +int GetPartialGraphIndex(const void *primitive) { + int index = -1; + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + if (schema_version == SCHEMA_CUR) { + index = static_cast(primitive)->value_as_PartialFusion()->sub_graph_index(); + } +#ifdef ENABLE_V0 + if (schema_version == SCHEMA_V0) { + index = static_cast(primitive)->value_as_Partial()->subGraphIndex(); + } +#endif + return index; +} + +bool IsWhileNode(const void *primitive) { + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + if (schema_version == SCHEMA_CUR) { + return reinterpret_cast(primitive)->value_type() == schema::PrimitiveType_While; + } +#ifdef ENABLE_V0 + if (schema_version == SCHEMA_V0) { + return reinterpret_cast(primitive)->value_type() == schema::v0::PrimitiveType_While; + } +#endif + return false; +} + +int GetWhileBodySubgraphIndex(const void *primitive) { + int index = -1; + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + if (schema_version == SCHEMA_CUR) { + index = reinterpret_cast(primitive)->value_as_While()->body_subgraph_index(); + } +#ifdef ENABLE_V0 + if (schema_version == SCHEMA_V0) { + index = reinterpret_cast(primitive)->value_as_While()->bodySubgraphIndex(); + } +#endif + return index; +} + +int GetWhileCondSubgraphIndex(const void *primitive) { + int index = -1; + int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); + if (schema_version == SCHEMA_CUR) { + index = reinterpret_cast(primitive)->value_as_While()->cond_subgraph_index(); + } +#ifdef ENABLE_V0 + if (schema_version == SCHEMA_V0) { + index = reinterpret_cast(primitive)->value_as_While()->condSubgraphIndex(); + } +#endif + return index; +} +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/common/prim_util.h b/mindspore/lite/src/common/prim_util.h new file mode 100644 index 0000000000..f414a2d644 --- /dev/null +++ b/mindspore/lite/src/common/prim_util.h @@ -0,0 +1,34 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_COMMON_PRIM_UTIL_H_ +#define MINDSPORE_LITE_SRC_COMMON_PRIM_UTIL_H_ + +namespace mindspore { +namespace lite { +int GetPrimitiveType(const void *prim); +const char *PrimitiveTypeName(int type); +const char *PrimitiveCurVersionTypeName(int type); +int GenPrimVersionKey(int primitive_type, int schema_version); +bool IsPartialNode(const void *primitive); +int GetPartialGraphIndex(const void *primitive); +bool IsWhileNode(const void *primitive); +int GetWhileBodySubgraphIndex(const void *primitive); +int GetWhileCondSubgraphIndex(const void *primitive); +} // namespace lite +} // namespace mindspore + +#endif // MINDSPORE_LITE_SRC_COMMON_PRIM_UTIL_H_ diff --git a/mindspore/lite/src/common/tensor_util.cc b/mindspore/lite/src/common/tensor_util.cc new file mode 100644 index 0000000000..7c00a156a0 --- /dev/null +++ b/mindspore/lite/src/common/tensor_util.cc @@ -0,0 +1,375 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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 "src/common/tensor_util.h" +#include "schema/model_generated.h" +#include "include/errorcode.h" +#include "src/common/log_adapter.h" + +namespace mindspore { +namespace lite { +int InputTensor2TensorC(const std::vector &tensors_in, std::vector *tensors_out) { + for (size_t i = 0; i < tensors_in.size(); ++i) { + size_t shape_size = tensors_in[i]->shape().size(); + if (shape_size >= MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "shape size " << shape_size << " unsupported!"; + return RET_ERROR; + } + auto *tensor_c = static_cast(malloc(sizeof(TensorC))); + if (tensor_c == nullptr) { + MS_LOG(ERROR) << "malloc tensor fail!"; + return RET_ERROR; + } + tensor_c->format_ = tensors_in[i]->format(); + tensor_c->data_type_ = tensors_in[i]->data_type(); + tensor_c->shape_size_ = shape_size; + tensor_c->data_ = tensors_in[i]->data_c(); + for (size_t j = 0; j < shape_size; ++j) { + tensor_c->shape_[j] = tensors_in[i]->shape()[j]; + } + tensors_out->push_back(tensor_c); + } + return RET_OK; +} + +int OutputTensor2TensorC(const std::vector &tensors, std::vector *tensors_c) { + for (size_t i = 0; i < tensors.size(); ++i) { + auto *tensor_c = static_cast(malloc(sizeof(TensorC))); + if (tensor_c == nullptr) { + MS_LOG(ERROR) << "malloc tensor fail!"; + return RET_ERROR; + } + tensor_c->data_type_ = kNumberTypeFloat32; + tensor_c->format_ = schema::Format::Format_NCHW; + tensor_c->data_ = nullptr; + tensor_c->shape_size_ = 0; + tensors_c->push_back(tensor_c); + } + return RET_OK; +} + +void TensorC2LiteTensor(const std::vector &tensors_in, std::vector *tensors_out) { + for (size_t i = 0; i < tensors_in.size(); ++i) { + tensors_out->at(i)->set_format(static_cast(tensors_in[i]->format_)); + tensors_out->at(i)->set_data_type(static_cast(tensors_in[i]->data_type_)); + tensors_out->at(i)->set_shape({tensors_in[i]->shape_, tensors_in[i]->shape_ + tensors_in[i]->shape_size_}); + } +} + +void FreeAllTensorC(std::vector *tensors_in) { + for (auto &i : *tensors_in) { + if (i == nullptr) { + continue; + } + if (i->data_type_ == kObjectTypeTensorType) { + TensorListC *tensorListC = reinterpret_cast(i); + FreeTensorListC(tensorListC); + tensorListC = nullptr; + } else { + free(i); + i = nullptr; + } + } + tensors_in->clear(); +} + +void FreeTensorListC(TensorListC *tensorlist_c) { + for (size_t i = 0; i < tensorlist_c->element_num_; i++) { + free(tensorlist_c->tensors_[i]); + tensorlist_c->tensors_[i] = nullptr; + } + if (tensorlist_c->tensors_ != nullptr) { + free(tensorlist_c->tensors_); + tensorlist_c->tensors_ = nullptr; + } + free(tensorlist_c); +} + +TensorC *NewTensorC() { + auto *tensor_c = static_cast(malloc(sizeof(TensorC))); + if (tensor_c == nullptr) { + MS_LOG(ERROR) << "malloc tensor fail!"; + return nullptr; + } + tensor_c->data_type_ = kNumberTypeFloat32; + tensor_c->format_ = schema::Format::Format_NCHW; + tensor_c->data_ = nullptr; + tensor_c->shape_size_ = 0; + return tensor_c; +} + +void Tensor2TensorC(Tensor *src, TensorC *dst) { + dst->format_ = src->format(); + dst->data_ = src->data_c(); + dst->data_type_ = src->data_type(); + dst->shape_size_ = src->shape().size(); + for (size_t i = 0; i < dst->shape_size_; i++) { + dst->shape_[i] = src->shape().at(i); + } +} + +void TensorC2Tensor(TensorC *src, Tensor *dst) { + dst->set_format(static_cast(src->format_)); + dst->set_data_type(static_cast(src->data_type_)); // get data during the runtime period + dst->set_shape(std::vector(src->shape_, src->shape_ + src->shape_size_)); +} + +int TensorList2TensorListC(TensorList *src, TensorListC *dst) { + dst->data_type_ = static_cast(src->data_type()); + dst->format_ = src->format(); + dst->element_num_ = src->shape().empty() ? 0 : src->tensors().size(); + + dst->tensors_ = reinterpret_cast(malloc(dst->element_num_ * sizeof(TensorC *))); + if (dst->tensors_ == nullptr) { + return RET_ERROR; + } + memset(dst->tensors_, 0, dst->element_num_ * sizeof(TensorC *)); + for (size_t i = 0; i < dst->element_num_; i++) { + dst->tensors_[i] = reinterpret_cast(malloc(sizeof(TensorC))); + if (dst->tensors_[i] == nullptr) { + return NNACL_ERR; + } + memset(dst->tensors_[i], 0, sizeof(TensorC)); + Tensor2TensorC(src->tensors().at(i), dst->tensors_[i]); + } + + dst->tensors_data_type_ = src->tensors_data_type(); + dst->element_shape_size_ = src->element_shape().size(); + for (size_t i = 0; i < dst->element_shape_size_; i++) { + dst->element_shape_[i] = src->element_shape().at(i); + } + dst->max_elements_num_ = src->max_elements_num(); + return NNACL_OK; +} + +void TensorListC2TensorList(TensorListC *src, TensorList *dst) { + dst->set_data_type(static_cast(src->data_type_)); + dst->set_format(static_cast(src->format_)); + dst->set_shape(std::vector(1, src->element_num_)); + dst->set_tensors_data_type(static_cast(src->tensors_data_type_)); + + // Set Tensors + for (size_t i = 0; i < src->element_num_; i++) { + TensorC2Tensor(src->tensors_[i], dst->GetTensor(i)); + } + + dst->set_element_shape(std::vector(src->element_shape_, src->element_shape_ + src->element_shape_size_)); + dst->set_max_elements_num(src->max_elements_num_); +} + +int GenerateMergeOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c) { + int ret = RET_OK; + for (size_t i = 0; i < outputs->size(); i++) { + if (inputs.at(i)->data_type() == kObjectTypeTensorType) { + auto *output_tensorlist = reinterpret_cast(malloc(sizeof(TensorListC))); + if (output_tensorlist == nullptr) { + return RET_ERROR; + } + memset(output_tensorlist, 0, sizeof(TensorListC)); + output_tensorlist->element_num_ = inputs[i]->shape().empty() ? 0 : inputs[i]->shape().at(0); + if (output_tensorlist->element_num_ != 0) { + output_tensorlist->tensors_ = + reinterpret_cast(malloc(output_tensorlist->element_num_ * sizeof(TensorC *))); + if (output_tensorlist->tensors_ == nullptr) { + free(output_tensorlist); + output_tensorlist = nullptr; + return RET_ERROR; + } + memset(output_tensorlist->tensors_, 0, output_tensorlist->element_num_ * sizeof(TensorC *)); + for (size_t j = 0; j < output_tensorlist->element_num_; j++) { + output_tensorlist->tensors_[j] = reinterpret_cast(malloc(sizeof(TensorC))); + if (output_tensorlist->tensors_[j] == nullptr) { + for (size_t k = 0; k < j; k++) { + free(output_tensorlist->tensors_[k]); + output_tensorlist->tensors_[k] = nullptr; + } + free(output_tensorlist->tensors_); + output_tensorlist->tensors_ = nullptr; + free(output_tensorlist); + output_tensorlist = nullptr; + return RET_ERROR; + } + memset(output_tensorlist->tensors_[j], 0, sizeof(TensorC)); + } + } + + out_tensor_c->push_back(reinterpret_cast(output_tensorlist)); + } else { + auto *output_tensor = NewTensorC(); + if (output_tensor == nullptr) { + MS_LOG(ERROR) << "malloc tensor_c failed"; + ret = RET_ERROR; + break; + } + out_tensor_c->push_back(reinterpret_cast(output_tensor)); + } + } + return ret; +} + +int GenerateSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c) { + int ret = RET_OK; + MS_ASSERT(inputs.size() == outputs->size() / 2 + 1); + out_tensor_c->resize(outputs->size()); + for (size_t i = 0; i < outputs->size() / 2; i++) { + if (inputs.at(i + 1)->data_type() == kObjectTypeTensorType) { + auto *output_tensorlist1 = reinterpret_cast(malloc(sizeof(TensorListC))); + if (output_tensorlist1 == nullptr) { + MS_LOG(ERROR) << "malloc tensorlist_c failed"; + ret = RET_ERROR; + break; + } + + memset(output_tensorlist1, 0, sizeof(TensorListC)); + output_tensorlist1->element_num_ = inputs[i + 1]->shape().empty() ? 0 : inputs[i + 1]->shape().at(0); + if (output_tensorlist1->element_num_ != 0) { + output_tensorlist1->tensors_ = + reinterpret_cast(malloc(output_tensorlist1->element_num_ * sizeof(TensorC *))); + if (output_tensorlist1->tensors_ == nullptr) { + free(output_tensorlist1); + output_tensorlist1 = nullptr; + return RET_ERROR; + } + memset(output_tensorlist1->tensors_, 0, output_tensorlist1->element_num_ * sizeof(TensorC *)); + for (size_t j = 0; j < output_tensorlist1->element_num_; j++) { + output_tensorlist1->tensors_[j] = reinterpret_cast(malloc(sizeof(TensorC))); + if (output_tensorlist1->tensors_[j] == nullptr) { + for (size_t k = 0; k < j; k++) { + free(output_tensorlist1->tensors_[k]); + output_tensorlist1->tensors_[k] = nullptr; + } + free(output_tensorlist1->tensors_); + output_tensorlist1->tensors_ = nullptr; + return RET_ERROR; + } + memset(output_tensorlist1->tensors_[j], 0, sizeof(TensorC)); + } + } + + out_tensor_c->at(i) = reinterpret_cast(output_tensorlist1); + + auto *output_tensorlist2 = reinterpret_cast(malloc(sizeof(TensorListC))); + if (output_tensorlist2 == nullptr) { + return RET_ERROR; + } + memset(output_tensorlist2, 0, sizeof(TensorListC)); + output_tensorlist2->element_num_ = inputs[i + 1]->shape().empty() ? 0 : inputs[i + 1]->shape().at(0); + if (output_tensorlist2->element_num_ != 0) { + output_tensorlist2->tensors_ = + reinterpret_cast(malloc(output_tensorlist2->element_num_ * sizeof(TensorC *))); + if (output_tensorlist2->tensors_ == nullptr) { + free(output_tensorlist2); + output_tensorlist2 = nullptr; + return RET_ERROR; + } + memset(output_tensorlist2->tensors_, 0, output_tensorlist2->element_num_ * sizeof(TensorC *)); + for (size_t j = 0; j < output_tensorlist2->element_num_; j++) { + output_tensorlist2->tensors_[j] = reinterpret_cast(malloc(sizeof(TensorC))); + if (output_tensorlist2->tensors_[j] == nullptr) { + for (size_t k = 0; k < j; k++) { + free(output_tensorlist2->tensors_[k]); + output_tensorlist2->tensors_[k] = nullptr; + } + free(output_tensorlist2->tensors_); + output_tensorlist2->tensors_ = nullptr; + free(output_tensorlist2); + output_tensorlist2 = nullptr; + return RET_ERROR; + } + memset(output_tensorlist2->tensors_[j], 0, sizeof(TensorC)); + } + } + + out_tensor_c->at(i + outputs->size() / 2) = reinterpret_cast(output_tensorlist2); + } else { + auto *output_tensor1 = NewTensorC(); + if (output_tensor1 == nullptr) { + MS_LOG(ERROR) << "malloc tensor_c failed"; + ret = RET_ERROR; + break; + } + out_tensor_c->at(i) = reinterpret_cast(output_tensor1); + auto *output_tensor2 = NewTensorC(); + if (output_tensor2 == nullptr) { + MS_LOG(ERROR) << "malloc tensor_c failed"; + ret = RET_ERROR; + break; + } + out_tensor_c->at(i + outputs->size() / 2) = reinterpret_cast(output_tensor2); + } + } + return ret; +} + +int GenerateOutTensorC(const OpParameter *const parameter, const std::vector &inputs, + std::vector *outputs, std::vector *out_tensor_c) { + int ret = RET_OK; + if (parameter->type_ == mindspore::schema::PrimitiveType_TensorListFromTensor || + parameter->type_ == mindspore::schema::PrimitiveType_TensorListReserve || + parameter->type_ == mindspore::schema::PrimitiveType_TensorListSetItem) { + // TensorListC ->TensorC + auto *tensor_list_c = reinterpret_cast(malloc(sizeof(TensorListC))); + if (tensor_list_c == nullptr) { + return RET_ERROR; + } + memset(tensor_list_c, 0, sizeof(TensorListC)); + out_tensor_c->push_back(reinterpret_cast(tensor_list_c)); + } else if (parameter->type_ == mindspore::schema::PrimitiveType_Merge) { + ret = GenerateMergeOutTensorC(inputs, outputs, out_tensor_c); + } else if (parameter->type_ == mindspore::schema::PrimitiveType_Switch) { + ret = GenerateSwitchOutTensorC(inputs, outputs, out_tensor_c); + } else { + ret = OutputTensor2TensorC(*outputs, out_tensor_c); + } + return ret; +} + +int GenerateInTensorC(const OpParameter *const parameter, const std::vector &inputs, + std::vector *outputs, std::vector *in_tensor_c) { + int ret = RET_OK; + for (auto input : inputs) { + if (input->data_type() == kObjectTypeTensorType) { + // Tensor ->TensorList -> TensorListC -> TensorC + auto *tensor_list = reinterpret_cast(input); + auto *tensor_list_c = reinterpret_cast(malloc(sizeof(TensorListC))); + if (tensor_list_c == nullptr) { + ret = RET_NULL_PTR; + break; + } + memset(tensor_list_c, 0, sizeof(TensorListC)); + ret = TensorList2TensorListC(tensor_list, tensor_list_c); + if (ret != RET_OK) { + return NNACL_ERR; + } + in_tensor_c->push_back(reinterpret_cast(tensor_list_c)); + } else { + // Tensor -> TensorC + auto *tensor_c = reinterpret_cast(malloc(sizeof(TensorC))); + if (tensor_c == nullptr) { + ret = RET_NULL_PTR; + break; + } + Tensor2TensorC(input, tensor_c); + in_tensor_c->emplace_back(tensor_c); + } + } + return ret; +} + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/common/tensor_util.h b/mindspore/lite/src/common/tensor_util.h new file mode 100644 index 0000000000..6fdce47445 --- /dev/null +++ b/mindspore/lite/src/common/tensor_util.h @@ -0,0 +1,48 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_COMMON_TENSOR_UTIL_H_ +#define MINDSPORE_LITE_SRC_COMMON_TENSOR_UTIL_H_ +#include +#include "src/tensor.h" +#include "src/tensorlist.h" +#include "nnacl/tensor_c.h" +#include "nnacl/infer/common_infer.h" + +namespace mindspore { +namespace lite { +int InputTensor2TensorC(const std::vector &tensors_in, std::vector *tensors_out); +int OutputTensor2TensorC(const std::vector &tensors_in, std::vector *tensors_out); +void TensorC2LiteTensor(const std::vector &tensors_in, std::vector *tensors_out); +void FreeAllTensorC(std::vector *tensors_in); +void FreeTensorListC(TensorListC *tensorListC); +TensorC *NewTensorC(); +void Tensor2TensorC(Tensor *src, TensorC *dst); +void TensorC2Tensor(TensorC *src, Tensor *dst); +int TensorList2TensorListC(TensorList *src, TensorListC *dst); +void TensorListC2TensorList(TensorListC *src, TensorList *dst); +int GenerateMergeOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c); +int GenerateSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c); +int GenerateInTensorC(const OpParameter *const parameter, const std::vector &inputs, + std::vector *outputs, std::vector *in_tensor_c); +int GenerateOutTensorC(const OpParameter *const parameter, const std::vector &inputs, + std::vector *outputs, std::vector *out_tensor_c); +} // namespace lite +} // namespace mindspore + +#endif // MINDSPORE_LITE_SRC_COMMON_TENSOR_UTIL_H_ diff --git a/mindspore/lite/src/common/version_manager.h b/mindspore/lite/src/common/version_manager.h index 5b336c000d..4998963ed7 100644 --- a/mindspore/lite/src/common/version_manager.h +++ b/mindspore/lite/src/common/version_manager.h @@ -18,8 +18,7 @@ #define MINDSPORE_LITE_SRC_COMMON_VERSION_MANAGER_H_ #include -#include "src/lite_model.h" - +#include "src/common/common.h" namespace mindspore { namespace lite { class VersionManager { @@ -32,6 +31,7 @@ class VersionManager { void SetSchemaVersion(const int schema_version) { schema_version_ = schema_version; } int GetSchemaVersion() const { return schema_version_; } + bool CheckV0Schema() const { return schema_version_ == SCHEMA_VERSION::SCHEMA_V0; } private: VersionManager() = default; diff --git a/mindspore/lite/src/dequant.cc b/mindspore/lite/src/dequant.cc index 1591281a05..6987c9a45c 100644 --- a/mindspore/lite/src/dequant.cc +++ b/mindspore/lite/src/dequant.cc @@ -18,7 +18,7 @@ #include #include "src/dequant.h" #include "src/huffman_decode.h" -#include "src/ops/matmul.h" +#include "nnacl/matmul_parameter.h" namespace mindspore::lite { float *DequantUtil::DequantWeight(lite::Tensor *input_tensor, bool channel_first) { @@ -66,7 +66,7 @@ int DequantUtil::UnPackToInt(const schema::Tensor *input_tensor, void *unpack_in return RET_OK; } -std::map> DequantUtil::DequantTensor(const mindspore::lite::PrimitiveC *primitive, +std::map> DequantUtil::DequantTensor(OpParameter *op_param, const std::vector &in_tensors, TypeId data_type, bool need_restore) { std::map> tensor_origin_data; @@ -76,13 +76,12 @@ std::map> DequantUtil::DequantTensor(const m MS_ASSERT(weight_tensor != nullptr); input_i++; auto channel_first = true; - if ((schema::PrimitiveType)primitive->Type() == schema::PrimitiveType_MatMul && - weight_tensor->shape().size() == 2) { - auto param = reinterpret_cast(const_cast(primitive)); + if (op_param->type_ == schema::PrimitiveType_MatMul && weight_tensor->shape().size() == 2) { + auto param = reinterpret_cast(op_param); if (input_i == 1) { - channel_first = !param->GetTransposeA(); + channel_first = !param->a_transpose_; } else if (input_i == 2) { - channel_first = param->GetTransposeB(); + channel_first = param->b_transpose_; } else { MS_LOG(WARNING) << "unexpected input_i"; } diff --git a/mindspore/lite/src/dequant.h b/mindspore/lite/src/dequant.h index a45aa03620..f2c7a76655 100644 --- a/mindspore/lite/src/dequant.h +++ b/mindspore/lite/src/dequant.h @@ -33,7 +33,7 @@ class DequantUtil { static int UnPackToInt(const schema::Tensor *input_tensor, void *weight_unpack_data); - static std::map> DequantTensor(const mindspore::lite::PrimitiveC *primitive, + static std::map> DequantTensor(OpParameter *op_param, const std::vector &in_tensors, TypeId data_type, bool need_restore = true); diff --git a/mindspore/lite/src/kernel_registry.cc b/mindspore/lite/src/kernel_registry.cc index 3a964e45c4..063daa9bd0 100644 --- a/mindspore/lite/src/kernel_registry.cc +++ b/mindspore/lite/src/kernel_registry.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2020-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ #include "src/kernel_registry.h" #include "include/errorcode.h" #include "src/ops/populate/populate_register.h" +#include "src/common/version_manager.h" +#include "src/common/prim_util.h" +#include "nnacl/pooling_parameter.h" #ifdef ENABLE_ARM64 #include #include "common/utils.h" @@ -38,6 +41,7 @@ KernelRegistry *KernelRegistry::GetInstance() { if (instance.creator_arrays_ == nullptr) { return nullptr; } + memset(instance.creator_arrays_, 0, array_size_ * sizeof(KernelRegistry)); } return &instance; } @@ -76,7 +80,7 @@ int KernelRegistry::GetCreatorFuncIndex(const kernel::KernelKey desc) { int index; int device_index = static_cast(desc.arch) - kKernelArch_MIN; int dType_index = static_cast(desc.data_type) - kNumberTypeBegin; - int op_index = static_cast(desc.type) - PrimitiveType_MIN; + int op_index = static_cast(desc.type); index = device_index * data_type_length_ * op_type_length_ + dType_index * op_type_length_ + op_index; return index; } @@ -91,8 +95,7 @@ void KernelRegistry::RegKernel(const KernelKey desc, const kernel::KernelCreator creator_arrays_[index] = creator; } -void KernelRegistry::RegKernel(const KERNEL_ARCH arch, const TypeId data_type, const schema::PrimitiveType op_type, - kernel::KernelCreator creator) { +void KernelRegistry::RegKernel(KERNEL_ARCH arch, TypeId data_type, int op_type, kernel::KernelCreator creator) { KernelKey desc = {arch, data_type, op_type}; int index = GetCreatorFuncIndex(desc); if (index >= array_size_) { @@ -105,36 +108,6 @@ void KernelRegistry::RegKernel(const KERNEL_ARCH arch, const TypeId data_type, c bool KernelRegistry::Merge(const std::unordered_map &new_creators) { return false; } -kernel::LiteKernel *KernelRegistry::GetKernel(const std::vector &in_tensors, - const std::vector &out_tensors, const PrimitiveC *primitive, - const InnerContext *ctx, const kernel::KernelKey &key) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != ctx); - auto func_pointer = PopulateRegistry::GetInstance()->GetParameterCreator(schema::PrimitiveType(primitive->Type())); - if (func_pointer == nullptr) { - MS_LOG(ERROR) << "ParameterCreator function pointer is nullptr, type: " - << schema::EnumNamePrimitiveType((schema::PrimitiveType)primitive->Type()); - return nullptr; - } - auto parameter = func_pointer(primitive); - if (parameter == nullptr) { - MS_LOG(ERROR) << "PopulateParameter return nullptr, type: " - << schema::EnumNamePrimitiveType((schema::PrimitiveType)primitive->Type()); - return nullptr; - } - auto creator = GetCreator(key); - if (creator != nullptr) { - auto kernel = creator(in_tensors, out_tensors, parameter, ctx, key, primitive); - if (kernel != nullptr) { - kernel->set_desc(key); - } - return kernel; - } else { - free(parameter); - } - return nullptr; -} - KernelRegistry::~KernelRegistry() { KernelRegistry *instance = GetInstance(); std::unique_lock malloc_creator_array(instance->lock_); @@ -143,4 +116,21 @@ KernelRegistry::~KernelRegistry() { instance->creator_arrays_ = nullptr; } } + +int KernelRegistry::GetKernel(const std::vector &in_tensors, const std::vector &out_tensors, + const InnerContext *ctx, const kernel::KernelKey &key, OpParameter *parameter, + kernel::LiteKernel **kernel) { + MS_ASSERT(ctx != nullptr); + MS_ASSERT(kernel != nullptr); + auto creator = GetCreator(key); + if (creator != nullptr) { + *kernel = creator(in_tensors, out_tensors, parameter, ctx, key); + if (*kernel != nullptr) { + (*kernel)->set_desc(key); + return RET_OK; + } + return RET_ERROR; + } + return RET_NOT_SUPPORT; +} } // namespace mindspore::lite diff --git a/mindspore/lite/src/kernel_registry.h b/mindspore/lite/src/kernel_registry.h index 77922c4c35..31f3b875fb 100644 --- a/mindspore/lite/src/kernel_registry.h +++ b/mindspore/lite/src/kernel_registry.h @@ -40,10 +40,11 @@ class KernelRegistry { const kernel::KernelCreator *GetCreatorArrays(); int GetCreatorFuncIndex(kernel::KernelKey desc); void RegKernel(kernel::KernelKey desc, kernel::KernelCreator creator); - void RegKernel(kernel::KERNEL_ARCH arch, TypeId data_type, schema::PrimitiveType type, kernel::KernelCreator creator); + void RegKernel(kernel::KERNEL_ARCH arch, TypeId data_type, int type, kernel::KernelCreator creator); bool Merge(const std::unordered_map &newCreators); - kernel::LiteKernel *GetKernel(const std::vector &in_tensors, const std::vector &out_tensors, - const PrimitiveC *primitive, const InnerContext *ctx, const kernel::KernelKey &key); + int GetKernel(const std::vector &in_tensors, const std::vector &out_tensors, + const InnerContext *ctx, const kernel::KernelKey &key, OpParameter *op_parameter, + kernel::LiteKernel **kernel); protected: static const int device_type_length_{kKernelArch_MAX - kKernelArch_MIN + 1}; @@ -63,7 +64,7 @@ class KernelRegistrar { } ~KernelRegistrar() = default; - KernelRegistrar(const kernel::KERNEL_ARCH arch, const TypeId data_type, const schema::PrimitiveType op_type, + KernelRegistrar(const kernel::KERNEL_ARCH arch, const TypeId data_type, const int op_type, kernel::KernelCreator creator) { KernelRegistry::GetInstance()->RegKernel(arch, data_type, op_type, creator); } diff --git a/mindspore/lite/src/lite_kernel.cc b/mindspore/lite/src/lite_kernel.cc index 4fb2c18cd5..485c7bd58b 100644 --- a/mindspore/lite/src/lite_kernel.cc +++ b/mindspore/lite/src/lite_kernel.cc @@ -20,6 +20,8 @@ #include #include "src/tensor.h" #include "src/common/utils.h" +#include "src/runtime/infer_manager.h" +#include "src/common/version_manager.h" namespace mindspore::kernel { using mindspore::lite::RET_ERROR; @@ -87,10 +89,10 @@ int LiteKernel::FreeInWorkTensor() const { int LiteKernel::PreProcess() { if (!InferShapeDone()) { - (const_cast(primitive_))->set_infer_flag(true); - auto ret = (const_cast(primitive_))->InferShape(in_tensors_, out_tensors_); + op_parameter_->infer_flag_ = true; + auto ret = lite::KernelInferShape(in_tensors_, &out_tensors_, op_parameter_); if (ret != 0) { - (const_cast(primitive_))->set_infer_flag(false); + op_parameter_->infer_flag_ = false; MS_LOG(ERROR) << "InferShape fail!"; return ret; } diff --git a/mindspore/lite/src/lite_kernel.h b/mindspore/lite/src/lite_kernel.h index 365a40e5e9..3d7788fbe6 100644 --- a/mindspore/lite/src/lite_kernel.h +++ b/mindspore/lite/src/lite_kernel.h @@ -20,7 +20,6 @@ #include #include #include -#include "src/ops/primitive_c.h" #include "src/common/utils.h" #ifdef ENABLE_ARM #include @@ -29,6 +28,7 @@ #include "src/inner_context.h" #include "src/tensor.h" #include "include/errorcode.h" +#include "schema/model_generated.h" static constexpr int kPerTensor = 1; static constexpr size_t kPerBatch = 3; @@ -38,7 +38,7 @@ enum KERNEL_ARCH { kCPU, kGPU, kAPU, kNPU, kKernelArch_MIN = kCPU, kKernelArch_M struct KernelKey { KERNEL_ARCH arch; TypeId data_type; - schema::PrimitiveType type; + int type; bool operator<(const KernelKey &dst) const { if (arch != dst.arch) { @@ -57,11 +57,10 @@ class LiteKernel { public: LiteKernel() = default; LiteKernel(OpParameter *parameter, std::vector in_tensors, std::vector out_tensors, - const lite::InnerContext *ctx, const mindspore::lite::PrimitiveC *primitive) + const lite::InnerContext *ctx) : op_parameter_(parameter), in_tensors_(std::move(in_tensors)), out_tensors_(std::move(out_tensors)), - primitive_(primitive), context_(ctx) { if (op_parameter_ != nullptr && ctx != nullptr) { op_parameter_->thread_num_ = ctx->thread_num_; @@ -169,8 +168,6 @@ class LiteKernel { void set_desc(const KernelKey kernel_key) { desc_ = kernel_key; } - const mindspore::lite::PrimitiveC *GetPrimitive() const { return primitive_; } - SubGraphType subgraph_type() const { return this->subgraph_type_; } virtual std::string ToString() const; @@ -184,7 +181,12 @@ class LiteKernel { #endif protected: - bool InferShapeDone() { return !(primitive_ != nullptr && !primitive_->infer_flag()); } + bool InferShapeDone() { + if (op_parameter_ != nullptr) { + return op_parameter_->infer_flag_; + } + return false; + } KernelKey desc_{}; std::string name_; @@ -192,7 +194,6 @@ class LiteKernel { // tensor will free in ~lite_session() std::vector in_tensors_; std::vector out_tensors_; - const mindspore::lite::PrimitiveC *primitive_ = nullptr; const lite::InnerContext *context_ = nullptr; std::vector in_kernels_; std::vector out_kernels_; @@ -208,8 +209,7 @@ class LiteKernel { typedef LiteKernel *(*KernelCreator)(const std::vector &inputs, const std::vector &outputs, OpParameter *parameter, - const lite::InnerContext *ctx, const KernelKey &desc, - const mindspore::lite::PrimitiveC *primitive); + const lite::InnerContext *ctx, const KernelKey &desc); class LiteKernelUtil { public: @@ -231,9 +231,8 @@ class LiteKernelUtil { template kernel::LiteKernel *LiteKernelCreator(const std::vector &inputs, const std::vector &outputs, OpParameter *parameter, - const lite::InnerContext *ctx, const kernel::KernelKey &desc, - const mindspore::lite::PrimitiveC *primitive) { - auto *kernel = new (std::nothrow) T(parameter, inputs, outputs, ctx, primitive); + const lite::InnerContext *ctx, const kernel::KernelKey &desc) { + auto *kernel = new (std::nothrow) T(parameter, inputs, outputs, ctx); if (kernel == nullptr) { MS_LOG(ERROR) << "kernel: " << parameter->name_ << "is nullptr."; free(parameter); diff --git a/mindspore/lite/src/lite_model.cc b/mindspore/lite/src/lite_model.cc index 17c58cf4aa..5863459720 100644 --- a/mindspore/lite/src/lite_model.cc +++ b/mindspore/lite/src/lite_model.cc @@ -18,26 +18,28 @@ #include #include #include -#include "src/ops/while.h" +#include "src/common/prim_util.h" #ifdef ENABLE_V0 #include "src/ops/compat/compat_register.h" #endif namespace mindspore::lite { #ifdef ENABLE_V0 -int LiteModel::ConvertAttrs(Model::Node *node, const schema::v0::Primitive *prim, - std::vector *dst_tensor) { +int LiteModel::ConvertAttrs(Model::Node *node, std::vector *dst_tensor) { if (node == nullptr || dst_tensor == nullptr) { MS_LOG(ERROR) << "node or tensor_vec is nullptr."; return RET_ERROR; } + auto primitive = node->primitive_; + MS_ASSERT(primitive != nullptr); + auto prim = reinterpret_cast(primitive); int primitive_type = prim->value_type(); auto creator = CompatRegistry::GetInstance()->GetTransferAttrFunc(SCHEMA_VERSION::SCHEMA_V0, primitive_type); if (creator == nullptr) { MS_LOG(DEBUG) << "the node don't need to convert attr to tensor."; return RET_OK; } - int status = creator(reinterpret_cast(prim), node, dst_tensor, &this->attr_tensor_bufs_); + int status = creator(node, dst_tensor, &this->attr_tensor_bufs_); if (status != RET_OK && status != RET_NO_CHANGE) { MS_LOG(ERROR) << "translate attr to tensor failed."; return status; @@ -45,14 +47,12 @@ int LiteModel::ConvertAttrs(Model::Node *node, const schema::v0::Primitive *prim return RET_OK; } -int LiteModel::ConvertAttrToTensors(const void *meta_graph) { - MS_ASSERT(meta_graph != nullptr); +int LiteModel::ConvertAttrToTensors() { int schema_version = VersionManager::GetInstance()->GetSchemaVersion(); if (schema_version != SCHEMA_VERSION::SCHEMA_V0) { MS_LOG(DEBUG) << "no need to convert attr to tensor."; return RET_OK; } - auto meta_graph_v0 = reinterpret_cast(meta_graph); std::unordered_map> subgraph_node_indexes; for (size_t subgraph_index = 0; subgraph_index < this->sub_graphs_.size(); ++subgraph_index) { for (size_t node_index = 0; node_index < this->sub_graphs_[subgraph_index]->node_indices_.size(); ++node_index) { @@ -62,9 +62,7 @@ int LiteModel::ConvertAttrToTensors(const void *meta_graph) { int cur_all_tensors_size = this->all_tensors_.size(); for (size_t index = 0; index < this->all_nodes_.size(); ++index) { std::vector dst_tensors; - auto prim = meta_graph_v0->nodes()->GetAs(index)->primitive(); - MS_ASSERT(prim != nullptr); - int status = ConvertAttrs(this->all_nodes_[index], prim, &dst_tensors); + int status = ConvertAttrs(this->all_nodes_[index], &dst_tensors); if (status != RET_OK) { MS_LOG(ERROR) << "fail to convert attr to tensor."; return RET_ERROR; @@ -96,6 +94,11 @@ void LiteModel::Free() { free(this->buf); this->buf = nullptr; } + auto nodes_size = this->all_nodes_.size(); + for (size_t i = 0; i < nodes_size; ++i) { + auto node = this->all_nodes_[i]; + node->primitive_ = nullptr; + } for (auto &tensor_buf : attr_tensor_bufs_) { free(tensor_buf); tensor_buf = nullptr; @@ -109,9 +112,6 @@ void LiteModel::Destroy() { for (size_t i = 0; i < nodes_size; ++i) { auto node = this->all_nodes_[i]; MS_ASSERT(node != nullptr); - MS_ASSERT(node->primitive_ != nullptr); - delete node->primitive_; - node->primitive_ = nullptr; delete node; } this->all_nodes_.clear(); @@ -193,15 +193,10 @@ int LiteModel::NodeVerify() const { return RET_ERROR; } - auto prim = node->primitive_; - if (prim->Type() == schema::PrimitiveType_While) { - auto whileOp = reinterpret_cast(const_cast(prim)); - if (whileOp == nullptr) { - MS_LOG(ERROR) << "whileOp is null."; - return RET_ERROR; - } - if (static_cast(whileOp->GetBodySubgraphIndex()) >= subGraph_size || - static_cast(whileOp->GetCondSubgraphIndex()) >= subGraph_size) { + if (IsWhileNode(node->primitive_)) { + auto body_index = GetWhileBodySubgraphIndex(node->primitive_); + auto cond_index = GetWhileCondSubgraphIndex(node->primitive_); + if (static_cast(body_index) >= subGraph_size || static_cast(cond_index) >= subGraph_size) { MS_LOG(ERROR) << "index of subGraph is beyond subGraph_size."; return RET_ERROR; } diff --git a/mindspore/lite/src/lite_model.h b/mindspore/lite/src/lite_model.h index 02af51f19c..7885e27da7 100644 --- a/mindspore/lite/src/lite_model.h +++ b/mindspore/lite/src/lite_model.h @@ -19,15 +19,12 @@ #include #include +#include "include/errorcode.h" #include "include/model.h" -#include "src/ops/primitive_c.h" #include "include/version.h" #include "schema/model_generated.h" #include "src/common/common.h" #include "src/common/version_manager.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif #ifdef ENABLE_V0 #include "schema/model_v0_generated.h" #endif @@ -48,9 +45,9 @@ class LiteModel : public Model { private: #ifdef ENABLE_V0 - int ConvertAttrs(Model::Node *node, const schema::v0::Primitive *prim, std::vector *dst_tensor); + int ConvertAttrs(Model::Node *node, std::vector *dst_tensor); - int ConvertAttrToTensors(const void *meta_graph); + int ConvertAttrToTensors(); #endif template @@ -66,31 +63,10 @@ class LiteModel : public Model { return false; } auto c_node = meta_graph.nodes()->template GetAs(i); - MS_ASSERT(c_node != nullptr); - auto src_prim = reinterpret_cast(c_node->primitive()); -#ifdef PRIMITIVE_WRITEABLE - node->primitive_ = PrimitiveC::Create(const_cast(src_prim)); -#else - auto primitive = const_cast(src_prim); - auto func_pointer = OpsRegistry::GetInstance()->GetPrimitiveCreator(primitive->value_type()); - if (func_pointer == nullptr) { - MS_LOG(ERROR) << "PrimitiveCreator function pointer is nullptr, type: " - << schema::EnumNamePrimitiveType(primitive->value_type()); - delete node; - return false; - } - node->primitive_ = func_pointer(primitive); -#endif - if (node->primitive_ == nullptr) { - MS_LOG(ERROR) << "unpack primitive == nullptr!"; - delete node; - return false; - } - node->primitive_->set_quant_type(static_cast(c_node->quantType())); - MS_ASSERT(c_node->name() != nullptr); + node->primitive_ = c_node->primitive(); + node->quant_type_ = c_node->quantType(); node->name_ = c_node->name()->c_str(); node->node_type_ = static_cast(c_node->nodeType()); - MS_ASSERT(c_node->inputIndex() != nullptr); auto count = c_node->inputIndex()->size(); for (uint32_t j = 0; j < count; ++j) { node->input_indices_.push_back(size_t(c_node->inputIndex()->template GetAs(j))); @@ -195,7 +171,7 @@ class LiteModel : public Model { } } #ifdef ENABLE_V0 - if (ConvertAttrToTensors(&meta_graph) != RET_OK) { + if (ConvertAttrToTensors() != RET_OK) { MS_LOG(ERROR) << "fail to convert attr to tensor."; return RET_ERROR; } diff --git a/mindspore/lite/src/lite_session.cc b/mindspore/lite/src/lite_session.cc index 6bbdee5f63..4875231935 100644 --- a/mindspore/lite/src/lite_session.cc +++ b/mindspore/lite/src/lite_session.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2020-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ #include "src/runtime/allocator.h" #include "src/executor.h" #include "src/common/utils.h" +#include "src/common/prim_util.h" #include "src/common/graph_util.h" #include "src/kernel_registry.h" #include "src/lite_model.h" @@ -52,7 +53,7 @@ static bool WeightTensorNeedCopy(const lite::Model *model, const uint32_t tensor return std::none_of(post_node_idxes.begin(), post_node_idxes.end(), [&](const size_t &post_node_idx) { auto node = model->all_nodes_[post_node_idx]; MS_ASSERT(node != nullptr); - return IsPackedOp(static_cast(node->primitive_->Type())); + return IsPackedOp(GetPrimitiveType(node->primitive_)); }); } diff --git a/mindspore/lite/src/ops/CMakeLists.txt b/mindspore/lite/src/ops/CMakeLists.txt index 27bb7067ec..465d5296fc 100644 --- a/mindspore/lite/src/ops/CMakeLists.txt +++ b/mindspore/lite/src/ops/CMakeLists.txt @@ -6,7 +6,8 @@ file(GLOB OPS_SRC ) if(ENABLE_V0) file(GLOB_RECURSE COMPAT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/compat/*.cc) - set(OPS_SRC ${OPS_SRC} ${COMPAT_SRC}) + file(GLOB OPS_SRC_V0 ${CMAKE_CURRENT_SOURCE_DIR}/populate/v0/*.cc) + set(OPS_SRC ${OPS_SRC} ${COMPAT_SRC} ${OPS_SRC_V0}) endif() add_library(cpu_ops_mid OBJECT ${OPS_SRC}) diff --git a/mindspore/lite/src/ops/abs.cc b/mindspore/lite/src/ops/abs.cc deleted file mode 100644 index 8b4ccdae2c..0000000000 --- a/mindspore/lite/src/ops/abs.cc +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/abs.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Abs::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Abs; - } - if (this->primitive_->value.type != schema::PrimitiveType_Abs) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AbsT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int Abs::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateAbs(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Abs, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *AbsCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AbsRegistry(schema::PrimitiveType_Abs, AbsCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/abs.h b/mindspore/lite/src/ops/abs.h deleted file mode 100644 index f985351177..0000000000 --- a/mindspore/lite/src/ops/abs.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include - -#include "src/ops/arithmetic_self.h" - -#ifndef MINDSPORE_LITE_SRC_OPS_ABS_H_ -#define MINDSPORE_LITE_SRC_OPS_ABS_H_ - -namespace mindspore { -namespace lite { -class Abs : public ArithmeticSelf { - public: - Abs() = default; - ~Abs() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Abs, ArithmeticSelf); - explicit Abs(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore -#endif // MINDSPORE_LITE_SRC_OPS_ABS_H_ diff --git a/mindspore/lite/src/ops/abs_grad.cc b/mindspore/lite/src/ops/abs_grad.cc deleted file mode 100644 index 3ca69a667c..0000000000 --- a/mindspore/lite/src/ops/abs_grad.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/abs_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int AbsGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_AbsGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_AbsGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AbsGradT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int AbsGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(primitive != nullptr); - MS_ASSERT(fbb != nullptr); - auto attr = primitive->value_as_AbsGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_AbsGrad return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAbsGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_AbsGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AbsGradCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AbsGradRegistry(schema::PrimitiveType_AbsGrad, AbsGradCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/abs_grad.h b/mindspore/lite/src/ops/abs_grad.h deleted file mode 100644 index 763c31c800..0000000000 --- a/mindspore/lite/src/ops/abs_grad.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ABS_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_ABS_GRAD_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class AbsGrad : public PrimitiveC { - public: - AbsGrad() = default; - ~AbsGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(AbsGrad, PrimitiveC); - explicit AbsGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore -#endif // MINDSPORE_LITE_SRC_OPS_ABS_GRAD_H_ diff --git a/mindspore/lite/src/ops/activation.cc b/mindspore/lite/src/ops/activation.cc deleted file mode 100644 index e959d8cb8e..0000000000 --- a/mindspore/lite/src/ops/activation.cc +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/activation.h" -#include -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Activation::GetType() const { return this->primitive_->value.AsActivation()->type; } -float Activation::GetAlpha() const { return this->primitive_->value.AsActivation()->alpha; } -float Activation::GetMinVal() const { return this->primitive_->value.AsActivation()->min_val; } -float Activation::GetMaxVal() const { return this->primitive_->value.AsActivation()->max_val; } - -void Activation::SetType(int type) { this->primitive_->value.AsActivation()->type = (schema::ActivationType)type; } -void Activation::SetAlpha(float alpha) { this->primitive_->value.AsActivation()->alpha = alpha; } -void Activation::SetMinVal(float min_val) { this->primitive_->value.AsActivation()->min_val = min_val; } -void Activation::SetMaxVal(float max_val) { this->primitive_->value.AsActivation()->max_val = max_val; } - -int Activation::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Activation; - } - if (this->primitive_->value.type != schema::PrimitiveType_Activation) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - auto attr = std::make_unique(); - if (prim.name() == "ReLU") { - attr->type = schema::ActivationType_RELU; - } else if (prim.name() == "Sigmoid") { - attr->type = schema::ActivationType_SIGMOID; - } else if (prim.name() == "ReLU6") { - attr->type = schema::ActivationType_RELU6; - } else if (prim.name() == "Swish") { - attr->type = schema::ActivationType_SWISH; - } else if (prim.name() == "HSwish") { - attr->type = schema::ActivationType_HSWISH; - } else if (prim.name() == "HSigmoid") { - attr->type = schema::ActivationType_HSIGMOID; - } else if (prim.name() == "Tanh") { - attr->type = schema::ActivationType_TANH; - } - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - return RET_OK; -} -#else -int Activation::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Activation(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Activation return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateActivation(*fbb, attr->type(), attr->alpha(), attr->min_val(), attr->max_val()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Activation, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Activation::GetType() const { return this->primitive_->value_as_Activation()->type(); } -float Activation::GetAlpha() const { return this->primitive_->value_as_Activation()->alpha(); } -float Activation::GetMinVal() const { return this->primitive_->value_as_Activation()->min_val(); } -float Activation::GetMaxVal() const { return this->primitive_->value_as_Activation()->max_val(); } - -PrimitiveC *ActivationCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ActivationRegistry(schema::PrimitiveType_Activation, ActivationCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/activation.h b/mindspore/lite/src/ops/activation.h deleted file mode 100644 index 7157248bf6..0000000000 --- a/mindspore/lite/src/ops/activation.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Activation : public PrimitiveC { - public: - Activation() = default; - ~Activation() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Activation, PrimitiveC); - explicit Activation(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetType(int type); - void SetAlpha(float alpha); - void SetMinVal(float minVal); - void SetMaxVal(float maxVal); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetType() const; - float GetAlpha() const; - float GetMinVal() const; - float GetMaxVal() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ diff --git a/mindspore/lite/src/ops/activation_grad.cc b/mindspore/lite/src/ops/activation_grad.cc deleted file mode 100644 index ac4f093fc8..0000000000 --- a/mindspore/lite/src/ops/activation_grad.cc +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/activation_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ActivationGrad::GetType() const { return this->primitive_->value.AsActivationGrad()->type; } -float ActivationGrad::GetAlpha() const { return this->primitive_->value.AsActivationGrad()->alpha; } -void ActivationGrad::SetType(int type) { - this->primitive_->value.AsActivationGrad()->type = (schema::ActivationType)type; -} -void ActivationGrad::SetAlpha(float alpha) { this->primitive_->value.AsActivationGrad()->alpha = alpha; } -int ActivationGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ActivationGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_ActivationGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - auto attr = std::make_unique(); - if (prim.name() == "ReluGrad") { - attr->type = schema::ActivationType_RELU; - } else if (prim.name() == "SigmoidGrad") { - attr->type = schema::ActivationType_SIGMOID; - } else if (prim.name() == "ReLU6Grad") { - attr->type = schema::ActivationType_RELU6; - } else if (prim.name() == "HSigmoidGrad") { - attr->type = schema::ActivationType_HSIGMOID; - } else if (prim.name() == "HSwishGrad") { - attr->type = schema::ActivationType_HSWISH; - } - attr->alpha = 0; // alpha; - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - return RET_OK; -} -#else -int ActivationGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ActivationGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ActivationGrad return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateActivationGrad(*fbb, attr->type(), attr->alpha()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ActivationGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int ActivationGrad::GetType() const { return this->primitive_->value_as_ActivationGrad()->type(); } -float ActivationGrad::GetAlpha() const { return this->primitive_->value_as_ActivationGrad()->alpha(); } - -PrimitiveC *ActivationGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ActivationGradRegistry(schema::PrimitiveType_ActivationGrad, ActivationGradCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/activation_grad.h b/mindspore/lite/src/ops/activation_grad.h deleted file mode 100644 index c6c6181efc..0000000000 --- a/mindspore/lite/src/ops/activation_grad.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ACTIVATION_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_ACTIVATION_GRAD_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ActivationGrad : public PrimitiveC { - public: - ActivationGrad() = default; - ~ActivationGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ActivationGrad, PrimitiveC); - explicit ActivationGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetType(int type); - void SetAlpha(float alpha); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetType() const; - float GetAlpha() const; -}; -} // namespace lite -} // namespace mindspore -#endif // MINDSPORE_LITE_SRC_OPS_ACTIVATION_GRAD_H_ diff --git a/mindspore/lite/src/ops/adam.cc b/mindspore/lite/src/ops/adam.cc deleted file mode 100644 index ed2cc49c9f..0000000000 --- a/mindspore/lite/src/ops/adam.cc +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/adam.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool Adam::GetUseNesterov() const { return this->primitive_->value.AsAdam()->useNesterov; } -int Adam::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Adam; - } - if (this->primitive_->value.type != schema::PrimitiveType_Adam) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = std::make_unique(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->useNesterov = GetValue(prim.GetAttr("use_nesterov")); - - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -bool Adam::GetUseNesterov() const { return this->primitive_->value_as_Adam()->useNesterov(); } -int Adam::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Adam(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Adam return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAdam(*fbb, attr->useNesterov()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Adam, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AdamCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AdamRegistry(schema::PrimitiveType_Adam, AdamCreator); -#endif - -int Adam::InferShape(std::vector inputs, std::vector outputs) { - if (10 != inputs.size()) { - MS_LOG(ERROR) << "Adam should have 10 input tensors"; - return RET_ERROR; - } - - if (inputs[0]->ElementsNum() != inputs[1]->ElementsNum() || inputs[0]->ElementsNum() != inputs[2]->ElementsNum() || - inputs[0]->ElementsNum() != inputs[9]->ElementsNum() || inputs[3]->ElementsNum() != 1 || - inputs[4]->ElementsNum() != 1 || inputs[5]->ElementsNum() != 1 || inputs[6]->ElementsNum() != 1 || - inputs[7]->ElementsNum() != 1 || inputs[8]->ElementsNum() != 1) { - MS_LOG(ERROR) << "error input data size!"; - return RET_ERROR; - } - if (!outputs.empty()) { - auto *out = outputs.front(); - MS_ASSERT(out != nullptr); - out->set_data_type(inputs[0]->data_type()); - out->set_format(inputs[0]->format()); - out->set_shape({1}); - } - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/adam.h b/mindspore/lite/src/ops/adam.h deleted file mode 100644 index 6258da7d40..0000000000 --- a/mindspore/lite/src/ops/adam.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ADAM_H_ -#define MINDSPORE_LITE_SRC_OPS_ADAM_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Adam : public PrimitiveC { - public: - Adam() = default; - ~Adam() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Adam, PrimitiveC); - explicit Adam(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool GetUseNesterov() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_ADAM_H_ diff --git a/mindspore/lite/src/ops/add.cc b/mindspore/lite/src/ops/add.cc deleted file mode 100644 index 8661180456..0000000000 --- a/mindspore/lite/src/ops/add.cc +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/add.h" -#include -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Add::GetActivationType() const { return this->primitive_->value.AsAdd()->activationType; } - -void Add::SetActivationType(int activation_type) { - this->primitive_->value.AsAdd()->activationType = (schema::ActivationType)activation_type; -} - -int Add::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Add; - } - if (this->primitive_->value.type != schema::PrimitiveType_Add) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AddT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int Add::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Add(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Add return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAdd(*fbb, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Add, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Add::GetActivationType() const { return this->primitive_->value_as_Add()->activationType(); } - -PrimitiveC *AddCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AddRegistry(schema::PrimitiveType_Add, AddCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/add.h b/mindspore/lite/src/ops/add.h deleted file mode 100644 index 4bb4cddf77..0000000000 --- a/mindspore/lite/src/ops/add.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ADD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ADD_H_ - -#include -#include -#include -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Add : public Arithmetic { - public: - Add() = default; - ~Add() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Add, Arithmetic); - explicit Add(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetActivationType(int activation_type); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ADD_H_ diff --git a/mindspore/lite/src/ops/adder.cc b/mindspore/lite/src/ops/adder.cc deleted file mode 100644 index 6320c48cb9..0000000000 --- a/mindspore/lite/src/ops/adder.cc +++ /dev/null @@ -1,185 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/adder.h" -#include -#include - -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Adder::GetFormat() const { return this->primitive_->value.AsAdder()->format; } -int Adder::GetGroup() const { return this->primitive_->value.AsAdder()->group; } -int Adder::GetChannelIn() const { return this->primitive_->value.AsAdder()->channelIn; } -int Adder::GetChannelOut() const { return this->primitive_->value.AsAdder()->channelOut; } -int Adder::GetKernelW() const { return this->primitive_->value.AsAdder()->kernelW; } -int Adder::GetKernelH() const { return this->primitive_->value.AsAdder()->kernelH; } -int Adder::GetStrideW() const { return this->primitive_->value.AsAdder()->strideW; } -int Adder::GetStrideH() const { return this->primitive_->value.AsAdder()->strideH; } -int Adder::GetPadMode() const { return this->primitive_->value.AsAdder()->padMode; } -int Adder::GetPadUp() const { return this->primitive_->value.AsAdder()->padUp; } -int Adder::GetPadDown() const { return this->primitive_->value.AsAdder()->padDown; } -int Adder::GetPadLeft() const { return this->primitive_->value.AsAdder()->padLeft; } -int Adder::GetPadRight() const { return this->primitive_->value.AsAdder()->padRight; } -int Adder::GetDilateW() const { return this->primitive_->value.AsAdder()->dilateW; } -int Adder::GetDilateH() const { return this->primitive_->value.AsAdder()->dilateH; } -int Adder::GetActivationType() const { return this->primitive_->value.AsAdder()->activationType; } - -void Adder::SetFormat(int format) { this->primitive_->value.AsAdder()->format = (schema::Format)format; } -void Adder::SetGroup(int group) { this->primitive_->value.AsAdder()->group = group; } -void Adder::SetChannelIn(int channel_in) { this->primitive_->value.AsAdder()->channelIn = channel_in; } -void Adder::SetChannelOut(int channel_out) { this->primitive_->value.AsAdder()->channelOut = channel_out; } -void Adder::SetKernelW(int kernel_w) { this->primitive_->value.AsAdder()->kernelW = kernel_w; } -void Adder::SetKernelH(int kernel_h) { this->primitive_->value.AsAdder()->kernelH = kernel_h; } -void Adder::SetStrideW(int stride_w) { this->primitive_->value.AsAdder()->strideW = stride_w; } -void Adder::SetStrideH(int stride_h) { this->primitive_->value.AsAdder()->strideH = stride_h; } -void Adder::SetPadMode(int pad_mode) { this->primitive_->value.AsAdder()->padMode = (schema::PadMode)pad_mode; } -void Adder::SetPadUp(int pad_up) { this->primitive_->value.AsAdder()->padUp = pad_up; } -void Adder::SetPadDown(int pad_down) { this->primitive_->value.AsAdder()->padDown = pad_down; } -void Adder::SetPadLeft(int pad_left) { this->primitive_->value.AsAdder()->padLeft = pad_left; } -void Adder::SetPadRight(int pad_right) { this->primitive_->value.AsAdder()->padRight = pad_right; } -void Adder::SetDilateW(int dilate_w) { this->primitive_->value.AsAdder()->dilateW = dilate_w; } -void Adder::SetDilateH(int dilate_h) { this->primitive_->value.AsAdder()->dilateH = dilate_h; } -void Adder::SetActivationType(int activation_type) { - this->primitive_->value.AsAdder()->activationType = (schema::ActivationType)activation_type; -} - -void Adder::PopulaterAdderSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group) { - auto attr = std::make_unique(); - attr->group = group; - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation[2]; - attr->dilateW = dilation[3]; - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride[2]; - attr->strideW = stride[3]; - - attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - auto activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - primitive->value.type = schema::PrimitiveType_Adder; - primitive->value.value = attr.release(); -} - -int Adder::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Adder; - } - if (this->primitive_->value.type != schema::PrimitiveType_Adder) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - auto groupAttr = prim.GetAttr("group"); - if (groupAttr == nullptr) { - MS_LOG(ERROR) << "conv2d op has no group attr,please check pb model"; - return RET_NULL_PTR; - } - int group = CastToInt(groupAttr).front(); - PopulaterAdderSingleGroup(prim, this->primitive_, group); - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int Adder::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Adder(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Adder return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateAdder(*fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), - attr->kernelW(), attr->kernelH(), attr->strideW(), attr->strideH(), - attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Adder, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int Adder::GetFormat() const { return this->primitive_->value_as_Adder()->format(); } -int Adder::GetGroup() const { return this->primitive_->value_as_Adder()->group(); } -int Adder::GetChannelIn() const { return this->primitive_->value_as_Adder()->channelIn(); } -int Adder::GetChannelOut() const { return this->primitive_->value_as_Adder()->channelOut(); } -int Adder::GetKernelW() const { return this->primitive_->value_as_Adder()->kernelW(); } -int Adder::GetKernelH() const { return this->primitive_->value_as_Adder()->kernelH(); } -int Adder::GetStrideW() const { return this->primitive_->value_as_Adder()->strideW(); } -int Adder::GetStrideH() const { return this->primitive_->value_as_Adder()->strideH(); } -int Adder::GetPadMode() const { return this->primitive_->value_as_Adder()->padMode(); } -int Adder::GetPadUp() const { return this->primitive_->value_as_Adder()->padUp(); } -int Adder::GetPadDown() const { return this->primitive_->value_as_Adder()->padDown(); } -int Adder::GetPadLeft() const { return this->primitive_->value_as_Adder()->padLeft(); } -int Adder::GetPadRight() const { return this->primitive_->value_as_Adder()->padRight(); } -int Adder::GetDilateW() const { return this->primitive_->value_as_Adder()->dilateW(); } -int Adder::GetDilateH() const { return this->primitive_->value_as_Adder()->dilateH(); } -int Adder::GetActivationType() const { return this->primitive_->value_as_Adder()->activationType(); } - -PrimitiveC *AdderCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AdderRegistry(schema::PrimitiveType_Adder, AdderCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/adder.h b/mindspore/lite/src/ops/adder.h deleted file mode 100644 index 1e1d33830b..0000000000 --- a/mindspore/lite/src/ops/adder.h +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ - -#include -#include -#include -#include -#include "src/ops/conv2d.h" - -namespace mindspore { -namespace lite { -class Adder : public Conv2D { - public: - Adder() = default; - ~Adder() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Adder, Conv2D); - explicit Adder(schema::PrimitiveT *primitive) : Conv2D(primitive) {} - - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetFormat(int format); - void SetGroup(int group); - void SetChannelIn(int channel_in); - void SetChannelOut(int channel_out); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); - - private: - void PopulaterAdderSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb); -#endif - - public: - int GetFormat() const; - int GetGroup() const; - int GetChannelIn() const; - int GetChannelOut() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ADDER_H_ diff --git a/mindspore/lite/src/ops/addn.cc b/mindspore/lite/src/ops/addn.cc deleted file mode 100644 index 26e244cb28..0000000000 --- a/mindspore/lite/src/ops/addn.cc +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/addn.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int AddN::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_AddN; - } - if (this->primitive_->value.type != schema::PrimitiveType_AddN) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AddNT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int AddN::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateAddN(*fbb, 0); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_AddN, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AddNCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AddNRegistry(schema::PrimitiveType_AddN, AddNCreator); -#endif - -namespace { -constexpr int kLeastInputNum = 2; -} -int AddN::InferShape(std::vector inputs, std::vector outputs) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs.front(); - MS_ASSERT(input != nullptr); - auto output = outputs.front(); - MS_ASSERT(output != nullptr); - if (inputs.size() < kLeastInputNum) { - MS_LOG(ERROR) << "input size" << inputs.size() << " is error!"; - return RET_INPUT_TENSOR_ERROR; - } - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - size_t max_dims = inputs.at(0)->shape().size(); - size_t max_dims_idx = 0; - - // determine max_dims - for (size_t i = 1; i < inputs.size(); ++i) { - if (inputs.at(i)->shape().size() > max_dims) { - max_dims = inputs.at(i)->shape().size(); - max_dims_idx = i; - } - } - - output->set_shape(inputs.at(max_dims_idx)->shape()); - - // make sure all elements have the same size or 1 (broadcasting) in all dimensions - for (size_t i = 1; i < inputs.size(); ++i) { - if ((inputs.at(i)->shape().size() != max_dims) && - (inputs.at(i)->ElementsNum() != inputs.at(max_dims_idx)->ElementsNum())) { - MS_LOG(ERROR) << "AddN inputs shape is not equal!"; - return RET_INPUT_TENSOR_ERROR; - } - if (inputs.at(i)->data_type() != inputs.at(0)->data_type()) { - MS_LOG(ERROR) << "AddN all input data type should be the same!"; - return RET_INPUT_TENSOR_ERROR; - } - } - - for (size_t d = 0; d < input->shape().size(); ++d) { - size_t max_dim = 0; - for (size_t i = 0; i < inputs.size(); ++i) { - size_t shift = max_dims - inputs.at(i)->shape().size(); - size_t dim = (i < shift) ? 1 : inputs.at(i)->shape().at(d); - if (dim > max_dim) { - max_dim = dim; - } - } - output->shape()[d] = max_dim; // set the biggest dimension in the output tensor - } - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/addn.h b/mindspore/lite/src/ops/addn.h deleted file mode 100644 index 6d25bb8a9b..0000000000 --- a/mindspore/lite/src/ops/addn.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ADDN_H_ -#define MINDSPORE_LITE_SRC_OPS_ADDN_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class AddN : public PrimitiveC { - public: - AddN() = default; - ~AddN() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(AddN, PrimitiveC); - explicit AddN(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_ADDN_H_ diff --git a/mindspore/lite/src/ops/apply_momentum.cc b/mindspore/lite/src/ops/apply_momentum.cc deleted file mode 100644 index e38e032efc..0000000000 --- a/mindspore/lite/src/ops/apply_momentum.cc +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/apply_momentum.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float ApplyMomentum::GetGradientScale() const { return this->primitive_->value.AsApplyMomentum()->gradientScale; } -bool ApplyMomentum::GetUseNesterov() const { return this->primitive_->value.AsApplyMomentum()->useNesterov; } - -int ApplyMomentum::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ApplyMomentum; - } - if (this->primitive_->value.type != schema::PrimitiveType_ApplyMomentum) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = std::make_unique(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->gradientScale = GetValue(prim.GetAttr("gradient_scale")); - attr->useNesterov = GetValue(prim.GetAttr("use_nesterov")); - - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -float ApplyMomentum::GetGradientScale() const { return this->primitive_->value_as_ApplyMomentum()->gradientScale(); } -bool ApplyMomentum::GetUseNesterov() const { return this->primitive_->value_as_ApplyMomentum()->useNesterov(); } - -int ApplyMomentum::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ApplyMomentum(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ApplyMomentum return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateApplyMomentum(*fbb, attr->gradientScale(), attr->useNesterov()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ApplyMomentum, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ApplyMomentumCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ApplyMomentumRegistry(schema::PrimitiveType_ApplyMomentum, ApplyMomentumCreator); -#endif - -int ApplyMomentum::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() != 5) { - MS_LOG(ERROR) << "ApplyMomentum should have at least 5 input tensors"; - return RET_ERROR; - } - - if (inputs[0]->ElementsNum() != inputs[1]->ElementsNum() || inputs[0]->ElementsNum() != inputs[3]->ElementsNum() || - inputs[2]->ElementsNum() != 1 || inputs[4]->ElementsNum() != 1) { - MS_LOG(ERROR) << "error input data size!"; - return RET_ERROR; - } - if (!outputs.empty()) { - auto *out = outputs.front(); - MS_ASSERT(out != nullptr); - out->set_data_type(inputs[0]->data_type()); - out->set_format(inputs[0]->format()); - out->set_shape({1}); - } - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/apply_momentum.h b/mindspore/lite/src/ops/apply_momentum.h deleted file mode 100644 index 0d9454018a..0000000000 --- a/mindspore/lite/src/ops/apply_momentum.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_APPLY_MOMENTUM_H_ -#define MINDSPORE_LITE_SRC_OPS_APPLY_MOMENTUM_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ApplyMomentum : public PrimitiveC { - public: - ApplyMomentum() = default; - ~ApplyMomentum() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ApplyMomentum, PrimitiveC); - explicit ApplyMomentum(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetGradientScale() const; - bool GetUseNesterov() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_APPLY_MOMENTUM_H_ diff --git a/mindspore/lite/src/ops/argmax.cc b/mindspore/lite/src/ops/argmax.cc deleted file mode 100644 index b189b3d1d6..0000000000 --- a/mindspore/lite/src/ops/argmax.cc +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/argmax.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ArgMax::GetAxis() const { return this->primitive_->value.AsArgMax()->axis; } -bool ArgMax::GetOutMaxValue() const { return this->primitive_->value.AsArgMax()->outMaxValue; } -int ArgMax::GetTopK() const { return this->primitive_->value.AsArgMax()->topK; } -bool ArgMax::GetKeepDims() const { return this->primitive_->value.AsArgMax()->keepDims; } -int ArgMax::GetAxisType() const { return this->primitive_->value.AsArgMax()->axisType; } - -void ArgMax::SetAxis(int axis) { this->primitive_->value.AsArgMax()->axis = axis; } -void ArgMax::SetOutMaxValue(bool out_max_value) { this->primitive_->value.AsArgMax()->outMaxValue = out_max_value; } -void ArgMax::SetTopK(int top_k) { this->primitive_->value.AsArgMax()->topK = top_k; } -void ArgMax::SetKeepDims(bool keep_dims) { this->primitive_->value.AsArgMax()->keepDims = keep_dims; } -void ArgMax::SetAxisType(int axis_type) { this->primitive_->value.AsArgMax()->axisType = axis_type; } -int ArgMax::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ArgMax; - } - if (this->primitive_->value.type != schema::PrimitiveType_ArgMax) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto argmax_attr = new (std::nothrow) schema::ArgMaxT(); - if (argmax_attr == nullptr) { - MS_LOG(ERROR) << "new primitive value.value error"; - return RET_ERROR; - } - if (prim.GetAttr("axis") != nullptr) { - argmax_attr->axis = static_cast(GetValue(prim.GetAttr("axis"))); - } - if (prim.GetAttr("keep_dims") != nullptr) { - argmax_attr->keepDims = static_cast(GetValue(prim.GetAttr("keep_dims"))); - } - argmax_attr->outMaxValue = false; - this->primitive_->value.value = argmax_attr; - } - return RET_OK; -} -#else -int ArgMax::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ArgMax(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ArgMax return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreateArgMax(*fbb, attr->axis(), attr->outMaxValue(), attr->topK(), attr->keepDims(), attr->axisType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ArgMax, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int ArgMax::GetAxis() const { return this->primitive_->value_as_ArgMax()->axis(); } -bool ArgMax::GetOutMaxValue() const { return this->primitive_->value_as_ArgMax()->outMaxValue(); } -int ArgMax::GetTopK() const { return this->primitive_->value_as_ArgMax()->topK(); } -bool ArgMax::GetKeepDims() const { return this->primitive_->value_as_ArgMax()->keepDims(); } -int ArgMax::GetAxisType() const { return this->primitive_->value_as_ArgMax()->axisType(); } - -PrimitiveC *ArgMaxCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ArgMaxRegistry(schema::PrimitiveType_ArgMax, ArgMaxCreator); -#endif - -int ArgMax::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (inputs_.size() != kSingleNum || outputs_.size() > kDoubleNum) { - MS_LOG(ERROR) << "tensor number is error."; - return RET_ERROR; - } - - output->set_format(input->format()); - if (GetOutMaxValue() && outputs_.size() == kSingleNum) { - output->set_data_type(input->data_type()); - } else { - output->set_data_type(kNumberTypeInt32); - } - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector output_shape(input->shape()); - auto input_shape_size = input->shape().size(); - auto axis = GetAxis() < 0 ? GetAxis() + input_shape_size : GetAxis(); - if (axis >= input_shape_size || axis < 0) { - MS_LOG(ERROR) << "Invalid axis " << GetAxis() << ", input shape size: " << input_shape_size; - return RET_PARAM_INVALID; - } - if (GetTopK() == 1 && !GetKeepDims()) { - output_shape.erase(output_shape.begin() + axis); - } else { - output_shape[axis] = GetTopK(); - } - - output->set_shape(output_shape); - if (outputs_.size() == kDoubleNum) { - outputs_.at(1)->set_format(input->format()); - outputs_.at(1)->set_data_type(input->data_type()); - outputs_.at(1)->set_shape(output_shape); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/argmax.h b/mindspore/lite/src/ops/argmax.h deleted file mode 100644 index d208c2b60a..0000000000 --- a/mindspore/lite/src/ops/argmax.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ArgMax : public PrimitiveC { - public: - ArgMax() = default; - ~ArgMax() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ArgMax, PrimitiveC); - explicit ArgMax(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int axis); - void SetOutMaxValue(bool out_max_value); - void SetTopK(int top_k); - void SetKeepDims(bool keep_dims); - void SetAxisType(int axis_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetAxis() const; - bool GetOutMaxValue() const; - int GetTopK() const; - bool GetKeepDims() const; - int GetAxisType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ diff --git a/mindspore/lite/src/ops/argmin.cc b/mindspore/lite/src/ops/argmin.cc deleted file mode 100644 index cf9e7d5d4e..0000000000 --- a/mindspore/lite/src/ops/argmin.cc +++ /dev/null @@ -1,134 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/argmin.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ArgMin::GetAxis() const { return this->primitive_->value.AsArgMin()->axis; } -bool ArgMin::GetOutMaxValue() const { return this->primitive_->value.AsArgMin()->outMaxValue; } -int ArgMin::GetTopK() const { return this->primitive_->value.AsArgMin()->topK; } -bool ArgMin::GetKeepDims() const { return this->primitive_->value.AsArgMin()->keepDims; } -int ArgMin::GetAxisType() const { return this->primitive_->value.AsArgMin()->axisType; } - -void ArgMin::SetAxis(int axis) { this->primitive_->value.AsArgMin()->axis = axis; } -void ArgMin::SetOutMaxValue(bool out_max_value) { this->primitive_->value.AsArgMin()->outMaxValue = out_max_value; } -void ArgMin::SetTopK(int top_k) { this->primitive_->value.AsArgMin()->topK = top_k; } -void ArgMin::SetKeepDims(bool keep_dims) { this->primitive_->value.AsArgMin()->keepDims = keep_dims; } -void ArgMin::SetAxisType(int axis_type) { this->primitive_->value.AsArgMin()->axisType = axis_type; } - -int ArgMin::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ArgMin; - } - if (this->primitive_->value.type != schema::PrimitiveType_ArgMin) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ArgMinT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (prim.GetAttr("axis") != nullptr) { - attr->axis = static_cast(GetValue(prim.GetAttr("axis"))); - } - if (prim.GetAttr("keep_dims") != nullptr) { - attr->keepDims = static_cast(GetValue(prim.GetAttr("keep_dims"))); - } - attr->outMaxValue = false; - } - return RET_OK; -} - -#else -int ArgMin::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ArgMin(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ArgMin return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreateArgMin(*fbb, attr->axis(), attr->outMaxValue(), attr->topK(), attr->keepDims(), attr->axisType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ArgMin, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int ArgMin::GetAxis() const { return this->primitive_->value_as_ArgMin()->axis(); } -bool ArgMin::GetOutMaxValue() const { return this->primitive_->value_as_ArgMin()->outMaxValue(); } -int ArgMin::GetTopK() const { return this->primitive_->value_as_ArgMin()->topK(); } -bool ArgMin::GetKeepDims() const { return this->primitive_->value_as_ArgMin()->keepDims(); } -int ArgMin::GetAxisType() const { return this->primitive_->value_as_ArgMin()->axisType(); } - -PrimitiveC *ArgMinCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ArgMinRegistry(schema::PrimitiveType_ArgMin, ArgMinCreator); -#endif - -int ArgMin::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (inputs_.size() != kSingleNum || outputs_.size() > kDoubleNum) { - MS_LOG(ERROR) << "tensor number is error."; - } - output->set_format(input->format()); - if (GetOutMaxValue() && outputs_.size() == kSingleNum) { - output->set_data_type(input->data_type()); - } else { - output->set_data_type(kNumberTypeInt32); - } - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape_size = input->shape().size(); - auto axis = GetAxis() < 0 ? GetAxis() + input_shape_size : GetAxis(); - if (axis >= input_shape_size || axis < 0) { - MS_LOG(ERROR) << "Invalid axis " << GetAxis() << ", input shape size: " << input_shape_size; - return RET_PARAM_INVALID; - } - std::vector output_shape(input->shape()); - if (GetTopK() == 1 && !GetKeepDims()) { - output_shape.erase(output_shape.begin() + axis); - } else { - output_shape[axis] = GetTopK(); - } - - output->set_shape(output_shape); - if (outputs_.size() == kDoubleNum) { - outputs_.at(1)->set_format(input->format()); - outputs_.at(1)->set_data_type(input->data_type()); - outputs_.at(1)->set_shape(output_shape); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/argmin.h b/mindspore/lite/src/ops/argmin.h deleted file mode 100644 index 4a1ab9af12..0000000000 --- a/mindspore/lite/src/ops/argmin.h +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ArgMin : public PrimitiveC { - public: - ArgMin() = default; - ~ArgMin() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ArgMin, PrimitiveC); - explicit ArgMin(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int axis); - void SetOutMaxValue(bool out_max_value); - void SetTopK(int top_k); - void SetKeepDims(bool keep_dims); - void SetAxisType(int axis_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetAxis() const; - bool GetOutMaxValue() const; - int GetTopK() const; - bool GetKeepDims() const; - int GetAxisType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ diff --git a/mindspore/lite/src/ops/arithmetic.cc b/mindspore/lite/src/ops/arithmetic.cc deleted file mode 100644 index 0c03bd6917..0000000000 --- a/mindspore/lite/src/ops/arithmetic.cc +++ /dev/null @@ -1,114 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/arithmetic.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" - -namespace mindspore { -namespace lite { - -int Arithmetic::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() != kDoubleNum) { - MS_LOG(ERROR) << "The number of input must be " << kDoubleNum; - return RET_INPUT_TENSOR_ERROR; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "The number of output must be " << kSingleNum; - return RET_INPUT_TENSOR_ERROR; - } - auto input0 = inputs_[0]; - MS_ASSERT(input0 != nullptr); - auto input1 = inputs_[1]; - MS_ASSERT(input1 != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - - auto input_shape0 = input0->shape(); - auto input_shape1 = input1->shape(); - auto format = input0->format(); - output->set_format(format); - output->set_data_type(input0->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if (input_shape0.size() > 10 || input_shape1.size() > 10) { - int wrong_dim = input_shape0.size() > input_shape1.size() ? input_shape0.size() : input_shape1.size(); - MS_LOG(ERROR) << "Not support input dim: " << wrong_dim << ", The input dim must be less than 10"; - return RET_ERROR; - } - in_shape0_.resize(10); - in_shape1_.resize(10); - out_shape_.resize(10); - - ndim_ = input_shape0.size(); - if (input_shape0.size() < input_shape1.size()) { - ndim_ = input_shape1.size(); - auto fill_dim_num = input_shape1.size() - input_shape0.size(); - int j = 0; - for (size_t i = 0; i < input_shape1.size(); i++) { - if (i < fill_dim_num) { - in_shape0_[i] = 1; - } else { - in_shape0_[i] = input_shape0[j++]; - } - in_shape1_[i] = input_shape1[i]; - } - format = input0->format(); - } else if (input_shape0.size() > input_shape1.size()) { - ndim_ = input_shape0.size(); - auto fill_dim_num = input_shape0.size() - input_shape1.size(); - int j = 0; - for (size_t i = 0; i < input_shape0.size(); i++) { - if (i < fill_dim_num) { - in_shape1_[i] = 1; - } else { - in_shape1_[i] = input_shape1[j++]; - } - in_shape0_[i] = input_shape0[i]; - } - } else { - for (size_t i = 0; i < input_shape0.size(); i++) { - in_shape1_[i] = input_shape1[i]; - in_shape0_[i] = input_shape0[i]; - } - } - - std::vector output_shape; - for (int i = 0; i < ndim_; i++) { - if (in_shape0_[i] != in_shape1_[i]) { - if (in_shape0_[i] == 1) { - out_shape_[i] = in_shape1_[i]; - } else if (in_shape1_[i] == 1) { - out_shape_[i] = in_shape0_[i]; - } else { - MS_LOG(ERROR) << "shapes of input tensors can not be broadCasted"; - return -1; - } - broadcasting_ = true; - } else { - out_shape_[i] = in_shape0_[i]; - } - output_shape.push_back(out_shape_[i]); - } - - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/arithmetic.h b/mindspore/lite/src/ops/arithmetic.h deleted file mode 100644 index 6c5c6f807a..0000000000 --- a/mindspore/lite/src/ops/arithmetic.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" -#include "nnacl/arithmetic.h" - -namespace mindspore { -namespace lite { -class Arithmetic : public PrimitiveC { - public: - Arithmetic() = default; - ~Arithmetic() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Arithmetic, PrimitiveC); - explicit Arithmetic(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - // explicit Arithmetic(schema::Primitive *primitive) : PrimitiveC(primitive) {} - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override { - return RET_ERROR; - } -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool Broadcasting() const { return this->broadcasting_; } - int NDims() const { return this->ndim_; } - std::vector InShape0() const { return this->in_shape0_; } - std::vector InShape1() const { return this->in_shape1_; } - std::vector OutputShape() const { return this->out_shape_; } - - protected: - bool broadcasting_ = false; - int ndim_ = 0; - std::vector in_shape0_; - std::vector in_shape1_; - std::vector out_shape_; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ diff --git a/mindspore/lite/src/ops/arithmetic_compare.cc b/mindspore/lite/src/ops/arithmetic_compare.cc deleted file mode 100644 index 57c3db7f74..0000000000 --- a/mindspore/lite/src/ops/arithmetic_compare.cc +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { - -int ArithmeticCompare::InferShape(std::vector inputs_, std::vector outputs_) { - auto res = Arithmetic::InferShape(inputs_, outputs_); - auto output = outputs_.front(); - output->set_data_type(TypeId::kNumberTypeBool); - return res; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/arithmetic_compare.h b/mindspore/lite/src/ops/arithmetic_compare.h deleted file mode 100644 index 4917a61792..0000000000 --- a/mindspore/lite/src/ops/arithmetic_compare.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_COMPARE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_COMPARE_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class ArithmeticCompare : public Arithmetic { - public: - ArithmeticCompare() = default; - ~ArithmeticCompare() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ArithmeticCompare, Arithmetic); - explicit ArithmeticCompare(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_COMPARE_H_ diff --git a/mindspore/lite/src/ops/arithmetic_grad.cc b/mindspore/lite/src/ops/arithmetic_grad.cc deleted file mode 100644 index 58be418faa..0000000000 --- a/mindspore/lite/src/ops/arithmetic_grad.cc +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/arithmetic_grad.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" - -namespace mindspore { -namespace lite { -int ArithmeticGrad::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != 3) { - MS_LOG(ERROR) << "The number of input must be 3"; - return RET_ERROR; - } - if (outputs_.size() != 2) { - MS_LOG(ERROR) << "The number of output must be 2"; - return RET_ERROR; - } - auto dy = inputs_[0]; - auto x1 = inputs_[1]; - auto x2 = inputs_[2]; - auto dx1 = outputs_[0]; - auto dx2 = outputs_[1]; - - MS_ASSERT(dy != nullptr); - MS_ASSERT(x1 != nullptr); - MS_ASSERT(x2 != nullptr); - MS_ASSERT(dx1 != nullptr); - MS_ASSERT(dx2 != nullptr); - - if ((Type() == schema::PrimitiveType_MaximumGrad) || (Type() == schema::PrimitiveType_MinimumGrad)) { - x1 = inputs_[0]; - x2 = inputs_[1]; - dy = inputs_[2]; - } - - auto inShape0 = x1->shape(); - auto inShape1 = x2->shape(); - auto outShape = dy->shape(); - - if ((Type() == schema::PrimitiveType_AddGrad) || (Type() == schema::PrimitiveType_SubGrad) || - (Type() == schema::PrimitiveType_MaximumGrad) || (Type() == schema::PrimitiveType_MinimumGrad)) { - ndim_ = outShape.size(); - x1_shape_.resize(ndim_); - x2_shape_.resize(ndim_); - dy_shape_.resize(ndim_); - auto fillDimNum0 = outShape.size() - inShape0.size(); - auto fillDimNum1 = outShape.size() - inShape1.size(); - int j0 = 0; - int j1 = 0; - for (unsigned int i = 0; i < outShape.size(); i++) { - x1_shape_[i] = (i < fillDimNum0) ? 1 : inShape0[j0++]; - x2_shape_[i] = (i < fillDimNum1) ? 1 : inShape1[j1++]; - dy_shape_[i] = outShape[i]; - } - } else { - if (dx1->ElementsNum() < dx2->ElementsNum()) { - ndim_ = inShape1.size(); - x1_shape_.resize(ndim_); - x2_shape_.resize(ndim_); - dy_shape_.resize(ndim_); - auto fillDimNum = inShape1.size() - inShape0.size(); // This will not work for batch! - int j = 0; - for (unsigned int i = 0; i < inShape1.size(); i++) { - if (i < fillDimNum) { - x2_shape_[i] = 1; - } else { - x2_shape_[i] = inShape0[j++]; - } - x1_shape_[i] = inShape1[i]; - dy_shape_[i] = outShape[i]; - } - } else if (dx2->ElementsNum() < dx1->ElementsNum()) { // if (inShape0.size() > inShape1.size()) - ndim_ = inShape0.size(); - x1_shape_.resize(ndim_); - x2_shape_.resize(ndim_); - dy_shape_.resize(ndim_); - broadcasting_ = true; - int j = 0; - auto fillDimNum = inShape0.size() - inShape1.size(); - for (unsigned int i = 0; i < inShape0.size(); i++) { - if (i < fillDimNum) { - x2_shape_[i] = 1; - } else { - x2_shape_[i] = inShape1[j++]; - } - x1_shape_[i] = inShape0[i]; - dy_shape_[i] = outShape[i]; - } - } else { - broadcasting_ = false; - for (unsigned int i = 0; i < inShape0.size(); i++) { - x2_shape_[i] = inShape1[i]; - x1_shape_[i] = inShape0[i]; - dy_shape_[i] = outShape[i]; - } - } - } - - dx1->set_shape(x1->shape()); - dx2->set_shape(x2->shape()); - dx1->set_data_type(dy->data_type()); - dx2->set_data_type(dy->data_type()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/arithmetic_grad.h b/mindspore/lite/src/ops/arithmetic_grad.h deleted file mode 100644 index d4a1cf666d..0000000000 --- a/mindspore/lite/src/ops/arithmetic_grad.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ARITHMETIC_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_ARITHMETIC_GRAD_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" -#include "nnacl/arithmetic_self_parameter.h" - -namespace mindspore { -namespace lite { -class ArithmeticGrad : public PrimitiveC { - public: - ArithmeticGrad() = default; - ~ArithmeticGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ArithmeticGrad, PrimitiveC); - explicit ArithmeticGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - // explicit ArithmeticGrad(const schema::Primitive &primitive) : PrimitiveC(primitive) {} - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override { - return RET_ERROR; - } -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool Broadcasting() { return this->broadcasting_; } - int NDims() { return this->ndim_; } - std::vector dyShape() { return this->dy_shape_; } - std::vector x1Shape() { return this->x1_shape_; } - std::vector x2Shape() { return this->x2_shape_; } - - protected: - bool broadcasting_ = false; - int ndim_; - std::vector dy_shape_; - std::vector x1_shape_; - std::vector x2_shape_; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_ARITHMETIC_GRAD_H_ diff --git a/mindspore/lite/src/ops/arithmetic_self.cc b/mindspore/lite/src/ops/arithmetic_self.cc deleted file mode 100644 index bc8c2a5831..0000000000 --- a/mindspore/lite/src/ops/arithmetic_self.cc +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/arithmetic_self.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -int ArithmeticSelf::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - output->set_shape(input->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/arithmetic_self.h b/mindspore/lite/src/ops/arithmetic_self.h deleted file mode 100644 index dafba50a81..0000000000 --- a/mindspore/lite/src/ops/arithmetic_self.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ - -#include -#include "src/ops/primitive_c.h" -#include "nnacl/arithmetic_self_parameter.h" - -namespace mindspore { -namespace lite { -class ArithmeticSelf : public PrimitiveC { - public: - ArithmeticSelf() = default; - ~ArithmeticSelf() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ArithmeticSelf, PrimitiveC); - explicit ArithmeticSelf(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - // explicit ArithmeticSelf(schema::Primitive *primitive) : PrimitiveC(primitive) {} - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override { - return RET_ERROR; - } -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -OpParameter *PopulateArithmeticSelf(const mindspore::lite::PrimitiveC *primitive); -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ diff --git a/mindspore/lite/src/ops/assert_op.cc b/mindspore/lite/src/ops/assert_op.cc deleted file mode 100644 index 83074c1b7f..0000000000 --- a/mindspore/lite/src/ops/assert_op.cc +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/assert_op.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int AssertOP::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Assert; - } - if (this->primitive_->value.type != schema::PrimitiveType_Assert) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AssertT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int AssertOP::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Assert(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Assert return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAssert(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Assert, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AssertCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AssertRegistry(schema::PrimitiveType_Assert, AssertCreator); -#endif - -int AssertOP::InferShape(std::vector inputs_, std::vector outputs_) { return RET_OK; } -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/assert_op.h b/mindspore/lite/src/ops/assert_op.h deleted file mode 100644 index ba0399e07d..0000000000 --- a/mindspore/lite/src/ops/assert_op.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_ASSERT_OP_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_ASSERT_OP_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class AssertOP : public PrimitiveC { - public: - AssertOP() = default; - ~AssertOP() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(AssertOP, PrimitiveC); - explicit AssertOP(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_SRC_OPS_ASSERT_OP_H_ diff --git a/mindspore/lite/src/ops/assign.cc b/mindspore/lite/src/ops/assign.cc deleted file mode 100644 index 9facccd921..0000000000 --- a/mindspore/lite/src/ops/assign.cc +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/assign.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Assign::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Assign; - } - if (this->primitive_->value.type != schema::PrimitiveType_Assign) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AssignT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Assign::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Assign(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Assign return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAssign(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Assign, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AssignCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry AssignRegistry(schema::PrimitiveType_Assign, AssignCreator); -#endif - -int Assign::InferShape(std::vector inputs, std::vector outputs) { - if (2 != inputs.size()) { - MS_LOG(ERROR) << "Assign should have at least 5 input tensors"; - return RET_ERROR; - } - - if (inputs.at(0)->ElementsNum() != inputs.at(1)->ElementsNum()) { - MS_LOG(ERROR) << "error input data size!"; - return RET_ERROR; - } - - if (!outputs.empty()) { - auto *out = outputs.front(); - MS_ASSERT(out != nullptr); - out->set_data_type(inputs.at(0)->data_type()); - out->set_format(inputs.at(0)->format()); - out->set_shape({1}); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/assign.h b/mindspore/lite/src/ops/assign.h deleted file mode 100644 index e53ac0a636..0000000000 --- a/mindspore/lite/src/ops/assign.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_ASSIGN_H_ -#define MINDSPORE_LITE_SRC_OPS_ASSIGN_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Assign : public PrimitiveC { - public: - Assign() = default; - ~Assign() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Assign, PrimitiveC); - explicit Assign(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_ASSIGN_H_ diff --git a/mindspore/lite/src/ops/assign_add.cc b/mindspore/lite/src/ops/assign_add.cc deleted file mode 100644 index 6d77708ad1..0000000000 --- a/mindspore/lite/src/ops/assign_add.cc +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/assign_add.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int AssignAdd::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_AssignAdd; - } - if (this->primitive_->value.type != schema::PrimitiveType_AssignAdd) { - MS_LOG(ERROR) << "PrimitiveType_AssignAdd primitive value type : " - << schema::EnumNamePrimitiveType(primitive_->value.type) << "is not equal" - << schema::EnumNamePrimitiveType(schema::PrimitiveType_AssignAdd); - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::AssignAddT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int AssignAdd::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_AssignAdd(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_AssignAdd return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAssignAdd(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_AssignAdd, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AssignAddCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry AssignAddRegistry(schema::PrimitiveType_AssignAdd, AssignAddCreator); -#endif - -int AssignAdd::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_.at(0); - Tensor *y = inputs_.at(1); - Tensor *out = outputs_.at(0); - std::vector x_shape = x->shape(); - if (x->data_type() != y->data_type()) { - MS_LOG(ERROR) << "no matched shape of x and y"; - return RET_ERROR; - } - std::vector output_shape(x_shape.size()); - for (size_t i = 0; i < x_shape.size(); i++) { - output_shape[i] = x_shape[i]; - } - out->set_shape(output_shape); - out->set_format(x->format()); - out->set_data_type(x->data_type()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/assign_add.h b/mindspore/lite/src/ops/assign_add.h deleted file mode 100644 index 6e0e94edab..0000000000 --- a/mindspore/lite/src/ops/assign_add.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include -#include "src/ops/primitive_c.h" -#ifndef LITE_SRC_OPS_ASSIGN_ADD_H_ -#define LITE_SRC_OPS_ASSIGN_ADD_H_ -namespace mindspore { -namespace lite { -class AssignAdd : public PrimitiveC { - public: - AssignAdd() = default; - ~AssignAdd() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(AssignAdd, PrimitiveC); - explicit AssignAdd(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_SRC_OPS_ASSIGN_ADD_H_ diff --git a/mindspore/lite/src/ops/audio_spectrogram.cc b/mindspore/lite/src/ops/audio_spectrogram.cc deleted file mode 100644 index 6adce58037..0000000000 --- a/mindspore/lite/src/ops/audio_spectrogram.cc +++ /dev/null @@ -1,107 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/audio_spectrogram.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int AudioSpectrogram::GetWindowSize() const { return this->primitive_->value.AsAudioSpectrogram()->windowSize; } -int AudioSpectrogram::GetStride() const { return this->primitive_->value.AsAudioSpectrogram()->stride; } -bool AudioSpectrogram::GetMagSquare() const { return this->primitive_->value.AsAudioSpectrogram()->magSquare; } - -#else -int AudioSpectrogram::GetWindowSize() const { return this->primitive_->value_as_AudioSpectrogram()->windowSize(); } -int AudioSpectrogram::GetStride() const { return this->primitive_->value_as_AudioSpectrogram()->stride(); } -bool AudioSpectrogram::GetMagSquare() const { return this->primitive_->value_as_AudioSpectrogram()->magSquare(); } -int AudioSpectrogram::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_AudioSpectrogram(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Add return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateAudioSpectrogram(*fbb, attr->windowSize(), attr->stride(), attr->magSquare()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_AudioSpectrogram, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *AudioSpectrogramCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry AudioSpectrogramRegistry(schema::PrimitiveType_AudioSpectrogram, AudioSpectrogramCreator); -#endif -int AudioSpectrogram::Log2Ceil(uint32_t length) { - if (length == 0) { - return -1; - } - int floor = 0; - for (int i = 4; i >= 0; --i) { - const int shift = (1 << i); - uint32_t tmp = length >> shift; - if (tmp != 0) { - length = tmp; - floor += shift; - } - } - return length == (length & ~(length - 1)) ? floor : floor + 1; -} -uint32_t AudioSpectrogram::GetFftLength(uint32_t length) { - int shift = Log2Ceil(length); - return 1 << shift; -} -int AudioSpectrogram::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - if (input_shape.size() != 2) { - MS_LOG(ERROR) << "input shape is error, which need to be 2 dimensions"; - return RET_ERROR; - } - if (GetWindowSize() < 2) { - MS_LOG(ERROR) << "window size is too short, now is " << GetWindowSize(); - return RET_ERROR; - } - if (GetStride() < 1) { - MS_LOG(ERROR) << "stride must be positive, now is " << GetStride(); - return RET_ERROR; - } - std::vector output_shape(3); - output_shape[0] = input_shape[1]; - // output height - int sample_sub_window = input_shape[0] - GetWindowSize(); - output_shape[1] = sample_sub_window < 0 ? 0 : 1 + sample_sub_window / GetStride(); - // compute fft length - int fft_length = GetFftLength(GetWindowSize()); - output_shape[2] = fft_length / 2 + 1; - outputs_.front()->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/audio_spectrogram.h b/mindspore/lite/src/ops/audio_spectrogram.h deleted file mode 100644 index e996543ad3..0000000000 --- a/mindspore/lite/src/ops/audio_spectrogram.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_AUDIO_SPECTROGRAM_H_ -#define LITE_MINDSPORE_LITE_C_OPS_AUDIO_SPECTROGRAM_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class AudioSpectrogram : public PrimitiveC { - public: - AudioSpectrogram() = default; - ~AudioSpectrogram() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(AudioSpectrogram, PrimitiveC); - explicit AudioSpectrogram(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetWindowSize(int window_size) { this->primitive_->value.AsAudioSpectrogram()->windowSize = window_size; } - void SetStride(int stride) { this->primitive_->value.AsAudioSpectrogram()->stride = stride; } - void SetMagSquare(bool mag_square) { this->primitive_->value.AsAudioSpectrogram()->magSquare = mag_square; } -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetWindowSize() const; - int GetStride() const; - bool GetMagSquare() const; - int Log2Ceil(uint32_t length); - uint32_t GetFftLength(uint32_t length); - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_AUDIO_SPECTROGRAM_H_ diff --git a/mindspore/lite/src/ops/batch_norm.cc b/mindspore/lite/src/ops/batch_norm.cc deleted file mode 100644 index 3374ef1123..0000000000 --- a/mindspore/lite/src/ops/batch_norm.cc +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/batch_norm.h" -#include -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float BatchNorm::GetEpsilon() const { return this->primitive_->value.AsBatchNorm()->epsilon; } - -void BatchNorm::SetEpsilon(float epsilon) { this->primitive_->value.AsBatchNorm()->epsilon = epsilon; } - -int BatchNorm::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_FusedBatchNorm; - } - if (this->primitive_->value.type != schema::PrimitiveType_FusedBatchNorm) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::FusedBatchNormT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new FusedBatchNormT failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - attr->epsilon = GetValue(prim.GetAttr("epsilon")); - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else -int BatchNorm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateBatchNorm(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BatchNorm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float BatchNorm::GetEpsilon() const { return this->primitive_->value_as_BatchNorm()->epsilon(); } - -PrimitiveC *BatchNormCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BatchNormRegistry(schema::PrimitiveType_BatchNorm, BatchNormCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/batch_norm.h b/mindspore/lite/src/ops/batch_norm.h deleted file mode 100644 index f4f98648b4..0000000000 --- a/mindspore/lite/src/ops/batch_norm.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ -#define LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BatchNorm : public PrimitiveC { - public: - BatchNorm() = default; - ~BatchNorm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BatchNorm, PrimitiveC); - explicit BatchNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetEpsilon(float epsilon); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetEpsilon() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ diff --git a/mindspore/lite/src/ops/batch_to_space.cc b/mindspore/lite/src/ops/batch_to_space.cc deleted file mode 100644 index da7dcc3316..0000000000 --- a/mindspore/lite/src/ops/batch_to_space.cc +++ /dev/null @@ -1,231 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/batch_to_space.h" -#include "src/common/common.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector BatchToSpace::GetBlockShape() const { return this->primitive_->value.AsBatchToSpace()->blockShape; } -std::vector BatchToSpace::GetCrops() const { return this->primitive_->value.AsBatchToSpace()->crops; } - -void BatchToSpace::SetBlockShape(const std::vector &block_shape) { - this->primitive_->value.AsBatchToSpace()->blockShape = block_shape; -} -void BatchToSpace::SetCrops(const std::vector &crops) { this->primitive_->value.AsBatchToSpace()->crops = crops; } - -#else -int BatchToSpace::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BatchToSpace(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BatchToSpace return nullptr"; - return RET_ERROR; - } - std::vector blockShape; - if (attr->blockShape() != nullptr) { - for (int i = 0; i < static_cast(attr->blockShape()->size()); i++) { - blockShape.push_back(attr->blockShape()->data()[i]); - } - } - std::vector crops; - if (attr->crops() != nullptr) { - for (int i = 0; i < static_cast(attr->crops()->size()); i++) { - crops.push_back(attr->crops()->data()[i]); - } - } - auto val_offset = schema::CreateBatchToSpaceDirect(*fbb, &blockShape, &crops); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BatchToSpace, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -std::vector BatchToSpace::GetBlockShape() const { - auto fb_vector = this->primitive_->value_as_BatchToSpace()->blockShape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -std::vector BatchToSpace::GetCrops() const { - auto fb_vector = this->primitive_->value_as_BatchToSpace()->crops(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} - -PrimitiveC *BatchToSpaceCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BatchToSpaceRegistry(schema::PrimitiveType_BatchToSpace, BatchToSpaceCreator); -#endif - -namespace { -constexpr int kBatchToSpaceOutputNum = 1; -constexpr int kBatchToSpaceOneInput = 1; -constexpr int kBatchToSpaceThreeInput = 3; -constexpr int kBlockShapeSize = 2; -constexpr int kCropsSize = 4; -} // namespace - -int BatchToSpace::SetOutputShapeFromParam(const std::vector inputs, - std::vector outputs) { - auto input_shape = inputs[0]->shape(); - if (input_shape.size() != kQuadrupleNum) { - MS_LOG(ERROR) << "input shape dimension size should == " << kQuadrupleNum; - return RET_PARAM_INVALID; - } - auto block_shape = GetBlockShape(); - if (block_shape.size() != kBlockShapeSize) { - MS_LOG(ERROR) << "Block shape size should be " << kBlockShapeSize; - return RET_PARAM_INVALID; - } - auto crops = GetCrops(); - if (crops.size() != kCropsSize) { - MS_LOG(ERROR) << "Crops size should be " << kCropsSize; - return RET_PARAM_INVALID; - } - mul_block_shape_ = 1; - - for (size_t i = 0; i < kBlockShapeSize; ++i) { - if (block_shape[i] <= 0) { - MS_LOG(ERROR) << "Input block_shape should > 0!"; - return RET_PARAM_INVALID; - } - if (input_shape[NHWC_N] % block_shape[i]) { - MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " can not divide block_shape[" << i << "] " - << block_shape[i]; - return 1; - } - mul_block_shape_ *= block_shape[i]; - } - - if (input_shape[NHWC_N] < mul_block_shape_) { - MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " < product of block shape!"; - return RET_PARAM_INVALID; - } - for (size_t i = 0; i < kCropsSize; ++i) { - if (crops[i] < 0) { - MS_LOG(ERROR) << "Input crops should >= 0"; - return RET_PARAM_INVALID; - } - } - std::vector output_shape(input_shape.size()); - output_shape[NHWC_N] = input_shape[NHWC_N] / mul_block_shape_; - output_shape[NHWC_H] = input_shape[NHWC_H] * block_shape[0] - crops[0] - crops[1]; - output_shape[NHWC_W] = input_shape[NHWC_W] * block_shape[1] - crops[2] - crops[3]; - if (input_shape.size() > 3) { - output_shape[NHWC_C] = input_shape[NHWC_C]; - } - outputs[0]->set_shape(output_shape); - return RET_OK; -} - -int BatchToSpace::SetOutputShapeFromInput(const std::vector inputs, - std::vector outputs) { - auto input_shape = inputs[0]->shape(); - if (input_shape.size() != kQuadrupleNum) { - MS_LOG(ERROR) << "input shape dimension size should == " << kQuadrupleNum; - return RET_PARAM_INVALID; - } - auto block_shape_data = inputs[1]->data_c(); - auto crops_data = inputs[2]->data_c(); - auto block_shape = static_cast(block_shape_data); - auto crops = static_cast(crops_data); - if (inputs[1]->ElementsNum() != kBlockShapeSize) { - MS_LOG(ERROR) << "Block shape size should be " << kBlockShapeSize; - return RET_PARAM_INVALID; - } - if (inputs[2]->ElementsNum() != kCropsSize) { - MS_LOG(ERROR) << "Crops size should be " << kCropsSize; - return RET_PARAM_INVALID; - } - mul_block_shape_ = 1; - - for (size_t i = 0; i < kBlockShapeSize; ++i) { - if (block_shape[i] <= 0) { - MS_LOG(ERROR) << "Input block_shape should > 0!"; - return RET_PARAM_INVALID; - } - if (input_shape[NHWC_N] % block_shape[i]) { - MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " can not divide block_shape[" << i << "] " - << block_shape[i]; - return 1; - } - mul_block_shape_ *= block_shape[i]; - } - - if (input_shape[NHWC_N] < mul_block_shape_) { - MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " < product of block shape!"; - return RET_PARAM_INVALID; - } - for (size_t i = 0; i < kCropsSize; ++i) { - if (crops[i] < 0) { - MS_LOG(ERROR) << "Input crops should >= 0"; - return RET_PARAM_INVALID; - } - } - std::vector output_shape(input_shape.size()); - output_shape[NHWC_N] = input_shape[NHWC_N] / mul_block_shape_; - output_shape[NHWC_H] = input_shape[NHWC_H] * block_shape[0] - crops[0] - crops[1]; - output_shape[NHWC_W] = input_shape[NHWC_W] * block_shape[1] - crops[2] - crops[3]; - if (input_shape.size() > 3) { - output_shape[NHWC_C] = input_shape[NHWC_C]; - } - outputs[0]->set_shape(output_shape); - return RET_OK; -} - -int BatchToSpace::InferShape(std::vector inputs, std::vector outputs) { - MS_ASSERT(this->primitive_ != nullptr); - if (outputs.size() != kBatchToSpaceOutputNum || - (inputs.size() != kBatchToSpaceOneInput && inputs.size() != kBatchToSpaceThreeInput)) { - MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); - return RET_PARAM_INVALID; - } - - auto input = inputs.at(0); - if (input->format() != schema::Format::Format_NHWC) { - MS_LOG(ERROR) << "batch_to_space only support NHWC now!"; - return RET_FORMAT_ERR; - } - outputs[0]->set_format(input->format()); - outputs[0]->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - if (inputs.size() == kBatchToSpaceOneInput) { - auto ret = SetOutputShapeFromParam(inputs, outputs); - return ret; - } - if (inputs.size() == kBatchToSpaceThreeInput) { - if (inputs[0]->data_c() == nullptr) { - return RET_INFER_INVALID; - } - MS_ASSERT(inputs[1]->data_c() != nullptr); - MS_ASSERT(inputs[2]->data_c() != nullptr); - auto ret = SetOutputShapeFromInput(inputs, outputs); - return ret; - } - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/batch_to_space.h b/mindspore/lite/src/ops/batch_to_space.h deleted file mode 100644 index aa8a2433b8..0000000000 --- a/mindspore/lite/src/ops/batch_to_space.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BatchToSpace : public PrimitiveC { - public: - BatchToSpace() = default; - ~BatchToSpace() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BatchToSpace, PrimitiveC); - explicit BatchToSpace(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetBlockShape(const std::vector &block_shape); - void SetCrops(const std::vector &crops); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetBlockShape() const; - std::vector GetCrops() const; - - private: - int SetOutputShapeFromParam(const std::vector inputs, std::vector outputs); - int SetOutputShapeFromInput(const std::vector inputs, std::vector outputs); - int mul_block_shape_; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ diff --git a/mindspore/lite/src/ops/bias_add.cc b/mindspore/lite/src/ops/bias_add.cc deleted file mode 100644 index cdb0b56f36..0000000000 --- a/mindspore/lite/src/ops/bias_add.cc +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/bias_add.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int BiasAdd::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_BiasAdd; - } - if (this->primitive_->value.type != schema::PrimitiveType_BiasAdd) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::BiasAddT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.GetAttr("axis") == nullptr) { - MS_LOG(INFO) << "BiasAdd's attr axis is set to default"; - attr->axis = {1}; - } else { - attr->axis = CastToInt(prim.GetAttr("axis")); - } - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else -int BiasAdd::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BiasAdd(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BiasAdd return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateBiasAddDirect(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BiasAdd, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *BiasAddCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry BiasAddRegistry(schema::PrimitiveType_BiasAdd, BiasAddCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/bias_add.h b/mindspore/lite/src/ops/bias_add.h deleted file mode 100644 index d1cdf391e2..0000000000 --- a/mindspore/lite/src/ops/bias_add.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BiasAdd : public PrimitiveC { - public: - BiasAdd() = default; - ~BiasAdd() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BiasAdd, PrimitiveC); - explicit BiasAdd(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ diff --git a/mindspore/lite/src/ops/bias_grad.cc b/mindspore/lite/src/ops/bias_grad.cc deleted file mode 100644 index 162c807c05..0000000000 --- a/mindspore/lite/src/ops/bias_grad.cc +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/bias_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int BiasGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_BiasGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_BiasGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::BiasGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#else -int BiasGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BiasGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BiasGrad return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateBiasGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BiasGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *BiasGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BiasGradRegistry(schema::PrimitiveType_BiasGrad, BiasGradCreator); -#endif - -int BiasGrad::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() != 1) { - MS_LOG(ERROR) << "BiasGrad should have one input"; - return RET_ERROR; - } - if (outputs.size() != 1) { - MS_LOG(ERROR) << "BiasGrad should have one output"; - return RET_ERROR; - } - auto *in0 = inputs.front(); - auto *out = outputs.front(); - MS_ASSERT(in0 != nullptr); - MS_ASSERT(out != nullptr); - - auto inshape = in0->shape(); - int ndim = inshape.size(); - for (int i = 0; i < ndim - 1; i++) { - inshape[i] = 1; - } - out->set_shape(inshape); - out->set_data_type(in0->data_type()); - out->set_format(in0->format()); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/bias_grad.h b/mindspore/lite/src/ops/bias_grad.h deleted file mode 100644 index 44df55a8cd..0000000000 --- a/mindspore/lite/src/ops/bias_grad.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_BIAS_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_BIAS_GRAD_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BiasGrad : public PrimitiveC { - public: - BiasGrad() = default; - ~BiasGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BiasGrad, PrimitiveC); - explicit BiasGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(const std::vector &axis); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs, std::vector outputs) override; - std::vector GetAxis() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_BIAS_GRAD_H_ diff --git a/mindspore/lite/src/ops/binary_cross_entropy.cc b/mindspore/lite/src/ops/binary_cross_entropy.cc deleted file mode 100644 index da06fff538..0000000000 --- a/mindspore/lite/src/ops/binary_cross_entropy.cc +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include "src/ops/binary_cross_entropy.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int BinaryCrossEntropy::GetReduction() const { return this->primitive_->value.AsBinaryCrossEntropy()->reduction; } - -int BinaryCrossEntropy::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_BinaryCrossEntropy; - } - if (this->primitive_->value.type != schema::PrimitiveType_BinaryCrossEntropy) { - MS_LOG(ERROR) << "PrimitiveType_BinaryCrossEntropy primitive value type : " - << schema::EnumNamePrimitiveType(primitive_->value.type) << "is not equal" - << schema::EnumNamePrimitiveType(schema::PrimitiveType_BinaryCrossEntropy); - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - schema::BinaryCrossEntropyT *attr = new (std::nothrow) schema::BinaryCrossEntropyT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new binary cross entropy attr failed!"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - // default is mean - string reduction = "mean"; - if (prim.GetAttr("reduction") == nullptr) { - MS_LOG(ERROR) << "get reduction failed!"; - delete this->primitive_; - delete attr; - this->primitive_ = nullptr; - attr = nullptr; - return RET_ERROR; - } else { - reduction = GetValue(prim.GetAttr("reduction")); - } - if (reduction == "none") { - attr->reduction = 0; - } else if (reduction == "sum") { - attr->reduction = 2; - } else { - // default is mean - attr->reduction = 1; - } - this->primitive_->value.value = attr; - } - - return RET_OK; -} -#else -int BinaryCrossEntropy::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BinaryCrossEntropy(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BinaryCrossEntropy return nullptr"; - return RET_ERROR; - } - int reduction = attr->reduction(); - auto val_offset = schema::CreateBinaryCrossEntropy(*fbb, reduction); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BinaryCrossEntropy, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int BinaryCrossEntropy::GetReduction() const { return this->primitive_->value_as_BinaryCrossEntropy()->reduction(); } - -PrimitiveC *BinaryCrossEntropyCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BinaryCrossEntropyRegistry(schema::PrimitiveType_BinaryCrossEntropy, BinaryCrossEntropyCreator); -#endif -int BinaryCrossEntropy::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_.at(0); - Tensor *out = outputs_.at(0); - out->set_format(x->format()); - out->set_data_type(x->data_type()); - int reduction = GetReduction(); - if (reduction == 1 || reduction == 2) { - out->set_shape({1}); - } else { - std::vector x_shape = x->shape(); - std::vector output_shape(x_shape.size()); - output_shape.assign(x_shape.begin(), x_shape.end()); - out->set_shape(output_shape); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/binary_cross_entropy.h b/mindspore/lite/src/ops/binary_cross_entropy.h deleted file mode 100644 index c9ad936770..0000000000 --- a/mindspore/lite/src/ops/binary_cross_entropy.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include -#include "src/ops/primitive_c.h" - -#ifndef LITE_SRC_OPS_BINARYCROSSENTROPY_H_ -#define LITE_SRC_OPS_BINARYCROSSENTROPY_H_ -namespace mindspore { -namespace lite { -class BinaryCrossEntropy : public PrimitiveC { - public: - BinaryCrossEntropy() = default; - ~BinaryCrossEntropy() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BinaryCrossEntropy, PrimitiveC); - - explicit BinaryCrossEntropy(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - - int GetReduction() const; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; - - int GetReduction() const; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_SRC_OPS_BINARYCROSSENTROPY_H_ diff --git a/mindspore/lite/src/ops/binary_cross_entropy_grad.cc b/mindspore/lite/src/ops/binary_cross_entropy_grad.cc deleted file mode 100644 index 61016b1075..0000000000 --- a/mindspore/lite/src/ops/binary_cross_entropy_grad.cc +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include "src/ops/binary_cross_entropy_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE - -int BinaryCrossEntropyGrad::GetReduction() const { - return this->primitive_->value.AsBinaryCrossEntropyGrad()->reduction; -} - -int BinaryCrossEntropyGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_BinaryCrossEntropyGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_BinaryCrossEntropyGrad) { - MS_LOG(ERROR) << "PrimitiveType_BinaryCrossEntropyGrad primitive value type : " - << schema::EnumNamePrimitiveType(primitive_->value.type) << "is not equal" - << schema::EnumNamePrimitiveType(schema::PrimitiveType_BinaryCrossEntropyGrad); - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - schema::BinaryCrossEntropyGradT *attr = new (std::nothrow) schema::BinaryCrossEntropyGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new binary cross entropy attr failed!"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - // default is mean - string reduction = "mean"; - if (prim.GetAttr("reduction") == nullptr) { - MS_LOG(ERROR) << "get reduction failed!"; - delete this->primitive_; - delete attr; - this->primitive_ = nullptr; - attr = nullptr; - return RET_ERROR; - } else { - reduction = GetValue(prim.GetAttr("reduction")); - } - - if (reduction == "none") { - attr->reduction = 0; - } else if (reduction == "sum") { - attr->reduction = 2; - } else { - // default is mean - attr->reduction = 1; - } - this->primitive_->value.value = attr; - } - - return RET_OK; -} -#else -int BinaryCrossEntropyGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, - flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BinaryCrossEntropyGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BinaryCrossEntropyGrad return nullptr"; - return RET_ERROR; - } - int reduction = attr->reduction(); - auto val_offset = schema::CreateBinaryCrossEntropyGrad(*fbb, reduction); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BinaryCrossEntropyGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int BinaryCrossEntropyGrad::GetReduction() const { - return this->primitive_->value_as_BinaryCrossEntropyGrad()->reduction(); -} - -PrimitiveC *BinaryCrossEntropyGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BinaryCrossEntropyGradRegistry(schema::PrimitiveType_BinaryCrossEntropyGrad, BinaryCrossEntropyGradCreator); -#endif -int BinaryCrossEntropyGrad::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_[0]; - Tensor *out = outputs_[0]; - out->set_format(x->format()); - out->set_data_type(x->data_type()); - std::vector x_shape = x->shape(); - std::vector output_shape(x_shape.size()); - output_shape.assign(x_shape.begin(), x_shape.end()); - out->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/binary_cross_entropy_grad.h b/mindspore/lite/src/ops/binary_cross_entropy_grad.h deleted file mode 100644 index bb21020541..0000000000 --- a/mindspore/lite/src/ops/binary_cross_entropy_grad.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include -#include "src/ops/primitive_c.h" - -#ifndef LITE_SRC_OPS_BINARY_CROSS_ENTROPY_GRAD_H_ -#define LITE_SRC_OPS_BINARY_CROSS_ENTROPY_GRAD_H_ -namespace mindspore { -namespace lite { -class BinaryCrossEntropyGrad : public PrimitiveC { - public: - BinaryCrossEntropyGrad() = default; - ~BinaryCrossEntropyGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BinaryCrossEntropyGrad, PrimitiveC); - - explicit BinaryCrossEntropyGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - - int GetReduction() const; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; - - int GetReduction() const; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_SRC_OPS_BINARY_CROSS_ENTROPY_GRAD_H_ diff --git a/mindspore/lite/src/ops/bn_grad.cc b/mindspore/lite/src/ops/bn_grad.cc deleted file mode 100644 index 99604e2d51..0000000000 --- a/mindspore/lite/src/ops/bn_grad.cc +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/bn_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float BNGrad::GetEps() const { return this->primitive_->value.AsBNGrad()->eps; } -float BNGrad::GetMomentum() const { return this->primitive_->value.AsBNGrad()->momentum; } - -void BNGrad::SetEps(float eps) { this->primitive_->value.AsBNGrad()->eps = eps; } -void BNGrad::SetMomentum(float momentum) { this->primitive_->value.AsBNGrad()->momentum = momentum; } -int BNGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_BNGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_BNGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::BNGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->momentum = 0.1f; - if (prim.GetAttr("momentum") != nullptr) { - attr->momentum = GetValue(prim.GetAttr("momentum")); - } - attr->eps = 1e-5; - if (prim.GetAttr("epsilon") != nullptr) { - attr->eps = GetValue(prim.GetAttr("epsilon")); - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#else -int BNGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BNGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BNGradInput return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateBNGrad(*fbb, attr->eps(), attr->momentum()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BNGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *BNGradCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry BNGradRegistry(schema::PrimitiveType_BNGrad, BNGradCreator); - -float BNGrad::GetEps() const { return this->primitive_->value_as_BNGrad()->eps(); } -float BNGrad::GetMomentum() const { return this->primitive_->value_as_BNGrad()->momentum(); } -#endif -int BNGrad::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() != 6) { - MS_LOG(ERROR) << "BNGrad should have five inputs"; - return RET_ERROR; - } - if (outputs.size() != 3) { - MS_LOG(ERROR) << "BNGrad should have three outputs"; - return RET_ERROR; - } - auto in = inputs[1]; - auto scale = inputs[2]; - - if (in->shape().size() != 4) { - MS_LOG(ERROR) << "Grad Fused batchnorm only support nhwc input!"; - } - - outputs[0]->set_shape(in->shape()); - outputs[1]->set_shape(scale->shape()); - outputs[2]->set_shape(scale->shape()); - outputs[0]->set_data_type(in->data_type()); - outputs[1]->set_data_type(scale->data_type()); - outputs[2]->set_data_type(scale->data_type()); - outputs[0]->set_format(in->format()); - outputs[1]->set_format(scale->format()); - outputs[2]->set_format(scale->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/bn_grad.h b/mindspore/lite/src/ops/bn_grad.h deleted file mode 100644 index a0b68ea45e..0000000000 --- a/mindspore/lite/src/ops/bn_grad.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_BN_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_BN_GRAD_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BNGrad : public PrimitiveC { - public: - BNGrad() = default; - ~BNGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BNGrad, PrimitiveC); - explicit BNGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetEps(float eps); - void SetMomentum(float momentum); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetEps() const; - float GetMomentum() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_BN_GRAD_H_ diff --git a/mindspore/lite/src/ops/broadcast_to.cc b/mindspore/lite/src/ops/broadcast_to.cc deleted file mode 100644 index e5a891af84..0000000000 --- a/mindspore/lite/src/ops/broadcast_to.cc +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/broadcast_to.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector BroadcastTo::GetDstShape() const { return this->primitive_->value.AsBroadcastTo()->dst_shape; } - -void BroadcastTo::SetDstShape(const std::vector &dst_shape) { - this->primitive_->value.AsBroadcastTo()->dst_shape = dst_shape; -} - -#else -int BroadcastTo::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_BroadcastTo(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_BroadcastTo return nullptr"; - return RET_ERROR; - } - std::vector dst_shape; - if (attr->dst_shape() != nullptr) { - for (int i = 0; i < static_cast(attr->dst_shape()->size()); i++) { - dst_shape.push_back(attr->dst_shape()->data()[i]); - } - } - auto val_offset = schema::CreateBroadcastToDirect(*fbb, &dst_shape); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_BroadcastTo, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -std::vector BroadcastTo::GetDstShape() const { - auto fb_vector = this->primitive_->value_as_BroadcastTo()->dst_shape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} - -PrimitiveC *BroadcastToCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry BroadcastToRegistry(schema::PrimitiveType_BroadcastTo, BroadcastToCreator); -#endif - -namespace { -constexpr int kBroadcastToInputNum = 1; -constexpr int kBroadcastToOnnxInputNum = 2; -constexpr int kBroadcastToOutputNum = 1; -} // namespace - -int BroadcastTo::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() != kBroadcastToInputNum && inputs.size() != kBroadcastToOnnxInputNum) { - MS_LOG(ERROR) << "input size:" << inputs.size(); - return RET_PARAM_INVALID; - } - if (outputs.size() != kBroadcastToOutputNum) { - MS_LOG(ERROR) << "output size:" << outputs.size(); - return RET_PARAM_INVALID; - } - - auto input = inputs.at(0); - outputs[0]->set_format(input->format()); - outputs[0]->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector dst_shape(GetDstShape()); - for (size_t i = 0; i < dst_shape.size(); ++i) { - if (dst_shape[i] == -1) { - dst_shape[i] = inputs[0]->shape()[i]; - } - } - auto input_shape = input->shape(); - std::vector shape(dst_shape.size()); - int input_shape_index = input_shape.size() - 1; - if (input_shape.size() > dst_shape.size()) { - MS_LOG(ERROR) << "input shape size " << input_shape.size() << " should <= broadcast to shape size " - << dst_shape.size() << "!"; - return RET_PARAM_INVALID; - } - - for (int i = dst_shape.size() - 1; i >= 0; --i) { - if (dst_shape[i] < 0) { - MS_LOG(ERROR) << "shape[" << i << "] = " << dst_shape[i] << " ] should be > 0!"; - return RET_PARAM_INVALID; - } - if (input_shape_index >= 0) { - auto dim = input_shape[input_shape_index]; - if (dim != dst_shape[i] && dim != 1) { - MS_LOG(ERROR) << "Invalid broadcast shape!"; - return RET_PARAM_INVALID; - } - } - shape[i] = dst_shape[i]; - --input_shape_index; - } - outputs[0]->set_shape(shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/broadcast_to.h b/mindspore/lite/src/ops/broadcast_to.h deleted file mode 100644 index 4794a38bac..0000000000 --- a/mindspore/lite/src/ops/broadcast_to.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ -#define LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class BroadcastTo : public PrimitiveC { - public: - BroadcastTo() = default; - ~BroadcastTo() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(BroadcastTo, PrimitiveC); - explicit BroadcastTo(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetDstShape(const std::vector &dst_shape); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetDstShape() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ diff --git a/mindspore/lite/src/ops/cast.cc b/mindspore/lite/src/ops/cast.cc deleted file mode 100644 index abebfe508e..0000000000 --- a/mindspore/lite/src/ops/cast.cc +++ /dev/null @@ -1,112 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/cast.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Cast::GetSrcT() const { return this->primitive_->value.AsCast()->srcT; } -int Cast::GetDstT() const { return this->primitive_->value.AsCast()->dstT; } - -void Cast::SetSrcT(int src_t) { this->primitive_->value.AsCast()->srcT = src_t; } -void Cast::SetDstT(int dst_t) { this->primitive_->value.AsCast()->dstT = dst_t; } - -int Cast::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Cast; - } - if (this->primitive_->value.type != schema::PrimitiveType_Cast) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::CastT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - auto srcAnf = reinterpret_cast(prim.GetAttr("SrcT").get()); - auto dstAnf = reinterpret_cast(prim.GetAttr("DstT").get()); - attr->srcT = srcAnf->number_type(); - attr->dstT = dstAnf->number_type(); - this->primitive_->value.value = attr; - } - - return RET_OK; -} - -#else -int Cast::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Cast(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Cast return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateCast(*fbb, attr->srcT(), attr->dstT()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Cast, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Cast::GetSrcT() const { return this->primitive_->value_as_Cast()->srcT(); } -int Cast::GetDstT() const { return this->primitive_->value_as_Cast()->dstT(); } - -PrimitiveC *CastCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry CastRegistry(schema::PrimitiveType_Cast, CastCreator); -#endif - -int Cast::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "tensor number is error."; - return RET_INPUT_TENSOR_ERROR; - } - output->set_format(input->format()); - - output->set_data_type(static_cast(GetDstT())); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - if (GetSrcT() != 0 && input->data_type() != GetSrcT()) { - MS_LOG(ERROR) << "input dataType is error"; - return RET_INPUT_TENSOR_ERROR; - } - if (kSupportDataType.find(input->data_type()) == kSupportDataType.end()) { - MS_LOG(ERROR) << "Unsupported input data type " << input->data_type(); - return RET_INPUT_TENSOR_ERROR; - } - - output->set_shape(input->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/cast.h b/mindspore/lite/src/ops/cast.h deleted file mode 100644 index 4ef1d67cce..0000000000 --- a/mindspore/lite/src/ops/cast.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CAST_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CAST_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Cast : public PrimitiveC { - public: - Cast() = default; - ~Cast() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Cast, PrimitiveC); - explicit Cast(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetSrcT(int src_t); - void SetDstT(int dst_t); - int UnPackAttr(const Primitive &prim, const std::vector &inputs); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetSrcT() const; - int GetDstT() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CAST_H_ diff --git a/mindspore/lite/src/ops/ceil.cc b/mindspore/lite/src/ops/ceil.cc deleted file mode 100644 index 208cf2ecac..0000000000 --- a/mindspore/lite/src/ops/ceil.cc +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/ceil.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -PrimitiveC *CeilCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry CeilRegistry(schema::PrimitiveType_Ceil, CeilCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/ceil.h b/mindspore/lite/src/ops/ceil.h deleted file mode 100644 index 41d56ac797..0000000000 --- a/mindspore/lite/src/ops/ceil.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CEIL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CEIL_H_ - -#include -#include -#include -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Ceil : public ArithmeticSelf { - public: - Ceil() = default; - ~Ceil() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Ceil, ArithmeticSelf); - explicit Ceil(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateCeil(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Ceil, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; - } -#endif -}; - -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CEIL_H_ diff --git a/mindspore/lite/src/ops/clip.cc b/mindspore/lite/src/ops/clip.cc deleted file mode 100644 index 9d545f871e..0000000000 --- a/mindspore/lite/src/ops/clip.cc +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/clip.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Clip::GetMax() const { return this->primitive_->value.AsClip()->max; } -float Clip::GetMin() const { return this->primitive_->value.AsClip()->min; } - -void Clip::SetMax(float max) { this->primitive_->value.AsClip()->max = max; } -void Clip::SetMin(float min) { this->primitive_->value.AsClip()->min = min; } - -#else -int Clip::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Clip(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Clip return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateClip(*fbb, attr->max(), attr->min()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Clip, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float Clip::GetMax() const { return this->primitive_->value_as_Clip()->max(); } -float Clip::GetMin() const { return this->primitive_->value_as_Clip()->min(); } - -PrimitiveC *ClipCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ClipRegistry(schema::PrimitiveType_Clip, ClipCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/clip.h b/mindspore/lite/src/ops/clip.h deleted file mode 100644 index 6c451d9e57..0000000000 --- a/mindspore/lite/src/ops/clip.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Clip : public PrimitiveC { - public: - Clip() = default; - ~Clip() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Clip, PrimitiveC); - explicit Clip(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetMax(float max); - void SetMin(float min); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetMax() const; - float GetMin() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ diff --git a/mindspore/lite/src/ops/compat/attr_transfer_common.cc b/mindspore/lite/src/ops/compat/attr_transfer_common.cc index 633482ea24..c981ba6f44 100644 --- a/mindspore/lite/src/ops/compat/attr_transfer_common.cc +++ b/mindspore/lite/src/ops/compat/attr_transfer_common.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/mindspore/lite/src/ops/compat/attr_transfer_common.h b/mindspore/lite/src/ops/compat/attr_transfer_common.h index 6ecf2be251..265db8db22 100644 --- a/mindspore/lite/src/ops/compat/attr_transfer_common.h +++ b/mindspore/lite/src/ops/compat/attr_transfer_common.h @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ #include "ir/dtype/type_id.h" #include "src/tensor.h" #include "include/errorcode.h" -#include "schema/model_v0_generated.h" #include "src/common/common.h" #include "src/ops/compat/compat_register.h" diff --git a/mindspore/lite/src/ops/compat/compat_register.h b/mindspore/lite/src/ops/compat/compat_register.h index 8285d1e7f2..61352d6dd5 100644 --- a/mindspore/lite/src/ops/compat/compat_register.h +++ b/mindspore/lite/src/ops/compat/compat_register.h @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,7 @@ namespace mindspore { namespace lite { // compatibility, transfer attr to input tensor. -typedef int (*TransferAttrFunc)(const void *primitive, Model::Node *node, std::vector *tensor, +typedef int (*TransferAttrFunc)(Model::Node *node, std::vector *tensor, std::vector *tensor_bufs); class CompatRegistry { public: diff --git a/mindspore/lite/src/ops/compat/v0/broadcast_to_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/broadcast_to_compat_v0.cc new file mode 100644 index 0000000000..fd944e821c --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/broadcast_to_compat_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferBroadcastToAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "broadcast_to don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto dst_shape_attr = prim->value_as_BroadcastTo()->dst_shape(); + std::vector dst_shape = std::vector(dst_shape_attr->begin(), dst_shape_attr->end()); + auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (dst_shape_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_shape_tensor); + return RET_OK; +} + +Register BroadcastToTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_BroadcastTo, + TransferBroadcastToAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/broadcat_to_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/broadcat_to_compat_v0.cc deleted file mode 100644 index 6959fd70b2..0000000000 --- a/mindspore/lite/src/ops/compat/v0/broadcat_to_compat_v0.cc +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/compat/attr_transfer_common.h" - -namespace mindspore { -namespace lite { -int TransferBroadcastToAttr(const void *primitive, Model::Node *node, std::vector *dst_tensors, - std::vector *tensor_bufs) { - if (primitive == nullptr || node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { - MS_LOG(ERROR) << "the parameter of this function is nullptr."; - return RET_ERROR; - } - if (node->input_indices_.size() != 1) { - MS_LOG(DEBUG) << "broadcast_to don't need to convert attr to tensor."; - return RET_OK; - } - dst_tensors->clear(); - auto prim = reinterpret_cast(primitive); - auto dst_shape_attr = prim->value_as_BroadcastTo()->dst_shape(); - std::vector dst_shape = std::vector(dst_shape_attr->begin(), dst_shape_attr->end()); - auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); - if (dst_shape_tensor == nullptr) { - MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; - return RET_NULL_PTR; - } - dst_tensors->push_back(dst_shape_tensor); - return RET_OK; -} - -Register BroadcastToTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_BroadcastTo, - TransferBroadcastToAttr); -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/cast_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/cast_compat_v0.cc new file mode 100644 index 0000000000..583f7cc93c --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/cast_compat_v0.cc @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferCastAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto dst_type_attr = prim->value_as_Cast()->dstT(); + auto dst_type_tensor = AttrToTensor(&dst_type_attr, 1, false, kNumberTypeInt32, tensor_bufs); + if (dst_type_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_type_tensor); + return RET_OK; +} + +Register CastTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Cast, TransferCastAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/expand_dims_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/expand_dims_compat_v0.cc new file mode 100644 index 0000000000..140784ba5a --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/expand_dims_compat_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferExpandDimsAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + MS_ASSERT(node->input_indices_.size() == 1); + MS_ASSERT(dst_tensors->size() == 0); + + auto prim = reinterpret_cast(node->primitive_); + int32_t dim = prim->value_as_ExpandDims()->dim(); + auto dim_tensor = AttrToTensor(&dim, 1, false, kNumberTypeInt32, tensor_bufs); + if (dim_tensor == nullptr) { + MS_LOG(ERROR) << "transfer expand dim tensor failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dim_tensor); + return RET_OK; +} + +Register ExpandDimsTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_ExpandDims, + TransferExpandDimsAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/fill_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/fill_compat_v0.cc new file mode 100644 index 0000000000..5ab1f62148 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/fill_compat_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferFillToAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "fill don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto dims_attr = prim->value_as_Fill()->dims(); + std::vector dims = std::vector(dims_attr->begin(), dims_attr->end()); + auto dims_tensor = AttrToTensor(dims.data(), dims.size(), true, kNumberTypeInt32, tensor_bufs); + if (dims_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dims_tensor); + return RET_OK; +} + +Register FillTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Fill, TransferFillToAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/gather_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/gather_compat_v0.cc new file mode 100644 index 0000000000..179e65a433 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/gather_compat_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferGatherAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto axis_attr = prim->value_as_Gather()->axis(); + auto axis_tensor = AttrToTensor(&axis_attr, 1, false, kNumberTypeInt32, tensor_bufs); + if (axis_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(axis_tensor); + return RET_OK; +} + +Register GatherTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Gather, TransferGatherAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/nchw2nhwc_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/nchw2nhwc_compat_v0.cc new file mode 100644 index 0000000000..1164ebd821 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/nchw2nhwc_compat_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferNchw2NhwcAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "nchw2nhwc don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + std::vector dst_shape{0, 2, 3, 1}; // nchw to nhwc + auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (dst_shape_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_shape_tensor); + return RET_OK; +} + +Register Nchw2NhwcTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Nchw2Nhwc, + TransferNchw2NhwcAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/nhwc2nchw_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/nhwc2nchw_compat_v0.cc new file mode 100644 index 0000000000..0d6297a7f2 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/nhwc2nchw_compat_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferNhwc2NchwAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "nhwc2nchw don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + std::vector dst_shape{0, 3, 1, 2}; // nhwc to nchw + auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (dst_shape_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_shape_tensor); + return RET_OK; +} + +Register Nhwc2NchwTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Nhwc2Nchw, + TransferNhwc2NchwAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/pad_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/pad_compat_v0.cc new file mode 100644 index 0000000000..f4fb1c5416 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/pad_compat_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferPadAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + dst_tensors->clear(); + if (node->input_indices_.size() > 1) { + MS_LOG(DEBUG) << "pad don't need to convert attr to tensor."; + return RET_OK; + } + auto prim = reinterpret_cast(node->primitive_); + auto paddings_attr = prim->value_as_Pad()->paddings(); + std::vector paddings = std::vector(paddings_attr->begin(), paddings_attr->end()); + auto paddings_tensor = AttrToTensor(paddings.data(), paddings.size(), true, kNumberTypeInt32, tensor_bufs); + if (paddings_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(paddings_tensor); + return RET_OK; +} + +Register PadTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Pad, TransferPadAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/permute_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/permute_compat_v0.cc new file mode 100644 index 0000000000..ec76fa82d9 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/permute_compat_v0.cc @@ -0,0 +1,51 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferPermuteAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "permute don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto order_attr = prim->value_as_Permute()->order(); + + std::vector dst_shape; + for (auto it = order_attr->begin(); it != order_attr->end(); ++it) { + dst_shape.push_back(static_cast(*it)); + } + auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (dst_shape_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_shape_tensor); + return RET_OK; +} + +Register PermuteTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Permute, TransferPermuteAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/power_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/power_compat_v0.cc new file mode 100644 index 0000000000..ea999154d9 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/power_compat_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferPowerToAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "power don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto power_attr = prim->value_as_Power()->power(); + auto power_tensor = AttrToTensor(&power_attr, 1, false, kNumberTypeFloat32, tensor_bufs); + if (power_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(power_tensor); + return RET_OK; +} + +Register PowerTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Power, TransferPowerToAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/reduce_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/reduce_compat_v0.cc new file mode 100644 index 0000000000..463f5edd10 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/reduce_compat_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferReduceToAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "fill don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto axes_attr = prim->value_as_Reduce()->axes(); + std::vector axes = std::vector(axes_attr->begin(), axes_attr->end()); + auto axes_tensor = AttrToTensor(axes.data(), axes.size(), true, kNumberTypeInt32, tensor_bufs); + if (axes_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(axes_tensor); + return RET_OK; +} + +Register ReduceTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Reduce, TransferReduceToAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/reshape_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/reshape_compat_v0.cc index 622900116f..ec919c898b 100644 --- a/mindspore/lite/src/ops/compat/v0/reshape_compat_v0.cc +++ b/mindspore/lite/src/ops/compat/v0/reshape_compat_v0.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,22 +14,23 @@ * limitations under the License. */ +#include "schema/model_v0_generated.h" #include "src/ops/compat/attr_transfer_common.h" namespace mindspore { namespace lite { -int TransferReshapeAttr(const void *primitive, Model::Node *node, std::vector *dst_tensors, +int TransferReshapeAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { - if (primitive == nullptr || node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { MS_LOG(ERROR) << "the parameter of this function is nullptr."; return RET_ERROR; } if (node->input_indices_.size() != 1) { - MS_LOG(DEBUG) << "reshape need to convert attr to tensor."; + MS_LOG(DEBUG) << "reshape don't need to convert attr to tensor."; return RET_OK; } dst_tensors->clear(); - auto prim = reinterpret_cast(primitive); + auto prim = reinterpret_cast(node->primitive_); auto dst_shape_attr = prim->value_as_Reshape()->shape(); std::vector dst_shape = std::vector(dst_shape_attr->begin(), dst_shape_attr->end()); auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); diff --git a/mindspore/lite/src/ops/compat/v0/slice_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/slice_compat_v0.cc new file mode 100644 index 0000000000..2472c7aa27 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/slice_compat_v0.cc @@ -0,0 +1,59 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferSliceAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { + if (node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "Slice don't need to convert attr to tensor."; + return RET_OK; + } + MS_ASSERT(dst_tensors->size() == 0); + auto prim = reinterpret_cast(node->primitive_); + + /* transfer begin tensor */ + auto begin_attr = prim->value_as_Slice()->begin(); + std::vector begin_shape = std::vector(begin_attr->begin(), begin_attr->end()); + auto begin_tensor = AttrToTensor(begin_shape.data(), begin_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (begin_tensor == nullptr) { + MS_LOG(ERROR) << "slice transfer begin failed"; + return RET_NULL_PTR; + } + dst_tensors->push_back(begin_tensor); + + /* transfer size tensor */ + auto size_attr = prim->value_as_Slice()->size(); + std::vector size_shape = std::vector(size_attr->begin(), size_attr->end()); + auto size_tensor = AttrToTensor(size_shape.data(), size_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (size_tensor == nullptr) { + MS_LOG(ERROR) << "slice transfer size failed"; + return RET_NULL_PTR; + } + dst_tensors->push_back(size_tensor); + + return RET_OK; +} + +Register SliceTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Slice, TransferSliceAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/strided_slice_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/strided_slice_compat_v0.cc index 74556895b7..5bbd541d96 100644 --- a/mindspore/lite/src/ops/compat/v0/strided_slice_compat_v0.cc +++ b/mindspore/lite/src/ops/compat/v0/strided_slice_compat_v0.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,18 +14,19 @@ * limitations under the License. */ +#include "schema/model_v0_generated.h" #include "src/ops/compat/attr_transfer_common.h" namespace mindspore { namespace lite { -int TransferStridedSliceAttr(const void *primitive, Model::Node *node, std::vector *dst_tensors, +int TransferStridedSliceAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { - if (primitive == nullptr || node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { MS_LOG(ERROR) << "the parameter of this function is nullptr."; return RET_ERROR; } dst_tensors->clear(); - auto prim = reinterpret_cast(primitive); + auto prim = reinterpret_cast(node->primitive_); int inputs_size = node->input_indices_.size(); switch (inputs_size) { case 1: { diff --git a/mindspore/lite/src/ops/compat/v0/tile_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/tile_compat_v0.cc new file mode 100644 index 0000000000..3960fef8eb --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/tile_compat_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferTileToAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "tile don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto multiples_attr = prim->value_as_Tile()->multiples(); + std::vector multiples = std::vector(multiples_attr->begin(), multiples_attr->end()); + auto multiples_tensor = AttrToTensor(multiples.data(), multiples.size(), true, kNumberTypeInt32, tensor_bufs); + if (multiples_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(multiples_tensor); + return RET_OK; +} + +Register TileTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Tile, TransferTileToAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/topk_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/topk_compat_v0.cc new file mode 100644 index 0000000000..48ce7bd234 --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/topk_compat_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferTopkAttr(Model::Node *node, std::vector *dst_tensors, std::vector *tensor_bufs) { + if (node == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "topK need to convert attr to tensor."; + return RET_OK; + } + MS_ASSERT(dst_tensors->size() == 0); + auto prim = reinterpret_cast(node->primitive_); + int32_t topk_k = prim->value_as_TopK()->k(); + auto k_tensor = AttrToTensor(&topk_k, 1, false, kNumberTypeInt32, tensor_bufs); + if (k_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(k_tensor); + return RET_OK; +} + +Register TopkTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_TopK, TransferTopkAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/compat/v0/transpose_compat_v0.cc b/mindspore/lite/src/ops/compat/v0/transpose_compat_v0.cc new file mode 100644 index 0000000000..b833e9659f --- /dev/null +++ b/mindspore/lite/src/ops/compat/v0/transpose_compat_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/compat/attr_transfer_common.h" + +namespace mindspore { +namespace lite { +int TransferTransposeAttr(Model::Node *node, std::vector *dst_tensors, + std::vector *tensor_bufs) { + if (node == nullptr || node->primitive_ == nullptr || dst_tensors == nullptr || tensor_bufs == nullptr) { + MS_LOG(ERROR) << "the parameter of this function is nullptr."; + return RET_ERROR; + } + if (node->input_indices_.size() != 1) { + MS_LOG(DEBUG) << "transpose don't need to convert attr to tensor."; + return RET_OK; + } + dst_tensors->clear(); + auto prim = reinterpret_cast(node->primitive_); + auto perm_attr = prim->value_as_Transpose()->perm(); + std::vector dst_shape = std::vector(perm_attr->begin(), perm_attr->end()); + auto dst_shape_tensor = AttrToTensor(dst_shape.data(), dst_shape.size(), true, kNumberTypeInt32, tensor_bufs); + if (dst_shape_tensor == nullptr) { + MS_LOG(ERROR) << "attr tensor is nullptr, transform is failed."; + return RET_NULL_PTR; + } + dst_tensors->push_back(dst_shape_tensor); + return RET_OK; +} + +Register TransposeTransferRegistry(SCHEMA_VERSION::SCHEMA_V0, schema::v0::PrimitiveType_Transpose, + TransferTransposeAttr); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/concat.cc b/mindspore/lite/src/ops/concat.cc deleted file mode 100644 index 45eff714a1..0000000000 --- a/mindspore/lite/src/ops/concat.cc +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/concat.h" -#include -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Concat::GetAxis() const { return this->primitive_->value.AsConcat()->axis; } - -void Concat::SetAxis(int axis) { this->primitive_->value.AsConcat()->axis = axis; } - -int Concat::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Concat; - } - if (this->primitive_->value.type != schema::PrimitiveType_Concat) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ConcatT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - auto prim_axis = CastToInt(prim.GetAttr("axis")).front(); - attr->axis = prim_axis; - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int Concat::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Concat(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Concat return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateConcat(*fbb, attr->axis()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Concat, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Concat::GetAxis() const { return this->primitive_->value_as_Concat()->axis(); } - -PrimitiveC *ConcatCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ConcatRegistry(schema::PrimitiveType_Concat, ConcatCreator); - -#endif - -namespace { -constexpr int kConcatOutputNum = 1; -} -int Concat::InferShape(std::vector inputs_, std::vector outputs_) { - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "primitive is nullptr!"; - return RET_PARAM_INVALID; - } - auto input0 = inputs_.front(); - auto output = outputs_.front(); - if (outputs_.size() != kConcatOutputNum) { - MS_LOG(ERROR) << "output size is error"; - return RET_PARAM_INVALID; - } - output->set_data_type(input0->data_type()); - output->set_format(input0->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto input0_shape = inputs_.at(0)->shape(); - auto axis = GetAxis() < 0 ? GetAxis() + input0_shape.size() : GetAxis(); - if (axis < 0 || axis >= input0_shape.size()) { - MS_LOG(ERROR) << "Invalid axis: " << axis; - return RET_PARAM_INVALID; - } - auto input0_shape_without_axis = input0_shape; - input0_shape_without_axis.erase(input0_shape_without_axis.begin() + axis); - int output_axis_dim = input0_shape.at(axis); - for (size_t i = 1; i < inputs_.size(); ++i) { - auto shape_tmp = inputs_.at(i)->shape(); - if (shape_tmp.size() != input0_shape.size()) { - MS_LOG(ERROR) << "All inputs should have the same dim num!"; - return RET_PARAM_INVALID; - } - if ((inputs_.at(i)->data_type() != output->data_type()) && - !((inputs_.at(i)->data_type() == kNumberTypeFloat16 && output->data_type() == kNumberTypeFloat32) || - (inputs_.at(i)->data_type() == kNumberTypeFloat32 && output->data_type() == kNumberTypeFloat16))) { - MS_LOG(ERROR) << "All inputs should have the same type!"; - return RET_PARAM_INVALID; - } - auto axis_tmp = shape_tmp[axis]; - shape_tmp.erase(shape_tmp.begin() + axis); - if (input0_shape_without_axis != shape_tmp) { - MS_LOG(ERROR) << "Inputs should have the same dim except axis!"; - return RET_PARAM_INVALID; - } - output_axis_dim += axis_tmp; - } - auto output_shape = input0_shape; - output_shape[axis] = output_axis_dim; - outputs_[0]->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/concat.h b/mindspore/lite/src/ops/concat.h deleted file mode 100644 index c12c7f94d3..0000000000 --- a/mindspore/lite/src/ops/concat.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Concat : public PrimitiveC { - public: - Concat() = default; - ~Concat() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Concat, PrimitiveC); - explicit Concat(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetAxis(int axis); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetAxis() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ diff --git a/mindspore/lite/src/ops/constant.h b/mindspore/lite/src/ops/constant.h deleted file mode 100644 index 659331c650..0000000000 --- a/mindspore/lite/src/ops/constant.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifdef PRIMITIVE_WRITEABLE -#ifndef LITE_MINDSPORE_LITE_C_OPS_CONSTANT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CONSTANT_H_ - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Constant : public PrimitiveC { - public: - Constant() = default; - ~Constant() = default; - MS_DECLARE_PARENT(Constant, PrimitiveC); - explicit Constant(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CONSTANT_H_ -#endif diff --git a/mindspore/lite/src/ops/constant_of_shape.cc b/mindspore/lite/src/ops/constant_of_shape.cc deleted file mode 100644 index 5e5a78bce7..0000000000 --- a/mindspore/lite/src/ops/constant_of_shape.cc +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/constant_of_shape.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore::lite { -namespace { -constexpr int kShapeInputNum = 1; -constexpr int kShapeOutputNum = 1; -} // namespace -#ifdef PRIMITIVE_WRITEABLE -std::vector ConstantOfShape::GetValue() const { return this->primitive_->value.AsConstantOfShape()->value; } - -int ConstantOfShape::GetDataType() const { return this->primitive_->value.AsConstantOfShape()->dataType; } - -#else -int ConstantOfShape::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ConstantOfShape(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ConstantOfShape return nullptr"; - return RET_ERROR; - } - std::vector value; - if (attr->value() != nullptr) { - for (int i = 0; i < static_cast(attr->value()->size()); i++) { - value.push_back(attr->value()->data()[i]); - } - } - auto val_offset = schema::CreateConstantOfShapeDirect(*fbb, attr->dataType(), &value); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ConstantOfShape, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -std::vector ConstantOfShape::GetValue() const { - auto fb_vector = this->primitive_->value_as_ConstantOfShape()->value(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int ConstantOfShape::GetDataType() const { return this->primitive_->value_as_ConstantOfShape()->dataType(); } - -PrimitiveC *ConstantOfShapeCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ConstantOfShapeRegistry(schema::PrimitiveType_ConstantOfShape, ConstantOfShapeCreator); - -#endif - -int ConstantOfShape::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kShapeInputNum) { - MS_LOG(ERROR) << "inputs to ConstantOfShape operator should be 1, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - if (inputs_.front() == nullptr) { - MS_LOG(ERROR) << "primitive is nullptr!"; - return RET_PARAM_INVALID; - } - if (outputs_.size() != kShapeOutputNum) { - MS_LOG(ERROR) << "outputs to ConstantOfShape operator should be 1, but " << outputs_.size() << " is given."; - return RET_ERROR; - } - - auto in_tensor = inputs_.front(); - auto out_tensor = outputs_.front(); - out_tensor->set_data_type(static_cast(GetDataType())); - out_tensor->set_format(in_tensor->format()); - - if (!infer_flag() || in_tensor->data_c() == nullptr) { - return RET_INFER_INVALID; - } - - int size = in_tensor->ElementsNum(); - std::vector out_shape(size); - - switch (in_tensor->data_type()) { - case kNumberTypeInt32: { - int32_t *in_data = reinterpret_cast(in_tensor->data_c()); - for (int i = 0; i < size; ++i) { - out_shape[i] = in_data[i]; - MS_ASSERT(out_shape[i] > 0); - } - break; - } - case kNumberTypeInt64: { - int64_t *in_data = reinterpret_cast(in_tensor->data_c()); - for (int i = 0; i < size; ++i) { - out_shape[i] = in_data[i]; - MS_ASSERT(out_shape[i] > 0); - } - break; - } - default: - MS_LOG(INFO) << "Invalid input data type!"; - return RET_INFER_INVALID; - } - - out_tensor->set_shape(out_shape); - return RET_OK; -} -} // namespace mindspore::lite diff --git a/mindspore/lite/src/ops/constant_of_shape.h b/mindspore/lite/src/ops/constant_of_shape.h deleted file mode 100644 index a72979a62a..0000000000 --- a/mindspore/lite/src/ops/constant_of_shape.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_CONSTANT_OF_SHAPE_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_CONSTANT_OF_SHAPE_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ConstantOfShape : public PrimitiveC { - public: - ConstantOfShape() = default; - ~ConstantOfShape() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ConstantOfShape, PrimitiveC); - explicit ConstantOfShape(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetValue() const; - int GetDataType() const; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_SRC_OPS_CONSTANT_OF_SHAPE_H_ diff --git a/mindspore/lite/src/ops/control_depend.cc b/mindspore/lite/src/ops/control_depend.cc deleted file mode 100644 index c5296bdd17..0000000000 --- a/mindspore/lite/src/ops/control_depend.cc +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/control_depend.h" -#include -#include - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ControlDepend::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ControlDepend; - } - if (this->primitive_->value.type != schema::PrimitiveType_ControlDepend) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow)(schema::ControlDependT); - if (attr == nullptr) { - MS_LOG(ERROR) << "attr is nullptr"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#else -int ControlDepend::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateControlDepend(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ControlDepend, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/control_depend.h b/mindspore/lite/src/ops/control_depend.h deleted file mode 100644 index 0737dbc4c2..0000000000 --- a/mindspore/lite/src/ops/control_depend.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_CONTROL_DEPEND_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_CONTROL_DEPEND_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ControlDepend : public PrimitiveC { - public: - ControlDepend() = default; - ~ControlDepend() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ControlDepend, PrimitiveC); - explicit ControlDepend(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_SRC_OPS_CONTROL_DEPEND_H_ diff --git a/mindspore/lite/src/ops/conv2d.cc b/mindspore/lite/src/ops/conv2d.cc deleted file mode 100644 index c493586503..0000000000 --- a/mindspore/lite/src/ops/conv2d.cc +++ /dev/null @@ -1,417 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/conv2d.h" - -#include -#include -#include - -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -int Conv2D::PadUp() const { return this->pad_u_; } -int Conv2D::PadDown() const { return this->pad_d_; } -int Conv2D::PadLeft() const { return this->pad_l_; } -int Conv2D::PadRight() const { return this->pad_r_; } -#ifdef PRIMITIVE_WRITEABLE -int Conv2D::GetFormat() const { return this->primitive_->value.AsConv2D()->format; } -int Conv2D::GetGroup() const { return this->primitive_->value.AsConv2D()->group; } -int Conv2D::GetChannelIn() const { return this->primitive_->value.AsConv2D()->channelIn; } -int Conv2D::GetChannelOut() const { return this->primitive_->value.AsConv2D()->channelOut; } -int Conv2D::GetKernelW() const { return this->primitive_->value.AsConv2D()->kernelW; } -int Conv2D::GetKernelH() const { return this->primitive_->value.AsConv2D()->kernelH; } -int Conv2D::GetStrideW() const { return this->primitive_->value.AsConv2D()->strideW; } -int Conv2D::GetStrideH() const { return this->primitive_->value.AsConv2D()->strideH; } -int Conv2D::GetPadMode() const { return this->primitive_->value.AsConv2D()->padMode; } -int Conv2D::GetPadUp() const { return this->primitive_->value.AsConv2D()->padUp; } -int Conv2D::GetPadDown() const { return this->primitive_->value.AsConv2D()->padDown; } -int Conv2D::GetPadLeft() const { return this->primitive_->value.AsConv2D()->padLeft; } -int Conv2D::GetPadRight() const { return this->primitive_->value.AsConv2D()->padRight; } -int Conv2D::GetDilateW() const { return this->primitive_->value.AsConv2D()->dilateW; } -int Conv2D::GetDilateH() const { return this->primitive_->value.AsConv2D()->dilateH; } -int Conv2D::GetActivationType() const { return this->primitive_->value.AsConv2D()->activationType; } - -void Conv2D::SetFormat(int format) { this->primitive_->value.AsConv2D()->format = (schema::Format)format; } -void Conv2D::SetGroup(int group) { this->primitive_->value.AsConv2D()->group = group; } -void Conv2D::SetChannelIn(int channel_in) { this->primitive_->value.AsConv2D()->channelIn = channel_in; } -void Conv2D::SetChannelOut(int channel_out) { this->primitive_->value.AsConv2D()->channelOut = channel_out; } -void Conv2D::SetKernelW(int kernel_w) { this->primitive_->value.AsConv2D()->kernelW = kernel_w; } -void Conv2D::SetKernelH(int kernel_h) { this->primitive_->value.AsConv2D()->kernelH = kernel_h; } -void Conv2D::SetStrideW(int stride_w) { this->primitive_->value.AsConv2D()->strideW = stride_w; } -void Conv2D::SetStrideH(int stride_h) { this->primitive_->value.AsConv2D()->strideH = stride_h; } -void Conv2D::SetPadMode(int pad_mode) { this->primitive_->value.AsConv2D()->padMode = (schema::PadMode)pad_mode; } -void Conv2D::SetPadUp(int pad_up) { this->primitive_->value.AsConv2D()->padUp = pad_up; } -void Conv2D::SetPadDown(int pad_down) { this->primitive_->value.AsConv2D()->padDown = pad_down; } -void Conv2D::SetPadLeft(int pad_left) { this->primitive_->value.AsConv2D()->padLeft = pad_left; } -void Conv2D::SetPadRight(int pad_right) { this->primitive_->value.AsConv2D()->padRight = pad_right; } -void Conv2D::SetDilateW(int dilate_w) { this->primitive_->value.AsConv2D()->dilateW = dilate_w; } -void Conv2D::SetDilateH(int dilate_h) { this->primitive_->value.AsConv2D()->dilateH = dilate_h; } -void Conv2D::SetActivationType(int activation_type) { - this->primitive_->value.AsConv2D()->activationType = (schema::ActivationType)activation_type; -} -template -void ConvertConvWeight(const ParameterPtr ¶m_node) { - MS_ASSERT(param_node != nullptr); - auto param = param_node->default_param(); - auto weight = std::dynamic_pointer_cast(param); - MS_ASSERT(weight != nullptr); - - std::unique_ptr buf(new (std::nothrow) T[weight->tensor_shape_size()]); - - if (buf == nullptr) { - MS_LOG(ERROR) << "new buf failed"; - return; - } - - size_t filter_k = weight->tensor_shape().at(0); - size_t filter_c = weight->tensor_shape().at(1); - size_t filter_h = weight->tensor_shape().at(2); - size_t filter_w = weight->tensor_shape().at(3); - T *p1Buff = nullptr; - T *p2Buff = nullptr; - for (size_t k = 0; k < filter_k; ++k) { - for (size_t c = 0; c < filter_c; ++c) { - for (size_t h = 0; h < filter_h; ++h) { - for (size_t w = 0; w < filter_w; ++w) { - p1Buff = reinterpret_cast(weight->tensor_addr()) + - ((k * filter_c * filter_h * filter_w) + (c * filter_h * filter_w) + (h * filter_w) + (w)); - p2Buff = - buf.get() + ((c * filter_k * filter_h * filter_w) + (k * filter_h * filter_w) + (h * filter_w) + (w)); - *p2Buff = *p1Buff; - } - } - } - } - - auto ret = ::memcpy_s(weight->tensor_addr(), weight->tensor_shape_size() * sizeof(T), buf.get(), - weight->tensor_shape_size() * sizeof(T)); - if (ret != EOK) { - MS_LOG(ERROR) << "memcpy_s failed: " << ret; - return; - } - - auto abstract_base = param_node->abstract(); - MS_ASSERT(abstract_base != nullptr); - if (utils::isa(abstract_base)) { - auto abstract_tensor = utils::cast(abstract_base); - utils::cast(abstract_tensor->BuildShape())->shape()[0] = filter_c; - utils::cast(abstract_tensor->BuildShape())->shape()[1] = filter_k; - utils::cast(abstract_tensor->BuildShape())->shape()[2] = filter_h; - utils::cast(abstract_tensor->BuildShape())->shape()[3] = filter_w; - weight->set_tensor_shape( - {static_cast(filter_c), static_cast(filter_k), static_cast(filter_h), static_cast(filter_w)}); - } - return; -} -void Conv2D::PopulaterConv2DMultiGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group, - const std::vector &inputs) { - auto attr = std::make_unique(); - if (attr.get() == nullptr) { - MS_LOG(ERROR) << "Memory allocation failed"; - return; - } - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - if (train_flag()) { - attr->dilateH = dilation.at(2); - attr->dilateW = dilation.at(3); - } else { - attr->dilateH = dilation.at(0); - attr->dilateW = dilation.at(1); - } - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = (kernel_size.size() > 1) ? kernel_size.at(1) : kernel_size.at(0); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(2); - attr->strideW = stride.at(3); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - int channel_mutiplier = 1; - if (prim.GetAttr("channel_mutiplier") != nullptr) { - channel_mutiplier = CastToInt(prim.GetAttr("channel_multiplier")).front(); - } - attr->channelMultiplier = channel_mutiplier; - - MS_ASSERT(inputs.size() == kAnfPopulaterInputNumTwo); - auto input_node = inputs.at(kAnfPopulaterInputNumOne); - MS_ASSERT(input_node != nullptr); - if (input_node->isa()) { - auto param_node = input_node->cast(); - ConvertConvWeight(param_node); - auto abstractBase = param_node->abstract(); - MS_ASSERT(abstractBase != nullptr); - if (utils::isa(abstractBase)) { - auto abstractTensor = utils::cast(abstractBase); - MS_ASSERT(abstractTensor != nullptr); - if (utils::isa(abstractTensor->BuildShape())) { - auto dims = utils::cast(abstractTensor->BuildShape())->shape(); - attr->channelIn = dims.at(kAnfPopulaterInputNumOne); - } - } - } else if (input_node->isa()) { - // The weight of convolution is the output from the other operators which could be folded by const folding pass. - attr->channelIn = -1; - } - - primitive->value.type = schema::PrimitiveType_DepthwiseConv2D; - primitive->value.value = attr.release(); -} - -void Conv2D::PopulaterConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group) { - auto attr = std::make_unique(); - if (attr.get() == nullptr) { - MS_LOG(ERROR) << "Memory allocation failed"; - return; - } - attr->group = group; - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(2); - attr->dilateW = dilation.at(3); - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = (kernel_size.size() > 1) ? kernel_size.at(1) : kernel_size.at(0); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(2); - attr->strideW = stride.at(3); - - attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - primitive->value.type = schema::PrimitiveType_Conv2D; - primitive->value.value = attr.release(); -} - -int Conv2D::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Conv2D; - } - if (this->primitive_->value.type != schema::PrimitiveType_Conv2D) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - auto groupAttr = prim.GetAttr("group"); - if (groupAttr == nullptr) { - MS_LOG(ERROR) << "conv2d op has no group attr,please check pb model"; - return RET_NULL_PTR; - } - int group = CastToInt(groupAttr).front(); - if (group > 1) { - PopulaterConv2DMultiGroup(prim, this->primitive_, group, inputs); - } else { - PopulaterConv2DSingleGroup(prim, this->primitive_, group); - } - - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int Conv2D::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Conv2D(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Conv2D return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateConv2D( - *fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Conv2D, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Conv2D::GetFormat() const { return this->primitive_->value_as_Conv2D()->format(); } -int Conv2D::GetGroup() const { return this->primitive_->value_as_Conv2D()->group(); } -int Conv2D::GetChannelIn() const { return this->primitive_->value_as_Conv2D()->channelIn(); } -int Conv2D::GetChannelOut() const { return this->primitive_->value_as_Conv2D()->channelOut(); } -int Conv2D::GetKernelW() const { return this->primitive_->value_as_Conv2D()->kernelW(); } -int Conv2D::GetKernelH() const { return this->primitive_->value_as_Conv2D()->kernelH(); } -int Conv2D::GetStrideW() const { return this->primitive_->value_as_Conv2D()->strideW(); } -int Conv2D::GetStrideH() const { return this->primitive_->value_as_Conv2D()->strideH(); } -int Conv2D::GetPadMode() const { return this->primitive_->value_as_Conv2D()->padMode(); } -int Conv2D::GetPadUp() const { return this->primitive_->value_as_Conv2D()->padUp(); } -int Conv2D::GetPadDown() const { return this->primitive_->value_as_Conv2D()->padDown(); } -int Conv2D::GetPadLeft() const { return this->primitive_->value_as_Conv2D()->padLeft(); } -int Conv2D::GetPadRight() const { return this->primitive_->value_as_Conv2D()->padRight(); } -int Conv2D::GetDilateW() const { return this->primitive_->value_as_Conv2D()->dilateW(); } -int Conv2D::GetDilateH() const { return this->primitive_->value_as_Conv2D()->dilateH(); } -int Conv2D::GetActivationType() const { return this->primitive_->value_as_Conv2D()->activationType(); } - -PrimitiveC *Conv2DCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry Conv2DRegistry(schema::PrimitiveType_Conv2D, Conv2DCreator); -#endif - -void Conv2D::ConvInferShape(int input_h, int input_w, int *output_h, int *output_w) { - MS_ASSERT(this->primitive_ != nullptr); - int kernel_w = GetKernelW(); - int kernel_h = GetKernelH(); - int stride_w = GetStrideW(); - int stride_h = GetStrideH(); - int dilate_w = GetDilateW(); - int dilate_h = GetDilateH(); - - if (GetPadMode() == schema::PadMode_SAME_UPPER) { - *output_w = std::ceil(static_cast(input_w) / static_cast(stride_w)); - *output_h = std::ceil(static_cast(input_h) / static_cast(stride_h)); - auto pad_h_all = ((*output_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - input_h); - auto pad_w_all = ((*output_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - input_w); - if (pad_h_all < 0) { - pad_u_ = pad_d_ = 0; - } else { - pad_u_ = pad_h_all / 2; - pad_d_ = pad_h_all - pad_u_; - } - if (pad_w_all < 0) { - pad_l_ = pad_r_ = 0; - } else { - pad_l_ = pad_w_all / 2; - pad_r_ = pad_w_all - pad_l_; - } - } else { - *output_w = std::ceil((static_cast(input_w) + pad_l_ + pad_r_ - - (static_cast(kernel_w) - 1) * static_cast(dilate_w)) / - static_cast(stride_w)); - *output_h = std::ceil((static_cast(input_h) + pad_u_ + pad_d_ - - (static_cast(kernel_h) - 1) * static_cast(dilate_h)) / - static_cast(stride_h)); - } -} - -int Conv2D::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != 2 && inputs_.size() != 3) { - MS_LOG(ERROR) << "Conv2d should has two or three inputs"; - return RET_ERROR; - } - if (outputs_.size() != 1) { - MS_LOG(ERROR) << "Conv2d should has one outputs"; - return RET_ERROR; - } - auto *input_tensor = inputs_.front(); - auto *weight_tensor = inputs_.at(1); - auto *out_tensor = outputs_.front(); - MS_ASSERT(input_tensor != nullptr); - MS_ASSERT(out_tensor != nullptr); - - out_tensor->set_format(input_tensor->format()); - out_tensor->set_data_type(input_tensor->data_type()); - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto in_shape = input_tensor->shape(); - if (in_shape.size() == 0) { - return RET_INFER_INVALID; - } - int input_h = in_shape.at(1); - int input_w = in_shape.at(2); - int output_w = 0, output_h = 0; - - this->ConvInferShape(input_h, input_w, &output_h, &output_w); - - std::vector out_shape{input_tensor->shape()}; - out_shape.at(1) = output_h >= 0 ? output_h : 1; - out_shape.at(2) = output_w >= 0 ? output_w : 1; - out_shape.at(3) = weight_tensor->shape()[0]; - out_tensor->set_shape(out_shape); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/conv2d.h b/mindspore/lite/src/ops/conv2d.h deleted file mode 100644 index c40e3ac61b..0000000000 --- a/mindspore/lite/src/ops/conv2d.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Conv2D : public PrimitiveC { - public: - Conv2D() = default; - ~Conv2D() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Conv2D, PrimitiveC); - explicit Conv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - virtual void SetFormat(int format); - virtual void SetGroup(int group); - virtual void SetChannelIn(int channel_in); - virtual void SetChannelOut(int channel_out); - virtual void SetKernelW(int kernel_w); - virtual void SetKernelH(int kernel_h); - virtual void SetStrideW(int stride_w); - virtual void SetStrideH(int stride_h); - virtual void SetPadMode(int pad_mode); - virtual void SetPadUp(int pad_up); - virtual void SetPadDown(int pad_down); - virtual void SetPadLeft(int pad_left); - virtual void SetPadRight(int pad_right); - virtual void SetDilateW(int dilate_w); - virtual void SetDilateH(int dilate_h); - virtual void SetActivationType(int activation_type); - - private: - void PopulaterConv2DMultiGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group, - const std::vector &inputs); - void PopulaterConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - - public: - int InferShape(std::vector inputs_, std::vector outputs_) override; - int PadUp() const; - int PadDown() const; - int PadLeft() const; - int PadRight() const; - - virtual int GetFormat() const; - virtual int GetGroup() const; - virtual int GetChannelIn() const; - virtual int GetChannelOut() const; - virtual int GetKernelW() const; - virtual int GetKernelH() const; - virtual int GetStrideW() const; - virtual int GetStrideH() const; - virtual int GetPadMode() const; - virtual int GetPadUp() const; - virtual int GetPadDown() const; - virtual int GetPadLeft() const; - virtual int GetPadRight() const; - virtual int GetDilateW() const; - virtual int GetDilateH() const; - virtual int GetActivationType() const; - - protected: - void ConvInferShape(int input_h, int input_w, int *output_h, int *output_w); - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ diff --git a/mindspore/lite/src/ops/conv2d_grad_filter.cc b/mindspore/lite/src/ops/conv2d_grad_filter.cc deleted file mode 100644 index 3963d962d4..0000000000 --- a/mindspore/lite/src/ops/conv2d_grad_filter.cc +++ /dev/null @@ -1,244 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/conv2d_grad_filter.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Conv2DGradFilter::GetFormat() const { return this->primitive_->value.AsConv2DGradFilter()->format; } -int Conv2DGradFilter::GetGroup() const { return this->primitive_->value.AsConv2DGradFilter()->group; } -int Conv2DGradFilter::GetChannelIn() const { return this->primitive_->value.AsConv2DGradFilter()->channelIn; } -int Conv2DGradFilter::GetChannelOut() const { return this->primitive_->value.AsConv2DGradFilter()->channelOut; } -int Conv2DGradFilter::GetKernelW() const { return this->primitive_->value.AsConv2DGradFilter()->kernelW; } -int Conv2DGradFilter::GetKernelH() const { return this->primitive_->value.AsConv2DGradFilter()->kernelH; } -int Conv2DGradFilter::GetStrideW() const { return this->primitive_->value.AsConv2DGradFilter()->strideW; } -int Conv2DGradFilter::GetStrideH() const { return this->primitive_->value.AsConv2DGradFilter()->strideH; } -int Conv2DGradFilter::GetPadMode() const { return this->primitive_->value.AsConv2DGradFilter()->padMode; } -int Conv2DGradFilter::GetPadUp() const { return this->primitive_->value.AsConv2DGradFilter()->padUp; } -int Conv2DGradFilter::GetPadDown() const { return this->primitive_->value.AsConv2DGradFilter()->padDown; } -int Conv2DGradFilter::GetPadLeft() const { return this->primitive_->value.AsConv2DGradFilter()->padLeft; } -int Conv2DGradFilter::GetPadRight() const { return this->primitive_->value.AsConv2DGradFilter()->padRight; } -int Conv2DGradFilter::GetDilateW() const { return this->primitive_->value.AsConv2DGradFilter()->dilateW; } -int Conv2DGradFilter::GetDilateH() const { return this->primitive_->value.AsConv2DGradFilter()->dilateH; } - -int Conv2DGradFilter::GetActivationType() const { return this->primitive_->value.AsConv2DGradFilter()->activationType; } - -void Conv2DGradFilter::SetFormat(int format) { - this->primitive_->value.AsConv2DGradFilter()->format = (schema::Format)format; -} -void Conv2DGradFilter::SetGroup(int group) { this->primitive_->value.AsConv2DGradFilter()->group = group; } -void Conv2DGradFilter::SetChannelIn(int channel_in) { - this->primitive_->value.AsConv2DGradFilter()->channelIn = channel_in; -} -void Conv2DGradFilter::SetChannelOut(int channel_out) { - this->primitive_->value.AsConv2DGradFilter()->channelOut = channel_out; -} -void Conv2DGradFilter::SetKernelW(int kernel_w) { this->primitive_->value.AsConv2DGradFilter()->kernelW = kernel_w; } -void Conv2DGradFilter::SetKernelH(int kernel_h) { this->primitive_->value.AsConv2DGradFilter()->kernelH = kernel_h; } -void Conv2DGradFilter::SetStrideW(int stride_w) { this->primitive_->value.AsConv2DGradFilter()->strideW = stride_w; } -void Conv2DGradFilter::SetStrideH(int stride_h) { this->primitive_->value.AsConv2DGradFilter()->strideH = stride_h; } -void Conv2DGradFilter::SetPadMode(int pad_mode) { - this->primitive_->value.AsConv2DGradFilter()->padMode = (schema::PadMode)pad_mode; -} -void Conv2DGradFilter::SetPadUp(int pad_up) { this->primitive_->value.AsConv2DGradFilter()->padUp = pad_up; } -void Conv2DGradFilter::SetPadDown(int pad_down) { this->primitive_->value.AsConv2DGradFilter()->padDown = pad_down; } -void Conv2DGradFilter::SetPadLeft(int pad_left) { this->primitive_->value.AsConv2DGradFilter()->padLeft = pad_left; } -void Conv2DGradFilter::SetPadRight(int pad_right) { - this->primitive_->value.AsConv2DGradFilter()->padRight = pad_right; -} -void Conv2DGradFilter::SetDilateW(int dilate_w) { this->primitive_->value.AsConv2DGradFilter()->dilateW = dilate_w; } -void Conv2DGradFilter::SetDilateH(int dilate_h) { this->primitive_->value.AsConv2DGradFilter()->dilateH = dilate_h; } -std::vector Conv2DGradFilter::GetFilterShape() const { - return this->primitive_->value.AsConv2DGradFilter()->filter_shape; -} -void Conv2DGradFilter::SetActivationType(int activation_type) { - this->primitive_->value.AsConv2DGradFilter()->activationType = (schema::ActivationType)activation_type; -} - -int Conv2DGradFilter::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Conv2DGradFilter; - } - if (this->primitive_->value.type != schema::PrimitiveType_Conv2DGradFilter) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::Conv2DGradFilterT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->group = CastToInt(prim.GetAttr("group")).front(); - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format_NHWC; - } else { - attr->format = schema::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(2); - attr->dilateW = dilation.at(3); - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = (kernel_size.size() > 1) ? kernel_size.at(1) : kernel_size.at(0); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(0); - attr->strideW = stride.at(1); - - attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - if (inputs.size() >= kAnfPopulaterInputNumThree) { - auto filter_shape = inputs[kAnfPopulaterInputNumTwo]; - MS_ASSERT(filter_shape != nullptr); - if (filter_shape->isa()) { - auto valueNode = filter_shape->cast(); - MS_ASSERT(valueNode != nullptr); - auto value = valueNode->value(); - MS_ASSERT(value != nullptr); - if (value->isa()) { - auto valTuplPtr = dyn_cast(value); - MS_ASSERT(valTuplPtr != nullptr); - const int nchw2nhwc[] = {0, 3, 1, 2}; - attr->filter_shape.resize(valTuplPtr->size()); - for (size_t i = 0; i < valTuplPtr->size(); i++) { - auto elem = (*valTuplPtr)[i]; - MS_ASSERT(elem != nullptr); - attr->filter_shape[nchw2nhwc[i]] = CastToInt(elem).front(); - } - } - } - } - - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Conv2DGradFilter::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Conv2DGradFilter(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Conv2DGradFilter return nullptr"; - return RET_ERROR; - } - std::vector filter_shape; - if (attr->filter_shape() != nullptr) { - for (int i = 0; i < static_cast(attr->filter_shape()->size()); i++) { - filter_shape.push_back(attr->filter_shape()->data()[i]); - } - } - auto val_offset = schema::CreateConv2DGradFilterDirect( - *fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), &filter_shape, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Conv2DGradFilter, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Conv2DGradFilter::GetFormat() const { return this->primitive_->value_as_Conv2DGradFilter()->format(); } -int Conv2DGradFilter::GetGroup() const { return this->primitive_->value_as_Conv2DGradFilter()->group(); } -int Conv2DGradFilter::GetChannelIn() const { return this->primitive_->value_as_Conv2DGradFilter()->channelIn(); } -int Conv2DGradFilter::GetChannelOut() const { return this->primitive_->value_as_Conv2DGradFilter()->channelOut(); } -int Conv2DGradFilter::GetKernelW() const { return this->primitive_->value_as_Conv2DGradFilter()->kernelW(); } -int Conv2DGradFilter::GetKernelH() const { return this->primitive_->value_as_Conv2DGradFilter()->kernelH(); } -int Conv2DGradFilter::GetStrideW() const { return this->primitive_->value_as_Conv2DGradFilter()->strideW(); } -int Conv2DGradFilter::GetStrideH() const { return this->primitive_->value_as_Conv2DGradFilter()->strideH(); } -int Conv2DGradFilter::GetPadMode() const { return this->primitive_->value_as_Conv2DGradFilter()->padMode(); } -int Conv2DGradFilter::GetPadUp() const { return this->primitive_->value_as_Conv2DGradFilter()->padUp(); } -int Conv2DGradFilter::GetPadDown() const { return this->primitive_->value_as_Conv2DGradFilter()->padDown(); } -int Conv2DGradFilter::GetPadLeft() const { return this->primitive_->value_as_Conv2DGradFilter()->padLeft(); } -int Conv2DGradFilter::GetPadRight() const { return this->primitive_->value_as_Conv2DGradFilter()->padRight(); } -int Conv2DGradFilter::GetDilateW() const { return this->primitive_->value_as_Conv2DGradFilter()->dilateW(); } -int Conv2DGradFilter::GetDilateH() const { return this->primitive_->value_as_Conv2DGradFilter()->dilateH(); } -std::vector Conv2DGradFilter::GetFilterShape() const { - auto fb_vector = this->primitive_->value_as_Conv2DGradFilter()->filter_shape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Conv2DGradFilter::GetActivationType() const { - return this->primitive_->value_as_Conv2DGradFilter()->activationType(); -} - -PrimitiveC *Conv2DGradFilterCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry conv2DGradFilterRegistry(schema::PrimitiveType_Conv2DGradFilter, Conv2DGradFilterCreator); -#endif - -int Conv2DGradFilter::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() < 2) { - MS_LOG(ERROR) << "Conv2d Grad Filter should be at least two input, but it got " << inputs.size(); - return RET_ERROR; - } - if (outputs.size() != 1) { - MS_LOG(ERROR) << "Conv2d Grad Filter should have one output but it got " << outputs.size(); - return RET_ERROR; - } - - auto *in0 = inputs.at(0); - MS_ASSERT(in0 != nullptr); - - auto *out = outputs.at(0); - MS_ASSERT(out != nullptr); - - out->set_shape(GetFilterShape()); - out->set_data_type(in0->data_type()); - out->set_format(in0->format()); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/conv2d_grad_filter.h b/mindspore/lite/src/ops/conv2d_grad_filter.h deleted file mode 100644 index bf538c45bf..0000000000 --- a/mindspore/lite/src/ops/conv2d_grad_filter.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_FILTER_H_ -#define MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_FILTER_H_ - -#include -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Conv2DGradFilter : public PrimitiveC { - public: - Conv2DGradFilter() = default; - ~Conv2DGradFilter() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Conv2DGradFilter, PrimitiveC); - explicit Conv2DGradFilter(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetGroup(int group); - void SetChannelIn(int channel_in); - void SetChannelOut(int channel_out); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetGroup() const; - int GetChannelIn() const; - int GetChannelOut() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; - std::vector GetFilterShape() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_FILTER_H_ diff --git a/mindspore/lite/src/ops/conv2d_grad_input.cc b/mindspore/lite/src/ops/conv2d_grad_input.cc deleted file mode 100644 index 83c8f88b95..0000000000 --- a/mindspore/lite/src/ops/conv2d_grad_input.cc +++ /dev/null @@ -1,244 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/conv2d_grad_input.h" -#include "src/ops/group_conv2d_grad_input.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Conv2DGradInput::GetFormat() const { return this->primitive_->value.AsConv2DGradInput()->format; } -int Conv2DGradInput::GetGroup() const { return this->primitive_->value.AsConv2DGradInput()->group; } -int Conv2DGradInput::GetChannelIn() const { return this->primitive_->value.AsConv2DGradInput()->channelIn; } -int Conv2DGradInput::GetChannelOut() const { return this->primitive_->value.AsConv2DGradInput()->channelOut; } -int Conv2DGradInput::GetKernelW() const { return this->primitive_->value.AsConv2DGradInput()->kernelW; } -int Conv2DGradInput::GetKernelH() const { return this->primitive_->value.AsConv2DGradInput()->kernelH; } -int Conv2DGradInput::GetStrideW() const { return this->primitive_->value.AsConv2DGradInput()->strideW; } -int Conv2DGradInput::GetStrideH() const { return this->primitive_->value.AsConv2DGradInput()->strideH; } -int Conv2DGradInput::GetPadMode() const { return this->primitive_->value.AsConv2DGradInput()->padMode; } -int Conv2DGradInput::GetPadUp() const { return this->primitive_->value.AsConv2DGradInput()->padUp; } -int Conv2DGradInput::GetPadDown() const { return this->primitive_->value.AsConv2DGradInput()->padDown; } -int Conv2DGradInput::GetPadLeft() const { return this->primitive_->value.AsConv2DGradInput()->padLeft; } -int Conv2DGradInput::GetPadRight() const { return this->primitive_->value.AsConv2DGradInput()->padRight; } -int Conv2DGradInput::GetDilateW() const { return this->primitive_->value.AsConv2DGradInput()->dilateW; } -int Conv2DGradInput::GetDilateH() const { return this->primitive_->value.AsConv2DGradInput()->dilateH; } -std::vector Conv2DGradInput::GetInputShape() const { - return this->primitive_->value.AsConv2DGradInput()->input_shape; -} -int Conv2DGradInput::GetActivationType() const { return this->primitive_->value.AsConv2DGradInput()->activationType; } - -void Conv2DGradInput::SetFormat(int format) { - this->primitive_->value.AsConv2DGradInput()->format = (schema::Format)format; -} -void Conv2DGradInput::SetGroup(int group) { this->primitive_->value.AsConv2DGradInput()->group = group; } -void Conv2DGradInput::SetChannelIn(int channel_in) { - this->primitive_->value.AsConv2DGradInput()->channelIn = channel_in; -} -void Conv2DGradInput::SetChannelOut(int channel_out) { - this->primitive_->value.AsConv2DGradInput()->channelOut = channel_out; -} -void Conv2DGradInput::SetKernelW(int kernel_w) { this->primitive_->value.AsConv2DGradInput()->kernelW = kernel_w; } -void Conv2DGradInput::SetKernelH(int kernel_h) { this->primitive_->value.AsConv2DGradInput()->kernelH = kernel_h; } -void Conv2DGradInput::SetStrideW(int stride_w) { this->primitive_->value.AsConv2DGradInput()->strideW = stride_w; } -void Conv2DGradInput::SetStrideH(int stride_h) { this->primitive_->value.AsConv2DGradInput()->strideH = stride_h; } -void Conv2DGradInput::SetPadMode(int pad_mode) { - this->primitive_->value.AsConv2DGradInput()->padMode = (schema::PadMode)pad_mode; -} -void Conv2DGradInput::SetPadUp(int pad_up) { this->primitive_->value.AsConv2DGradInput()->padUp = pad_up; } -void Conv2DGradInput::SetPadDown(int pad_down) { this->primitive_->value.AsConv2DGradInput()->padDown = pad_down; } -void Conv2DGradInput::SetPadLeft(int pad_left) { this->primitive_->value.AsConv2DGradInput()->padLeft = pad_left; } -void Conv2DGradInput::SetPadRight(int pad_right) { this->primitive_->value.AsConv2DGradInput()->padRight = pad_right; } -void Conv2DGradInput::SetDilateW(int dilate_w) { this->primitive_->value.AsConv2DGradInput()->dilateW = dilate_w; } -void Conv2DGradInput::SetDilateH(int dilate_h) { this->primitive_->value.AsConv2DGradInput()->dilateH = dilate_h; } -void Conv2DGradInput::SetActivationType(int activation_type) { - this->primitive_->value.AsConv2DGradInput()->activationType = (schema::ActivationType)activation_type; -} - -int Conv2DGradInput::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Conv2DGradInput; - } - if (this->primitive_->value.type != schema::PrimitiveType_Conv2DGradInput) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::Conv2DGradInputT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->group = CastToInt(prim.GetAttr("group")).front(); - if (attr->group > 1) { - this->primitive_->value.type = schema::PrimitiveType_GroupConv2DGradInput; - } - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format_NHWC; - } else { - attr->format = schema::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(2); - attr->dilateW = dilation.at(3); - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = (kernel_size.size() > 1) ? kernel_size.at(1) : kernel_size.at(0); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(0); - attr->strideW = stride.at(1); - - attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - if (inputs.size() >= kAnfPopulaterInputNumThree) { - auto input_shape = inputs[kAnfPopulaterInputNumTwo]; - MS_ASSERT(input_shape != nullptr); - if (input_shape->isa()) { - auto valueNode = input_shape->cast(); - MS_ASSERT(valueNode != nullptr); - auto value = valueNode->value(); - MS_ASSERT(value != nullptr); - if (value->isa()) { - auto valTuplPtr = dyn_cast(value); - MS_ASSERT(valTuplPtr != nullptr); - const int nchw2nhwc[] = {0, 3, 1, 2}; - attr->input_shape.resize(valTuplPtr->size()); - for (size_t i = 0; i < valTuplPtr->size(); i++) { - auto elem = (*valTuplPtr)[i]; - MS_ASSERT(elem != nullptr); - attr->input_shape[nchw2nhwc[i]] = CastToInt(elem).front(); - } - } - } - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Conv2DGradInput::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Conv2DGradInput(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Conv2DGradInput return nullptr"; - return RET_ERROR; - } - std::vector input_shape; - if (attr->input_shape() != nullptr) { - for (int i = 0; i < static_cast(attr->input_shape()->size()); i++) { - input_shape.push_back(attr->input_shape()->data()[i]); - } - } - auto val_offset = schema::CreateConv2DGradInputDirect( - *fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), &input_shape, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Conv2DGradInput, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Conv2DGradInput::GetFormat() const { return this->primitive_->value_as_Conv2DGradInput()->format(); } -int Conv2DGradInput::GetGroup() const { return this->primitive_->value_as_Conv2DGradInput()->group(); } -int Conv2DGradInput::GetChannelIn() const { return this->primitive_->value_as_Conv2DGradInput()->channelIn(); } -int Conv2DGradInput::GetChannelOut() const { return this->primitive_->value_as_Conv2DGradInput()->channelOut(); } -int Conv2DGradInput::GetKernelW() const { return this->primitive_->value_as_Conv2DGradInput()->kernelW(); } -int Conv2DGradInput::GetKernelH() const { return this->primitive_->value_as_Conv2DGradInput()->kernelH(); } -int Conv2DGradInput::GetStrideW() const { return this->primitive_->value_as_Conv2DGradInput()->strideW(); } -int Conv2DGradInput::GetStrideH() const { return this->primitive_->value_as_Conv2DGradInput()->strideH(); } -int Conv2DGradInput::GetPadMode() const { return this->primitive_->value_as_Conv2DGradInput()->padMode(); } -int Conv2DGradInput::GetPadUp() const { return this->primitive_->value_as_Conv2DGradInput()->padUp(); } -int Conv2DGradInput::GetPadDown() const { return this->primitive_->value_as_Conv2DGradInput()->padDown(); } -int Conv2DGradInput::GetPadLeft() const { return this->primitive_->value_as_Conv2DGradInput()->padLeft(); } -int Conv2DGradInput::GetPadRight() const { return this->primitive_->value_as_Conv2DGradInput()->padRight(); } -int Conv2DGradInput::GetDilateW() const { return this->primitive_->value_as_Conv2DGradInput()->dilateW(); } -int Conv2DGradInput::GetDilateH() const { return this->primitive_->value_as_Conv2DGradInput()->dilateH(); } -std::vector Conv2DGradInput::GetInputShape() const { - auto fb_vector = this->primitive_->value_as_Conv2DGradInput()->input_shape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Conv2DGradInput::GetActivationType() const { - return this->primitive_->value_as_Conv2DGradInput()->activationType(); -} - -PrimitiveC *Conv2DGradInputCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry Conv2DGradInputRegistry(schema::PrimitiveType_Conv2DGradInput, Conv2DGradInputCreator); -#endif - -int Conv2DGradInput::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() < 2) { - MS_LOG(ERROR) << "Conv2d Grad Input should be at least two input"; - return RET_ERROR; - } - if (outputs.size() != 1) { - MS_LOG(ERROR) << "Conv2d Grad output should have one output"; - return RET_ERROR; - } - - auto *in0 = inputs.at(0); - MS_ASSERT(in0 != nullptr); - - auto *out = outputs.at(0); - MS_ASSERT(out != nullptr); - out->set_shape(GetInputShape()); - out->set_data_type(in0->data_type()); - out->set_format(in0->format()); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/conv2d_grad_input.h b/mindspore/lite/src/ops/conv2d_grad_input.h deleted file mode 100644 index b12c96a51c..0000000000 --- a/mindspore/lite/src/ops/conv2d_grad_input.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_INPUT_H_ -#define MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_INPUT_H_ - -#include -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Conv2DGradInput : public PrimitiveC { - public: - Conv2DGradInput() = default; - ~Conv2DGradInput() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Conv2DGradInput, PrimitiveC); - explicit Conv2DGradInput(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetGroup(int group); - void SetChannelIn(int channel_in); - void SetChannelOut(int channel_out); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetGroup() const; - int GetChannelIn() const; - int GetChannelOut() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; - std::vector GetInputShape() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_CONV2D_GRAD_INPUT_H_ diff --git a/mindspore/lite/src/ops/cos.cc b/mindspore/lite/src/ops/cos.cc deleted file mode 100644 index 1a02632937..0000000000 --- a/mindspore/lite/src/ops/cos.cc +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/cos.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Cos::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Cos; - } - if (this->primitive_->value.type != schema::PrimitiveType_Cos) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::CosT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int Cos::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateCos(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Cos, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *CosCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry CosRegistry(schema::PrimitiveType_Cos, CosCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/cos.h b/mindspore/lite/src/ops/cos.h deleted file mode 100644 index 70269945d6..0000000000 --- a/mindspore/lite/src/ops/cos.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_COS_H_ -#define MINDSPORE_LITE_SRC_OPS_COS_H_ - -#include -#include -#include -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Cos : public ArithmeticSelf { - public: - Cos() = default; - ~Cos() = default; -#ifdef PRIMITIVE_WRITEABLE - explicit Cos(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_COS_H_ diff --git a/mindspore/lite/src/ops/crop.cc b/mindspore/lite/src/ops/crop.cc deleted file mode 100644 index c568f8832b..0000000000 --- a/mindspore/lite/src/ops/crop.cc +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/crop.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int64_t Crop::GetAxis() const { return this->primitive_->value.AsCrop()->axis; } -std::vector Crop::GetOffsets() const { return this->primitive_->value.AsCrop()->offsets; } - -void Crop::SetAxis(int64_t axis) { this->primitive_->value.AsCrop()->axis = axis; } -void Crop::SetOffsets(const std::vector &offsets) { this->primitive_->value.AsCrop()->offsets = offsets; } - -#else -int Crop::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Crop(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Crop return nullptr"; - return RET_ERROR; - } - std::vector offsets; - if (attr->offsets() != nullptr) { - for (int i = 0; i < static_cast(attr->offsets()->size()); i++) { - offsets.push_back(attr->offsets()->data()[i]); - } - } - auto val_offset = schema::CreateCropDirect(*fbb, attr->axis(), &offsets); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Crop, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int64_t Crop::GetAxis() const { return this->primitive_->value_as_Crop()->axis(); } -std::vector Crop::GetOffsets() const { - auto fb_vector = this->primitive_->value_as_Crop()->offsets(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} - -PrimitiveC *CropCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry CropRegistry(schema::PrimitiveType_Crop, CropCreator); -#endif - -namespace { -constexpr int kCropOutputNum = 1; -constexpr int kCropInputNum = 2; -} // namespace -int Crop::InferShape(std::vector inputs, std::vector outputs) { - if (outputs.size() != kCropOutputNum || inputs.size() != kCropInputNum) { - MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); - return RET_PARAM_INVALID; - } - outputs[0]->set_format(inputs[0]->format()); - outputs[0]->set_data_type(inputs[0]->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - outputs[0]->set_shape(inputs[1]->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/crop.h b/mindspore/lite/src/ops/crop.h deleted file mode 100644 index 002843d677..0000000000 --- a/mindspore/lite/src/ops/crop.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CROP_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CROP_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Crop : public PrimitiveC { - public: - Crop() = default; - ~Crop() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Crop, PrimitiveC); - explicit Crop(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int64_t axis); - void SetOffsets(const std::vector &offsets); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int64_t GetAxis() const; - std::vector GetOffsets() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CROP_H_ diff --git a/mindspore/lite/src/ops/crop_and_resize.cc b/mindspore/lite/src/ops/crop_and_resize.cc deleted file mode 100644 index f9af64baef..0000000000 --- a/mindspore/lite/src/ops/crop_and_resize.cc +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/crop_and_resize.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int CropAndResize::GetMethod() const { return this->primitive_->value.AsCropAndResize()->method; } -float CropAndResize::GetExtrapolationValue() const { - return this->primitive_->value.AsCropAndResize()->extrapolation_value; -} - -void CropAndResize::SetMethod(int method) { - this->primitive_->value.AsCropAndResize()->method = (schema::ResizeMethod)method; -} -void CropAndResize::SetExtrapolationValue(float value) { - this->primitive_->value.AsCropAndResize()->extrapolation_value = value; -} -#else - -int CropAndResize::GetMethod() const { return this->primitive_->value_as_CropAndResize()->method(); } -float CropAndResize::GetExtrapolationValue() const { - return this->primitive_->value_as_CropAndResize()->extrapolation_value(); -} -int CropAndResize::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_CropAndResize(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_CropAndResize return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateCropAndResize(*fbb, attr->method(), attr->extrapolation_value()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_CropAndResize, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *CropAndResizeCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry CropAndResizeRegistry(schema::PrimitiveType_CropAndResize, CropAndResizeCreator); -#endif - -namespace { -constexpr int kInputRank = 4; -} // namespace -int CropAndResize::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() != 4) { - MS_LOG(ERROR) << "Input tensor num should be 4 for crop_an_resize."; - return RET_ERROR; - } - auto input = inputs_.front(); - if (input == nullptr) { - return RET_ERROR; - } - if (!input->shape().empty() && input->shape().size() != kInputRank) { - MS_LOG(ERROR) << "Size of input shape is wrong."; - return RET_ERROR; - } - if (input->format() != schema::Format_NHWC) { - MS_LOG(ERROR) << "Crop_an_resize op only support NHWC format."; - return RET_ERROR; - } - - auto output = outputs_.front(); - if (output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector output_shape; - if (inputs_[1]->data_c() != nullptr) { - auto boxes_tensor = inputs_[1]; - output_shape.push_back(boxes_tensor->shape()[0]); - } else { - output_shape.push_back(input->Batch()); - } - - auto shape_tensor = inputs_[3]; - auto data = reinterpret_cast(shape_tensor->data_c()); - if (data == nullptr) { - MS_LOG(INFO) << "The data of 4th input tensor(shape tensor) for crop_an_resize op is nullptr."; - return RET_INFER_INVALID; - } - output_shape.push_back(data[0]); - output_shape.push_back(data[1]); - output_shape.push_back(input->Channel()); - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/crop_and_resize.h b/mindspore/lite/src/ops/crop_and_resize.h deleted file mode 100644 index 6cbfa98b8b..0000000000 --- a/mindspore/lite/src/ops/crop_and_resize.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class CropAndResize : public PrimitiveC { - public: - CropAndResize() = default; - ~CropAndResize() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(CropAndResize, PrimitiveC); - explicit CropAndResize(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetMethod(int method); - void SetExtrapolationValue(float value); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetMethod() const; - float GetExtrapolationValue() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CROP_AND_RESIZE_H_ diff --git a/mindspore/lite/src/ops/custom_extract_features.cc b/mindspore/lite/src/ops/custom_extract_features.cc deleted file mode 100644 index 5054b25fba..0000000000 --- a/mindspore/lite/src/ops/custom_extract_features.cc +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/custom_extract_features.h" - -#include "src/common/string_util.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int CustomExtractFeatures::UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_OK; } -#else -int CustomExtractFeatures::UnPackToFlatBuilder(const schema::Primitive *primitive, - flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateCustomExtractFeatures(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_CustomExtractFeatures, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *CustomExtractFeaturesCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry CustomExtractFeaturesRegistry(schema::PrimitiveType_CustomExtractFeatures, CustomExtractFeaturesCreator); -#endif - -int CustomExtractFeatures::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.at(0); - auto output0 = outputs_.at(0); - auto output1 = outputs_.at(1); - MS_ASSERT(input != nullptr); - MS_ASSERT(output0 != nullptr); - MS_ASSERT(output1 != nullptr); - - output0->set_data_type(kNumberTypeInt32); - output0->set_format(input->format()); - output1->set_data_type(kNumberTypeFloat32); - output1->set_format(input->format()); - - if (input->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - std::vector shape; - int string_num = lite::GetStringCount(input); - shape.push_back(string_num == 0 ? 1 : string_num); - - output0->set_shape(shape); - output1->set_shape(shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/custom_extract_features.h b/mindspore/lite/src/ops/custom_extract_features.h deleted file mode 100644 index c9718a6b45..0000000000 --- a/mindspore/lite/src/ops/custom_extract_features.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_CUSTOM_EXTRACT_FEATURES_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CUSTOM_EXTRACT_FEATURES_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class CustomExtractFeatures : public PrimitiveC { - public: - CustomExtractFeatures() = default; - ~CustomExtractFeatures() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(CustomExtractFeatures, PrimitiveC); - explicit CustomExtractFeatures(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CUSTOM_EXTRACT_FEATURES_H_ diff --git a/mindspore/lite/src/ops/custom_normalize.cc b/mindspore/lite/src/ops/custom_normalize.cc deleted file mode 100644 index 6ba50c6a59..0000000000 --- a/mindspore/lite/src/ops/custom_normalize.cc +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/custom_normalize.h" - -#include "src/common/string_util.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int CustomNormalize::UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_OK; } -#else -int CustomNormalize::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateCustomNormalize(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_CustomNormalize, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *CustomNormalizeCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry CustomNormalizeRegistry(schema::PrimitiveType_CustomNormalize, CustomNormalizeCreator); -#endif - -int CustomNormalize::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.at(0); - auto output = outputs_.at(0); - MS_ASSERT(input != nullptr); - MS_ASSERT(output != nullptr); - - output->set_data_type(input->data_type()); - output->set_format(input->format()); - - if (input->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - std::vector shape; - int string_num = lite::GetStringCount(input); - shape.push_back(string_num == 0 ? 1 : string_num); - - output->set_shape(shape); - return RET_OK; -} - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/custom_normalize.h b/mindspore/lite/src/ops/custom_normalize.h deleted file mode 100644 index 799df336aa..0000000000 --- a/mindspore/lite/src/ops/custom_normalize.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_CUSTOM_NORMALIZE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CUSTOM_NORMALIZE_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class CustomNormalize : public PrimitiveC { - public: - CustomNormalize() = default; - ~CustomNormalize() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(CustomNormalize, PrimitiveC); - explicit CustomNormalize(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CUSTOM_NORMALIZE_H_ diff --git a/mindspore/lite/src/ops/custom_predict.cc b/mindspore/lite/src/ops/custom_predict.cc deleted file mode 100644 index 0afbbfa77d..0000000000 --- a/mindspore/lite/src/ops/custom_predict.cc +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/custom_predict.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int CustomPredict::GetOutputNum() const { return this->primitive_->value.AsCustomPredict()->outputNum; } -float CustomPredict::GetWeightThreshold() const { return this->primitive_->value.AsCustomPredict()->weightThreshold; } - -void CustomPredict::SetOutputNum(int output_num) { this->primitive_->value.AsCustomPredict()->outputNum = output_num; } -void CustomPredict::SetWeightThreshold(float weight_threshold) { - this->primitive_->value.AsCustomPredict()->weightThreshold = weight_threshold; -} -int CustomPredict::UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_OK; } -#else -int CustomPredict::GetOutputNum() const { return this->primitive_->value_as_CustomPredict()->outputNum(); } -float CustomPredict::GetWeightThreshold() const { - return this->primitive_->value_as_CustomPredict()->weightThreshold(); -} - -int CustomPredict::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_CustomPredict(); - if (attr == nullptr) { - MS_LOG(ERROR) << "CustomPredict attr is nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateCustomPredict(*fbb, attr->outputNum(), attr->weightThreshold()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_CustomPredict, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *CustomPredictCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry CustomPredictRegistry(schema::PrimitiveType_CustomPredict, CustomPredictCreator); -#endif - -int CustomPredict::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.at(0); - auto output0 = outputs_.at(0); - auto output1 = outputs_.at(1); - MS_ASSERT(input != nullptr); - MS_ASSERT(output0 != nullptr); - MS_ASSERT(output1 != nullptr); - - std::vector shape; - shape.push_back(GetOutputNum()); - - output0->set_shape(shape); - output0->set_data_type(kNumberTypeInt32); - output0->set_format(input->format()); - output1->set_shape(shape); - output1->set_data_type(kNumberTypeFloat32); - output1->set_format(input->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/custom_predict.h b/mindspore/lite/src/ops/custom_predict.h deleted file mode 100644 index 404558829d..0000000000 --- a/mindspore/lite/src/ops/custom_predict.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_CUSTOM_PREDICT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_CUSTOM_PREDICT_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class CustomPredict : public PrimitiveC { - public: - CustomPredict() = default; - ~CustomPredict() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(CustomPredict, PrimitiveC); - explicit CustomPredict(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int GetOutputNum() const; - float GetWeightThreshold() const; - void SetOutputNum(int output_num); - void SetWeightThreshold(float weight_threshold); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int GetOutputNum() const; - float GetWeightThreshold() const; - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_CUSTOM_PREDICT_H_ diff --git a/mindspore/lite/src/ops/deconv2d.cc b/mindspore/lite/src/ops/deconv2d.cc deleted file mode 100644 index 0b36395fd6..0000000000 --- a/mindspore/lite/src/ops/deconv2d.cc +++ /dev/null @@ -1,375 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/deconv2d.h" -#include -#include -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int DeConv2D::GetFormat() const { return this->primitive_->value.AsDeConv2D()->format; } -int DeConv2D::GetGroup() const { return this->primitive_->value.AsDeConv2D()->group; } -int DeConv2D::GetChannelIn() const { return this->primitive_->value.AsDeConv2D()->channelIn; } -int DeConv2D::GetChannelOut() const { return this->primitive_->value.AsDeConv2D()->channelOut; } -int DeConv2D::GetKernelW() const { return this->primitive_->value.AsDeConv2D()->kernelW; } -int DeConv2D::GetKernelH() const { return this->primitive_->value.AsDeConv2D()->kernelH; } -int DeConv2D::GetStrideW() const { return this->primitive_->value.AsDeConv2D()->strideW; } -int DeConv2D::GetStrideH() const { return this->primitive_->value.AsDeConv2D()->strideH; } -int DeConv2D::GetPadMode() const { return this->primitive_->value.AsDeConv2D()->padMode; } -int DeConv2D::GetPadUp() const { return this->primitive_->value.AsDeConv2D()->padUp; } -int DeConv2D::GetPadDown() const { return this->primitive_->value.AsDeConv2D()->padDown; } -int DeConv2D::GetPadLeft() const { return this->primitive_->value.AsDeConv2D()->padLeft; } -int DeConv2D::GetPadRight() const { return this->primitive_->value.AsDeConv2D()->padRight; } -int DeConv2D::GetDilateW() const { return this->primitive_->value.AsDeConv2D()->dilateW; } -int DeConv2D::GetDilateH() const { return this->primitive_->value.AsDeConv2D()->dilateH; } -int DeConv2D::GetActivationType() const { return this->primitive_->value.AsDeConv2D()->activationType; } -int DeConv2D::GetOutputPaddingW() const { return this->primitive_->value.AsDeConv2D()->outputPaddingW; } -int DeConv2D::GetOutputPaddingH() const { return this->primitive_->value.AsDeConv2D()->outputPaddingH; } - -void DeConv2D::SetFormat(int format) { this->primitive_->value.AsDeConv2D()->format = (schema::Format)format; } -void DeConv2D::SetGroup(int group) { this->primitive_->value.AsDeConv2D()->group = group; } -void DeConv2D::SetChannelIn(int channel_in) { this->primitive_->value.AsDeConv2D()->channelIn = channel_in; } -void DeConv2D::SetChannelOut(int channel_out) { this->primitive_->value.AsDeConv2D()->channelOut = channel_out; } -void DeConv2D::SetKernelW(int kernel_w) { this->primitive_->value.AsDeConv2D()->kernelW = kernel_w; } -void DeConv2D::SetKernelH(int kernel_h) { this->primitive_->value.AsDeConv2D()->kernelH = kernel_h; } -void DeConv2D::SetStrideW(int stride_w) { this->primitive_->value.AsDeConv2D()->strideW = stride_w; } -void DeConv2D::SetStrideH(int stride_h) { this->primitive_->value.AsDeConv2D()->strideH = stride_h; } -void DeConv2D::SetPadMode(int pad_mode) { this->primitive_->value.AsDeConv2D()->padMode = (schema::PadMode)pad_mode; } -void DeConv2D::SetPadUp(int pad_up) { this->primitive_->value.AsDeConv2D()->padUp = pad_up; } -void DeConv2D::SetPadDown(int pad_down) { this->primitive_->value.AsDeConv2D()->padDown = pad_down; } -void DeConv2D::SetPadLeft(int pad_left) { this->primitive_->value.AsDeConv2D()->padLeft = pad_left; } -void DeConv2D::SetPadRight(int pad_right) { this->primitive_->value.AsDeConv2D()->padRight = pad_right; } -void DeConv2D::SetDilateW(int dilate_w) { this->primitive_->value.AsDeConv2D()->dilateW = dilate_w; } -void DeConv2D::SetDilateH(int dilate_h) { this->primitive_->value.AsDeConv2D()->dilateH = dilate_h; } -void DeConv2D::SetActivationType(int activation_type) { - this->primitive_->value.AsDeConv2D()->activationType = (schema::ActivationType)activation_type; -} -template -void ConvertConvWeight(const ParameterPtr ¶m_node) { - MS_ASSERT(param_node != nullptr); - auto param = param_node->default_param(); - auto weight = std::dynamic_pointer_cast(param); - MS_ASSERT(weight != nullptr); - - std::unique_ptr buf(new (std::nothrow) T[weight->tensor_shape_size()]); - if (buf == nullptr) { - MS_LOG(ERROR) << "new buf failed"; - return; - } - - size_t filter_k = weight->tensor_shape().at(0); - size_t filter_c = weight->tensor_shape().at(1); - size_t filter_h = weight->tensor_shape().at(2); - size_t filter_w = weight->tensor_shape().at(3); - T *p1Buff = nullptr; - T *p2Buff = nullptr; - for (size_t k = 0; k < filter_k; ++k) { - for (size_t c = 0; c < filter_c; ++c) { - for (size_t h = 0; h < filter_h; ++h) { - for (size_t w = 0; w < filter_w; ++w) { - p1Buff = reinterpret_cast(weight->tensor_addr()) + - ((k * filter_c * filter_h * filter_w) + (c * filter_h * filter_w) + (h * filter_w) + (w)); - p2Buff = - buf.get() + ((c * filter_k * filter_h * filter_w) + (k * filter_h * filter_w) + (h * filter_w) + (w)); - *p2Buff = *p1Buff; - } - } - } - } - - auto ret = ::memcpy_s(weight->tensor_addr(), weight->tensor_shape_size() * sizeof(T), buf.get(), - weight->tensor_shape_size() * sizeof(T)); - if (ret != EOK) { - MS_LOG(ERROR) << "memcpy_s failed: " << ret; - return; - } - - auto abstract_base = param_node->abstract(); - MS_ASSERT(abstract_base != nullptr); - if (utils::isa(abstract_base)) { - auto abstract_tensor = utils::cast(abstract_base); - utils::cast(abstract_tensor->BuildShape())->shape()[0] = filter_c; - utils::cast(abstract_tensor->BuildShape())->shape()[1] = filter_k; - utils::cast(abstract_tensor->BuildShape())->shape()[2] = filter_h; - utils::cast(abstract_tensor->BuildShape())->shape()[3] = filter_w; - } - return; -} - -void DeConv2D::PopulaterConv2DMultiGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group, - const std::vector &inputs) { - auto attr = std::make_unique(); - if (attr.get() == nullptr) { - MS_LOG(ERROR) << "Memory allocation failed"; - return; - } - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(0); - attr->dilateW = dilation.at(1); - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = kernel_size.at(1); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(0); - attr->strideW = stride.at(1); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - int channel_mutiplier = 1; - if (prim.GetAttr("channel_mutiplier") != nullptr) { - channel_mutiplier = CastToInt(prim.GetAttr("channel_multiplier")).front(); - } - attr->channelMultiplier = channel_mutiplier; - - MS_ASSERT(inputs.size() == kAnfPopulaterInputNumTwo); - auto input_node = inputs[kAnfPopulaterInputNumOne]; - MS_ASSERT(input_node != nullptr); - if (input_node->isa()) { - auto param_node = input_node->cast(); - ConvertConvWeight(param_node); - } - - primitive->value.type = schema::PrimitiveType_DeDepthwiseConv2D; - primitive->value.value = attr.release(); -} - -void DeConv2D::PopulaterDeConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group) { - auto attr = std::make_unique(); - if (attr.get() == nullptr) { - MS_LOG(ERROR) << "Memory allocation failed"; - return; - } - attr->group = group; - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format_NHWC; - } else { - attr->format = schema::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(0); - attr->dilateW = dilation.at(1); - - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = kernel_size.at(1); - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(0); - attr->strideW = stride.at(1); - - attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid" || pad_mode == "VALID") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same" || pad_mode == "SAME") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - - primitive->value.type = schema::PrimitiveType_DeConv2D; - primitive->value.value = attr.release(); -} - -int DeConv2D::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_DeConv2D; - } - if (this->primitive_->value.type != schema::PrimitiveType_DeConv2D) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - int group = CastToInt(prim.GetAttr("group")).front(); - if (group == 1) { - PopulaterDeConv2DSingleGroup(prim, this->primitive_, group); - } else if (group > 1) { - PopulaterConv2DMultiGroup(prim, this->primitive_, group, inputs); - } - PopulaterQuantParam(prim, inputs); - return RET_OK; -} -#else -int DeConv2D::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_DeConv2D(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DeConv2D return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreateDeConv2D(*fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), attr->kernelW(), - attr->kernelH(), attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), - attr->padDown(), attr->padLeft(), attr->padRight(), attr->dilateW(), attr->dilateH(), - attr->hasBias(), attr->activationType(), attr->outputPaddingW(), attr->outputPaddingH()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DeConv2D, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int DeConv2D::GetFormat() const { return this->primitive_->value_as_DeConv2D()->format(); } -int DeConv2D::GetGroup() const { return this->primitive_->value_as_DeConv2D()->group(); } -int DeConv2D::GetChannelIn() const { return this->primitive_->value_as_DeConv2D()->channelIn(); } -int DeConv2D::GetChannelOut() const { return this->primitive_->value_as_DeConv2D()->channelOut(); } -int DeConv2D::GetKernelW() const { return this->primitive_->value_as_DeConv2D()->kernelW(); } -int DeConv2D::GetKernelH() const { return this->primitive_->value_as_DeConv2D()->kernelH(); } -int DeConv2D::GetStrideW() const { return this->primitive_->value_as_DeConv2D()->strideW(); } -int DeConv2D::GetStrideH() const { return this->primitive_->value_as_DeConv2D()->strideH(); } -int DeConv2D::GetPadMode() const { return this->primitive_->value_as_DeConv2D()->padMode(); } -int DeConv2D::GetPadUp() const { return this->primitive_->value_as_DeConv2D()->padUp(); } -int DeConv2D::GetPadDown() const { return this->primitive_->value_as_DeConv2D()->padDown(); } -int DeConv2D::GetPadLeft() const { return this->primitive_->value_as_DeConv2D()->padLeft(); } -int DeConv2D::GetPadRight() const { return this->primitive_->value_as_DeConv2D()->padRight(); } -int DeConv2D::GetDilateW() const { return this->primitive_->value_as_DeConv2D()->dilateW(); } -int DeConv2D::GetDilateH() const { return this->primitive_->value_as_DeConv2D()->dilateH(); } -int DeConv2D::GetActivationType() const { return this->primitive_->value_as_DeConv2D()->activationType(); } -int DeConv2D::GetOutputPaddingW() const { return this->primitive_->value_as_DeConv2D()->outputPaddingW(); } -int DeConv2D::GetOutputPaddingH() const { return this->primitive_->value_as_DeConv2D()->outputPaddingH(); } - -PrimitiveC *DeConv2DCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DeConv2DRegistry(schema::PrimitiveType_DeConv2D, DeConv2DCreator); -#endif - -int DeConv2D::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto weight = inputs_.at(1); - MS_ASSERT(weight != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - int32_t input_h = input->Height(); - int32_t input_w = input->Width(); - - int32_t output_n = input->Batch(); - int32_t output_h = 0; - int32_t output_w = 0; - int32_t output_c = weight->Channel(); - - int kernel_w = GetKernelW(); - int kernel_h = GetKernelH(); - int stride_w = GetStrideW(); - int stride_h = GetStrideH(); - int dilate_w = GetDilateW(); - int dilate_h = GetDilateH(); - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - auto pad_mode = (schema::PadMode)GetPadMode(); - if (pad_mode == schema::PadMode_CAFFE || pad_mode == schema::PadMode_NOTSET) { - output_h = (input_h - 1) * stride_h + ((kernel_h - 1) * dilate_h + 1) - pad_u_ - pad_d_; - output_w = (input_w - 1) * stride_w + ((kernel_w - 1) * dilate_w + 1) - pad_l_ - pad_r_; - } else if (pad_mode == schema::PadMode_SAME_UPPER) { - output_h = input_h * stride_h; - output_w = input_w * stride_w; - } else if (pad_mode == schema::PadMode_VALID) { - output_h = (input_h - 1) * stride_h + kernel_h; - output_w = (input_w - 1) * stride_w + kernel_w; - } else { - MS_LOG(ERROR) << "unsupported pad mode for deconv"; - return RET_ERROR; - } - output_h += GetOutputPaddingH(); - output_w += GetOutputPaddingW(); - std::vector out_shape = {output_n, output_h, output_w, output_c}; - output->set_shape(out_shape); - - if (pad_mode == schema::PadMode_SAME_UPPER) { - pad_u_ = ((input_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - output_h) / 2; - pad_l_ = ((input_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - output_w) / 2; - } else if (pad_mode == schema::PadMode_VALID) { - pad_u_ = 0; - pad_l_ = 0; - } else if (pad_mode == schema::PadMode_CAFFE || pad_mode == schema::PadMode_NOTSET) { - } else { - MS_LOG(ERROR) << "unsupported pad mode for deconv"; - return RET_ERROR; - } - - return 0; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/deconv2d.h b/mindspore/lite/src/ops/deconv2d.h deleted file mode 100644 index 5ffe92b83a..0000000000 --- a/mindspore/lite/src/ops/deconv2d.h +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ -#define LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DeConv2D : public PrimitiveC { - public: - DeConv2D() = default; - ~DeConv2D() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DeConv2D, PrimitiveC); - explicit DeConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetGroup(int group); - void SetChannelIn(int channel_in); - void SetChannelOut(int channel_out); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); - void PopulaterDeConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group); - void PopulaterConv2DMultiGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group, - const std::vector &inputs); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetGroup() const; - int GetChannelIn() const; - int GetChannelOut() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; - int GetOutputPaddingW() const; - int GetOutputPaddingH() const; - int PadUp() const { return this->pad_u_; } - int PadDown() const { return this->pad_d_; } - int PadLeft() const { return this->pad_l_; } - int PadRight() const { return this->pad_r_; } - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ diff --git a/mindspore/lite/src/ops/dedepthwise_conv2d.cc b/mindspore/lite/src/ops/dedepthwise_conv2d.cc deleted file mode 100644 index 5badb82105..0000000000 --- a/mindspore/lite/src/ops/dedepthwise_conv2d.cc +++ /dev/null @@ -1,171 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/dedepthwise_conv2d.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int DeDepthwiseConv2D::GetFormat() const { return this->primitive_->value.AsDeDepthwiseConv2D()->format; } -int DeDepthwiseConv2D::GetChannelIn() const { return this->primitive_->value.AsDeDepthwiseConv2D()->channelIn; } -int DeDepthwiseConv2D::GetChannelMultiplier() const { - return this->primitive_->value.AsDeDepthwiseConv2D()->channelMultiplier; -} -int DeDepthwiseConv2D::GetKernelW() const { return this->primitive_->value.AsDeDepthwiseConv2D()->kernelW; } -int DeDepthwiseConv2D::GetKernelH() const { return this->primitive_->value.AsDeDepthwiseConv2D()->kernelH; } -int DeDepthwiseConv2D::GetStrideW() const { return this->primitive_->value.AsDeDepthwiseConv2D()->strideW; } -int DeDepthwiseConv2D::GetStrideH() const { return this->primitive_->value.AsDeDepthwiseConv2D()->strideH; } -int DeDepthwiseConv2D::GetPadMode() const { return this->primitive_->value.AsDeDepthwiseConv2D()->padMode; } -int DeDepthwiseConv2D::GetPadUp() const { return this->primitive_->value.AsDeDepthwiseConv2D()->padUp; } -int DeDepthwiseConv2D::GetPadDown() const { return this->primitive_->value.AsDeDepthwiseConv2D()->padDown; } -int DeDepthwiseConv2D::GetPadLeft() const { return this->primitive_->value.AsDeDepthwiseConv2D()->padLeft; } -int DeDepthwiseConv2D::GetPadRight() const { return this->primitive_->value.AsDeDepthwiseConv2D()->padRight; } -int DeDepthwiseConv2D::GetDilateW() const { return this->primitive_->value.AsDeDepthwiseConv2D()->dilateW; } -int DeDepthwiseConv2D::GetDilateH() const { return this->primitive_->value.AsDeDepthwiseConv2D()->dilateH; } -int DeDepthwiseConv2D::GetActivationType() const { - return this->primitive_->value.AsDeDepthwiseConv2D()->activationType; -} - -void DeDepthwiseConv2D::SetFormat(int format) { - this->primitive_->value.AsDeDepthwiseConv2D()->format = static_cast(format); -} -void DeDepthwiseConv2D::SetChannelIn(int channel_in) { - this->primitive_->value.AsDeDepthwiseConv2D()->channelIn = channel_in; -} -void DeDepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) { - this->primitive_->value.AsDeDepthwiseConv2D()->channelMultiplier = channel_multiplier; -} -void DeDepthwiseConv2D::SetKernelW(int kernel_w) { this->primitive_->value.AsDeDepthwiseConv2D()->kernelW = kernel_w; } -void DeDepthwiseConv2D::SetKernelH(int kernel_h) { this->primitive_->value.AsDeDepthwiseConv2D()->kernelH = kernel_h; } -void DeDepthwiseConv2D::SetStrideW(int stride_w) { this->primitive_->value.AsDeDepthwiseConv2D()->strideW = stride_w; } -void DeDepthwiseConv2D::SetStrideH(int stride_h) { this->primitive_->value.AsDeDepthwiseConv2D()->strideH = stride_h; } -void DeDepthwiseConv2D::SetPadMode(int pad_mode) { - this->primitive_->value.AsDeDepthwiseConv2D()->padMode = static_cast(pad_mode); -} -void DeDepthwiseConv2D::SetPadUp(int pad_up) { this->primitive_->value.AsDeDepthwiseConv2D()->padUp = pad_up; } -void DeDepthwiseConv2D::SetPadDown(int pad_down) { this->primitive_->value.AsDeDepthwiseConv2D()->padDown = pad_down; } -void DeDepthwiseConv2D::SetPadLeft(int pad_left) { this->primitive_->value.AsDeDepthwiseConv2D()->padLeft = pad_left; } -void DeDepthwiseConv2D::SetPadRight(int pad_right) { - this->primitive_->value.AsDeDepthwiseConv2D()->padRight = pad_right; -} -void DeDepthwiseConv2D::SetDilateW(int dilate_w) { this->primitive_->value.AsDeDepthwiseConv2D()->dilateW = dilate_w; } -void DeDepthwiseConv2D::SetDilateH(int dilate_h) { this->primitive_->value.AsDeDepthwiseConv2D()->dilateH = dilate_h; } -void DeDepthwiseConv2D::SetActivationType(int activation_type) { - this->primitive_->value.AsDeDepthwiseConv2D()->activationType = static_cast(activation_type); -} - -#else -int DeDepthwiseConv2D::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto attr = primitive->value_as_DeDepthwiseConv2D(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DeDepthwiseConv2D return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateDeDepthwiseConv2D( - *fbb, attr->format(), attr->channelIn(), attr->channelMultiplier(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DeDepthwiseConv2D, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int DeDepthwiseConv2D::GetFormat() const { return this->primitive_->value_as_DeDepthwiseConv2D()->format(); } -int DeDepthwiseConv2D::GetChannelIn() const { return this->primitive_->value_as_DeDepthwiseConv2D()->channelIn(); } -int DeDepthwiseConv2D::GetChannelMultiplier() const { - return this->primitive_->value_as_DeDepthwiseConv2D()->channelMultiplier(); -} -int DeDepthwiseConv2D::GetKernelW() const { return this->primitive_->value_as_DeDepthwiseConv2D()->kernelW(); } -int DeDepthwiseConv2D::GetKernelH() const { return this->primitive_->value_as_DeDepthwiseConv2D()->kernelH(); } -int DeDepthwiseConv2D::GetStrideW() const { return this->primitive_->value_as_DeDepthwiseConv2D()->strideW(); } -int DeDepthwiseConv2D::GetStrideH() const { return this->primitive_->value_as_DeDepthwiseConv2D()->strideH(); } -int DeDepthwiseConv2D::GetPadMode() const { return this->primitive_->value_as_DeDepthwiseConv2D()->padMode(); } -int DeDepthwiseConv2D::GetPadUp() const { return this->primitive_->value_as_DeDepthwiseConv2D()->padUp(); } -int DeDepthwiseConv2D::GetPadDown() const { return this->primitive_->value_as_DeDepthwiseConv2D()->padDown(); } -int DeDepthwiseConv2D::GetPadLeft() const { return this->primitive_->value_as_DeDepthwiseConv2D()->padLeft(); } -int DeDepthwiseConv2D::GetPadRight() const { return this->primitive_->value_as_DeDepthwiseConv2D()->padRight(); } -int DeDepthwiseConv2D::GetDilateW() const { return this->primitive_->value_as_DeDepthwiseConv2D()->dilateW(); } -int DeDepthwiseConv2D::GetDilateH() const { return this->primitive_->value_as_DeDepthwiseConv2D()->dilateH(); } -int DeDepthwiseConv2D::GetActivationType() const { - return this->primitive_->value_as_DeDepthwiseConv2D()->activationType(); -} - -PrimitiveC *DeDepthwiseConv2DCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DeDepthwiseConv2DRegistry(schema::PrimitiveType_DeDepthwiseConv2D, DeDepthwiseConv2DCreator); -#endif - -int DeDepthwiseConv2D::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kDoubleNum && inputs_.size() != kTripleNum) { - MS_LOG(ERROR) << "inputs number is invalid"; - return 1; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "output number is invalid"; - return 1; - } - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto weight = inputs_.at(1); - MS_ASSERT(weight != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto in_shape = input->shape(); - int input_h = in_shape.at(1); - int input_w = in_shape.at(2); - int input_channel = in_shape.at(3); - int output_w = 0, output_h = 0; - - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - output_h = GetStrideH() * (input_h - 1) + GetKernelH() - pad_u_ - pad_d_; - output_w = GetStrideW() * (input_w - 1) + GetKernelW() - pad_l_ - pad_r_; - if ((output_h + GetPadUp() + GetPadDown() - GetKernelH()) % GetStrideH() != 0) { - output_h += (output_h + GetPadLeft() + GetPadRight() - GetKernelH()) % GetStrideH(); - } - if ((output_w + GetPadLeft() + GetPadRight() - GetKernelW()) % GetStrideW() != 0) { - output_w += (output_w + GetPadLeft() + GetPadRight() - GetKernelW()) % GetStrideW(); - } - std::vector out_shape{input->shape()}; - out_shape.at(1) = output_h; - out_shape.at(2) = output_w; - if (GetChannelMultiplier() * input_channel != weight->shape()[0]) { - MS_LOG(ERROR) << "Conv dedepthwise only support group equals output channel."; - return RET_ERROR; - } - out_shape.at(3) = weight->shape()[0] * weight->shape()[3]; // in_channel * out_channel - - output->set_shape(out_shape); - return 0; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/dedepthwise_conv2d.h b/mindspore/lite/src/ops/dedepthwise_conv2d.h deleted file mode 100644 index 4848661564..0000000000 --- a/mindspore/lite/src/ops/dedepthwise_conv2d.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_DEDEPTHWISE_CONV2D_H_ -#define MINDSPORE_LITE_SRC_OPS_DEDEPTHWISE_CONV2D_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DeDepthwiseConv2D : public PrimitiveC { - public: - DeDepthwiseConv2D() = default; - ~DeDepthwiseConv2D() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DeDepthwiseConv2D, PrimitiveC); - explicit DeDepthwiseConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetChannelIn(int channel_in); - void SetChannelMultiplier(int channel_multiplier); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetChannelIn() const; - int GetChannelMultiplier() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; - - int PadUp() const { return this->pad_u_; } - int PadDown() const { return this->pad_d_; } - int PadLeft() const { return this->pad_l_; } - int PadRight() const { return this->pad_r_; } - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_DEDEPTHWISE_CONV2D_H_ diff --git a/mindspore/lite/src/ops/depend.cc b/mindspore/lite/src/ops/depend.cc deleted file mode 100644 index f1a4139e4f..0000000000 --- a/mindspore/lite/src/ops/depend.cc +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/depend.h" -#include -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Depend::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Depend; - } - if (this->primitive_->value.type != schema::PrimitiveType_Depend) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow)(schema::DependT); - if (attr == nullptr) { - MS_LOG(ERROR) << "attr is nullptr"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#else -int Depend::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateDepend(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Depend, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *DependCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry DependRegistry(schema::PrimitiveType_Depend, DependCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/depend.h b/mindspore/lite/src/ops/depend.h deleted file mode 100644 index cc7f797308..0000000000 --- a/mindspore/lite/src/ops/depend.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_DEPEND_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_DEPEND_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Depend : public PrimitiveC { - public: - Depend() = default; - ~Depend() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Depend, PrimitiveC); - explicit Depend(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_SRC_OPS_DEPEND_H_ diff --git a/mindspore/lite/src/ops/depth_to_space.cc b/mindspore/lite/src/ops/depth_to_space.cc deleted file mode 100644 index 1109e678a5..0000000000 --- a/mindspore/lite/src/ops/depth_to_space.cc +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/depth_to_space.h" -#include "src/common/common.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int DepthToSpace::GetBlockSize() const { return this->primitive_->value.AsDepthToSpace()->blockSize; } -int DepthToSpace::GetFormat() const { return this->primitive_->value.AsDepthToSpace()->format; } - -void DepthToSpace::SetBlockSize(int block_size) { this->primitive_->value.AsDepthToSpace()->blockSize = block_size; } -void DepthToSpace::SetFormat(int format) { this->primitive_->value.AsDepthToSpace()->format = (schema::Format)format; } - -#else -int DepthToSpace::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_DepthToSpace(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DepthToSpace return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDepthToSpace(*fbb, attr->blockSize(), attr->format()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DepthToSpace, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int DepthToSpace::GetBlockSize() const { return this->primitive_->value_as_DepthToSpace()->blockSize(); } -int DepthToSpace::GetFormat() const { return this->primitive_->value_as_DepthToSpace()->format(); } - -PrimitiveC *DepthToSpaceCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DepthToSpaceRegistry(schema::PrimitiveType_DepthToSpace, DepthToSpaceCreator); - -#endif - -namespace { -constexpr int kDepthToSpaceOutputNum = 1; -constexpr int kDepthToSpaceInputNum = 1; -} // namespace - -int DepthToSpace::InferShape(std::vector inputs, std::vector outputs) { - MS_ASSERT(this->primitive_ != nullptr); - if (outputs.size() != kDepthToSpaceOutputNum || inputs.size() != kDepthToSpaceInputNum) { - MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); - return RET_PARAM_INVALID; - } - - auto input = inputs.at(0); - if (input->format() != schema::Format::Format_NHWC) { - MS_LOG(ERROR) << "depth_to_space only support NHWC now!"; - return RET_FORMAT_ERR; - } - outputs[0]->set_data_type(input->data_type()); - outputs[0]->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - if (input_shape.size() != kQuadrupleNum) { - MS_LOG(ERROR) << "input shape dimension size should == " << kQuadrupleNum; - return RET_PARAM_INVALID; - } - - int32_t block_size = GetBlockSize(); - if (input_shape[NHWC_C] % (block_size * block_size) != 0 || input_shape[NHWC_C] == 0) { - MS_LOG(ERROR) << "input dimension c size " << input_shape[NHWC_C] << " should be multiple of block_size(" - << block_size << ") * block_size)!"; - return RET_PARAM_INVALID; - } - std::vector output_shape(input_shape.size()); - output_shape[NHWC_N] = input_shape[NHWC_N]; - output_shape[NHWC_H] = input_shape[NHWC_H] * block_size; - output_shape[NHWC_W] = input_shape[NHWC_W] * block_size; - output_shape[NHWC_C] = input_shape[NHWC_C] / (block_size * block_size); - outputs[0]->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/depth_to_space.h b/mindspore/lite/src/ops/depth_to_space.h deleted file mode 100644 index c9066fea37..0000000000 --- a/mindspore/lite/src/ops/depth_to_space.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DepthToSpace : public PrimitiveC { - public: - DepthToSpace() = default; - ~DepthToSpace() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DepthToSpace, PrimitiveC); - explicit DepthToSpace(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetBlockSize(int block_size); - void SetFormat(int format); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetBlockSize() const; - int GetFormat() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ diff --git a/mindspore/lite/src/ops/depthwise_conv2d.cc b/mindspore/lite/src/ops/depthwise_conv2d.cc deleted file mode 100644 index 587a58242b..0000000000 --- a/mindspore/lite/src/ops/depthwise_conv2d.cc +++ /dev/null @@ -1,262 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/depthwise_conv2d.h" - -#include -#include -#ifdef PRIMITIVE_WRITEABLE -#include "src/param_value_lite.h" -#endif -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int DepthwiseConv2D::GetFormat() const { return this->primitive_->value.AsDepthwiseConv2D()->format; } -int DepthwiseConv2D::GetChannelIn() const { return this->primitive_->value.AsDepthwiseConv2D()->channelIn; } -int DepthwiseConv2D::GetChannelMultiplier() const { - return this->primitive_->value.AsDepthwiseConv2D()->channelMultiplier; -} -int DepthwiseConv2D::GetKernelW() const { return this->primitive_->value.AsDepthwiseConv2D()->kernelW; } -int DepthwiseConv2D::GetKernelH() const { return this->primitive_->value.AsDepthwiseConv2D()->kernelH; } -int DepthwiseConv2D::GetStrideW() const { return this->primitive_->value.AsDepthwiseConv2D()->strideW; } -int DepthwiseConv2D::GetStrideH() const { return this->primitive_->value.AsDepthwiseConv2D()->strideH; } -int DepthwiseConv2D::GetPadMode() const { return this->primitive_->value.AsDepthwiseConv2D()->padMode; } -int DepthwiseConv2D::GetPadUp() const { return this->primitive_->value.AsDepthwiseConv2D()->padUp; } -int DepthwiseConv2D::GetPadDown() const { return this->primitive_->value.AsDepthwiseConv2D()->padDown; } -int DepthwiseConv2D::GetPadLeft() const { return this->primitive_->value.AsDepthwiseConv2D()->padLeft; } -int DepthwiseConv2D::GetPadRight() const { return this->primitive_->value.AsDepthwiseConv2D()->padRight; } -int DepthwiseConv2D::GetDilateW() const { return this->primitive_->value.AsDepthwiseConv2D()->dilateW; } -int DepthwiseConv2D::GetDilateH() const { return this->primitive_->value.AsDepthwiseConv2D()->dilateH; } -int DepthwiseConv2D::GetActivationType() const { return this->primitive_->value.AsDepthwiseConv2D()->activationType; } - -void DepthwiseConv2D::SetFormat(int format) { - this->primitive_->value.AsDepthwiseConv2D()->format = static_cast(format); -} -void DepthwiseConv2D::SetChannelIn(int channel_in) { - this->primitive_->value.AsDepthwiseConv2D()->channelIn = channel_in; -} -void DepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) { - this->primitive_->value.AsDepthwiseConv2D()->channelMultiplier = channel_multiplier; -} -void DepthwiseConv2D::SetKernelW(int kernel_w) { this->primitive_->value.AsDepthwiseConv2D()->kernelW = kernel_w; } -void DepthwiseConv2D::SetKernelH(int kernel_h) { this->primitive_->value.AsDepthwiseConv2D()->kernelH = kernel_h; } -void DepthwiseConv2D::SetStrideW(int stride_w) { this->primitive_->value.AsDepthwiseConv2D()->strideW = stride_w; } -void DepthwiseConv2D::SetStrideH(int stride_h) { this->primitive_->value.AsDepthwiseConv2D()->strideH = stride_h; } -void DepthwiseConv2D::SetPadMode(int pad_mode) { - this->primitive_->value.AsDepthwiseConv2D()->padMode = static_cast(pad_mode); -} -void DepthwiseConv2D::SetPadUp(int pad_up) { this->primitive_->value.AsDepthwiseConv2D()->padUp = pad_up; } -void DepthwiseConv2D::SetPadDown(int pad_down) { this->primitive_->value.AsDepthwiseConv2D()->padDown = pad_down; } -void DepthwiseConv2D::SetPadLeft(int pad_left) { this->primitive_->value.AsDepthwiseConv2D()->padLeft = pad_left; } -void DepthwiseConv2D::SetPadRight(int pad_right) { this->primitive_->value.AsDepthwiseConv2D()->padRight = pad_right; } -void DepthwiseConv2D::SetDilateW(int dilate_w) { this->primitive_->value.AsDepthwiseConv2D()->dilateW = dilate_w; } -void DepthwiseConv2D::SetDilateH(int dilate_h) { this->primitive_->value.AsDepthwiseConv2D()->dilateH = dilate_h; } -void DepthwiseConv2D::SetActivationType(int activation_type) { - this->primitive_->value.AsDepthwiseConv2D()->activationType = static_cast(activation_type); -} - -int DepthwiseConv2D::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - this->primitive_ = new (schema::PrimitiveT); - auto attr = std::make_unique(); - - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - auto pad_list = CastToInt(prim.GetAttr("pads")); - attr->padUp = pad_list.at(0); - attr->padDown = pad_list.at(1); - attr->padLeft = pad_list.at(2); - attr->padRight = pad_list.at(3); - - auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation.at(0); - attr->dilateW = dilation.at(1); - - if (utils::isa(prim.GetAttr("kernel_size"))) { - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size.at(0); - attr->kernelW = kernel_size.at(1); - } else { - auto kernel_size = CastToInt(prim.GetAttr("kernel_size")).front(); - attr->kernelH = kernel_size; - attr->kernelW = kernel_size; - } - - auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride.at(2); - attr->strideW = stride.at(3); - - auto pad_mode = GetValue(prim.GetAttr("pad_mode")); - if (pad_mode == "valid") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "same") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - if (prim.GetAttr("activation_name") != nullptr) { - std::string activate_name = GetValue(prim.GetAttr("activation_name")); - attr->activationType = kActivationTypeMap[activate_name]; - } else { - attr->activationType = schema::ActivationType_NO_ACTIVATION; - } - auto channel_multiplier = CastToInt(prim.GetAttr("channel_multiplier")).front(); - attr->channelMultiplier = channel_multiplier; - - MS_ASSERT(inputs.size() == kAnfPopulaterInputNumTwo); - auto inputNode = inputs.at(kAnfPopulaterInputNumOne); - MS_ASSERT(inputNode != nullptr); - if (inputNode->isa()) { - auto paramNode = inputNode->cast(); - auto abstractBase = paramNode->abstract(); - MS_ASSERT(abstractBase != nullptr); - if (utils::isa(abstractBase)) { - auto abstractTensor = utils::cast(abstractBase); - MS_ASSERT(abstractTensor != nullptr); - if (utils::isa(abstractTensor->BuildShape())) { - auto dims = utils::cast(abstractTensor->BuildShape())->shape(); - attr->channelIn = dims.at(kAnfPopulaterInputNumOne); - } - } - } - - this->primitive_->value.type = schema::PrimitiveType_DepthwiseConv2D; - this->primitive_->value.value = attr.release(); - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int DepthwiseConv2D::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_DepthwiseConv2D(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DepthwiseConv2D return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDepthwiseConv2D( - *fbb, attr->format(), attr->channelIn(), attr->channelMultiplier(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DepthwiseConv2D, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int DepthwiseConv2D::GetFormat() const { return this->primitive_->value_as_DepthwiseConv2D()->format(); } -int DepthwiseConv2D::GetChannelIn() const { return this->primitive_->value_as_DepthwiseConv2D()->channelIn(); } -int DepthwiseConv2D::GetChannelMultiplier() const { - return this->primitive_->value_as_DepthwiseConv2D()->channelMultiplier(); -} -int DepthwiseConv2D::GetKernelW() const { return this->primitive_->value_as_DepthwiseConv2D()->kernelW(); } -int DepthwiseConv2D::GetKernelH() const { return this->primitive_->value_as_DepthwiseConv2D()->kernelH(); } -int DepthwiseConv2D::GetStrideW() const { return this->primitive_->value_as_DepthwiseConv2D()->strideW(); } -int DepthwiseConv2D::GetStrideH() const { return this->primitive_->value_as_DepthwiseConv2D()->strideH(); } -int DepthwiseConv2D::GetPadMode() const { return this->primitive_->value_as_DepthwiseConv2D()->padMode(); } -int DepthwiseConv2D::GetPadUp() const { return this->primitive_->value_as_DepthwiseConv2D()->padUp(); } -int DepthwiseConv2D::GetPadDown() const { return this->primitive_->value_as_DepthwiseConv2D()->padDown(); } -int DepthwiseConv2D::GetPadLeft() const { return this->primitive_->value_as_DepthwiseConv2D()->padLeft(); } -int DepthwiseConv2D::GetPadRight() const { return this->primitive_->value_as_DepthwiseConv2D()->padRight(); } -int DepthwiseConv2D::GetDilateW() const { return this->primitive_->value_as_DepthwiseConv2D()->dilateW(); } -int DepthwiseConv2D::GetDilateH() const { return this->primitive_->value_as_DepthwiseConv2D()->dilateH(); } -int DepthwiseConv2D::GetActivationType() const { - return this->primitive_->value_as_DepthwiseConv2D()->activationType(); -} - -PrimitiveC *DepthWiseConv2DCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DepthWiseConv2DRegistry(schema::PrimitiveType_DepthwiseConv2D, DepthWiseConv2DCreator); - -#endif - -int DepthwiseConv2D::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kDoubleNum && inputs_.size() != kTripleNum) { - MS_LOG(ERROR) << "inputs number is invalid"; - return 1; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "output number is invalid"; - return 1; - } - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto weight = inputs_.at(1); - MS_ASSERT(weight != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto in_shape = input->shape(); - int input_h = in_shape.at(1); - int input_w = in_shape.at(2); - int input_channel = in_shape.at(3); - int output_w = 0, output_h = 0; - input_channel_ = input_channel; - - if (GetPadMode() == schema::PadMode_SAME_UPPER) { - output_h = std::ceil(static_cast(input_h) / static_cast(GetStrideH())); - output_w = std::ceil(static_cast(input_w) / static_cast(GetStrideW())); - auto pad_h_all = ((output_h - 1) * GetStrideH() + (GetKernelH() - 1) * GetDilateH() + 1 - input_h); - auto pad_w_all = ((output_w - 1) * GetStrideW() + (GetKernelW() - 1) * GetDilateW() + 1 - input_w); - if (pad_h_all > 0) { - pad_u_ = pad_h_all / 2; - pad_d_ = pad_h_all - pad_u_; - } - if (pad_w_all > 0) { - pad_l_ = pad_w_all / 2; - pad_r_ = pad_w_all - pad_l_; - } - } else { - output_h = std::ceil((static_cast(input_h) + pad_u_ + pad_d_ - - (static_cast(GetKernelH()) - 1) * static_cast(GetDilateH())) / - static_cast(GetStrideH())); - output_w = std::ceil((static_cast(input_w) + pad_l_ + pad_r_ - - (static_cast(GetKernelW()) - 1) * static_cast(GetDilateW())) / - static_cast(GetStrideW())); - } - std::vector out_shape{input->shape()}; - out_shape.at(1) = output_h; - out_shape.at(2) = output_w; - if (GetChannelMultiplier() * input_channel != weight->shape().at(0)) { - MS_LOG(ERROR) << "Conv depthwise only support group equals output channel."; - return 1; - } - out_shape.at(3) = weight->shape().at(0) * weight->shape().at(3); // in_channel * out_channel - - output->set_shape(out_shape); - return 0; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/depthwise_conv2d.h b/mindspore/lite/src/ops/depthwise_conv2d.h deleted file mode 100644 index 7243914a2a..0000000000 --- a/mindspore/lite/src/ops/depthwise_conv2d.h +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_DEPTHWISE_CONV2D_H_ -#define MINDSPORE_LITE_SRC_OPS_DEPTHWISE_CONV2D_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DepthwiseConv2D : public PrimitiveC { - public: - DepthwiseConv2D() = default; - ~DepthwiseConv2D() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DepthwiseConv2D, PrimitiveC); - explicit DepthwiseConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetFormat(int format); - void SetChannelIn(int channel_in); - void SetChannelMultiplier(int channel_multiplier); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetChannelIn() const; - int GetChannelMultiplier() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - int GetActivationType() const; - - int PadUp() const { return this->pad_u_; } - int PadDown() const { return this->pad_d_; } - int PadLeft() const { return this->pad_l_; } - int PadRight() const { return this->pad_r_; } - int GetInputChannel() const { return this->input_channel_; } - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; - int input_channel_ = 0; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_DEPTHWISE_CONV2D_H_ diff --git a/mindspore/lite/src/ops/dequant.cc b/mindspore/lite/src/ops/dequant.cc deleted file mode 100644 index 13de810376..0000000000 --- a/mindspore/lite/src/ops/dequant.cc +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/dequant.h" -#include -#include - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Dequant::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_OnnxInt8Dequantize; - } - if (this->primitive_->value.type != schema::PrimitiveType_OnnxInt8Dequantize) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow)(schema::OnnxInt8DequantizeT); - if (attr == nullptr) { - MS_LOG(ERROR) << "attr is nullptr"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/dequant.h b/mindspore/lite/src/ops/dequant.h deleted file mode 100644 index 046055abbd..0000000000 --- a/mindspore/lite/src/ops/dequant.h +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_DEQUANT_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_DEQUANT_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Dequant : public PrimitiveC { - public: - Dequant() = default; - ~Dequant() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Dequant, PrimitiveC); - explicit Dequant(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_SRC_OPS_DEQUANT_H_ diff --git a/mindspore/lite/src/ops/detection_post_process.cc b/mindspore/lite/src/ops/detection_post_process.cc deleted file mode 100644 index dc608ef40e..0000000000 --- a/mindspore/lite/src/ops/detection_post_process.cc +++ /dev/null @@ -1,208 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/detection_post_process.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int DetectionPostProcess::GetFormat() const { return this->primitive_->value.AsDetectionPostProcess()->format; } -int DetectionPostProcess::GetInputSize() const { return this->primitive_->value.AsDetectionPostProcess()->inputSize; } -float DetectionPostProcess::GetHScale() const { return this->primitive_->value.AsDetectionPostProcess()->hScale; } -float DetectionPostProcess::GetWScale() const { return this->primitive_->value.AsDetectionPostProcess()->wScale; } -float DetectionPostProcess::GetXScale() const { return this->primitive_->value.AsDetectionPostProcess()->xScale; } -float DetectionPostProcess::GetYScale() const { return this->primitive_->value.AsDetectionPostProcess()->yScale; } -float DetectionPostProcess::GetNmsIouThreshold() const { - return this->primitive_->value.AsDetectionPostProcess()->NmsIouThreshold; -} -float DetectionPostProcess::GetNmsScoreThreshold() const { - return this->primitive_->value.AsDetectionPostProcess()->NmsScoreThreshold; -} -int64_t DetectionPostProcess::GetMaxDetections() const { - return this->primitive_->value.AsDetectionPostProcess()->MaxDetections; -} -int64_t DetectionPostProcess::GetDetectionsPerClass() const { - return this->primitive_->value.AsDetectionPostProcess()->DetectionsPerClass; -} -int64_t DetectionPostProcess::GetMaxClassesPerDetection() const { - return this->primitive_->value.AsDetectionPostProcess()->MaxClassesPerDetection; -} -int64_t DetectionPostProcess::GetNumClasses() const { - return this->primitive_->value.AsDetectionPostProcess()->NumClasses; -} -bool DetectionPostProcess::GetUseRegularNms() const { - return this->primitive_->value.AsDetectionPostProcess()->UseRegularNms; -} -void DetectionPostProcess::SetFormat(int format) { - this->primitive_->value.AsDetectionPostProcess()->format = (schema::Format)format; -} -void DetectionPostProcess::SetInputSize(int input_size) { - this->primitive_->value.AsDetectionPostProcess()->inputSize = input_size; -} -void DetectionPostProcess::SetHScale(float h_scale) { - this->primitive_->value.AsDetectionPostProcess()->hScale = h_scale; -} -void DetectionPostProcess::SetWScale(float w_scale) { - this->primitive_->value.AsDetectionPostProcess()->wScale = w_scale; -} -void DetectionPostProcess::SetXScale(float x_scale) { - this->primitive_->value.AsDetectionPostProcess()->xScale = x_scale; -} -void DetectionPostProcess::SetYScale(float y_scale) { - this->primitive_->value.AsDetectionPostProcess()->yScale = y_scale; -} -void DetectionPostProcess::SetNmsIouThreshold(float nms_iou_threshold) { - this->primitive_->value.AsDetectionPostProcess()->NmsIouThreshold = nms_iou_threshold; -} -void DetectionPostProcess::SetNmsScoreThreshold(float nms_score_threshold) { - this->primitive_->value.AsDetectionPostProcess()->NmsScoreThreshold = nms_score_threshold; -} -void DetectionPostProcess::SetMaxDetections(int64_t max_detections) { - this->primitive_->value.AsDetectionPostProcess()->MaxDetections = max_detections; -} -void DetectionPostProcess::SetDetectionsPerClass(int64_t detections_per_class) { - this->primitive_->value.AsDetectionPostProcess()->DetectionsPerClass = detections_per_class; -} -void DetectionPostProcess::SetMaxClassesPerDetection(int64_t max_classes_per_detection) { - this->primitive_->value.AsDetectionPostProcess()->MaxClassesPerDetection = max_classes_per_detection; -} -void DetectionPostProcess::SetNumClasses(int64_t num_classes) { - this->primitive_->value.AsDetectionPostProcess()->NumClasses = num_classes; -} -void DetectionPostProcess::SetUseRegularNms(bool use_regular_nms) { - this->primitive_->value.AsDetectionPostProcess()->UseRegularNms = use_regular_nms; -} -void DetectionPostProcess::SetOutQuantized(bool out_quantized) { - this->primitive_->value.AsDetectionPostProcess()->OutQuantized = out_quantized; -} - -#else -int DetectionPostProcess::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_DetectionPostProcess(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DetectionPostProcess return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDetectionPostProcess( - *fbb, attr->format(), attr->inputSize(), attr->hScale(), attr->wScale(), attr->xScale(), attr->yScale(), - attr->NmsIouThreshold(), attr->NmsScoreThreshold(), attr->MaxDetections(), attr->DetectionsPerClass(), - attr->MaxClassesPerDetection(), attr->NumClasses(), attr->UseRegularNms(), attr->OutQuantized()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DetectionPostProcess, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int DetectionPostProcess::GetFormat() const { return this->primitive_->value_as_DetectionPostProcess()->format(); } -int DetectionPostProcess::GetInputSize() const { - return this->primitive_->value_as_DetectionPostProcess()->inputSize(); -} -float DetectionPostProcess::GetHScale() const { return this->primitive_->value_as_DetectionPostProcess()->hScale(); } -float DetectionPostProcess::GetWScale() const { return this->primitive_->value_as_DetectionPostProcess()->wScale(); } -float DetectionPostProcess::GetXScale() const { return this->primitive_->value_as_DetectionPostProcess()->xScale(); } -float DetectionPostProcess::GetYScale() const { return this->primitive_->value_as_DetectionPostProcess()->yScale(); } -float DetectionPostProcess::GetNmsIouThreshold() const { - return this->primitive_->value_as_DetectionPostProcess()->NmsIouThreshold(); -} -float DetectionPostProcess::GetNmsScoreThreshold() const { - return this->primitive_->value_as_DetectionPostProcess()->NmsScoreThreshold(); -} -int64_t DetectionPostProcess::GetMaxDetections() const { - return this->primitive_->value_as_DetectionPostProcess()->MaxDetections(); -} -int64_t DetectionPostProcess::GetDetectionsPerClass() const { - return this->primitive_->value_as_DetectionPostProcess()->DetectionsPerClass(); -} -int64_t DetectionPostProcess::GetMaxClassesPerDetection() const { - return this->primitive_->value_as_DetectionPostProcess()->MaxClassesPerDetection(); -} -int64_t DetectionPostProcess::GetNumClasses() const { - return this->primitive_->value_as_DetectionPostProcess()->NumClasses(); -} -bool DetectionPostProcess::GetUseRegularNms() const { - return this->primitive_->value_as_DetectionPostProcess()->UseRegularNms(); -} -bool DetectionPostProcess::GetOutQuantized() const { - return this->primitive_->value_as_DetectionPostProcess()->OutQuantized(); -} - -PrimitiveC *DetectionPostProcessCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DetectionPostProcessRegistry(schema::PrimitiveType_DetectionPostProcess, DetectionPostProcessCreator); -#endif -namespace { -constexpr int kDetectionPostProcessOutputNum = 4; -constexpr int kDetectionPostProcessInputNum = 3; -} // namespace -int DetectionPostProcess::InferShape(std::vector inputs_, std::vector outputs_) { - if (outputs_.size() != kDetectionPostProcessOutputNum || inputs_.size() != kDetectionPostProcessInputNum) { - MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs_.size() << ",input size: " << inputs_.size(); - return RET_PARAM_INVALID; - } - auto boxes = inputs_.at(0); - MS_ASSERT(boxes != nullptr); - auto scores = inputs_.at(1); - MS_ASSERT(scores != nullptr); - auto anchors = inputs_.at(2); - MS_ASSERT(anchors != nullptr); - - const auto input_box_shape = boxes->shape(); - const auto input_scores_shape = scores->shape(); - const auto input_anchors_shape = anchors->shape(); - MS_ASSERT(input_scores_shape[2] >= GetNumClasses()); - MS_ASSERT(input_scores_shape[2] - GetNumClasses() <= 1); - MS_ASSERT(input_box_shape[1] == input_scores_shape[1]); - MS_ASSERT(input_box_shape[1] == input_anchors_shape[0]); - - auto detected_boxes = outputs_.at(0); - MS_ASSERT(detected_boxes != nullptr); - auto detected_classes = outputs_.at(1); - MS_ASSERT(detected_classes != nullptr); - auto detected_scores = outputs_.at(2); - MS_ASSERT(detected_scores != nullptr); - auto num_det = outputs_.at(3); - MS_ASSERT(num_det != nullptr); - - detected_boxes->set_format(boxes->format()); - detected_boxes->set_data_type(kNumberTypeFloat32); - detected_classes->set_format(boxes->format()); - detected_classes->set_data_type(kNumberTypeFloat32); - detected_scores->set_format(boxes->format()); - detected_scores->set_data_type(kNumberTypeFloat32); - num_det->set_format(boxes->format()); - num_det->set_data_type(kNumberTypeFloat32); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - const auto max_detections = GetMaxDetections(); - const auto max_classes_per_detection = GetMaxClassesPerDetection(); - const auto num_detected_boxes = static_cast(max_detections * max_classes_per_detection); - const std::vector box_shape{1, num_detected_boxes, 4}; - const std::vector class_shape{1, num_detected_boxes}; - const std::vector num_shape{1}; - detected_boxes->set_shape(box_shape); - detected_classes->set_shape(class_shape); - detected_scores->set_shape(class_shape); - num_det->set_shape(num_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/detection_post_process.h b/mindspore/lite/src/ops/detection_post_process.h deleted file mode 100644 index d93d5807a2..0000000000 --- a/mindspore/lite/src/ops/detection_post_process.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ -#define LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DetectionPostProcess : public PrimitiveC { - public: - DetectionPostProcess() = default; - ~DetectionPostProcess() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DetectionPostProcess, PrimitiveC); - explicit DetectionPostProcess(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetInputSize(int input_size); - void SetHScale(float h_scale); - void SetWScale(float w_scale); - void SetXScale(float x_scale); - void SetYScale(float y_scale); - void SetNmsIouThreshold(float nms_iou_threshold); - void SetNmsScoreThreshold(float nms_score_threshold); - void SetMaxDetections(int64_t max_detections); - void SetDetectionsPerClass(int64_t detections_per_class); - void SetMaxClassesPerDetection(int64_t max_classes_per_detection); - void SetNumClasses(int64_t num_classes); - void SetUseRegularNms(bool use_regular_nms); - void SetOutQuantized(bool out_quantized); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetInputSize() const; - float GetHScale() const; - float GetWScale() const; - float GetXScale() const; - float GetYScale() const; - float GetNmsIouThreshold() const; - float GetNmsScoreThreshold() const; - int64_t GetMaxDetections() const; - int64_t GetDetectionsPerClass() const; - int64_t GetMaxClassesPerDetection() const; - int64_t GetNumClasses() const; - bool GetUseRegularNms() const; - bool GetOutQuantized() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ diff --git a/mindspore/lite/src/ops/div.cc b/mindspore/lite/src/ops/div.cc deleted file mode 100644 index f345e01d30..0000000000 --- a/mindspore/lite/src/ops/div.cc +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/div.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Div::GetActivationType() const { return this->primitive_->value.AsDiv()->activationType; } - -void Div::SetActivationType(int activation_type) { - this->primitive_->value.AsDiv()->activationType = (schema::ActivationType)activation_type; -} - -int Div::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Div; - } - if (this->primitive_->value.type != schema::PrimitiveType_Div) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::DivT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - - return RET_OK; -} - -#else -int Div::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Div(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Div return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDiv(*fbb, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Div, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Div::GetActivationType() const { return this->primitive_->value_as_Div()->activationType(); } - -PrimitiveC *DivCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC
(primitive); } -Registry DivRegistry(schema::PrimitiveType_Div, DivCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/div.h b/mindspore/lite/src/ops/div.h deleted file mode 100644 index c23e7ab5c4..0000000000 --- a/mindspore/lite/src/ops/div.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_DIV_H_ -#define LITE_MINDSPORE_LITE_C_OPS_DIV_H_ - -#include -#include -#include -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Div : public Arithmetic { - public: - Div() = default; - ~Div() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Div, Arithmetic); - explicit Div(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - void SetActivationType(int activation_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_DIV_H_ diff --git a/mindspore/lite/src/ops/dropout.cc b/mindspore/lite/src/ops/dropout.cc deleted file mode 100644 index a34bdeaa97..0000000000 --- a/mindspore/lite/src/ops/dropout.cc +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/dropout.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Dropout::GetRatio() const { return this->primitive_->value.AsDropout()->ratio; } - -void Dropout::SetRatio(float ratio) { this->primitive_->value.AsDropout()->ratio = ratio; } - -int Dropout::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Dropout; - } - if (this->primitive_->value.type != schema::PrimitiveType_Dropout) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::DropoutT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.GetAttr("keep_prob") != nullptr) { - attr->ratio = GetValue(prim.GetAttr("keep_prob")); - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int Dropout::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Dropout(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Dropout return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDropout(*fbb, attr->ratio()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Dropout, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float Dropout::GetRatio() const { return this->primitive_->value_as_Dropout()->ratio(); } - -PrimitiveC *DropoutCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry DropoutRegistry(schema::PrimitiveType_Dropout, DropoutCreator); -#endif -int Dropout::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output0 = outputs_.front(); - MS_ASSERT(output0 != nullptr); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - output0->set_shape(input->shape()); - output0->set_data_type(input->data_type()); - output0->set_format(input->format()); - if (outputs_.size() > 1) { - auto output1 = outputs_[1]; - MS_ASSERT(output1 != nullptr); - output1->set_shape(input->shape()); - output1->set_data_type(input->data_type()); - output1->set_format(input->format()); - } - return RET_OK; -} - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/dropout.h b/mindspore/lite/src/ops/dropout.h deleted file mode 100644 index 21310974b6..0000000000 --- a/mindspore/lite/src/ops/dropout.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_DROPOUT_H_ -#define MINDSPORE_LITE_SRC_OPS_DROPOUT_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Dropout : public PrimitiveC { - public: - Dropout() = default; - ~Dropout() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Dropout, PrimitiveC); - explicit Dropout(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetRatio(float ratio); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetRatio() const; - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; - -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_DROPOUT_H_ diff --git a/mindspore/lite/src/ops/dropout_grad.cc b/mindspore/lite/src/ops/dropout_grad.cc deleted file mode 100644 index 443a5571e9..0000000000 --- a/mindspore/lite/src/ops/dropout_grad.cc +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/dropout_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float DropoutGrad::GetRatio() const { return this->primitive_->value.AsDropout()->ratio; } - -void DropoutGrad::SetRatio(float ratio) { this->primitive_->value.AsDropout()->ratio = ratio; } - -int DropoutGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_DropoutGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_DropoutGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::DropoutGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.GetAttr("keep_prob") != nullptr) { - attr->ratio = GetValue(prim.GetAttr("keep_prob")); - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int DropoutGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_DropoutGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_DropoutGrad return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateDropoutGrad(*fbb, attr->ratio()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_DropoutGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float DropoutGrad::GetRatio() const { return this->primitive_->value_as_DropoutGrad()->ratio(); } - -PrimitiveC *DropoutGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry DropoutGradRegistry(schema::PrimitiveType_DropoutGrad, DropoutGradCreator); - -#endif -int DropoutGrad::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - MS_ASSERT(inputs_.size() == 2); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - output->set_shape(input->shape()); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/dropout_grad.h b/mindspore/lite/src/ops/dropout_grad.h deleted file mode 100644 index c0d0d11c29..0000000000 --- a/mindspore/lite/src/ops/dropout_grad.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_DROPOUT_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_DROPOUT_GRAD_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class DropoutGrad : public PrimitiveC { - public: -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(DropoutGrad, PrimitiveC); - DropoutGrad() = default; - explicit DropoutGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetRatio(float ratio); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - -#else - DropoutGrad() = default; - - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetRatio() const; - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_DROPOUT_GRAD_H_ diff --git a/mindspore/lite/src/ops/eltwise.cc b/mindspore/lite/src/ops/eltwise.cc deleted file mode 100644 index 0bec8276c4..0000000000 --- a/mindspore/lite/src/ops/eltwise.cc +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/eltwise.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Eltwise::GetMode() const { return this->primitive_->value.AsEltwise()->mode; } - -void Eltwise::SetMode(int mode) { this->primitive_->value.AsEltwise()->mode = (schema::EltwiseMode)mode; } - -#else -int Eltwise::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Eltwise(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Eltwise return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateEltwise(*fbb, attr->mode()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Eltwise, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Eltwise::GetMode() const { return this->primitive_->value_as_Eltwise()->mode(); } - -PrimitiveC *EltwiseCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry EltwiseRegistry(schema::PrimitiveType_Eltwise, EltwiseCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/eltwise.h b/mindspore/lite/src/ops/eltwise.h deleted file mode 100644 index 1f6222144c..0000000000 --- a/mindspore/lite/src/ops/eltwise.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Eltwise : public Arithmetic { - public: - Eltwise() = default; - ~Eltwise() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Eltwise, Arithmetic); - explicit Eltwise(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - void SetMode(int mode); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetMode() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ diff --git a/mindspore/lite/src/ops/elu.cc b/mindspore/lite/src/ops/elu.cc deleted file mode 100644 index 506f9f381f..0000000000 --- a/mindspore/lite/src/ops/elu.cc +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/elu.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Elu::GetAlpha() const { return this->primitive_->value.AsElu()->alpha; } - -void Elu::SetAlpha(float alpha) { this->primitive_->value.AsElu()->alpha = alpha; } - -int Elu::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Elu; - } - if (this->primitive_->value.type != schema::PrimitiveType_Elu) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - auto attr = std::make_unique(); - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - return RET_OK; -} -#else -int Elu::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Elu(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Elu return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateElu(*fbb, attr->alpha()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Elu, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float Elu::GetAlpha() const { return this->primitive_->value_as_Elu()->alpha(); } - -PrimitiveC *EluCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry EluRegistry(schema::PrimitiveType_Elu, EluCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/elu.h b/mindspore/lite/src/ops/elu.h deleted file mode 100644 index 9b025e69bd..0000000000 --- a/mindspore/lite/src/ops/elu.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ELU_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ELU_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Elu : public PrimitiveC { - public: - Elu() = default; - ~Elu() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Elu, PrimitiveC); - explicit Elu(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAlpha(float alpha); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetAlpha() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ELU_H_ diff --git a/mindspore/lite/src/ops/embedding_lookup.cc b/mindspore/lite/src/ops/embedding_lookup.cc deleted file mode 100644 index a0a3ee7a06..0000000000 --- a/mindspore/lite/src/ops/embedding_lookup.cc +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/embedding_lookup.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float EmbeddingLookup::GetMaxNorm() const { return this->primitive_->value.AsEmbeddingLookup()->maxNorm; } - -void EmbeddingLookup::SetMaxNorm(float max_norm) { this->primitive_->value.AsEmbeddingLookup()->maxNorm = max_norm; } - -#else -int EmbeddingLookup::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto attr = primitive->value_as_EmbeddingLookup(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_EmbeddingLookup return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateEmbeddingLookup(*fbb, attr->maxNorm()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_EmbeddingLookup, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float EmbeddingLookup::GetMaxNorm() const { return this->primitive_->value_as_EmbeddingLookup()->maxNorm(); } - -PrimitiveC *EmbeddingLookupCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry EmbeddingLookupRegistry(schema::PrimitiveType_EmbeddingLookup, EmbeddingLookupCreator); -#endif - -int EmbeddingLookup::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() < kDoubleNum) { - MS_LOG(ERROR) << "Embedding Lookup should have at least two inputs"; - return RET_INPUT_TENSOR_ERROR; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "Embedding Lookup should have one outputs"; - return RET_INPUT_TENSOR_ERROR; - } - auto params_ = inputs_.front(); - MS_ASSERT(params_ != nullptr); - auto ids = inputs_.back(); - MS_ASSERT(ids != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(params_->format()); - output->set_data_type(params_->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto embedding_shape = params_->shape(); - embedding_shape.erase(embedding_shape.begin()); - std::vector output_shape(ids->shape()); - for (size_t i = 0; i < embedding_shape.size(); ++i) { - output_shape.push_back(embedding_shape.at(i)); - } - for (size_t i = 1; i < inputs_.size() - 1; ++i) { - auto embedding_shape_t = inputs_.at(i)->shape(); - embedding_shape_t.erase(embedding_shape_t.begin()); - if (embedding_shape_t != embedding_shape) { - MS_LOG(ERROR) << "The embedded layers should have the same shape"; - return RET_INPUT_TENSOR_ERROR; - } - } - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/embedding_lookup.h b/mindspore/lite/src/ops/embedding_lookup.h deleted file mode 100644 index 01898bb7fb..0000000000 --- a/mindspore/lite/src/ops/embedding_lookup.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ -#define LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class EmbeddingLookup : public PrimitiveC { - public: - EmbeddingLookup() = default; - ~EmbeddingLookup() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(EmbeddingLookup, PrimitiveC); - explicit EmbeddingLookup(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetMaxNorm(float max_norm); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetMaxNorm() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ diff --git a/mindspore/lite/src/ops/equal.cc b/mindspore/lite/src/ops/equal.cc deleted file mode 100644 index ef2ebaeee6..0000000000 --- a/mindspore/lite/src/ops/equal.cc +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/equal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Equal::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Equal; - } - if (this->primitive_->value.type != schema::PrimitiveType_Equal) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::EqualT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Equal::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Equal, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *EqualCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry EqualRegistry(schema::PrimitiveType_Equal, EqualCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/equal.h b/mindspore/lite/src/ops/equal.h deleted file mode 100644 index 1dc8d3ab75..0000000000 --- a/mindspore/lite/src/ops/equal.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ - -#include -#include -#include -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class Equal : public ArithmeticCompare { - public: - Equal() = default; - ~Equal() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Equal, ArithmeticCompare); - explicit Equal(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ diff --git a/mindspore/lite/src/ops/erf.h b/mindspore/lite/src/ops/erf.h deleted file mode 100644 index a8c9c56038..0000000000 --- a/mindspore/lite/src/ops/erf.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ERF_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ERF_H_ - -namespace mindspore { -namespace lite { -class Erf : public PrimitiveC { - public: - MS_DECLARE_PARENT(Erf, PrimitiveC); - Erf() = default; - ~Erf() = default; - explicit Erf(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_ERF_H_ diff --git a/mindspore/lite/src/ops/exp.cc b/mindspore/lite/src/ops/exp.cc deleted file mode 100644 index e51a67f05e..0000000000 --- a/mindspore/lite/src/ops/exp.cc +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/exp.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -void Exp::SetBase(float base) { this->primitive_->value.AsExp()->base = base; } -void Exp::SetScale(float scale) { this->primitive_->value.AsExp()->scale = scale; } -void Exp::SetShift(float shift) { this->primitive_->value.AsExp()->shift = shift; } - -float Exp::GetBase() const { return this->primitive_->value.AsExp()->base; } -float Exp::GetScale() const { return this->primitive_->value.AsExp()->scale; } -float Exp::GetShift() const { return this->primitive_->value.AsExp()->shift; } - -int Exp::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Exp; - } - if (this->primitive_->value.type != schema::PrimitiveType_Exp) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::ExpT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else - -int Exp::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Exp(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Exp return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateExp(*fbb, attr->base(), attr->scale(), attr->shift()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Exp, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -float Exp::GetBase() const { return this->primitive_->value_as_Exp()->base(); } -float Exp::GetScale() const { return this->primitive_->value_as_Exp()->scale(); } -float Exp::GetShift() const { return this->primitive_->value_as_Exp()->shift(); } - -PrimitiveC *ExpCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ExpRegistry(schema::PrimitiveType_Exp, ExpCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/exp.h b/mindspore/lite/src/ops/exp.h deleted file mode 100644 index 681326efea..0000000000 --- a/mindspore/lite/src/ops/exp.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_EXP_H_ -#define LITE_MINDSPORE_LITE_C_OPS_EXP_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Exp : public PrimitiveC { - public: - Exp() = default; - ~Exp() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Exp, PrimitiveC); - explicit Exp(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetBase(float base); - void SetShift(float shift); - void SetScale(float scale); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetBase() const; - float GetShift() const; - float GetScale() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_EXP_H_ diff --git a/mindspore/lite/src/ops/expand_dims.cc b/mindspore/lite/src/ops/expand_dims.cc deleted file mode 100644 index 4ca40f682b..0000000000 --- a/mindspore/lite/src/ops/expand_dims.cc +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/expand_dims.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ExpandDims::GetDim() const { return this->primitive_->value.AsExpandDims()->dim; } - -void ExpandDims::SetDim(int dim) { this->primitive_->value.AsExpandDims()->dim = dim; } - -int ExpandDims::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_ExpandDims; - } - if (this->primitive_->value.type != schema::PrimitiveType_ExpandDims) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ExpandDimsT(); - if (attr == nullptr) { - delete this->primitive_; - this->primitive_ = nullptr; - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - // use axis instead of dim - if (inputs.at(1)->isa()) { - auto axis_tensor = inputs.at(1)->cast(); - int axis = CastToInt(axis_tensor->value()).front(); - attr->dim = axis; - } else { - MS_LOG(ERROR) << "input axis is not value node."; - delete this->primitive_; - delete attr; - this->primitive_ = nullptr; - attr = nullptr; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else -int ExpandDims::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_ExpandDims(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ExpandDims return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateExpandDims(*fbb, attr->dim()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ExpandDims, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int ExpandDims::GetDim() const { return this->primitive_->value_as_ExpandDims()->dim(); } - -PrimitiveC *ExpandDimsCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ExpandDimsRegistry(schema::PrimitiveType_ExpandDims, ExpandDimsCreator); -#endif - -int ExpandDims::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "output size is invalid"; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - int dim = GetDim(); - if (dim < 0) { - dim += input->shape().size() + 1; - } - if (dim > static_cast(input->shape().size())) { - MS_LOG(ERROR) << "attribute dim out of range"; - return RET_INPUT_TENSOR_ERROR; - } - auto out_shape = input->shape(); - out_shape.insert(out_shape.begin() + dim, 1, 1); - output->set_shape(out_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/expand_dims.h b/mindspore/lite/src/ops/expand_dims.h deleted file mode 100644 index bb580b8411..0000000000 --- a/mindspore/lite/src/ops/expand_dims.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ -#define LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ExpandDims : public PrimitiveC { - public: - ExpandDims() = default; - ~ExpandDims() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ExpandDims, PrimitiveC); - explicit ExpandDims(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetDim(int dim); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetDim() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ diff --git a/mindspore/lite/src/ops/fake_quant_with_min_max_vars.cc b/mindspore/lite/src/ops/fake_quant_with_min_max_vars.cc deleted file mode 100644 index 0f09dc4de2..0000000000 --- a/mindspore/lite/src/ops/fake_quant_with_min_max_vars.cc +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/fake_quant_with_min_max_vars.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool FakeQuantWithMinMaxVars::GetNarrowRange() const { - return this->primitive_->value.AsFakeQuantWithMinMaxVars()->narrowRange; -} -int FakeQuantWithMinMaxVars::GetNumBits() const { return this->primitive_->value.AsFakeQuantWithMinMaxVars()->numBits; } - -void FakeQuantWithMinMaxVars::SetNarrowRange(bool narrow_range) { - this->primitive_->value.AsFakeQuantWithMinMaxVars()->narrowRange = narrow_range; -} -void FakeQuantWithMinMaxVars::SetNumBits(int num_bits) { - this->primitive_->value.AsFakeQuantWithMinMaxVars()->numBits = num_bits; -} - -#else -int FakeQuantWithMinMaxVars::UnPackToFlatBuilder(const schema::Primitive *primitive, - flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_FakeQuantWithMinMaxVars(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_FakeQuantWithMinMaxVars return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateFakeQuantWithMinMaxVars(*fbb, attr->narrowRange(), attr->numBits()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FakeQuantWithMinMaxVars, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -bool FakeQuantWithMinMaxVars::GetNarrowRange() const { - return this->primitive_->value_as_FakeQuantWithMinMaxVars()->narrowRange(); -} -int FakeQuantWithMinMaxVars::GetNumBits() const { - return this->primitive_->value_as_FakeQuantWithMinMaxVars()->numBits(); -} - -PrimitiveC *FakeQuantWithMinMaxVarsCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FakeQuantWithMinMaxVarsRegistry(schema::PrimitiveType_FakeQuantWithMinMaxVars, FakeQuantWithMinMaxVarsCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/fake_quant_with_min_max_vars.h b/mindspore/lite/src/ops/fake_quant_with_min_max_vars.h deleted file mode 100644 index 7b9e6dd1c5..0000000000 --- a/mindspore/lite/src/ops/fake_quant_with_min_max_vars.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FakeQuantWithMinMaxVars : public PrimitiveC { - public: - FakeQuantWithMinMaxVars() = default; - ~FakeQuantWithMinMaxVars() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FakeQuantWithMinMaxVars, PrimitiveC); - explicit FakeQuantWithMinMaxVars(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetNarrowRange(bool narrow_range); - void SetNumBits(int num_bits); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - bool GetNarrowRange() const; - int GetNumBits() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ diff --git a/mindspore/lite/src/ops/fft_imag.cc b/mindspore/lite/src/ops/fft_imag.cc deleted file mode 100644 index 73f9b9b60f..0000000000 --- a/mindspore/lite/src/ops/fft_imag.cc +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/fft_imag.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int FftImag::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FftImag, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *FftImagCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry FftImagRegistry(schema::PrimitiveType_FftImag, FftImagCreator); -#endif -int FftImag::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(TypeId::kNumberTypeFloat32); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - input_shape.pop_back(); - outputs_.front()->set_shape(input_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/fft_imag.h b/mindspore/lite/src/ops/fft_imag.h deleted file mode 100644 index c804630b10..0000000000 --- a/mindspore/lite/src/ops/fft_imag.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FFT_IMAG_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FFT_IMAG_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FftImag : public PrimitiveC { - public: - FftImag() = default; - ~FftImag() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FftImag, PrimitiveC); - explicit FftImag(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FFT_IMAG_H_ diff --git a/mindspore/lite/src/ops/fft_real.cc b/mindspore/lite/src/ops/fft_real.cc deleted file mode 100644 index 5d65ce0f34..0000000000 --- a/mindspore/lite/src/ops/fft_real.cc +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/fft_real.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int FftReal::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FftReal, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *FftRealCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry FftRealRegistry(schema::PrimitiveType_FftReal, FftRealCreator); -#endif -int FftReal::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(TypeId::kNumberTypeFloat32); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - input_shape.pop_back(); - outputs_.front()->set_shape(input_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/fft_real.h b/mindspore/lite/src/ops/fft_real.h deleted file mode 100644 index f61493956e..0000000000 --- a/mindspore/lite/src/ops/fft_real.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FFT_REAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FFT_REAL_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FftReal : public PrimitiveC { - public: - FftReal() = default; - ~FftReal() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FftReal, PrimitiveC); - explicit FftReal(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FFT_REAL_H_ diff --git a/mindspore/lite/src/ops/fill.cc b/mindspore/lite/src/ops/fill.cc deleted file mode 100644 index b322bc1ac6..0000000000 --- a/mindspore/lite/src/ops/fill.cc +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/fill.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector Fill::GetDims() const { return this->primitive_->value.AsFill()->dims; } - -void Fill::SetDims(const std::vector &dims) { this->primitive_->value.AsFill()->dims = dims; } - -#else -int Fill::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Fill(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Fill return nullptr"; - return RET_ERROR; - } - std::vector dims; - if (attr->dims() != nullptr) { - for (int i = 0; i < static_cast(attr->dims()->size()); i++) { - dims.push_back(attr->dims()->data()[i]); - } - } - auto val_offset = schema::CreateFillDirect(*fbb, &dims); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Fill, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -std::vector Fill::GetDims() const { - auto fb_vector = this->primitive_->value_as_Fill()->dims(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} - -PrimitiveC *FillCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry FillRegistry(schema::PrimitiveType_Fill, FillCreator); -#endif - -int Fill::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - auto output = outputs_.front(); - if (input == nullptr || output == nullptr) { - MS_LOG(ERROR) << "Fill input or output is null!"; - return RET_ERROR; - } - if ((inputs_.size() != kSingleNum && inputs_.size() != kDoubleNum) || outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); - return RET_INPUT_TENSOR_ERROR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector output_shape; - auto param_dims = GetDims(); - for (size_t i = 0; i < param_dims.size(); i++) { - output_shape.push_back(param_dims.at(i)); - } - - if (inputs_.size() == kDoubleNum) { - auto input_dims = inputs_.at(1); - MS_ASSERT(input_dims != nullptr); - if (input_dims->data_c() == nullptr) { - return RET_INFER_INVALID; - } - int *dims_data = reinterpret_cast(input_dims->data_c()); - output_shape = std::vector{dims_data, dims_data + input_dims->ElementsNum()}; - } - - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/fill.h b/mindspore/lite/src/ops/fill.h deleted file mode 100644 index 5af4037c3c..0000000000 --- a/mindspore/lite/src/ops/fill.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FILL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FILL_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Fill : public PrimitiveC { - public: - Fill() = default; - ~Fill() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Fill, PrimitiveC); - explicit Fill(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetDims(const std::vector &dims); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetDims() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FILL_H_ diff --git a/mindspore/lite/src/ops/flatten.cc b/mindspore/lite/src/ops/flatten.cc deleted file mode 100644 index 06227a12d7..0000000000 --- a/mindspore/lite/src/ops/flatten.cc +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/flatten.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -int Flatten::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - auto output = outputs_.front(); - if (input == nullptr || output == nullptr) { - MS_LOG(ERROR) << "Flatten input or output is null!"; - return RET_ERROR; - } - if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); - return RET_INPUT_TENSOR_ERROR; - } - - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto input_shape = input->shape(); - std::vector output_shape(2); - output_shape.at(0) = input_shape.at(0); - output_shape.at(1) = 1; - for (size_t i = 1; i < input_shape.size(); i++) { - output_shape.at(1) *= input_shape.at(i); - } - output->set_shape(output_shape); - return RET_OK; -} -#ifdef PRIMITIVE_WRITEABLE -int Flatten::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Flatten; - } - if (this->primitive_->value.type != schema::PrimitiveType_Flatten) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::FlattenT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Flatten::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateFlatten(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Flatten, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *FlattenCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry FlattenRegistry(schema::PrimitiveType_Flatten, FlattenCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/flatten.h b/mindspore/lite/src/ops/flatten.h deleted file mode 100644 index 04b5d97550..0000000000 --- a/mindspore/lite/src/ops/flatten.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Flatten : public PrimitiveC { - public: - Flatten() = default; - ~Flatten() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Flatten, PrimitiveC); - explicit Flatten(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ diff --git a/mindspore/lite/src/ops/flatten_grad.cc b/mindspore/lite/src/ops/flatten_grad.cc deleted file mode 100644 index f0e52562f3..0000000000 --- a/mindspore/lite/src/ops/flatten_grad.cc +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/flatten_grad.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -int FlattenGrad::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - auto output = outputs_.front(); - if (input == nullptr || output == nullptr) { - MS_LOG(ERROR) << "FlattenGrad input or output is null!"; - return RET_ERROR; - } - if (inputs_.size() != kDoubleNum || outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); - return RET_INPUT_TENSOR_ERROR; - } - - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto output_size = inputs_.at(1)->shape().at(0); - std::vector output_shape(output_size); - for (int i = 0; i < output_size; i++) { - output_shape.at(i) = static_cast(inputs_.at(1)->data_c())[i]; - } - output->set_shape(output_shape); - return RET_OK; -} - -#ifdef PRIMITIVE_WRITEABLE -int FlattenGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_FlattenGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_FlattenGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::FlattenGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int FlattenGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateFlattenGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FlattenGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *FlattenGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FlattenGradRegistry(schema::PrimitiveType_FlattenGrad, FlattenGradCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/flatten_grad.h b/mindspore/lite/src/ops/flatten_grad.h deleted file mode 100644 index 59fb1823e0..0000000000 --- a/mindspore/lite/src/ops/flatten_grad.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FlattenGrad_GRAD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FlattenGrad_GRAD_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FlattenGrad : public PrimitiveC { - public: - FlattenGrad() = default; - ~FlattenGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FlattenGrad, PrimitiveC); - explicit FlattenGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FlattenGrad_H_ diff --git a/mindspore/lite/src/ops/floor.cc b/mindspore/lite/src/ops/floor.cc deleted file mode 100644 index 80e4bc1122..0000000000 --- a/mindspore/lite/src/ops/floor.cc +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/floor.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Floor::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Floor; - } - if (this->primitive_->value.type != schema::PrimitiveType_Floor) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::FloorT(); - if (attr == nullptr) { - delete this->primitive_; - this->primitive_ = nullptr; - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} -#else -int Floor::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateFloor(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Floor, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *FloorCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry FloorRegistry(schema::PrimitiveType_Floor, FloorCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/floor.h b/mindspore/lite/src/ops/floor.h deleted file mode 100644 index 54a1ad566f..0000000000 --- a/mindspore/lite/src/ops/floor.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ - -#include -#include -#include -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Floor : public ArithmeticSelf { - public: - Floor() = default; - ~Floor() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Floor, ArithmeticSelf); - explicit Floor(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ diff --git a/mindspore/lite/src/ops/floor_div.cc b/mindspore/lite/src/ops/floor_div.cc deleted file mode 100644 index c52a6e84c9..0000000000 --- a/mindspore/lite/src/ops/floor_div.cc +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/floor_div.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int FloorDiv::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_FloorDiv; - } - if (this->primitive_->value.type != schema::PrimitiveType_FloorDiv) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::FloorDivT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -int FloorDiv::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateFloorDiv(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FloorDiv, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *FloorDivCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FloorDivRegistry(schema::PrimitiveType_FloorDiv, FloorDivCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/floor_div.h b/mindspore/lite/src/ops/floor_div.h deleted file mode 100644 index f8515d4ab2..0000000000 --- a/mindspore/lite/src/ops/floor_div.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_FLOOR_DIV_H_ -#define MINDSPORE_LITE_SRC_OPS_FLOOR_DIV_H_ - -#include -#include -#include -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class FloorDiv : public Arithmetic { - public: - FloorDiv() = default; - ~FloorDiv() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FloorDiv, Arithmetic); - explicit FloorDiv(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_FLOOR_DIV_H_ diff --git a/mindspore/lite/src/ops/floor_mod.cc b/mindspore/lite/src/ops/floor_mod.cc deleted file mode 100644 index d84de0d21d..0000000000 --- a/mindspore/lite/src/ops/floor_mod.cc +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/floor_mod.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE - -int FloorMod::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateFloorMod(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FloorMod, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *FloorModCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FloorModRegistry(schema::PrimitiveType_FloorMod, FloorModCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/floor_mod.h b/mindspore/lite/src/ops/floor_mod.h deleted file mode 100644 index ecd4a44f16..0000000000 --- a/mindspore/lite/src/ops/floor_mod.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ - -#include -#include -#include -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class FloorMod : public Arithmetic { - public: - FloorMod() = default; - ~FloorMod() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FloorMod, Arithmetic); - explicit FloorMod(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ diff --git a/mindspore/lite/src/ops/full_connection.cc b/mindspore/lite/src/ops/full_connection.cc deleted file mode 100644 index 7ec366c870..0000000000 --- a/mindspore/lite/src/ops/full_connection.cc +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/full_connection.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool FullConnection::GetHasBias() const { return this->primitive_->value.AsFullConnection()->hasBias; } -int FullConnection::GetAxis() const { return this->primitive_->value.AsFullConnection()->axis; } -bool FullConnection::GetUseAxis() const { return this->primitive_->value.AsFullConnection()->useAxis; } -int FullConnection::GetActivationType() const { return this->primitive_->value.AsFullConnection()->activationType; } - -void FullConnection::SetHasBias(bool has_bias) { this->primitive_->value.AsFullConnection()->hasBias = has_bias; } -void FullConnection::SetAxis(int axis) { this->primitive_->value.AsFullConnection()->axis = axis; } -void FullConnection::SetUseAxis(bool use_axis) { this->primitive_->value.AsFullConnection()->useAxis = use_axis; } -void FullConnection::SetActivationType(int activationType) { - this->primitive_->value.AsFullConnection()->activationType = static_cast(activationType); -} -#else -int FullConnection::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_FullConnection(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_FullConnection return nullptr"; - return RET_ERROR; - } - - auto val_offset = - schema::CreateFullConnection(*fbb, attr->hasBias(), attr->axis(), attr->useAxis(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FullConnection, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -bool FullConnection::GetHasBias() const { return this->primitive_->value_as_FullConnection()->hasBias(); } -int FullConnection::GetAxis() const { return this->primitive_->value_as_FullConnection()->axis(); } -bool FullConnection::GetUseAxis() const { return this->primitive_->value_as_FullConnection()->useAxis(); } -int FullConnection::GetActivationType() const { return this->primitive_->value_as_FullConnection()->activationType(); } - -PrimitiveC *FullConnectionCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FullConnectionRegistry(schema::PrimitiveType_FullConnection, FullConnectionCreator); -#endif - -int FullConnection::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input0 = inputs_.front(); - MS_ASSERT(input0 != nullptr); - auto input1 = inputs_.at(1); - MS_ASSERT(input1 != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if ((GetHasBias() && inputs_.size() != kTripleNum) || (!GetHasBias() && inputs_.size() != kDoubleNum)) { - MS_LOG(ERROR) << "Input tensors num error"; - return RET_INPUT_TENSOR_ERROR; - } - if (GetUseAxis() && (GetAxis() < 1 || GetAxis() > static_cast(input0->shape().size()))) { - MS_LOG(ERROR) << "FullConnection axis invalid"; - return RET_ERROR; - } - int new_k = 1; - if (GetUseAxis()) { - for (size_t i = GetAxis(); i < input0->shape().size(); ++i) { - new_k *= input0->shape().at(i); - } - if (new_k != input1->shape().at(1)) { - MS_LOG(ERROR) << "Input1 size invalid"; - return RET_INPUT_TENSOR_ERROR; - } - } else { - new_k = input1->shape().at(1); - } - if (GetHasBias()) { - if (inputs_.at(2)->shape().at(0) != input1->shape().at(0)) { - MS_LOG(ERROR) << "bias size invalid"; - return RET_INPUT_TENSOR_ERROR; - } - } - std::vector out_shape{inputs_.at(0)->shape()}; - if (GetUseAxis()) { - out_shape.resize(GetAxis() + 1); - out_shape.at(GetAxis()) = input1->shape().at(0); - } else { - int total = 1; - for (size_t i = 0; i < input0->shape().size(); ++i) { - total *= input0->shape().at(i); - } - out_shape.resize(2); - auto batch_size = total / new_k; - out_shape.at(0) = batch_size; - out_shape.at(1) = input1->shape().at(0); - } - output->set_shape(out_shape); - output->set_data_type(input0->data_type()); - output->set_format(input0->format()); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/full_connection.h b/mindspore/lite/src/ops/full_connection.h deleted file mode 100644 index 53e3ddd524..0000000000 --- a/mindspore/lite/src/ops/full_connection.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ -#define LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FullConnection : public PrimitiveC { - public: - FullConnection() = default; - ~FullConnection() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FullConnection, PrimitiveC); - explicit FullConnection(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetHasBias(bool has_bias); - void SetAxis(int axis); - void SetUseAxis(bool use_axis); - void SetActivationType(int activationType); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool GetHasBias() const; - int GetAxis() const; - bool GetUseAxis() const; - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ diff --git a/mindspore/lite/src/ops/fused_batchnorm.cc b/mindspore/lite/src/ops/fused_batchnorm.cc deleted file mode 100644 index f1c79306a5..0000000000 --- a/mindspore/lite/src/ops/fused_batchnorm.cc +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/fused_batchnorm.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float FusedBatchNorm::GetEpsilon() const { return this->primitive_->value.AsFusedBatchNorm()->epsilon; } -float FusedBatchNorm::GetMomentum() const { return this->primitive_->value.AsFusedBatchNorm()->momentum; } -int FusedBatchNorm::GetSpatial() const { return this->primitive_->value.AsFusedBatchNorm()->spatial; } - -void FusedBatchNorm::SetEpsilon(float epsilon) { this->primitive_->value.AsFusedBatchNorm()->epsilon = epsilon; } -void FusedBatchNorm::SetMomentum(float momentum) { this->primitive_->value.AsFusedBatchNorm()->momentum = momentum; } -void FusedBatchNorm::SetSpatial(int spatial) { this->primitive_->value.AsFusedBatchNorm()->spatial = spatial; } - -int FusedBatchNorm::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_FusedBatchNorm; - } - if (this->primitive_->value.type != schema::PrimitiveType_FusedBatchNorm) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::FusedBatchNormT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new attr value failed"; - return RET_ERROR; - } - attr->epsilon = GetValue(prim.GetAttr("epsilon")); - attr->momentum = GetValue(prim.GetAttr("momentum")); - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else -int FusedBatchNorm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_FusedBatchNorm(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_FusedBatchNorm return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateFusedBatchNorm(*fbb, attr->epsilon(), attr->momentum(), attr->spatial()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_FusedBatchNorm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float FusedBatchNorm::GetEpsilon() const { return this->primitive_->value_as_FusedBatchNorm()->epsilon(); } -float FusedBatchNorm::GetMomentum() const { return this->primitive_->value_as_FusedBatchNorm()->momentum(); } -int FusedBatchNorm::GetSpatial() const { return this->primitive_->value_as_FusedBatchNorm()->spatial(); } - -PrimitiveC *FusedBatchNormCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry FusedBatchNormRegistry(schema::PrimitiveType_FusedBatchNorm, FusedBatchNormCreator); -#endif - -int FusedBatchNorm::InferShape(std::vector inputs_, std::vector outputs_) { - for (size_t i = 0; i < inputs_.size(); i++) { - if (outputs_.size() <= i) { - break; - } - outputs_.at(i)->set_shape(inputs_.at(i)->shape()); - outputs_.at(i)->set_data_type(inputs_.at(i)->data_type()); - outputs_.at(i)->set_format(inputs_.at(i)->format()); - } - if (outputs_.size() > 5) { - outputs_.at(5)->set_data_type(inputs_.at(0)->data_type()); - outputs_.at(5)->set_format(inputs_.at(0)->format()); - outputs_.at(5)->set_shape({1}); - } - return 0; -} - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/fused_batchnorm.h b/mindspore/lite/src/ops/fused_batchnorm.h deleted file mode 100644 index 3da196b580..0000000000 --- a/mindspore/lite/src/ops/fused_batchnorm.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_FUSED_BATCHNORM_H_ -#define MINDSPORE_LITE_SRC_OPS_FUSED_BATCHNORM_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class FusedBatchNorm : public PrimitiveC { - public: - FusedBatchNorm() = default; - ~FusedBatchNorm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(FusedBatchNorm, PrimitiveC); - explicit FusedBatchNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetEpsilon(float epsilon); - void SetMomentum(float momentum); - void SetSpatial(int spatial); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetEpsilon() const; - float GetMomentum() const; - int GetSpatial() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_FUSED_BATCHNORM_H_ diff --git a/mindspore/lite/src/ops/gather.cc b/mindspore/lite/src/ops/gather.cc deleted file mode 100644 index 9a7c6a7645..0000000000 --- a/mindspore/lite/src/ops/gather.cc +++ /dev/null @@ -1,149 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/gather.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Gather::GetAxis() const { return this->primitive_->value.AsGather()->axis; } -int Gather::GetBatchDims() const { return this->primitive_->value.AsGather()->batchDims; } - -void Gather::SetAxis(int axis) { this->primitive_->value.AsGather()->axis = axis; } -void Gather::SetBatchDims(int batch_dims) { this->primitive_->value.AsGather()->batchDims = batch_dims; } -int Gather::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Gather; - } - if (this->primitive_->value.type != schema::PrimitiveType_Gather) { - MS_LOG(ERROR) << "Gather primitive value type : " << schema::EnumNamePrimitiveType(primitive_->value.type) - << "is not equal" << schema::EnumNamePrimitiveType(schema::PrimitiveType_Gather); - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto gather_attr = new (std::nothrow) schema::GatherT(); - if (gather_attr == nullptr) { - MS_LOG(ERROR) << "new primitive value.value error"; - delete this->primitive_; - delete gather_attr; - this->primitive_ = nullptr; - gather_attr = nullptr; - return RET_ERROR; - } - if (inputs.at(2)->isa()) { - ValueNodePtr axis_tensor = inputs.at(2)->cast(); - int axis = CastToInt(axis_tensor->value()).front(); - gather_attr->axis = axis; - } else { - MS_LOG(ERROR) << "input axis is not value node."; - delete this->primitive_; - delete gather_attr; - this->primitive_ = nullptr; - gather_attr = nullptr; - return RET_ERROR; - } - gather_attr->batchDims = 0; - this->primitive_->value.value = gather_attr; - } - return RET_OK; -} -#else -int Gather::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Gather(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Gather return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateGather(*fbb, attr->axis(), attr->batchDims()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Gather, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int Gather::GetAxis() const { return this->primitive_->value_as_Gather()->axis(); } -int Gather::GetBatchDims() const { return this->primitive_->value_as_Gather()->batchDims(); } - -PrimitiveC *GatherCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry GatherRegistry(schema::PrimitiveType_Gather, GatherCreator); -#endif - -int Gather::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() < kDoubleNum) { - MS_LOG(DEBUG) << "Gather should be at least two inputs"; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "Gather should have one outputs"; - return RET_INPUT_TENSOR_ERROR; - } - auto input = inputs_.at(0); - MS_ASSERT(input != nullptr); - auto indices = inputs_.at(1); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(input != nullptr); - output->set_data_type(input->data_type()); - if (this->quant_type() == schema::QuantType_WeightQuant) { - output->set_data_type(kNumberTypeFloat32); - } - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - int axis = GetAxis(); - int batch_dims = GetBatchDims(); - if (axis < 0) { - axis += input->shape().size(); - } - auto indices_shape = indices->shape(); - int indices_rank = indices_shape.size(); - if (batch_dims != 0) { - MS_LOG(ERROR) << "batchDims " << batch_dims << " != 0, which is not support"; - return RET_ERROR; - } - auto in_shape = input->shape(); - int in_rank = in_shape.size(); - if (in_rank < axis + 1) { - MS_LOG(ERROR) << "input[0]'s rank is less than axis + 1"; - return RET_ERROR; - } - std::vector out_shape{in_shape}; - out_shape.erase(out_shape.begin() + axis); - for (int i = indices_rank - 1; i >= 0; --i) { - out_shape.insert(out_shape.begin() + axis, indices_shape.at(i)); - } - output->set_shape(out_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/gather.h b/mindspore/lite/src/ops/gather.h deleted file mode 100644 index f7dbc2adce..0000000000 --- a/mindspore/lite/src/ops/gather.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ -#define LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Gather : public PrimitiveC { - public: - Gather() = default; - ~Gather() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Gather, PrimitiveC); - explicit Gather(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int axis); - void SetBatchDims(int batch_dims); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetAxis() const; - int GetBatchDims() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ diff --git a/mindspore/lite/src/ops/gather_nd.cc b/mindspore/lite/src/ops/gather_nd.cc deleted file mode 100644 index f420e606f8..0000000000 --- a/mindspore/lite/src/ops/gather_nd.cc +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/gather_nd.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int GatherNd::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_GatherNd; - } - if (this->primitive_->value.type != schema::PrimitiveType_GatherNd) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::GatherNdT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.GetAttr("batchDims") != nullptr) { - attr->batchDims = static_cast(GetValue(prim.GetAttr("batchDims"))); - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int GatherNd::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_GatherNd(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_GatherNd return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateGatherNd(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_GatherNd, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *GatherNdCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry GatherNdRegistry(schema::PrimitiveType_GatherNd, GatherNdCreator); -#endif - -int GatherNd::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() != kDoubleNum) { - MS_LOG(ERROR) << "GatherNd should have two inputs"; - return RET_INPUT_TENSOR_ERROR; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "GatherNd should have one outputs"; - return RET_INPUT_TENSOR_ERROR; - } - auto input = inputs_.at(0); - MS_ASSERT(input != nullptr); - auto indices = inputs_.at(1); - MS_ASSERT(indices != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto in_shape = input->shape(); - int in_rank = in_shape.size(); - auto indices_shape = indices->shape(); - int indices_rank = indices_shape.size(); - if (indices_shape.at(indices_rank - 1) > in_rank) { - MS_LOG(ERROR) << "Input of indices data is error!"; - return RET_ERROR; - } - std::vector out_shape; - int i = 0; - for (i = 0; i < indices_rank - 1; ++i) { - out_shape.emplace_back(indices_shape.at(i)); - } - for (i = indices_shape.at(indices_rank - 1); i < in_rank; ++i) { - out_shape.emplace_back(in_shape.at(i)); - } - output->set_shape(out_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/gather_nd.h b/mindspore/lite/src/ops/gather_nd.h deleted file mode 100644 index 7733050c53..0000000000 --- a/mindspore/lite/src/ops/gather_nd.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ -#define LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class GatherNd : public PrimitiveC { - public: - GatherNd() = default; - ~GatherNd() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(GatherNd, PrimitiveC); - explicit GatherNd(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ diff --git a/mindspore/lite/src/ops/gelu.cc b/mindspore/lite/src/ops/gelu.cc deleted file mode 100644 index 234f8e7454..0000000000 --- a/mindspore/lite/src/ops/gelu.cc +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/gelu.h" -#include -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int GeLU::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_GeLU; - } - if (this->primitive_->value.type != schema::PrimitiveType_GeLU) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::GeLUT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/gelu.h b/mindspore/lite/src/ops/gelu.h deleted file mode 100644 index d2fc914a75..0000000000 --- a/mindspore/lite/src/ops/gelu.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_GELU_H_ -#define LITE_MINDSPORE_LITE_C_OPS_GELU_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class GeLU : public PrimitiveC { - public: - GeLU() = default; - ~GeLU() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(GeLU, PrimitiveC); - explicit GeLU(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_GELU_H_ diff --git a/mindspore/lite/src/ops/greater.cc b/mindspore/lite/src/ops/greater.cc deleted file mode 100644 index a90926b5be..0000000000 --- a/mindspore/lite/src/ops/greater.cc +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/greater.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Greater::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Greater; - } - if (this->primitive_->value.type != schema::PrimitiveType_Greater) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::GreaterT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Greater::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateGreater(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Greater, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *GreaterCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry GreaterRegistry(schema::PrimitiveType_Greater, GreaterCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/greater.h b/mindspore/lite/src/ops/greater.h deleted file mode 100644 index ae7ef82b51..0000000000 --- a/mindspore/lite/src/ops/greater.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ -#define LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class Greater : public ArithmeticCompare { - public: - Greater() = default; - ~Greater() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Greater, ArithmeticCompare); - explicit Greater(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ diff --git a/mindspore/lite/src/ops/greater_equal.cc b/mindspore/lite/src/ops/greater_equal.cc deleted file mode 100644 index e7dd799802..0000000000 --- a/mindspore/lite/src/ops/greater_equal.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/greater_equal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int GreaterEqual::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateGreaterEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_GreaterEqual, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *GreaterEqualCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry GreaterEqualRegistry(schema::PrimitiveType_GreaterEqual, GreaterEqualCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/greater_equal.h b/mindspore/lite/src/ops/greater_equal.h deleted file mode 100644 index f8df62e2fa..0000000000 --- a/mindspore/lite/src/ops/greater_equal.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class GreaterEqual : public ArithmeticCompare { - public: - GreaterEqual() = default; - ~GreaterEqual() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(GreaterEqual, ArithmeticCompare); - explicit GreaterEqual(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ diff --git a/mindspore/lite/src/ops/group_conv2d_grad_input.cc b/mindspore/lite/src/ops/group_conv2d_grad_input.cc deleted file mode 100644 index 7858392340..0000000000 --- a/mindspore/lite/src/ops/group_conv2d_grad_input.cc +++ /dev/null @@ -1,172 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/group_conv2d_grad_input.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int GroupConv2DGradInput::GetFormat() const { return this->primitive_->value.AsGroupConv2DGradInput()->format; } -int GroupConv2DGradInput::GetGroup() const { return this->primitive_->value.AsGroupConv2DGradInput()->group; } -int GroupConv2DGradInput::GetChannelIn() const { return this->primitive_->value.AsGroupConv2DGradInput()->channelIn; } -int GroupConv2DGradInput::GetChannelOut() const { return this->primitive_->value.AsGroupConv2DGradInput()->channelOut; } -int GroupConv2DGradInput::GetKernelW() const { return this->primitive_->value.AsGroupConv2DGradInput()->kernelW; } -int GroupConv2DGradInput::GetKernelH() const { return this->primitive_->value.AsGroupConv2DGradInput()->kernelH; } -int GroupConv2DGradInput::GetStrideW() const { return this->primitive_->value.AsGroupConv2DGradInput()->strideW; } -int GroupConv2DGradInput::GetStrideH() const { return this->primitive_->value.AsGroupConv2DGradInput()->strideH; } -int GroupConv2DGradInput::GetPadMode() const { return this->primitive_->value.AsGroupConv2DGradInput()->padMode; } -int GroupConv2DGradInput::GetPadUp() const { return this->primitive_->value.AsGroupConv2DGradInput()->padUp; } -int GroupConv2DGradInput::GetPadDown() const { return this->primitive_->value.AsGroupConv2DGradInput()->padDown; } -int GroupConv2DGradInput::GetPadLeft() const { return this->primitive_->value.AsGroupConv2DGradInput()->padLeft; } -int GroupConv2DGradInput::GetPadRight() const { return this->primitive_->value.AsGroupConv2DGradInput()->padRight; } -int GroupConv2DGradInput::GetDilateW() const { return this->primitive_->value.AsGroupConv2DGradInput()->dilateW; } -int GroupConv2DGradInput::GetDilateH() const { return this->primitive_->value.AsGroupConv2DGradInput()->dilateH; } -std::vector GroupConv2DGradInput::GetInputShape() const { - return this->primitive_->value.AsGroupConv2DGradInput()->input_shape; -} -int GroupConv2DGradInput::GetActivationType() const { - return this->primitive_->value.AsGroupConv2DGradInput()->activationType; -} - -void GroupConv2DGradInput::SetFormat(int format) { - this->primitive_->value.AsGroupConv2DGradInput()->format = (schema::Format)format; -} -void GroupConv2DGradInput::SetGroup(int group) { this->primitive_->value.AsGroupConv2DGradInput()->group = group; } -void GroupConv2DGradInput::SetChannelIn(int channel_in) { - this->primitive_->value.AsGroupConv2DGradInput()->channelIn = channel_in; -} -void GroupConv2DGradInput::SetChannelOut(int channel_out) { - this->primitive_->value.AsGroupConv2DGradInput()->channelOut = channel_out; -} -void GroupConv2DGradInput::SetKernelW(int kernel_w) { - this->primitive_->value.AsGroupConv2DGradInput()->kernelW = kernel_w; -} -void GroupConv2DGradInput::SetKernelH(int kernel_h) { - this->primitive_->value.AsGroupConv2DGradInput()->kernelH = kernel_h; -} -void GroupConv2DGradInput::SetStrideW(int stride_w) { - this->primitive_->value.AsGroupConv2DGradInput()->strideW = stride_w; -} -void GroupConv2DGradInput::SetStrideH(int stride_h) { - this->primitive_->value.AsGroupConv2DGradInput()->strideH = stride_h; -} -void GroupConv2DGradInput::SetPadMode(int pad_mode) { - this->primitive_->value.AsGroupConv2DGradInput()->padMode = (schema::PadMode)pad_mode; -} -void GroupConv2DGradInput::SetPadUp(int pad_up) { this->primitive_->value.AsGroupConv2DGradInput()->padUp = pad_up; } -void GroupConv2DGradInput::SetPadDown(int pad_down) { - this->primitive_->value.AsGroupConv2DGradInput()->padDown = pad_down; -} -void GroupConv2DGradInput::SetPadLeft(int pad_left) { - this->primitive_->value.AsGroupConv2DGradInput()->padLeft = pad_left; -} -void GroupConv2DGradInput::SetPadRight(int pad_right) { - this->primitive_->value.AsGroupConv2DGradInput()->padRight = pad_right; -} -void GroupConv2DGradInput::SetDilateW(int dilate_w) { - this->primitive_->value.AsGroupConv2DGradInput()->dilateW = dilate_w; -} -void GroupConv2DGradInput::SetDilateH(int dilate_h) { - this->primitive_->value.AsGroupConv2DGradInput()->dilateH = dilate_h; -} -void GroupConv2DGradInput::SetActivationType(int activation_type) { - this->primitive_->value.AsGroupConv2DGradInput()->activationType = (schema::ActivationType)activation_type; -} -#else -int GroupConv2DGradInput::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_GroupConv2DGradInput(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_GroupConv2DGradInput return nullptr"; - return RET_ERROR; - } - std::vector input_shape; - if (attr->input_shape() != nullptr) { - for (int i = 0; i < static_cast(attr->input_shape()->size()); i++) { - input_shape.push_back(attr->input_shape()->data()[i]); - } - } - auto val_offset = schema::CreateGroupConv2DGradInputDirect( - *fbb, attr->format(), attr->group(), attr->channelIn(), attr->channelOut(), attr->kernelW(), attr->kernelH(), - attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), attr->padDown(), attr->padLeft(), - attr->padRight(), attr->dilateW(), attr->dilateH(), attr->hasBias(), &input_shape, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_GroupConv2DGradInput, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -int GroupConv2DGradInput::GetFormat() const { return this->primitive_->value_as_GroupConv2DGradInput()->format(); } -int GroupConv2DGradInput::GetGroup() const { return this->primitive_->value_as_GroupConv2DGradInput()->group(); } -int GroupConv2DGradInput::GetChannelIn() const { - return this->primitive_->value_as_GroupConv2DGradInput()->channelIn(); -} -int GroupConv2DGradInput::GetChannelOut() const { - return this->primitive_->value_as_GroupConv2DGradInput()->channelOut(); -} -int GroupConv2DGradInput::GetKernelW() const { return this->primitive_->value_as_GroupConv2DGradInput()->kernelW(); } -int GroupConv2DGradInput::GetKernelH() const { return this->primitive_->value_as_GroupConv2DGradInput()->kernelH(); } -int GroupConv2DGradInput::GetStrideW() const { return this->primitive_->value_as_GroupConv2DGradInput()->strideW(); } -int GroupConv2DGradInput::GetStrideH() const { return this->primitive_->value_as_GroupConv2DGradInput()->strideH(); } -int GroupConv2DGradInput::GetPadMode() const { return this->primitive_->value_as_GroupConv2DGradInput()->padMode(); } -int GroupConv2DGradInput::GetPadUp() const { return this->primitive_->value_as_GroupConv2DGradInput()->padUp(); } -int GroupConv2DGradInput::GetPadDown() const { return this->primitive_->value_as_GroupConv2DGradInput()->padDown(); } -int GroupConv2DGradInput::GetPadLeft() const { return this->primitive_->value_as_GroupConv2DGradInput()->padLeft(); } -int GroupConv2DGradInput::GetPadRight() const { return this->primitive_->value_as_GroupConv2DGradInput()->padRight(); } -int GroupConv2DGradInput::GetDilateW() const { return this->primitive_->value_as_GroupConv2DGradInput()->dilateW(); } -int GroupConv2DGradInput::GetDilateH() const { return this->primitive_->value_as_GroupConv2DGradInput()->dilateH(); } -std::vector GroupConv2DGradInput::GetInputShape() const { - auto fb_vector = this->primitive_->value_as_GroupConv2DGradInput()->input_shape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int GroupConv2DGradInput::GetActivationType() const { - return this->primitive_->value_as_GroupConv2DGradInput()->activationType(); -} -PrimitiveC *GroupConv2DGradInputCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry GroupConv2DGradInputRegistry(schema::PrimitiveType_GroupConv2DGradInput, GroupConv2DGradInputCreator); - -#endif - -int GroupConv2DGradInput::InferShape(std::vector inputs, std::vector outputs) { - if (inputs.size() < 2) { - MS_LOG(ERROR) << "Conv2d Grad input should be at least two input"; - return RET_ERROR; - } - if (1 != outputs.size()) { - MS_LOG(ERROR) << "Conv2d Grad output should have one output"; - return RET_ERROR; - } - - auto *in0 = inputs.at(0); - - MS_ASSERT(in0 != nullptr); - - auto *out = outputs.at(0); - MS_ASSERT(out != nullptr); - out->set_shape(GetInputShape()); - - out->set_data_type(in0->data_type()); - out->set_format(in0->format()); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/group_conv2d_grad_input.h b/mindspore/lite/src/ops/group_conv2d_grad_input.h deleted file mode 100644 index 8581abdfcb..0000000000 --- a/mindspore/lite/src/ops/group_conv2d_grad_input.h +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_GROUP_CONV2D_GRAD_INPUT_H_ -#define MINDSPORE_LITE_SRC_OPS_GROUP_CONV2D_GRAD_INPUT_H_ - -#include -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class GroupConv2DGradInput : public PrimitiveC { - public: - GroupConv2DGradInput() = default; - ~GroupConv2DGradInput() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(GroupConv2DGradInput, PrimitiveC); - explicit GroupConv2DGradInput(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetGroup(int group); - void SetChannelIn(int channel_in); - void SetChannelOut(int channel_out); - void SetKernelW(int kernel_w); - void SetKernelH(int kernel_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetDilateW(int dilate_w); - void SetDilateH(int dilate_h); - void SetActivationType(int activation_type); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetGroup() const; - int GetChannelIn() const; - int GetChannelOut() const; - int GetKernelW() const; - int GetKernelH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetDilateW() const; - int GetDilateH() const; - std::vector GetInputShape() const; - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_GROUP_CONV2D_GRAD_INPUT_H_ diff --git a/mindspore/lite/src/ops/gru.cc b/mindspore/lite/src/ops/gru.cc deleted file mode 100644 index 40ae70335d..0000000000 --- a/mindspore/lite/src/ops/gru.cc +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/gru.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool Gru::GetBidirection() const { return this->primitive_->value.AsGru()->bidirection; } - -void Gru::SetBidirection(bool bidirection) { this->primitive_->value.AsGru()->bidirection = bidirection; } - -#else - -bool Gru::GetBidirection() const { return this->primitive_->value_as_Gru()->bidirection(); } -int Gru::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Gru(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Gru return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateGru(*fbb, attr->bidirection()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Gru, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *GruCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry GruRegistry(schema::PrimitiveType_Gru, GruCreator); -#endif - -const int kGruInputNum = 5; -const int kGruInputWithSeqLenNum = 6; -const int kGruOutputNum = 2; -int Gru::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if ((inputs_.size() != kGruInputNum && inputs_.size() != kGruInputWithSeqLenNum) || - outputs_.size() != kGruOutputNum) { - MS_LOG(ERROR) << "OpGru inputs or outputs size error."; - return RET_INPUT_TENSOR_ERROR; - } - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto weight_gate = inputs_.at(1); - MS_ASSERT(weight_gate != nullptr); - auto weight_recurrence = inputs_.at(2); - MS_ASSERT(weight_recurrence != nullptr); - auto bias = inputs_.at(3); - MS_ASSERT(bias != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - for (int i = 0; i < kGruOutputNum; i++) { - outputs_.at(i)->set_data_type(input->data_type()); - outputs_.at(i)->set_format(input->format()); - } - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto in_shape = input->shape(); // seq_len, batch, input_size - auto w_gate_shape = weight_gate->shape(); // num_direction, hidden_size * 3, input_size - auto w_recu_shape = weight_recurrence->shape(); // num_direction, hidden_size * 3, hidden_size - auto bias_shape = bias->shape(); // num_direction, hidden_size * 6 - if (in_shape.size() != 3 || w_gate_shape.size() != 3 || w_recu_shape.size() != 3) { - MS_LOG(ERROR) << "OpGru input dims should be 3."; - return RET_ERROR; - } - if (w_gate_shape[1] != w_recu_shape[1] || w_recu_shape[1] * 2 != bias_shape[1]) { - MS_LOG(ERROR) << "OpGru w_gate, w_recu and bias hidden size not match."; - return RET_ERROR; - } - if (inputs_.size() == kGruInputWithSeqLenNum) { - auto seq_len_shape = inputs_.at(5)->shape(); - if (seq_len_shape[0] > 1) { - MS_LOG(WARNING) << "OpGru with batch_size > 1 only support all same sequence_len now."; - return RET_ERROR; - } - if (seq_len_shape.size() != 1 && seq_len_shape[0] != in_shape[1]) { - MS_LOG(ERROR) << "OpGru sequence_len shape[0] and batch_size not match."; - return RET_ERROR; - } - } - - int hidden_size = w_gate_shape[1] / 3; - // set output - std::vector out_shape(in_shape); - out_shape[2] = hidden_size; - if (GetBidirection()) { - out_shape.insert(out_shape.begin() + 1, 2); - } else { - out_shape.insert(out_shape.begin() + 1, 1); - } - output->set_shape(out_shape); - // set hidden state - std::vector state_shape(in_shape); - state_shape[0] = GetBidirection() ? 2 : 1; - state_shape[2] = hidden_size; - outputs_[1]->set_shape(state_shape); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/gru.h b/mindspore/lite/src/ops/gru.h deleted file mode 100644 index 84ca28fb9b..0000000000 --- a/mindspore/lite/src/ops/gru.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef MINDSPORE_LITE_SRC_OPS_GRU_H_ -#define MINDSPORE_LITE_SRC_OPS_GRU_H_ -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -/* - * gru with linear_before_reset = 0 - */ -class Gru : public PrimitiveC { - public: - Gru() = default; - ~Gru() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Gru, PrimitiveC); - explicit Gru(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetBidirection(bool bidirection); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool GetBidirection() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_GRU_H_ diff --git a/mindspore/lite/src/ops/hashtable_lookup.cc b/mindspore/lite/src/ops/hashtable_lookup.cc deleted file mode 100644 index d479bdddb0..0000000000 --- a/mindspore/lite/src/ops/hashtable_lookup.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/hashtable_lookup.h" - -#include "src/common/string_util.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int HashtableLookup::UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_OK; } -#else -int HashtableLookup::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateHashtableLookup(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_HashtableLookup, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *HashtableLookupCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry HashtableLookupRegistry(schema::PrimitiveType_HashtableLookup, HashtableLookupCreator); -#endif - -int HashtableLookup::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.at(0); - auto values = inputs_.at(2); - auto output = outputs_.at(0); - auto hits = outputs_.at(1); - MS_ASSERT(input != nullptr); - MS_ASSERT(values != nullptr); - MS_ASSERT(output != nullptr); - MS_ASSERT(hits != nullptr); - - std::vector hits_shape; - hits_shape.push_back(input->DimensionSize(0)); - - output->set_data_type(values->data_type()); - output->set_format(input->format()); - hits->set_shape(hits_shape); - hits->set_data_type(kNumberTypeUInt8); - hits->set_format(input->format()); - - if (input->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/hashtable_lookup.h b/mindspore/lite/src/ops/hashtable_lookup.h deleted file mode 100644 index fd8fa86be3..0000000000 --- a/mindspore/lite/src/ops/hashtable_lookup.h +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_HASHTABLE_LOOKUP_H_ -#define LITE_MINDSPORE_LITE_C_OPS_HASHTABLE_LOOKUP_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class HashtableLookup : public PrimitiveC { - public: - HashtableLookup() = default; - ~HashtableLookup() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(HashtableLookup, PrimitiveC); - explicit HashtableLookup(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_HASHTABLE_LOOKUP_H_ diff --git a/mindspore/lite/src/ops/identity.h b/mindspore/lite/src/ops/identity.h deleted file mode 100644 index a66091e1b7..0000000000 --- a/mindspore/lite/src/ops/identity.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" - -#ifndef LITE_MINDSPORE_LITE_C_OPS_IDENTITY_H_ -#define LITE_MINDSPORE_LITE_C_OPS_IDENTITY_H_ - -namespace mindspore { -namespace lite { -class Identity : public PrimitiveC { - public: - MS_DECLARE_PARENT(Identity, PrimitiveC); - Identity() = default; - ~Identity() = default; - explicit Identity(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_IDENTITY_H_ diff --git a/mindspore/lite/src/ops/if.h b/mindspore/lite/src/ops/if.h deleted file mode 100644 index 6c5f0a730f..0000000000 --- a/mindspore/lite/src/ops/if.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" - -#ifndef LITE_MINDSPORE_LITE_C_OPS_IF_H_ -#define LITE_MINDSPORE_LITE_C_OPS_IF_H_ - -namespace mindspore { -namespace lite { -class If : public PrimitiveC { - public: - MS_DECLARE_PARENT(If, PrimitiveC); - If() = default; - ~If() = default; - explicit If(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_IF_H_ diff --git a/mindspore/lite/src/ops/instance_norm.cc b/mindspore/lite/src/ops/instance_norm.cc deleted file mode 100644 index 62d8cfc65e..0000000000 --- a/mindspore/lite/src/ops/instance_norm.cc +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/instance_norm.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float InstanceNorm::GetEpsilon() const { return this->primitive_->value.AsInstanceNorm()->epsilon; } - -void InstanceNorm::SetEpsilon(float epsilon) { this->primitive_->value.AsInstanceNorm()->epsilon = epsilon; } - -int InstanceNorm::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_InstanceNorm; - } - if (this->primitive_->value.type != schema::PrimitiveType_InstanceNorm) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::InstanceNormT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new InstanceNormT failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - attr->epsilon = GetValue(prim.GetAttr("epsilon")); - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else -int InstanceNorm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateInstanceNorm(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_InstanceNorm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -float InstanceNorm::GetEpsilon() const { return this->primitive_->value_as_InstanceNorm()->epsilon(); } - -PrimitiveC *InstanceNormCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry InstanceNormRegistry(schema::PrimitiveType_InstanceNorm, InstanceNormCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/instance_norm.h b/mindspore/lite/src/ops/instance_norm.h deleted file mode 100644 index 7f74fc0da2..0000000000 --- a/mindspore/lite/src/ops/instance_norm.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_INSTANE_NORM_H_ -#define LITE_MINDSPORE_LITE_C_OPS_INSTANE_NORM_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class InstanceNorm : public PrimitiveC { - public: - InstanceNorm() = default; - ~InstanceNorm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(InstanceNorm, PrimitiveC); - explicit InstanceNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetEpsilon(float epsilon); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetEpsilon() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_INSTANE_NORM_H_ diff --git a/mindspore/lite/src/ops/invert_permutation.cc b/mindspore/lite/src/ops/invert_permutation.cc deleted file mode 100644 index 3d7479988d..0000000000 --- a/mindspore/lite/src/ops/invert_permutation.cc +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/invert_permutation.h" -#include "src/common/common.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -#ifdef PRIMITIVE_WRITEABLE -#else -int InvertPermutation::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateSize(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_InvertPermutation, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *InvertPermutationCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry InvertPermutationRegistry(schema::PrimitiveType_InvertPermutation, InvertPermutationCreator); -#endif - -int InvertPermutation::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if (input->data_type() != kNumberTypeInt32) { - MS_LOG(ERROR) << "InvertPermutation does not support input of data type: " << input->data_type(); - return RET_ERROR; - } - if (input->shape().size() != 1) { - MS_LOG(ERROR) << "InvertPermutation input must be one-dimensional."; - return RET_ERROR; - } - output->set_shape(input->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/invert_permutation.h b/mindspore/lite/src/ops/invert_permutation.h deleted file mode 100644 index a79f4814a4..0000000000 --- a/mindspore/lite/src/ops/invert_permutation.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_INVERTPERMUTATION_H_ -#define LITE_MINDSPORE_LITE_C_OPS_INVERTPERMUTATION_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class InvertPermutation : public PrimitiveC { - public: - InvertPermutation() = default; - ~InvertPermutation() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(InvertPermutation, PrimitiveC); - explicit InvertPermutation(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_INVERTPERMUTATION_H_ diff --git a/mindspore/lite/src/ops/is_finite.h b/mindspore/lite/src/ops/is_finite.h deleted file mode 100644 index 9d18ebc757..0000000000 --- a/mindspore/lite/src/ops/is_finite.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" - -#ifndef LITE_MINDSPORE_LITE_C_OPS_IS_FINITE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_IS_FINITE_H_ - -namespace mindspore { -namespace lite { -class IsFinite : public PrimitiveC { - public: - MS_DECLARE_PARENT(IsFinite, PrimitiveC); - IsFinite() = default; - ~IsFinite() = default; - explicit IsFinite(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_IS_FINITE_H_ diff --git a/mindspore/lite/src/ops/l2_norm.cc b/mindspore/lite/src/ops/l2_norm.cc deleted file mode 100644 index 3edaec93e6..0000000000 --- a/mindspore/lite/src/ops/l2_norm.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/l2_norm.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector L2Norm::GetAxis() const { return this->primitive_->value.AsL2Norm()->axis; } -float L2Norm::GetEpsilon() const { return this->primitive_->value.AsL2Norm()->epsilon; } -int L2Norm::GetActivationType() const { return this->primitive_->value.AsL2Norm()->activationType; } - -void L2Norm::SetAxis(const std::vector &axis) { this->primitive_->value.AsL2Norm()->axis = axis; } -void L2Norm::SetEpsilon(float epsilon) { this->primitive_->value.AsL2Norm()->epsilon = epsilon; } -void L2Norm::SetActivationType(int activationType) { - this->primitive_->value.AsL2Norm()->activationType = (schema::ActivationType)activationType; -} - -#else -int L2Norm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_L2Norm(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_L2Norm return nullptr"; - return RET_ERROR; - } - - std::vector axis; - if (attr->axis() != nullptr) { - for (int i = 0; i < static_cast(attr->axis()->size()); i++) { - axis.push_back(attr->axis()->data()[i]); - } - } - auto val_offset = schema::CreateL2NormDirect(*fbb, &axis, attr->epsilon()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_L2Norm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -std::vector L2Norm::GetAxis() const { - auto fb_vector = this->primitive_->value_as_L2Norm()->axis(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -float L2Norm::GetEpsilon() const { return this->primitive_->value_as_L2Norm()->epsilon(); } -int L2Norm::GetActivationType() const { return this->primitive_->value_as_L2Norm()->activationType(); } - -PrimitiveC *L2NormCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry L2NormRegistry(schema::PrimitiveType_L2Norm, L2NormCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/l2_norm.h b/mindspore/lite/src/ops/l2_norm.h deleted file mode 100644 index e4e0aefb25..0000000000 --- a/mindspore/lite/src/ops/l2_norm.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ -#define LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class L2Norm : public PrimitiveC { - public: - L2Norm() = default; - ~L2Norm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(L2Norm, PrimitiveC); - explicit L2Norm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(const std::vector &axis); - void SetEpsilon(float epsilon); - void SetActivationType(int activationType); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - std::vector GetAxis() const; - float GetEpsilon() const; - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ diff --git a/mindspore/lite/src/ops/layer_norm.cc b/mindspore/lite/src/ops/layer_norm.cc deleted file mode 100644 index 1a531d73df..0000000000 --- a/mindspore/lite/src/ops/layer_norm.cc +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/layer_norm.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float LayerNorm::GetEpsilon() const { return this->primitive_->value.AsLayerNorm()->epsilon; } -int LayerNorm::GetBeginNormAxis() const { return this->primitive_->value.AsLayerNorm()->begin_norm_axis; } -int LayerNorm::GetBeginParamsAxis() const { return this->primitive_->value.AsLayerNorm()->begin_params_axis; } - -void LayerNorm::SetEpsilon(float epsilon) { this->primitive_->value.AsLayerNorm()->epsilon = epsilon; } -void LayerNorm::SetBeginNormAxis(int axis) { this->primitive_->value.AsLayerNorm()->begin_norm_axis = axis; } -void LayerNorm::SetBeginParamsAxis(int axis) { this->primitive_->value.AsLayerNorm()->begin_params_axis = axis; } - -int LayerNorm::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_LayerNorm; - } - if (this->primitive_->value.type != schema::PrimitiveType_LayerNorm) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto layer_norm_attr = new (std::nothrow) schema::LayerNormT(); - if (layer_norm_attr == nullptr) { - MS_LOG(ERROR) << "new primitive value.value error"; - return RET_ERROR; - } - auto value_attr = prim.GetAttr("epsilon"); - if (value_attr != nullptr) { - layer_norm_attr->epsilon = GetValue(value_attr); - } else { - layer_norm_attr->epsilon = 1e-7; - } - auto norm_axis_attr = prim.GetAttr("begin_norm_axis"); - if (norm_axis_attr != nullptr) { - layer_norm_attr->begin_norm_axis = GetValue(norm_axis_attr); - } else { - layer_norm_attr->begin_norm_axis = -1; - } - auto params_axis_attr = prim.GetAttr("begin_params_axis"); - if (params_axis_attr != nullptr) { - layer_norm_attr->begin_params_axis = GetValue(params_axis_attr); - } else { - layer_norm_attr->begin_params_axis = -1; - } - this->primitive_->value.value = layer_norm_attr; - } - return RET_OK; -} -#else -int LayerNorm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_LayerNorm(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_LayerNorm return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateLayerNorm(*fbb, attr->begin_norm_axis(), attr->begin_params_axis(), attr->epsilon()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LayerNorm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -float LayerNorm::GetEpsilon() const { return this->primitive_->value_as_LayerNorm()->epsilon(); } -int LayerNorm::GetBeginNormAxis() const { return this->primitive_->value_as_LayerNorm()->begin_norm_axis(); } -int LayerNorm::GetBeginParamsAxis() const { return this->primitive_->value_as_LayerNorm()->begin_params_axis(); } - -PrimitiveC *LayerNormCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LayerNormRegistry(schema::PrimitiveType_LayerNorm, LayerNormCreator); -#endif -int LayerNorm::InferShape(std::vector inputs_, std::vector outputs_) { - if (outputs_.size() != kSingleNum || (inputs_.size() != kSingleNum && inputs_.size() != kTripleNum)) { - MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs_.size() << ",input size: " << inputs_.size(); - return RET_PARAM_INVALID; - } - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.at(0); - MS_ASSERT(output != nullptr); - output->set_format(input->format()); - output->set_data_type(input->data_type()); - - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - for (size_t i = GetBeginNormAxis(); i < input_shape.size(); i++) { - normlized_shape_.push_back(input_shape[i]); - } - output->set_shape(input_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/layer_norm.h b/mindspore/lite/src/ops/layer_norm.h deleted file mode 100644 index 6307c8bb8d..0000000000 --- a/mindspore/lite/src/ops/layer_norm.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef MINDSPORE_LITE_SRC_OPS_LAYER_NORM_H_ -#define MINDSPORE_LITE_SRC_OPS_LAYER_NORM_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class LayerNorm : public PrimitiveC { - public: - LayerNorm() = default; - ~LayerNorm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LayerNorm, PrimitiveC); - explicit LayerNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetEpsilon(float epsilon); - void SetBeginNormAxis(int axis); - void SetBeginParamsAxis(int axis); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetEpsilon() const; - int GetBeginNormAxis() const; - int GetBeginParamsAxis() const; - std::vector GetNormlizedShape() const { return normlized_shape_; } - - protected: - std::vector normlized_shape_; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_LAYER_NORM_H_ diff --git a/mindspore/lite/src/ops/leaky_relu.cc b/mindspore/lite/src/ops/leaky_relu.cc deleted file mode 100644 index e39858c8e1..0000000000 --- a/mindspore/lite/src/ops/leaky_relu.cc +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/leaky_relu.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float LeakyReLU::GetNegativeSlope() const { return this->primitive_->value.AsLeakyReLU()->negativeSlope; } - -void LeakyReLU::SetNegativeSlope(float negative_slope) { - this->primitive_->value.AsLeakyReLU()->negativeSlope = negative_slope; -} - -#else - -float LeakyReLU::GetNegativeSlope() const { return this->primitive_->value_as_LeakyReLU()->negativeSlope(); } - -int LeakyReLU::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_LeakyReLU(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_LeakyReLU return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateLeakyReLU(*fbb, attr->negativeSlope()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LeakyReLU, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *LeakyReLUCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LeakyReLURegistry(schema::PrimitiveType_LeakyReLU, LeakyReLUCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/leaky_relu.h b/mindspore/lite/src/ops/leaky_relu.h deleted file mode 100644 index 64922f1afa..0000000000 --- a/mindspore/lite/src/ops/leaky_relu.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class LeakyReLU : public PrimitiveC { - public: - LeakyReLU() = default; - ~LeakyReLU() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LeakyReLU, PrimitiveC); - explicit LeakyReLU(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetNegativeSlope(float negative_slope); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetNegativeSlope() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ diff --git a/mindspore/lite/src/ops/less.cc b/mindspore/lite/src/ops/less.cc deleted file mode 100644 index fe4d82ee76..0000000000 --- a/mindspore/lite/src/ops/less.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/less.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int Less::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto val_offset = schema::CreateLess(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Less, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LessCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry LessRegistry(schema::PrimitiveType_Less, LessCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/less.h b/mindspore/lite/src/ops/less.h deleted file mode 100644 index 2967cfd70a..0000000000 --- a/mindspore/lite/src/ops/less.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LESS_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LESS_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class Less : public ArithmeticCompare { - public: - Less() = default; - ~Less() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Less, ArithmeticCompare); - explicit Less(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LESS_H_ diff --git a/mindspore/lite/src/ops/less_equal.cc b/mindspore/lite/src/ops/less_equal.cc deleted file mode 100644 index 89a88fc6c7..0000000000 --- a/mindspore/lite/src/ops/less_equal.cc +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/less_equal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int LessEqual::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLessEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LessEqual, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *LessEqualCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LessEqualRegistry(schema::PrimitiveType_LessEqual, LessEqualCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/less_equal.h b/mindspore/lite/src/ops/less_equal.h deleted file mode 100644 index ade4d12c4c..0000000000 --- a/mindspore/lite/src/ops/less_equal.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class LessEqual : public ArithmeticCompare { - public: - LessEqual() = default; - ~LessEqual() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LessEqual, ArithmeticCompare); - explicit LessEqual(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ diff --git a/mindspore/lite/src/ops/lin_space.cc b/mindspore/lite/src/ops/lin_space.cc deleted file mode 100644 index 5a1ac650fb..0000000000 --- a/mindspore/lite/src/ops/lin_space.cc +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/lin_space.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int LinSpace::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLinSpace(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LinSpace, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *LinSpaceCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LinSpaceRegistry(schema::PrimitiveType_LinSpace, LinSpaceCreator); -#endif -int LinSpace::InferShape(std::vector inputs, std::vector outputs) { - auto input = inputs.front(); - MS_ASSERT(input != nullptr); - auto output = outputs.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - auto num = inputs.at(2)->data_c(); - if (num == nullptr) { - return RET_INFER_INVALID; - } - output->set_shape({reinterpret_cast(num)[0]}); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/lin_space.h b/mindspore/lite/src/ops/lin_space.h deleted file mode 100644 index d22f959922..0000000000 --- a/mindspore/lite/src/ops/lin_space.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 -#include -#include - -#include "src/ops/primitive_c.h" - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LIN_SPACE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LIN_SPACE_H_ - -namespace mindspore { -namespace lite { -class LinSpace : public PrimitiveC { - public: - LinSpace() = default; - ~LinSpace() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LinSpace, PrimitiveC); - explicit LinSpace(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs, std::vector outputs) override; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_LIN_SPACE_H_ diff --git a/mindspore/lite/src/ops/local_response_normalization.cc b/mindspore/lite/src/ops/local_response_normalization.cc deleted file mode 100644 index d3df71d4a6..0000000000 --- a/mindspore/lite/src/ops/local_response_normalization.cc +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/local_response_normalization.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int LocalResponseNormalization::GetDepthRadius() const { - return this->primitive_->value.AsLocalResponseNormalization()->depth_radius; -} -float LocalResponseNormalization::GetBias() const { - return this->primitive_->value.AsLocalResponseNormalization()->bias; -} -float LocalResponseNormalization::GetAlpha() const { - return this->primitive_->value.AsLocalResponseNormalization()->alpha; -} -float LocalResponseNormalization::GetBeta() const { - return this->primitive_->value.AsLocalResponseNormalization()->beta; -} - -void LocalResponseNormalization::SetDepthRadius(int depth_radius) { - this->primitive_->value.AsLocalResponseNormalization()->depth_radius = depth_radius; -} -void LocalResponseNormalization::SetBias(float bias) { - this->primitive_->value.AsLocalResponseNormalization()->bias = bias; -} -void LocalResponseNormalization::SetAlpha(float alpha) { - this->primitive_->value.AsLocalResponseNormalization()->alpha = alpha; -} -void LocalResponseNormalization::SetBeta(float beta) { - this->primitive_->value.AsLocalResponseNormalization()->beta = beta; -} - -#else - -int LocalResponseNormalization::GetDepthRadius() const { - return this->primitive_->value_as_LocalResponseNormalization()->depth_radius(); -} -float LocalResponseNormalization::GetBias() const { - return this->primitive_->value_as_LocalResponseNormalization()->bias(); -} -float LocalResponseNormalization::GetAlpha() const { - return this->primitive_->value_as_LocalResponseNormalization()->alpha(); -} -float LocalResponseNormalization::GetBeta() const { - return this->primitive_->value_as_LocalResponseNormalization()->beta(); -} - -int LocalResponseNormalization::UnPackToFlatBuilder(const schema::Primitive *primitive, - flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_LocalResponseNormalization(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_LocalResponseNormalization return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreateLocalResponseNormalization(*fbb, attr->depth_radius(), attr->bias(), attr->alpha(), attr->beta()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LocalResponseNormalization, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LocalResponseNormalizationCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LocalResponseNormalizationRegistry(schema::PrimitiveType_LocalResponseNormalization, - LocalResponseNormalizationCreator); - -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/local_response_normalization.h b/mindspore/lite/src/ops/local_response_normalization.h deleted file mode 100644 index 972a38c4b0..0000000000 --- a/mindspore/lite/src/ops/local_response_normalization.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class LocalResponseNormalization : public PrimitiveC { - public: - LocalResponseNormalization() = default; - ~LocalResponseNormalization() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LocalResponseNormalization, PrimitiveC); - explicit LocalResponseNormalization(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetDepthRadius(int depth_radius); - void SetBias(float bias); - void SetAlpha(float alpha); - void SetBeta(float beta); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetDepthRadius() const; - float GetBias() const; - float GetAlpha() const; - float GetBeta() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ diff --git a/mindspore/lite/src/ops/log.cc b/mindspore/lite/src/ops/log.cc deleted file mode 100644 index 73b57e2a25..0000000000 --- a/mindspore/lite/src/ops/log.cc +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/log.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Log::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Log; - } - if (this->primitive_->value.type != schema::PrimitiveType_Log) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - auto attr = std::make_unique(); - this->primitive_->value.value = attr.release(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - return RET_OK; -} -#else -int Log::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLog(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Log, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *LogCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry LogRegistry(schema::PrimitiveType_Log, LogCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/log.h b/mindspore/lite/src/ops/log.h deleted file mode 100644 index f742e087e8..0000000000 --- a/mindspore/lite/src/ops/log.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOG_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOG_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Log : public ArithmeticSelf { - public: - Log() = default; - ~Log() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Log, ArithmeticSelf); - explicit Log(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LOG_H_ diff --git a/mindspore/lite/src/ops/log_grad.cc b/mindspore/lite/src/ops/log_grad.cc deleted file mode 100644 index 4bb8be9d67..0000000000 --- a/mindspore/lite/src/ops/log_grad.cc +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/log_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int LogGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(primitive != nullptr); - MS_ASSERT(fbb != nullptr); - auto attr = primitive->value_as_LogGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_LogGrad return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateLogGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LogGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LogGradCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry LogGradRegistry(schema::PrimitiveType_LogGrad, LogGradCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/log_grad.h b/mindspore/lite/src/ops/log_grad.h deleted file mode 100644 index 6c88dd5a1a..0000000000 --- a/mindspore/lite/src/ops/log_grad.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOG_GRAD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOG_GRAD_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class LogGrad : public PrimitiveC { - public: - LogGrad() = default; - ~LogGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LogGrad, PrimitiveC); - explicit LogGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_LOG_GRAD_H_ diff --git a/mindspore/lite/src/ops/logical_and.cc b/mindspore/lite/src/ops/logical_and.cc deleted file mode 100644 index 461d87e7b3..0000000000 --- a/mindspore/lite/src/ops/logical_and.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/logical_and.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int LogicalAnd::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLogicalAnd(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LogicalAnd, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LogicalAndCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LogicalAndRegistry(schema::PrimitiveType_LogicalAnd, LogicalAndCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/logical_and.h b/mindspore/lite/src/ops/logical_and.h deleted file mode 100644 index 765a7cb5d9..0000000000 --- a/mindspore/lite/src/ops/logical_and.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class LogicalAnd : public Arithmetic { - public: - LogicalAnd() = default; - ~LogicalAnd() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LogicalAnd, Arithmetic); - explicit LogicalAnd(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ diff --git a/mindspore/lite/src/ops/logical_not.cc b/mindspore/lite/src/ops/logical_not.cc deleted file mode 100644 index 5eeae1915d..0000000000 --- a/mindspore/lite/src/ops/logical_not.cc +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/logical_not.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int LogicalNot::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLogicalNot(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LogicalNot, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *LogicalNotCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LogicalNotRegistry(schema::PrimitiveType_LogicalNot, LogicalNotCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/logical_not.h b/mindspore/lite/src/ops/logical_not.h deleted file mode 100644 index 53b511c104..0000000000 --- a/mindspore/lite/src/ops/logical_not.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class LogicalNot : public ArithmeticSelf { - public: - LogicalNot() = default; - ~LogicalNot() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LogicalNot, ArithmeticSelf); - explicit LogicalNot(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ diff --git a/mindspore/lite/src/ops/logical_or.cc b/mindspore/lite/src/ops/logical_or.cc deleted file mode 100644 index 142d22b986..0000000000 --- a/mindspore/lite/src/ops/logical_or.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/logical_or.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int LogicalOr::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateLogicalOr(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LogicalOr, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LogicalOrCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LogicalOrRegistry(schema::PrimitiveType_LogicalOr, LogicalOrCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/logical_or.h b/mindspore/lite/src/ops/logical_or.h deleted file mode 100644 index 5c342410bf..0000000000 --- a/mindspore/lite/src/ops/logical_or.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class LogicalOr : public Arithmetic { - public: - LogicalOr() = default; - ~LogicalOr() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LogicalOr, Arithmetic); - explicit LogicalOr(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ diff --git a/mindspore/lite/src/ops/lrn.cc b/mindspore/lite/src/ops/lrn.cc deleted file mode 100644 index 851e070c10..0000000000 --- a/mindspore/lite/src/ops/lrn.cc +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/lrn.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Lrn::GetAlpha() const { return this->primitive_->value.AsLrn()->alpha; } -float Lrn::GetBeta() const { return this->primitive_->value.AsLrn()->beta; } -float Lrn::GetBias() const { return this->primitive_->value.AsLrn()->bias; } -int Lrn::GetSize() const { return this->primitive_->value.AsLrn()->size; } - -void Lrn::SetAlpha(float alpha) { this->primitive_->value.AsLrn()->alpha = alpha; } -void Lrn::SetBeta(float beta) { this->primitive_->value.AsLrn()->beta = beta; } -void Lrn::SetBias(float bias) { this->primitive_->value.AsLrn()->bias = bias; } -void Lrn::SetSize(int size) { this->primitive_->value.AsLrn()->size = size; } - -#else - -float Lrn::GetAlpha() const { return this->primitive_->value_as_Lrn()->alpha(); } -float Lrn::GetBeta() const { return this->primitive_->value_as_Lrn()->beta(); } -float Lrn::GetBias() const { return this->primitive_->value_as_Lrn()->bias(); } -int Lrn::GetSize() const { return this->primitive_->value_as_Lrn()->size(); } - -int Lrn::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Lrn(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Lrn return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateLrn(*fbb, attr->alpha(), attr->beta(), attr->bias(), attr->size()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Lrn, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LrnCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry LrnRegistry(schema::PrimitiveType_Lrn, LrnCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/lrn.h b/mindspore/lite/src/ops/lrn.h deleted file mode 100644 index fac65bd8ef..0000000000 --- a/mindspore/lite/src/ops/lrn.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LRN_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LRN_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Lrn : public PrimitiveC { - public: - Lrn() = default; - ~Lrn() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Lrn, PrimitiveC); - explicit Lrn(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAlpha(float alpha); - void SetBeta(float beta); - void SetBias(float bias); - void SetSize(int size); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetAlpha() const; - float GetBeta() const; - float GetBias() const; - int GetSize() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LRN_H_ diff --git a/mindspore/lite/src/ops/lsh_projection.cc b/mindspore/lite/src/ops/lsh_projection.cc deleted file mode 100644 index 893fde7870..0000000000 --- a/mindspore/lite/src/ops/lsh_projection.cc +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/lsh_projection.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int LshProjection::UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_OK; } -int LshProjection::GetLshType() const { return this->primitive_->value.AsLshProjection()->type; } -#else -int LshProjection::GetLshType() const { return this->primitive_->value_as_LshProjection()->type(); } - -int LshProjection::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_LshProjection(); - if (attr == nullptr) { - MS_LOG(ERROR) << "LshProjection attr is nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateLshProjection(*fbb, attr->type()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_LshProjection, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LshProjectionCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry LshProjectionRegistry(schema::PrimitiveType_LshProjection, LshProjectionCreator); - -#endif - -int LshProjection::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kDoubleNum && inputs_.size() != kTripleNum) { - MS_LOG(ERROR) << "inputs to LshProjection operator should be 2 or 3, but " << inputs_.size() << " is given."; - return RET_ERROR; - } - if (outputs_.size() != kSingleNum) { - MS_LOG(ERROR) << "outputs to Shape operator should be 1, but " << outputs_.size() << " is given."; - return RET_ERROR; - } - - auto in_hash = inputs_.at(0); - MS_ASSERT(in_hash->shape().size() == 2); - MS_ASSERT(in_hash->DimensionSize(1) <= 32); - MS_ASSERT(inputs_.at(1)->shape().size() >= 1); - - if (inputs_.size() == kTripleNum) { - MS_ASSERT(inputs_.at(2)->shape().size() == 1); - MS_ASSERT(inputs_.at(2)->DimensionSize(0) == inputs_.at(1)->DimensionSize(0)); - } - - auto out_tensor = outputs_.front(); - out_tensor->set_data_type(kNumberTypeInt32); - out_tensor->set_format(schema::Format::Format_NHWC); - - std::vector out_shape; - switch (GetLshType()) { - case schema::LshProjectionType_SPARSE: - out_shape.push_back(in_hash->DimensionSize(0)); - break; - case schema::LshProjectionType_DENSE: - out_shape.push_back(in_hash->DimensionSize(0) * in_hash->DimensionSize(1)); - break; - default: - return RET_ERROR; - } - out_tensor->set_shape(out_shape); - return RET_OK; -} - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/lsh_projection.h b/mindspore/lite/src/ops/lsh_projection.h deleted file mode 100644 index 8888d4d73f..0000000000 --- a/mindspore/lite/src/ops/lsh_projection.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef LITE_MINDSPORE_LITE_C_OPS_LSH_PROJECTION_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LSH_PROJECTION_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class LshProjection : public PrimitiveC { - public: - LshProjection() = default; - ~LshProjection() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(LshProjection, PrimitiveC); - explicit LshProjection(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetLshType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LSH_PROJECTION_H_ diff --git a/mindspore/lite/src/ops/lstm.cc b/mindspore/lite/src/ops/lstm.cc deleted file mode 100644 index 7d1a398784..0000000000 --- a/mindspore/lite/src/ops/lstm.cc +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/lstm.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool Lstm::GetBidirection() const { return this->primitive_->value.AsLstm()->bidirection; } - -float Lstm::GetSmooth() const { return this->primitive_->value.AsLstm()->smooth; } - -void Lstm::SetBidirection(bool bidirection) { this->primitive_->value.AsLstm()->bidirection = bidirection; } - -void Lstm::SetSmooth(float smooth) { this->primitive_->value.AsLstm()->smooth = smooth; } - -#else - -bool Lstm::GetBidirection() const { return this->primitive_->value_as_Lstm()->bidirection(); } -float Lstm::GetSmooth() const { return this->primitive_->value_as_Lstm()->smooth(); } -int Lstm::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Lstm(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Lstm return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateLstm(*fbb, attr->bidirection(), attr->smooth()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Lstm, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *LstmCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry LstmRegistry(schema::PrimitiveType_Lstm, LstmCreator); - -#endif - -const int kLstmInputNum = 6; -const int kLstmOutputNum = 3; -int Lstm::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() != kLstmInputNum || outputs_.size() != kLstmOutputNum) { - MS_LOG(ERROR) << "OpLstm inputs or outputs size error."; - return RET_INPUT_TENSOR_ERROR; - } - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto weight_i = inputs_.at(1); - MS_ASSERT(weight_i != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - for (int i = 0; i < kLstmOutputNum; i++) { - outputs_.at(i)->set_data_type(input->data_type()); - outputs_.at(i)->set_format(input->format()); - } - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector in_shape = input->shape(); - std::vector w_shape = weight_i->shape(); // layer, hidden_size * 4, input_size - if (in_shape.size() != 3 || w_shape.size() != 3) { - MS_LOG(ERROR) << "OpLstm input dims should be 3."; - return RET_ERROR; - } - - int hidden_size = w_shape[1] / 4; - // set output - std::vector out_shape(in_shape); - out_shape[2] = hidden_size; - if (GetBidirection()) { - out_shape.insert(out_shape.begin() + 1, 2); - } else { - out_shape.insert(out_shape.begin() + 1, 1); - } - output->set_shape(out_shape); - // set hidden state, cell state - std::vector state_shape(in_shape); - state_shape[0] = GetBidirection() ? 2 : 1; - state_shape[2] = hidden_size; - outputs_[1]->set_shape(state_shape); - outputs_[2]->set_shape(state_shape); - - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/lstm.h b/mindspore/lite/src/ops/lstm.h deleted file mode 100644 index fd58a99a46..0000000000 --- a/mindspore/lite/src/ops/lstm.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ -#define LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Lstm : public PrimitiveC { - public: - Lstm() = default; - ~Lstm() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Lstm, PrimitiveC); - explicit Lstm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetBidirection(bool bidirection); - void SetSmooth(float smooth); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool GetBidirection() const; - float GetSmooth() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ diff --git a/mindspore/lite/src/ops/make_tuple.cc b/mindspore/lite/src/ops/make_tuple.cc deleted file mode 100644 index 63528302d5..0000000000 --- a/mindspore/lite/src/ops/make_tuple.cc +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/make_tuple.h" -#include -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int MakeTuple::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_MakeTuple; - } - if (this->primitive_->value.type != schema::PrimitiveType_MakeTuple) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MakeTupleT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int MakeTuple::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMakeTuple(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_MakeTuple, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MakeTupleCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry MakeTupleRegistry(schema::PrimitiveType_MakeTuple, MakeTupleCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/make_tuple.h b/mindspore/lite/src/ops/make_tuple.h deleted file mode 100644 index 5a7611af48..0000000000 --- a/mindspore/lite/src/ops/make_tuple.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_MAKE_TUPLE_H_ -#define MINDSPORE_LITE_SRC_OPS_MAKE_TUPLE_H_ -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class MakeTuple : public PrimitiveC { - public: - MakeTuple() = default; - ~MakeTuple() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(MakeTuple, PrimitiveC); - explicit MakeTuple(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_MAKE_TUPLE_H_ diff --git a/mindspore/lite/src/ops/matmul.cc b/mindspore/lite/src/ops/matmul.cc deleted file mode 100644 index 9e50dc222e..0000000000 --- a/mindspore/lite/src/ops/matmul.cc +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/matmul.h" -#include -#include -#ifdef PRIMITIVE_WRITEABLE -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool MatMul::GetTransposeA() const { return this->primitive_->value.AsMatMul()->transposeA; } -bool MatMul::GetTransposeB() const { return this->primitive_->value.AsMatMul()->transposeB; } - -void MatMul::SetTransposeA(bool transpose_a) { this->primitive_->value.AsMatMul()->transposeA = transpose_a; } -void MatMul::SetTransposeB(bool transpose_b) { this->primitive_->value.AsMatMul()->transposeB = transpose_b; } - -int MatMul::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_MatMul; - } - if (this->primitive_->value.type != schema::PrimitiveType_MatMul) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MatMulT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->transposeA = GetValue(prim.GetAttr("transpose_a")); - attr->transposeB = GetValue(prim.GetAttr("transpose_b")); - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else - -bool MatMul::GetTransposeA() const { return this->primitive_->value_as_MatMul()->transposeA(); } -bool MatMul::GetTransposeB() const { return this->primitive_->value_as_MatMul()->transposeB(); } - -int MatMul::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_MatMul(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_MatMul return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateMatMul(*fbb, attr->broadcast(), attr->transposeA(), attr->transposeB()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_MatMul, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MatMulCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MatMulRegistry(schema::PrimitiveType_MatMul, MatMulCreator); -#endif - -int MatMul::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input0 = inputs_.front(); - MS_ASSERT(input0 != nullptr); - auto input1 = inputs_.at(1); - MS_ASSERT(input1 != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - - output->set_data_type(input0->data_type()); - output->set_format(input0->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector a_shape = input0->shape(); - std::vector b_shape = input1->shape(); - - if (a_shape.size() == 4 && a_shape[2] == 1 && a_shape[3] == 1) { - a_shape.resize(2); - input0->set_shape(a_shape); - } - - bool del_start = false; - bool del_end = false; - if (a_shape.size() == 1) { - a_shape.insert(a_shape.begin(), 1); - input0->set_shape(a_shape); - del_start = true; - } - if (b_shape.size() == 1) { - b_shape.push_back(1); - input1->set_shape(b_shape); - del_end = true; - } - for (size_t i = 0; i < (a_shape.size() - 2) && i < (b_shape.size() - 2); ++i) { - if (a_shape.at(a_shape.size() - 3 - i) != b_shape.at(b_shape.size() - 3 - i)) { - MS_LOG(ERROR) << "Op MatMul's dimensions must be equal"; - return RET_INPUT_TENSOR_ERROR; - } - } - - if (GetTransposeA()) { - std::swap(a_shape[a_shape.size() - 1], a_shape[a_shape.size() - 2]); - } - if (GetTransposeB()) { - std::swap(b_shape[b_shape.size() - 1], b_shape[b_shape.size() - 2]); - } - std::vector c_shape(a_shape); - c_shape[c_shape.size() - 1] = b_shape[b_shape.size() - 1]; - if (del_start) { - c_shape.erase(c_shape.begin()); - } - if (del_end) { - c_shape.pop_back(); - } - output->set_shape(c_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/matmul.h b/mindspore/lite/src/ops/matmul.h deleted file mode 100644 index 9c2d1b650a..0000000000 --- a/mindspore/lite/src/ops/matmul.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class MatMul : public PrimitiveC { - public: - MatMul() = default; - ~MatMul() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(MatMul, PrimitiveC); - explicit MatMul(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetTransposeA(bool transpose_a); - void SetTransposeB(bool transpose_b); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - bool GetTransposeA() const; - bool GetTransposeB() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ diff --git a/mindspore/lite/src/ops/maximum.cc b/mindspore/lite/src/ops/maximum.cc deleted file mode 100644 index 1d17d3e655..0000000000 --- a/mindspore/lite/src/ops/maximum.cc +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "include/errorcode.h" -#include "src/ops/maximum.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Maximum::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Maximum; - } - if (this->primitive_->value.type != schema::PrimitiveType_Maximum) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MaximumT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Maximum::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMaximum(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Maximum, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MaximumCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MaximumRegistry(schema::PrimitiveType_Maximum, MaximumCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/maximum.h b/mindspore/lite/src/ops/maximum.h deleted file mode 100644 index 052088ebab..0000000000 --- a/mindspore/lite/src/ops/maximum.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_MAXIMUM_H_ -#define MINDSPORE_LITE_SRC_OPS_MAXIMUM_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Maximum : public Arithmetic { - public: - Maximum() = default; - ~Maximum() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Arithmetic, Arithmetic); - explicit Maximum(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_MAXIMUM_H_ diff --git a/mindspore/lite/src/ops/maximum_grad.cc b/mindspore/lite/src/ops/maximum_grad.cc deleted file mode 100644 index 634e4d5853..0000000000 --- a/mindspore/lite/src/ops/maximum_grad.cc +++ /dev/null @@ -1,126 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "include/errorcode.h" -#include "src/ops/maximum_grad.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int MaximumGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_MaximumGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_MaximumGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MaximumGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int MaximumGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMaximumGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_MaximumGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *MaximumGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry MaximumGradRegistry(schema::PrimitiveType_MaximumGrad, MaximumGradCreator); - -#endif -int MaximumGrad::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != 3) { - MS_LOG(ERROR) << "The number of input must be 3"; - return RET_ERROR; - } - if (outputs_.size() != 2) { - MS_LOG(ERROR) << "The number of output must be 2"; - return RET_ERROR; - } - - auto x1 = inputs_[0]; - auto x2 = inputs_[1]; - auto dy = inputs_[2]; - auto dx1 = outputs_[0]; - auto dx2 = outputs_[1]; - - MS_ASSERT(dy != nullptr); - MS_ASSERT(x1 != nullptr); - MS_ASSERT(x2 != nullptr); - MS_ASSERT(dx1 != nullptr); - MS_ASSERT(dx2 != nullptr); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto inShape0 = x1->shape(); - auto inShape1 = x2->shape(); - auto outShape = dy->shape(); - - ndim_ = outShape.size(); - x1_shape_.resize(ndim_); - x2_shape_.resize(ndim_); - dy_shape_.resize(ndim_); - auto fillDimNum0 = outShape.size() - inShape0.size(); - auto fillDimNum1 = outShape.size() - inShape1.size(); - int j0 = 0; - int j1 = 0; - for (unsigned int i = 0; i < outShape.size(); i++) { - x1_shape_[i] = (i < fillDimNum0) ? 1 : inShape0[j0++]; - x2_shape_[i] = (i < fillDimNum1) ? 1 : inShape1[j1++]; - dy_shape_[i] = outShape[i]; - } - - dx1->set_shape(x1->shape()); - dx2->set_shape(x2->shape()); - dx1->set_data_type(dy->data_type()); - dx2->set_data_type(dy->data_type()); - dx1->set_format(dy->format()); - dx2->set_format(dy->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/maximum_grad.h b/mindspore/lite/src/ops/maximum_grad.h deleted file mode 100644 index 10e73b485a..0000000000 --- a/mindspore/lite/src/ops/maximum_grad.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_MAXIMUM_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_MAXIMUM_GRAD_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_grad.h" -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class MaximumGrad : public ArithmeticGrad { - public: -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(MaximumGrad, ArithmeticGrad); - MaximumGrad() = default; - explicit MaximumGrad(schema::PrimitiveT *primitive) : ArithmeticGrad(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - MaximumGrad() = default; - - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_MAXIMUM_GRAD_H_ diff --git a/mindspore/lite/src/ops/merge.cc b/mindspore/lite/src/ops/merge.cc deleted file mode 100644 index fcf8b505b7..0000000000 --- a/mindspore/lite/src/ops/merge.cc +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/merge.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif -#include "src/tensorlist.h" - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Merge::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Merge; - } - if (this->primitive_->value.type != schema::PrimitiveType_Merge) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::MergeT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - PopulaterQuantParam(prim, inputs); - return RET_OK; -} - -#else -int Merge::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Merge(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Merge return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateMerge(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Merge, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MergeCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MergeRegistry(schema::PrimitiveType_Merge, MergeCreator); -#endif - -InferStatus Merge::AbleToInfer(const std::vector &inputs) { - for (auto &input : inputs) { - if (input->shape().empty()) { - return HasZeroShape; - } - if (input->root_tensor() != nullptr && input->root_tensor()->data_c() != nullptr) { - continue; - } - if (input->data_c() == nullptr) { - return NotAble; - } - } - return Able; -} - -int Merge::Infer(const std::vector &inputs, const std::vector &outputs) { - for (size_t i = 0; i < inputs.size(); i++) { - auto *input = inputs[i]; - auto *output = outputs[i]; - if (input == nullptr) { - MS_LOG(ERROR) << "input tensor is nullptr"; - return RET_ERROR; - } - if (output == nullptr) { - MS_LOG(ERROR) << "output tensor is nullptr"; - return RET_ERROR; - } - output->set_data_type(input->data_type()); - output->set_shape(input->shape()); - output->set_format(input->format()); - auto data_type = input->data_type(); - if (data_type != kObjectTypeTensorType) { - continue; - } else { - auto input_tensorlist = reinterpret_cast(input); - auto output_tensorlist = reinterpret_cast(output); - output_tensorlist->set_element_shape(input_tensorlist->element_shape()); - output_tensorlist->set_max_elements_num(input_tensorlist->max_elements_num()); - output_tensorlist->set_tensors_data_type(input_tensorlist->tensors_data_type()); - } - } - return RET_OK; -} - -int Merge::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(inputs_.size() == 2 * outputs_.size()); - for (size_t i = 0; i < outputs_.size(); ++i) { - outputs_[i]->set_data_type(inputs_[i]->data_type()); - } - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector left_part_inputs{}; - left_part_inputs.assign(inputs_.begin(), inputs_.begin() + inputs_.size() / 2); - - std::vector right_part_inputs{}; - right_part_inputs.assign(inputs_.begin() + inputs_.size() / 2, inputs_.end()); - - if (AbleToInfer(left_part_inputs) == Able) { - return Infer(left_part_inputs, outputs_); - } - - if (AbleToInfer(right_part_inputs) == Able) { - return Infer(right_part_inputs, outputs_); - } - - if (AbleToInfer(left_part_inputs) == HasZeroShape && AbleToInfer(right_part_inputs) == HasZeroShape) { - return Infer(left_part_inputs, outputs_); - } - - return RET_INFER_INVALID; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/merge.h b/mindspore/lite/src/ops/merge.h deleted file mode 100644 index fa177913ec..0000000000 --- a/mindspore/lite/src/ops/merge.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_MERGE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_MERGE_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -enum InferStatus { Able, NotAble, HasZeroShape }; - -class Merge : public PrimitiveC { - public: - Merge() = default; - ~Merge() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Merge, PrimitiveC); - explicit Merge(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - - private: - static InferStatus AbleToInfer(const std::vector &inputs); - static int Infer(const std::vector &inputs, const std::vector &outputs); -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_MERGE_H_ diff --git a/mindspore/lite/src/ops/mfcc.cc b/mindspore/lite/src/ops/mfcc.cc deleted file mode 100644 index 511e1cfc95..0000000000 --- a/mindspore/lite/src/ops/mfcc.cc +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/mfcc.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Mfcc::GetFreqUpperLimit() const { return this->primitive_->value.AsMfcc()->freqUpperLimit; } -float Mfcc::GetFreqLowerLimit() const { return this->primitive_->value.AsMfcc()->freqLowerLimit; } -int Mfcc::GetFilterBankChannelNum() const { return this->primitive_->value.AsMfcc()->filterBankChannelNum; } -int Mfcc::GetDctCoeffNum() const { return this->primitive_->value.AsMfcc()->dctCoeffNum; } - -#else -float Mfcc::GetFreqUpperLimit() const { return this->primitive_->value_as_Mfcc()->freqUpperLimit(); } -float Mfcc::GetFreqLowerLimit() const { return this->primitive_->value_as_Mfcc()->freqLowerLimit(); } -int Mfcc::GetFilterBankChannelNum() const { return this->primitive_->value_as_Mfcc()->filterBankChannelNum(); } -int Mfcc::GetDctCoeffNum() const { return this->primitive_->value_as_Mfcc()->dctCoeffNum(); } -int Mfcc::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Mfcc(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Add return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateMfcc(*fbb, attr->freqUpperLimit(), attr->freqLowerLimit(), - attr->filterBankChannelNum(), attr->dctCoeffNum()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Mfcc, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MfccCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MfccRegistry(schema::PrimitiveType_Mfcc, MfccCreator); -#endif -int Mfcc::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - if (input_shape.size() != 3) { - MS_LOG(ERROR) << "first input shape is error, which need to be 3 dimensions, but the dimension is " - << input_shape.size(); - return RET_ERROR; - } - if (inputs_[1]->ElementsNum() != 1) { - MS_LOG(ERROR) << "second input element num is error, which need only a value, but the number is " - << inputs_[1]->ElementsNum(); - return RET_ERROR; - } - std::vector output_shape(3); - output_shape[0] = input_shape[0]; - output_shape[1] = input_shape[1]; - output_shape[2] = GetDctCoeffNum(); - outputs_.front()->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/mfcc.h b/mindspore/lite/src/ops/mfcc.h deleted file mode 100644 index 8b94599226..0000000000 --- a/mindspore/lite/src/ops/mfcc.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_MFCC_H_ -#define LITE_MINDSPORE_LITE_C_OPS_MFCC_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Mfcc : public PrimitiveC { - public: - Mfcc() = default; - ~Mfcc() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Mfcc, PrimitiveC); - explicit Mfcc(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFreqUpperLimit(float freq_upper_limit) { - this->primitive_->value.AsMfcc()->freqUpperLimit = freq_upper_limit; - } - void SetFreqLowerLimit(float freq_lower_limit) { - this->primitive_->value.AsMfcc()->freqLowerLimit = freq_lower_limit; - } - void SetFilterBankChannelNum(int filter_bank_channel_num) { - this->primitive_->value.AsMfcc()->filterBankChannelNum = filter_bank_channel_num; - } - void SetDctCoeffNum(int dct_coeff_num) { this->primitive_->value.AsMfcc()->dctCoeffNum = dct_coeff_num; } -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetFreqUpperLimit() const; - float GetFreqLowerLimit() const; - int GetFilterBankChannelNum() const; - int GetDctCoeffNum() const; - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_MFCC_H_ diff --git a/mindspore/lite/src/ops/minimum.cc b/mindspore/lite/src/ops/minimum.cc deleted file mode 100644 index 5881976ad3..0000000000 --- a/mindspore/lite/src/ops/minimum.cc +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/minimum.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Minimum::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Minimum; - } - if (this->primitive_->value.type != schema::PrimitiveType_Minimum) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MinimumT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Minimum::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMinimum(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Minimum, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *MinimumCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MinimumRegistry(schema::PrimitiveType_Minimum, MinimumCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/minimum.h b/mindspore/lite/src/ops/minimum.h deleted file mode 100644 index de69645c70..0000000000 --- a/mindspore/lite/src/ops/minimum.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_MINIMUM_H_ -#define MINDSPORE_LITE_SRC_OPS_MINIMUM_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Minimum : public Arithmetic { - public: - Minimum() = default; - ~Minimum() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Arithmetic, Arithmetic); - explicit Minimum(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_MINIMUM_H_ diff --git a/mindspore/lite/src/ops/minimum_grad.cc b/mindspore/lite/src/ops/minimum_grad.cc deleted file mode 100644 index 6c5df183f5..0000000000 --- a/mindspore/lite/src/ops/minimum_grad.cc +++ /dev/null @@ -1,128 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "include/errorcode.h" -#include "src/ops/minimum_grad.h" -#include "src/common/log_adapter.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include "src/param_value_lite.h" -#endif - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int MinimumGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_MinimumGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_MinimumGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MinimumGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -PrimitiveC *MinimumGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry MinimumGradRegistry(schema::PrimitiveType_MinimumGrad, MinimumGradCreator); - -int MinimumGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMinimumGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_MinimumGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -#endif - -int MinimumGrad::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != 3) { - MS_LOG(ERROR) << "The number of input must be 3"; - return RET_ERROR; - } - if (outputs_.size() != 2) { - MS_LOG(ERROR) << "The number of output must be 2"; - return RET_ERROR; - } - - auto x1 = inputs_[0]; - auto x2 = inputs_[1]; - auto dy = inputs_[2]; - auto dx1 = outputs_[0]; - auto dx2 = outputs_[1]; - - MS_ASSERT(dy != nullptr); - MS_ASSERT(x1 != nullptr); - MS_ASSERT(x2 != nullptr); - MS_ASSERT(dx1 != nullptr); - MS_ASSERT(dx2 != nullptr); - if (!infer_flag()) { - return RET_OK; - } - - auto inShape0 = x1->shape(); - auto inShape1 = x2->shape(); - auto outShape = dy->shape(); - - ndim_ = outShape.size(); - x1_shape_.resize(ndim_); - x2_shape_.resize(ndim_); - dy_shape_.resize(ndim_); - auto fillDimNum0 = outShape.size() - inShape0.size(); - auto fillDimNum1 = outShape.size() - inShape1.size(); - int j0 = 0; - int j1 = 0; - for (unsigned int i = 0; i < outShape.size(); i++) { - x1_shape_[i] = (i < fillDimNum0) ? 1 : inShape0[j0++]; - x2_shape_[i] = (i < fillDimNum1) ? 1 : inShape1[j1++]; - dy_shape_[i] = outShape[i]; - } - - dx1->set_shape(x1->shape()); - dx2->set_shape(x2->shape()); - dx1->set_data_type(dy->data_type()); - dx2->set_data_type(dy->data_type()); - dx1->set_format(dy->format()); - dx2->set_format(dy->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/minimum_grad.h b/mindspore/lite/src/ops/minimum_grad.h deleted file mode 100644 index 83418897b2..0000000000 --- a/mindspore/lite/src/ops/minimum_grad.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_MINIMUM_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_MINIMUM_GRAD_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_grad.h" -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class MinimumGrad : public ArithmeticGrad { - public: -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(MinimumGrad, ArithmeticGrad); - MinimumGrad() = default; - explicit MinimumGrad(schema::PrimitiveT *primitive) : ArithmeticGrad(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - MinimumGrad() = default; - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_MINIMUM_GRAD_H_ diff --git a/mindspore/lite/src/ops/mod.cc b/mindspore/lite/src/ops/mod.cc deleted file mode 100644 index c0024408ac..0000000000 --- a/mindspore/lite/src/ops/mod.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/mod.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Mod::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Mod; - } - if (this->primitive_->value.type != schema::PrimitiveType_Mod) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ModT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else - -int Mod::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateMod(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Mod, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *ModCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ModRegistry(schema::PrimitiveType_Mod, ModCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/mod.h b/mindspore/lite/src/ops/mod.h deleted file mode 100644 index 3a351e6889..0000000000 --- a/mindspore/lite/src/ops/mod.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_MOD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_MOD_H_ - -#include -#include -#include -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Mod : public Arithmetic { - public: - Mod() = default; - ~Mod() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Mod, Arithmetic); - explicit Mod(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ diff --git a/mindspore/lite/src/ops/mul.cc b/mindspore/lite/src/ops/mul.cc deleted file mode 100644 index bb46cb0382..0000000000 --- a/mindspore/lite/src/ops/mul.cc +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/mul.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Mul::GetActivationType() const { return this->primitive_->value.AsMul()->activationType; } - -void Mul::SetActivationType(int activation_type) { - this->primitive_->value.AsMul()->activationType = (schema::ActivationType)activation_type; -} -int Mul::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Mul; - } - if (this->primitive_->value.type != schema::PrimitiveType_Mul) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::MulT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - - return RET_OK; -} - -#else - -int Mul::GetActivationType() const { return this->primitive_->value_as_Mul()->activationType(); } - -int Mul::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Mul(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Mul return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateMul(*fbb, attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Mul, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *MulCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry MulRegistry(schema::PrimitiveType_Mul, MulCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/mul.h b/mindspore/lite/src/ops/mul.h deleted file mode 100644 index 65b31556d7..0000000000 --- a/mindspore/lite/src/ops/mul.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_MUL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_MUL_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class Mul : public Arithmetic { - public: - Mul() = default; - ~Mul() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Mul, Arithmetic); - explicit Mul(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - void SetActivationType(int activation_type); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_MUL_H_ diff --git a/mindspore/lite/src/ops/nchw2nhwc.cc b/mindspore/lite/src/ops/nchw2nhwc.cc deleted file mode 100644 index ff80c7aba8..0000000000 --- a/mindspore/lite/src/ops/nchw2nhwc.cc +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/nchw2nhwc.h" -#include "src/common/common.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int Nchw2Nhwc::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateNchw2Nhwc(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Nchw2Nhwc, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *Nchw2NhwcCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry Nchw2NhwcRegistry(schema::PrimitiveType_Nchw2Nhwc, Nchw2NhwcCreator); -#endif - -int Nchw2Nhwc::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(schema::Format::Format_NHWC); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector nchw_shape = input->shape(); - if (nchw_shape.size() != 4) { - output->set_shape(nchw_shape); - } else { - std::vector nhwc_shape{nchw_shape}; - nhwc_shape[NHWC_N] = nchw_shape[NCHW_N]; - nhwc_shape[NHWC_H] = nchw_shape[NCHW_H]; - nhwc_shape[NHWC_W] = nchw_shape[NCHW_W]; - nhwc_shape[NHWC_C] = nchw_shape[NCHW_C]; - output->set_shape(nhwc_shape); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/nchw2nhwc.h b/mindspore/lite/src/ops/nchw2nhwc.h deleted file mode 100644 index 5894e993b5..0000000000 --- a/mindspore/lite/src/ops/nchw2nhwc.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ -#define LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Nchw2Nhwc : public PrimitiveC { - public: - Nchw2Nhwc() = default; - ~Nchw2Nhwc() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Nchw2Nhwc, PrimitiveC); - explicit Nchw2Nhwc(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ diff --git a/mindspore/lite/src/ops/neg.cc b/mindspore/lite/src/ops/neg.cc deleted file mode 100644 index 8f52f69dcc..0000000000 --- a/mindspore/lite/src/ops/neg.cc +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/neg.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Neg::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Neg; - } - if (this->primitive_->value.type != schema::PrimitiveType_Neg) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::NegT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int Neg::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(primitive != nullptr); - MS_ASSERT(fbb != nullptr); - auto val_offset = schema::CreateNeg(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Neg, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *NegCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry NegRegistry(schema::PrimitiveType_Neg, NegCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/neg.h b/mindspore/lite/src/ops/neg.h deleted file mode 100644 index e22c346d12..0000000000 --- a/mindspore/lite/src/ops/neg.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_NEG_H_ -#define LITE_MINDSPORE_LITE_C_OPS_NEG_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Neg : public ArithmeticSelf { - public: - Neg() = default; - ~Neg() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Neg, ArithmeticSelf); - explicit Neg(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NEG_H_ diff --git a/mindspore/lite/src/ops/neg_grad.cc b/mindspore/lite/src/ops/neg_grad.cc deleted file mode 100644 index 4c74be9953..0000000000 --- a/mindspore/lite/src/ops/neg_grad.cc +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/neg_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifndef PRIMITIVE_WRITEABLE -int NegGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(primitive != nullptr); - MS_ASSERT(fbb != nullptr); - auto val_offset = schema::CreateNegGrad(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_NegGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *NegGradCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry NegGradRegistry(schema::PrimitiveType_NegGrad, NegGradCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/neg_grad.h b/mindspore/lite/src/ops/neg_grad.h deleted file mode 100644 index dd31995eaa..0000000000 --- a/mindspore/lite/src/ops/neg_grad.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_NEG_GRAD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_NEG_GRAD_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class NegGrad : public ArithmeticSelf { - public: - NegGrad() = default; - ~NegGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(NegGrad, ArithmeticSelf); - explicit NegGrad(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NEG_GRAD_H_ diff --git a/mindspore/lite/src/ops/nhwc2nchw.cc b/mindspore/lite/src/ops/nhwc2nchw.cc deleted file mode 100644 index 9e7648be72..0000000000 --- a/mindspore/lite/src/ops/nhwc2nchw.cc +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/nhwc2nchw.h" -#include "src/common/common.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -#ifdef PRIMITIVE_WRITEABLE -#else -int Nhwc2Nchw::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateNhwc2Nchw(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Nhwc2Nchw, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *Nhwc2NchwCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry Nhwc2NchwRegistry(schema::PrimitiveType_Nhwc2Nchw, Nhwc2NchwCreator); -#endif - -int Nhwc2Nchw::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_format(schema::Format::Format_NCHW); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector nhwc_shape = input->shape(); - if (nhwc_shape.size() != 4) { - output->set_shape(nhwc_shape); - } else { - std::vector nchw_shape{nhwc_shape}; - nchw_shape[NCHW_N] = nhwc_shape[NHWC_N]; - nchw_shape[NCHW_C] = nhwc_shape[NHWC_C]; - nchw_shape[NCHW_H] = nhwc_shape[NHWC_H]; - nchw_shape[NCHW_W] = nhwc_shape[NHWC_W]; - output->set_shape(nchw_shape); - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/nhwc2nchw.h b/mindspore/lite/src/ops/nhwc2nchw.h deleted file mode 100644 index f76d22695a..0000000000 --- a/mindspore/lite/src/ops/nhwc2nchw.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ -#define LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Nhwc2Nchw : public PrimitiveC { - public: - Nhwc2Nchw() = default; - ~Nhwc2Nchw() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Nhwc2Nchw, PrimitiveC); - explicit Nhwc2Nchw(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ diff --git a/mindspore/lite/src/ops/non_max_suppression.cc b/mindspore/lite/src/ops/non_max_suppression.cc deleted file mode 100644 index 131a8594c1..0000000000 --- a/mindspore/lite/src/ops/non_max_suppression.cc +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/non_max_suppression.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -void NonMaxSuppression::SetCenterPointBox(int centerPointBox) { - this->primitive_->value.AsNonMaxSuppression()->centerPointBox = centerPointBox; -} - -int NonMaxSuppression::GetCenterPointBox() const { - return this->primitive_->value.AsNonMaxSuppression()->centerPointBox; -} -#else -int NonMaxSuppression::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_NonMaxSuppression(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_NonMaxSuppression return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateNonMaxSuppression(*fbb, attr->centerPointBox()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_NonMaxSuppression, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int NonMaxSuppression::GetCenterPointBox() const { - return this->primitive_->value_as_NonMaxSuppression()->centerPointBox(); -} - -PrimitiveC *NonMaxSuppressionCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} - -Registry NonMaxSuppressionRegistry(schema::PrimitiveType_NonMaxSuppression, NonMaxSuppressionCreator); - -#endif -int NonMaxSuppression::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(kNumberTypeInt32); - output->set_format(input->format()); - MS_LOG(INFO) << "NonMaxSuppression infer shape in runtime."; - return RET_INFER_INVALID; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/non_max_suppression.h b/mindspore/lite/src/ops/non_max_suppression.h deleted file mode 100644 index ecfcaa3fbc..0000000000 --- a/mindspore/lite/src/ops/non_max_suppression.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_NON_MAX_SUPPRESSION_H_ -#define LITE_MINDSPORE_LITE_NON_MAX_SUPPRESSION_H_ - -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class NonMaxSuppression : public PrimitiveC { - public: - NonMaxSuppression() = default; - ~NonMaxSuppression() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(NonMaxSuppression, PrimitiveC); - explicit NonMaxSuppression(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetCenterPointBox(int centerPointBox); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetCenterPointBox() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ diff --git a/mindspore/lite/src/ops/nonzero.cc b/mindspore/lite/src/ops/nonzero.cc deleted file mode 100644 index d4e7a3793c..0000000000 --- a/mindspore/lite/src/ops/nonzero.cc +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/nonzero.h" -#include -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int NonZero::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_NonZero; - } - if (this->primitive_->value.type != schema::PrimitiveType_NonZero) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::NonZeroT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - PopulaterQuantParam(prim, inputs); - return RET_OK; -} -#else -int NonZero::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_NonZero(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_NonZero return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateNonZero(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_NonZero, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *NonZeroCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry NonZeroRegistry(schema::PrimitiveType_NonZero, NonZeroCreator); -#endif -template -void CalShape(const T *data, const std::vector &inputs, std::vector *out_shape) { - int input_count = inputs[0]->ElementsNum(); - int input_dim_size = inputs[0]->shape().empty() ? 1 : inputs[0]->shape().size(); - out_shape->emplace_back(input_dim_size); - int nonzero_size = 0; - for (int i = 0; i < input_count; i++) { - if (static_cast(data[i]) != 0) { - nonzero_size++; - } - } - if (nonzero_size == 0) { - return; - } else { - out_shape->emplace_back(nonzero_size); - } -} -int NonZero::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - MS_ASSERT(inputs_.size() == 1); - auto input_tensor = inputs_.front(); - MS_ASSERT(input_tensor != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(TypeId::kNumberTypeInt32); - output->set_format(input_tensor->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector out_shape; - if (inputs_.size() == kSingleNum) { - if (input_tensor->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - switch (input_tensor->data_type()) { - case kNumberTypeBool: { - auto data = reinterpret_cast(input_tensor->MutableData()); - CalShape(data, inputs_, &out_shape); - } break; - default: { - MS_LOG(ERROR) << "NonZero weight tensor has unsupported dataType: " << input_tensor->data_type(); - return RET_INFER_ERR; - } - } - } else { - MS_LOG(ERROR) << "inputs tensor size invalid."; - return RET_INFER_ERR; - } - output->set_shape(out_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/nonzero.h b/mindspore/lite/src/ops/nonzero.h deleted file mode 100644 index ba769e8076..0000000000 --- a/mindspore/lite/src/ops/nonzero.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_NONZERO_H_ -#define MINDSPORE_LITE_SRC_OPS_NONZERO_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class NonZero : public PrimitiveC { - public: - NonZero() = default; - ~NonZero() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(NonZero, PrimitiveC); - explicit NonZero(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_NONZERO_H_ diff --git a/mindspore/lite/src/ops/not_equal.cc b/mindspore/lite/src/ops/not_equal.cc deleted file mode 100644 index 618025c400..0000000000 --- a/mindspore/lite/src/ops/not_equal.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/not_equal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int NotEqual::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateNotEqual(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_NotEqual, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *NotEqualCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry NotEqualRegistry(schema::PrimitiveType_NotEqual, NotEqualCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/not_equal.h b/mindspore/lite/src/ops/not_equal.h deleted file mode 100644 index 464d27d685..0000000000 --- a/mindspore/lite/src/ops/not_equal.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_compare.h" - -namespace mindspore { -namespace lite { -class NotEqual : public ArithmeticCompare { - public: - NotEqual() = default; - ~NotEqual() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(NotEqual, ArithmeticCompare); - explicit NotEqual(schema::PrimitiveT *primitive) : ArithmeticCompare(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ diff --git a/mindspore/lite/src/ops/one_hot.cc b/mindspore/lite/src/ops/one_hot.cc deleted file mode 100644 index c580134ba0..0000000000 --- a/mindspore/lite/src/ops/one_hot.cc +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/one_hot.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int OneHot::GetAxis() const { return this->primitive_->value.AsOneHot()->axis; } - -void OneHot::SetAxis(int axis) { this->primitive_->value.AsOneHot()->axis = axis; } - -int OneHot::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_OneHot; - } - if (this->primitive_->value.type != schema::PrimitiveType_OneHot) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::OneHotT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->axis = -1; - if (prim.GetAttr("axis") != nullptr) { - attr->axis = CastToInt(prim.GetAttr("axis")).front(); - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -int OneHot::GetAxis() const { return this->primitive_->value_as_OneHot()->axis(); } - -int OneHot::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_OneHot(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_OneHot return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateOneHot(*fbb, attr->axis()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_OneHot, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *OneHotCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry OneHotRegistry(schema::PrimitiveType_OneHot, OneHotCreator); -#endif - -namespace { -constexpr size_t kOneHotInputNum = 4; -constexpr size_t kOneHotInputNumOpt = 3; -} // namespace -int OneHot::InferShape(std::vector inputs, std::vector outputs) { - if (this->primitive_ == nullptr) { - return RET_NULL_PTR; - } - - int axis = GetAxis(); - // indices, depth, on_value, off_value - if (inputs.size() != kOneHotInputNum && inputs.size() != kOneHotInputNumOpt) { - MS_LOG(ERROR) << "OneHot got inputs num " << inputs.size() << ", should be " << kOneHotInputNum << " or " - << kOneHotInputNumOpt; - return RET_ERROR; - } - auto depth_tensor = inputs.at(1); - if (depth_tensor == nullptr) { - return RET_NULL_PTR; - } - const int *depth = static_cast(depth_tensor->MutableData()); - auto input = inputs.front(); - if (input == nullptr) { - return RET_NULL_PTR; - } - auto on_value = inputs.at(2); - if (on_value == nullptr) { - return RET_NULL_PTR; - } - auto output = outputs.front(); - if (output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(on_value->data_type()); - output->set_format(on_value->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - const auto input_shape = input->shape(); - int input_rank = static_cast(input_shape.size()); - if (axis < 0) { - axis += input_rank + 1; - } - std::vector output_shape(input_shape); - output_shape.insert(output_shape.cbegin() + axis, *depth); - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/one_hot.h b/mindspore/lite/src/ops/one_hot.h deleted file mode 100644 index 61b3dc522c..0000000000 --- a/mindspore/lite/src/ops/one_hot.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class OneHot : public PrimitiveC { - public: - OneHot() = default; - ~OneHot() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(OneHot, PrimitiveC); - explicit OneHot(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int axis); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetAxis() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ diff --git a/mindspore/lite/src/ops/oneslike.cc b/mindspore/lite/src/ops/oneslike.cc deleted file mode 100644 index f564195eb0..0000000000 --- a/mindspore/lite/src/ops/oneslike.cc +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/oneslike.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int OnesLike::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitive error"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_OnesLike; - } - if (this->primitive_->value.type != schema::PrimitiveType_OnesLike) { - MS_LOG(ERROR) << "PrimitiveType_OnesLike primitive value type : " - << schema::EnumNamePrimitiveType(primitive_->value.type) << "is not equal" - << schema::EnumNamePrimitiveType(schema::PrimitiveType_OnesLike); - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::OnesLikeT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int OnesLike::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_OnesLike(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_OnesLike return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateOnesLike(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_OnesLike, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *OnesLikeCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry OnesLikeRegistry(schema::PrimitiveType_OnesLike, OnesLikeCreator); -#endif -int OnesLike::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_.at(0); - Tensor *out = outputs_.at(0); - std::vector x_shape = x->shape(); - std::vector output_shape(x_shape.size()); - output_shape.assign(x_shape.begin(), x_shape.end()); - out->set_shape(output_shape); - out->set_format(x->format()); - out->set_data_type(x->data_type()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/oneslike.h b/mindspore/lite/src/ops/oneslike.h deleted file mode 100644 index e89095e0d4..0000000000 --- a/mindspore/lite/src/ops/oneslike.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include -#include -#include "src/ops/primitive_c.h" - -#ifndef LITE_SRC_OPS_ONESLIKE_H_ -#define LITE_SRC_OPS_ONESLIKE_H_ -namespace mindspore { -namespace lite { -class OnesLike : public PrimitiveC { - public: - OnesLike() = default; - ~OnesLike() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(OnesLike, PrimitiveC); - explicit OnesLike(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore -#endif // LITE_SRC_OPS_ONESLIKE_H_ diff --git a/mindspore/lite/src/ops/ops_def.cc b/mindspore/lite/src/ops/ops_def.cc index 720662a05a..590bf935e9 100644 --- a/mindspore/lite/src/ops/ops_def.cc +++ b/mindspore/lite/src/ops/ops_def.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,5 +13,1055 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "src/ops/ops_def.h" +#include "src/ops/ops_func_declare.h" -#include "src/ops/schema_def.h" +OP_TYPE_DEF_BEGIN(PrimitiveType) +OP_TYPE(Abs) +OP_TYPE(Activation) +OP_TYPE(ActivationGrad) +OP_TYPE(Adam) +OP_TYPE(AddFusion) +OP_TYPE(AdderFusion) +OP_TYPE(AddGrad) +OP_TYPE(AddN) +OP_TYPE(All) +OP_TYPE(ApplyMomentum) +OP_TYPE(ArgMaxFusion) +OP_TYPE(ArgMinFusion) +OP_TYPE(Assert) +OP_TYPE(Assign) +OP_TYPE(AssignAdd) +OP_TYPE(AudioSpectrogram) +OP_TYPE(AvgPoolFusion) +OP_TYPE(AvgPoolGrad) +OP_TYPE(BatchNorm) +OP_TYPE(BatchNormGrad) +OP_TYPE(BatchToSpace) +OP_TYPE(BatchToSpaceND) +OP_TYPE(BiasAdd) +OP_TYPE(BinaryCrossEntropy) +OP_TYPE(BinaryCrossEntropyGrad) +OP_TYPE(BiasAddGrad) +OP_TYPE(BroadcastTo) +OP_TYPE(Cast) +OP_TYPE(Ceil) +OP_TYPE(Clip) +OP_TYPE(Concat) +OP_TYPE(ControlDepend) +OP_TYPE(Conv2DBackpropFilterFusion) +OP_TYPE(Conv2DBackpropInputFusion) +OP_TYPE(Conv2DFusion) +OP_TYPE(Conv2dTransposeFusion) +OP_TYPE(Cos) +OP_TYPE(ConstantOfShape) +OP_TYPE(Crop) +OP_TYPE(CustomExtractFeatures) +OP_TYPE(CustomNormalize) +OP_TYPE(CustomPredict) +OP_TYPE(DeConv2DGradFilter) +OP_TYPE(Depend) +OP_TYPE(DepthToSpace) +OP_TYPE(DetectionPostProcess) +OP_TYPE(DivFusion) +OP_TYPE(DivGrad) +OP_TYPE(Dropout) +OP_TYPE(DropoutGrad) +OP_TYPE(Elu) +OP_TYPE(Eltwise) +OP_TYPE(Equal) +OP_TYPE(EmbeddingLookupFusion) +OP_TYPE(ExpFusion) +OP_TYPE(ExpandDims) +OP_TYPE(FakeQuantWithMinMaxVars) +OP_TYPE(FakeQuantWithMinMaxVarsPerChannel) +OP_TYPE(FftReal) +OP_TYPE(FftImag) +OP_TYPE(Flatten) +OP_TYPE(FlattenGrad) +OP_TYPE(Floor) +OP_TYPE(FloorDiv) +OP_TYPE(FloorMod) +OP_TYPE(Fill) +OP_TYPE(FullConnection) +OP_TYPE(FusedBatchNorm) +OP_TYPE(Gather) +OP_TYPE(GatherNd) +OP_TYPE(Greater) +OP_TYPE(GreaterEqual) +OP_TYPE(HashtableLookup) +OP_TYPE(InstanceNorm) +OP_TYPE(LayerNormFusion) +OP_TYPE(LeakyRelu) +OP_TYPE(Less) +OP_TYPE(LessEqual) +OP_TYPE(Log) +OP_TYPE(LogGrad) +OP_TYPE(LogicalAnd) +OP_TYPE(LogicalNot) +OP_TYPE(LogicalOr) +OP_TYPE(LpNormalization) +OP_TYPE(LRN) +OP_TYPE(LshProjection) +OP_TYPE(LSTM) +OP_TYPE(L2NormalizeFusion) +OP_TYPE(MatMul) +OP_TYPE(Maximum) +OP_TYPE(MaximumGrad) +OP_TYPE(MaxPoolFusion) +OP_TYPE(MaxPoolGrad) +OP_TYPE(Merge) +OP_TYPE(Mfcc) +OP_TYPE(Minimum) +OP_TYPE(MinimumGrad) +OP_TYPE(Mod) +OP_TYPE(MulFusion) +OP_TYPE(MulGrad) +OP_TYPE(Neg) +OP_TYPE(NegGrad) +OP_TYPE(NotEqual) +OP_TYPE(NonMaxSuppression) +OP_TYPE(OneHot) +OP_TYPE(OnesLike) +OP_TYPE(PadFusion) +OP_TYPE(PartialFusion) +OP_TYPE(PowerGrad) +OP_TYPE(PowFusion) +OP_TYPE(PriorBox) +OP_TYPE(PReLUFusion) +OP_TYPE(QuantDTypeCast) +OP_TYPE(Rank) +OP_TYPE(Range) +OP_TYPE(Reciprocal) +OP_TYPE(RealDiv) +OP_TYPE(ReduceFusion) +OP_TYPE(Reshape) +OP_TYPE(Resize) +OP_TYPE(ReverseSequence) +OP_TYPE(ReverseV2) +OP_TYPE(Rfft) +OP_TYPE(ROIPooling) +OP_TYPE(Round) +OP_TYPE(Rsqrt) +OP_TYPE(ScaleFusion) +OP_TYPE(ScatterNd) +OP_TYPE(SGD) +OP_TYPE(Shape) +OP_TYPE(SigmoidCrossEntropyWithLogits) +OP_TYPE(SigmoidCrossEntropyWithLogitsGrad) +OP_TYPE(Sin) +OP_TYPE(SkipGram) +OP_TYPE(SliceFusion) +OP_TYPE(SmoothL1Loss) +OP_TYPE(SmoothL1LossGrad) +OP_TYPE(Softmax) +OP_TYPE(SoftmaxCrossEntropyWithLogits) +OP_TYPE(SpaceToBatch) +OP_TYPE(SpaceToBatchND) +OP_TYPE(SpaceToDepth) +OP_TYPE(SparseSoftmaxCrossEntropy) +OP_TYPE(SparseToDense) +OP_TYPE(Split) +OP_TYPE(Sqrt) +OP_TYPE(Squeeze) +OP_TYPE(Square) +OP_TYPE(SquaredDifference) +OP_TYPE(Stack) +OP_TYPE(StridedSlice) +OP_TYPE(SubFusion) +OP_TYPE(SubGrad) +OP_TYPE(Switch) +OP_TYPE(TensorListFromTensor) +OP_TYPE(TensorListGetItem) +OP_TYPE(TensorListReserve) +OP_TYPE(TensorListSetItem) +OP_TYPE(TensorListStack) +OP_TYPE(TileFusion) +OP_TYPE(TopKFusion) +OP_TYPE(Transpose) +OP_TYPE(Unique) +OP_TYPE(UnsortedSegmentSum) +OP_TYPE(Unsqueeze) +OP_TYPE(Unstack) +OP_TYPE(While) +OP_TYPE(Where) +OP_TYPE(ZerosLike) +OP_TYPE(Select) +OP_TYPE(If) +OP_TYPE(Gru) +OP_TYPE(NonZero) +OP_TYPE(InvertPermutation) +OP_TYPE(Size) +OP_TYPE(RandomStandardNormal) +OP_TYPE(CropAndResize) +OP_TYPE(Erf) +OP_TYPE(StridedSliceGrad) +OP_TYPE(IsFinite) +OP_TYPE(LinSpace) +OP_TYPE(UniformReal) +OP_TYPE(AbsGrad) +OP_TYPE_DEF_END(PrimitiveType) + +OP_SCHEMA_DEF(Abs) +OP_SCHEMA_DEF_END(Abs) + +OP_SCHEMA_DEF(Activation) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_ATTR(alpha, float) +OP_ATTR(min_val, float) +OP_ATTR(max_val, float) +OP_SCHEMA_DEF_END(Activation) + +OP_SCHEMA_DEF(ActivationGrad) +OP_ATTR_ENUM(activation_type, ActivationType) +OP_ATTR(alpha, float) +OP_SCHEMA_DEF_END(ActivationGrad) + +OP_SCHEMA_DEF(Adam) +OP_ATTR(use_locking, bool) +OP_ATTR(use_nesterov, bool) +OP_SCHEMA_DEF_END(Adam) + +OP_SCHEMA_DEF(AddFusion) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(AddFusion) + +OP_SCHEMA_DEF(AdderFusion) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(kernel_size, [long]) +OP_ATTR(stride, [long]) +OP_ATTR(dilation, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR(pad_list, [long]) +OP_ATTR(group, long) +OP_ATTR(in_channel, long) +OP_ATTR(out_channel, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(AdderFusion) + +OP_SCHEMA_DEF(AddGrad) +OP_SCHEMA_DEF_END(AddGrad) + +OP_SCHEMA_DEF(AddN) +OP_SCHEMA_DEF_END(AddN) + +OP_SCHEMA_DEF(All) +OP_ATTR(keep_dims, long) +OP_SCHEMA_DEF_END(All) + +OP_SCHEMA_DEF(ApplyMomentum) +OP_ATTR(use_nesterov, bool) +OP_ATTR(use_locking, bool) +OP_ATTR(gradient_scale, float) +OP_SCHEMA_DEF_END(ApplyMomentum) + +OP_SCHEMA_DEF(ArgMaxFusion) +OP_ATTR(axis, long) +OP_ATTR_WITH_VALUE(top_k, long, 1) +OP_ATTR(keep_dims, bool) +OP_ATTR(out_max_value, bool) +OP_SCHEMA_DEF_END(ArgMaxFusion) + +OP_SCHEMA_DEF(ArgMinFusion) +OP_ATTR(axis, long) +OP_ATTR(top_k, long) +OP_ATTR(keep_dims, bool) +OP_ATTR(out_max_value, bool) +OP_SCHEMA_DEF_END(ArgMinFusion) + +OP_SCHEMA_DEF(Assert) +OP_ATTR(summarize, long) +OP_SCHEMA_DEF_END(Assert) + +OP_SCHEMA_DEF(Assign) +OP_SCHEMA_DEF_END(Assign) + +OP_SCHEMA_DEF(AssignAdd) +OP_SCHEMA_DEF_END(AssignAdd) + +OP_SCHEMA_DEF(AudioSpectrogram) +OP_ATTR(window_size, long) +OP_ATTR(stride, long) +OP_ATTR(mag_square, bool) +OP_SCHEMA_DEF_END(AudioSpectrogram) + +OP_SCHEMA_DEF(AvgPoolFusion) +OP_ATTR(kernel_size, [long]) +OP_ATTR(strides, [long]) +OP_ATTR(pad, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR_ENUM(round_mode, RoundMode) +OP_ATTR_ENUM(format, Format) +OP_ATTR(global, bool) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(AvgPoolFusion) + +OP_SCHEMA_DEF(AvgPoolGrad) +OP_ATTR(kernel_size, [long]) +OP_ATTR(strides, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR_ENUM(format, Format) +OP_SCHEMA_DEF_END(AvgPoolGrad) + +OP_SCHEMA_DEF(BatchNorm) +OP_ATTR(epsilon, float) +OP_ATTR_ENUM(format, Format) +OP_ATTR(is_training, bool) +OP_SCHEMA_DEF_END(BatchNorm) + +OP_SCHEMA_DEF(BatchNormGrad) +OP_ATTR(epsilon, float) +OP_SCHEMA_DEF_END(BatchNormGrad) + +OP_SCHEMA_DEF(BatchToSpace) +OP_ATTR(block_size, [long]) +OP_ATTR_VEC2D(crops, Vec2D); +OP_SCHEMA_DEF_END(BatchToSpace) + +OP_SCHEMA_DEF(BatchToSpaceND) +OP_ATTR(block_shape, [long]) +OP_ATTR_VEC2D(crops, Vec2D); +OP_SCHEMA_DEF_END(BatchToSpaceND) + +OP_SCHEMA_DEF(BiasAdd) +OP_ATTR_ENUM(format, Format) +OP_SCHEMA_DEF_END(BiasAdd) + +OP_SCHEMA_DEF(BinaryCrossEntropy) +OP_ATTR_ENUM(reduction, Reduction) +OP_SCHEMA_DEF_END(BinaryCrossEntropy) + +OP_SCHEMA_DEF(BinaryCrossEntropyGrad) +OP_ATTR_ENUM_WITH_VALUE(reduction, Reduction, 1) +OP_SCHEMA_DEF_END(BinaryCrossEntropyGrad) + +OP_SCHEMA_DEF(BiasAddGrad) +OP_SCHEMA_DEF_END(BiasAddGrad) + +OP_SCHEMA_DEF(BroadcastTo) +OP_ATTR(shape, [long]) +OP_SCHEMA_DEF_END(BroadcastTo) + +OP_SCHEMA_DEF(Cast) +OP_SCHEMA_DEF_END(Cast) + +OP_SCHEMA_DEF(Ceil) +OP_SCHEMA_DEF_END(Ceil) + +OP_SCHEMA_DEF(Clip) +OP_ATTR(max, float) +OP_ATTR(min, float) +OP_SCHEMA_DEF_END(Clip) + +OP_SCHEMA_DEF(Concat) +OP_ATTR(axis, long) +OP_SCHEMA_DEF_END(Concat) + +OP_SCHEMA_DEF(ControlDepend) +OP_ATTR(depend_mode, long) +OP_SCHEMA_DEF_END(ControlDepend) + +OP_SCHEMA_DEF(Conv2DBackpropFilterFusion) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(kernel_size, [long]) +OP_ATTR(stride, [long]) +OP_ATTR(dilation, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR(pad_list, [long]) +OP_ATTR(mode, long) +OP_ATTR(group, long) +OP_ATTR(in_channel, long) +OP_ATTR(out_channel, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(Conv2DBackpropFilterFusion) + +OP_SCHEMA_DEF(Conv2DBackpropInputFusion) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(kernel_size, [long]) +OP_ATTR(stride, [long]) +OP_ATTR(dilation, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR(pad, [long]) +OP_ATTR(pad_list, [long]) +OP_ATTR(mode, long) +OP_ATTR(group, long) +OP_ATTR(in_channel, long) +OP_ATTR(out_channel, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(Conv2DBackpropInputFusion) + +OP_SCHEMA_DEF(Conv2DFusion) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(kernel_size, [long]) +OP_ATTR(stride, [long]) +OP_ATTR(dilation, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR(pad_list, [long]) +OP_ATTR(mode, long) +OP_ATTR(group, long) +OP_ATTR(in_channel, long) +OP_ATTR(out_channel, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(Conv2DFusion) + +OP_SCHEMA_DEF(Conv2dTransposeFusion) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(kernel_size, [long]) +OP_ATTR(stride, [long]) +OP_ATTR(dilation, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR(pad, [long]) +OP_ATTR(pad_list, [long]) +OP_ATTR(mode, long) +OP_ATTR(group, long) +OP_ATTR(in_channel, long) +OP_ATTR(out_channel, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(Conv2dTransposeFusion) + +OP_SCHEMA_DEF(Cos) +OP_SCHEMA_DEF_END(Cos) + +OP_SCHEMA_DEF(ConstantOfShape) +OP_ATTR(data_type, long) +OP_ATTR(value, [float]) +OP_SCHEMA_DEF_END(ConstantOfShape) + +OP_SCHEMA_DEF(Crop) +OP_ATTR(axis, long) +OP_ATTR(offsets, [long]) +OP_SCHEMA_DEF_END(Crop) + +OP_SCHEMA_DEF(CustomExtractFeatures) +OP_SCHEMA_DEF_END(CustomExtractFeatures) + +OP_SCHEMA_DEF(CustomNormalize) +OP_SCHEMA_DEF_END(CustomNormalize) + +OP_SCHEMA_DEF(CustomPredict) +OP_ATTR(output_num, long) +OP_ATTR(weight_threshold, float) +OP_SCHEMA_DEF_END(CustomPredict) + +OP_SCHEMA_DEF(DeConv2DGradFilter) +OP_ATTR(in_channel, long); +OP_ATTR(out_channel, long); +OP_ATTR(kernel_size, [long]); +OP_ATTR_ENUM(pad_mode, PadMode); +OP_ATTR(pad_list, [long]); +OP_ATTR(stride, [long]); +OP_ATTR(dilation, [long]); +OP_ATTR(group, long); +OP_ATTR_ENUM(format, Format); +OP_ATTR_ENUM(activation_type, ActivationType); +OP_SCHEMA_DEF_END(DeConv2DGradFilter) + +OP_SCHEMA_DEF(Depend) +OP_SCHEMA_DEF_END(Depend) + +OP_SCHEMA_DEF(DepthToSpace) +OP_ATTR(block_size, long) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_SCHEMA_DEF_END(DepthToSpace) + +OP_SCHEMA_DEF(DetectionPostProcess) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR(input_size, long) +OP_ATTR(scale, [float]) +OP_ATTR(nms_iou_threshold, float) +OP_ATTR(nms_score_threshold, float) +OP_ATTR(max_detections, long) +OP_ATTR(detections_per_class, long) +OP_ATTR(max_classes_per_detection, long) +OP_ATTR(num_classes, long) +OP_ATTR(use_regular_nms, bool) +OP_ATTR(out_quantized, bool) +OP_SCHEMA_DEF_END(DetectionPostProcess) + +OP_SCHEMA_DEF(DivFusion) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(DivFusion) + +OP_SCHEMA_DEF(DivGrad) +OP_SCHEMA_DEF_END(DivGrad) + +OP_SCHEMA_DEF(Dropout) +OP_ATTR_WITH_VALUE(keep_prob, float, 0.5) +OP_SCHEMA_DEF_END(Dropout) + +OP_SCHEMA_DEF(DropoutGrad) +OP_ATTR(keep_prob, float) +OP_SCHEMA_DEF_END(DropoutGrad) + +OP_SCHEMA_DEF(Elu) +OP_ATTR(alpha, float) +OP_SCHEMA_DEF_END(Elu) + +OP_SCHEMA_DEF(Eltwise) +OP_ATTR_ENUM(mode, EltwiseMode) +OP_SCHEMA_DEF_END(Eltwise) + +OP_SCHEMA_DEF(Equal) +OP_SCHEMA_DEF_END(Equal) + +OP_SCHEMA_DEF(EmbeddingLookupFusion) +OP_ATTR(max_norm, float) +OP_SCHEMA_DEF_END(EmbeddingLookupFusion) + +OP_SCHEMA_DEF(ExpFusion) +OP_ATTR_WITH_VALUE(base, float, -1) +OP_ATTR_WITH_VALUE(scale, float, 1.0) +OP_ATTR_WITH_VALUE(shift, float, 0.0) +OP_SCHEMA_DEF_END(ExpFusion) + +OP_SCHEMA_DEF(ExpandDims) +OP_SCHEMA_DEF_END(ExpandDims) + +OP_SCHEMA_DEF(FakeQuantWithMinMaxVars) +OP_ATTR(num_bits, long) +OP_ATTR(narrow_range, bool) +OP_SCHEMA_DEF_END(FakeQuantWithMinMaxVars) + +OP_SCHEMA_DEF(FakeQuantWithMinMaxVarsPerChannel) +OP_ATTR(num_bits, long) +OP_ATTR(narrow_range, bool) +OP_SCHEMA_DEF_END(FakeQuantWithMinMaxVarsPerChannel) + +OP_SCHEMA_DEF(FftReal) +OP_SCHEMA_DEF_END(FftReal) + +OP_SCHEMA_DEF(FftImag) +OP_SCHEMA_DEF_END(FftImag) + +OP_SCHEMA_DEF(Flatten) +OP_SCHEMA_DEF_END(Flatten) + +OP_SCHEMA_DEF(FlattenGrad) +OP_SCHEMA_DEF_END(FlattenGrad) + +OP_SCHEMA_DEF(Floor) +OP_SCHEMA_DEF_END(Floor) + +OP_SCHEMA_DEF(FloorDiv) +OP_SCHEMA_DEF_END(FloorDiv) + +OP_SCHEMA_DEF(FloorMod) +OP_SCHEMA_DEF_END(FloorMod) + +OP_SCHEMA_DEF(Fill) +OP_SCHEMA_DEF_END(Fill) + +OP_SCHEMA_DEF(FullConnection) +OP_ATTR(has_bias, bool) +OP_ATTR(use_axis, bool) +OP_ATTR(axis, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(FullConnection) + +OP_SCHEMA_DEF(FusedBatchNorm) +OP_ATTR_WITH_VALUE(epsilon, float, 0.0001) +OP_ATTR_WITH_VALUE(momentum, float, 0.9) +OP_ATTR(mode, long) +OP_SCHEMA_DEF_END(FusedBatchNorm) + +OP_SCHEMA_DEF(Gather) +OP_SCHEMA_DEF_END(Gather) + +OP_SCHEMA_DEF(GatherNd) +OP_SCHEMA_DEF_END(GatherNd) + +OP_SCHEMA_DEF(Greater) +OP_SCHEMA_DEF_END(Greater) + +OP_SCHEMA_DEF(GreaterEqual) +OP_SCHEMA_DEF_END(GreaterEqual) + +OP_SCHEMA_DEF(HashtableLookup) +OP_SCHEMA_DEF_END(HashtableLookup) + +OP_SCHEMA_DEF(InstanceNorm) +OP_ATTR(epsilon, float) +OP_SCHEMA_DEF_END(InstanceNorm) + +OP_SCHEMA_DEF(LayerNormFusion) +OP_ATTR(begin_norm_axis, long) +OP_ATTR_WITH_VALUE(epsilon, float, 0.00001) +OP_ATTR(elementwise_affine, bool) +OP_ATTR(begin_params_axis, long) +OP_SCHEMA_DEF_END(LayerNormFusion) + +OP_SCHEMA_DEF(LeakyRelu) +OP_ATTR(negative_slope, float) +OP_SCHEMA_DEF_END(LeakyRelu) + +OP_SCHEMA_DEF(Less) +OP_SCHEMA_DEF_END(Less) + +OP_SCHEMA_DEF(LessEqual) +OP_SCHEMA_DEF_END(LessEqual) + +OP_SCHEMA_DEF(Log) +OP_SCHEMA_DEF_END(Log) + +OP_SCHEMA_DEF(LogGrad) +OP_SCHEMA_DEF_END(LogGrad) + +OP_SCHEMA_DEF(LogicalAnd) +OP_SCHEMA_DEF_END(LogicalAnd) + +OP_SCHEMA_DEF(LogicalNot) +OP_SCHEMA_DEF_END(LogicalNot) + +OP_SCHEMA_DEF(LogicalOr) +OP_SCHEMA_DEF_END(LogicalOr) + +OP_SCHEMA_DEF(LpNormalization) +OP_ATTR(axis, long) +OP_ATTR(p, long) +OP_SCHEMA_DEF_END(LpNormalization) + +OP_SCHEMA_DEF(LRN) +OP_ATTR(depth_radius, long) +OP_ATTR(bias, float) +OP_ATTR(alpha, float) +OP_ATTR(beta, float) +OP_ATTR(norm_region, string) +OP_SCHEMA_DEF_END(LRN) + +OP_SCHEMA_DEF(LshProjection) +OP_ATTR_ENUM(type, LshProjectionType) +OP_SCHEMA_DEF_END(LshProjection) + +OP_SCHEMA_DEF(LSTM) +OP_ATTR(bidirectional, bool) +OP_ATTR(has_bias, bool) +OP_ATTR(input_size, long) +OP_ATTR(hidden_size, long) +OP_ATTR(num_layers, long) +OP_ATTR(num_directions, long) +OP_ATTR(dropout, float) +OP_ATTR_WITH_VALUE(zoneout_cell, float, 0) +OP_ATTR_WITH_VALUE(zoneout_hidden, float, 0) +OP_SCHEMA_DEF_END(LSTM) + +OP_SCHEMA_DEF(L2NormalizeFusion) +OP_ATTR(axis, [long]) +OP_ATTR(epsilon, float) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(L2NormalizeFusion) + +OP_SCHEMA_DEF(MatMul) +OP_ATTR_WITH_VALUE(transpose_a, bool, false) +OP_ATTR_WITH_VALUE(transpose_b, bool, false) +OP_SCHEMA_DEF_END(MatMul) + +OP_SCHEMA_DEF(Maximum) +OP_SCHEMA_DEF_END(Maximum) + +OP_SCHEMA_DEF(MaximumGrad) +OP_ATTR(grad_x, bool) +OP_ATTR(grad_y, bool) +OP_SCHEMA_DEF_END(MaximumGrad) + +OP_SCHEMA_DEF(MaxPoolFusion) +OP_ATTR(kernel_size, [long]) +OP_ATTR(strides, [long]) +OP_ATTR(pad, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR_ENUM(round_mode, RoundMode) +OP_ATTR_ENUM(format, Format) +OP_ATTR(global, bool) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(MaxPoolFusion) + +OP_SCHEMA_DEF(MaxPoolGrad) +OP_ATTR(kernel_size, [long]) +OP_ATTR(strides, [long]) +OP_ATTR_ENUM(pad_mode, PadMode) +OP_ATTR_ENUM(format, Format) +OP_SCHEMA_DEF_END(MaxPoolGrad) + +OP_SCHEMA_DEF(Merge) +OP_SCHEMA_DEF_END(Merge) + +OP_SCHEMA_DEF(Mfcc) +OP_ATTR(freq_upper_limit, float) +OP_ATTR(freq_lower_limit, float) +OP_ATTR(filter_bank_channel_num, long) +OP_ATTR(dct_coeff_num, long) +OP_SCHEMA_DEF_END(Mfcc) + +OP_SCHEMA_DEF(Minimum) +OP_SCHEMA_DEF_END(Minimum) + +OP_SCHEMA_DEF(MinimumGrad) +OP_ATTR(grad_x, bool) +OP_ATTR(grad_y, bool) +OP_SCHEMA_DEF_END(MinimumGrad) + +OP_SCHEMA_DEF(Mod) +OP_SCHEMA_DEF_END(Mod) + +OP_SCHEMA_DEF(MulFusion) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(MulFusion) + +OP_SCHEMA_DEF(MulGrad) +OP_SCHEMA_DEF_END(MulGrad) + +OP_SCHEMA_DEF(Neg) +OP_SCHEMA_DEF_END(Neg) + +OP_SCHEMA_DEF(NegGrad) +OP_SCHEMA_DEF_END(NegGrad) + +OP_SCHEMA_DEF(NotEqual) +OP_SCHEMA_DEF_END(NotEqual) + +OP_SCHEMA_DEF(NonMaxSuppression) +OP_ATTR(center_point_box, long) +OP_SCHEMA_DEF_END(NonMaxSuppression) + +OP_SCHEMA_DEF(OneHot) +OP_ATTR(axis, long) +OP_SCHEMA_DEF_END(OneHot) + +OP_SCHEMA_DEF(OnesLike) +OP_SCHEMA_DEF_END(OnesLike) + +OP_SCHEMA_DEF(PadFusion) +OP_ATTR_VEC2D(paddings, Vec2D); +OP_ATTR_ENUM(padding_mode, PaddingMode) +OP_ATTR(constant_value, float) +OP_SCHEMA_DEF_END(PadFusion) + +OP_SCHEMA_DEF(PartialFusion) +OP_ATTR(sub_graph_index, long) +OP_SCHEMA_DEF_END(PartialFusion) + +OP_SCHEMA_DEF(PowerGrad) +OP_ATTR(power, float) +OP_ATTR(scale, float) +OP_ATTR(shift, float) +OP_SCHEMA_DEF_END(PowerGrad) + +OP_SCHEMA_DEF(PowFusion) +OP_ATTR_WITH_VALUE(scale, float, 1) +OP_ATTR_WITH_VALUE(shift, float, 0) +OP_SCHEMA_DEF_END(PowFusion) + +OP_SCHEMA_DEF(PriorBox) +OP_ATTR(min_sizes, [long]) +OP_ATTR(max_sizes, [long]) +OP_ATTR(aspect_ratios, [float]) +OP_ATTR(variances, [float]) +OP_ATTR(image_size_w, long) +OP_ATTR(image_size_h, long) +OP_ATTR(step_w, float) +OP_ATTR(step_h, float) +OP_ATTR(clip, bool) +OP_ATTR(flip, bool) +OP_ATTR(offset, float) +OP_SCHEMA_DEF_END(PriorBox) + +OP_SCHEMA_DEF(PReLUFusion) +OP_ATTR(channel_shared, bool) +OP_SCHEMA_DEF_END(PReLUFusion) + +OP_SCHEMA_DEF(Rank) +OP_SCHEMA_DEF_END(Rank) + +OP_SCHEMA_DEF(Range) +OP_ATTR(d_type, long) +OP_ATTR(start, long) +OP_ATTR(limit, long) +OP_ATTR_WITH_VALUE(delta, long, 1) +OP_SCHEMA_DEF_END(Range) + +OP_SCHEMA_DEF(Reciprocal) +OP_SCHEMA_DEF_END(Reciprocal) + +OP_SCHEMA_DEF(RealDiv) +OP_SCHEMA_DEF_END(RealDiv) + +OP_SCHEMA_DEF(ReduceFusion) +OP_ATTR(keep_dims, bool) +OP_ATTR_ENUM(mode, ReduceMode) +OP_ATTR(reduce_to_end, bool) +OP_ATTR(coeff, float) +OP_SCHEMA_DEF_END(ReduceFusion) + +OP_SCHEMA_DEF(Reshape) +OP_SCHEMA_DEF_END(Reshape) + +OP_SCHEMA_DEF(Resize) +OP_ATTR_ENUM_WITH_VALUE(format, Format, 0) +OP_ATTR_ENUM(method, ResizeMethod) +OP_ATTR(new_height, long) +OP_ATTR(new_width, long) +OP_ATTR_WITH_VALUE(preserve_aspect_ratio, bool, false) +OP_ATTR_ENUM(coordinate_transform_mode, CoordinateTransformMode) +OP_ATTR(cubic_coeff, float) +OP_ATTR(exclude_outside, long) +OP_ATTR(extrapolation_value, float) +OP_ATTR_ENUM(nearest_mode, NearestMode) +OP_SCHEMA_DEF_END(Resize) + +OP_SCHEMA_DEF(ReverseSequence) +OP_ATTR(seq_dim, long) +OP_ATTR(batch_dim, long) +OP_SCHEMA_DEF_END(ReverseSequence) + +OP_SCHEMA_DEF(ReverseV2) +OP_ATTR(axis, [long]) +OP_SCHEMA_DEF_END(ReverseV2) + +OP_SCHEMA_DEF(Rfft) +OP_ATTR(fft_length, long) +OP_SCHEMA_DEF_END(Rfft) + +OP_SCHEMA_DEF(ROIPooling) +OP_ATTR(pooled_h, long) +OP_ATTR(pooled_w, long) +OP_ATTR(scale, float) +OP_SCHEMA_DEF_END(ROIPooling) + +OP_SCHEMA_DEF(Round) +OP_SCHEMA_DEF_END(Round) + +OP_SCHEMA_DEF(Rsqrt) +OP_SCHEMA_DEF_END(Rsqrt) + +OP_SCHEMA_DEF(QuantDTypeCast) +OP_ATTR(src_t, long) +OP_ATTR(dst_t, long) +OP_SCHEMA_DEF_END(QuantDTypeCast) + +OP_SCHEMA_DEF(ScaleFusion) +OP_ATTR(axis, long) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(ScaleFusion) + +OP_SCHEMA_DEF(ScatterNd) +OP_SCHEMA_DEF_END(ScatterNd) + +OP_SCHEMA_DEF(SGD) +OP_ATTR(nesterov, bool) +OP_ATTR(dampening, float) +OP_ATTR(weight_decay, float) +OP_SCHEMA_DEF_END(SGD) + +OP_SCHEMA_DEF(Shape) +OP_SCHEMA_DEF_END(Shape) + +OP_SCHEMA_DEF(SigmoidCrossEntropyWithLogits) +OP_SCHEMA_DEF_END(SigmoidCrossEntropyWithLogits) + +OP_SCHEMA_DEF(SigmoidCrossEntropyWithLogitsGrad) +OP_SCHEMA_DEF_END(SigmoidCrossEntropyWithLogitsGrad) + +OP_SCHEMA_DEF(Sin) +OP_SCHEMA_DEF_END(Sin) + +OP_SCHEMA_DEF(SkipGram) +OP_ATTR(include_all_grams, bool) +OP_ATTR(max_skip_size, long) +OP_ATTR(ngram_size, long) +OP_SCHEMA_DEF_END(SkipGram) + +OP_SCHEMA_DEF(SliceFusion) +OP_ATTR(axes, [long]) +OP_SCHEMA_DEF_END(SliceFusion) + +OP_SCHEMA_DEF(SmoothL1Loss) +OP_ATTR(beta, float) +OP_SCHEMA_DEF_END(SmoothL1Loss) + +OP_SCHEMA_DEF(SmoothL1LossGrad) +OP_ATTR(beta, float) +OP_SCHEMA_DEF_END(SmoothL1LossGrad) + +OP_SCHEMA_DEF(Softmax) +OP_ATTR(axis, [long]) +OP_SCHEMA_DEF_END(Softmax) + +OP_SCHEMA_DEF(SoftmaxCrossEntropyWithLogits) +OP_SCHEMA_DEF_END(SoftmaxCrossEntropyWithLogits) + +OP_SCHEMA_DEF(SpaceToBatch) +OP_ATTR(block_size, [long]) +OP_ATTR_VEC2D(paddings, Vec2D); +OP_SCHEMA_DEF_END(SpaceToBatch) + +OP_SCHEMA_DEF(SpaceToBatchND) +OP_ATTR(block_shape, [long]) +OP_ATTR_VEC2D(paddings, Vec2D); +OP_SCHEMA_DEF_END(SpaceToBatchND) + +OP_SCHEMA_DEF(SpaceToDepth) +OP_ATTR(block_size, long) +OP_ATTR_ENUM(format, Format) +OP_SCHEMA_DEF_END(SpaceToDepth) + +OP_SCHEMA_DEF(SparseSoftmaxCrossEntropy) +OP_ATTR(grad, bool) +OP_SCHEMA_DEF_END(SparseSoftmaxCrossEntropy) + +OP_SCHEMA_DEF(SparseToDense) +OP_SCHEMA_DEF_END(SparseToDense) + +OP_SCHEMA_DEF(Split) +OP_ATTR(output_num, long) +OP_ATTR(size_splits, [long]) +OP_ATTR(axis, long) +OP_SCHEMA_DEF_END(Split) + +OP_SCHEMA_DEF(Sqrt) +OP_SCHEMA_DEF_END(Sqrt) + +OP_SCHEMA_DEF(Squeeze) +OP_ATTR(axis, [long]) +OP_SCHEMA_DEF_END(Squeeze) + +OP_SCHEMA_DEF(Square) +OP_SCHEMA_DEF_END(Square) + +OP_SCHEMA_DEF(SquaredDifference) +OP_SCHEMA_DEF_END(SquaredDifference) + +OP_SCHEMA_DEF(Stack) +OP_ATTR(axis, long) +OP_SCHEMA_DEF_END(Stack) + +OP_SCHEMA_DEF(StridedSlice) +OP_ATTR(begin_mask, long) +OP_ATTR(end_mask, long) +OP_ATTR(ellipsis_mask, long) +OP_ATTR(new_axis_mask, long) +OP_ATTR(shrink_axis_mask, long) +OP_SCHEMA_DEF_END(StridedSlice) + +OP_SCHEMA_DEF(SubFusion) +OP_ATTR_ENUM_WITH_VALUE(activation_type, ActivationType, 0) +OP_SCHEMA_DEF_END(SubFusion) + +OP_SCHEMA_DEF(SubGrad) +OP_SCHEMA_DEF_END(SubGrad) + +OP_SCHEMA_DEF(Switch) +OP_SCHEMA_DEF_END(Switch) + +OP_SCHEMA_DEF(TensorListFromTensor) +OP_ATTR(element_dtype, long) +OP_ATTR(shape_type, long) +OP_SCHEMA_DEF_END(TensorListFromTensor) + +OP_SCHEMA_DEF(TensorListGetItem) +OP_ATTR(element_dtype, long) +OP_SCHEMA_DEF_END(TensorListGetItem) + +OP_SCHEMA_DEF(TensorListReserve) +OP_ATTR(element_dtype, long) +OP_ATTR(shape_type, long) +OP_SCHEMA_DEF_END(TensorListReserve) + +OP_SCHEMA_DEF(TensorListSetItem) +OP_ATTR(element_dtype, long) +OP_SCHEMA_DEF_END(TensorListSetItem) + +OP_SCHEMA_DEF(TensorListStack) +OP_ATTR(num_elements, long) +OP_ATTR(element_dtype, long) +OP_SCHEMA_DEF_END(TensorListStack) + +OP_SCHEMA_DEF(TileFusion) +OP_ATTR(dims, [long]) +OP_SCHEMA_DEF_END(TileFusion) + +OP_SCHEMA_DEF(TopKFusion) +OP_ATTR_WITH_VALUE(sorted, bool, true) +OP_ATTR(axis, long) +OP_ATTR(largest, long) +OP_SCHEMA_DEF_END(TopKFusion) + +OP_SCHEMA_DEF(Transpose) +OP_SCHEMA_DEF_END(Transpose) + +OP_SCHEMA_DEF(Unique) +OP_SCHEMA_DEF_END(Unique) + +OP_SCHEMA_DEF(UnsortedSegmentSum) +OP_SCHEMA_DEF_END(UnsortedSegmentSum) + +OP_SCHEMA_DEF(Unsqueeze) +OP_ATTR(axis, [long]) +OP_SCHEMA_DEF_END(Unsqueeze) + +OP_SCHEMA_DEF(Unstack) +OP_ATTR_WITH_VALUE(axis, long, 0) +OP_SCHEMA_DEF_END(Unstack) + +OP_SCHEMA_DEF(While) +OP_ATTR(cond_subgraph_index, long) +OP_ATTR(body_subgraph_index, long) +OP_SCHEMA_DEF_END(While) + +OP_SCHEMA_DEF(Where) +OP_SCHEMA_DEF_END(Where) + +OP_SCHEMA_DEF(ZerosLike) +OP_SCHEMA_DEF_END(ZerosLike) + +OP_SCHEMA_DEF(Select) +OP_SCHEMA_DEF_END(Select) + +OP_SCHEMA_DEF(If) +OP_SCHEMA_DEF_END(If) + +OP_SCHEMA_DEF(GRU) +OP_ATTR_WITH_VALUE(bidirectional, bool, false) +OP_SCHEMA_DEF_END(GRU) + +OP_SCHEMA_DEF(NonZero) +OP_SCHEMA_DEF_END(NonZero) + +OP_SCHEMA_DEF(InvertPermutation) +OP_SCHEMA_DEF_END(InvertPermutation) + +OP_SCHEMA_DEF(Size) +OP_SCHEMA_DEF_END(Size) + +OP_SCHEMA_DEF(RandomStandardNormal) +OP_ATTR(seed, long) +OP_ATTR(seed2, long) +OP_SCHEMA_DEF_END(RandomStandardNormal) + +OP_SCHEMA_DEF(CropAndResize) +OP_ATTR_ENUM(method, ResizeMethod) +OP_ATTR(extrapolation_value, float) +OP_SCHEMA_DEF_END(CropAndResize) + +OP_SCHEMA_DEF(Erf) +OP_SCHEMA_DEF_END(Erf) + +OP_SCHEMA_DEF(StridedSliceGrad) +OP_ATTR(begin_mask, long) +OP_ATTR(end_mask, long) +OP_ATTR(ellipsis_mask, long) +OP_ATTR(new_axis_mask, long) +OP_ATTR(shrink_axis_mask, long) +OP_SCHEMA_DEF_END(StridedSliceGrad) + +OP_SCHEMA_DEF(IsFinite) +OP_SCHEMA_DEF_END(IsFinite) + +OP_SCHEMA_DEF(LinSpace) +OP_SCHEMA_DEF_END(LinSpace) + +OP_SCHEMA_DEF(UniformReal) +OP_ATTR(seed, long) +OP_ATTR(seed2, long) +OP_SCHEMA_DEF_END(UniformReal) + +OP_SCHEMA_DEF(AbsGrad) +OP_SCHEMA_DEF_END(AbsGrad) diff --git a/mindspore/lite/src/ops/ops_def.h b/mindspore/lite/src/ops/ops_def.h new file mode 100644 index 0000000000..9f7c1f875b --- /dev/null +++ b/mindspore/lite/src/ops/ops_def.h @@ -0,0 +1,157 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_SRC_OPS_OPS_DEF_H_ +#define MINDSPORE_LITE_SRC_OPS_OPS_DEF_H_ +#include +#include +#include +#include +#include "src/ops/ops_func_declare.h" +#include "src/ops/schema_register.h" + +#ifdef PRIMITIVE_WRITEABLE +#include "mindspore/core/utils/check_convert_utils.h" +#include "schema/inner/model_generated.h" +#include "schema/inner/ops_types_generated.h" +#endif + +#ifdef GEN_SCHEMA_DEF +#define OP_TYPE_DEF_BEGIN(type) \ + namespace mindspore::lite::ops { \ + std::string Gen##type() { \ + std::string prims_type = "union "; \ + prims_type.append(#type).append(" {\n"); + +#define OP_TYPE(OP) prims_type.append(" ").append(#OP).append(",\n"); + +#define OP_TYPE_DEF_END(type) \ + prims_type.append("}\n\n"); \ + return prims_type; \ + } \ + PrimitiveTypeRegister g_gen##type(Gen##type); \ + } // namespace mindspore::lite::ops +#else +#define OP_TYPE_DEF_BEGIN(type) +#define OP_TYPE(OP) +#define OP_TYPE_DEF_END(type) +#endif + +#ifdef GEN_SCHEMA_DEF +#define OP_SCHEMA_DEF(OP) \ + namespace mindspore::lite::ops { \ + std::string Gen##OP##Def() { \ + std::string op_def = "table "; \ + op_def.append(#OP); \ + op_def.append(" {\n"); + +#elif PRIMITIVE_WRITEABLE +#define OP_SCHEMA_DEF(OP) \ + namespace mindspore::lite::ops { \ + mindspore::schema::PrimitiveT *MSOp2SchemaOp(const mindspore::ops::OP *op) { \ + mindspore::schema::OP##T *schema_op = new (std::nothrow) mindspore::schema::OP##T(); +#else +#define OP_SCHEMA_DEF(OP) +#endif + +#ifdef GEN_SCHEMA_DEF +#define OP_ATTR(key, type) op_def.append(" ").append(#key).append(": ").append(#type).append(";\n"); +#define OP_ATTR_ENUM(key, type) op_def.append(" ").append(#key).append(": ").append(#type).append(";\n"); +#define OP_ATTR_VEC2D(key, type) op_def.append(" ").append(#key).append(": ").append(#type).append(";\n"); +#elif PRIMITIVE_WRITEABLE +#define OP_ATTR(key, type) \ + if (schema_op != nullptr) { \ + if (op->GetAttr(#key) != nullptr) { \ + schema_op->key = op->get_##key(); \ + } \ + } else { \ + return nullptr; \ + } + +#define OP_ATTR_ENUM(key, type) \ + if (schema_op != nullptr) { \ + if (op->GetAttr(#key) != nullptr) { \ + schema_op->key = static_cast(op->get_##key()); \ + } \ + } + +#define OP_ATTR_VEC2D(key, type) \ + if (schema_op != nullptr) { \ + auto vec2d = std::make_unique(); \ + if (op->GetAttr(#key) != nullptr) { \ + auto data = op->get_##key(); \ + for (size_t i = 0; i < data.size(); ++i) { \ + auto vec = std::make_unique(); \ + vec->data.assign(data.at(i).begin(), data.at(i).end()); \ + vec2d->data.push_back(std::move(vec)); \ + } \ + schema_op->key = std::move(vec2d); \ + } \ + } + +#else +#define OP_ATTR(key, type) +#define OP_ATTR_ENUM(key, type) +#define OP_ATTR_VEC2D(key, type) +#endif + +#ifdef GEN_SCHEMA_DEF +#define OP_ATTR_WITH_VALUE(key, type, value) \ + op_def.append(" ").append(#key).append(": ").append(#type).append(" = ").append(#value).append(";\n"); +#define OP_ATTR_ENUM_WITH_VALUE(key, type, value) \ + op_def.append(" ").append(#key).append(": ").append(#type).append(" = ").append(#value).append(";\n"); +#elif PRIMITIVE_WRITEABLE +#define OP_ATTR_WITH_VALUE(key, type, value) \ + if (schema_op != nullptr) { \ + if (op->GetAttr(#key) != nullptr) { \ + schema_op->key = op->get_##key(); \ + } \ + } else { \ + return nullptr; \ + } + +#define OP_ATTR_ENUM_WITH_VALUE(key, type, value) \ + if (schema_op != nullptr) { \ + if (op->GetAttr(#key) != nullptr) { \ + schema_op->key = static_cast(op->get_##key()); \ + } \ + } +#else +#define OP_ATTR_WITH_VALUE(key, type, value) +#define OP_ATTR_ENUM_WITH_VALUE(key, type, value) +#endif + +#ifdef GEN_SCHEMA_DEF +#define OP_SCHEMA_DEF_END(OP) \ + op_def.append("}\n\n"); \ + return op_def; \ + } \ + SchemaOpRegister g_schema_op_##OP(Gen##OP##Def); \ + } // namespace mindspore::lite::ops +#elif PRIMITIVE_WRITEABLE +#define OP_SCHEMA_DEF_END(OP) \ + schema::PrimitiveT *prim = new (std::nothrow) schema::PrimitiveT(); \ + if (prim == nullptr) { \ + return nullptr; \ + } \ + prim->value.value = schema_op; \ + prim->value.type = schema::PrimitiveType_##OP; \ + return prim; \ + } \ + } // namespace mindspore::lite::ops +#else +#define OP_SCHEMA_DEF_END(OP) +#endif +#endif // MINDSPORE_LITE_SRC_OPS_OPS_DEF_H_ diff --git a/mindspore/lite/src/ops/ops_func_declare.h b/mindspore/lite/src/ops/ops_func_declare.h new file mode 100644 index 0000000000..d0525d3744 --- /dev/null +++ b/mindspore/lite/src/ops/ops_func_declare.h @@ -0,0 +1,453 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_SRC_OPS_OPS_FUNC_DECLARE_H_ +#define MINDSPORE_LITE_SRC_OPS_OPS_FUNC_DECLARE_H_ + +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#include "ops/abs.h" +#include "ops/adam.h" +#include "ops/add.h" +#include "ops/adder.h" +#include "ops/addn.h" +#include "ops/all.h" +#include "ops/apply_momentum.h" +#include "ops/arg_max.h" +#include "ops/arg_min.h" +#include "ops/asin.h" +#include "ops/assert.h" +#include "ops/assign.h" +#include "ops/assign_add.h" +#include "ops/atan.h" +#include "ops/audio_spectrogram.h" +#include "ops/avg_pool.h" +#include "ops/batch_norm.h" +#include "ops/batch_to_space.h" +#include "ops/batch_to_space_nd.h" +#include "ops/bias_add.h" +#include "ops/binary_cross_entropy.h" +#include "ops/black_box.h" +#include "ops/broadcast_to.h" +#include "ops/broadcast.h" +#include "ops/cast.h" +#include "ops/ceil.h" +#include "ops/clip.h" +#include "ops/custom.h" +#include "ops/custom_normalize.h" +#include "ops/custom_predict.h" +#include "ops/custom_extract_features.h" +#include "ops/concat.h" +#include "ops/constant.h" +#include "ops/constant_of_shape.h" +#include "ops/control_depend.h" +#include "ops/cos.h" +#include "ops/crop.h" +#include "ops/depth_to_space.h" +#include "ops/depend.h" +#include "ops/detection_post_process.h" +#include "ops/div.h" +#include "ops/dropout.h" +#include "ops/eltwise.h" +#include "ops/elu.h" +#include "ops/embedding_lookup.h" +#include "ops/equal.h" +#include "ops/expand_dims.h" +#include "ops/exp.h" +#include "ops/fake_quant_with_min_max_vars.h" +#include "ops/fake_quant_with_min_max_vars_per_channel.h" +#include "ops/fft_imag.h" +#include "ops/fft_real.h" +#include "ops/fill.h" +#include "ops/flatten.h" +#include "ops/floor.h" +#include "ops/floor_div.h" +#include "ops/floor_mod.h" +#include "ops/fused_batch_norm.h" +#include "ops/gather.h" +#include "ops/gather_nd.h" +#include "ops/greater_equal.h" +#include "ops/greater.h" +#include "ops/hashtable_lookup.h" +#include "ops/instance_norm.h" +#include "ops/l2_normalize.h" +#include "ops/layer_norm.h" +#include "ops/leaky_relu.h" +#include "ops/less.h" +#include "ops/less_equal.h" +#include "ops/log.h" +#include "ops/logical_and.h" +#include "ops/logical_not.h" +#include "ops/logical_or.h" +#include "ops/logical_xor.h" +#include "ops/loop.h" +#include "ops/lp_normalization.h" +#include "ops/lrn.h" +#include "ops/lsh_projection.h" +#include "ops/lstm.h" +#include "ops/make_tuple.h" +#include "ops/mat_mul.h" +#include "ops/matrix_diag.h" +#include "ops/max_pool.h" +#include "ops/maximum.h" +#include "ops/merge.h" +#include "ops/mfcc.h" +#include "ops/minimum.h" +#include "ops/mod.h" +#include "ops/mul.h" +#include "ops/neg.h" +#include "ops/net_output.h" +#include "ops/non_max_suppression.h" +#include "ops/not_equal.h" +#include "ops/one_hot.h" +#include "ops/ones_like.h" +#include "ops/pad.h" +#include "ops/permute.h" +#include "ops/prelu.h" +#include "ops/prior_box.h" +#include "ops/proposal.h" +#include "ops/quant_dtype_cast.h" +#include "ops/range.h" +#include "ops/rank.h" +#include "ops/real_div.h" +#include "ops/reciprocal.h" +#include "ops/reduce.h" +#include "ops/relu6.h" +#include "ops/reshape.h" +#include "ops/resize.h" +#include "ops/return.h" +#include "ops/reverse_sequence.h" +#include "ops/reverse_v2.h" +#include "ops/rfft.h" +#include "ops/roi_pooling.h" +#include "ops/round.h" +#include "ops/rsqrt.h" +#include "ops/scale.h" +#include "ops/scatter_nd.h" +#include "ops/select.h" +#include "ops/sgd.h" +#include "ops/shape.h" +#include "ops/sigmoid.h" +#include "ops/sigmoid_cross_entropy_with_logits.h" +#include "ops/sin.h" +#include "ops/skip_gram.h" +#include "ops/smooth_l1_loss.h" +#include "ops/softmax.h" +#include "ops/softmax_cross_entropy_with_logits.h" +#include "ops/space_to_batch.h" +#include "ops/space_to_batch_nd.h" +#include "ops/space_to_depth.h" +#include "ops/sparse_softmax_cross_entropy.h" +#include "ops/sparse_to_dense.h" +#include "ops/split.h" +#include "ops/square.h" +#include "ops/squeeze.h" +#include "ops/sqrt.h" +#include "ops/squared_difference.h" +#include "ops/stack.h" +#include "ops/strided_slice.h" +#include "ops/sub.h" +#include "ops/switch.h" +#include "ops/tan.h" +#include "ops/tanh.h" +#include "ops/tensor_list_from_tensor.h" +#include "ops/tensor_list_get_item.h" +#include "ops/tensor_list_reserve.h" +#include "ops/tensor_list_set_item.h" +#include "ops/tensor_list_stack.h" +#include "ops/tile.h" +#include "ops/transpose.h" +#include "ops/tuple_get_item.h" +#include "ops/unique.h" +#include "ops/unstack.h" +#include "ops/unsqueeze.h" +#include "ops/unsorted_segment_sum.h" +#include "ops/where.h" +#include "ops/while.h" +#include "ops/zeros_like.h" +#include "ops/grad/activation_grad.h" +#include "ops/grad/add_grad.h" +#include "ops/grad/avg_pool_grad.h" +#include "ops/grad/bias_add_grad.h" +#include "ops/grad/batch_norm_grad.h" +#include "ops/grad/binary_cross_entropy_grad.h" +#include "ops/grad/de_conv2d_grad_filter.h" +#include "ops/grad/div_grad.h" +#include "ops/grad/dropout_grad.h" +#include "ops/grad/flatten_grad.h" +#include "ops/grad/group_conv2d_grad_input.h" +#include "ops/grad/log_grad.h" +#include "ops/grad/max_pool_grad.h" +#include "ops/grad/maximum_grad.h" +#include "ops/grad/minimum_grad.h" +#include "ops/grad/mul_grad.h" +#include "ops/grad/neg_grad.h" +#include "ops/grad/pooling_grad.h" +#include "ops/grad/power_grad.h" +#include "ops/grad/sigmoid_cross_entropy_with_logits_grad.h" +#include "ops/grad/smooth_l1_loss_grad.h" +#include "ops/grad/sub_grad.h" +#include "ops/fusion/activation.h" +#include "ops/fusion/add_fusion.h" +#include "ops/fusion/adder_fusion.h" +#include "ops/fusion/arg_max_fusion.h" +#include "ops/fusion/arg_min_fusion.h" +#include "ops/fusion/avg_pool_fusion.h" +#include "ops/fusion/conv2d_backprop_filter_fusion.h" +#include "ops/fusion/conv2d_backprop_input_fusion.h" +#include "ops/fusion/conv2d_fusion.h" +#include "ops/fusion/conv2d_transpose_fusion.h" +#include "ops/fusion/div_fusion.h" +#include "ops/fusion/embedding_lookup_fusion.h" +#include "ops/fusion/exp_fusion.h" +#include "ops/fusion/full_connection.h" +#include "ops/fusion/l2_normalize_fusion.h" +#include "ops/fusion/layer_norm_fusion.h" +#include "ops/fusion/max_pool_fusion.h" +#include "ops/fusion/mul_fusion.h" +#include "ops/fusion/pad_fusion.h" +#include "ops/fusion/partial_fusion.h" +#include "ops/fusion/pow_fusion.h" +#include "ops/fusion/prelu_fusion.h" +#include "ops/fusion/reduce_fusion.h" +#include "ops/fusion/scale_fusion.h" +#include "ops/fusion/slice_fusion.h" +#include "ops/fusion/sub_fusion.h" +#include "ops/fusion/tile_fusion.h" +#include "ops/fusion/topk_fusion.h" +#include "ops/if.h" +#include "ops/gru.h" +#include "ops/non_zero.h" +#include "ops/invert_permutation.h" +#include "ops/size.h" +#include "ops/random_standard_normal.h" +#include "ops/crop_and_resize.h" +#include "ops/erf.h" +#include "ops/grad/strided_slice_grad.h" +#include "ops/is_finite.h" +#include "ops/lin_space.h" +#include "ops/uniform_real.h" +#include "ops/grad/abs_grad.h" + +#define FUNC_MSOP2SCHEMAOP_DECLARE(OP) \ + namespace mindspore::lite::ops { \ + mindspore::schema::PrimitiveT *MSOp2SchemaOp(const mindspore::ops::OP *op); \ + } +#else +#define FUNC_MSOP2SCHEMAOP_DECLARE(OP) +#endif + +#ifdef PRIMITIVE_WRITEABLE +FUNC_MSOP2SCHEMAOP_DECLARE(Abs); +FUNC_MSOP2SCHEMAOP_DECLARE(Activation); +FUNC_MSOP2SCHEMAOP_DECLARE(ActivationGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Adam); +FUNC_MSOP2SCHEMAOP_DECLARE(AddFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(AdderFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(AddGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(AddN); +FUNC_MSOP2SCHEMAOP_DECLARE(All); +FUNC_MSOP2SCHEMAOP_DECLARE(ApplyMomentum); +FUNC_MSOP2SCHEMAOP_DECLARE(ArgMax); +FUNC_MSOP2SCHEMAOP_DECLARE(ArgMaxFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(ArgMin); +FUNC_MSOP2SCHEMAOP_DECLARE(ArgMinFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Asin); +FUNC_MSOP2SCHEMAOP_DECLARE(Assert); +FUNC_MSOP2SCHEMAOP_DECLARE(Assign); +FUNC_MSOP2SCHEMAOP_DECLARE(AssignAdd); +FUNC_MSOP2SCHEMAOP_DECLARE(Atan); +FUNC_MSOP2SCHEMAOP_DECLARE(AudioSpectrogram); +FUNC_MSOP2SCHEMAOP_DECLARE(AvgPoolFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(AvgPoolGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(BatchNorm); +FUNC_MSOP2SCHEMAOP_DECLARE(BatchNormGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(BatchToSpace); +FUNC_MSOP2SCHEMAOP_DECLARE(BatchToSpaceND); +FUNC_MSOP2SCHEMAOP_DECLARE(BiasAdd); +FUNC_MSOP2SCHEMAOP_DECLARE(BiasAddGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(BinaryCrossEntropy); +FUNC_MSOP2SCHEMAOP_DECLARE(BinaryCrossEntropyGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(BroadcastTo); +FUNC_MSOP2SCHEMAOP_DECLARE(Cast); +FUNC_MSOP2SCHEMAOP_DECLARE(Ceil); +FUNC_MSOP2SCHEMAOP_DECLARE(Clip); +FUNC_MSOP2SCHEMAOP_DECLARE(Concat); +FUNC_MSOP2SCHEMAOP_DECLARE(ControlDepend); +FUNC_MSOP2SCHEMAOP_DECLARE(Constant); +FUNC_MSOP2SCHEMAOP_DECLARE(ConstantOfShape); +FUNC_MSOP2SCHEMAOP_DECLARE(Conv2DBackpropFilterFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Conv2DBackpropInputFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Conv2DFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Conv2dTransposeFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Cos); +FUNC_MSOP2SCHEMAOP_DECLARE(Crop); +FUNC_MSOP2SCHEMAOP_DECLARE(CustomExtractFeatures); +FUNC_MSOP2SCHEMAOP_DECLARE(CustomNormalize); +FUNC_MSOP2SCHEMAOP_DECLARE(CustomPredict); +FUNC_MSOP2SCHEMAOP_DECLARE(DeConv2DGradFilter); +FUNC_MSOP2SCHEMAOP_DECLARE(Depend); +FUNC_MSOP2SCHEMAOP_DECLARE(DepthToSpace); +FUNC_MSOP2SCHEMAOP_DECLARE(DetectionPostProcess); +FUNC_MSOP2SCHEMAOP_DECLARE(DivFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(DivGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Dropout); +FUNC_MSOP2SCHEMAOP_DECLARE(DropoutGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Eltwise); +FUNC_MSOP2SCHEMAOP_DECLARE(Elu); +FUNC_MSOP2SCHEMAOP_DECLARE(EmbeddingLookupFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Equal); +FUNC_MSOP2SCHEMAOP_DECLARE(ExpFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(ExpandDims); +FUNC_MSOP2SCHEMAOP_DECLARE(FakeQuantWithMinMaxVars); +FUNC_MSOP2SCHEMAOP_DECLARE(FakeQuantWithMinMaxVarsPerChannel); +FUNC_MSOP2SCHEMAOP_DECLARE(FftImag); +FUNC_MSOP2SCHEMAOP_DECLARE(FftReal); +FUNC_MSOP2SCHEMAOP_DECLARE(Fill); +FUNC_MSOP2SCHEMAOP_DECLARE(Flatten); +FUNC_MSOP2SCHEMAOP_DECLARE(FlattenGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Floor); +FUNC_MSOP2SCHEMAOP_DECLARE(FloorDiv); +FUNC_MSOP2SCHEMAOP_DECLARE(FloorMod); +FUNC_MSOP2SCHEMAOP_DECLARE(FullConnection); +FUNC_MSOP2SCHEMAOP_DECLARE(FusedBatchNorm); +FUNC_MSOP2SCHEMAOP_DECLARE(Gather); +FUNC_MSOP2SCHEMAOP_DECLARE(GatherNd); +FUNC_MSOP2SCHEMAOP_DECLARE(Greater); +FUNC_MSOP2SCHEMAOP_DECLARE(GreaterEqual); +FUNC_MSOP2SCHEMAOP_DECLARE(GroupConv2DGradInput); +FUNC_MSOP2SCHEMAOP_DECLARE(HashtableLookup); +FUNC_MSOP2SCHEMAOP_DECLARE(InstanceNorm); +FUNC_MSOP2SCHEMAOP_DECLARE(LayerNormFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(LeakyRelu); +FUNC_MSOP2SCHEMAOP_DECLARE(Less); +FUNC_MSOP2SCHEMAOP_DECLARE(LessEqual); +FUNC_MSOP2SCHEMAOP_DECLARE(Log); +FUNC_MSOP2SCHEMAOP_DECLARE(LogGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(LogicalAnd); +FUNC_MSOP2SCHEMAOP_DECLARE(LogicalNot); +FUNC_MSOP2SCHEMAOP_DECLARE(LogicalOr); +FUNC_MSOP2SCHEMAOP_DECLARE(LogicalXor); +FUNC_MSOP2SCHEMAOP_DECLARE(LpNormalization); +FUNC_MSOP2SCHEMAOP_DECLARE(LRN); +FUNC_MSOP2SCHEMAOP_DECLARE(LshProjection); +FUNC_MSOP2SCHEMAOP_DECLARE(LSTM); +FUNC_MSOP2SCHEMAOP_DECLARE(L2NormalizeFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(MakeTuple); +FUNC_MSOP2SCHEMAOP_DECLARE(MatMul); +FUNC_MSOP2SCHEMAOP_DECLARE(Maximum); +FUNC_MSOP2SCHEMAOP_DECLARE(MaximumGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(MaxPoolFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(MaxPoolGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Merge); +FUNC_MSOP2SCHEMAOP_DECLARE(Mfcc); +FUNC_MSOP2SCHEMAOP_DECLARE(Minimum); +FUNC_MSOP2SCHEMAOP_DECLARE(MinimumGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Mod); +FUNC_MSOP2SCHEMAOP_DECLARE(MulFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(MulGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Neg); +FUNC_MSOP2SCHEMAOP_DECLARE(NegGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(NotEqual); +FUNC_MSOP2SCHEMAOP_DECLARE(NonMaxSuppression); +FUNC_MSOP2SCHEMAOP_DECLARE(OneHot); +FUNC_MSOP2SCHEMAOP_DECLARE(OnesLike); +FUNC_MSOP2SCHEMAOP_DECLARE(PadFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(PartialFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(PowFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(PowerGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(PReLUFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(PriorBox); +FUNC_MSOP2SCHEMAOP_DECLARE(Proposal); +FUNC_MSOP2SCHEMAOP_DECLARE(Rank); +FUNC_MSOP2SCHEMAOP_DECLARE(Range); +FUNC_MSOP2SCHEMAOP_DECLARE(Rank); +FUNC_MSOP2SCHEMAOP_DECLARE(RealDiv); +FUNC_MSOP2SCHEMAOP_DECLARE(Reciprocal); +FUNC_MSOP2SCHEMAOP_DECLARE(ReduceFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Reshape); +FUNC_MSOP2SCHEMAOP_DECLARE(Resize); +FUNC_MSOP2SCHEMAOP_DECLARE(Return); +FUNC_MSOP2SCHEMAOP_DECLARE(ReverseSequence); +FUNC_MSOP2SCHEMAOP_DECLARE(ReverseV2); +FUNC_MSOP2SCHEMAOP_DECLARE(Rfft); +FUNC_MSOP2SCHEMAOP_DECLARE(ROIPooling); +FUNC_MSOP2SCHEMAOP_DECLARE(Round); +FUNC_MSOP2SCHEMAOP_DECLARE(Rsqrt); +FUNC_MSOP2SCHEMAOP_DECLARE(QuantDTypeCast); +FUNC_MSOP2SCHEMAOP_DECLARE(Scale); +FUNC_MSOP2SCHEMAOP_DECLARE(ScaleFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(ScatterNd); +FUNC_MSOP2SCHEMAOP_DECLARE(Select); +FUNC_MSOP2SCHEMAOP_DECLARE(SGD); +FUNC_MSOP2SCHEMAOP_DECLARE(Shape); +FUNC_MSOP2SCHEMAOP_DECLARE(SigmoidCrossEntropyWithLogits); +FUNC_MSOP2SCHEMAOP_DECLARE(SigmoidCrossEntropyWithLogitsGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Sin); +FUNC_MSOP2SCHEMAOP_DECLARE(SkipGram); +FUNC_MSOP2SCHEMAOP_DECLARE(SliceFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(SmoothL1Loss); +FUNC_MSOP2SCHEMAOP_DECLARE(SmoothL1LossGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Softmax); +FUNC_MSOP2SCHEMAOP_DECLARE(SoftmaxCrossEntropyWithLogits); +FUNC_MSOP2SCHEMAOP_DECLARE(SpaceToBatch); +FUNC_MSOP2SCHEMAOP_DECLARE(SpaceToBatchND); +FUNC_MSOP2SCHEMAOP_DECLARE(SpaceToDepth); +FUNC_MSOP2SCHEMAOP_DECLARE(SparseSoftmaxCrossEntropy); +FUNC_MSOP2SCHEMAOP_DECLARE(SparseToDense); +FUNC_MSOP2SCHEMAOP_DECLARE(Split); +FUNC_MSOP2SCHEMAOP_DECLARE(Sqrt); +FUNC_MSOP2SCHEMAOP_DECLARE(Square); +FUNC_MSOP2SCHEMAOP_DECLARE(SquaredDifference); +FUNC_MSOP2SCHEMAOP_DECLARE(Squeeze); +FUNC_MSOP2SCHEMAOP_DECLARE(Stack); +FUNC_MSOP2SCHEMAOP_DECLARE(StridedSlice); +FUNC_MSOP2SCHEMAOP_DECLARE(Sub); +FUNC_MSOP2SCHEMAOP_DECLARE(SubFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(SubGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(Switch); +FUNC_MSOP2SCHEMAOP_DECLARE(Tan); +FUNC_MSOP2SCHEMAOP_DECLARE(TensorListFromTensor); +FUNC_MSOP2SCHEMAOP_DECLARE(TensorListGetItem); +FUNC_MSOP2SCHEMAOP_DECLARE(TensorListReserve); +FUNC_MSOP2SCHEMAOP_DECLARE(TensorListSetItem); +FUNC_MSOP2SCHEMAOP_DECLARE(TensorListStack); +FUNC_MSOP2SCHEMAOP_DECLARE(TileFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(TopKFusion); +FUNC_MSOP2SCHEMAOP_DECLARE(Transpose); +FUNC_MSOP2SCHEMAOP_DECLARE(TupleGetItem); +FUNC_MSOP2SCHEMAOP_DECLARE(Unique); +FUNC_MSOP2SCHEMAOP_DECLARE(UnsortedSegmentSum); +FUNC_MSOP2SCHEMAOP_DECLARE(Unsqueeze); +FUNC_MSOP2SCHEMAOP_DECLARE(Unstack); +FUNC_MSOP2SCHEMAOP_DECLARE(While); +FUNC_MSOP2SCHEMAOP_DECLARE(Where); +FUNC_MSOP2SCHEMAOP_DECLARE(ZerosLike); +FUNC_MSOP2SCHEMAOP_DECLARE(Select); +FUNC_MSOP2SCHEMAOP_DECLARE(If); +FUNC_MSOP2SCHEMAOP_DECLARE(GRU); +FUNC_MSOP2SCHEMAOP_DECLARE(NonZero); +FUNC_MSOP2SCHEMAOP_DECLARE(InvertPermutation); +FUNC_MSOP2SCHEMAOP_DECLARE(Size); +FUNC_MSOP2SCHEMAOP_DECLARE(RandomStandardNormal); +FUNC_MSOP2SCHEMAOP_DECLARE(CropAndResize); +FUNC_MSOP2SCHEMAOP_DECLARE(Erf); +FUNC_MSOP2SCHEMAOP_DECLARE(StridedSliceGrad); +FUNC_MSOP2SCHEMAOP_DECLARE(IsFinite); +FUNC_MSOP2SCHEMAOP_DECLARE(LinSpace); +FUNC_MSOP2SCHEMAOP_DECLARE(UniformReal); +FUNC_MSOP2SCHEMAOP_DECLARE(AbsGrad); +#endif +#endif // MINDSPORE_LITE_SRC_OPS_OPS_FUNC_DECLARE_H_ diff --git a/mindspore/lite/src/ops/ops_register.h b/mindspore/lite/src/ops/ops_register.h deleted file mode 100644 index 969f925f00..0000000000 --- a/mindspore/lite/src/ops/ops_register.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_OP_REGISTER_H -#define LITE_MINDSPORE_LITE_C_OPS_OP_REGISTER_H - -#include -#include "src/ops/primitive_c.h" -namespace mindspore { -namespace lite { -class OpsRegistry { - public: - static OpsRegistry *GetInstance() { - static OpsRegistry registry; - return ®istry; - } - - void InsertPrimitiveCMap(schema::PrimitiveType type, PrimitiveCCreator creator) { - primitive_creators[type] = creator; - } - PrimitiveCCreator GetPrimitiveCreator(schema::PrimitiveType type) { - if (primitive_creators.find(type) != primitive_creators.end()) { - return primitive_creators[type]; - } else { - MS_LOG(ERROR) << "Unsupported primitive type in Create : " << schema::EnumNamePrimitiveType(type); - return nullptr; - } - } - - protected: - std::map primitive_creators; -}; - -class Registry { - public: - Registry(schema::PrimitiveType primitive_type, PrimitiveCCreator creator) { - OpsRegistry::GetInstance()->InsertPrimitiveCMap(primitive_type, creator); - } -}; - -} // namespace lite -} // namespace mindspore -#endif // LITE_MINDSPORE_LITE_C_OPS_OP_REGISTER_H diff --git a/mindspore/lite/src/ops/ops_utils.cc b/mindspore/lite/src/ops/ops_utils.cc new file mode 100644 index 0000000000..1e358e0f5a --- /dev/null +++ b/mindspore/lite/src/ops/ops_utils.cc @@ -0,0 +1,905 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 +#include "src/ops/ops_utils.h" + +#ifdef PRIMITIVE_WRITEABLE +#include "mindspore/core/ir/anf.h" + +namespace mindspore { +namespace lite { +schema::PrimitiveT *GetPrimitiveT(const AnfNodePtr &node) { + auto prim = GetValueNode>(node); + if (prim == nullptr) { + MS_LOG(DEBUG) << "primitive is nullptr"; + return nullptr; + } + + if (prim->name().empty()) { + MS_LOG(ERROR) << "the name of primitive is null"; + return nullptr; + } + + MS_LOG(INFO) << "export prim: " << prim->name(); + auto creator = MSOpsRegistry::GetInstance()->GetPrimitiveCreator(prim->name()); + if (creator != nullptr) { + return creator(node); + } else { + MS_LOG(ERROR) << "can not find MSOpsRegistry for op: " << prim->name(); + return nullptr; + } +} + +schema::PrimitiveT *AbsPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ActivationPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ActivationGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AdamPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AdderFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AddFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AddGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AddNPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AllPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ApplyMomentumPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ArgMaxFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ArgMinFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AssertPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AssignPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AssignAddPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AudioSpectrogramPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AvgPoolFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *AvgPoolGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BatchNormPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BatchToSpacePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BatchToSpaceNDPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BiasAddPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BiasAddGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BNGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *BroadcastToPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CastPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CeilPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ClipPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ConcatPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ConstantOfShapePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ControlDependPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *Conv2DBackpropFilterFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *Conv2DBackpropInputFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *Conv2DFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *Conv2dTransposeFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CosPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CropPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CropAndResizePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CustomExtractFeaturesPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CustomNormalizePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *CustomPredictPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DependPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DepthToSpacePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DetectionPostProcessPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DivFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DivGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DropoutPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *DropoutGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *EltwisePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *EluPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *EmbeddingLookupFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *EqualPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ExpandDimsPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ExpFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FftImagPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FftRealPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FillPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FlattenPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FlattenGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FloorPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FloorDivPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FloorModPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FullConnectionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *FusedBatchNormPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *GatherPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *GatherNdPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *GreaterPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *GreaterEqualPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *HashtableLookupPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *InstanceNormPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *InvertPermutationPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LayerNormFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LeakyReluPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LessPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LessEqualPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LogPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LogGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LogicalAndPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LogicalNotPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LogicalOrPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LrnPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LpNormalizationPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LshProjectionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *LSTMPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *L2NormalizeFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MatMulPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MaximumPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MaximumGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MaxPoolFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MaxPoolGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MergePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MfccPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MinimumPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MinimumGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ModPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MulFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *MulGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *NegPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *NegGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *NotEqualPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *NonMaxSuppressionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *OneHotPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *OnesLikePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *PadFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *PartialFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *PowerGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *PowFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *PReLUFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *QuantDTypeCastPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RangePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RankPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RealDivPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ReciprocalPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ReduceFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ReshapePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ResizePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ReverseV2PrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ReverseSequencePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RfftPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ROIPoolingPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RoundPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *RsqrtPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ScaleFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ScatterNdPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SelectPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SGDPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ShapePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SigmoidCrossEntropyWithLogitsPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SigmoidCrossEntropyWithLogitsGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SinPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SizePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SkipGramPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SliceFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SmoothL1LossPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SmoothL1LossGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SoftmaxPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SoftmaxCrossEntropyWithLogitsPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SpaceToBatchPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SpaceToBatchNDPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SpaceToDepthPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SparseToDensePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SplitPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SqrtPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SquarePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SquaredDifferencePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SqueezePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *StackPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *StridedSlicePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SubFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SubGradPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *SwitchPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TensorListFromTensorPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TensorListGetItemPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TensorListReservePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TensorListSetItemPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TensorListStackPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TileFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TopKFusionPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *TransposePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *UniquePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *UnstackPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *UnsortedSegmentSumPrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *UnsqueezePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *WherePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} +schema::PrimitiveT *ZerosLikePrimitiveCreator(const AnfNodePtr &node) { + auto ms_primc = GetValueNode>(node); + return ms_primc != nullptr ? ops::MSOp2SchemaOp(ms_primc.get()) : nullptr; +} + +RegistryMSOps g_absPrimitiveCreatorRegistry("Abs", AbsPrimitiveCreator); +RegistryMSOps g_activationPrimitiveCreatorRegistry("Activation", ActivationPrimitiveCreator); +RegistryMSOps g_activationGradPrimitiveCreatorRegistry("ActivationGrad", ActivationGradPrimitiveCreator); +RegistryMSOps g_reluGradPrimitiveCreatorRegistry("ReluGrad", ActivationGradPrimitiveCreator); // ? +RegistryMSOps g_addPrimitiveCreatorRegistry("Add", AddFusionPrimitiveCreator); +RegistryMSOps g_addFusionPrimitiveCreatorRegistry("AddFusion", AddFusionPrimitiveCreator); +RegistryMSOps g_addGradPrimitiveCreatorRegistry("AddGrad", AddGradPrimitiveCreator); +RegistryMSOps g_adamPrimitiveCreatorRegistry("Adam", AdamPrimitiveCreator); +RegistryMSOps g_adderPrimitiveCreatorRegistry("Adder", AdderFusionPrimitiveCreator); +RegistryMSOps g_adderFusionPrimitiveCreatorRegistry("AdderFusion", AdderFusionPrimitiveCreator); +RegistryMSOps g_addNPrimitiveCreatorRegistry("AddN", AddNPrimitiveCreator); +RegistryMSOps g_allPrimitiveCreatorRegistry("All", AllPrimitiveCreator); +RegistryMSOps g_applyMomentumPrimitiveCreatorRegistry("ApplyMomentum", ApplyMomentumPrimitiveCreator); +RegistryMSOps g_argMaxPrimitiveCreatorRegistry("ArgMax", ArgMaxFusionPrimitiveCreator); +RegistryMSOps g_argMaxFusionPrimitiveCreatorRegistry("ArgMaxFusion", ArgMaxFusionPrimitiveCreator); +RegistryMSOps g_argMinPrimitiveCreatorRegistry("ArgMin", ArgMinFusionPrimitiveCreator); +RegistryMSOps g_argMinFusionPrimitiveCreatorRegistry("ArgMinFusion", ArgMinFusionPrimitiveCreator); +RegistryMSOps g_assertPrimitiveCreatorRegistry("Assert", AssertPrimitiveCreator); +RegistryMSOps g_assignPrimitiveCreatorRegistry("Assign", AssignPrimitiveCreator); +RegistryMSOps g_assignAddPrimitiveCreatorRegistry("AssignAdd", AssignAddPrimitiveCreator); +RegistryMSOps g_audioSpectrogramPrimitiveCreatorRegistry("AudioSpectrogram", AudioSpectrogramPrimitiveCreator); +RegistryMSOps g_avgPoolPrimitiveCreatorRegistry("AvgPool", AvgPoolFusionPrimitiveCreator); +RegistryMSOps g_avgPoolFusionPrimitiveCreatorRegistry("AvgPoolFusion", AvgPoolFusionPrimitiveCreator); +RegistryMSOps g_avgPoolGradPrimitiveCreatorRegistry("AvgPoolGrad", AvgPoolGradPrimitiveCreator); +RegistryMSOps g_batchNormPrimitiveCreatorRegistry("BatchNorm", BatchNormPrimitiveCreator); +RegistryMSOps g_batchToSpacePrimitiveCreatorRegistry("BatchToSpace", BatchToSpacePrimitiveCreator); +RegistryMSOps g_batchToSpaceNDPrimitiveCreatorRegistry("BatchToSpaceND", BatchToSpaceNDPrimitiveCreator); +RegistryMSOps g_biasAddPrimitiveCreatorRegistry("BiasAdd", BiasAddPrimitiveCreator); +RegistryMSOps g_biasGradAddPrimitiveCreatorRegistry("BiasGrad", BiasAddGradPrimitiveCreator); +RegistryMSOps g_biasAddGradAddPrimitiveCreatorRegistry("BiasAddGrad", BiasAddGradPrimitiveCreator); +RegistryMSOps g_bNGradPrimitiveCreatorRegistry("BatchNormGrad", BNGradPrimitiveCreator); +RegistryMSOps g_broadcastToPrimitiveCreatorRegistry("BroadcastTo", BroadcastToPrimitiveCreator); +RegistryMSOps g_castPrimitiveCreatorRegistry("Cast", CastPrimitiveCreator); +RegistryMSOps g_ceilPrimitiveCreatorRegistry("Ceil", CeilPrimitiveCreator); +RegistryMSOps g_clipPrimitiveCreatorRegistry("Clip", ClipPrimitiveCreator); +RegistryMSOps g_concatPrimitiveCreatorRegistry("Concat", ConcatPrimitiveCreator); +RegistryMSOps g_controlDependPrimitiveCreatorRegistry("ControlDepend", ControlDependPrimitiveCreator); +RegistryMSOps g_conv2DBackpropFilterFusionPrimitiveCreatorRegistry("Conv2DBackpropFilterFusion", + Conv2DBackpropFilterFusionPrimitiveCreator); +RegistryMSOps g_conv2DBackpropInputFusionPrimitiveCreatorRegistry("Conv2DBackpropInputFusion", + Conv2DBackpropInputFusionPrimitiveCreator); +RegistryMSOps g_conv2DPrimitiveCreatorRegistry("Conv2D", Conv2DFusionPrimitiveCreator); +RegistryMSOps g_conv2DFusionPrimitiveCreatorRegistry("Conv2DFusion", Conv2DFusionPrimitiveCreator); +RegistryMSOps g_conv2dTransposePrimitiveCreatorRegistry("Conv2dTranspose", Conv2dTransposeFusionPrimitiveCreator); +RegistryMSOps g_conv2dTransposeFusionPrimitiveCreatorRegistry("Conv2dTransposeFusion", + Conv2dTransposeFusionPrimitiveCreator); +RegistryMSOps g_constantOfShapePrimitiveCreatorRegistry("ConstantOfShape", ConstantOfShapePrimitiveCreator); +RegistryMSOps g_cosPrimitiveCreatorRegistry("Cos", CosPrimitiveCreator); +RegistryMSOps g_cropPrimitiveCreatorRegistry("Crop", CropPrimitiveCreator); +RegistryMSOps g_cropAndResizePrimitiveCreatorRegistry("CropAndResize", CropAndResizePrimitiveCreator); +RegistryMSOps g_customExtractFeaturesPrimitiveCreatorRegistry("CustomExtractFeatures", + CustomExtractFeaturesPrimitiveCreator); +RegistryMSOps g_customNormalizePrimitiveCreatorRegistry("CustomNormalize", CustomNormalizePrimitiveCreator); +RegistryMSOps g_customPredictPrimitiveCreatorRegistry("CustomPredict", CustomPredictPrimitiveCreator); +RegistryMSOps g_dependPrimitiveCreatorRegistry("Depend", DependPrimitiveCreator); +RegistryMSOps g_depthToSpacePrimitiveCreatorRegistry("DepthToSpace", DepthToSpacePrimitiveCreator); +RegistryMSOps g_detectionPostProcessPrimitiveCreatorRegistry("DetectionPostProcess", + DetectionPostProcessPrimitiveCreator); +RegistryMSOps g_divPrimitiveCreatorRegistry("Div", DivFusionPrimitiveCreator); +RegistryMSOps g_divFusionPrimitiveCreatorRegistry("DivFusion", DivFusionPrimitiveCreator); +RegistryMSOps g_divGradPrimitiveCreatorRegistry("DivGrad", DivGradPrimitiveCreator); +RegistryMSOps g_dropoutPrimitiveCreatorRegistry("Dropout", DropoutPrimitiveCreator); +RegistryMSOps g_dropoutGradPrimitiveCreatorRegistry("DropoutGrad", DropoutGradPrimitiveCreator); +RegistryMSOps g_eltwisePrimitiveCreatorRegistry("Eltwise", EltwisePrimitiveCreator); +RegistryMSOps g_eluPrimitiveCreatorRegistry("Elu", EluPrimitiveCreator); +RegistryMSOps g_equalPrimitiveCreatorRegistry("Equal", EqualPrimitiveCreator); +RegistryMSOps g_embeddingLookupFusionPrimitiveCreatorRegistry("EmbeddingLookupFusion", + EmbeddingLookupFusionPrimitiveCreator); +RegistryMSOps g_expandDimsPrimitiveCreatorRegistry("ExpandDims", ExpandDimsPrimitiveCreator); +RegistryMSOps g_expPrimitiveCreatorRegistry("Exp", ExpFusionPrimitiveCreator); +RegistryMSOps g_expFusionPrimitiveCreatorRegistry("ExpFusion", ExpFusionPrimitiveCreator); +RegistryMSOps g_fftImagPrimitiveCreatorRegistry("FftImag", FftImagPrimitiveCreator); +RegistryMSOps g_fftRealPrimitiveCreatorRegistry("FftReal", FftRealPrimitiveCreator); +RegistryMSOps g_fillPrimitiveCreatorRegistry("Fill", FillPrimitiveCreator); +RegistryMSOps g_flattenPrimitiveCreatorRegistry("Flatten", FlattenPrimitiveCreator); +RegistryMSOps g_flattenGradPrimitiveCreatorRegistry("FlattenGrad", FlattenGradPrimitiveCreator); +RegistryMSOps g_floorPrimitiveCreatorRegistry("Floor", FloorPrimitiveCreator); +RegistryMSOps g_floorDivPrimitiveCreatorRegistry("FloorDiv", FloorDivPrimitiveCreator); +RegistryMSOps g_floorModPrimitiveCreatorRegistry("FloorMod", FloorModPrimitiveCreator); +RegistryMSOps g_fullConnectionPrimitiveCreatorRegistry("FullConnection", FullConnectionPrimitiveCreator); +RegistryMSOps g_fusedBatchNormPrimitiveCreatorRegistry("FusedBatchNorm", FusedBatchNormPrimitiveCreator); +RegistryMSOps g_gatherPrimitiveCreatorRegistry("Gather", GatherPrimitiveCreator); +RegistryMSOps g_gatherNdPrimitiveCreatorRegistry("GatherNd", GatherNdPrimitiveCreator); +RegistryMSOps g_greaterPrimitiveCreatorRegistry("Greater", GreaterPrimitiveCreator); +RegistryMSOps g_greaterEqualPrimitiveCreatorRegistry("GreaterEqual", GreaterEqualPrimitiveCreator); +RegistryMSOps g_hashtableLookupPrimitiveCreatorRegistry("HashtableLookup", HashtableLookupPrimitiveCreator); +RegistryMSOps g_instanceNormPrimitiveCreatorRegistry("InstanceNorm", InstanceNormPrimitiveCreator); +RegistryMSOps g_invertPermutationPrimitiveCreatorRegistry("InvertPermutation", InvertPermutationPrimitiveCreator); +RegistryMSOps g_layerNormPrimitiveCreatorRegistry("LayerNorm", LayerNormFusionPrimitiveCreator); +RegistryMSOps g_layerNormFusionPrimitiveCreatorRegistry("LayerNormFusion", LayerNormFusionPrimitiveCreator); +RegistryMSOps g_leakyReluPrimitiveCreatorRegistry("LeakyRelu", LeakyReluPrimitiveCreator); +RegistryMSOps g_lessPrimitiveCreatorRegistry("Less", LessPrimitiveCreator); +RegistryMSOps g_lessEqualPrimitiveCreatorRegistry("LessEqual", LessEqualPrimitiveCreator); +RegistryMSOps g_logPrimitiveCreatorRegistry("Log", LogPrimitiveCreator); +RegistryMSOps g_logGradPrimitiveCreatorRegistry("LogGrad", LogGradPrimitiveCreator); +RegistryMSOps g_logicalAndPrimitiveCreatorRegistry("LogicalAnd", LogicalAndPrimitiveCreator); +RegistryMSOps g_logicalNotPrimitiveCreatorRegistry("LogicalNot", LogicalNotPrimitiveCreator); +RegistryMSOps g_logicalOrPrimitiveCreatorRegistry("LogicalOr", LogicalOrPrimitiveCreator); +RegistryMSOps g_lpNormalizationPrimitiveCreatorRegistry("LpNormalization", LpNormalizationPrimitiveCreator); +RegistryMSOps g_lrnPrimitiveCreatorRegistry("LRN", LrnPrimitiveCreator); +RegistryMSOps g_lshProjectionPrimitiveCreatorRegistry("LshProjection", LshProjectionPrimitiveCreator); +RegistryMSOps g_lSTMPrimitiveCreatorRegistry("LSTM", LSTMPrimitiveCreator); +RegistryMSOps g_l2NormalizeFusionPrimitiveCreatorRegistry("L2NormalizeFusion", L2NormalizeFusionPrimitiveCreator); +RegistryMSOps g_matMulPrimitiveCreatorRegistry("MatMul", MatMulPrimitiveCreator); +RegistryMSOps g_maximumPrimitiveCreatorRegistry("Maximum", MaximumPrimitiveCreator); +RegistryMSOps g_maximumGradPrimitiveCreatorRegistry("MaximumGrad", MaximumGradPrimitiveCreator); +RegistryMSOps g_maxPoolPrimitiveCreatorRegistry("MaxPool", MaxPoolFusionPrimitiveCreator); +RegistryMSOps g_maxPoolFusionPrimitiveCreatorRegistry("MaxPoolFusion", MaxPoolFusionPrimitiveCreator); +RegistryMSOps g_maxPoolGradPrimitiveCreatorRegistry("MaxPoolGrad", MaxPoolGradPrimitiveCreator); +RegistryMSOps g_mergePrimitiveCreatorRegistry("Merge", MergePrimitiveCreator); +RegistryMSOps g_mfccPrimitiveCreatorRegistry("Mfcc", MfccPrimitiveCreator); +RegistryMSOps g_minimumPrimitiveCreatorRegistry("Minimum", MinimumPrimitiveCreator); +RegistryMSOps g_minimumGradPrimitiveCreatorRegistry("MinimumGrad", MinimumGradPrimitiveCreator); +RegistryMSOps g_modPrimitiveCreatorRegistry("Mod", ModPrimitiveCreator); +RegistryMSOps g_mulPrimitiveCreatorRegistry("Mul", MulFusionPrimitiveCreator); +RegistryMSOps g_mulMulFusionPrimitiveCreatorRegistry("MulFusion", MulFusionPrimitiveCreator); +RegistryMSOps g_mulGradPrimitiveCreatorRegistry("MulGrad", MulGradPrimitiveCreator); +RegistryMSOps g_negPrimitiveCreatorRegistry("Neg", NegPrimitiveCreator); +RegistryMSOps g_negGradPrimitiveCreatorRegistry("NegGrad", NegGradPrimitiveCreator); +RegistryMSOps g_nonMaxSuppressionPrimitiveCreatorRegistry("NonMaxSuppression", NonMaxSuppressionPrimitiveCreator); +RegistryMSOps g_notEqualPrimitiveCreatorRegistry("NotEqual", NotEqualPrimitiveCreator); +RegistryMSOps g_oneHotPrimitiveCreatorRegistry("OneHot", OneHotPrimitiveCreator); +RegistryMSOps g_onesLikePrimitiveCreatorRegistry("OnesLike", OnesLikePrimitiveCreator); +RegistryMSOps g_padPrimitiveCreatorRegistry("Pad", PadFusionPrimitiveCreator); +RegistryMSOps g_padFusionPrimitiveCreatorRegistry("PadFusion", PadFusionPrimitiveCreator); +RegistryMSOps g_partialFusionPrimitiveCreatorRegistry("PartialFusion", PartialFusionPrimitiveCreator); +RegistryMSOps g_powerGradPrimitiveCreatorRegistry("PowerGrad", PowerGradPrimitiveCreator); +RegistryMSOps g_powFusionPrimitiveCreatorRegistry("PowFusion", PowFusionPrimitiveCreator); +RegistryMSOps g_pReLUFusionPrimitiveCreatorRegistry("PReLUFusion", PReLUFusionPrimitiveCreator); +RegistryMSOps g_rangePrimitiveCreatorRegistry("Range", RangePrimitiveCreator); +RegistryMSOps g_rankPrimitiveCreatorRegistry("Rank", RankPrimitiveCreator); +RegistryMSOps g_reciprocalPrimitiveCreatorRegistry("Reciprocal", ReciprocalPrimitiveCreator); +RegistryMSOps g_realDivPrimitiveCreatorRegistry("RealDiv", RealDivPrimitiveCreator); +RegistryMSOps g_reducePrimitiveCreatorRegistry("Reduce", ReduceFusionPrimitiveCreator); +RegistryMSOps g_reduceFusionPrimitiveCreatorRegistry("ReduceFusion", ReduceFusionPrimitiveCreator); +RegistryMSOps g_reshapePrimitiveCreatorRegistry("Reshape", ReshapePrimitiveCreator); +RegistryMSOps g_resizePrimitiveCreatorRegistry("Resize", ResizePrimitiveCreator); +RegistryMSOps g_reverseV2PrimitiveCreatorRegistry("ReverseV2", ReverseV2PrimitiveCreator); +RegistryMSOps g_reverseSequencePrimitiveCreatorRegistry("ReverseSequence", ReverseSequencePrimitiveCreator); +RegistryMSOps g_rfftPrimitiveCreatorRegistry("Rfft", RfftPrimitiveCreator); +RegistryMSOps g_rOIPoolingPrimitiveCreatorRegistry("ROIPooling", ROIPoolingPrimitiveCreator); +RegistryMSOps g_roundPrimitiveCreatorRegistry("Round", RoundPrimitiveCreator); +RegistryMSOps g_rsqrtPrimitiveCreatorRegistry("Rsqrt", RsqrtPrimitiveCreator); +RegistryMSOps g_quantDTypeCastPrimitiveCreatorRegistry("QuantDTypeCast", QuantDTypeCastPrimitiveCreator); +RegistryMSOps g_scalePrimitiveCreatorRegistry("Scale", ScaleFusionPrimitiveCreator); +RegistryMSOps g_scaleFusionPrimitiveCreatorRegistry("ScaleFusion", ScaleFusionPrimitiveCreator); +RegistryMSOps g_scatterNdPrimitiveCreatorRegistry("ScatterNd", ScatterNdPrimitiveCreator); +RegistryMSOps g_selectPrimitiveCreatorRegistry("Select", SelectPrimitiveCreator); +RegistryMSOps g_SGDPrimitiveCreatorRegistry("SGD", SGDPrimitiveCreator); +RegistryMSOps g_shapePrimitiveCreatorRegistry("Shape", ShapePrimitiveCreator); +RegistryMSOps g_sigmoidCrossEntropyWithLogitsPrimitiveCreatorRegistry("SigmoidCrossEntropyWithLogits", + SigmoidCrossEntropyWithLogitsPrimitiveCreator); +RegistryMSOps g_sigmoidCrossEntropyWithLogitsGradPrimitiveCreatorRegistry( + "SigmoidCrossEntropyWithLogitsGrad", SigmoidCrossEntropyWithLogitsGradPrimitiveCreator); +RegistryMSOps g_sinPrimitiveCreatorRegistry("Sin", SinPrimitiveCreator); +RegistryMSOps g_sizePrimitiveCreatorRegistry("Size", SizePrimitiveCreator); +RegistryMSOps g_skipGramPrimitiveCreatorRegistry("SkipGram", SkipGramPrimitiveCreator); +RegistryMSOps g_sliceFusionPrimitiveCreatorRegistry("SliceFusion", SliceFusionPrimitiveCreator); +RegistryMSOps g_smoothL1LossPrimitiveCreatorRegistry("SmoothL1Loss", SmoothL1LossPrimitiveCreator); +RegistryMSOps g_smoothL1LossGradPrimitiveCreatorRegistry("SmoothL1LossGrad", SmoothL1LossGradPrimitiveCreator); +RegistryMSOps g_softmaxPrimitiveCreatorRegistry("Softmax", SoftmaxPrimitiveCreator); +RegistryMSOps g_softmaxCrossEntropyWithLogitsPrimitiveCreatorRegistry("SoftmaxCrossEntropyWithLogits", + SoftmaxCrossEntropyWithLogitsPrimitiveCreator); +RegistryMSOps g_spaceToBatchPrimitiveCreatorRegistry("SpaceToBatch", SpaceToBatchPrimitiveCreator); +RegistryMSOps g_spaceToBatchNDPrimitiveCreatorRegistry("SpaceToBatchND", SpaceToBatchNDPrimitiveCreator); +RegistryMSOps g_spaceToDepthPrimitiveCreatorRegistry("SpaceToDepth", SpaceToDepthPrimitiveCreator); +RegistryMSOps g_sparseToDensePrimitiveCreatorRegistry("SparseToDense", SparseToDensePrimitiveCreator); +RegistryMSOps g_splitPrimitiveCreatorRegistry("Split", SplitPrimitiveCreator); +RegistryMSOps g_sqrtPrimitiveCreatorRegistry("Sqrt", SqrtPrimitiveCreator); +RegistryMSOps g_squeezePrimitiveCreatorRegistry("Squeeze", SqueezePrimitiveCreator); +RegistryMSOps g_squarePrimitiveCreatorRegistry("Square", SquarePrimitiveCreator); +RegistryMSOps g_squaredDifferencePrimitiveCreatorRegistry("SquaredDifference", SquaredDifferencePrimitiveCreator); +RegistryMSOps g_stackPrimitiveCreatorRegistry("Stack", StackPrimitiveCreator); +RegistryMSOps g_stridedSlicePrimitiveCreatorRegistry("StridedSlice", StridedSlicePrimitiveCreator); +RegistryMSOps g_subPrimitiveCreatorRegistry("Sub", SubFusionPrimitiveCreator); +RegistryMSOps g_subFusionPrimitiveCreatorRegistry("SubFusion", SubFusionPrimitiveCreator); +RegistryMSOps g_subGradPrimitiveCreatorRegistry("SubGrad", SubGradPrimitiveCreator); +RegistryMSOps g_switchPrimitiveCreatorRegistry("Switch", SwitchPrimitiveCreator); +RegistryMSOps g_tensorListFromTensorPrimitiveCreatorRegistry("TensorListFromTensor", + TensorListFromTensorPrimitiveCreator); +RegistryMSOps g_tensorListGetItemPrimitiveCreatorRegistry("TensorListGetItem", TensorListGetItemPrimitiveCreator); +RegistryMSOps g_tensorListReservePrimitiveCreatorRegistry("TensorListReserve", TensorListReservePrimitiveCreator); +RegistryMSOps g_tensorListSetItemPrimitiveCreatorRegistry("TensorListSetItem", TensorListSetItemPrimitiveCreator); +RegistryMSOps g_tensorListStackPrimitiveCreatorRegistry("TensorListStack", TensorListStackPrimitiveCreator); +RegistryMSOps g_tileFusionPrimitiveCreatorRegistry("TileFusion", TileFusionPrimitiveCreator); +RegistryMSOps g_topKPrimitiveCreatorRegistry("TopK", TopKFusionPrimitiveCreator); +RegistryMSOps g_topKFusionPrimitiveCreatorRegistry("TopKFusion", TopKFusionPrimitiveCreator); +RegistryMSOps g_transposePrimitiveCreatorxRegistry("Transpose", TransposePrimitiveCreator); +RegistryMSOps g_uniquePrimitiveCreatorRegistry("Unique", UniquePrimitiveCreator); +RegistryMSOps g_unstackPrimitiveCreatorRegistry("Unstack", UnstackPrimitiveCreator); +RegistryMSOps g_unsortedSegmentSumPrimitiveCreatorRegistry("UnsortedSegmentSum", UnsortedSegmentSumPrimitiveCreator); +RegistryMSOps g_unsqueezePrimitiveCreatorRegistry("Unsqueeze", UnsqueezePrimitiveCreator); +RegistryMSOps g_wherePrimitiveCreatorRegistry("Where", WherePrimitiveCreator); +RegistryMSOps g_zerosLikePrimitiveCreatorRegistry("ZerosLike", ZerosLikePrimitiveCreator); +} // namespace lite +} // namespace mindspore + +#endif diff --git a/mindspore/lite/src/ops/ops_utils.h b/mindspore/lite/src/ops/ops_utils.h new file mode 100644 index 0000000000..c7b7fcb25e --- /dev/null +++ b/mindspore/lite/src/ops/ops_utils.h @@ -0,0 +1,62 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_OPS_MS_OPS_UTILS_H_ +#define MINDSPORE_LITE_SRC_OPS_MS_OPS_UTILS_H_ + +#include +#include +#include "src/ops/ops_func_declare.h" + +#ifdef PRIMITIVE_WRITEABLE +namespace mindspore { +namespace lite { +typedef schema::PrimitiveT *(*PrimitiveTCreator)(const AnfNodePtr &node); + +class MSOpsRegistry { + public: + static MSOpsRegistry *GetInstance() { + static MSOpsRegistry registry; + return ®istry; + } + void InsertPrimitiveTMap(std::string name, PrimitiveTCreator creator) { primitive_creators[name] = creator; } + PrimitiveTCreator GetPrimitiveCreator(std::string name) { + if (primitive_creators.find(name) != primitive_creators.end()) { + return primitive_creators[name]; + } else { + MS_LOG(ERROR) << "Unsupported primitive type in Create: " << name; + return nullptr; + } + } + + protected: + std::map primitive_creators; +}; + +class RegistryMSOps { + public: + RegistryMSOps(std::string name, PrimitiveTCreator creator) { + MSOpsRegistry::GetInstance()->InsertPrimitiveTMap(name, creator); + } + ~RegistryMSOps() = default; +}; + +schema::PrimitiveT *GetPrimitiveT(const mindspore::AnfNodePtr &node); +} // namespace lite +} // namespace mindspore +#endif + +#endif // MINDSPORE_LITE_SRC_OPS_MS_OPS_UTILS_H_ diff --git a/mindspore/lite/src/ops/p_relu.cc b/mindspore/lite/src/ops/p_relu.cc deleted file mode 100644 index 6600a44396..0000000000 --- a/mindspore/lite/src/ops/p_relu.cc +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/p_relu.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -bool PReLU::GetChannelShared() const { return this->primitive_->value.AsPReLU()->channelShared; } - -void PReLU::SetChannelShared(bool channel_shared) { this->primitive_->value.AsPReLU()->channelShared = channel_shared; } - -#else -bool PReLU::GetChannelShared() const { return this->primitive_->value_as_PReLU()->channelShared(); } - -int PReLU::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_PReLU(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_PReLU return nullptr"; - return RET_ERROR; - } - std::vector slope; - if (attr->slope() != nullptr) { - for (int i = 0; i < static_cast(attr->slope()->size()); i++) { - slope.push_back(attr->slope()->data()[i]); - } - } - auto val_offset = schema::CreatePReLUDirect(*fbb, attr->channelShared(), &slope); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_PReLU, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PReLUCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry PReLURegistry(schema::PrimitiveType_PReLU, PReLUCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/p_relu.h b/mindspore/lite/src/ops/p_relu.h deleted file mode 100644 index c8fb191266..0000000000 --- a/mindspore/lite/src/ops/p_relu.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_P_RELU_H_ -#define LITE_MINDSPORE_LITE_C_OPS_P_RELU_H_ - -#include -#include -#include -#include - -#include "src/ops/activation.h" - -namespace mindspore { -namespace lite { -class PReLU : public Activation { - public: - PReLU() = default; - ~PReLU() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(PReLU, Activation); - explicit PReLU(schema::PrimitiveT *primitive) : Activation(primitive) {} - void SetChannelShared(bool channel_shared); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - bool GetChannelShared() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_P_RELU_H_ diff --git a/mindspore/lite/src/ops/pad.cc b/mindspore/lite/src/ops/pad.cc deleted file mode 100644 index 951b9d4f5f..0000000000 --- a/mindspore/lite/src/ops/pad.cc +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/pad.h" -#include -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector Pad::GetPaddings() const { return this->primitive_->value.AsPad()->paddings; } -int Pad::GetPaddingMode() const { return this->primitive_->value.AsPad()->paddingMode; } -float Pad::GetConstantValue() const { return this->primitive_->value.AsPad()->constantValue; } - -void Pad::SetPaddings(const std::vector &paddings) { this->primitive_->value.AsPad()->paddings = paddings; } -void Pad::SetPaddingMode(int padding_mode) { - this->primitive_->value.AsPad()->paddingMode = (schema::PaddingMode)padding_mode; -} -void Pad::SetConstantValue(float constant_value) { this->primitive_->value.AsPad()->constantValue = constant_value; } -int Pad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Pad; - } - if (this->primitive_->value.type != schema::PrimitiveType_Pad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PadT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - string paddingmode = "REFLECT"; - if (prim.GetAttr("mode") == nullptr) { - if (prim.name() == "Pad") { - paddingmode = "CONSTANT"; - } else { - MS_LOG(ERROR) << "get mode failed!"; - delete this->primitive_; - delete attr; - this->primitive_ = nullptr; - attr = nullptr; - return RET_ERROR; - } - } else { - paddingmode = GetValue(prim.GetAttr("mode")); - } - if (paddingmode == "REFLECT") { - attr->paddingMode = schema::PaddingMode_REFLECT; - } else if (paddingmode == "SYMMETRIC") { - attr->paddingMode = schema::PaddingMode_SYMMETRIC; - } else if (paddingmode == "CONSTANT") { - attr->paddingMode = schema::PaddingMode_CONSTANT; - if (prim.GetAttr("paddings") != nullptr) { - auto paddings = prim.GetAttr("paddings"); - auto str = (*paddings).ToString(); - std::replace(str.begin(), str.end(), ',', ' '); - std::replace(str.begin(), str.end(), ')', ' '); - std::replace(str.begin(), str.end(), '(', ' '); - std::stringstream ss(str); - for (int i; ss >> i;) { - attr->paddings.push_back(i); - } - } - } else { - MS_LOG(ERROR) << "model type not supported!"; - delete this->primitive_; - delete attr; - this->primitive_ = nullptr; - attr = nullptr; - return RET_ERROR; - } - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else - -std::vector Pad::GetPaddings() const { - auto fb_vector = this->primitive_->value_as_Pad()->paddings(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Pad::GetPaddingMode() const { return this->primitive_->value_as_Pad()->paddingMode(); } -float Pad::GetConstantValue() const { return this->primitive_->value_as_Pad()->constantValue(); } - -int Pad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Pad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Pad return nullptr"; - return RET_ERROR; - } - std::vector paddings; - if (attr->paddings() != nullptr) { - for (int i = 0; i < static_cast(attr->paddings()->size()); i++) { - paddings.push_back(attr->paddings()->data()[i]); - } - } - auto val_offset = schema::CreatePadDirect(*fbb, &paddings, attr->paddingMode(), attr->constantValue()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Pad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PadCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry PadRegistry(schema::PrimitiveType_Pad, PadCreator); -#endif - -int GetPaddingFromInput(const std::vector &inputs, std::vector *paddings) { - auto paddings_tensor = inputs.at(1); - int rank = static_cast(inputs.front()->shape().size()); - MS_ASSERT(paddings_tensor->ElementsNum() == 2 * rank); - if (paddings_tensor->data_c() == nullptr) { - return RET_INFER_ERR; - } - paddings->clear(); - if (paddings_tensor->data_type() == mindspore::kNumberTypeInt64) { - auto paddings_data = reinterpret_cast(paddings_tensor->data_c()); - for (auto i = 0; i < rank; ++i) { - paddings->emplace_back(paddings_data[i * 2]); - paddings->emplace_back(paddings_data[i * 2 + 1]); - } - } else if (paddings_tensor->data_type() == mindspore::kNumberTypeInt32) { - auto paddings_data = reinterpret_cast(paddings_tensor->data_c()); - for (auto i = 0; i < rank; ++i) { - paddings->emplace_back(paddings_data[i * 2]); - paddings->emplace_back(paddings_data[i * 2 + 1]); - } - } - return RET_OK; -} - -int Pad::InferShape(std::vector inputs, std::vector outputs) { - MS_ASSERT(this->primitive_ != nullptr); - if (this->primitive_ == nullptr) { - return RET_NULL_PTR; - } - - auto input = inputs.front(); - if (input == nullptr) { - return RET_NULL_PTR; - } - auto output = outputs.front(); - if (output == nullptr) { - return RET_NULL_PTR; - } - output->set_format(input->format()); - output->set_data_type(input->data_type()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector paddings; - if (inputs.size() == 1) { - paddings = GetPaddings(); - } else { - GetPaddingFromInput(inputs, &paddings); - } - - if (paddings.empty()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - std::vector output_shape; - MS_ASSERT(input->shape().size() <= 4); - for (size_t i = 0; i < input_shape.size(); i++) { - auto paddings_index = i; - auto shape = input_shape.at(i) + paddings.at(2 * paddings_index) + paddings.at(2 * paddings_index + 1); - output_shape.push_back(shape); - } - - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/pad.h b/mindspore/lite/src/ops/pad.h deleted file mode 100644 index af18c746a5..0000000000 --- a/mindspore/lite/src/ops/pad.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_PAD_H_ -#define LITE_MINDSPORE_LITE_C_OPS_PAD_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Pad : public PrimitiveC { - public: - Pad() = default; - ~Pad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Pad, PrimitiveC); - explicit Pad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetPaddings(const std::vector &paddings); - void SetPaddingMode(int padding_mode); - void SetConstantValue(float constant_value); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetPaddings() const; - int GetPaddingMode() const; - float GetConstantValue() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_PAD_H_ diff --git a/mindspore/lite/src/ops/partial.cc b/mindspore/lite/src/ops/partial.cc deleted file mode 100644 index deb4d80b20..0000000000 --- a/mindspore/lite/src/ops/partial.cc +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/partial.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE - -int Partial::GetSubGraphIndex() const { return this->primitive_->value.AsPartial()->subGraphIndex; } - -int Partial::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Partial; - } - if (this->primitive_->value.type != schema::PrimitiveType_Partial) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PartialT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else - -int Partial::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Partial(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Partial return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreatePartial(*fbb, attr->subGraphIndex()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Partial, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int Partial::GetSubGraphIndex() const { return this->primitive_->value_as_Partial()->subGraphIndex(); } - -PrimitiveC *PartialCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry PartialRegistry(schema::PrimitiveType_Partial, PartialCreator); - -#endif - -int Partial::InferShape(std::vector inputs_, std::vector outputs_) { return RET_OK; } -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/partial.h b/mindspore/lite/src/ops/partial.h deleted file mode 100644 index 66c680c845..0000000000 --- a/mindspore/lite/src/ops/partial.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_PARTIAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_PARTIAL_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Partial : public PrimitiveC { - public: - Partial() = default; - ~Partial() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Partial, PrimitiveC); - explicit Partial(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetSubGraphIndex() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_PARTIAL_H_ diff --git a/mindspore/lite/src/ops/pooling.cc b/mindspore/lite/src/ops/pooling.cc deleted file mode 100644 index 271418520a..0000000000 --- a/mindspore/lite/src/ops/pooling.cc +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/pooling.h" -#include -#include -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -#ifdef PRIMITIVE_WRITEABLE -int Pooling::GetFormat() const { return this->primitive_->value.AsPooling()->format; } -int Pooling::GetPoolingMode() const { return this->primitive_->value.AsPooling()->poolingMode; } -bool Pooling::GetGlobal() const { return this->primitive_->value.AsPooling()->global; } -int Pooling::GetWindowW() const { return this->primitive_->value.AsPooling()->windowW; } -int Pooling::GetWindowH() const { return this->primitive_->value.AsPooling()->windowH; } -int Pooling::GetStrideW() const { return this->primitive_->value.AsPooling()->strideW; } -int Pooling::GetStrideH() const { return this->primitive_->value.AsPooling()->strideH; } -int Pooling::GetPadMode() const { return this->primitive_->value.AsPooling()->padMode; } -int Pooling::GetPadUp() const { return this->primitive_->value.AsPooling()->padUp; } -int Pooling::GetPadDown() const { return this->primitive_->value.AsPooling()->padDown; } -int Pooling::GetPadLeft() const { return this->primitive_->value.AsPooling()->padLeft; } -int Pooling::GetPadRight() const { return this->primitive_->value.AsPooling()->padRight; } -int Pooling::GetRoundMode() const { return this->primitive_->value.AsPooling()->roundMode; } -int Pooling::GetActivationType() const { return this->primitive_->value.AsPooling()->activationType; } -int Pooling::GetAvgMode() const { return this->primitive_->value.AsPooling()->avgMode; } - -void Pooling::SetFormat(int format) { this->primitive_->value.AsPooling()->format = (schema::Format)format; } -void Pooling::SetPoolingMode(int pooling_mode) { - this->primitive_->value.AsPooling()->poolingMode = (schema::PoolMode)pooling_mode; -} -void Pooling::SetGlobal(bool global) { this->primitive_->value.AsPooling()->global = global; } -void Pooling::SetWindowW(int window_w) { this->primitive_->value.AsPooling()->windowW = window_w; } -void Pooling::SetWindowH(int window_h) { this->primitive_->value.AsPooling()->windowH = window_h; } -void Pooling::SetStrideW(int stride_w) { this->primitive_->value.AsPooling()->strideW = stride_w; } -void Pooling::SetStrideH(int stride_h) { this->primitive_->value.AsPooling()->strideH = stride_h; } -void Pooling::SetPadMode(int pad_mode) { this->primitive_->value.AsPooling()->padMode = (schema::PadMode)pad_mode; } -void Pooling::SetPadUp(int pad_up) { this->primitive_->value.AsPooling()->padUp = pad_up; } -void Pooling::SetPadDown(int pad_down) { this->primitive_->value.AsPooling()->padDown = pad_down; } -void Pooling::SetPadLeft(int pad_left) { this->primitive_->value.AsPooling()->padLeft = pad_left; } -void Pooling::SetPadRight(int pad_right) { this->primitive_->value.AsPooling()->padRight = pad_right; } -void Pooling::SetRoundMode(int round_mode) { - this->primitive_->value.AsPooling()->roundMode = (schema::RoundMode)round_mode; -} -void Pooling::SetActivationType(int activation_type) { - this->primitive_->value.AsPooling()->activationType = (schema::ActivationType)activation_type; -} -void Pooling::SetAvgMode(int avg_mode) { this->primitive_->value.AsPooling()->avgMode = avg_mode; } - -int Pooling::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Pooling; - } - if (this->primitive_->value.type != schema::PrimitiveType_Pooling) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PoolingT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.instance_name() == "MaxPool") { - attr->poolingMode = schema::PoolMode_MAX_POOLING; - } else if (prim.instance_name() == "MeanPool" || prim.instance_name() == "AvgPool") { - attr->poolingMode = schema::PoolMode_MEAN_POOLING; - } - - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format::Format_NHWC; - } else { - attr->format = schema::Format::Format_NUM_OF_FORMAT; - } - - auto pad_mode = GetValue(prim.GetAttr("padding")); - if (pad_mode == "VALID") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "SAME") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - auto kernel_size = CastToInt(prim.GetAttr("ksize")); - attr->windowH = kernel_size.at(2); - attr->windowW = kernel_size.at(3); - - auto stride = CastToInt(prim.GetAttr("strides")); - attr->strideH = stride.at(2); - attr->strideW = stride.at(3); - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - - return RET_OK; -} - -#else - -int Pooling::GetFormat() const { return this->primitive_->value_as_Pooling()->format(); } -int Pooling::GetPoolingMode() const { return this->primitive_->value_as_Pooling()->poolingMode(); } -bool Pooling::GetGlobal() const { return this->primitive_->value_as_Pooling()->global(); } -int Pooling::GetWindowW() const { return this->primitive_->value_as_Pooling()->windowW(); } -int Pooling::GetWindowH() const { return this->primitive_->value_as_Pooling()->windowH(); } -int Pooling::GetStrideW() const { return this->primitive_->value_as_Pooling()->strideW(); } -int Pooling::GetStrideH() const { return this->primitive_->value_as_Pooling()->strideH(); } -int Pooling::GetPadMode() const { return this->primitive_->value_as_Pooling()->padMode(); } -int Pooling::GetPadUp() const { return this->primitive_->value_as_Pooling()->padUp(); } -int Pooling::GetPadDown() const { return this->primitive_->value_as_Pooling()->padDown(); } -int Pooling::GetPadLeft() const { return this->primitive_->value_as_Pooling()->padLeft(); } -int Pooling::GetPadRight() const { return this->primitive_->value_as_Pooling()->padRight(); } -int Pooling::GetRoundMode() const { return this->primitive_->value_as_Pooling()->roundMode(); } -int Pooling::GetActivationType() const { return this->primitive_->value_as_Pooling()->activationType(); } -int Pooling::GetAvgMode() const { return this->primitive_->value_as_Pooling()->avgMode(); } - -int Pooling::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Pooling(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Pooling return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreatePooling(*fbb, attr->format(), attr->poolingMode(), attr->global(), attr->windowW(), - attr->windowH(), attr->strideW(), attr->strideH(), attr->padMode(), - attr->padUp(), attr->padDown(), attr->padLeft(), attr->padRight(), - attr->roundMode(), attr->activationType(), attr->avgMode()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Pooling, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PoolingCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry PoolingRegistry(schema::PrimitiveType_Pooling, PoolingCreator); - -#endif - -int Pooling::PadUp() const { return this->pad_u_; } -int Pooling::PadDown() const { return this->pad_d_; } -int Pooling::PadLeft() const { return this->pad_l_; } -int Pooling::PadRight() const { return this->pad_r_; } - -int Pooling::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(schema::Format::Format_NHWC); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - int input_h = input->shape().at(1); - int input_w = input->shape().at(2); - - auto window_h = GetWindowH(); - auto window_w = GetWindowW(); - if (GetGlobal()) { - window_h = input_h; - window_w = input_w; - } - int output_h = 0; - int output_w = 0; - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - if (GetPadMode() == schema::PadMode_SAME_UPPER) { - output_w = std::ceil(static_cast(input_w) / static_cast(GetStrideW())); - output_h = std::ceil(static_cast(input_h) / static_cast(GetStrideH())); - auto pad_h_all = ((output_h - 1) * GetStrideH() + (window_h - 1) + 1 - input_h); - auto pad_w_all = ((output_w - 1) * GetStrideW() + (window_w - 1) + 1 - input_w); - if (pad_h_all < 0) { - pad_u_ = pad_d_ = 0; - } else { - pad_u_ = pad_h_all / 2; - pad_d_ = pad_h_all - pad_u_; - } - if (pad_w_all < 0) { - pad_l_ = pad_r_ = 0; - } else { - pad_l_ = pad_w_all / 2; - pad_r_ = pad_w_all - pad_l_; - } - } else { - auto round_mode = (schema::RoundMode)GetRoundMode(); - if (round_mode == schema::RoundMode_FLOOR) { - output_h = std::floor(static_cast(input_h + pad_u_ + pad_d_ - window_h) / GetStrideH()) + 1; - output_w = std::floor(static_cast(input_w + pad_l_ + pad_r_ - window_w) / GetStrideW()) + 1; - } else if (round_mode == schema::RoundMode_CEIL) { - output_h = std::ceil(static_cast(input_h + pad_u_ + pad_d_ - window_h) / GetStrideH()) + 1; - output_w = std::ceil(static_cast(input_w + pad_l_ + pad_r_ - window_w) / GetStrideW()) + 1; - } else { - MS_LOG(ERROR) << "unsupported round mode."; - } - } - auto input_shape = input->shape(); - input_shape.at(1) = output_h > 0 ? output_h : 1; - input_shape.at(2) = output_w > 0 ? output_w : 1; - output->set_shape(input_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/pooling.h b/mindspore/lite/src/ops/pooling.h deleted file mode 100644 index 5e7572ffa3..0000000000 --- a/mindspore/lite/src/ops/pooling.h +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ -#define LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Pooling : public PrimitiveC { - public: - Pooling() = default; - ~Pooling() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Pooling, PrimitiveC); - explicit Pooling(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetPoolingMode(int pooling_mode); - void SetGlobal(bool global); - void SetWindowW(int window_w); - void SetWindowH(int window_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetRoundMode(int round_mode); - void SetActivationType(int activation_type); - void SetAvgMode(int avg_mode); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetPoolingMode() const; - bool GetGlobal() const; - int GetWindowW() const; - int GetWindowH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetRoundMode() const; - int GetActivationType() const; - int GetAvgMode() const; - - int PadUp() const; - int PadDown() const; - int PadLeft() const; - int PadRight() const; - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; -}; // namespace lite -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ diff --git a/mindspore/lite/src/ops/pooling_grad.cc b/mindspore/lite/src/ops/pooling_grad.cc deleted file mode 100644 index 47825e9709..0000000000 --- a/mindspore/lite/src/ops/pooling_grad.cc +++ /dev/null @@ -1,215 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/pooling_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int PoolingGrad::GetFormat() const { return this->primitive_->value.AsPoolingGrad()->format; } -int PoolingGrad::GetPoolingMode() const { return this->primitive_->value.AsPoolingGrad()->poolingMode; } -bool PoolingGrad::GetGlobal() const { return this->primitive_->value.AsPoolingGrad()->global; } -int PoolingGrad::GetWindowW() const { return this->primitive_->value.AsPoolingGrad()->windowW; } -int PoolingGrad::GetWindowH() const { return this->primitive_->value.AsPoolingGrad()->windowH; } -int PoolingGrad::GetStrideW() const { return this->primitive_->value.AsPoolingGrad()->strideW; } -int PoolingGrad::GetStrideH() const { return this->primitive_->value.AsPoolingGrad()->strideH; } -int PoolingGrad::GetPadMode() const { return this->primitive_->value.AsPoolingGrad()->padMode; } -int PoolingGrad::GetPadUp() const { return this->primitive_->value.AsPoolingGrad()->padUp; } -int PoolingGrad::GetPadDown() const { return this->primitive_->value.AsPoolingGrad()->padDown; } -int PoolingGrad::GetPadLeft() const { return this->primitive_->value.AsPoolingGrad()->padLeft; } -int PoolingGrad::GetPadRight() const { return this->primitive_->value.AsPoolingGrad()->padRight; } -int PoolingGrad::GetRoundMode() const { return this->primitive_->value.AsPoolingGrad()->roundMode; } - -void PoolingGrad::SetFormat(int format) { this->primitive_->value.AsPoolingGrad()->format = (schema::Format)format; } -void PoolingGrad::SetPoolingMode(int pooling_mode) { - this->primitive_->value.AsPoolingGrad()->poolingMode = (schema::PoolMode)pooling_mode; -} -void PoolingGrad::SetGlobal(bool global) { this->primitive_->value.AsPoolingGrad()->global = global; } -void PoolingGrad::SetWindowW(int window_w) { this->primitive_->value.AsPoolingGrad()->windowW = window_w; } -void PoolingGrad::SetWindowH(int window_h) { this->primitive_->value.AsPoolingGrad()->windowH = window_h; } -void PoolingGrad::SetStrideW(int stride_w) { this->primitive_->value.AsPoolingGrad()->strideW = stride_w; } -void PoolingGrad::SetStrideH(int stride_h) { this->primitive_->value.AsPoolingGrad()->strideH = stride_h; } -void PoolingGrad::SetPadMode(int pad_mode) { - this->primitive_->value.AsPoolingGrad()->padMode = (schema::PadMode)pad_mode; -} -void PoolingGrad::SetPadUp(int pad_up) { this->primitive_->value.AsPoolingGrad()->padUp = pad_up; } -void PoolingGrad::SetPadDown(int pad_down) { this->primitive_->value.AsPoolingGrad()->padDown = pad_down; } -void PoolingGrad::SetPadLeft(int pad_left) { this->primitive_->value.AsPoolingGrad()->padLeft = pad_left; } -void PoolingGrad::SetPadRight(int pad_right) { this->primitive_->value.AsPoolingGrad()->padRight = pad_right; } -void PoolingGrad::SetRoundMode(int round_mode) { - this->primitive_->value.AsPoolingGrad()->roundMode = (schema::RoundMode)round_mode; -} -int PoolingGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_PoolingGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_PoolingGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PoolingGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - - auto format = GetValue(prim.GetAttr("data_format")); - if (format == "NCHW") { - attr->format = schema::Format_NCHW; - } else if (format == "NHWC") { - attr->format = schema::Format_NHWC; - } else { - attr->format = schema::Format_NUM_OF_FORMAT; - } - - if (prim.instance_name() == "MaxPoolGrad") { - attr->poolingMode = schema::PoolMode_MAX_POOLING; - } else if (prim.instance_name() == "AvgPoolGrad") { - attr->poolingMode = schema::PoolMode_MEAN_POOLING; - } else if (prim.instance_name() == "AvgPoolGradGpu") { - attr->poolingMode = schema::PoolMode_MEAN_POOLING; - } else if (prim.instance_name() == "AvgPoolGradCpu") { - attr->poolingMode = schema::PoolMode_MEAN_POOLING; - } else { - attr->poolingMode = schema::PoolMode_MAX_POOLING; - } - - auto pad_mode = GetValue(prim.GetAttr("padding")); - if (pad_mode == "VALID") { - attr->padMode = schema::PadMode_VALID; - } else if (pad_mode == "SAME") { - attr->padMode = schema::PadMode_SAME_UPPER; - } else { - attr->padMode = schema::PadMode_NOTSET; - } - - auto kernel_size = CastToInt(prim.GetAttr("ksize")); - attr->windowH = kernel_size.at(2); - attr->windowW = kernel_size.at(3); - - auto stride = CastToInt(prim.GetAttr("strides")); - attr->strideH = stride.at(2); - attr->strideW = stride.at(3); - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -int PoolingGrad::GetFormat() const { return this->primitive_->value_as_PoolingGrad()->format(); } -int PoolingGrad::GetPoolingMode() const { return this->primitive_->value_as_PoolingGrad()->poolingMode(); } -bool PoolingGrad::GetGlobal() const { return this->primitive_->value_as_PoolingGrad()->global(); } -int PoolingGrad::GetWindowW() const { return this->primitive_->value_as_PoolingGrad()->windowW(); } -int PoolingGrad::GetWindowH() const { return this->primitive_->value_as_PoolingGrad()->windowH(); } -int PoolingGrad::GetStrideW() const { return this->primitive_->value_as_PoolingGrad()->strideW(); } -int PoolingGrad::GetStrideH() const { return this->primitive_->value_as_PoolingGrad()->strideH(); } -int PoolingGrad::GetPadMode() const { return this->primitive_->value_as_PoolingGrad()->padMode(); } -int PoolingGrad::GetPadUp() const { return this->primitive_->value_as_PoolingGrad()->padUp(); } -int PoolingGrad::GetPadDown() const { return this->primitive_->value_as_PoolingGrad()->padDown(); } -int PoolingGrad::GetPadLeft() const { return this->primitive_->value_as_PoolingGrad()->padLeft(); } -int PoolingGrad::GetPadRight() const { return this->primitive_->value_as_PoolingGrad()->padRight(); } -int PoolingGrad::GetRoundMode() const { return this->primitive_->value_as_PoolingGrad()->roundMode(); } - -int PoolingGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_PoolingGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_PoolingGrad return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreatePoolingGrad(*fbb, attr->format(), attr->poolingMode(), attr->global(), attr->windowW(), - attr->windowH(), attr->strideW(), attr->strideH(), attr->padMode(), attr->padUp(), - attr->padDown(), attr->padLeft(), attr->padRight(), attr->roundMode()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_PoolingGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PoolingGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry PoolingGradRegistry(schema::PrimitiveType_PoolingGrad, PoolingGradCreator); -#endif - -int PoolingGrad::InferShape(std::vector inputs_, std::vector outputs_) { - if (3 != inputs_.size()) { - MS_LOG(ERROR) << "Pooling Grad Filter should have 3 inputs"; - return RET_ERROR; - } - if (1 != outputs_.size()) { - MS_LOG(ERROR) << "Pooling Grad Filter should have one output"; - return RET_ERROR; - } - - auto input = inputs_.at(0); - MS_ASSERT(input != nullptr); - int input_h = input->shape().at(1); - int input_w = input->shape().at(2); - - auto window_h = GetWindowH(); - auto window_w = GetWindowW(); - if (GetGlobal()) { - window_h = input_h; - window_w = input_w; - } - - pad_l_ = GetPadLeft(); - pad_u_ = GetPadUp(); - pad_d_ = GetPadDown(); - pad_r_ = GetPadRight(); - if (GetPadMode() == schema::PadMode_SAME_UPPER) { - int output_w = std::ceil(static_cast(input_w) / static_cast(GetStrideW())); - int output_h = std::ceil(static_cast(input_h) / static_cast(GetStrideH())); - auto pad_h_all = ((output_h - 1) * GetStrideH() + (window_h - 1) + 1 - input_h); - auto pad_w_all = ((output_w - 1) * GetStrideW() + (window_w - 1) + 1 - input_w); - if (pad_h_all < 0) { - pad_u_ = pad_d_ = 0; - } else { - pad_u_ = pad_h_all / 2; - pad_d_ = pad_h_all - pad_u_; - } - if (pad_w_all < 0) { - pad_l_ = pad_r_ = 0; - } else { - pad_l_ = pad_w_all / 2; - pad_r_ = pad_w_all - pad_l_; - } - } - auto grad_output = outputs_.at(0); - auto output_shape = input->shape(); - grad_output->set_shape(output_shape); - grad_output->set_data_type(input->data_type()); - grad_output->set_format(input->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/pooling_grad.h b/mindspore/lite/src/ops/pooling_grad.h deleted file mode 100644 index 1f47d57e60..0000000000 --- a/mindspore/lite/src/ops/pooling_grad.h +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_POOLING_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_POOLING_GRAD_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class PoolingGrad : public PrimitiveC { - public: - PoolingGrad() = default; - ~PoolingGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(PoolingGrad, PrimitiveC); - explicit PoolingGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetPoolingMode(int pooling_mode); - void SetGlobal(bool global); - void SetWindowW(int window_w); - void SetWindowH(int window_h); - void SetStrideW(int stride_w); - void SetStrideH(int stride_h); - void SetPadMode(int pad_mode); - void SetPadUp(int pad_up); - void SetPadDown(int pad_down); - void SetPadLeft(int pad_left); - void SetPadRight(int pad_right); - void SetRoundMode(int round_mode); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetPoolingMode() const; - bool GetGlobal() const; - int GetWindowW() const; - int GetWindowH() const; - int GetStrideW() const; - int GetStrideH() const; - int GetPadMode() const; - int GetPadUp() const; - int GetPadDown() const; - int GetPadLeft() const; - int GetPadRight() const; - int GetRoundMode() const; - - protected: - int pad_u_ = 0; - int pad_d_ = 0; - int pad_l_ = 0; - int pad_r_ = 0; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_POOLING_GRAD_H_ diff --git a/mindspore/lite/src/ops/populate/activation_grad_populate.cc b/mindspore/lite/src/ops/populate/activation_grad_populate.cc index 54cfcbc0f7..f832719648 100644 --- a/mindspore/lite/src/ops/populate/activation_grad_populate.cc +++ b/mindspore/lite/src/ops/populate/activation_grad_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/activation_grad.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32_grad/activation_grad.h" namespace mindspore { namespace lite { -OpParameter *PopulateActivationGradParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateActivationGradParameter(const void *prim) { ActivationGradParameter *act_param = reinterpret_cast(malloc(sizeof(ActivationGradParameter))); if (act_param == nullptr) { @@ -29,13 +26,15 @@ OpParameter *PopulateActivationGradParameter(const mindspore::lite::PrimitiveC * return nullptr; } memset(act_param, 0, sizeof(ActivationGradParameter)); - act_param->op_parameter.type_ = primitive->Type(); - auto activation = - reinterpret_cast(const_cast(primitive)); - act_param->type_ = static_cast(activation->GetType()); - act_param->alpha_ = activation->GetAlpha(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_ActivationGrad(); + act_param->op_parameter.type_ = primitive->value_type(); + act_param->type_ = static_cast(value->activation_type()); + act_param->alpha_ = value->alpha(); return reinterpret_cast(act_param); } -Registry ActivationGradParameterRegistry(schema::PrimitiveType_ActivationGrad, PopulateActivationGradParameter); +Registry ActivationGradParameterRegistry(schema::PrimitiveType_ActivationGrad, PopulateActivationGradParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/activation_populate.cc b/mindspore/lite/src/ops/populate/activation_populate.cc index 82a4e99046..1f0e8c9e01 100644 --- a/mindspore/lite/src/ops/populate/activation_populate.cc +++ b/mindspore/lite/src/ops/populate/activation_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/activation.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/activation_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateActivationParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateRelu6Parameter(const void *prim) { ActivationParameter *act_param = reinterpret_cast(malloc(sizeof(ActivationParameter))); if (act_param == nullptr) { MS_LOG(ERROR) << "malloc ActivationParameter failed."; return nullptr; } memset(act_param, 0, sizeof(ActivationParameter)); - act_param->op_parameter_.type_ = primitive->Type(); - auto activation = - reinterpret_cast(const_cast(primitive)); - act_param->type_ = static_cast(activation->GetType()); - act_param->alpha_ = activation->GetAlpha(); - act_param->min_val_ = activation->GetMinVal(); - act_param->max_val_ = activation->GetMaxVal(); + auto primitive = static_cast(prim); + act_param->op_parameter_.type_ = primitive->value_type(); + auto acti_prim = primitive->value_as_Activation(); + act_param->type_ = static_cast(acti_prim->activation_type()); + act_param->alpha_ = acti_prim->alpha(); + act_param->min_val_ = acti_prim->min_val(); + act_param->max_val_ = acti_prim->max_val(); return reinterpret_cast(act_param); } -Registry ActivationParameterRegistry(schema::PrimitiveType_Activation, PopulateActivationParameter); +} // namespace + +Registry g_relu6ParameterRegistry(schema::PrimitiveType_Activation, PopulateRelu6Parameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/adam_populate.cc b/mindspore/lite/src/ops/populate/adam_populate.cc index ec06f36589..38682785ee 100644 --- a/mindspore/lite/src/ops/populate/adam_populate.cc +++ b/mindspore/lite/src/ops/populate/adam_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/adam.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateAdamParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateAdamParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc Adam Parameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry AdamParameterRegistry(schema::PrimitiveType_Adam, PopulateAdamParameter); +Registry AdamParameterRegistry(schema::PrimitiveType_Adam, PopulateAdamParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/add_populate.cc b/mindspore/lite/src/ops/populate/add_populate.cc index e2722ff084..ff5ac84dc3 100644 --- a/mindspore/lite/src/ops/populate/add_populate.cc +++ b/mindspore/lite/src/ops/populate/add_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/add.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arithmetic.h" #include "src/ops/populate/arithmetic_populate.h" namespace mindspore { namespace lite { -OpParameter *PopulateAddParameter(const mindspore::lite::PrimitiveC *primitive) { - ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); +namespace { +OpParameter *PopulateAddParameter(const void *prim) { + ArithmeticParameter *param = PopulateArithmeticCommonPara(prim); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; return nullptr; } - param->activation_type_ = reinterpret_cast(primitive)->GetActivationType(); + auto *primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); + auto add_prim = primitive->value_as_AddFusion(); + param->activation_type_ = add_prim->activation_type(); return reinterpret_cast(param); } -Registry AddParameterRegistry(schema::PrimitiveType_Add, PopulateAddParameter); - +} // namespace +Registry g_addParameterRegistry(schema::PrimitiveType_AddFusion, PopulateAddParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/adder_populate.cc b/mindspore/lite/src/ops/populate/adder_populate.cc index 59ab043381..295a814306 100644 --- a/mindspore/lite/src/ops/populate/adder_populate.cc +++ b/mindspore/lite/src/ops/populate/adder_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,42 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/adder.h" #include "src/common/log_adapter.h" #include "nnacl/conv_parameter.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateAdderParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateAdderParameter(const void *prim) { ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); if (conv_param == nullptr) { MS_LOG(ERROR) << "malloc ConvParameter failed."; return nullptr; } memset(conv_param, 0, sizeof(ConvParameter)); - conv_param->op_parameter_.type_ = primitive->Type(); - auto adder_primitive = - reinterpret_cast(const_cast(primitive)); - conv_param->kernel_h_ = adder_primitive->GetKernelH(); - conv_param->kernel_w_ = adder_primitive->GetKernelW(); - conv_param->group_ = adder_primitive->GetGroup(); - conv_param->stride_h_ = adder_primitive->GetStrideH(); - conv_param->stride_w_ = adder_primitive->GetStrideW(); - auto adder_lite_primitive = (lite::Adder *)primitive; - conv_param->pad_u_ = adder_lite_primitive->PadUp(); - conv_param->pad_d_ = adder_lite_primitive->PadDown(); - conv_param->pad_l_ = adder_lite_primitive->PadLeft(); - conv_param->pad_r_ = adder_lite_primitive->PadRight(); - conv_param->dilation_h_ = adder_primitive->GetDilateH(); - conv_param->dilation_w_ = adder_primitive->GetDilateW(); - conv_param->input_channel_ = adder_primitive->GetChannelIn(); - conv_param->output_channel_ = adder_primitive->GetChannelOut(); - conv_param->group_ = adder_primitive->GetGroup(); - auto act_type = adder_primitive->GetActivationType(); + auto primitive = static_cast(prim); + conv_param->op_parameter_.type_ = primitive->value_type(); + auto conv_primitive = primitive->value_as_AdderFusion(); + conv_param->kernel_h_ = static_cast(*(conv_primitive->kernel_size()->begin())); + conv_param->kernel_w_ = static_cast(*(conv_primitive->kernel_size()->begin() + 1)); + conv_param->group_ = static_cast(conv_primitive->group()); + conv_param->stride_h_ = static_cast(*(conv_primitive->stride()->begin())); + conv_param->stride_w_ = static_cast(*(conv_primitive->stride()->begin() + 1)); + conv_param->pad_u_ = static_cast(*(conv_primitive->pad_list()->begin())); + conv_param->pad_d_ = static_cast(*(conv_primitive->pad_list()->begin() + 1)); + conv_param->pad_l_ = static_cast(*(conv_primitive->pad_list()->begin() + 2)); + conv_param->pad_r_ = static_cast(*(conv_primitive->pad_list()->begin() + 3)); + conv_param->dilation_h_ = static_cast(*(conv_primitive->dilation()->begin())); + conv_param->dilation_w_ = static_cast(*(conv_primitive->dilation()->begin() + 1)); + conv_param->input_channel_ = static_cast(conv_primitive->in_channel()); + conv_param->output_channel_ = static_cast(conv_primitive->out_channel()); + auto act_type = conv_primitive->activation_type(); switch (act_type) { case schema::ActivationType_RELU: conv_param->act_type_ = ActType_Relu; @@ -62,6 +57,6 @@ OpParameter *PopulateAdderParameter(const mindspore::lite::PrimitiveC *primitive } return reinterpret_cast(conv_param); } -Registry AdderParameterRegistry(schema::PrimitiveType_Adder, PopulateAdderParameter); +Registry g_AdderParameterRegistry(schema::PrimitiveType_AdderFusion, PopulateAdderParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/addn_populate.cc b/mindspore/lite/src/ops/populate/addn_populate.cc index 22aacebc72..7932356f7a 100644 --- a/mindspore/lite/src/ops/populate/addn_populate.cc +++ b/mindspore/lite/src/ops/populate/addn_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,23 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/op_base.h" namespace mindspore { namespace lite { -OpParameter *PopulateAddNParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateAddNParameter(const void *prim) { OpParameter *addn_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (addn_param == nullptr) { MS_LOG(ERROR) << "malloc OpParameter failed."; return nullptr; } memset(addn_param, 0, sizeof(OpParameter)); - addn_param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + addn_param->type_ = primitive->value_type(); return reinterpret_cast(addn_param); } -Registry AddNParameterRegistry(schema::PrimitiveType_AddN, PopulateAddNParameter); +} // namespace +Registry g_addNParameterRegistry(schema::PrimitiveType_AddN, PopulateAddNParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/argmax_populate.cc b/mindspore/lite/src/ops/populate/argmax_populate.cc index 387001de96..99da4b97c1 100644 --- a/mindspore/lite/src/ops/populate/argmax_populate.cc +++ b/mindspore/lite/src/ops/populate/argmax_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,32 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/argmax.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arg_min_max_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateArgMaxParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateArgMaxParameter(const void *prim) { ArgMinMaxParameter *arg_param = reinterpret_cast(malloc(sizeof(ArgMinMaxParameter))); if (arg_param == nullptr) { MS_LOG(ERROR) << "malloc ArgMinMaxParameter failed."; return nullptr; } memset(arg_param, 0, sizeof(ArgMinMaxParameter)); - arg_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - arg_param->axis_ = param->GetAxis(); - arg_param->topk_ = param->GetTopK(); - arg_param->axis_type_ = param->GetAxisType(); - arg_param->out_value_ = param->GetOutMaxValue(); - arg_param->keep_dims_ = param->GetKeepDims(); + auto *primitive = static_cast(prim); + arg_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_ArgMaxFusion(); + arg_param->axis_ = param->axis(); + arg_param->topk_ = param->top_k(); + arg_param->out_value_ = param->out_max_value(); + arg_param->keep_dims_ = param->keep_dims(); arg_param->get_max_ = true; return reinterpret_cast(arg_param); } +} // namespace -Registry ArgMaxParameterRegistry(schema::PrimitiveType_ArgMax, PopulateArgMaxParameter); +Registry g_argMaxParameterRegistry(schema::PrimitiveType_ArgMaxFusion, PopulateArgMaxParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/argmin_populate.cc b/mindspore/lite/src/ops/populate/argmin_populate.cc index 61c98355f8..630260526e 100644 --- a/mindspore/lite/src/ops/populate/argmin_populate.cc +++ b/mindspore/lite/src/ops/populate/argmin_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,32 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/argmin.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arg_min_max_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateArgMinParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateArgMinParameter(const void *prim) { ArgMinMaxParameter *arg_param = reinterpret_cast(malloc(sizeof(ArgMinMaxParameter))); if (arg_param == nullptr) { MS_LOG(ERROR) << "malloc ArgMinMaxParameter failed."; return nullptr; } memset(arg_param, 0, sizeof(ArgMinMaxParameter)); - arg_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - arg_param->axis_ = param->GetAxis(); - arg_param->topk_ = param->GetTopK(); - arg_param->axis_type_ = param->GetAxisType(); - arg_param->out_value_ = param->GetOutMaxValue(); - arg_param->keep_dims_ = param->GetKeepDims(); + auto *primitive = static_cast(prim); + arg_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_ArgMinFusion(); + arg_param->axis_ = param->axis(); + arg_param->topk_ = param->top_k(); + arg_param->out_value_ = param->out_max_value(); + arg_param->keep_dims_ = param->keep_dims(); arg_param->get_max_ = false; return reinterpret_cast(arg_param); } +} // namespace -Registry ArgMinParameterRegistry(schema::PrimitiveType_ArgMin, PopulateArgMinParameter); +Registry g_argMinParameterRegistry(schema::PrimitiveType_ArgMinFusion, PopulateArgMinParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/arithmetic_populate.cc b/mindspore/lite/src/ops/populate/arithmetic_populate.cc index d02a050859..3106a0569f 100644 --- a/mindspore/lite/src/ops/populate/arithmetic_populate.cc +++ b/mindspore/lite/src/ops/populate/arithmetic_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,39 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "src/ops/populate/arithmetic_populate.h" -#include "src/ops/arithmetic.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { - -ArithmeticParameter *PopulateArithmeticCommonPara(const mindspore::lite::PrimitiveC *primitive) { +ArithmeticParameter *PopulateArithmeticCommonPara(const void *prim) { ArithmeticParameter *param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; return nullptr; } memset(param, 0, sizeof(ArithmeticParameter)); - param->op_parameter_.type_ = primitive->Type(); - param->broadcasting_ = reinterpret_cast(primitive)->Broadcasting(); - param->ndim_ = reinterpret_cast(primitive)->NDims(); + const schema::Primitive *primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); + param->broadcasting_ = false; + param->ndim_ = 0; param->activation_type_ = 0; - - auto tmp_shape = reinterpret_cast(primitive)->InShape0(); - memcpy(param->in_shape0_, static_cast(tmp_shape.data()), tmp_shape.size() * sizeof(int)); - tmp_shape = reinterpret_cast(primitive)->InShape1(); - memcpy(param->in_shape1_, static_cast(tmp_shape.data()), tmp_shape.size() * sizeof(int)); - tmp_shape = reinterpret_cast(primitive)->OutputShape(); - memcpy(param->out_shape_, static_cast(tmp_shape.data()), tmp_shape.size() * sizeof(int)); return param; } -OpParameter *PopulateArithmetic(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateArithmetic(const void *primitive) { ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; @@ -54,20 +42,20 @@ OpParameter *PopulateArithmetic(const mindspore::lite::PrimitiveC *primitive) { return reinterpret_cast(param); } -Registry RealDivParameterRegistry(schema::PrimitiveType_RealDiv, PopulateArithmetic); -Registry LogicalAndParameterRegistry(schema::PrimitiveType_LogicalAnd, PopulateArithmetic); -Registry ParameterRegistry(schema::PrimitiveType_LogicalOr, PopulateArithmetic); -Registry EqualParameterRegistry(schema::PrimitiveType_Equal, PopulateArithmetic); -Registry NotEqualParameterRegistry(schema::PrimitiveType_NotEqual, PopulateArithmetic); -Registry LessParameterRegistry(schema::PrimitiveType_Less, PopulateArithmetic); -Registry LessEqualParameterRegistry(schema::PrimitiveType_LessEqual, PopulateArithmetic); -Registry GreaterParameterRegistry(schema::PrimitiveType_Greater, PopulateArithmetic); -Registry GreaterEqualParameterRegistry(schema::PrimitiveType_GreaterEqual, PopulateArithmetic); -Registry MaximumParameterRegistry(schema::PrimitiveType_Maximum, PopulateArithmetic); -Registry MinimumParameterRegistry(schema::PrimitiveType_Minimum, PopulateArithmetic); -Registry FloorDivParameterRegistry(schema::PrimitiveType_FloorDiv, PopulateArithmetic); -Registry FloorModParameterRegistry(schema::PrimitiveType_FloorMod, PopulateArithmetic); -Registry ModParameterRegistry(schema::PrimitiveType_Mod, PopulateArithmetic); -Registry SquaredDifferenceParameterRegistry(schema::PrimitiveType_SquaredDifference, PopulateArithmetic); +Registry g_realDivParameterRegistry(schema::PrimitiveType_RealDiv, PopulateArithmetic, SCHEMA_CUR); +Registry g_ogicalAndParameterRegistry(schema::PrimitiveType_LogicalAnd, PopulateArithmetic, SCHEMA_CUR); +Registry g_parameterRegistry(schema::PrimitiveType_LogicalOr, PopulateArithmetic, SCHEMA_CUR); +Registry g_equalParameterRegistry(schema::PrimitiveType_Equal, PopulateArithmetic, SCHEMA_CUR); +Registry g_notEqualParameterRegistry(schema::PrimitiveType_NotEqual, PopulateArithmetic, SCHEMA_CUR); +Registry g_essParameterRegistry(schema::PrimitiveType_Less, PopulateArithmetic, SCHEMA_CUR); +Registry g_lessEqualParameterRegistry(schema::PrimitiveType_LessEqual, PopulateArithmetic, SCHEMA_CUR); +Registry g_greaterParameterRegistry(schema::PrimitiveType_Greater, PopulateArithmetic, SCHEMA_CUR); +Registry g_greaterEqualParameterRegistry(schema::PrimitiveType_GreaterEqual, PopulateArithmetic, SCHEMA_CUR); +Registry g_maximumParameterRegistry(schema::PrimitiveType_Maximum, PopulateArithmetic, SCHEMA_CUR); +Registry g_minimumParameterRegistry(schema::PrimitiveType_Minimum, PopulateArithmetic, SCHEMA_CUR); +Registry g_floorDivParameterRegistry(schema::PrimitiveType_FloorDiv, PopulateArithmetic, SCHEMA_CUR); +Registry g_floorModParameterRegistry(schema::PrimitiveType_FloorMod, PopulateArithmetic, SCHEMA_CUR); +Registry g_modParameterRegistry(schema::PrimitiveType_Mod, PopulateArithmetic, SCHEMA_CUR); +Registry g_squaredDifferenceParameterRegistry(schema::PrimitiveType_SquaredDifference, PopulateArithmetic, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/arithmetic_populate.h b/mindspore/lite/src/ops/populate/arithmetic_populate.h index 1112919aba..afce903883 100644 --- a/mindspore/lite/src/ops/populate/arithmetic_populate.h +++ b/mindspore/lite/src/ops/populate/arithmetic_populate.h @@ -16,14 +16,12 @@ #ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_ARITHMETIC_POPULATE_H_ #define MINDSPORE_LITE_SRC_OPS_POPULATE_ARITHMETIC_POPULATE_H_ -#include "src/ops/arithmetic.h" +#include "nnacl/arithmetic.h" namespace mindspore { namespace lite { - -ArithmeticParameter *PopulateArithmeticCommonPara(const mindspore::lite::PrimitiveC *primitive); -OpParameter *PopulateArithmetic(const mindspore::lite::PrimitiveC *primitive); - +ArithmeticParameter *PopulateArithmeticCommonPara(const void *primitive); +OpParameter *PopulateArithmetic(const void *primitive); } // namespace lite } // namespace mindspore #endif // MINDSPORE_LITE_SRC_OPS_POPULATE_ARITHMETIC_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/arithmetic_self_populate.cc b/mindspore/lite/src/ops/populate/arithmetic_self_populate.cc index 2d1cc864a7..99a47ebaaf 100644 --- a/mindspore/lite/src/ops/populate/arithmetic_self_populate.cc +++ b/mindspore/lite/src/ops/populate/arithmetic_self_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/arithmetic_self.h" #include "src/common/log_adapter.h" -#include "src/ops/primitive_c.h" +#include "nnacl/arithmetic_self_parameter.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateArithmeticSelf(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateArithmeticSelf(const void *prim) { ArithmeticSelfParameter *arithmetic_self_param = reinterpret_cast(malloc(sizeof(ArithmeticSelfParameter))); if (arithmetic_self_param == nullptr) { @@ -29,26 +27,25 @@ OpParameter *PopulateArithmeticSelf(const mindspore::lite::PrimitiveC *primitive return nullptr; } memset(arithmetic_self_param, 0, sizeof(ArithmeticSelfParameter)); - arithmetic_self_param->op_parameter_.type_ = primitive->Type(); + const schema::Primitive *primitive = static_cast(prim); + arithmetic_self_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(arithmetic_self_param); } -Registry AbsParameterRegistry(schema::PrimitiveType_Abs, PopulateArithmeticSelf); -Registry CosParameterRegistry(schema::PrimitiveType_Cos, PopulateArithmeticSelf); -Registry SinParameterRegistry(schema::PrimitiveType_Sin, PopulateArithmeticSelf); -Registry LogParameterRegistry(schema::PrimitiveType_Log, PopulateArithmeticSelf); -Registry NegParameterRegistry(schema::PrimitiveType_Neg, PopulateArithmeticSelf); -Registry NegGradParameterRegistry(schema::PrimitiveType_NegGrad, PopulateArithmeticSelf); -Registry LogGradParameterRegistry(schema::PrimitiveType_LogGrad, PopulateArithmeticSelf); -Registry AbsGradParameterRegistry(schema::PrimitiveType_AbsGrad, PopulateArithmeticSelf); -Registry SqrtParameterRegistry(schema::PrimitiveType_Sqrt, PopulateArithmeticSelf); -Registry SquareParameterRegistry(schema::PrimitiveType_Square, PopulateArithmeticSelf); -Registry RsqrtParameterRegistry(schema::PrimitiveType_Rsqrt, PopulateArithmeticSelf); -Registry LogicalNotParameterRegistry(schema::PrimitiveType_LogicalNot, PopulateArithmeticSelf); -Registry FloorParameterRegistry(schema::PrimitiveType_Floor, PopulateArithmeticSelf); -Registry CeilParameterRegistry(schema::PrimitiveType_Ceil, PopulateArithmeticSelf); -Registry RoundParameterRegistry(schema::PrimitiveType_Round, PopulateArithmeticSelf); -Registry ReciprocalParameterRegistry(schema::PrimitiveType_Reciprocal, PopulateArithmeticSelf); - +Registry g_absParameterRegistry(schema::PrimitiveType_Abs, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_cosParameterRegistry(schema::PrimitiveType_Cos, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_sinParameterRegistry(schema::PrimitiveType_Sin, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_logParameterRegistry(schema::PrimitiveType_Log, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_negParameterRegistry(schema::PrimitiveType_Neg, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_negGradParameterRegistry(schema::PrimitiveType_NegGrad, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_logGradParameterRegistry(schema::PrimitiveType_LogGrad, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_sqrtParameterRegistry(schema::PrimitiveType_Sqrt, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_squareParameterRegistry(schema::PrimitiveType_Square, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_rsqrtParameterRegistry(schema::PrimitiveType_Rsqrt, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_logicalNotParameterRegistry(schema::PrimitiveType_LogicalNot, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_floorParameterRegistry(schema::PrimitiveType_Floor, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_ceilParameterRegistry(schema::PrimitiveType_Ceil, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_roundParameterRegistry(schema::PrimitiveType_Round, PopulateArithmeticSelf, SCHEMA_CUR); +Registry g_reciprocalParameterRegistry(schema::PrimitiveType_Reciprocal, PopulateArithmeticSelf, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/assert_populate.cc b/mindspore/lite/src/ops/populate/assert_populate.cc index ef0f4e0b02..3a83b0f714 100644 --- a/mindspore/lite/src/ops/populate/assert_populate.cc +++ b/mindspore/lite/src/ops/populate/assert_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/assert_op.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateAssertParameter(const mindspore::lite::PrimitiveC *primitive) { + +OpParameter *PopulateAssertParameter(const void *prim) { OpParameter *assert_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); if (assert_parameter == nullptr) { MS_LOG(ERROR) << "malloc AssertParameter failed."; return nullptr; } memset(assert_parameter, 0, sizeof(OpParameter)); - assert_parameter->type_ = primitive->Type(); + auto primitive = static_cast(prim); + assert_parameter->type_ = primitive->value_type(); return reinterpret_cast(assert_parameter); } -Registry AssertParameterRegistry(schema::PrimitiveType_Assert, PopulateAssertParameter); +Registry AssertParameterRegistry(schema::PrimitiveType_Assert, PopulateAssertParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/assign_add_populate.cc b/mindspore/lite/src/ops/populate/assign_add_populate.cc index 7169e07b24..3e601f6fb6 100644 --- a/mindspore/lite/src/ops/populate/assign_add_populate.cc +++ b/mindspore/lite/src/ops/populate/assign_add_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/assign_add.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateAssignAddParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateAssignAddParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc AssignAdd Parameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry AssignAddParameterRegistry(schema::PrimitiveType_AssignAdd, PopulateAssignAddParameter); +Registry AssignAddParameterRegistry(schema::PrimitiveType_AssignAdd, PopulateAssignAddParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/assign_populate.cc b/mindspore/lite/src/ops/populate/assign_populate.cc index 86710bfd44..191897af93 100644 --- a/mindspore/lite/src/ops/populate/assign_populate.cc +++ b/mindspore/lite/src/ops/populate/assign_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/assign.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateAssignParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateAssignParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc Assign Parameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry AssignParameterRegistry(schema::PrimitiveType_Assign, PopulateAssignParameter); +Registry AssignParameterRegistry(schema::PrimitiveType_Assign, PopulateAssignParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/audio_spectrogram_populate.cc b/mindspore/lite/src/ops/populate/audio_spectrogram_populate.cc new file mode 100644 index 0000000000..3c88e961c2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/audio_spectrogram_populate.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "nnacl/infer/audio_spectrogram_infer.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAudioSpectrogramParameter(const void *prim) { + AudioSpectrogramParameter *arg_param = + reinterpret_cast(malloc(sizeof(AudioSpectrogramParameter))); + if (arg_param == nullptr) { + MS_LOG(ERROR) << "malloc AudioSpectrogramParameter failed."; + return nullptr; + } + memset(arg_param, 0, sizeof(AudioSpectrogramParameter)); + auto *primitive = static_cast(prim); + arg_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_AudioSpectrogram(); + arg_param->window_size_ = param->window_size(); + arg_param->stride_ = param->stride(); + return reinterpret_cast(arg_param); +} +} // namespace + +Registry g_audioSpectrogramParameterRegistry(schema::PrimitiveType_AudioSpectrogram, PopulateAudioSpectrogramParameter, + SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/batch_norm_populate.cc b/mindspore/lite/src/ops/populate/batch_norm_populate.cc index 3561572f49..77eb009a2e 100644 --- a/mindspore/lite/src/ops/populate/batch_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/batch_norm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/batch_norm.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/batchnorm_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateBatchNorm(const mindspore::lite::PrimitiveC *primitive) { - const auto param = - reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateBatchNorm(const void *prim) { BatchNormParameter *batch_norm_param = reinterpret_cast(malloc(sizeof(BatchNormParameter))); if (batch_norm_param == nullptr) { MS_LOG(ERROR) << "malloc BatchNormParameter failed."; return nullptr; } memset(batch_norm_param, 0, sizeof(BatchNormParameter)); - batch_norm_param->op_parameter_.type_ = primitive->Type(); - batch_norm_param->epsilon_ = param->GetEpsilon(); + const schema::Primitive *primitive = static_cast(prim); + batch_norm_param->op_parameter_.type_ = primitive->value_type(); + auto prim_batchnorm = primitive->value_as_BatchNorm(); + batch_norm_param->epsilon_ = prim_batchnorm->epsilon(); batch_norm_param->fused_ = false; return reinterpret_cast(batch_norm_param); } +} // namespace -Registry BatchNormParameterRegistry(schema::PrimitiveType_BatchNorm, PopulateBatchNorm); +Registry g_batchNormParameterRegistry(schema::PrimitiveType_BatchNorm, PopulateBatchNorm, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/batch_to_space_populate.cc b/mindspore/lite/src/ops/populate/batch_to_space_populate.cc index a3ae90ac9b..f39744918f 100644 --- a/mindspore/lite/src/ops/populate/batch_to_space_populate.cc +++ b/mindspore/lite/src/ops/populate/batch_to_space_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/batch_to_space.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/batch_to_space.h" namespace mindspore { namespace lite { -OpParameter *PopulateBatchToSpaceParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateBatchToSpaceParameter(const void *prim) { BatchToSpaceParameter *batch_space_param = reinterpret_cast(malloc(sizeof(BatchToSpaceParameter))); if (batch_space_param == nullptr) { @@ -31,21 +27,25 @@ OpParameter *PopulateBatchToSpaceParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(batch_space_param, 0, sizeof(BatchToSpaceParameter)); - batch_space_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - auto block_shape = param->GetBlockShape(); - if (block_shape.empty()) { + const schema::Primitive *primitive = static_cast(prim); + batch_space_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_BatchToSpace(); + if (param->block_size() == nullptr) { return reinterpret_cast(batch_space_param); } + auto block_shape = std::vector(param->block_size()->begin(), param->block_size()->end()); if (block_shape.size() != BATCH_TO_SPACE_BLOCK_SHAPE_SIZE) { MS_LOG(ERROR) << "batch_to_space blockShape size should be " << BATCH_TO_SPACE_BLOCK_SHAPE_SIZE; free(batch_space_param); return nullptr; } - auto crops = param->GetCrops(); - if (crops.empty()) { - return reinterpret_cast(batch_space_param); + auto fb_crops = param->crops()->data(); + std::vector crops; + for (auto iter = fb_crops->begin(); iter != fb_crops->end(); ++iter) { + auto crops_data = (*iter)->data(); + auto crops_vec = std::vector(crops_data->begin(), crops_data->end()); + crops.insert(crops.end(), crops_vec.begin(), crops_vec.end()); } if (crops.size() != COMM_SHAPE_SIZE) { MS_LOG(ERROR) << "batch_to_space crops size should be " << COMM_SHAPE_SIZE; @@ -54,19 +54,16 @@ OpParameter *PopulateBatchToSpaceParameter(const mindspore::lite::PrimitiveC *pr } for (int i = 0; i < BATCH_TO_SPACE_BLOCK_SHAPE_SIZE; ++i) { - batch_space_param->block_shape_[i] = block_shape[i]; + batch_space_param->block_shape_[i] = static_cast(block_shape[i]); } - batch_space_param->no_crop_ = true; for (int i = 0; i < COMM_SHAPE_SIZE; ++i) { - batch_space_param->crops_[i] = crops[i]; - if (batch_space_param->crops_[i] != 0) { - batch_space_param->no_crop_ = false; - } + batch_space_param->crops_[i] = static_cast(crops[i]); } return reinterpret_cast(batch_space_param); } -Registry BatchToSpaceParameterRegistry(schema::PrimitiveType_BatchToSpace, PopulateBatchToSpaceParameter); -Registry BatchToSpaceNDParameterRegistry(schema::PrimitiveType_BatchToSpaceND, PopulateBatchToSpaceParameter); +} // namespace +Registry g_batchToSpaceRegistry(schema::PrimitiveType_BatchToSpace, PopulateBatchToSpaceParameter, SCHEMA_CUR); +Registry g_batchToSpaceNDRegistry(schema::PrimitiveType_BatchToSpaceND, PopulateBatchToSpaceParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/bias_add_populate.cc b/mindspore/lite/src/ops/populate/bias_add_populate.cc index f4875a5fd4..b58b222845 100644 --- a/mindspore/lite/src/ops/populate/bias_add_populate.cc +++ b/mindspore/lite/src/ops/populate/bias_add_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arithmetic.h" namespace mindspore { namespace lite { -OpParameter *PopulateBiasAddParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateBiasAddParameter(const void *prim) { ArithmeticParameter *arithmetic_param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); if (arithmetic_param == nullptr) { MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; return nullptr; } memset(arithmetic_param, 0, sizeof(ArithmeticParameter)); - arithmetic_param->op_parameter_.type_ = primitive->Type(); + const schema::Primitive *primitive = static_cast(prim); + arithmetic_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(arithmetic_param); } -Registry BiasAddParameterRegistry(schema::PrimitiveType_BiasAdd, PopulateBiasAddParameter); +} // namespace +Registry g_biasAddParameterRegistry(schema::PrimitiveType_BiasAdd, PopulateBiasAddParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/bias_grad_populate.cc b/mindspore/lite/src/ops/populate/bias_grad_populate.cc index 0bb338d3e3..72cb7a9d82 100644 --- a/mindspore/lite/src/ops/populate/bias_grad_populate.cc +++ b/mindspore/lite/src/ops/populate/bias_grad_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arithmetic.h" namespace mindspore { namespace lite { -OpParameter *PopulateBiasGradParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateBiasAddGradParameter(const void *prim) { ArithmeticParameter *arithmetic_param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); if (arithmetic_param == nullptr) { MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; return nullptr; } memset(arithmetic_param, 0, sizeof(ArithmeticParameter)); - arithmetic_param->op_parameter_.type_ = primitive->Type(); + auto primitive = static_cast(prim); + arithmetic_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(arithmetic_param); } -Registry PopulateBiasGradParameterParameterRegistry(schema::PrimitiveType_BiasGrad, PopulateBiasGradParameter); +} // namespace +Registry g_populateBiasGradParameterParameterRegistry(schema::PrimitiveType_BiasAddGrad, PopulateBiasAddGradParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc b/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc index 0087432b08..910b055460 100644 --- a/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc +++ b/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/binary_cross_entropy_grad.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32_grad/binary_cross_entropy_grad.h" namespace mindspore { namespace lite { -OpParameter *PopulateBinaryCrossEntropyGradParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateBinaryCrossEntropyGradParameter(const void *prim) { BinaryCrossEntropyGradParameter *bce_param = reinterpret_cast(malloc(sizeof(BinaryCrossEntropyGradParameter))); if (bce_param == nullptr) { @@ -29,14 +27,15 @@ OpParameter *PopulateBinaryCrossEntropyGradParameter(const mindspore::lite::Prim return nullptr; } memset(bce_param, 0, sizeof(BinaryCrossEntropyGradParameter)); - bce_param->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - bce_param->reduction = param->GetReduction(); + auto *primitive = static_cast(prim); + bce_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_BinaryCrossEntropyGrad(); + bce_param->reduction = param->reduction(); return reinterpret_cast(bce_param); } +} // namespace -Registry BinaryCrossEntropyGradParameterRegistry(schema::PrimitiveType_BinaryCrossEntropyGrad, - PopulateBinaryCrossEntropyGradParameter); +Registry g_binaryCrossEntropyGradParameterRegistry(schema::PrimitiveType_BinaryCrossEntropyGrad, + PopulateBinaryCrossEntropyGradParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc b/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc index 1e150a21fa..2ade2b29fc 100644 --- a/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc +++ b/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/binary_cross_entropy.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32_grad/binary_cross_entropy.h" namespace mindspore { namespace lite { -OpParameter *PopulateBinaryCrossEntropyParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateBinaryCrossEntropyParameter(const void *prim) { BinaryCrossEntropyParameter *bce_param = reinterpret_cast(malloc(sizeof(BinaryCrossEntropyParameter))); if (bce_param == nullptr) { @@ -29,14 +26,14 @@ OpParameter *PopulateBinaryCrossEntropyParameter(const mindspore::lite::Primitiv return nullptr; } memset(bce_param, 0, sizeof(BinaryCrossEntropyParameter)); - bce_param->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - bce_param->reduction = param->GetReduction(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_BinaryCrossEntropy(); + bce_param->op_parameter_.type_ = primitive->value_type(); + bce_param->reduction = value->reduction(); return reinterpret_cast(bce_param); } Registry BinaryCrossEntropyParameterRegistry(schema::PrimitiveType_BinaryCrossEntropy, - PopulateBinaryCrossEntropyParameter); + PopulateBinaryCrossEntropyParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/broadcast_to_populate.cc b/mindspore/lite/src/ops/populate/broadcast_to_populate.cc index b73188cbff..0bc8dc4203 100644 --- a/mindspore/lite/src/ops/populate/broadcast_to_populate.cc +++ b/mindspore/lite/src/ops/populate/broadcast_to_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/broadcast_to.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/broadcast_to_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateBroadcastToParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateBroadcastToParameter(const void *prim) { BroadcastToParameter *broadcast_param = reinterpret_cast(malloc(sizeof(BroadcastToParameter))); if (broadcast_param == nullptr) { @@ -29,16 +26,17 @@ OpParameter *PopulateBroadcastToParameter(const mindspore::lite::PrimitiveC *pri return nullptr; } memset(broadcast_param, 0, sizeof(BroadcastToParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - broadcast_param->op_parameter_.type_ = primitive->Type(); - auto dst_shape = param->GetDstShape(); - broadcast_param->shape_size_ = dst_shape.size(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_BroadcastTo(); + broadcast_param->op_parameter_.type_ = primitive->value_type(); + auto dst_shape = value->shape(); + broadcast_param->shape_size_ = dst_shape->size(); for (size_t i = 0; i < broadcast_param->shape_size_; ++i) { - broadcast_param->shape_[i] = dst_shape[i]; + broadcast_param->shape_[i] = dst_shape->Get(i); } return reinterpret_cast(broadcast_param); } -Registry BroadcastToParameterRegistry(schema::PrimitiveType_BroadcastTo, PopulateBroadcastToParameter); +Registry BroadcastToParameterRegistry(schema::PrimitiveType_BroadcastTo, PopulateBroadcastToParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/cast_populate.cc b/mindspore/lite/src/ops/populate/cast_populate.cc index 3543a2a53c..d30859aa66 100644 --- a/mindspore/lite/src/ops/populate/cast_populate.cc +++ b/mindspore/lite/src/ops/populate/cast_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/cast.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "nnacl/cast_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateCastParameter(const mindspore::lite::PrimitiveC *primitive) { - CastParameter *cast_param = reinterpret_cast(malloc(sizeof(CastParameter))); +namespace { +OpParameter *PopulateCastParameter(const void *prim) { + OpParameter *cast_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (cast_param == nullptr) { MS_LOG(ERROR) << "malloc CastParameter failed."; return nullptr; } - memset(cast_param, 0, sizeof(CastParameter)); - cast_param->op_parameter_.type_ = primitive->Type(); - - auto param = reinterpret_cast(const_cast(primitive)); - cast_param->src_type_ = param->GetSrcT(); - cast_param->dst_type_ = param->GetDstT(); - + memset(cast_param, 0, sizeof(OpParameter)); + auto *primitive = static_cast(prim); + cast_param->type_ = primitive->value_type(); return reinterpret_cast(cast_param); } +} // namespace -Registry CastParameterRegistry(schema::PrimitiveType_Cast, PopulateCastParameter); +Registry g_castParameterRegistry(schema::PrimitiveType_Cast, PopulateCastParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/clip_populate.cc b/mindspore/lite/src/ops/populate/clip_populate.cc new file mode 100644 index 0000000000..6a2f2e2078 --- /dev/null +++ b/mindspore/lite/src/ops/populate/clip_populate.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateClipParameter(const void *prim) { + OpParameter *act_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (act_param == nullptr) { + MS_LOG(ERROR) << "malloc ClipParameter failed."; + return nullptr; + } + memset(act_param, 0, sizeof(OpParameter)); + auto primitive = static_cast(prim); + act_param->type_ = primitive->value_type(); + return reinterpret_cast(act_param); +} +} // namespace + +Registry g_clipParameterRegistry(schema::PrimitiveType_Clip, PopulateClipParameter, SCHEMA_CUR); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/common_populate.cc b/mindspore/lite/src/ops/populate/common_populate.cc index 3d7fe2ef97..0ddf8b0a3a 100644 --- a/mindspore/lite/src/ops/populate/common_populate.cc +++ b/mindspore/lite/src/ops/populate/common_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,26 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { - -OpParameter *PopulateCommonParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateCommonParameter(const void *prim) { auto *common_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); if (common_parameter == nullptr) { MS_LOG(ERROR) << "malloc OpParameter failed."; return nullptr; } memset(common_parameter, 0, sizeof(OpParameter)); + auto primitive = static_cast(prim); + common_parameter->type_ = primitive->value_type(); return common_parameter; } +} // namespace -Registry ZerosLikeParameterRegistry(schema::PrimitiveType_ZerosLike, PopulateCommonParameter); -Registry SizeParameterRegistry(schema::PrimitiveType_Size, PopulateCommonParameter); -Registry InvertPermutationParameterRegistry(schema::PrimitiveType_InvertPermutation, PopulateCommonParameter); - +Registry g_zerosLikeParameterRegistry(schema::PrimitiveType_ZerosLike, PopulateCommonParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/concat_populate.cc b/mindspore/lite/src/ops/populate/concat_populate.cc index e9be7786c9..10d0451675 100644 --- a/mindspore/lite/src/ops/populate/concat_populate.cc +++ b/mindspore/lite/src/ops/populate/concat_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/concat.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/concat_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateConcatParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateConcatParameter(const void *prim) { ConcatParameter *concat_param = reinterpret_cast(malloc(sizeof(ConcatParameter))); if (concat_param == nullptr) { MS_LOG(ERROR) << "malloc ConcatParameter failed."; return nullptr; } memset(concat_param, 0, sizeof(ConcatParameter)); - concat_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - concat_param->axis_ = param->GetAxis(); + const schema::Primitive *primitive = static_cast(prim); + concat_param->op_parameter_.type_ = primitive->value_type(); + concat_param->axis_ = static_cast(primitive->value_as_Concat()->axis()); return reinterpret_cast(concat_param); } +} // namespace -Registry ConcatParameterRegistry(schema::PrimitiveType_Concat, PopulateConcatParameter); +Registry g_concatParameterRegistry(schema::PrimitiveType_Concat, PopulateConcatParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc b/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc index 4a04ef0812..89997653c4 100644 --- a/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc +++ b/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,19 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/constant_of_shape.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/constant_of_shape.h" namespace mindspore::lite { namespace { -OpParameter *PopulateConstantOfShapeParameter(const mindspore::lite::PrimitiveC *primitive) { - auto attr = - reinterpret_cast(const_cast(primitive)); +OpParameter *PopulateConstantOfShapeParameter(const void *prim) { ConstantOfShapeParameter *param = reinterpret_cast(malloc(sizeof(ConstantOfShapeParameter))); if (param == nullptr) { @@ -33,25 +26,28 @@ OpParameter *PopulateConstantOfShapeParameter(const mindspore::lite::PrimitiveC return nullptr; } memset(param, 0, sizeof(ConstantOfShapeParameter)); - param->op_parameter_.type_ = primitive->Type(); - param->data_type_ = attr->GetDataType(); - auto value = attr->GetValue(); + auto primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); + auto attr = primitive->value_as_ConstantOfShape(); + auto value = std::vector(attr->value()->begin(), attr->value()->end()); + param->data_type_ = static_cast(attr->data_type()); if (value.empty() || value.size() > 1) { MS_LOG(ERROR) << "The value of constant of shape is empty or more than 1."; } else { switch (param->data_type_) { case kNumberTypeFloat32: - param->value_.f32_value_ = attr->GetValue().at(0); + param->value_.f32_value_ = *(attr->value()->begin()); break; case kNumberTypeInt32: - param->value_.int32_value_ = attr->GetValue().at(0); + param->value_.int32_value_ = *(attr->value()->begin()); break; default: MS_LOG(ERROR) << "The value of constant of shape is invalid"; } } return reinterpret_cast(param); -} -Registry ConstantOfShapeParameterRegistry(schema::PrimitiveType_ConstantOfShape, PopulateConstantOfShapeParameter); +} // namespace +Registry g_constantOfShapeParameterRegistry(schema::PrimitiveType_ConstantOfShape, PopulateConstantOfShapeParameter, + SCHEMA_CUR); } // namespace } // namespace mindspore::lite diff --git a/mindspore/lite/src/ops/populate/conv2d_populate.cc b/mindspore/lite/src/ops/populate/conv2d_populate.cc index 35f46c0288..4500cabbfb 100644 --- a/mindspore/lite/src/ops/populate/conv2d_populate.cc +++ b/mindspore/lite/src/ops/populate/conv2d_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,53 +14,53 @@ * limitations under the License. */ -#include "src/ops/conv2d.h" -#include "src/common/log_adapter.h" #include "nnacl/conv_parameter.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateConvParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateConvParameter(const void *prim) { ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); if (conv_param == nullptr) { MS_LOG(ERROR) << "malloc ConvParameter failed."; return nullptr; } memset(conv_param, 0, sizeof(ConvParameter)); - conv_param->op_parameter_.type_ = primitive->Type(); - auto conv_primitive = - reinterpret_cast(const_cast(primitive)); - conv_param->kernel_h_ = conv_primitive->GetKernelH(); - conv_param->kernel_w_ = conv_primitive->GetKernelW(); - conv_param->group_ = conv_primitive->GetGroup(); - conv_param->stride_h_ = conv_primitive->GetStrideH(); - conv_param->stride_w_ = conv_primitive->GetStrideW(); - - auto conv2d_lite_primitive = (lite::Conv2D *)primitive; - conv_param->pad_u_ = conv2d_lite_primitive->PadUp(); - conv_param->pad_d_ = conv2d_lite_primitive->PadDown(); - conv_param->pad_l_ = conv2d_lite_primitive->PadLeft(); - conv_param->pad_r_ = conv2d_lite_primitive->PadRight(); - conv_param->dilation_h_ = conv_primitive->GetDilateH(); - conv_param->dilation_w_ = conv_primitive->GetDilateW(); - conv_param->input_channel_ = conv_primitive->GetChannelIn(); - conv_param->output_channel_ = conv_primitive->GetChannelOut(); - conv_param->group_ = conv_primitive->GetGroup(); - auto pad_mode = conv_primitive->GetPadMode(); - switch (pad_mode) { - case schema::PadMode_SAME_UPPER: - conv_param->pad_mode_ = Pad_Same; + auto primitive = static_cast(prim); + conv_param->op_parameter_.type_ = primitive->value_type(); + auto conv_primitive = primitive->value_as_Conv2DFusion(); + conv_param->kernel_h_ = static_cast(*(conv_primitive->kernel_size()->begin())); + conv_param->kernel_w_ = static_cast(*(conv_primitive->kernel_size()->begin() + 1)); + conv_param->group_ = static_cast(conv_primitive->group()); + conv_param->stride_h_ = static_cast(*(conv_primitive->stride()->begin())); + conv_param->stride_w_ = static_cast(*(conv_primitive->stride()->begin() + 1)); + switch (conv_primitive->pad_mode()) { + case schema::PadMode_SAME: + conv_param->pad_mode_ = Pad_same; break; case schema::PadMode_VALID: - conv_param->pad_mode_ = Pad_Valid; + conv_param->pad_mode_ = Pad_valid; break; default: - conv_param->pad_mode_ = Pad_No; - break; + conv_param->pad_mode_ = Pad_pad; } - auto act_type = conv_primitive->GetActivationType(); + if (conv_primitive->pad_list() == nullptr || conv_primitive->pad_list()->size() < 4) { + conv_param->pad_u_ = 0; + conv_param->pad_d_ = 0; + conv_param->pad_l_ = 0; + conv_param->pad_r_ = 0; + } else { + conv_param->pad_u_ = static_cast(*(conv_primitive->pad_list()->begin())); + conv_param->pad_d_ = static_cast(*(conv_primitive->pad_list()->begin() + 1)); + conv_param->pad_l_ = static_cast(*(conv_primitive->pad_list()->begin() + 2)); + conv_param->pad_r_ = static_cast(*(conv_primitive->pad_list()->begin() + 3)); + } + conv_param->dilation_h_ = static_cast(*(conv_primitive->dilation()->begin())); + conv_param->dilation_w_ = static_cast(*(conv_primitive->dilation()->begin() + 1)); + conv_param->input_channel_ = static_cast(conv_primitive->in_channel()); + conv_param->output_channel_ = static_cast(conv_primitive->out_channel()); + auto act_type = conv_primitive->activation_type(); switch (act_type) { case schema::ActivationType_RELU: conv_param->act_type_ = ActType_Relu; @@ -70,10 +70,10 @@ OpParameter *PopulateConvParameter(const mindspore::lite::PrimitiveC *primitive) break; default: conv_param->act_type_ = ActType_No; - break; } return reinterpret_cast(conv_param); } -Registry Conv2DParameterRegistry(schema::PrimitiveType_Conv2D, PopulateConvParameter); +} // namespace +Registry g_conv2DParameterRegistry(schema::PrimitiveType_Conv2DFusion, PopulateConvParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/crop_and_resize_populate.cc b/mindspore/lite/src/ops/populate/crop_and_resize_populate.cc index e87ef703fa..0908af070b 100644 --- a/mindspore/lite/src/ops/populate/crop_and_resize_populate.cc +++ b/mindspore/lite/src/ops/populate/crop_and_resize_populate.cc @@ -13,14 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/crop_and_resize.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/resize_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateCropAndResizeParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateCropAndResizeParameter(const void *prim) { CropAndResizeParameter *crop_resize_param = reinterpret_cast(malloc(sizeof(CropAndResizeParameter))); if (crop_resize_param == nullptr) { @@ -28,13 +26,16 @@ OpParameter *PopulateCropAndResizeParameter(const mindspore::lite::PrimitiveC *p return nullptr; } memset(crop_resize_param, 0, sizeof(CropAndResizeParameter)); - crop_resize_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - crop_resize_param->method_ = static_cast(param->GetMethod()); - crop_resize_param->extrapolation_value_ = param->GetExtrapolationValue(); + auto primitive = static_cast(prim); + crop_resize_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_CropAndResize(); + crop_resize_param->method_ = static_cast(param->method()); + crop_resize_param->extrapolation_value_ = param->extrapolation_value(); return reinterpret_cast(crop_resize_param); } +} // namespace -Registry CropAndResizeParameterRegistry(schema::PrimitiveType_CropAndResize, PopulateCropAndResizeParameter); +Registry g_cropAndResizeParameterRegistry(schema::PrimitiveType_CropAndResize, PopulateCropAndResizeParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/crop_populate.cc b/mindspore/lite/src/ops/populate/crop_populate.cc index 0898fa283f..bdfb0ed193 100644 --- a/mindspore/lite/src/ops/populate/crop_populate.cc +++ b/mindspore/lite/src/ops/populate/crop_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,20 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/crop.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/crop_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateCropParameter(const mindspore::lite::PrimitiveC *primitive) { - auto param = reinterpret_cast(const_cast(primitive)); - auto param_offset = param->GetOffsets(); - if (param_offset.size() > COMM_SHAPE_SIZE) { - MS_LOG(ERROR) << "crop_param offset size(" << param_offset.size() << ") should <= " << COMM_SHAPE_SIZE; +namespace { +OpParameter *PopulateCropParameter(const void *prim) { + auto primitive = static_cast(prim); + auto crop_prim = primitive->value_as_Crop(); + auto param_offset = crop_prim->offsets(); + if (param_offset->size() > COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "crop_param offset size(" << param_offset->size() << ") should <= " << COMM_SHAPE_SIZE; return nullptr; } CropParameter *crop_param = reinterpret_cast(malloc(sizeof(CropParameter))); @@ -35,15 +33,16 @@ OpParameter *PopulateCropParameter(const mindspore::lite::PrimitiveC *primitive) return nullptr; } memset(crop_param, 0, sizeof(CropParameter)); - crop_param->op_parameter_.type_ = primitive->Type(); - crop_param->axis_ = param->GetAxis(); - crop_param->offset_size_ = param_offset.size(); - for (size_t i = 0; i < param_offset.size(); ++i) { - crop_param->offset_[i] = param_offset[i]; + crop_param->op_parameter_.type_ = primitive->value_type(); + crop_param->axis_ = crop_prim->axis(); + crop_param->offset_size_ = param_offset->size(); + for (size_t i = 0; i < param_offset->size(); ++i) { + crop_param->offset_[i] = *(param_offset->begin() + i); } return reinterpret_cast(crop_param); } -Registry CropParameterRegistry(schema::PrimitiveType_Crop, PopulateCropParameter); +} // namespace +Registry g_cropParameterRegistry(schema::PrimitiveType_Crop, PopulateCropParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/custom_extract_features_populate.cc b/mindspore/lite/src/ops/populate/custom_extract_features_populate.cc index 9d755dd15c..d7ab450664 100644 --- a/mindspore/lite/src/ops/populate/custom_extract_features_populate.cc +++ b/mindspore/lite/src/ops/populate/custom_extract_features_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,26 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/custom_extract_features.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { - -OpParameter *PopulateExtractFeaturesParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateExtractFeaturesParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "new OpParameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry CustomExtractFeaturesParameterRegistry(schema::PrimitiveType_CustomExtractFeatures, - PopulateExtractFeaturesParameter); - +} // namespace +Registry g_customExtractFeaturesParameterRegistry(schema::PrimitiveType_CustomExtractFeatures, + PopulateExtractFeaturesParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/custom_normalize_populate.cc b/mindspore/lite/src/ops/populate/custom_normalize_populate.cc index 2a127670da..94fa7975ea 100644 --- a/mindspore/lite/src/ops/populate/custom_normalize_populate.cc +++ b/mindspore/lite/src/ops/populate/custom_normalize_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/custom_normalize.h" -#include "src/common/string_util.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateCustomNormalizeParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateCustomNormalizeParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "new OpParameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry CustomNormalizeParameterRegistry(schema::PrimitiveType_CustomNormalize, PopulateCustomNormalizeParameter); +Registry CustomNormalizeParameterRegistry(schema::PrimitiveType_CustomNormalize, PopulateCustomNormalizeParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/custom_predict_populate.cc b/mindspore/lite/src/ops/populate/custom_predict_populate.cc index bf00613e81..d0084f246c 100644 --- a/mindspore/lite/src/ops/populate/custom_predict_populate.cc +++ b/mindspore/lite/src/ops/populate/custom_predict_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,28 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/custom_predict.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/predict_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateCustomPredictParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateCustomPredictParameter(const void *prim) { PredictParameter *param = reinterpret_cast(malloc(sizeof(PredictParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc param failed."; return nullptr; } memset(param, 0, sizeof(PredictParameter)); - param->op_parameter_.type_ = primitive->Type(); - auto prim = reinterpret_cast(const_cast(primitive)); - param->output_num = prim->GetOutputNum(); - param->weight_threshold = prim->GetWeightThreshold(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_CustomPredict(); + param->op_parameter_.type_ = primitive->value_type(); + param->output_num = value->output_num(); + param->weight_threshold = value->weight_threshold(); return reinterpret_cast(param); } -Registry CustomPredictParameterRegistry(schema::PrimitiveType_CustomPredict, PopulateCustomPredictParameter); +Registry CustomPredictParameterRegistry(schema::PrimitiveType_CustomPredict, PopulateCustomPredictParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/deconv2d_populate.cc b/mindspore/lite/src/ops/populate/deconv2d_populate.cc index 96ffb97e9c..f3ed7da114 100644 --- a/mindspore/lite/src/ops/populate/deconv2d_populate.cc +++ b/mindspore/lite/src/ops/populate/deconv2d_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,41 +13,54 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/deconv2d.h" #include "src/common/log_adapter.h" - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/conv_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateDeconvParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateDeconvParameter(const void *prim) { ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); if (conv_param == nullptr) { MS_LOG(ERROR) << "malloc ConvParameter failed."; return nullptr; } memset(conv_param, 0, sizeof(ConvParameter)); - conv_param->op_parameter_.type_ = primitive->Type(); - auto conv_primitive = - reinterpret_cast(const_cast(primitive)); - conv_param->kernel_h_ = conv_primitive->GetKernelH(); - conv_param->kernel_w_ = conv_primitive->GetKernelW(); - conv_param->stride_h_ = conv_primitive->GetStrideH(); - conv_param->stride_w_ = conv_primitive->GetStrideW(); - - auto deconv_lite_primitive = (lite::DeConv2D *)primitive; - conv_param->pad_u_ = deconv_lite_primitive->PadUp(); - conv_param->pad_d_ = deconv_lite_primitive->PadDown(); - conv_param->pad_l_ = deconv_lite_primitive->PadLeft(); - conv_param->pad_r_ = deconv_lite_primitive->PadRight(); - conv_param->dilation_h_ = conv_primitive->GetDilateH(); - conv_param->dilation_w_ = conv_primitive->GetDilateW(); - conv_param->group_ = conv_primitive->GetGroup(); - auto act_type = conv_primitive->GetActivationType(); + auto primitive = static_cast(prim); + conv_param->op_parameter_.type_ = primitive->value_type(); + auto conv_primitive = primitive->value_as_Conv2dTransposeFusion(); + conv_param->kernel_h_ = static_cast(*(conv_primitive->kernel_size()->begin())); + conv_param->kernel_w_ = static_cast(*(conv_primitive->kernel_size()->begin() + 1)); + conv_param->group_ = static_cast(conv_primitive->group()); + conv_param->stride_h_ = static_cast(*(conv_primitive->stride()->begin())); + conv_param->stride_w_ = static_cast(*(conv_primitive->stride()->begin() + 1)); + switch (conv_primitive->pad_mode()) { + case schema::PadMode_SAME: + conv_param->pad_mode_ = Pad_same; + break; + case schema::PadMode_VALID: + conv_param->pad_mode_ = Pad_valid; + break; + default: + conv_param->pad_mode_ = Pad_pad; + } + if (conv_primitive->pad_list() == nullptr || conv_primitive->pad_list()->size() < 4) { + conv_param->pad_u_ = 0; + conv_param->pad_d_ = 0; + conv_param->pad_l_ = 0; + conv_param->pad_r_ = 0; + } else { + conv_param->pad_u_ = static_cast(*(conv_primitive->pad_list()->begin())); + conv_param->pad_d_ = static_cast(*(conv_primitive->pad_list()->begin() + 1)); + conv_param->pad_l_ = static_cast(*(conv_primitive->pad_list()->begin() + 2)); + conv_param->pad_r_ = static_cast(*(conv_primitive->pad_list()->begin() + 3)); + } + conv_param->dilation_h_ = static_cast(*(conv_primitive->dilation()->begin())); + conv_param->dilation_w_ = static_cast(*(conv_primitive->dilation()->begin() + 1)); + conv_param->input_channel_ = static_cast(conv_primitive->in_channel()); + conv_param->output_channel_ = static_cast(conv_primitive->out_channel()); + auto act_type = conv_primitive->activation_type(); switch (act_type) { case schema::ActivationType_RELU: conv_param->act_type_ = ActType_Relu; @@ -62,7 +75,6 @@ OpParameter *PopulateDeconvParameter(const mindspore::lite::PrimitiveC *primitiv return reinterpret_cast(conv_param); } -Registry DeConv2DParameterRegistry(schema::PrimitiveType_DeConv2D, PopulateDeconvParameter); - +Registry g_Deconv2DParameterRegistry(schema::PrimitiveType_Conv2dTransposeFusion, PopulateDeconvParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/dedepthwise_conv2d_populate.cc b/mindspore/lite/src/ops/populate/dedepthwise_conv2d_populate.cc index 732c26cac3..332f4582c9 100644 --- a/mindspore/lite/src/ops/populate/dedepthwise_conv2d_populate.cc +++ b/mindspore/lite/src/ops/populate/dedepthwise_conv2d_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/dedepthwise_conv2d.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/conv_parameter.h" namespace mindspore { namespace lite { - +/* OpParameter *PopulateDeconvDwParameter(const mindspore::lite::PrimitiveC *primitive) { ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); if (conv_param == nullptr) { @@ -59,7 +56,6 @@ OpParameter *PopulateDeconvDwParameter(const mindspore::lite::PrimitiveC *primit return reinterpret_cast(conv_param); } -Registry DeDepthwiseConv2DParameterRegistry(schema::PrimitiveType_DeDepthwiseConv2D, PopulateDeconvDwParameter); - +*/ } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/default_populate.cc b/mindspore/lite/src/ops/populate/default_populate.cc new file mode 100644 index 0000000000..faac1397ca --- /dev/null +++ b/mindspore/lite/src/ops/populate/default_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/default_populate.h" +#include +#include +#include "schema/model_generated.h" +#include "src/common/log_adapter.h" + +namespace mindspore { +namespace lite { +OpParameter *DefaultPopulateParameter(const void *prim) { + OpParameter *param = static_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "Malloc OpParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + auto *primitive = static_cast(prim); + param->type_ = primitive->value_type(); + return param; +} +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/default_populate.h b/mindspore/lite/src/ops/populate/default_populate.h new file mode 100644 index 0000000000..fe215824af --- /dev/null +++ b/mindspore/lite/src/ops/populate/default_populate.h @@ -0,0 +1,26 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_DEFAULT_POPULATE_H_ +#define MINDSPORE_LITE_SRC_OPS_POPULATE_DEFAULT_POPULATE_H_ + +#include "nnacl/op_base.h" + +namespace mindspore { +namespace lite { +OpParameter *DefaultPopulateParameter(const void *prim); +} // namespace lite +} // namespace mindspore +#endif // MINDSPORE_LITE_SRC_OPS_POPULATE_DEFAULT_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/depth_to_space_populate.cc b/mindspore/lite/src/ops/populate/depth_to_space_populate.cc index 4b4227a886..fe6b92a785 100644 --- a/mindspore/lite/src/ops/populate/depth_to_space_populate.cc +++ b/mindspore/lite/src/ops/populate/depth_to_space_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/depth_to_space.h" -#include "src/common/common.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/depth_to_space_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateDepthToSpaceParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateDepthToSpaceParameter(const void *prim) { DepthToSpaceParameter *depth_space_param = reinterpret_cast(malloc(sizeof(DepthToSpaceParameter))); if (depth_space_param == nullptr) { @@ -31,14 +27,14 @@ OpParameter *PopulateDepthToSpaceParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(depth_space_param, 0, sizeof(DepthToSpaceParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - depth_space_param->op_parameter_.type_ = primitive->Type(); - depth_space_param->block_size_ = param->GetBlockSize(); + auto primitive = static_cast(prim); + auto param = primitive->value_as_DepthToSpace(); + depth_space_param->op_parameter_.type_ = primitive->value_type(); + depth_space_param->block_size_ = param->block_size(); return reinterpret_cast(depth_space_param); } +} // namespace -Registry DepthToSpaceParameterRegistry(schema::PrimitiveType_DepthToSpace, PopulateDepthToSpaceParameter); - +Registry g_depthToSpaceParamRegistry(schema::PrimitiveType_DepthToSpace, PopulateDepthToSpaceParameter, SCHEMA_CUR); } // namespace lite - } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/depthwise_conv2d_populate.cc b/mindspore/lite/src/ops/populate/depthwise_conv2d_populate.cc index b59536e950..678ff5a685 100644 --- a/mindspore/lite/src/ops/populate/depthwise_conv2d_populate.cc +++ b/mindspore/lite/src/ops/populate/depthwise_conv2d_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,67 +13,61 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/depthwise_conv2d.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/conv_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateConvDwParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateConvDwParameter(const void *primitive) { ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); if (conv_param == nullptr) { MS_LOG(ERROR) << "malloc ConvParameter failed."; return nullptr; } memset(conv_param, 0, sizeof(ConvParameter)); - conv_param->op_parameter_.type_ = primitive->Type(); + // conv_param->op_parameter_.type_ = primitive->Type(); - auto conv_primitive = - reinterpret_cast(const_cast(primitive)); - conv_param->kernel_h_ = conv_primitive->GetKernelH(); - conv_param->kernel_w_ = conv_primitive->GetKernelW(); - conv_param->stride_h_ = conv_primitive->GetStrideH(); - conv_param->stride_w_ = conv_primitive->GetStrideW(); + // auto conv_primitive = + // reinterpret_cast(const_cast(primitive)); + // conv_param->kernel_h_ = conv_primitive->GetKernelH(); + // conv_param->kernel_w_ = conv_primitive->GetKernelW(); + // conv_param->stride_h_ = conv_primitive->GetStrideH(); + // conv_param->stride_w_ = conv_primitive->GetStrideW(); - auto convdw_lite_primitive = (lite::DepthwiseConv2D *)primitive; - conv_param->pad_u_ = convdw_lite_primitive->PadUp(); - conv_param->pad_d_ = convdw_lite_primitive->PadDown(); - conv_param->pad_l_ = convdw_lite_primitive->PadLeft(); - conv_param->pad_r_ = convdw_lite_primitive->PadRight(); - conv_param->input_channel_ = convdw_lite_primitive->GetInputChannel(); - conv_param->dilation_h_ = conv_primitive->GetDilateH(); - conv_param->dilation_w_ = conv_primitive->GetDilateW(); - auto pad_mode = conv_primitive->GetPadMode(); - switch (pad_mode) { - case schema::PadMode_SAME_UPPER: - conv_param->pad_mode_ = Pad_Same; - break; - case schema::PadMode_VALID: - conv_param->pad_mode_ = Pad_Valid; - break; - default: - conv_param->pad_mode_ = Pad_No; - break; - } - auto act_type = conv_primitive->GetActivationType(); - switch (act_type) { - case schema::ActivationType_RELU: - conv_param->act_type_ = ActType_Relu; - break; - case schema::ActivationType_RELU6: - conv_param->act_type_ = ActType_Relu6; - break; - default: - conv_param->act_type_ = ActType_No; - break; - } + // auto convdw_lite_primitive = (lite::DepthwiseConv2D *)primitive; + // conv_param->pad_u_ = convdw_lite_primitive->PadUp(); + // conv_param->pad_d_ = convdw_lite_primitive->PadDown(); + // conv_param->pad_l_ = convdw_lite_primitive->PadLeft(); + // conv_param->pad_r_ = convdw_lite_primitive->PadRight(); + // conv_param->input_channel_ = convdw_lite_primitive->GetInputChannel(); + // conv_param->dilation_h_ = conv_primitive->GetDilateH(); + // conv_param->dilation_w_ = conv_primitive->GetDilateW(); + // auto pad_mode = conv_primitive->GetPadMode(); + // switch (pad_mode) { + // case schema::PadMode_SAME_UPPER: + // conv_param->pad_mode_ = Pad_Same; + // break; + // case schema::PadMode_VALID: + // conv_param->pad_mode_ = Pad_Valid; + // break; + // default: + // conv_param->pad_mode_ = Pad_No; + // break; + // } + // auto act_type = conv_primitive->GetActivationType(); + // switch (act_type) { + // case schema::ActivationType_RELU: + // conv_param->act_type_ = ActType_Relu; + // break; + // case schema::ActivationType_RELU6: + // conv_param->act_type_ = ActType_Relu6; + // break; + // default: + // conv_param->act_type_ = ActType_No; + // break; + // } return reinterpret_cast(conv_param); } - -Registry DepthwiseConv2DParameterRegistry(schema::PrimitiveType_DepthwiseConv2D, PopulateConvDwParameter); - } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/detection_post_process_populate.cc b/mindspore/lite/src/ops/populate/detection_post_process_populate.cc index 51895495cd..4e66d25374 100644 --- a/mindspore/lite/src/ops/populate/detection_post_process_populate.cc +++ b/mindspore/lite/src/ops/populate/detection_post_process_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/detection_post_process.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/detection_post_process_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateDetectionPostProcessParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateDetectionPostProcessParameter(const void *prim) { DetectionPostProcessParameter *detection_post_process_parameter = reinterpret_cast(malloc(sizeof(DetectionPostProcessParameter))); if (detection_post_process_parameter == nullptr) { @@ -30,24 +27,25 @@ OpParameter *PopulateDetectionPostProcessParameter(const mindspore::lite::Primit return nullptr; } memset(detection_post_process_parameter, 0, sizeof(DetectionPostProcessParameter)); - detection_post_process_parameter->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - detection_post_process_parameter->h_scale_ = param->GetHScale(); - detection_post_process_parameter->w_scale_ = param->GetWScale(); - detection_post_process_parameter->x_scale_ = param->GetXScale(); - detection_post_process_parameter->y_scale_ = param->GetYScale(); - detection_post_process_parameter->nms_iou_threshold_ = param->GetNmsIouThreshold(); - detection_post_process_parameter->nms_score_threshold_ = param->GetNmsScoreThreshold(); - detection_post_process_parameter->max_detections_ = param->GetMaxDetections(); - detection_post_process_parameter->detections_per_class_ = param->GetDetectionsPerClass(); - detection_post_process_parameter->max_classes_per_detection_ = param->GetMaxClassesPerDetection(); - detection_post_process_parameter->num_classes_ = param->GetNumClasses(); - detection_post_process_parameter->use_regular_nms_ = param->GetUseRegularNms(); + auto primitive = static_cast(prim); + detection_post_process_parameter->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_DetectionPostProcess(); + detection_post_process_parameter->h_scale_ = *(param->scale()->begin()); + detection_post_process_parameter->w_scale_ = *(param->scale()->begin() + 1); + detection_post_process_parameter->x_scale_ = *(param->scale()->begin() + 2); + detection_post_process_parameter->y_scale_ = *(param->scale()->begin() + 3); + detection_post_process_parameter->nms_iou_threshold_ = param->nms_iou_threshold(); + detection_post_process_parameter->nms_score_threshold_ = param->nms_score_threshold(); + detection_post_process_parameter->max_detections_ = param->max_detections(); + detection_post_process_parameter->detections_per_class_ = param->detections_per_class(); + detection_post_process_parameter->max_classes_per_detection_ = param->max_classes_per_detection(); + detection_post_process_parameter->num_classes_ = param->num_classes(); + detection_post_process_parameter->use_regular_nms_ = param->use_regular_nms(); return reinterpret_cast(detection_post_process_parameter); } -Registry DetectionPostProcessParameterRegistry(schema::PrimitiveType_DetectionPostProcess, - PopulateDetectionPostProcessParameter); +} // namespace +Registry g_detectionPostProcessParameterRegistry(schema::PrimitiveType_DetectionPostProcess, + PopulateDetectionPostProcessParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/div_populate.cc b/mindspore/lite/src/ops/populate/div_populate.cc index 78af04ef10..647ef86947 100644 --- a/mindspore/lite/src/ops/populate/div_populate.cc +++ b/mindspore/lite/src/ops/populate/div_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/div.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "src/ops/populate/arithmetic_populate.h" namespace mindspore { namespace lite { -OpParameter *PopulateDivParameter(const mindspore::lite::PrimitiveC *primitive) { - ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); +OpParameter *PopulateDivParameter(const void *prim) { + ArithmeticParameter *param = PopulateArithmeticCommonPara(prim); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; return nullptr; } - param->activation_type_ = reinterpret_cast(primitive)->GetActivationType(); return reinterpret_cast(param); } -Registry DivParameterRegistry(schema::PrimitiveType_Div, PopulateDivParameter); +Registry g_divParameterRegistry(schema::PrimitiveType_DivFusion, PopulateDivParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/eltwise_populate.cc b/mindspore/lite/src/ops/populate/eltwise_populate.cc index b3efabb77f..b58ed8f865 100644 --- a/mindspore/lite/src/ops/populate/eltwise_populate.cc +++ b/mindspore/lite/src/ops/populate/eltwise_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,40 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/div.h" -#include "src/ops/eltwise.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "src/ops/populate/arithmetic_populate.h" + namespace mindspore { namespace lite { - -OpParameter *PopulateEltwiseParameter(const mindspore::lite::PrimitiveC *primitive) { - ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); +namespace { +OpParameter *PopulateEltwiseParameter(const void *prim) { + ArithmeticParameter *param = PopulateArithmeticCommonPara(prim); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; return nullptr; } - auto eltwise = reinterpret_cast(primitive); - switch (eltwise->GetMode()) { - case schema::EltwiseMode_PROD: - param->op_parameter_.type_ = schema::PrimitiveType_Mul; - break; - case schema::EltwiseMode_SUM: - param->op_parameter_.type_ = schema::PrimitiveType_Add; - break; - case schema::EltwiseMode_MAXIMUM: - param->op_parameter_.type_ = schema::PrimitiveType_Maximum; - break; - default: - free(param); - return nullptr; - } + auto primitive = static_cast(prim); + param->eltwise_mode_ = primitive->value_as_Eltwise()->mode(); return reinterpret_cast(param); } +} // namespace -Registry EltwiseParameterRegistry(schema::PrimitiveType_Eltwise, PopulateEltwiseParameter); - +Registry g_eltwiseParameterRegistry(schema::PrimitiveType_Eltwise, PopulateEltwiseParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/elu_populate.cc b/mindspore/lite/src/ops/populate/elu_populate.cc index 95821b6481..b5854f3b6e 100644 --- a/mindspore/lite/src/ops/populate/elu_populate.cc +++ b/mindspore/lite/src/ops/populate/elu_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/elu.h" #include "nnacl/fp32/elu_fp32.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { - -OpParameter *PopulateEluParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateEluParameter(const void *prim) { EluParameter *elu_parameter = reinterpret_cast(malloc(sizeof(EluParameter))); if (elu_parameter == nullptr) { MS_LOG(ERROR) << "malloc EluParameter failed."; return nullptr; } memset(elu_parameter, 0, sizeof(EluParameter)); - elu_parameter->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - elu_parameter->alpha_ = param->GetAlpha(); + auto primitive = static_cast(prim); + elu_parameter->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_Elu(); + elu_parameter->alpha_ = param->alpha(); return reinterpret_cast(elu_parameter); } -Registry EluParameterRegistry(schema::PrimitiveType_Elu, PopulateEluParameter); +} // namespace +Registry g_eluParameterRegistry(schema::PrimitiveType_Elu, PopulateEluParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/embedding_lookup_populate.cc b/mindspore/lite/src/ops/populate/embedding_lookup_populate.cc index 907aa261f9..3c0881d1bc 100644 --- a/mindspore/lite/src/ops/populate/embedding_lookup_populate.cc +++ b/mindspore/lite/src/ops/populate/embedding_lookup_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,37 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/embedding_lookup.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/embedding_lookup_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateEmbeddingLookupParameter(const mindspore::lite::PrimitiveC *primitive) { - EmbeddingLookupParameter *embedding_lookup_parameter = +OpParameter *PopulateEmbeddingLookupParameter(const void *prim) { + EmbeddingLookupParameter *param = reinterpret_cast(malloc(sizeof(EmbeddingLookupParameter))); - if (embedding_lookup_parameter == nullptr) { + if (param == nullptr) { MS_LOG(ERROR) << "malloc EmbeddingLookupParameter failed."; return nullptr; } - memset(embedding_lookup_parameter, 0, sizeof(EmbeddingLookupParameter)); - embedding_lookup_parameter->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - embedding_lookup_parameter->max_norm_ = param->GetMaxNorm(); - if (embedding_lookup_parameter->max_norm_ < 0) { - MS_LOG(ERROR) << "Embedding lookup max norm should be positive number, got " - << embedding_lookup_parameter->max_norm_; - free(embedding_lookup_parameter); + memset(param, 0, sizeof(EmbeddingLookupParameter)); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_EmbeddingLookupFusion(); + param->op_parameter_.type_ = primitive->value_type(); + param->max_norm_ = value->max_norm(); + if (param->max_norm_ < 0) { + MS_LOG(ERROR) << "Embedding lookup max norm should be positive number, got " << param->max_norm_; + free(param); return nullptr; } - return reinterpret_cast(embedding_lookup_parameter); + return reinterpret_cast(param); } -Registry EmbeddingLookupParameterRegistry(schema::PrimitiveType_EmbeddingLookup, PopulateEmbeddingLookupParameter); +Registry EmbeddingLookupParameterRegistry(schema::PrimitiveType_EmbeddingLookupFusion, PopulateEmbeddingLookupParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/erf_populate.cc b/mindspore/lite/src/ops/populate/erf_populate.cc new file mode 100644 index 0000000000..ea7657b118 --- /dev/null +++ b/mindspore/lite/src/ops/populate/erf_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_erfParameterRegistry(schema::PrimitiveType_Erf, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/exp_populate.cc b/mindspore/lite/src/ops/populate/exp_populate.cc index 4535413fe7..122dc3c84a 100644 --- a/mindspore/lite/src/ops/populate/exp_populate.cc +++ b/mindspore/lite/src/ops/populate/exp_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,26 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/exp.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/exp_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateExpParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateExpParameter(const void *prim) { ExpParameter *exp_parameter = reinterpret_cast(malloc(sizeof(ExpParameter))); if (exp_parameter == nullptr) { MS_LOG(ERROR) << "malloc ExpParameter failed."; return nullptr; } memset(exp_parameter, 0, sizeof(ExpParameter)); - exp_parameter->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - exp_parameter->base_ = param->GetBase(); - exp_parameter->scale_ = param->GetScale(); - exp_parameter->shift_ = param->GetShift(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_ExpFusion(); + exp_parameter->op_parameter_.type_ = primitive->value_type(); + exp_parameter->base_ = value->base(); + exp_parameter->scale_ = value->scale(); + exp_parameter->shift_ = value->shift(); if (exp_parameter->base_ != -1 && exp_parameter->base_ <= 0) { MS_LOG(ERROR) << "Exp base must be strictly positive, got " << exp_parameter->base_; free(exp_parameter); @@ -41,6 +40,6 @@ OpParameter *PopulateExpParameter(const mindspore::lite::PrimitiveC *primitive) return reinterpret_cast(exp_parameter); } -Registry ExpParameterRegistry(schema::PrimitiveType_Exp, PopulateExpParameter); +Registry ExpParameterRegistry(schema::PrimitiveType_ExpFusion, PopulateExpParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/expand_dims_populate.cc b/mindspore/lite/src/ops/populate/expand_dims_populate.cc index bb62cc477c..47351d005f 100644 --- a/mindspore/lite/src/ops/populate/expand_dims_populate.cc +++ b/mindspore/lite/src/ops/populate/expand_dims_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { - -OpParameter *PopulateExpandDimsParameter(const mindspore::lite::PrimitiveC *primitive) { - OpParameter *expand_dims_param = reinterpret_cast(malloc(sizeof(OpParameter))); - if (expand_dims_param == nullptr) { +namespace { +OpParameter *PopulateExpandDimsParameter(const void *prim) { + OpParameter *expand_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (expand_param == nullptr) { MS_LOG(ERROR) << "malloc ExpandDimsParameter failed."; return nullptr; } - expand_dims_param->type_ = primitive->Type(); - memset(expand_dims_param, 0, sizeof(OpParameter)); - return reinterpret_cast(expand_dims_param); + memset(expand_param, 0, sizeof(OpParameter)); + auto primitive = static_cast(prim); + expand_param->type_ = primitive->value_type(); + return reinterpret_cast(expand_param); } +} // namespace -Registry ExpandDimsParameterRegistry(schema::PrimitiveType_ExpandDims, PopulateExpandDimsParameter); - +Registry g_expandDimsParameterRegistry(schema::PrimitiveType_ExpandDims, PopulateExpandDimsParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/fill_populate.cc b/mindspore/lite/src/ops/populate/fill_populate.cc index c68d457fce..b48f4b24cf 100644 --- a/mindspore/lite/src/ops/populate/fill_populate.cc +++ b/mindspore/lite/src/ops/populate/fill_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,34 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/fill.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "nnacl/fill_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateFillParameter(const mindspore::lite::PrimitiveC *primitive) { - const auto param = reinterpret_cast(const_cast(primitive)); - FillParameter *fill_param = reinterpret_cast(malloc(sizeof(FillParameter))); +namespace { +OpParameter *PopulateFillParameter(const void *prim) { + OpParameter *fill_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (fill_param == nullptr) { MS_LOG(ERROR) << "malloc FillParameter failed."; return nullptr; } - memset(fill_param, 0, sizeof(FillParameter)); - fill_param->op_parameter_.type_ = primitive->Type(); - auto flatDims = param->GetDims(); - fill_param->num_dims_ = flatDims.size(); - int i = 0; - for (auto iter = flatDims.begin(); iter != flatDims.end(); iter++) { - fill_param->dims_[i++] = *iter; - } + memset(fill_param, 0, sizeof(OpParameter)); + auto primitive = static_cast(prim); + fill_param->type_ = primitive->value_type(); return reinterpret_cast(fill_param); } +} // namespace -Registry FillParameterRegistry(schema::PrimitiveType_Fill, PopulateFillParameter); +Registry g_fillParameterRegistry(schema::PrimitiveType_Fill, PopulateFillParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/flatten_populate.cc b/mindspore/lite/src/ops/populate/flatten_populate.cc index 6905ad3176..09cbab37cd 100644 --- a/mindspore/lite/src/ops/populate/flatten_populate.cc +++ b/mindspore/lite/src/ops/populate/flatten_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateFlattenParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateFlattenParameter(const void *prim) { OpParameter *flatten_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (flatten_param == nullptr) { MS_LOG(ERROR) << "malloc FlattenParameter failed."; return nullptr; } memset(flatten_param, 0, sizeof(OpParameter)); - flatten_param->type_ = primitive->Type(); + + auto primitive = static_cast(prim); + flatten_param->type_ = primitive->value_type(); return reinterpret_cast(flatten_param); } -Registry FlattenParameterRegistry(schema::PrimitiveType_Flatten, PopulateFlattenParameter); +Registry FlattenParameterRegistry(schema::PrimitiveType_Flatten, PopulateFlattenParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/full_connection_populate.cc b/mindspore/lite/src/ops/populate/full_connection_populate.cc index fafc985b64..e60c48a222 100644 --- a/mindspore/lite/src/ops/populate/full_connection_populate.cc +++ b/mindspore/lite/src/ops/populate/full_connection_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,40 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/full_connection.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/matmul_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateFullconnectionParameter(const mindspore::lite::PrimitiveC *primitive) { - auto param = - reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateFullconnectionParameter(const void *prim) { MatMulParameter *matmul_param = reinterpret_cast(malloc(sizeof(MatMulParameter))); if (matmul_param == nullptr) { MS_LOG(ERROR) << "malloc MatMulParameter failed."; return nullptr; } memset(matmul_param, 0, sizeof(MatMulParameter)); - matmul_param->op_parameter_.type_ = primitive->Type(); + auto *primitive = static_cast(prim); + matmul_param->op_parameter_.type_ = primitive->value_type(); + auto full_conn_prim = primitive->value_as_FullConnection(); matmul_param->b_transpose_ = true; matmul_param->a_transpose_ = false; - matmul_param->has_bias_ = param->GetHasBias(); - if (param->GetActivationType() == schema::ActivationType_RELU) { + matmul_param->has_bias_ = full_conn_prim->has_bias(); + if (full_conn_prim->activation_type() == schema::ActivationType_RELU) { matmul_param->act_type_ = ActType_Relu; - } else if (param->GetActivationType() == schema::ActivationType_RELU6) { + } else if (full_conn_prim->activation_type() == schema::ActivationType_RELU6) { matmul_param->act_type_ = ActType_Relu6; } else { matmul_param->act_type_ = ActType_No; } - + matmul_param->axis_ = full_conn_prim->axis(); + matmul_param->use_axis_ = full_conn_prim->use_axis(); return reinterpret_cast(matmul_param); } +} // namespace -Registry FullConnectionParameterRegistry(schema::PrimitiveType_FullConnection, PopulateFullconnectionParameter); - +Registry g_fullConnRegistry(schema::PrimitiveType_FullConnection, PopulateFullconnectionParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/fused_batchnorm_populate.cc b/mindspore/lite/src/ops/populate/fused_batchnorm_populate.cc index c7825066f0..1457eeea6a 100644 --- a/mindspore/lite/src/ops/populate/fused_batchnorm_populate.cc +++ b/mindspore/lite/src/ops/populate/fused_batchnorm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,32 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/fused_batchnorm.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/batchnorm_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateFusedBatchNorm(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateFusedBatchNorm(const void *prim) { BatchNormParameter *batch_norm_param = reinterpret_cast(malloc(sizeof(BatchNormParameter))); if (batch_norm_param == nullptr) { MS_LOG(ERROR) << "malloc BatchNormParameter failed."; return nullptr; } memset(batch_norm_param, 0, sizeof(BatchNormParameter)); - batch_norm_param->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - batch_norm_param->epsilon_ = param->GetEpsilon(); - batch_norm_param->momentum_ = param->GetMomentum(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_FusedBatchNorm(); + batch_norm_param->op_parameter_.type_ = primitive->value_type(); + batch_norm_param->epsilon_ = value->epsilon(); + batch_norm_param->momentum_ = value->momentum(); batch_norm_param->fused_ = true; return reinterpret_cast(batch_norm_param); } -Registry FusedBatchNormParameterRegistry(schema::PrimitiveType_FusedBatchNorm, PopulateFusedBatchNorm); +Registry FusedBatchNormParameterRegistry(schema::PrimitiveType_FusedBatchNorm, PopulateFusedBatchNorm, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/gather_nd_populate.cc b/mindspore/lite/src/ops/populate/gather_nd_populate.cc index efadd2a69c..953cfe1b37 100644 --- a/mindspore/lite/src/ops/populate/gather_nd_populate.cc +++ b/mindspore/lite/src/ops/populate/gather_nd_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/gather_nd.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/gatherNd_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateGatherNdParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateGatherNdParameter(const void *prim) { GatherNdParameter *gather_nd_param = reinterpret_cast(malloc(sizeof(GatherNdParameter))); if (gather_nd_param == nullptr) { MS_LOG(ERROR) << "malloc GatherNdParameter failed."; return nullptr; } memset(gather_nd_param, 0, sizeof(GatherNdParameter)); - gather_nd_param->op_parameter_.type_ = primitive->Type(); + auto primitive = static_cast(prim); + gather_nd_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(gather_nd_param); } +} // namespace -Registry GatherNdParameterRegistry(schema::PrimitiveType_GatherNd, PopulateGatherNdParameter); - +Registry g_gatherNdParameterRegistry(schema::PrimitiveType_GatherNd, PopulateGatherNdParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/gather_populate.cc b/mindspore/lite/src/ops/populate/gather_populate.cc index bec392473a..0da4594501 100644 --- a/mindspore/lite/src/ops/populate/gather_populate.cc +++ b/mindspore/lite/src/ops/populate/gather_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,36 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/gather.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/gather_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateGatherParameter(const mindspore::lite::PrimitiveC *primitive) { - auto gather_attr = reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateGatherParameter(const void *prim) { GatherParameter *gather_param = reinterpret_cast(malloc(sizeof(GatherParameter))); if (gather_param == nullptr) { MS_LOG(ERROR) << "malloc GatherParameter failed."; return nullptr; } memset(gather_param, 0, sizeof(GatherParameter)); - gather_param->op_parameter_.type_ = primitive->Type(); - if (gather_attr->GetAxis() < 0) { - MS_LOG(ERROR) << "axis should be >= 0."; - free(gather_param); - return nullptr; - } - gather_param->axis_ = gather_attr->GetAxis(); - gather_param->batchDims_ = gather_attr->GetBatchDims(); + auto primitive = static_cast(prim); + gather_param->op_parameter_.type_ = primitive->value_type(); + return reinterpret_cast(gather_param); } -Registry GatherParameterRegistry(schema::PrimitiveType_Gather, PopulateGatherParameter); +} // namespace +Registry g_gatherParameterRegistry(schema::PrimitiveType_Gather, PopulateGatherParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/gru_populate.cc b/mindspore/lite/src/ops/populate/gru_populate.cc index 1e57855d30..4e492aca98 100644 --- a/mindspore/lite/src/ops/populate/gru_populate.cc +++ b/mindspore/lite/src/ops/populate/gru_populate.cc @@ -13,30 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/gru.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/gru_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateGruParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateGruParameter(const void *prim) { GruParameter *gru_param = reinterpret_cast(malloc(sizeof(GruParameter))); if (gru_param == nullptr) { MS_LOG(ERROR) << "malloc GruParameter failed."; return nullptr; } memset(gru_param, 0, sizeof(GruParameter)); - gru_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); + auto *primitive = static_cast(prim); + gru_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_GRU(); if (param == nullptr) { free(gru_param); MS_LOG(ERROR) << "get Gru param nullptr."; return nullptr; } - gru_param->bidirectional_ = param->GetBidirection(); + gru_param->bidirectional_ = param->bidirectional(); return reinterpret_cast(gru_param); } -Registry GruParameterRegistry(schema::PrimitiveType_Gru, PopulateGruParameter); +} // namespace + +Registry g_gruParameterRegistry(schema::PrimitiveType_GRU, PopulateGruParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/hashtable_lookup_populate.cc b/mindspore/lite/src/ops/populate/hashtable_lookup_populate.cc index 3b97fa9f5f..d4ff5ac11e 100644 --- a/mindspore/lite/src/ops/populate/hashtable_lookup_populate.cc +++ b/mindspore/lite/src/ops/populate/hashtable_lookup_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/hashtable_lookup.h" -#include "src/common/string_util.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateHashtableLookupParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateHashtableLookupParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "new OpParameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry HashtableLookupParameterRegistry(schema::PrimitiveType_HashtableLookup, PopulateHashtableLookupParameter); +Registry HashtableLookupParameterRegistry(schema::PrimitiveType_HashtableLookup, PopulateHashtableLookupParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/if_populate.cc b/mindspore/lite/src/ops/populate/if_populate.cc new file mode 100644 index 0000000000..57b4aa8018 --- /dev/null +++ b/mindspore/lite/src/ops/populate/if_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_ifParameterRegistry(schema::PrimitiveType_If, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/instance_norm_populate.cc b/mindspore/lite/src/ops/populate/instance_norm_populate.cc index 13d33fd8f8..ca4bf66b59 100644 --- a/mindspore/lite/src/ops/populate/instance_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/instance_norm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "src/ops/instance_norm.h" #include "nnacl/instance_norm_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateInstanceNormParameter(const mindspore::lite::PrimitiveC *primitive) { - const auto param = - reinterpret_cast(const_cast(primitive)); +OpParameter *PopulateInstanceNormParameter(const void *prim) { InstanceNormParameter *instance_norm_param = reinterpret_cast(malloc(sizeof(InstanceNormParameter))); if (instance_norm_param == nullptr) { @@ -31,11 +26,14 @@ OpParameter *PopulateInstanceNormParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(instance_norm_param, 0, sizeof(InstanceNormParameter)); - instance_norm_param->op_parameter_.type_ = primitive->Type(); - instance_norm_param->epsilon_ = param->GetEpsilon(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_InstanceNorm(); + instance_norm_param->op_parameter_.type_ = primitive->value_type(); + instance_norm_param->epsilon_ = value->epsilon(); return reinterpret_cast(instance_norm_param); } -Registry InstanceNormParameterRegistry(schema::PrimitiveType_InstanceNorm, PopulateInstanceNormParameter); +Registry InstanceNormParameterRegistry(schema::PrimitiveType_InstanceNorm, PopulateInstanceNormParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/invert_permutation_populate.cc b/mindspore/lite/src/ops/populate/invert_permutation_populate.cc new file mode 100644 index 0000000000..4268621d4d --- /dev/null +++ b/mindspore/lite/src/ops/populate/invert_permutation_populate.cc @@ -0,0 +1,24 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_invertPermutationParameterRegistry(schema::PrimitiveType_InvertPermutation, DefaultPopulateParameter, + SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/isfinite_populate.cc b/mindspore/lite/src/ops/populate/isfinite_populate.cc new file mode 100644 index 0000000000..9148ecb8b2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/isfinite_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_isFiniteParameterRegistry(schema::PrimitiveType_IsFinite, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/l2_norm_populate.cc b/mindspore/lite/src/ops/populate/l2_norm_populate.cc index cfcd249873..1bfe2ef74e 100644 --- a/mindspore/lite/src/ops/populate/l2_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/l2_norm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,52 +13,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/l2_norm.h" #include -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/l2_norm_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateL2NormParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateL2NormParameter(const void *prim) { L2NormParameter *l2_norm_parameter = reinterpret_cast(malloc(sizeof(L2NormParameter))); if (l2_norm_parameter == nullptr) { MS_LOG(ERROR) << "malloc L2NormParameter failed."; return nullptr; } memset(l2_norm_parameter, 0, sizeof(L2NormParameter)); - l2_norm_parameter->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - MS_ASSERT(param); - auto axis_vec = param->GetAxis(); - l2_norm_parameter->axis_num_ = axis_vec.size(); - if (axis_vec.size() > SIZE_MAX / sizeof(int)) { - MS_LOG(ERROR) << "axis_vec size too big"; - free(l2_norm_parameter); - return nullptr; - } - MS_ASSERT(axis_vec.size() < 8); - for (size_t i = 0; i < axis_vec.size(); i++) { - l2_norm_parameter->axis_[i] = axis_vec[i]; + + auto primitive = static_cast(prim); + auto value = primitive->value_as_L2NormalizeFusion(); + l2_norm_parameter->op_parameter_.type_ = primitive->value_type(); + + auto axis_vec = value->axis(); + l2_norm_parameter->axis_num_ = axis_vec->size(); + + MS_ASSERT(axis_vec->size() < 8); + for (size_t i = 0; i < axis_vec->size(); i++) { + l2_norm_parameter->axis_[i] = static_cast(axis_vec->Get(i)); } - if (param->GetEpsilon() < 1e-6) { + if (value->epsilon() < 1e-6) { l2_norm_parameter->epsilon_ = 1e-6; } else { - l2_norm_parameter->epsilon_ = param->GetEpsilon(); + l2_norm_parameter->epsilon_ = value->epsilon(); } - if (param->GetActivationType() == static_cast(schema::ActivationType_RELU)) { + if (value->activation_type() == static_cast(schema::ActivationType_RELU)) { l2_norm_parameter->act_type_ = ActType_Relu; - } else if (param->GetActivationType() == static_cast(schema::ActivationType_RELU6)) { + } else if (value->activation_type() == static_cast(schema::ActivationType_RELU6)) { l2_norm_parameter->act_type_ = ActType_Relu6; } else { l2_norm_parameter->act_type_ = ActType_No; } return reinterpret_cast(l2_norm_parameter); } -Registry L2NormParameterRegistry(schema::PrimitiveType_L2Norm, PopulateL2NormParameter); +Registry L2NormParameterRegistry(schema::PrimitiveType_L2NormalizeFusion, PopulateL2NormParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/layer_norm_populate.cc b/mindspore/lite/src/ops/populate/layer_norm_populate.cc index e87edc40d4..b99bb95a16 100644 --- a/mindspore/lite/src/ops/populate/layer_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/layer_norm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,35 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "nnacl/layer_norm_parameter.h" -#include "src/ops/layer_norm.h" -#include "src/ops/primitive_c.h" +#include #include "src/ops/populate/populate_register.h" - namespace mindspore { namespace lite { -OpParameter *PopulateLayerNormParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateLayerNormParameter(const void *prim) { auto layer_norm_parameter = reinterpret_cast(malloc(sizeof(LayerNormParameter))); if (layer_norm_parameter == nullptr) { MS_LOG(ERROR) << "malloc LayerNormParameter failed."; return nullptr; } memset(layer_norm_parameter, 0, sizeof(LayerNormParameter)); - layer_norm_parameter->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - auto normalized_shape = param->GetNormlizedShape(); - layer_norm_parameter->normalized_dims_ = normalized_shape.size(); - MS_ASSERT(normalized_shape.size() < 8); - for (size_t i = 0; i < normalized_shape.size(); i++) { - layer_norm_parameter->normalized_shape_[i] = normalized_shape[i]; - } - layer_norm_parameter->epsilon_ = param->GetEpsilon(); - layer_norm_parameter->begin_norm_axis_ = param->GetBeginNormAxis(); - layer_norm_parameter->begin_params_axis_ = param->GetBeginParamsAxis(); + auto *primitive = static_cast(prim); + layer_norm_parameter->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_LayerNormFusion(); + layer_norm_parameter->epsilon_ = param->epsilon(); + layer_norm_parameter->elementwise_affine_ = param->elementwise_affine(); + layer_norm_parameter->begin_norm_axis_ = static_cast(param->begin_norm_axis()); + layer_norm_parameter->begin_params_axis_ = static_cast(param->begin_params_axis()); return reinterpret_cast(layer_norm_parameter); } -Registry LayerNormParameterRegistry(schema::PrimitiveType_LayerNorm, PopulateLayerNormParameter); +Registry g_layerNormParameterRegistry(schema::PrimitiveType_LayerNormFusion, PopulateLayerNormParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/lin_space_populate.cc b/mindspore/lite/src/ops/populate/lin_space_populate.cc new file mode 100644 index 0000000000..fdc4f1a1db --- /dev/null +++ b/mindspore/lite/src/ops/populate/lin_space_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_linSpaceParameterRegistry(schema::PrimitiveType_LinSpace, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/local_response_normalization_populate.cc b/mindspore/lite/src/ops/populate/local_response_normalization_populate.cc index 36fc15ce04..a208a78a34 100644 --- a/mindspore/lite/src/ops/populate/local_response_normalization_populate.cc +++ b/mindspore/lite/src/ops/populate/local_response_normalization_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,18 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/local_response_normalization.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/local_response_norm_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateLocalResponseNormParameter(const mindspore::lite::PrimitiveC *primitive) { - auto local_response_norm_attr = reinterpret_cast( - const_cast(primitive)); +OpParameter *PopulateLocalResponseNormParameter(const void *prim) { LocalResponseNormParameter *lrn_param = reinterpret_cast(malloc(sizeof(LocalResponseNormParameter))); if (lrn_param == nullptr) { @@ -32,16 +27,18 @@ OpParameter *PopulateLocalResponseNormParameter(const mindspore::lite::Primitive return nullptr; } memset(lrn_param, 0, sizeof(LocalResponseNormParameter)); - lrn_param->op_parameter_.type_ = primitive->Type(); - lrn_param->depth_radius_ = local_response_norm_attr->GetDepthRadius(); - lrn_param->bias_ = local_response_norm_attr->GetBias(); - lrn_param->alpha_ = local_response_norm_attr->GetAlpha(); - lrn_param->beta_ = local_response_norm_attr->GetBeta(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_LRN(); + lrn_param->op_parameter_.type_ = primitive->value_type(); + lrn_param->depth_radius_ = value->depth_radius(); + lrn_param->bias_ = value->bias(); + lrn_param->alpha_ = value->alpha(); + lrn_param->beta_ = value->beta(); return reinterpret_cast(lrn_param); } -Registry LocalResponseNormalizationParameterRegistry(schema::PrimitiveType_LocalResponseNormalization, - PopulateLocalResponseNormParameter); +Registry LocalResponseNormalizationParameterRegistry(schema::PrimitiveType_LRN, PopulateLocalResponseNormParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/lsh_projection_populate.cc b/mindspore/lite/src/ops/populate/lsh_projection_populate.cc index 70fccf14da..f3b59f7aba 100644 --- a/mindspore/lite/src/ops/populate/lsh_projection_populate.cc +++ b/mindspore/lite/src/ops/populate/lsh_projection_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/lsh_projection.h" #include "nnacl/lsh_projection_parameter.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateLshProjectionParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateLshProjectionParameter(const void *prim) { LshProjectionParameter *lsh_project_param = reinterpret_cast(malloc(sizeof(LshProjectionParameter))); if (lsh_project_param == nullptr) { @@ -29,12 +27,15 @@ OpParameter *PopulateLshProjectionParameter(const mindspore::lite::PrimitiveC *p return nullptr; } memset(lsh_project_param, 0, sizeof(LshProjectionParameter)); - lsh_project_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - lsh_project_param->lsh_type_ = param->GetLshType(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_LshProjection(); + lsh_project_param->op_parameter_.type_ = primitive->value_type(); + lsh_project_param->lsh_type_ = value->type(); return reinterpret_cast(lsh_project_param); } -Registry LshProjectionParameterRegistry(schema::PrimitiveType_LshProjection, PopulateLshProjectionParameter); +Registry LshProjectionParameterRegistry(schema::PrimitiveType_LshProjection, PopulateLshProjectionParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/lstm_populate.cc b/mindspore/lite/src/ops/populate/lstm_populate.cc index 95642daec0..e94dcceeaa 100644 --- a/mindspore/lite/src/ops/populate/lstm_populate.cc +++ b/mindspore/lite/src/ops/populate/lstm_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,32 +13,34 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/lstm.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/lstm_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateLstmParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateLstmParameter(const void *prim) { LstmParameter *lstm_param = reinterpret_cast(malloc(sizeof(LstmParameter))); if (lstm_param == nullptr) { MS_LOG(ERROR) << "malloc LstmParameter failed."; return nullptr; } memset(lstm_param, 0, sizeof(LstmParameter)); - lstm_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); + auto primitive = static_cast(prim); + lstm_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_LSTM(); if (param == nullptr) { free(lstm_param); MS_LOG(ERROR) << "get Lstm param nullptr."; return nullptr; } - lstm_param->bidirectional_ = param->GetBidirection(); - lstm_param->smooth_ = param->GetSmooth(); + + lstm_param->bidirectional_ = param->bidirectional(); + lstm_param->zoneout_cell_ = param->zoneout_cell(); + lstm_param->zoneout_hidden_ = param->zoneout_hidden(); return reinterpret_cast(lstm_param); } -Registry LstmParameterRegistry(schema::PrimitiveType_Lstm, PopulateLstmParameter); +} // namespace +Registry g_lstmParameterRegistry(schema::PrimitiveType_LSTM, PopulateLstmParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/matmul_populate.cc b/mindspore/lite/src/ops/populate/matmul_populate.cc index 3c824202dc..f4bb92c109 100644 --- a/mindspore/lite/src/ops/populate/matmul_populate.cc +++ b/mindspore/lite/src/ops/populate/matmul_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,31 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/matmul.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/matmul_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateMatMulParameter(const mindspore::lite::PrimitiveC *primitive) { - auto param = reinterpret_cast(const_cast(primitive)); +OpParameter *PopulateMatMulParameter(const void *prim) { MatMulParameter *matmul_param = reinterpret_cast(malloc(sizeof(MatMulParameter))); if (matmul_param == nullptr) { MS_LOG(ERROR) << "malloc MatMulParameter failed."; return nullptr; } memset(matmul_param, 0, sizeof(MatMulParameter)); - matmul_param->op_parameter_.type_ = primitive->Type(); - matmul_param->b_transpose_ = param->GetTransposeB(); - matmul_param->a_transpose_ = param->GetTransposeA(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_MatMul(); + matmul_param->op_parameter_.type_ = primitive->value_type(); + matmul_param->b_transpose_ = value->transpose_b(); + matmul_param->a_transpose_ = value->transpose_a(); matmul_param->has_bias_ = false; matmul_param->act_type_ = ActType_No; return reinterpret_cast(matmul_param); } -Registry MatMulParameterRegistry(schema::PrimitiveType_MatMul, PopulateMatMulParameter); +Registry MatMulParameterRegistry(schema::PrimitiveType_MatMul, PopulateMatMulParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/merge_populate.cc b/mindspore/lite/src/ops/populate/merge_populate.cc index 1945864f05..8ab485a46d 100644 --- a/mindspore/lite/src/ops/populate/merge_populate.cc +++ b/mindspore/lite/src/ops/populate/merge_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,22 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateMergeParameter(const mindspore::lite::PrimitiveC *primitive) { + +OpParameter *PopulateMergeParameter(const void *prim) { OpParameter *merge_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); if (merge_parameter == nullptr) { MS_LOG(ERROR) << "malloc Merge parameter failed."; return nullptr; } memset(merge_parameter, 0, sizeof(OpParameter)); - merge_parameter->type_ = primitive->Type(); + auto primitive = static_cast(prim); + merge_parameter->type_ = primitive->value_type(); return reinterpret_cast(merge_parameter); } -Registry MergeParameterRegistry(schema::PrimitiveType_Merge, PopulateMergeParameter); +Registry MergeParameterRegistry(schema::PrimitiveType_Merge, PopulateMergeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/mfcc_populate.cc b/mindspore/lite/src/ops/populate/mfcc_populate.cc new file mode 100644 index 0000000000..22b2cea316 --- /dev/null +++ b/mindspore/lite/src/ops/populate/mfcc_populate.cc @@ -0,0 +1,39 @@ +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "nnacl/infer/mfcc_infer.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateMfccParameter(const void *prim) { + MfccParameter *arg_param = reinterpret_cast(malloc(sizeof(MfccParameter))); + if (arg_param == nullptr) { + MS_LOG(ERROR) << "malloc MfccParameter failed."; + return nullptr; + } + memset(arg_param, 0, sizeof(MfccParameter)); + auto *primitive = static_cast(prim); + arg_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_Mfcc(); + arg_param->dct_coeff_num_ = param->dct_coeff_num(); + return reinterpret_cast(arg_param); +} +} // namespace + +Registry g_mfccParameterRegistry(schema::PrimitiveType_Mfcc, PopulateMfccParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/mul_populate.cc b/mindspore/lite/src/ops/populate/mul_populate.cc index 1d7b709eda..b56957d39b 100644 --- a/mindspore/lite/src/ops/populate/mul_populate.cc +++ b/mindspore/lite/src/ops/populate/mul_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/mul.h" #include "nnacl/arithmetic.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "src/ops/populate/arithmetic_populate.h" namespace mindspore { namespace lite { - -OpParameter *PopulateMulParameter(const mindspore::lite::PrimitiveC *primitive) { - ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); +namespace { +OpParameter *PopulateMulParameter(const void *prim) { + ArithmeticParameter *param = PopulateArithmeticCommonPara(prim); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; return nullptr; } - param->activation_type_ = reinterpret_cast(primitive)->GetActivationType(); + auto *primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); + // auto mul_prim = primitive->value_as_Mul(); + // param->activation_type_ = mul_prim->activationType(); return reinterpret_cast(param); } +} // namespace -Registry MulParameterRegistry(schema::PrimitiveType_Mul, PopulateMulParameter); - +Registry g_mulParameterRegistry(schema::PrimitiveType_MulFusion, PopulateMulParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/nchw2nhwc_populate.cc b/mindspore/lite/src/ops/populate/nchw2nhwc_populate.cc deleted file mode 100644 index 47ba44e401..0000000000 --- a/mindspore/lite/src/ops/populate/nchw2nhwc_populate.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" -#include "src/ops/populate/populate_register.h" -#include "src/common/common.h" -#include "nnacl/transpose.h" - -namespace mindspore { -namespace lite { - -OpParameter *PopulateNchw2NhwcParameter(const mindspore::lite::PrimitiveC *primitive) { - TransposeParameter *parameter = reinterpret_cast(malloc(sizeof(TransposeParameter))); - if (parameter == nullptr) { - MS_LOG(ERROR) << "malloc OpParameter failed."; - return nullptr; - } - memset(parameter, 0, sizeof(OpParameter)); - parameter->op_parameter_.type_ = primitive->Type(); - parameter->num_axes_ = 4; - parameter->perm_[0] = 0; - parameter->perm_[1] = 2; - parameter->perm_[2] = 3; - parameter->perm_[3] = 1; - return reinterpret_cast(parameter); -} -Registry Nchw2NhwcParameterRegistry(schema::PrimitiveType_Nchw2Nhwc, PopulateNchw2NhwcParameter); - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/nhwc2nchw_populate.cc b/mindspore/lite/src/ops/populate/nhwc2nchw_populate.cc deleted file mode 100644 index 5156fa0b25..0000000000 --- a/mindspore/lite/src/ops/populate/nhwc2nchw_populate.cc +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" -#include "src/ops/populate/populate_register.h" -#include "src/common/common.h" -#include "nnacl/transpose.h" - -namespace mindspore { -namespace lite { - -OpParameter *PopulateNhwc2NchwParameter(const mindspore::lite::PrimitiveC *primitive) { - TransposeParameter *parameter = reinterpret_cast(malloc(sizeof(TransposeParameter))); - if (parameter == nullptr) { - MS_LOG(ERROR) << "malloc OpParameter failed."; - return nullptr; - } - memset(parameter, 0, sizeof(OpParameter)); - parameter->op_parameter_.type_ = primitive->Type(); - parameter->num_axes_ = 4; - parameter->perm_[0] = 0; - parameter->perm_[1] = 3; - parameter->perm_[2] = 1; - parameter->perm_[3] = 2; - return reinterpret_cast(parameter); -} - -Registry Nhwc2NchwParameterRegistry(schema::PrimitiveType_Nhwc2Nchw, PopulateNhwc2NchwParameter); - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc b/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc index 3fc60b09ca..95a49ca95f 100644 --- a/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc +++ b/mindspore/lite/src/ops/populate/non_max_suppression_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/non_max_suppression.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/non_max_suppression_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateNonMaxSuppressionParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateNonMaxSuppressionParameter(const void *prim) { NMSParameter *param = reinterpret_cast(malloc(sizeof(NMSParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc param failed."; return nullptr; } memset(param, 0, sizeof(NMSParameter)); - param->op_parameter_.type_ = primitive->Type(); - auto prim = - reinterpret_cast(const_cast(primitive)); - param->center_point_box_ = prim->GetCenterPointBox(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_NonMaxSuppression(); + param->op_parameter_.type_ = primitive->value_type(); + param->center_point_box_ = value->center_point_box(); return reinterpret_cast(param); } -Registry NonMaxSuppressionParameterRegistry(schema::PrimitiveType_NonMaxSuppression, - PopulateNonMaxSuppressionParameter); +Registry NonMaxSuppressionParameterRegistry(schema::PrimitiveType_NonMaxSuppression, PopulateNonMaxSuppressionParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/nonzero_populate.cc b/mindspore/lite/src/ops/populate/nonzero_populate.cc index 7436c1965b..dfa5e2d22d 100644 --- a/mindspore/lite/src/ops/populate/nonzero_populate.cc +++ b/mindspore/lite/src/ops/populate/nonzero_populate.cc @@ -13,22 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" namespace mindspore { namespace lite { - -OpParameter *PopulateNonZeroParameter(const mindspore::lite::PrimitiveC *primitive) { - auto nonzero_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); - if (nonzero_parameter == nullptr) { - MS_LOG(ERROR) << "malloc Where parameter failed."; - return nullptr; - } - memset(nonzero_parameter, 0, sizeof(OpParameter)); - nonzero_parameter->type_ = primitive->Type(); - return nonzero_parameter; -} -Registry NonZeroParameterRegistry(schema::PrimitiveType_NonZero, PopulateNonZeroParameter); +Registry g_nonZeroParameterRegistry(schema::PrimitiveType_NonZero, DefaultPopulateParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/one_hot_populate.cc b/mindspore/lite/src/ops/populate/one_hot_populate.cc index 2964637be0..1343c3d68f 100644 --- a/mindspore/lite/src/ops/populate/one_hot_populate.cc +++ b/mindspore/lite/src/ops/populate/one_hot_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,33 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/one_hot.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/one_hot_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateOneHotParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateOneHotParameter(const void *prim) { OneHotParameter *one_hot_param = reinterpret_cast(malloc(sizeof(OneHotParameter))); if (one_hot_param == nullptr) { MS_LOG(ERROR) << "malloc OneHotParameter failed."; return nullptr; } memset(one_hot_param, 0, sizeof(OneHotParameter)); - one_hot_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - if (param == nullptr) { - free(one_hot_param); - MS_LOG(ERROR) << "get OneHot param nullptr."; - return nullptr; - } - one_hot_param->axis_ = param->GetAxis(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_OneHot(); + one_hot_param->op_parameter_.type_ = primitive->value_type(); + one_hot_param->axis_ = value->axis(); return reinterpret_cast(one_hot_param); } -Registry OneHotParameterRegistry(schema::PrimitiveType_OneHot, PopulateOneHotParameter); +Registry OneHotParameterRegistry(schema::PrimitiveType_OneHot, PopulateOneHotParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/oneslike_populate.cc b/mindspore/lite/src/ops/populate/oneslike_populate.cc index 71b5a05a62..2882d8df26 100644 --- a/mindspore/lite/src/ops/populate/oneslike_populate.cc +++ b/mindspore/lite/src/ops/populate/oneslike_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/oneslike.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateOnesLikeParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateOnesLikeParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc OnesLike Parameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } -Registry OnesLikeParameterRegistry(schema::PrimitiveType_OnesLike, PopulateOnesLikeParameter); +Registry OnesLikeParameterRegistry(schema::PrimitiveType_OnesLike, PopulateOnesLikeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/p_relu_populate.cc b/mindspore/lite/src/ops/populate/p_relu_populate.cc index d666069659..18a3274142 100644 --- a/mindspore/lite/src/ops/populate/p_relu_populate.cc +++ b/mindspore/lite/src/ops/populate/p_relu_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,28 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/p_relu.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/prelu_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulatePReLUParameter(const mindspore::lite::PrimitiveC *primitive) { - auto param = reinterpret_cast(const_cast(primitive)); - PReluParameter *prelu_param = reinterpret_cast(malloc(sizeof(PReluParameter))); - if (prelu_param == nullptr) { +OpParameter *PopulatePReLUParameter(const void *prim) { + PReluParameter *param = reinterpret_cast(malloc(sizeof(PReluParameter))); + if (param == nullptr) { MS_LOG(ERROR) << "malloc PReluParameter failed."; return nullptr; } - memset(prelu_param, 0, sizeof(PReluParameter)); - prelu_param->op_parameter_.type_ = primitive->Type(); - prelu_param->channelShared = param->GetChannelShared(); - return reinterpret_cast(prelu_param); + memset(param, 0, sizeof(PReluParameter)); + auto primitive = static_cast(prim); + auto value = primitive->value_as_PReLUFusion(); + param->op_parameter_.type_ = primitive->value_type(); + param->channelShared = value->channel_shared(); + return reinterpret_cast(param); } -Registry PReLUParameterRegistry(schema::PrimitiveType_PReLU, PopulatePReLUParameter); - +Registry PReLUParameterRegistry(schema::PrimitiveType_PReLUFusion, PopulatePReLUParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/pad_populate.cc b/mindspore/lite/src/ops/populate/pad_populate.cc index d9f19f20e2..58a3945711 100644 --- a/mindspore/lite/src/ops/populate/pad_populate.cc +++ b/mindspore/lite/src/ops/populate/pad_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,44 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/pad.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/pad_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulatePadParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulatePadParameter(const void *prim) { PadParameter *pad_param = reinterpret_cast(malloc(sizeof(PadParameter))); if (pad_param == nullptr) { MS_LOG(ERROR) << "malloc PadParameter failed."; return nullptr; } memset(pad_param, 0, sizeof(PadParameter)); - pad_param->op_parameter_.type_ = primitive->Type(); - auto pad_node = reinterpret_cast(const_cast(primitive)); - pad_param->pad_mode_ = pad_node->GetPaddingMode(); - pad_param->constant_value_ = pad_node->GetConstantValue(); - auto size = pad_node->GetPaddings().size(); - if (size > MAX_PAD_SIZE) { - MS_LOG(ERROR) << "Invalid padding size: " << size; - free(pad_param); - return nullptr; - } - - for (size_t i = 0; i < MAX_PAD_SIZE - size; ++i) { - pad_param->paddings_[i] = 0; - } - for (size_t i = 0; i < size; i++) { - pad_param->paddings_[MAX_PAD_SIZE - size + i] = pad_node->GetPaddings()[i]; - } - pad_param->padding_length = MAX_PAD_SIZE; - + auto primitive = static_cast(prim); + auto value = primitive->value_as_PadFusion(); + pad_param->op_parameter_.type_ = primitive->value_type(); + pad_param->pad_mode_ = value->padding_mode(); + pad_param->constant_value_ = value->constant_value(); return reinterpret_cast(pad_param); } -Registry PadParameterRegistry(schema::PrimitiveType_Pad, PopulatePadParameter); +Registry PadParameterRegistry(schema::PrimitiveType_PadFusion, PopulatePadParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/partial_populate.cc b/mindspore/lite/src/ops/populate/partial_populate.cc index 300f5e2827..f808a530af 100644 --- a/mindspore/lite/src/ops/populate/partial_populate.cc +++ b/mindspore/lite/src/ops/populate/partial_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/partial.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { @@ -25,20 +22,20 @@ typedef struct PartialParameter { int sub_graph_index_; } PartialParameter; -OpParameter *PopulatePartialParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulatePartialParameter(const void *prim) { PartialParameter *partial_parameter = reinterpret_cast(malloc(sizeof(PartialParameter))); if (partial_parameter == nullptr) { MS_LOG(ERROR) << "malloc partial parameter failed."; return nullptr; } memset(partial_parameter, 0, sizeof(PartialParameter)); - partial_parameter->op_parameter_.type_ = primitive->Type(); - - auto param = reinterpret_cast(const_cast(primitive)); - partial_parameter->sub_graph_index_ = param->GetSubGraphIndex(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_PartialFusion(); + partial_parameter->op_parameter_.type_ = primitive->value_type(); + partial_parameter->sub_graph_index_ = value->sub_graph_index(); return reinterpret_cast(partial_parameter); } -Registry PartialParameterRegistry(schema::PrimitiveType_Partial, PopulatePartialParameter); +Registry PartialParameterRegistry(schema::PrimitiveType_PartialFusion, PopulatePartialParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/pooling_populate.cc b/mindspore/lite/src/ops/populate/pooling_populate.cc index 4943ff9275..af368282bb 100644 --- a/mindspore/lite/src/ops/populate/pooling_populate.cc +++ b/mindspore/lite/src/ops/populate/pooling_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,65 +13,98 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/pooling.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/pooling_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulatePoolingParameter(const mindspore::lite::PrimitiveC *primitive) { - auto pooling_primitive = - reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateAvgPoolParameter(const void *primitive) { PoolingParameter *pooling_param = reinterpret_cast(malloc(sizeof(PoolingParameter))); if (pooling_param == nullptr) { MS_LOG(ERROR) << "malloc PoolingParameter failed."; return nullptr; } memset(pooling_param, 0, sizeof(PoolingParameter)); - pooling_param->op_parameter_.type_ = primitive->Type(); - pooling_param->global_ = pooling_primitive->GetGlobal(); - pooling_param->window_w_ = pooling_primitive->GetWindowW(); - pooling_param->window_h_ = pooling_primitive->GetWindowH(); - auto pooling_lite_primitive = (lite::Pooling *)primitive; - pooling_param->pad_u_ = pooling_lite_primitive->PadUp(); - pooling_param->pad_d_ = pooling_lite_primitive->PadDown(); - pooling_param->pad_l_ = pooling_lite_primitive->PadLeft(); - pooling_param->pad_r_ = pooling_lite_primitive->PadRight(); - pooling_param->stride_w_ = pooling_primitive->GetStrideW(); - pooling_param->stride_h_ = pooling_primitive->GetStrideH(); - pooling_param->avg_mode_ = pooling_primitive->GetAvgMode(); - auto pad_mode = pooling_primitive->GetPadMode(); - switch (pad_mode) { - case schema::PadMode_SAME_UPPER: - pooling_param->pad_mode_ = Pad_Same; + auto pooling_prim = static_cast(primitive); + pooling_param->op_parameter_.type_ = pooling_prim->value_type(); + auto pooling_primitive = pooling_prim->value_as_AvgPoolFusion(); + pooling_param->pool_mode_ = PoolMode_AvgPool; + pooling_param->global_ = pooling_primitive->global(); + pooling_param->stride_w_ = static_cast(*(pooling_primitive->strides()->begin() + 1)); + pooling_param->stride_h_ = static_cast(*(pooling_primitive->strides()->begin())); + if (pooling_primitive->pad() != nullptr) { + pooling_param->pad_u_ = static_cast(*(pooling_primitive->pad()->begin())); + pooling_param->pad_d_ = static_cast(*(pooling_primitive->pad()->begin() + 1)); + pooling_param->pad_l_ = static_cast(*(pooling_primitive->pad()->begin() + 2)); + pooling_param->pad_r_ = static_cast(*(pooling_primitive->pad()->begin() + 3)); + } + if (!pooling_param->global_) { + pooling_param->window_w_ = static_cast(*(pooling_primitive->kernel_size()->begin() + 1)); + pooling_param->window_h_ = static_cast(*(pooling_primitive->kernel_size()->begin())); + } + + auto round_mode = pooling_primitive->round_mode(); + switch (round_mode) { + case schema::RoundMode_FLOOR: + pooling_param->round_mode_ = RoundMode_Floor; break; - case schema::PadMode_VALID: - pooling_param->pad_mode_ = Pad_Valid; + case schema::RoundMode_CEIL: + pooling_param->round_mode_ = RoundMode_Ceil; break; default: - pooling_param->pad_mode_ = Pad_No; + pooling_param->round_mode_ = RoundMode_No; break; } - auto is_global = pooling_primitive->GetGlobal(); - pooling_param->global_ = is_global; - auto pool_mode = pooling_primitive->GetPoolingMode(); - switch (pool_mode) { - case schema::PoolMode_MAX_POOLING: - pooling_param->pool_mode_ = PoolMode_MaxPool; + if (pooling_primitive->activation_type() == schema::ActivationType_RELU) { + pooling_param->act_type_ = ActType_Relu; + } else if (pooling_primitive->activation_type() == schema::ActivationType_RELU6) { + pooling_param->act_type_ = ActType_Relu6; + } else { + pooling_param->act_type_ = ActType_No; + } + + switch (pooling_primitive->pad_mode()) { + case schema::PadMode_SAME: + pooling_param->pad_mode_ = Pad_same; break; - case schema::PoolMode_MEAN_POOLING: - pooling_param->pool_mode_ = PoolMode_AvgPool; + case schema::PadMode_VALID: + pooling_param->pad_mode_ = Pad_valid; break; default: - pooling_param->pool_mode_ = PoolMode_No; + pooling_param->pad_mode_ = Pad_pad; break; } + return reinterpret_cast(pooling_param); +} + +OpParameter *PopulateMaxPoolParameter(const void *primitive) { + PoolingParameter *pooling_param = reinterpret_cast(malloc(sizeof(PoolingParameter))); + if (pooling_param == nullptr) { + MS_LOG(ERROR) << "malloc PoolingParameter failed."; + return nullptr; + } + memset(pooling_param, 0, sizeof(PoolingParameter)); + auto pooling_prim = static_cast(primitive); + pooling_param->op_parameter_.type_ = pooling_prim->value_type(); + auto max_pool_prim = pooling_prim->value_as_MaxPoolFusion(); + pooling_param->pool_mode_ = PoolMode_MaxPool; + pooling_param->global_ = max_pool_prim->global(); + if (!pooling_param->global_) { + pooling_param->window_w_ = static_cast(*(max_pool_prim->kernel_size()->begin() + 1)); + pooling_param->window_h_ = static_cast(*(max_pool_prim->kernel_size()->begin())); + pooling_param->stride_w_ = static_cast(*(max_pool_prim->strides()->begin() + 1)); + pooling_param->stride_h_ = static_cast(*(max_pool_prim->strides()->begin())); + if (max_pool_prim->pad() != nullptr) { + pooling_param->pad_u_ = static_cast(*(max_pool_prim->pad()->begin())); + pooling_param->pad_d_ = static_cast(*(max_pool_prim->pad()->begin() + 1)); + pooling_param->pad_l_ = static_cast(*(max_pool_prim->pad()->begin() + 2)); + pooling_param->pad_r_ = static_cast(*(max_pool_prim->pad()->begin() + 3)); + } + } - auto round_mode = pooling_primitive->GetRoundMode(); + auto round_mode = max_pool_prim->round_mode(); switch (round_mode) { case schema::RoundMode_FLOOR: pooling_param->round_mode_ = RoundMode_Floor; @@ -84,17 +117,30 @@ OpParameter *PopulatePoolingParameter(const mindspore::lite::PrimitiveC *primiti break; } - if (pooling_primitive->GetActivationType() == schema::ActivationType_RELU) { + if (max_pool_prim->activation_type() == schema::ActivationType_RELU) { pooling_param->act_type_ = ActType_Relu; - } else if (pooling_primitive->GetActivationType() == schema::ActivationType_RELU6) { + } else if (max_pool_prim->activation_type() == schema::ActivationType_RELU6) { pooling_param->act_type_ = ActType_Relu6; } else { pooling_param->act_type_ = ActType_No; } + + switch (max_pool_prim->pad_mode()) { + case schema::PadMode_SAME: + pooling_param->pad_mode_ = Pad_same; + break; + case schema::PadMode_VALID: + pooling_param->pad_mode_ = Pad_valid; + break; + default: + pooling_param->pad_mode_ = Pad_pad; + break; + } return reinterpret_cast(pooling_param); } +} // namespace -Registry PoolingParameterRegistry(schema::PrimitiveType_Pooling, PopulatePoolingParameter); - +Registry g_avgPoolParameterRegistry(schema::PrimitiveType_AvgPoolFusion, PopulateAvgPoolParameter, SCHEMA_CUR); +Registry g_maxPoolParameterRegistry(schema::PrimitiveType_MaxPoolFusion, PopulateMaxPoolParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/populate_register.h b/mindspore/lite/src/ops/populate/populate_register.h index 9e80d30c41..3811edf741 100644 --- a/mindspore/lite/src/ops/populate/populate_register.h +++ b/mindspore/lite/src/ops/populate/populate_register.h @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,14 @@ #define LITE_MINDSPORE_LITE_C_OPS_OP_POPULATE_REGISTER_H #include -#include "src/ops/primitive_c.h" +#include "schema/model_generated.h" +#include "nnacl/op_base.h" +#include "src/common/common.h" +#include "src/common/prim_util.h" namespace mindspore { namespace lite { - +typedef OpParameter *(*ParameterGen)(const void *prim); class PopulateRegistry { public: static PopulateRegistry *GetInstance() { @@ -30,25 +33,30 @@ class PopulateRegistry { return ®istry; } - void InsertParameterMap(schema::PrimitiveType type, ParameterCreator creator) { parameter_creators[type] = creator; } + void InsertParameterMap(int type, ParameterGen creator, int version) { + parameters_[GenPrimVersionKey(type, version)] = creator; + } - ParameterCreator GetParameterCreator(schema::PrimitiveType type) { - if (parameter_creators.find(type) != parameter_creators.end()) { - return parameter_creators[type]; - } else { - MS_LOG(ERROR) << "Unsupported parameter type in Create : " << schema::EnumNamePrimitiveType(type); + ParameterGen GetParameterCreator(int type, int version) { + ParameterGen param_creator = nullptr; + auto iter = parameters_.find(GenPrimVersionKey(type, version)); + if (iter == parameters_.end()) { + MS_LOG(ERROR) << "Unsupported parameter type in Create : " << type; return nullptr; } + param_creator = iter->second; + return param_creator; } protected: - std::map parameter_creators; + // key:type * 1000 + schema_version + std::map parameters_; }; class Registry { public: - Registry(schema::PrimitiveType primitive_type, ParameterCreator creator) { - PopulateRegistry::GetInstance()->InsertParameterMap(primitive_type, creator); + Registry(int primitive_type, ParameterGen creator, int version) { + PopulateRegistry::GetInstance()->InsertParameterMap(primitive_type, creator, version); } ~Registry() = default; }; diff --git a/mindspore/lite/src/ops/populate/power_populate.cc b/mindspore/lite/src/ops/populate/power_populate.cc index a2e805c086..26b5bfdcc9 100644 --- a/mindspore/lite/src/ops/populate/power_populate.cc +++ b/mindspore/lite/src/ops/populate/power_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,31 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/power.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/power_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulatePowerParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulatePowerParameter(const void *prim) { PowerParameter *power_param = reinterpret_cast(malloc(sizeof(PowerParameter))); if (power_param == nullptr) { MS_LOG(ERROR) << "malloc PowerParameter failed."; return nullptr; } memset(power_param, 0, sizeof(PowerParameter)); - power_param->op_parameter_.type_ = primitive->Type(); - auto power = reinterpret_cast(const_cast(primitive)); - power_param->power_ = power->GetPower(); - power_param->scale_ = power->GetScale(); - power_param->shift_ = power->GetShift(); + auto primitive = static_cast(prim); + power_param->op_parameter_.type_ = primitive->value_type(); + auto power_prim = primitive->value_as_PowFusion(); + power_param->scale_ = power_prim->scale(); + power_param->shift_ = power_prim->shift(); return reinterpret_cast(power_param); } +} // namespace -Registry PowerParameterRegistry(schema::PrimitiveType_Power, PopulatePowerParameter); - +Registry g_powerParameterRegistry(schema::PrimitiveType_PowFusion, PopulatePowerParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/prior_box_populate.cc b/mindspore/lite/src/ops/populate/prior_box_populate.cc index b9c20f5bb4..611b594dcf 100644 --- a/mindspore/lite/src/ops/populate/prior_box_populate.cc +++ b/mindspore/lite/src/ops/populate/prior_box_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,71 +13,63 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/prior_box.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "mindspore/lite/nnacl/prior_box_parameter.h" +#include "nnacl/prior_box_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulatePriorBoxParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulatePriorBoxParameter(const void *prim) { PriorBoxParameter *prior_box_param = reinterpret_cast(malloc(sizeof(PriorBoxParameter))); if (prior_box_param == nullptr) { MS_LOG(ERROR) << "malloc PriorBoxParameter failed."; return nullptr; } memset(prior_box_param, 0, sizeof(PriorBoxParameter)); - prior_box_param->op_parameter_.type_ = primitive->Type(); - auto prior_box_attr = - reinterpret_cast(const_cast(primitive)); - if (prior_box_attr->GetMinSizes().size() > MAX_SHAPE_SIZE) { - MS_LOG(ERROR) << "PriorBox min_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " - << prior_box_attr->GetMinSizes(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_PriorBox(); + prior_box_param->op_parameter_.type_ = primitive->value_type(); + if (value->min_sizes()->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox min_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " << value->min_sizes(); free(prior_box_param); return nullptr; } - prior_box_param->min_sizes_size = prior_box_attr->GetMinSizes().size(); - if (prior_box_attr->GetMaxSizes().size() > MAX_SHAPE_SIZE) { - MS_LOG(ERROR) << "PriorBox max_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " - << prior_box_attr->GetMaxSizes(); + prior_box_param->min_sizes_size = value->min_sizes()->size(); + if (value->max_sizes()->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox max_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " << value->max_sizes(); free(prior_box_param); return nullptr; } - prior_box_param->max_sizes_size = prior_box_attr->GetMaxSizes().size(); - memcpy(prior_box_param->max_sizes, prior_box_attr->GetMaxSizes().data(), - prior_box_attr->GetMaxSizes().size() * sizeof(int32_t)); - memcpy(prior_box_param->min_sizes, prior_box_attr->GetMinSizes().data(), - prior_box_attr->GetMinSizes().size() * sizeof(int32_t)); + prior_box_param->max_sizes_size = value->max_sizes()->size(); + memcpy(prior_box_param->max_sizes, value->max_sizes()->data(), value->max_sizes()->size() * sizeof(int32_t)); + memcpy(prior_box_param->min_sizes, value->min_sizes()->data(), value->min_sizes()->size() * sizeof(int32_t)); - if (prior_box_attr->GetAspectRatios().size() > MAX_SHAPE_SIZE) { + if (value->aspect_ratios()->size() > MAX_SHAPE_SIZE) { MS_LOG(ERROR) << "PriorBox aspect_ratios size exceeds max num " << MAX_SHAPE_SIZE << ", got " - << prior_box_attr->GetAspectRatios(); + << value->aspect_ratios(); free(prior_box_param); return nullptr; } - prior_box_param->aspect_ratios_size = prior_box_attr->GetAspectRatios().size(); - memcpy(prior_box_param->aspect_ratios, prior_box_attr->GetAspectRatios().data(), - prior_box_attr->GetAspectRatios().size() * sizeof(float)); - if (prior_box_attr->GetVariances().size() != COMM_SHAPE_SIZE) { - MS_LOG(ERROR) << "PriorBox variances size should be " << COMM_SHAPE_SIZE << ", got " - << prior_box_attr->GetVariances().size(); + prior_box_param->aspect_ratios_size = value->aspect_ratios()->size(); + memcpy(prior_box_param->aspect_ratios, value->aspect_ratios()->data(), + value->aspect_ratios()->size() * sizeof(float)); + if (value->variances()->size() != COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox variances size should be " << COMM_SHAPE_SIZE << ", got " << value->variances()->size(); free(prior_box_param); return nullptr; } - memcpy(prior_box_param->variances, prior_box_attr->GetVariances().data(), COMM_SHAPE_SIZE * sizeof(float)); - prior_box_param->flip = prior_box_attr->GetFlip(); - prior_box_param->clip = prior_box_attr->GetClip(); - prior_box_param->offset = prior_box_attr->GetOffset(); - prior_box_param->image_size_h = prior_box_attr->GetImageSizeH(); - prior_box_param->image_size_w = prior_box_attr->GetImageSizeW(); - prior_box_param->step_h = prior_box_attr->GetStepH(); - prior_box_param->step_w = prior_box_attr->GetStepW(); + memcpy(prior_box_param->variances, value->variances()->data(), COMM_SHAPE_SIZE * sizeof(float)); + prior_box_param->flip = value->flip(); + prior_box_param->clip = value->clip(); + prior_box_param->offset = value->offset(); + prior_box_param->image_size_h = value->image_size_h(); + prior_box_param->image_size_w = value->image_size_w(); + prior_box_param->step_h = value->step_h(); + prior_box_param->step_w = value->step_w(); return reinterpret_cast(prior_box_param); } -Registry PriorBoxParameterRegistry(schema::PrimitiveType_PriorBox, PopulatePriorBoxParameter); +Registry PriorBoxParameterRegistry(schema::PrimitiveType_PriorBox, PopulatePriorBoxParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/quant_dtype_cast_populate.cc b/mindspore/lite/src/ops/populate/quant_dtype_cast_populate.cc index b91238ea39..d49640110a 100644 --- a/mindspore/lite/src/ops/populate/quant_dtype_cast_populate.cc +++ b/mindspore/lite/src/ops/populate/quant_dtype_cast_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/quant_dtype_cast.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/int8/quant_dtype_cast_int8.h" namespace mindspore { namespace lite { -OpParameter *PopulateQuantDTypeCastParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateQuantDTypeCastParameter(const void *prim) { QuantDTypeCastParameter *parameter = reinterpret_cast(malloc(sizeof(QuantDTypeCastParameter))); if (parameter == nullptr) { @@ -30,14 +27,15 @@ OpParameter *PopulateQuantDTypeCastParameter(const mindspore::lite::PrimitiveC * return nullptr; } memset(parameter, 0, sizeof(QuantDTypeCastParameter)); - parameter->op_parameter_.type_ = primitive->Type(); - auto quant_dtype_cast_param = - reinterpret_cast(const_cast(primitive)); - parameter->srcT = quant_dtype_cast_param->GetSrcT(); - parameter->dstT = quant_dtype_cast_param->GetDstT(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_QuantDTypeCast(); + parameter->op_parameter_.type_ = primitive->value_type(); + parameter->srcT = value->src_t(); + parameter->dstT = value->dst_t(); return reinterpret_cast(parameter); } -Registry QuantDTypeCastParameterRegistry(schema::PrimitiveType_QuantDTypeCast, PopulateQuantDTypeCastParameter); +Registry QuantDTypeCastParameterRegistry(schema::PrimitiveType_QuantDTypeCast, PopulateQuantDTypeCastParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/random_standard_normal_populate.cc b/mindspore/lite/src/ops/populate/random_standard_normal_populate.cc index 89fddd46d5..df22a0575d 100644 --- a/mindspore/lite/src/ops/populate/random_standard_normal_populate.cc +++ b/mindspore/lite/src/ops/populate/random_standard_normal_populate.cc @@ -14,14 +14,13 @@ * limitations under the License. */ -#include "src/ops/random_standard_normal.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/random_standard_normal_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateRandomStandardNormalParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateRandomStandardNormalParameter(const void *prim) { RandomStandardNormalParam *random_parameter = reinterpret_cast(malloc(sizeof(RandomStandardNormalParam))); if (random_parameter == nullptr) { @@ -29,14 +28,15 @@ OpParameter *PopulateRandomStandardNormalParameter(const mindspore::lite::Primit return nullptr; } memset(random_parameter, 0, sizeof(RandomStandardNormalParam)); - random_parameter->op_parameter_.type_ = primitive->Type(); - auto param = - reinterpret_cast(const_cast(primitive)); - random_parameter->seed_ = param->GetSeed(); - random_parameter->seed2_ = param->GetSeed2(); + auto *primitive = static_cast(prim); + random_parameter->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_RandomStandardNormal(); + random_parameter->seed_ = param->seed(); + random_parameter->seed2_ = param->seed2(); return reinterpret_cast(random_parameter); } -Registry RandomStandardNormalParameterRegistry(schema::PrimitiveType_RandomStandardNormal, - PopulateRandomStandardNormalParameter); +} // namespace +Registry g_randomStandardNormalParameterRegistry(schema::PrimitiveType_RandomStandardNormal, + PopulateRandomStandardNormalParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/range_populate.cc b/mindspore/lite/src/ops/populate/range_populate.cc index 71baee7067..2f30e10d92 100644 --- a/mindspore/lite/src/ops/populate/range_populate.cc +++ b/mindspore/lite/src/ops/populate/range_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,31 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/range.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/range_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateRangeParameter(const mindspore::lite::PrimitiveC *primitive) { - auto range_attr = reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateRangeParameter(const void *prim) { RangeParameter *range_param = reinterpret_cast(malloc(sizeof(RangeParameter))); if (range_param == nullptr) { MS_LOG(ERROR) << "malloc RangeParameter failed."; return nullptr; } memset(range_param, 0, sizeof(RangeParameter)); - range_param->op_parameter_.type_ = primitive->Type(); - range_param->start_ = range_attr->GetStart(); - range_param->limit_ = range_attr->GetLimit(); - range_param->delta_ = range_attr->GetDelta(); - range_param->dType_ = range_attr->GetDType(); + auto primitive = static_cast(prim); + range_param->op_parameter_.type_ = primitive->value_type(); + auto range_prim = primitive->value_as_Range(); + range_param->start_ = range_prim->start(); + range_param->limit_ = range_prim->limit(); + range_param->delta_ = range_prim->delta(); + range_param->dType_ = range_prim->d_type(); return reinterpret_cast(range_param); } -Registry RangeParameterRegistry(schema::PrimitiveType_Range, PopulateRangeParameter); +} // namespace +Registry g_rangeParameterRegistry(schema::PrimitiveType_Range, PopulateRangeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/rank_populate.cc b/mindspore/lite/src/ops/populate/rank_populate.cc new file mode 100644 index 0000000000..f9dd3373fa --- /dev/null +++ b/mindspore/lite/src/ops/populate/rank_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateRankParameter(const void *prim) { + OpParameter *rank_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (rank_param == nullptr) { + MS_LOG(ERROR) << "malloc RankParameter failed."; + return nullptr; + } + memset(rank_param, 0, sizeof(OpParameter)); + auto primitive = static_cast(prim); + rank_param->type_ = primitive->value_type(); + return reinterpret_cast(rank_param); +} +} // namespace + +Registry g_rankParameterRegistry(schema::PrimitiveType_Rank, PopulateRankParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/reduce_populate.cc b/mindspore/lite/src/ops/populate/reduce_populate.cc index b881fbd479..84c478a5c9 100644 --- a/mindspore/lite/src/ops/populate/reduce_populate.cc +++ b/mindspore/lite/src/ops/populate/reduce_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,44 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/reduce.h" #include -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/reduce_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateReduceParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateReduceParameter(const void *prim) { ReduceParameter *reduce_param = reinterpret_cast(malloc(sizeof(ReduceParameter))); if (reduce_param == nullptr) { MS_LOG(ERROR) << "malloc ReduceParameter failed."; return nullptr; } memset(reduce_param, 0, sizeof(ReduceParameter)); - reduce_param->op_parameter_.type_ = primitive->Type(); - auto reduce = reinterpret_cast(const_cast(primitive)); - reduce_param->keep_dims_ = reduce->GetKeepDims(); - reduce_param->reduce_to_end_ = reduce->GetReduceToEnd(); - reduce_param->coeff = reduce->GetCoeff(); - auto axisVector = reduce->GetAxes(); - if (axisVector.size() > MAX_SHAPE_SIZE) { - MS_LOG(ERROR) << "Reduce axes size " << axisVector.size() << " exceed limit " << MAX_SHAPE_SIZE; - free(reduce_param); - return nullptr; - } - reduce_param->num_axes_ = static_cast(axisVector.size()); - int i = 0; - for (auto iter = axisVector.begin(); iter != axisVector.end(); iter++) { - reduce_param->axes_[i++] = *iter; - } - reduce_param->mode_ = static_cast(reduce->GetMode()); + auto primitive = static_cast(prim); + auto value = primitive->value_as_ReduceFusion(); + reduce_param->op_parameter_.type_ = primitive->value_type(); + reduce_param->keep_dims_ = value->keep_dims(); + reduce_param->reduce_to_end_ = value->reduce_to_end(); + reduce_param->coeff = value->coeff(); + reduce_param->mode_ = static_cast(value->mode()); return reinterpret_cast(reduce_param); } -Registry ReduceParameterRegistry(schema::PrimitiveType_Reduce, PopulateReduceParameter); +Registry ReduceParameterRegistry(schema::PrimitiveType_ReduceFusion, PopulateReduceParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/reshape_populate.cc b/mindspore/lite/src/ops/populate/reshape_populate.cc index 556ba8d306..a4685326d1 100644 --- a/mindspore/lite/src/ops/populate/reshape_populate.cc +++ b/mindspore/lite/src/ops/populate/reshape_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,36 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" #include "nnacl/reshape_parameter.h" -#include "src/ops/reshape.h" namespace mindspore { namespace lite { - -OpParameter *PopulateReshapeParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateReshapeParameter(const void *prim) { ReshapeParameter *reshape_param = reinterpret_cast(malloc(sizeof(ReshapeParameter))); if (reshape_param == nullptr) { MS_LOG(ERROR) << "malloc ReshapeParameter failed."; return nullptr; } memset(reshape_param, 0, sizeof(ReshapeParameter)); - reshape_param->op_parameter_.type_ = primitive->Type(); - auto reshape_lite_primitive = (lite::Reshape *)primitive; - auto shape = reshape_lite_primitive->GetShape(); - reshape_param->shape_dim_ = shape.size(); - int i = 0; - for (auto iter = shape.begin(); iter != shape.end(); iter++) { - reshape_param->shape_[i++] = *iter; - } + auto *primitive = static_cast(prim); + reshape_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(reshape_param); } +} // namespace -Registry ReshapeParameterRegistry(schema::PrimitiveType_Reshape, PopulateReshapeParameter); - +Registry g_reshapeParameterRegistry(schema::PrimitiveType_Reshape, PopulateReshapeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/resize_populate.cc b/mindspore/lite/src/ops/populate/resize_populate.cc index af6be62d77..a67cacacab 100644 --- a/mindspore/lite/src/ops/populate/resize_populate.cc +++ b/mindspore/lite/src/ops/populate/resize_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,34 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/resize.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/resize_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateResizeParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateResizeParameter(const void *prim) { ResizeParameter *resize_param = reinterpret_cast(malloc(sizeof(ResizeParameter))); if (resize_param == nullptr) { MS_LOG(ERROR) << "malloc ResizeParameter failed."; return nullptr; } memset(resize_param, 0, sizeof(ResizeParameter)); - resize_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - resize_param->method_ = static_cast(param->GetMethod()); - resize_param->new_height_ = param->new_height(); - resize_param->new_width_ = param->new_width(); - resize_param->coordinate_transform_mode_ = param->GetCoordinateTransformMode(); - resize_param->preserve_aspect_ratio_ = param->GetPreserveAspectRatio(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_Resize(); + resize_param->op_parameter_.type_ = primitive->value_type(); + + resize_param->method_ = static_cast(value->method()); + resize_param->new_height_ = value->new_height(); + resize_param->new_width_ = value->new_width(); + resize_param->coordinate_transform_mode_ = value->coordinate_transform_mode(); + resize_param->preserve_aspect_ratio_ = value->preserve_aspect_ratio(); return reinterpret_cast(resize_param); } -Registry ResizeParameterRegistry(schema::PrimitiveType_Resize, PopulateResizeParameter); - +Registry ResizeParameterRegistry(schema::PrimitiveType_Resize, PopulateResizeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/reverse_populate.cc b/mindspore/lite/src/ops/populate/reverse_populate.cc index 08a4c989c6..04beec67fa 100644 --- a/mindspore/lite/src/ops/populate/reverse_populate.cc +++ b/mindspore/lite/src/ops/populate/reverse_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,35 +13,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/reverse.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/reverse_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateReverseParameter(const mindspore::lite::PrimitiveC *primitive) { - auto reverse_attr = - reinterpret_cast(const_cast(primitive)); +OpParameter *PopulateReverseParameter(const void *prim) { ReverseParameter *reverse_param = reinterpret_cast(malloc(sizeof(ReverseParameter))); if (reverse_param == nullptr) { MS_LOG(ERROR) << "malloc ReverseParameter failed."; return nullptr; } memset(reverse_param, 0, sizeof(ReverseParameter)); - reverse_param->op_parameter_.type_ = primitive->Type(); - auto flatAxis = reverse_attr->GetAxis(); - reverse_param->num_axis_ = flatAxis.size(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_ReverseV2(); + reverse_param->op_parameter_.type_ = primitive->value_type(); + + auto flatAxis = value->axis(); + reverse_param->num_axis_ = flatAxis->size(); int i = 0; - for (auto iter = flatAxis.begin(); iter != flatAxis.end(); iter++) { + for (auto iter = flatAxis->begin(); iter != flatAxis->end(); iter++) { reverse_param->axis_[i++] = *iter; } return reinterpret_cast(reverse_param); } -Registry ReverseParameterRegistry(schema::PrimitiveType_Reverse, PopulateReverseParameter); +Registry ReverseParameterRegistry(schema::PrimitiveType_ReverseV2, PopulateReverseParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc b/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc index d566a37a31..86eaa35c05 100644 --- a/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc +++ b/mindspore/lite/src/ops/populate/reverse_sequence_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/reverse_sequence.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "mindspore/lite/nnacl/fp32/reverse_sequence_fp32.h" +#include "nnacl/reverse_sequence_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateReverseSequenceParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateReverseSequenceParameter(const void *prim) { ReverseSequenceParameter *reverse_sequence_param = reinterpret_cast(malloc(sizeof(ReverseSequenceParameter))); if (reverse_sequence_param == nullptr) { @@ -30,14 +27,17 @@ OpParameter *PopulateReverseSequenceParameter(const mindspore::lite::PrimitiveC return nullptr; } memset(reverse_sequence_param, 0, sizeof(ReverseSequenceParameter)); - auto param = - reinterpret_cast(const_cast(primitive)); - reverse_sequence_param->op_parameter_.type_ = primitive->Type(); - reverse_sequence_param->seq_axis_ = param->GetSeqAxis(); - reverse_sequence_param->batch_axis_ = param->GetBatchAxis(); + auto primitive = static_cast(prim); + auto param = primitive->value_as_ReverseSequence(); + reverse_sequence_param->op_parameter_.type_ = primitive->value_type(); + reverse_sequence_param->seq_axis_ = static_cast(param->seq_dim()); + reverse_sequence_param->batch_axis_ = static_cast(param->batch_dim()); return reinterpret_cast(reverse_sequence_param); } -Registry ReverseSequenceParameterRegistry(schema::PrimitiveType_ReverseSequence, PopulateReverseSequenceParameter); +} // namespace + +Registry ReverseSequenceParameterRegistry(schema::PrimitiveType_ReverseSequence, PopulateReverseSequenceParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/roi_pooling_populate.cc b/mindspore/lite/src/ops/populate/roi_pooling_populate.cc index cd3aa3f085..5867a0701f 100644 --- a/mindspore/lite/src/ops/populate/roi_pooling_populate.cc +++ b/mindspore/lite/src/ops/populate/roi_pooling_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,32 +13,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/roi_pooling.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/roi_pooling_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateROIPoolingParameter(const mindspore::lite::PrimitiveC *primitive) { - const auto param = - reinterpret_cast(const_cast(primitive)); - ROIPoolingParameter *roi_pooling_param = reinterpret_cast(malloc(sizeof(ROIPoolingParameter))); - if (roi_pooling_param == nullptr) { +namespace { +OpParameter *PopulateROIPoolingParameter(const void *prim) { + ROIPoolingParameter *roi_param = reinterpret_cast(malloc(sizeof(ROIPoolingParameter))); + if (roi_param == nullptr) { MS_LOG(ERROR) << "malloc ROIPoolingParameter failed."; return nullptr; } - memset(roi_pooling_param, 0, sizeof(ROIPoolingParameter)); - roi_pooling_param->op_parameter_.type_ = primitive->Type(); - roi_pooling_param->pooledH_ = param->GetPooledH(); - roi_pooling_param->pooledW_ = param->GetPooledW(); - roi_pooling_param->scale_ = param->GetScale(); - return reinterpret_cast(roi_pooling_param); -} -Registry ROIPoolingParameterRegistry(schema::PrimitiveType_ROIPooling, PopulateROIPoolingParameter); + memset(roi_param, 0, sizeof(ROIPoolingParameter)); + auto primitive = static_cast(prim); + roi_param->op_parameter_.type_ = primitive->value_type(); + auto roi_prim = primitive->value_as_ROIPooling(); + roi_param->pooledH_ = roi_prim->pooled_h(); + roi_param->pooledW_ = roi_prim->pooled_w(); + roi_param->scale_ = roi_prim->scale(); + return reinterpret_cast(roi_param); +} +} // namespace +Registry g_ROIPoolingParameterRegistry(schema::PrimitiveType_ROIPooling, PopulateROIPoolingParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/scale_populate.cc b/mindspore/lite/src/ops/populate/scale_populate.cc index f71294cc55..923fbcbf8f 100644 --- a/mindspore/lite/src/ops/populate/scale_populate.cc +++ b/mindspore/lite/src/ops/populate/scale_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,33 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/scale.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/scale.h" namespace mindspore { namespace lite { - -OpParameter *PopulateScaleParameter(const mindspore::lite::PrimitiveC *primitive) { - if (primitive == nullptr) { - MS_LOG(ERROR) << "input primitive is nullptr"; - return nullptr; - } +namespace { +OpParameter *PopulateScaleParameter(const void *prim) { ScaleParameter *scale_param = reinterpret_cast(malloc(sizeof(ScaleParameter))); if (scale_param == nullptr) { MS_LOG(ERROR) << "malloc ScaleParameter failed."; return nullptr; } memset(scale_param, 0, sizeof(ScaleParameter)); - scale_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - scale_param->axis_ = param->GetAxis(); - scale_param->activation_type_ = param->GetActivationType(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_ScaleFusion(); + scale_param->op_parameter_.type_ = primitive->value_type(); + scale_param->axis_ = value->axis(); + scale_param->activation_type_ = value->activation_type(); return reinterpret_cast(scale_param); } -Registry ScaleParameterRegistry(schema::PrimitiveType_Scale, PopulateScaleParameter); +} // namespace +Registry g_scaleParameterRegistry(schema::PrimitiveType_ScaleFusion, PopulateScaleParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/scatter_nd_populate.cc b/mindspore/lite/src/ops/populate/scatter_nd_populate.cc index 46b3fb22ee..4a8dfa4b17 100644 --- a/mindspore/lite/src/ops/populate/scatter_nd_populate.cc +++ b/mindspore/lite/src/ops/populate/scatter_nd_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/scatter_nd.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateScatterNDParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateScatterNDParameter(const void *prim) { OpParameter *scatter_nd_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (scatter_nd_param == nullptr) { MS_LOG(ERROR) << "malloc ScatterNDParameter failed."; return nullptr; } memset(scatter_nd_param, 0, sizeof(OpParameter)); - scatter_nd_param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + scatter_nd_param->type_ = primitive->value_type(); return reinterpret_cast(scatter_nd_param); } -Registry ScatterNDParameterRegistry(schema::PrimitiveType_ScatterND, PopulateScatterNDParameter); +} // namespace +Registry g_scatterNDParameterRegistry(schema::PrimitiveType_ScatterNd, PopulateScatterNDParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/select_populate.cc b/mindspore/lite/src/ops/populate/select_populate.cc index efee92d035..2ed6cd9d2d 100644 --- a/mindspore/lite/src/ops/populate/select_populate.cc +++ b/mindspore/lite/src/ops/populate/select_populate.cc @@ -13,24 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/select.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" namespace mindspore { namespace lite { -OpParameter *PopulateSelectParameter(const mindspore::lite::PrimitiveC *primitive) { - OpParameter *select_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); - if (select_parameter == nullptr) { - MS_LOG(ERROR) << "malloc SelectParameter failed."; - return nullptr; - } - memset(select_parameter, 0, sizeof(OpParameter)); - select_parameter->type_ = primitive->Type(); - - return reinterpret_cast(select_parameter); -} -Registry SelectParameterRegistry(schema::PrimitiveType_Select, PopulateSelectParameter); +Registry g_selectParameterRegistry(schema::PrimitiveType_Select, DefaultPopulateParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/shape_populate.cc b/mindspore/lite/src/ops/populate/shape_populate.cc index d6f392b102..eab41e1144 100644 --- a/mindspore/lite/src/ops/populate/shape_populate.cc +++ b/mindspore/lite/src/ops/populate/shape_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "src/common/log_adapter.h" #include "src/tensor.h" namespace mindspore { namespace lite { -OpParameter *PopulateShapeParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateShapeParameter(const void *prim) { OpParameter *shape_param = reinterpret_cast(malloc(sizeof(OpParameter))); if (shape_param == nullptr) { MS_LOG(ERROR) << "malloc ShapeParameter failed."; return nullptr; } memset(shape_param, 0, sizeof(OpParameter)); - shape_param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + shape_param->type_ = primitive->value_type(); return reinterpret_cast(shape_param); } +} // namespace -Registry ShapeParameterRegistry(schema::PrimitiveType_Shape, PopulateShapeParameter); +Registry g_shapeParameterRegistry(schema::PrimitiveType_Shape, PopulateShapeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/size_populate.cc b/mindspore/lite/src/ops/populate/size_populate.cc new file mode 100644 index 0000000000..7c35d9e7c4 --- /dev/null +++ b/mindspore/lite/src/ops/populate/size_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_sizeParameterRegistry(schema::PrimitiveType_Size, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/skip_gram_populate.cc b/mindspore/lite/src/ops/populate/skip_gram_populate.cc index 4760edcdf4..bfe59511d9 100644 --- a/mindspore/lite/src/ops/populate/skip_gram_populate.cc +++ b/mindspore/lite/src/ops/populate/skip_gram_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/skip_gram.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "mindspore/lite/nnacl/skip_gram_parameter.h" +#include "nnacl/skip_gram_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateSkipGramParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateSkipGramParameter(const void *prim) { SkipGramParameter *skipGramParameter = reinterpret_cast(malloc(sizeof(SkipGramParameter))); if (skipGramParameter == nullptr) { MS_LOG(ERROR) << "malloc SkipGramParameter failed."; return nullptr; } memset(skipGramParameter, 0, sizeof(SkipGramParameter)); - skipGramParameter->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - skipGramParameter->ngram_size = param->GetNgramSize(); - skipGramParameter->max_skip_size = param->GetMaxSkipSize(); - skipGramParameter->include_all_ngrams = param->GetIncludeAllNgrams(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_SkipGram(); + skipGramParameter->op_parameter_.type_ = primitive->value_type(); + skipGramParameter->ngram_size = value->ngram_size(); + skipGramParameter->max_skip_size = value->max_skip_size(); + skipGramParameter->include_all_ngrams = value->include_all_grams(); return reinterpret_cast(skipGramParameter); } -Registry SkipGramParameterRegistry(schema::PrimitiveType_SkipGram, PopulateSkipGramParameter); +Registry SkipGramParameterRegistry(schema::PrimitiveType_SkipGram, PopulateSkipGramParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/slice_populate.cc b/mindspore/lite/src/ops/populate/slice_populate.cc index 0873836cbc..fa224d2cd4 100644 --- a/mindspore/lite/src/ops/populate/slice_populate.cc +++ b/mindspore/lite/src/ops/populate/slice_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,40 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/slice.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/slice_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateSliceParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateSliceParameter(const void *prim) { SliceParameter *slice_param = reinterpret_cast(malloc(sizeof(SliceParameter))); if (slice_param == nullptr) { MS_LOG(ERROR) << "malloc SliceParameter failed."; return nullptr; } memset(slice_param, 0, sizeof(SliceParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - slice_param->op_parameter_.type_ = primitive->Type(); - auto param_begin = param->GetPostProcessBegin(); - auto param_size = param->GetPostProcessSize(); - if (param_begin.size() != param_size.size()) { - free(slice_param); - return nullptr; - } - slice_param->param_length_ = static_cast(param_begin.size()); - for (int32_t i = 0; i < slice_param->param_length_; ++i) { - slice_param->begin_[i] = param_begin.at(i); - slice_param->size_[i] = param_size.at(i); + auto primitive = static_cast(prim); + auto value = primitive->value_as_SliceFusion(); + slice_param->op_parameter_.type_ = primitive->value_type(); + for (size_t i = 0; i < value->axes()->size(); ++i) { + slice_param->axis_[i] = value->axes()->Get(i); } return reinterpret_cast(slice_param); } -Registry SliceParameterRegistry(schema::PrimitiveType_Slice, PopulateSliceParameter); +Registry SliceParameterRegistry(schema::PrimitiveType_SliceFusion, PopulateSliceParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/softmax_populate.cc b/mindspore/lite/src/ops/populate/softmax_populate.cc index fa29b6eaaa..8e1aeaee93 100644 --- a/mindspore/lite/src/ops/populate/softmax_populate.cc +++ b/mindspore/lite/src/ops/populate/softmax_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/softmax.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/softmax_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateSoftmaxParameter(const mindspore::lite::PrimitiveC *primitive) { - auto softmax_primitive = - reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateSoftmaxParameter(const void *prim) { SoftmaxParameter *softmax_param = reinterpret_cast(malloc(sizeof(SoftmaxParameter))); if (softmax_param == nullptr) { MS_LOG(ERROR) << "malloc SoftmaxParameter failed."; return nullptr; } memset(softmax_param, 0, sizeof(SoftmaxParameter)); - softmax_param->op_parameter_.type_ = primitive->Type(); - softmax_param->axis_ = softmax_primitive->GetAxis(); + auto primitive = static_cast(prim); + softmax_param->op_parameter_.type_ = primitive->value_type(); + auto prim_softmax = primitive->value_as_Softmax(); + if (prim_softmax->axis()->size() != 1) { + MS_LOG(ERROR) << "axis number invalid!number: " << prim_softmax->axis()->size(); + return nullptr; + } + softmax_param->axis_ = prim_softmax->axis()->data()[0]; return reinterpret_cast(softmax_param); } +} // namespace -Registry SoftMaxParameterRegistry(schema::PrimitiveType_SoftMax, PopulateSoftmaxParameter); - +Registry g_softmaxParameterRegistry(schema::PrimitiveType_Softmax, PopulateSoftmaxParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/space_to_batch_nd_populate.cc b/mindspore/lite/src/ops/populate/space_to_batch_nd_populate.cc index 0682c9fe31..af71de033f 100644 --- a/mindspore/lite/src/ops/populate/space_to_batch_nd_populate.cc +++ b/mindspore/lite/src/ops/populate/space_to_batch_nd_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,47 +13,60 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/space_to_batch_nd.h" -#include "src/common/common.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/space_to_batch_fp32.h" namespace mindspore { namespace lite { -OpParameter *PopulateSpaceToBatchNDParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateSpaceToBatchNDParameter(const void *prim) { auto *space_batch_param_nd = reinterpret_cast(malloc(sizeof(SpaceToBatchParameter))); if (space_batch_param_nd == nullptr) { MS_LOG(ERROR) << "malloc SpaceToBatchParameter failed."; return nullptr; } - - space_batch_param_nd->op_parameter_.type_ = primitive->Type(); - auto block_sizes = ((mindspore::lite::SpaceToBatchND *)primitive)->GetBlockShape(); - if (block_sizes.empty()) { + memset(space_batch_param_nd, 0, sizeof(SpaceToBatchParameter)); + const schema::Primitive *primitive = static_cast(prim); + space_batch_param_nd->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_SpaceToBatchND(); + if (param->block_shape() == nullptr) { return reinterpret_cast(space_batch_param_nd); } - space_batch_param_nd->m_ = block_sizes.size(); - if (block_sizes.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of block_sizes.size() is too big"; + auto block_shapes = std::vector(param->block_shape()->begin(), param->block_shape()->end()); + if (block_shapes.size() > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of block_shapes.size() is too big"; free(space_batch_param_nd); return nullptr; } - memcpy(space_batch_param_nd->block_sizes_, (block_sizes.data()), block_sizes.size() * sizeof(int)); - auto paddings = ((mindspore::lite::SpaceToBatchND *)primitive)->GetPaddings(); - if (paddings.empty()) { - return reinterpret_cast(space_batch_param_nd); - } - if (paddings.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of paddings.size() is too big"; + space_batch_param_nd->m_ = block_shapes.size(); + + auto fb_paddings = param->paddings()->data(); + if (fb_paddings->size() == 0 || + static_cast(fb_paddings->size() * (*(fb_paddings->begin()))->data()->size()) > + std::numeric_limits::max() / sizeof(int64_t)) { + MS_LOG(ERROR) << "The value of paddings.size() is zero or too big"; free(space_batch_param_nd); return nullptr; } - memcpy(space_batch_param_nd->paddings_, (paddings.data()), paddings.size() * sizeof(int)); + std::vector paddings; + for (auto iter = fb_paddings->begin(); iter != fb_paddings->end(); ++iter) { + auto paddings_data = (*iter)->data(); + auto paddings_vec = std::vector(paddings_data->begin(), paddings_data->end()); + paddings.insert(paddings.end(), paddings_vec.begin(), paddings_vec.end()); + } + + for (size_t i = 0; i < block_shapes.size(); ++i) { + space_batch_param_nd->block_sizes_[i] = static_cast(block_shapes[i]); + } + + space_batch_param_nd->m_ = block_shapes.size(); + + for (size_t i = 0; i < paddings.size(); ++i) { + space_batch_param_nd->paddings_[i] = static_cast(paddings[i]); + } return reinterpret_cast(space_batch_param_nd); } -Registry SpaceToBatchNDParameterRegistry(schema::PrimitiveType_SpaceToBatchND, PopulateSpaceToBatchNDParameter); - +} // namespace +Registry g_spaceToBatchNDRegistry(schema::PrimitiveType_SpaceToBatchND, PopulateSpaceToBatchNDParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/space_to_batch_populate.cc b/mindspore/lite/src/ops/populate/space_to_batch_populate.cc index d4d803f3b9..75077c72bd 100644 --- a/mindspore/lite/src/ops/populate/space_to_batch_populate.cc +++ b/mindspore/lite/src/ops/populate/space_to_batch_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/space_to_batch.h" -#include "src/common/common.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/space_to_batch_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateSpaceToBatchParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateSpaceToBatchParameter(const void *prim) { SpaceToBatchParameter *space_batch_param = reinterpret_cast(malloc(sizeof(SpaceToBatchParameter))); if (space_batch_param == nullptr) { @@ -31,25 +27,42 @@ OpParameter *PopulateSpaceToBatchParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(space_batch_param, 0, sizeof(SpaceToBatchParameter)); - space_batch_param->op_parameter_.type_ = primitive->Type(); - auto block_sizes = ((mindspore::lite::SpaceToBatch *)primitive)->BlockSizes(); - space_batch_param->m_ = block_sizes.size(); + const schema::Primitive *primitive = static_cast(prim); + space_batch_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_SpaceToBatch(); + auto block_sizes = std::vector(param->block_size()->begin(), param->block_size()->end()); if (block_sizes.size() > std::numeric_limits::max() / sizeof(int)) { MS_LOG(ERROR) << "The value of block_sizes.size() is too big"; free(space_batch_param); return nullptr; } - memcpy(space_batch_param->block_sizes_, (block_sizes.data()), block_sizes.size() * sizeof(int)); - auto paddings = ((mindspore::lite::SpaceToBatch *)primitive)->Paddings(); - if (paddings.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of paddings.size() is too big"; + space_batch_param->m_ = block_sizes.size(); + + auto fb_paddings = param->paddings()->data(); + if (fb_paddings->size() == 0 || + static_cast(fb_paddings->size() * (*(fb_paddings->begin()))->data()->size()) > + std::numeric_limits::max() / sizeof(int64_t)) { + MS_LOG(ERROR) << "The value of paddings.size() is zero or too big"; free(space_batch_param); return nullptr; } - memcpy(space_batch_param->paddings_, (paddings.data()), paddings.size() * sizeof(int)); + std::vector paddings; + for (auto iter = fb_paddings->begin(); iter != fb_paddings->end(); ++iter) { + auto paddings_data = (*iter)->data(); + auto paddings_vec = std::vector(paddings_data->begin(), paddings_data->end()); + paddings.insert(paddings.end(), paddings_vec.begin(), paddings_vec.end()); + } + + for (size_t i = 0; i < block_sizes.size(); ++i) { + space_batch_param->block_sizes_[i] = static_cast(block_sizes[i]); + } + + for (size_t i = 0; i < paddings.size(); ++i) { + space_batch_param->paddings_[i] = static_cast(paddings[i]); + } return reinterpret_cast(space_batch_param); } -Registry SpaceToBatchParameterRegistry(schema::PrimitiveType_SpaceToBatch, PopulateSpaceToBatchParameter); - +} // namespace +Registry g_spaceToBatchRegistry(schema::PrimitiveType_SpaceToBatch, PopulateSpaceToBatchParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/space_to_depth_populate.cc b/mindspore/lite/src/ops/populate/space_to_depth_populate.cc index 2207470d3d..c78e23382d 100644 --- a/mindspore/lite/src/ops/populate/space_to_depth_populate.cc +++ b/mindspore/lite/src/ops/populate/space_to_depth_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/space_to_depth.h" -#include "src/common/common.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/space_to_depth_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateSpaceToDepthParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateSpaceToDepthParameter(const void *prim) { SpaceToDepthParameter *space_depth_param = reinterpret_cast(malloc(sizeof(SpaceToDepthParameter))); if (space_depth_param == nullptr) { @@ -30,17 +26,17 @@ OpParameter *PopulateSpaceToDepthParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(space_depth_param, 0, sizeof(SpaceToDepthParameter)); - space_depth_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - space_depth_param->op_parameter_.type_ = primitive->Type(); - space_depth_param->block_size_ = param->GetBlockSize(); - if (param->GetFormat() != schema::Format::Format_NHWC) { + auto primitive = static_cast(prim); + auto value = primitive->value_as_SpaceToDepth(); + space_depth_param->op_parameter_.type_ = primitive->value_type(); + space_depth_param->block_size_ = value->block_size(); + if (value->format() != schema::Format::Format_NHWC) { MS_LOG(ERROR) << "Currently only NHWC format is supported."; free(space_depth_param); return nullptr; } return reinterpret_cast(space_depth_param); } -Registry SpaceToDepthParameterRegistry(schema::PrimitiveType_SpaceToDepth, PopulateSpaceToDepthParameter); +Registry SpaceToDepthParameterRegistry(schema::PrimitiveType_SpaceToDepth, PopulateSpaceToDepthParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc b/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc index 85f759eee7..578824279d 100644 --- a/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc +++ b/mindspore/lite/src/ops/populate/sparse_to_dense_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,26 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/sparse_to_dense.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/sparse_to_dense_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateSparseToDenseParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateSparseToDenseParameter(const void *prim) { auto *sparse_to_dense_param = reinterpret_cast(malloc(sizeof(SparseToDenseParameter))); if (sparse_to_dense_param == nullptr) { MS_LOG(ERROR) << "malloc SparseToDenseParameter failed."; return nullptr; } memset(sparse_to_dense_param, 0, sizeof(SparseToDenseParameter)); - sparse_to_dense_param->op_parameter_.type_ = primitive->Type(); + auto primitive = static_cast(prim); + sparse_to_dense_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(sparse_to_dense_param); } +} // namespace -Registry SparseToDenseParameterRegistry(schema::PrimitiveType_SparseToDense, PopulateSparseToDenseParameter); +Registry g_sparseToDenseParameterRegistry(schema::PrimitiveType_SparseToDense, PopulateSparseToDenseParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/split_populate.cc b/mindspore/lite/src/ops/populate/split_populate.cc index 9fcd931506..74c32b024d 100644 --- a/mindspore/lite/src/ops/populate/split_populate.cc +++ b/mindspore/lite/src/ops/populate/split_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/split.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/split_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateSplitParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateSplitParameter(const void *prim) { auto *split_param = reinterpret_cast(malloc(sizeof(SplitParameter))); if (split_param == nullptr) { MS_LOG(ERROR) << "malloc SplitParameter failed."; return nullptr; } memset(split_param, 0, sizeof(SplitParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - split_param->op_parameter_.type_ = primitive->Type(); - split_param->num_split_ = param->num_split(); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_Split(); + split_param->op_parameter_.type_ = primitive->value_type(); + split_param->num_split_ = value->output_num(); if (split_param->num_split_ > std::numeric_limits::max() / static_cast(sizeof(int))) { MS_LOG(ERROR) << "The value of split_param->num_split_ is too big"; free(split_param); @@ -46,15 +45,20 @@ OpParameter *PopulateSplitParameter(const mindspore::lite::PrimitiveC *primitive return nullptr; } memset(split_param->split_sizes_, 0, split_param->num_split_ * sizeof(int)); - - auto split_sizes_vector_ = param->size_splits(); - for (size_t i = 0; i < split_sizes_vector_.size(); i++) { - split_param->split_sizes_[i] = split_sizes_vector_[i]; + auto split_sizes_vector_ = value->size_splits(); + if (split_sizes_vector_ != NULL) { + int i = 0; + for (auto iter : *split_sizes_vector_) { + split_param->split_sizes_[i++] = iter; + } + split_param->split_count_ = split_param->num_split_; + } else { + split_param->split_count_ = 0; } - - split_param->split_dim_ = param->GetSplitDim(); + split_param->split_dim_ = value->axis(); return reinterpret_cast(split_param); } -Registry SplitParameterRegistry(schema::PrimitiveType_Split, PopulateSplitParameter); +} // namespace +Registry g_splitParameterRegistry(schema::PrimitiveType_Split, PopulateSplitParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/squeeze_populate.cc b/mindspore/lite/src/ops/populate/squeeze_populate.cc index c589483e44..0b0e91bc69 100644 --- a/mindspore/lite/src/ops/populate/squeeze_populate.cc +++ b/mindspore/lite/src/ops/populate/squeeze_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,22 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" +#include "nnacl/squeeze_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateSqueezeParameter(const mindspore::lite::PrimitiveC *primitive) { - OpParameter *squeeze_param = reinterpret_cast(malloc(sizeof(OpParameter))); +namespace { +OpParameter *PopulateSqueezeParameter(const void *prim) { + SqueezeParameter *squeeze_param = reinterpret_cast(malloc(sizeof(SqueezeParameter))); if (squeeze_param == nullptr) { MS_LOG(ERROR) << "malloc SqueezeParameter failed."; return nullptr; } - memset(squeeze_param, 0, sizeof(OpParameter)); - squeeze_param->type_ = primitive->Type(); + memset(squeeze_param, 0, sizeof(SqueezeParameter)); + const schema::Primitive *primitive = static_cast(prim); + squeeze_param->op_parameter_.type_ = primitive->value_type(); + + auto squeeze_prim = primitive->value_as_Squeeze(); + if (squeeze_prim->axis() != nullptr) { + squeeze_param->axis_size_ = squeeze_prim->axis()->size(); + for (size_t i = 0; i < squeeze_param->axis_size_; i++) { + squeeze_param->axis_[i] = *(squeeze_prim->axis()->begin() + i); + } + } else { + squeeze_param->axis_size_ = 0; + } + return reinterpret_cast(squeeze_param); } -Registry SqueezeParameterRegistry(schema::PrimitiveType_Squeeze, PopulateSqueezeParameter); +} // namespace +Registry g_squeezeParameterRegistry(schema::PrimitiveType_Squeeze, PopulateSqueezeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/stack_populate.cc b/mindspore/lite/src/ops/populate/stack_populate.cc index 728b197688..7eede89858 100644 --- a/mindspore/lite/src/ops/populate/stack_populate.cc +++ b/mindspore/lite/src/ops/populate/stack_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/stack.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/stack_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateStackParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateStackParameter(const void *prim) { StackParameter *stack_param = reinterpret_cast(malloc(sizeof(StackParameter))); if (stack_param == nullptr) { MS_LOG(ERROR) << "malloc StackParameter failed."; return nullptr; } memset(stack_param, 0, sizeof(StackParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - stack_param->op_parameter_.type_ = primitive->Type(); - stack_param->axis_ = param->GetAxis(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_Stack(); + stack_param->op_parameter_.type_ = primitive->value_type(); + stack_param->axis_ = static_cast(value->axis()); return reinterpret_cast(stack_param); } -Registry StackParameterRegistry(schema::PrimitiveType_Stack, PopulateStackParameter); +} // namespace +Registry g_stackParameterRegistry(schema::PrimitiveType_Stack, PopulateStackParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/strided_slice_populate.cc b/mindspore/lite/src/ops/populate/strided_slice_populate.cc index fdf29bda75..894a6b121e 100644 --- a/mindspore/lite/src/ops/populate/strided_slice_populate.cc +++ b/mindspore/lite/src/ops/populate/strided_slice_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "src/ops/populate/strided_slice_populate.h" -#include -#include "src/ops/strided_slice.h" -#include "src/ops/primitive_c.h" -#include "src/ops/populate/populate_register.h" -#include "nnacl/strided_slice_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateStridedSliceParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateStridedSliceParameter(const void *prim) { StridedSliceParameter *strided_slice_param = reinterpret_cast(malloc(sizeof(StridedSliceParameter))); if (strided_slice_param == nullptr) { @@ -31,42 +25,20 @@ OpParameter *PopulateStridedSliceParameter(const mindspore::lite::PrimitiveC *pr return nullptr; } memset(strided_slice_param, 0, sizeof(StridedSliceParameter)); - strided_slice_param->op_parameter_.type_ = primitive->Type(); - auto n_dims = ((lite::StridedSlice *)primitive)->NDims(); - strided_slice_param->num_axes_ = n_dims; - auto begin = ((lite::StridedSlice *)primitive)->GetBegins(); - if (begin.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of begin.size() is too big"; - free(strided_slice_param); - return nullptr; - } - memcpy(strided_slice_param->begins_, (begin.data()), begin.size() * sizeof(int)); - auto end = ((lite::StridedSlice *)primitive)->GetEnds(); - if (end.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of end.size() is too big"; - free(strided_slice_param); - return nullptr; - } - memcpy(strided_slice_param->ends_, (end.data()), end.size() * sizeof(int)); - auto stride = ((lite::StridedSlice *)primitive)->GetStrides(); - if (stride.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of stride.size() is too big"; - free(strided_slice_param); - return nullptr; - } - memcpy(strided_slice_param->strides_, (stride.data()), stride.size() * sizeof(int)); - auto in_shape = ((lite::StridedSlice *)primitive)->GetInShape(); - if (in_shape.size() > std::numeric_limits::max() / sizeof(int)) { - MS_LOG(ERROR) << "The value of in_shape.size() is too big"; - free(strided_slice_param); - return nullptr; - } - memcpy(strided_slice_param->in_shape_, (in_shape.data()), in_shape.size() * sizeof(int)); - strided_slice_param->in_shape_length_ = static_cast(in_shape.size()); + + auto primitive = static_cast(prim); + auto value = primitive->value_as_StridedSlice(); + strided_slice_param->op_parameter_.type_ = primitive->value_type(); + + strided_slice_param->begins_mask_ = value->begin_mask(); + strided_slice_param->ends_mask_ = value->end_mask(); + strided_slice_param->ellipsisMask_ = value->ellipsis_mask(); + strided_slice_param->newAxisMask_ = value->new_axis_mask(); + strided_slice_param->shrinkAxisMask_ = value->shrink_axis_mask(); return reinterpret_cast(strided_slice_param); } -Registry StridedSliceParameterRegistry(schema::PrimitiveType_StridedSlice, PopulateStridedSliceParameter); +Registry StridedSliceParameterRegistry(schema::PrimitiveType_StridedSlice, PopulateStridedSliceParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/strided_slice_populate.h b/mindspore/lite/src/ops/populate/strided_slice_populate.h index d7efaae086..bebc3ad647 100644 --- a/mindspore/lite/src/ops/populate/strided_slice_populate.h +++ b/mindspore/lite/src/ops/populate/strided_slice_populate.h @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ #define MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ -#include "src/ops/arithmetic.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/strided_slice_parameter.h" namespace mindspore { namespace lite { - -OpParameter *PopulateStridedSliceParameter(const mindspore::lite::PrimitiveC *primitive); - +OpParameter *PopulateStridedSliceParameter(const void *prim); } // namespace lite } // namespace mindspore #endif // MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/sub_populate.cc b/mindspore/lite/src/ops/populate/sub_populate.cc index 5685851953..78c8d5186d 100644 --- a/mindspore/lite/src/ops/populate/sub_populate.cc +++ b/mindspore/lite/src/ops/populate/sub_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/sub.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/arithmetic.h" #include "src/ops/populate/arithmetic_populate.h" @@ -23,17 +20,18 @@ namespace mindspore { namespace lite { -OpParameter *PopulateSubParameter(const mindspore::lite::PrimitiveC *primitive) { - ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); +OpParameter *PopulateSubParameter(const void *prim) { + ArithmeticParameter *param = PopulateArithmeticCommonPara(prim); if (param == nullptr) { MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; return nullptr; } - param->activation_type_ = reinterpret_cast(primitive)->GetActivationType(); + auto primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(param); } -Registry SubParameterRegistry(schema::PrimitiveType_Sub, PopulateSubParameter); +Registry g_subParameterRegistry(schema::PrimitiveType_SubFusion, PopulateSubParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/switch_populate.cc b/mindspore/lite/src/ops/populate/switch_populate.cc index c895b9ae6c..b06e0de518 100644 --- a/mindspore/lite/src/ops/populate/switch_populate.cc +++ b/mindspore/lite/src/ops/populate/switch_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/switch.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateSwitchParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateSwitchParameter(const void *prim) { OpParameter *switch_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); if (switch_parameter == nullptr) { MS_LOG(ERROR) << "malloc SwitchParameter failed."; return nullptr; } memset(switch_parameter, 0, sizeof(OpParameter)); - switch_parameter->type_ = primitive->Type(); + auto primitive = static_cast(prim); + switch_parameter->type_ = primitive->value_type(); return reinterpret_cast(switch_parameter); } -Registry SwitchParameterRegistry(schema::PrimitiveType_Switch, PopulateSwitchParameter); +Registry SwitchParameterRegistry(schema::PrimitiveType_Switch, PopulateSwitchParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tensorlistfromtensor_populate.cc b/mindspore/lite/src/ops/populate/tensorlistfromtensor_populate.cc index 3c7f157d30..f80b76263b 100644 --- a/mindspore/lite/src/ops/populate/tensorlistfromtensor_populate.cc +++ b/mindspore/lite/src/ops/populate/tensorlistfromtensor_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,28 +15,26 @@ */ #include "nnacl/tensorlist_parameter.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "src/ops/tensorlist_fromtensor.h" namespace mindspore { namespace lite { -OpParameter *PopulateTensorListFromTensorParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTensorListFromTensorParameter(const void *prim) { TensorListParameter *TensorList_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); if (TensorList_param == nullptr) { MS_LOG(ERROR) << "malloc TensorListParameter failed."; return nullptr; } memset(TensorList_param, 0, sizeof(TensorListParameter)); - TensorList_param->op_parameter_.type_ = primitive->Type(); - auto tensorList = - reinterpret_cast(const_cast(primitive)); - TensorList_param->shape_type_ = (TypeId)(tensorList->GetShapeType()); - TensorList_param->element_dtype_ = (TypeId)(tensorList->GetElementDType()); + auto primitive = static_cast(prim); + auto value = primitive->value_as_TensorListFromTensor(); + TensorList_param->op_parameter_.type_ = primitive->value_type(); + TensorList_param->shape_type_ = value->shape_type(); + TensorList_param->element_dtype_ = value->element_dtype(); return reinterpret_cast(TensorList_param); } Registry TensorListFromTensorParameterRegistry(schema::PrimitiveType_TensorListFromTensor, - PopulateTensorListFromTensorParameter); + PopulateTensorListFromTensorParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tensorlistgetitem_populate.cc b/mindspore/lite/src/ops/populate/tensorlistgetitem_populate.cc index 18c8b3508a..40db0cde97 100644 --- a/mindspore/lite/src/ops/populate/tensorlistgetitem_populate.cc +++ b/mindspore/lite/src/ops/populate/tensorlistgetitem_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/tensorlist_getitem.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/tensorlist_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateTensorListGetItemParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTensorListGetItemParameter(const void *prim) { TensorListParameter *getItem_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); if (getItem_param == nullptr) { MS_LOG(ERROR) << "malloc TensorListParameter failed."; return nullptr; } memset(getItem_param, 0, sizeof(TensorListParameter)); - getItem_param->op_parameter_.type_ = primitive->Type(); - auto getItem = - reinterpret_cast(const_cast(primitive)); - getItem_param->element_dtype_ = getItem->GetElementDType(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_TensorListGetItem(); + getItem_param->op_parameter_.type_ = primitive->value_type(); + getItem_param->element_dtype_ = value->element_dtype(); return reinterpret_cast(getItem_param); } -Registry TensorListGetItemParameterRegistry(schema::PrimitiveType_TensorListGetItem, - PopulateTensorListGetItemParameter); +Registry TensorListGetItemParameterRegistry(schema::PrimitiveType_TensorListGetItem, PopulateTensorListGetItemParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tensorlistreserve_populate.cc b/mindspore/lite/src/ops/populate/tensorlistreserve_populate.cc index 76a007cd02..dadcb9f799 100644 --- a/mindspore/lite/src/ops/populate/tensorlistreserve_populate.cc +++ b/mindspore/lite/src/ops/populate/tensorlistreserve_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/tensorlist_reserve.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/tensorlist_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateTensorListReserveParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTensorListReserveParameter(const void *prim) { TensorListParameter *reserve_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); if (reserve_param == nullptr) { MS_LOG(ERROR) << "malloc TensorListParameter failed."; return nullptr; } memset(reserve_param, 0, sizeof(TensorListParameter)); - reserve_param->op_parameter_.type_ = primitive->Type(); - auto reserve = - reinterpret_cast(const_cast(primitive)); - reserve_param->element_dtype_ = reserve->GetElementDType(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_TensorListReserve(); + reserve_param->op_parameter_.type_ = primitive->value_type(); + reserve_param->element_dtype_ = value->element_dtype(); return reinterpret_cast(reserve_param); } -Registry TensorListReserveParameterRegistry(schema::PrimitiveType_TensorListReserve, - PopulateTensorListReserveParameter); +Registry TensorListReserveParameterRegistry(schema::PrimitiveType_TensorListReserve, PopulateTensorListReserveParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tensorlistsetlitem_populate.cc b/mindspore/lite/src/ops/populate/tensorlistsetlitem_populate.cc index 73b463e67b..36a0788b29 100644 --- a/mindspore/lite/src/ops/populate/tensorlistsetlitem_populate.cc +++ b/mindspore/lite/src/ops/populate/tensorlistsetlitem_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,28 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/tensorlist_setitem.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/tensorlist_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateTensorListSetItemParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTensorListSetItemParameter(const void *prim) { TensorListParameter *setItem_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); if (setItem_param == nullptr) { MS_LOG(ERROR) << "malloc TensorListParameter failed."; return nullptr; } memset(setItem_param, 0, sizeof(TensorListParameter)); - setItem_param->op_parameter_.type_ = primitive->Type(); - auto setItem = - reinterpret_cast(const_cast(primitive)); - setItem_param->element_dtype_ = setItem->GetElementDType(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_TensorListSetItem(); + setItem_param->op_parameter_.type_ = primitive->value_type(); + setItem_param->element_dtype_ = value->element_dtype(); return reinterpret_cast(setItem_param); } -Registry TensorListSetItemParameterRegistry(schema::PrimitiveType_TensorListSetItem, - PopulateTensorListSetItemParameter); +Registry TensorListSetItemParameterRegistry(schema::PrimitiveType_TensorListSetItem, PopulateTensorListSetItemParameter, + SCHEMA_CUR); + } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tensorliststack_populate.cc b/mindspore/lite/src/ops/populate/tensorliststack_populate.cc index a06638ca24..615c142a75 100644 --- a/mindspore/lite/src/ops/populate/tensorliststack_populate.cc +++ b/mindspore/lite/src/ops/populate/tensorliststack_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/tensorlist_stack.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/tensorlist_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateTensorListStackParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTensorListStackParameter(const void *prim) { TensorListParameter *stack_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); if (stack_param == nullptr) { MS_LOG(ERROR) << "malloc TensorListParameter failed."; return nullptr; } memset(stack_param, 0, sizeof(TensorListParameter)); - stack_param->op_parameter_.type_ = primitive->Type(); - auto stack = - reinterpret_cast(const_cast(primitive)); - stack_param->element_dtype_ = stack->GetElementDType(); - stack_param->num_element_ = stack->GetNumElements(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_TensorListStack(); + stack_param->op_parameter_.type_ = primitive->value_type(); + stack_param->element_dtype_ = value->element_dtype(); + stack_param->num_element_ = value->num_elements(); return reinterpret_cast(stack_param); } -Registry TensorListStackParameterRegistry(schema::PrimitiveType_TensorListStack, PopulateTensorListStackParameter); +Registry TensorListStackParameterRegistry(schema::PrimitiveType_TensorListStack, PopulateTensorListStackParameter, + SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/tile_populate.cc b/mindspore/lite/src/ops/populate/tile_populate.cc index 09721b5354..73eafa0b6a 100644 --- a/mindspore/lite/src/ops/populate/tile_populate.cc +++ b/mindspore/lite/src/ops/populate/tile_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,46 +13,33 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/tile.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/base/tile_base.h" namespace mindspore { namespace lite { -OpParameter *PopulateTileParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateTileParameter(const void *prim) { TileParameter *tile_param = reinterpret_cast(malloc(sizeof(TileParameter))); if (tile_param == nullptr) { MS_LOG(ERROR) << "malloc TileParameter failed."; return nullptr; } memset(tile_param, 0, sizeof(TileParameter)); - tile_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); -#ifdef SUPPORT_TRAIN - auto multiples = param->GetMultiples(); - tile_param->in_dim_ = multiples.size(); - for (int i = 0; i < tile_param->in_dim_; ++i) { - tile_param->multiples_[i] = multiples[i]; - } -#else - auto dims = param->GetDims(); - auto multiples = param->GetMultiples(); - for (size_t i = 0; i < kQuadrupleNum; ++i) { - tile_param->multiples_[i] = 1; - } - if (!dims.empty() && !multiples.empty()) { - for (size_t i = 0; i < dims.size(); ++i) { - tile_param->multiples_[dims[i]] = multiples[i]; + auto primitive = static_cast(prim); + auto value = primitive->value_as_TileFusion(); + tile_param->op_parameter_.type_ = primitive->value_type(); + auto dims = value->dims(); + if (dims != nullptr) { + for (size_t i = 0; i < dims->size(); ++i) { + tile_param->dims_[i] = static_cast(dims->Get(i)); } + tile_param->dims_size_ = dims->size(); } -#endif return reinterpret_cast(tile_param); } -Registry TileParameterRegistry(schema::PrimitiveType_Tile, PopulateTileParameter); +Registry TileParameterRegistry(schema::PrimitiveType_TileFusion, PopulateTileParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/topk_populate.cc b/mindspore/lite/src/ops/populate/topk_populate.cc index bafb35493e..17b03004c8 100644 --- a/mindspore/lite/src/ops/populate/topk_populate.cc +++ b/mindspore/lite/src/ops/populate/topk_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/topk.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/topk_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateTopKParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateTopKParameter(const void *prim) { TopkParameter *topk_param = reinterpret_cast(malloc(sizeof(TopkParameter))); if (topk_param == nullptr) { MS_LOG(ERROR) << "malloc TopkParameter failed."; return nullptr; } memset(topk_param, 0, sizeof(TopkParameter)); - topk_param->op_parameter_.type_ = primitive->Type(); - auto param = reinterpret_cast(const_cast(primitive)); - topk_param->k_ = param->GetK(); - topk_param->sorted_ = param->GetSorted(); + auto primitive = static_cast(prim); + topk_param->op_parameter_.type_ = primitive->value_type(); + auto param = primitive->value_as_TopKFusion(); + topk_param->sorted_ = param->sorted(); return reinterpret_cast(topk_param); } -Registry TopKParameterRegistry(schema::PrimitiveType_TopK, PopulateTopKParameter); +} // namespace +Registry g_topKParameterRegistry(schema::PrimitiveType_TopKFusion, PopulateTopKParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/transpose_populate.cc b/mindspore/lite/src/ops/populate/transpose_populate.cc index ecd2686b01..8c647dbddb 100644 --- a/mindspore/lite/src/ops/populate/transpose_populate.cc +++ b/mindspore/lite/src/ops/populate/transpose_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,36 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/transpose.h" -#include -#include "src/common/log_adapter.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/transpose.h" namespace mindspore { namespace lite { - -OpParameter *PopulateTransposeParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateTransposeParameter(const void *prim) { TransposeParameter *transpose_param = reinterpret_cast(malloc(sizeof(TransposeParameter))); if (transpose_param == nullptr) { MS_LOG(ERROR) << "malloc TransposeParameter failed."; return nullptr; } memset(transpose_param, 0, sizeof(TransposeParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - transpose_param->op_parameter_.type_ = primitive->Type(); - auto perm_vector_ = param->GetPerm(); - int i = 0; - for (auto iter = perm_vector_.begin(); iter != perm_vector_.end(); iter++) { - transpose_param->perm_[i++] = *iter; - } - transpose_param->num_axes_ = i; + auto primitive = static_cast(prim); + transpose_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(transpose_param); } +} // namespace -Registry TransposeParameterRegistry(schema::PrimitiveType_Transpose, PopulateTransposeParameter); +Registry g_transposeParameterRegistry(schema::PrimitiveType_Transpose, PopulateTransposeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/uniform_real_populate.cc b/mindspore/lite/src/ops/populate/uniform_real_populate.cc new file mode 100644 index 0000000000..f8edf8b0a7 --- /dev/null +++ b/mindspore/lite/src/ops/populate/uniform_real_populate.cc @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "src/ops/populate/default_populate.h" + +namespace mindspore { +namespace lite { +Registry g_uniformRealParameterRegistry(schema::PrimitiveType_UniformReal, DefaultPopulateParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/unique_populate.cc b/mindspore/lite/src/ops/populate/unique_populate.cc index 1ba3424ab8..abc028d76b 100644 --- a/mindspore/lite/src/ops/populate/unique_populate.cc +++ b/mindspore/lite/src/ops/populate/unique_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/unique.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/fp32/unique_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateUniqueParameter(const mindspore::lite::PrimitiveC *primitive) { +namespace { +OpParameter *PopulateUniqueParameter(const void *prim) { UniqueParameter *unique_param = reinterpret_cast(malloc(sizeof(UniqueParameter))); if (unique_param == nullptr) { MS_LOG(ERROR) << "malloc UniqueParameter failed."; return nullptr; } memset(unique_param, 0, sizeof(UniqueParameter)); - unique_param->op_parameter_.type_ = primitive->Type(); + auto primitive = static_cast(prim); + unique_param->op_parameter_.type_ = primitive->value_type(); return reinterpret_cast(unique_param); } +} // namespace -Registry UniqueParameterRegistry(schema::PrimitiveType_Unique, PopulateUniqueParameter); +Registry g_uniqueParameterRegistry(schema::PrimitiveType_Unique, PopulateUniqueParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc b/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc index 0d72aaf912..1066a3baab 100644 --- a/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc +++ b/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/unsorted_segment_sum.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { namespace lite { -OpParameter *PopulateUnsortedSegmentSumParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateUnsortedSegmentSumParameter(const void *prim) { OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); if (param == nullptr) { MS_LOG(ERROR) << "malloc UnsortedSegmentSum Parameter failed."; return nullptr; } memset(param, 0, sizeof(OpParameter)); - param->type_ = primitive->Type(); + auto primitive = static_cast(prim); + param->type_ = primitive->value_type(); return param; } Registry UnsortedSegmentSumParameterRegistry(schema::PrimitiveType_UnsortedSegmentSum, - PopulateUnsortedSegmentSumParameter); + PopulateUnsortedSegmentSumParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/unsqueeze_populate.cc b/mindspore/lite/src/ops/populate/unsqueeze_populate.cc index 6e1cdd387a..aefa55b564 100644 --- a/mindspore/lite/src/ops/populate/unsqueeze_populate.cc +++ b/mindspore/lite/src/ops/populate/unsqueeze_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,33 +13,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/unsqueeze.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "mindspore/lite/nnacl/unsqueeze_parameter.h" +#include "nnacl/unsqueeze_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateUnsqueezeParameter(const mindspore::lite::PrimitiveC *primitive) { - auto unsqueeze_attr = reinterpret_cast(const_cast(primitive)); +namespace { +OpParameter *PopulateUnsqueezeParameter(const void *prim) { UnSqueezeParameter *unsqueeze_param = reinterpret_cast(malloc(sizeof(UnSqueezeParameter))); if (unsqueeze_param == nullptr) { - MS_LOG(ERROR) << "malloc UnsqueezeParameter failed."; + MS_LOG(ERROR) << "malloc UnSqueezeParameter failed."; return nullptr; } memset(unsqueeze_param, 0, sizeof(UnSqueezeParameter)); - unsqueeze_param->op_parameter_.type_ = primitive->Type(); - auto flatAxis = unsqueeze_attr->GetAxis(); + auto primitive = static_cast(prim); + unsqueeze_param->op_parameter_.type_ = primitive->value_type(); + auto unsqueeze_prim = primitive->value_as_Unsqueeze(); + auto flat_axis = std::vector(unsqueeze_prim->axis()->begin(), unsqueeze_prim->axis()->end()); + unsqueeze_param->num_dim_ = flat_axis.size(); int i = 0; - for (auto iter = flatAxis.begin(); iter != flatAxis.end(); iter++) { + for (auto iter = flat_axis.begin(); iter != flat_axis.end(); ++iter) { unsqueeze_param->dims_[i++] = *iter; } return reinterpret_cast(unsqueeze_param); } -Registry UnsqueezeParameterRegistry(schema::PrimitiveType_Unsqueeze, PopulateUnsqueezeParameter); +} // namespace +Registry g_unsqueezeParameterRegistry(schema::PrimitiveType_Unsqueeze, PopulateUnsqueezeParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/unstack_populate.cc b/mindspore/lite/src/ops/populate/unstack_populate.cc index a6e5b6e749..ab2bc59295 100644 --- a/mindspore/lite/src/ops/populate/unstack_populate.cc +++ b/mindspore/lite/src/ops/populate/unstack_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,27 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/unstack.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" #include "nnacl/unstack_parameter.h" namespace mindspore { namespace lite { -OpParameter *PopulateUnstackParameter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateUnstackParameter(const void *prim) { UnstackParameter *unstack_param = reinterpret_cast(malloc(sizeof(UnstackParameter))); if (unstack_param == nullptr) { MS_LOG(ERROR) << "malloc UnstackParameter failed."; return nullptr; } memset(unstack_param, 0, sizeof(UnstackParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - unstack_param->op_parameter_.type_ = primitive->Type(); - unstack_param->axis_ = param->GetAxis(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_Unstack(); + unstack_param->op_parameter_.type_ = primitive->value_type(); + unstack_param->axis_ = value->axis(); return reinterpret_cast(unstack_param); } -Registry UnstackParameterRegistry(schema::PrimitiveType_Unstack, PopulateUnstackParameter); +Registry UnstackParameterRegistry(schema::PrimitiveType_Unstack, PopulateUnstackParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/upsample_populate.cc b/mindspore/lite/src/ops/populate/upsample_populate.cc deleted file mode 100644 index 617196552c..0000000000 --- a/mindspore/lite/src/ops/populate/upsample_populate.cc +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/upsample.h" -#include "src/ops/primitive_c.h" -#include "src/ops/populate/populate_register.h" -#include "nnacl/upsample_parameter.h" - -namespace mindspore { -namespace lite { - -OpParameter *PopulateUpsampleParameter(const mindspore::lite::PrimitiveC *primitive) { - UpsampleParameter *upsample_parameter = reinterpret_cast(malloc(sizeof(UpsampleParameter))); - if (upsample_parameter == nullptr) { - MS_LOG(ERROR) << "malloc Upsample Parameter failed."; - return nullptr; - } - memset(upsample_parameter, 0, sizeof(UpsampleParameter)); - auto param = reinterpret_cast(const_cast(primitive)); - upsample_parameter->op_parameter_.type_ = primitive->Type(); - auto method = param->GetMode(); - if (method == "linear") { - upsample_parameter->method_ = 0; - } else { - upsample_parameter->method_ = 1; - } - return reinterpret_cast(upsample_parameter); -} -Registry UpsampleParemeterRegistry(schema::PrimitiveType_Upsample, PopulateUpsampleParameter); -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/activation_grad_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/activation_grad_populate_v0.cc new file mode 100644 index 0000000000..0e06a24f48 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/activation_grad_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/activation_grad.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateActivationGradParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto activation_grad_prim = primitive->value_as_ActivationGrad(); + ActivationGradParameter *act_param = + reinterpret_cast(malloc(sizeof(ActivationGradParameter))); + if (act_param == nullptr) { + MS_LOG(ERROR) << "malloc ActivationParameter failed."; + return nullptr; + } + memset(act_param, 0, sizeof(ActivationGradParameter)); + act_param->op_parameter.type_ = schema::PrimitiveType_ActivationGrad; + + act_param->type_ = static_cast(activation_grad_prim->type()); + act_param->alpha_ = activation_grad_prim->alpha(); + return reinterpret_cast(act_param); +} +} // namespace + +Registry g_activationGradV0ParameterRegistry(schema::v0::PrimitiveType_ActivationGrad, PopulateActivationGradParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/activation_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/activation_populate_v0.cc new file mode 100644 index 0000000000..a08c3d9c0c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/activation_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/activation_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateActivationParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto activation_prim = primitive->value_as_Activation(); + ActivationParameter *act_param = reinterpret_cast(malloc(sizeof(ActivationParameter))); + if (act_param == nullptr) { + MS_LOG(ERROR) << "malloc ActivationParameter failed."; + return nullptr; + } + memset(act_param, 0, sizeof(ActivationParameter)); + act_param->op_parameter_.type_ = schema::PrimitiveType_Activation; + + act_param->type_ = static_cast(activation_prim->type()); + act_param->alpha_ = activation_prim->alpha(); + act_param->min_val_ = activation_prim->min_val(); + act_param->max_val_ = activation_prim->max_val(); + return reinterpret_cast(act_param); +} +} // namespace + +Registry g_activationV0ParameterRegistry(schema::v0::PrimitiveType_Activation, PopulateActivationParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/adam_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/adam_populate_v0.cc new file mode 100644 index 0000000000..ab70b40556 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/adam_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAdamParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc Adam Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_Adam; + return param; +} +} // namespace + +Registry g_adamV0ParameterRegistry(schema::v0::PrimitiveType_Adam, PopulateAdamParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/add_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/add_populate_v0.cc new file mode 100644 index 0000000000..9f80d6ca1d --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/add_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic.h" +#include "src/ops/populate/v0/arithmetic_populate_v0.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAddParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto add_prim = primitive->value_as_Add(); + ArithmeticParameter *param = PopulateArithmeticV0CommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + + param->op_parameter_.type_ = schema::PrimitiveType_AddFusion; + param->activation_type_ = add_prim->activationType(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_addV0ParameterRegistry(schema::v0::PrimitiveType_Add, PopulateAddParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/addn_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/addn_populate_v0.cc new file mode 100644 index 0000000000..3b678868f6 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/addn_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/op_base.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAddNParameter(const void *prim) { + OpParameter *addn_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (addn_param == nullptr) { + MS_LOG(ERROR) << "malloc OpParameter failed."; + return nullptr; + } + memset(addn_param, 0, sizeof(OpParameter)); + addn_param->type_ = schema::PrimitiveType_AddN; + return reinterpret_cast(addn_param); +} +} // namespace + +Registry g_addNV0ParameterRegistry(schema::v0::PrimitiveType_AddN, PopulateAddNParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/argmax_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/argmax_populate_v0.cc new file mode 100644 index 0000000000..0e1682d865 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/argmax_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arg_min_max_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateArgMaxParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto argmax_prim = primitive->value_as_ArgMax(); + ArgMinMaxParameter *arg_param = reinterpret_cast(malloc(sizeof(ArgMinMaxParameter))); + if (arg_param == nullptr) { + MS_LOG(ERROR) << "malloc ArgMinMaxParameter failed."; + return nullptr; + } + memset(arg_param, 0, sizeof(ArgMinMaxParameter)); + arg_param->op_parameter_.type_ = schema::PrimitiveType_ArgMaxFusion; + + arg_param->axis_ = argmax_prim->axis(); + arg_param->topk_ = argmax_prim->topK(); + arg_param->axis_type_ = argmax_prim->axisType(); + arg_param->out_value_ = argmax_prim->outMaxValue(); + arg_param->keep_dims_ = argmax_prim->keepDims(); + arg_param->get_max_ = true; + return reinterpret_cast(arg_param); +} +} // namespace + +Registry g_argMaxV0ParameterRegistry(schema::v0::PrimitiveType_ArgMax, PopulateArgMaxParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/argmin_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/argmin_populate_v0.cc new file mode 100644 index 0000000000..10d82fa59f --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/argmin_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arg_min_max_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateArgMinParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto argmin_prim = primitive->value_as_ArgMin(); + ArgMinMaxParameter *arg_param = reinterpret_cast(malloc(sizeof(ArgMinMaxParameter))); + if (arg_param == nullptr) { + MS_LOG(ERROR) << "malloc ArgMinMaxParameter failed."; + return nullptr; + } + memset(arg_param, 0, sizeof(ArgMinMaxParameter)); + arg_param->op_parameter_.type_ = schema::PrimitiveType_ArgMinFusion; + + arg_param->axis_ = argmin_prim->axis(); + arg_param->topk_ = argmin_prim->topK(); + arg_param->axis_type_ = argmin_prim->axisType(); + arg_param->out_value_ = argmin_prim->outMaxValue(); + arg_param->keep_dims_ = argmin_prim->keepDims(); + arg_param->get_max_ = false; + return reinterpret_cast(arg_param); +} +} // namespace + +Registry g_argMinV0ParameterRegistry(schema::v0::PrimitiveType_ArgMin, PopulateArgMinParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.cc new file mode 100644 index 0000000000..8454e57a75 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.cc @@ -0,0 +1,91 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/v0/arithmetic_populate_v0.h" +#include "src/common/log_adapter.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/common.h" + +namespace mindspore { +namespace lite { +ArithmeticParameter *PopulateArithmeticV0CommonPara(const void *prim) { + auto *param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(ArithmeticParameter)); + const auto *primitive = static_cast(prim); + param->op_parameter_.type_ = primitive->value_type(); + param->broadcasting_ = false; + param->ndim_ = 0; + param->activation_type_ = 0; + return param; +} + +OpParameter *PopulateArithmeticV0(const void *primitive) { + ArithmeticParameter *param = PopulateArithmeticV0CommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + int type = param->op_parameter_.type_; + if (type == schema::v0::PrimitiveType_RealDiv) { + param->op_parameter_.type_ = schema::PrimitiveType_RealDiv; + } else if (type == schema::v0::PrimitiveType_LogicalAnd) { + param->op_parameter_.type_ = schema::PrimitiveType_LogicalAnd; + } else if (type == schema::v0::PrimitiveType_LogicalOr) { + param->op_parameter_.type_ = schema::PrimitiveType_LogicalOr; + } else if (type == schema::v0::PrimitiveType_Equal) { + param->op_parameter_.type_ = schema::PrimitiveType_Equal; + } else if (type == schema::v0::PrimitiveType_NotEqual) { + param->op_parameter_.type_ = schema::PrimitiveType_NotEqual; + } else if (type == schema::v0::PrimitiveType_Less) { + param->op_parameter_.type_ = schema::PrimitiveType_Less; + } else if (type == schema::v0::PrimitiveType_LessEqual) { + param->op_parameter_.type_ = schema::PrimitiveType_LessEqual; + } else if (type == schema::v0::PrimitiveType_Greater) { + param->op_parameter_.type_ = schema::PrimitiveType_Greater; + } else if (type == schema::v0::PrimitiveType_GreaterEqual) { + param->op_parameter_.type_ = schema::PrimitiveType_GreaterEqual; + } else if (type == schema::v0::PrimitiveType_Maximum) { + param->op_parameter_.type_ = schema::PrimitiveType_Maximum; + } else if (type == schema::v0::PrimitiveType_Minimum) { + param->op_parameter_.type_ = schema::PrimitiveType_Minimum; + } else if (type == schema::v0::PrimitiveType_FloorDiv) { + param->op_parameter_.type_ = schema::PrimitiveType_FloorDiv; + } else if (type == schema::v0::PrimitiveType_FloorMod) { + param->op_parameter_.type_ = schema::PrimitiveType_FloorMod; + } + return reinterpret_cast(param); +} + +Registry g_realDivV0ParameterRegistry(schema::v0::PrimitiveType_RealDiv, PopulateArithmeticV0, SCHEMA_V0); +Registry g_logicalAndV0ParameterRegistry(schema::v0::PrimitiveType_LogicalAnd, PopulateArithmeticV0, SCHEMA_V0); +Registry g_logicalOrV0parameterRegistry(schema::v0::PrimitiveType_LogicalOr, PopulateArithmeticV0, SCHEMA_V0); +Registry g_equalV0ParameterRegistry(schema::v0::PrimitiveType_Equal, PopulateArithmeticV0, SCHEMA_V0); +Registry g_notEqualV0ParameterRegistry(schema::v0::PrimitiveType_NotEqual, PopulateArithmeticV0, SCHEMA_V0); +Registry g_lessV0ParameterRegistry(schema::v0::PrimitiveType_Less, PopulateArithmeticV0, SCHEMA_V0); +Registry g_lessEqualV0ParameterRegistry(schema::v0::PrimitiveType_LessEqual, PopulateArithmeticV0, SCHEMA_V0); +Registry g_greaterV0ParameterRegistry(schema::v0::PrimitiveType_Greater, PopulateArithmeticV0, SCHEMA_V0); +Registry g_greaterEqualV0ParameterRegistry(schema::v0::PrimitiveType_GreaterEqual, PopulateArithmeticV0, SCHEMA_V0); +Registry g_maximumV0ParameterRegistry(schema::v0::PrimitiveType_Maximum, PopulateArithmeticV0, SCHEMA_V0); +Registry g_minimumV0ParameterRegistry(schema::v0::PrimitiveType_Minimum, PopulateArithmeticV0, SCHEMA_V0); +Registry g_floorDivV0ParameterRegistry(schema::v0::PrimitiveType_FloorDiv, PopulateArithmeticV0, SCHEMA_V0); +Registry g_floorModV0ParameterRegistry(schema::v0::PrimitiveType_FloorMod, PopulateArithmeticV0, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.h b/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.h new file mode 100644 index 0000000000..c2612e6c20 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/arithmetic_populate_v0.h @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_V0_ARITHMETIC_POPULATE_H_ +#define MINDSPORE_LITE_SRC_OPS_POPULATE_V0_ARITHMETIC_POPULATE_H_ + +#include "nnacl/arithmetic.h" + +namespace mindspore { +namespace lite { +ArithmeticParameter *PopulateArithmeticV0CommonPara(const void *primitive); +OpParameter *PopulateArithmeticV0(const void *primitive); + +} // namespace lite +} // namespace mindspore +#endif // MINDSPORE_LITE_SRC_OPS_POPULATE_V0_ARITHMETIC_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/v0/arithmetic_self_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/arithmetic_self_populate_v0.cc new file mode 100644 index 0000000000..5e497faf61 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/arithmetic_self_populate_v0.cc @@ -0,0 +1,83 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/common/log_adapter.h" +#include "nnacl/arithmetic_self_parameter.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateArithmeticSelfV0(const void *prim) { + ArithmeticSelfParameter *arithmetic_self_param = + reinterpret_cast(malloc(sizeof(ArithmeticSelfParameter))); + if (arithmetic_self_param == nullptr) { + MS_LOG(ERROR) << "malloc ArithmeticSelfParameter failed."; + return nullptr; + } + memset(arithmetic_self_param, 0, sizeof(ArithmeticSelfParameter)); + auto primitive = static_cast(prim); + int type = primitive->value_type(); + if (type == schema::v0::PrimitiveType_Abs) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Abs; + } else if (type == schema::v0::PrimitiveType_Cos) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Cos; + } else if (type == schema::v0::PrimitiveType_Sin) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Sin; + } else if (type == schema::v0::PrimitiveType_Log) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Log; + } else if (type == schema::v0::PrimitiveType_Neg) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Neg; + } else if (type == schema::v0::PrimitiveType_NegGrad) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_NegGrad; + } else if (type == schema::v0::PrimitiveType_LogGrad) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_LogGrad; + } else if (type == schema::v0::PrimitiveType_Sqrt) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Sqrt; + } else if (type == schema::v0::PrimitiveType_Square) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Square; + } else if (type == schema::v0::PrimitiveType_Rsqrt) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Rsqrt; + } else if (type == schema::v0::PrimitiveType_LogicalNot) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_LogicalNot; + } else if (type == schema::v0::PrimitiveType_Floor) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Floor; + } else if (type == schema::v0::PrimitiveType_Ceil) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Ceil; + } else if (type == schema::v0::PrimitiveType_Round) { + arithmetic_self_param->op_parameter_.type_ = schema::PrimitiveType_Round; + } + return reinterpret_cast(arithmetic_self_param); +} +} // namespace + +Registry g_absV0ParameterRegistry(schema::v0::PrimitiveType_Abs, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_cosV0ParameterRegistry(schema::v0::PrimitiveType_Cos, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_sinV0ParameterRegistry(schema::v0::PrimitiveType_Sin, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_logV0ParameterRegistry(schema::v0::PrimitiveType_Log, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_negV0ParameterRegistry(schema::v0::PrimitiveType_Neg, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_negGradV0ParameterRegistry(schema::v0::PrimitiveType_NegGrad, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_logGradV0ParameterRegistry(schema::v0::PrimitiveType_LogGrad, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_sqrtV0ParameterRegistry(schema::v0::PrimitiveType_Sqrt, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_squareV0ParameterRegistry(schema::v0::PrimitiveType_Square, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_rsqrtV0ParameterRegistry(schema::v0::PrimitiveType_Rsqrt, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_logicalNotV0ParameterRegistry(schema::v0::PrimitiveType_LogicalNot, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_floorV0ParameterRegistry(schema::v0::PrimitiveType_Floor, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_ceilV0ParameterRegistry(schema::v0::PrimitiveType_Ceil, PopulateArithmeticSelfV0, SCHEMA_V0); +Registry g_roundV0ParameterRegistry(schema::v0::PrimitiveType_Round, PopulateArithmeticSelfV0, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/assert_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/assert_populate_v0.cc new file mode 100644 index 0000000000..d4ed74d179 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/assert_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAssertParameter(const void *prim) { + OpParameter *assert_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); + if (assert_parameter == nullptr) { + MS_LOG(ERROR) << "malloc AssertParameter failed."; + return nullptr; + } + memset(assert_parameter, 0, sizeof(OpParameter)); + assert_parameter->type_ = schema::PrimitiveType_Assert; + + return reinterpret_cast(assert_parameter); +} +} // namespace + +Registry g_assertV0ParameterRegistry(schema::v0::PrimitiveType_Assert, PopulateAssertParameter, SCHEMA_CUR); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/assign_add_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/assign_add_populate_v0.cc new file mode 100644 index 0000000000..b3ec9280f8 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/assign_add_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAssignAddParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc AssignAdd Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_AssignAdd; + return param; +} +} // namespace + +Registry g_assignAddV0ParameterRegistry(schema::v0::PrimitiveType_AssignAdd, PopulateAssignAddParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/assign_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/assign_populate_v0.cc new file mode 100644 index 0000000000..80539f0cd6 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/assign_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateAssignParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc Assign Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_Assign; + return param; +} +} // namespace + +Registry g_assignV0ParameterRegistry(schema::v0::PrimitiveType_Assign, PopulateAssignParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/batch_norm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/batch_norm_populate_v0.cc new file mode 100644 index 0000000000..71dee7f1ce --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/batch_norm_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/batchnorm_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBatchNormParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto batch_norm_prim = primitive->value_as_BatchNorm(); + + BatchNormParameter *batch_norm_param = reinterpret_cast(malloc(sizeof(BatchNormParameter))); + if (batch_norm_param == nullptr) { + MS_LOG(ERROR) << "malloc BatchNormParameter failed."; + return nullptr; + } + memset(batch_norm_param, 0, sizeof(BatchNormParameter)); + batch_norm_param->op_parameter_.type_ = schema::PrimitiveType_BatchNorm; + batch_norm_param->epsilon_ = batch_norm_prim->epsilon(); + batch_norm_param->fused_ = false; + return reinterpret_cast(batch_norm_param); +} +} // namespace + +Registry g_batchNormV0ParameterRegistry(schema::v0::PrimitiveType_BatchNorm, PopulateBatchNormParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/batch_to_space_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/batch_to_space_populate_v0.cc new file mode 100644 index 0000000000..a8ff816a90 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/batch_to_space_populate_v0.cc @@ -0,0 +1,70 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/batch_to_space.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBatchToSpaceParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto batch_to_space_prim = primitive->value_as_BatchToSpace(); + BatchToSpaceParameter *batch_space_param = + reinterpret_cast(malloc(sizeof(BatchToSpaceParameter))); + if (batch_space_param == nullptr) { + MS_LOG(ERROR) << "malloc BatchToSpaceParameter failed."; + return nullptr; + } + memset(batch_space_param, 0, sizeof(BatchToSpaceParameter)); + if (primitive->value_type() == schema::v0::PrimitiveType_BatchToSpace) { + batch_space_param->op_parameter_.type_ = schema::PrimitiveType_BatchToSpace; + } else { + batch_space_param->op_parameter_.type_ = schema::PrimitiveType_BatchToSpaceND; + } + + auto block_shape = batch_to_space_prim->blockShape(); + if (block_shape->size() != BATCH_TO_SPACE_BLOCK_SHAPE_SIZE) { + MS_LOG(ERROR) << "batch_to_space blockShape size should be " << BATCH_TO_SPACE_BLOCK_SHAPE_SIZE; + free(batch_space_param); + return nullptr; + } + + auto crops = batch_to_space_prim->crops(); + if (crops->size() != COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "batch_to_space crops size should be " << COMM_SHAPE_SIZE; + free(batch_space_param); + return nullptr; + } + + for (int i = 0; i < BATCH_TO_SPACE_BLOCK_SHAPE_SIZE; ++i) { + batch_space_param->block_shape_[i] = *(block_shape->begin() + i); + } + + for (int i = 0; i < COMM_SHAPE_SIZE; ++i) { + batch_space_param->crops_[i] = *(crops->begin() + i); + } + return reinterpret_cast(batch_space_param); +} +} // namespace + +Registry g_batchToSpaceV0ParameterRegistry(schema::v0::PrimitiveType_BatchToSpace, PopulateBatchToSpaceParameter, + SCHEMA_V0); +Registry g_batchToSpaceNDV0ParameterRegistry(schema::v0::PrimitiveType_BatchToSpaceND, PopulateBatchToSpaceParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/bias_add_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/bias_add_populate_v0.cc new file mode 100644 index 0000000000..be86128c38 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/bias_add_populate_v0.cc @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBiasAddParameter(const void *prim) { + ArithmeticParameter *arithmetic_param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); + if (arithmetic_param == nullptr) { + MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; + return nullptr; + } + memset(arithmetic_param, 0, sizeof(ArithmeticParameter)); + arithmetic_param->op_parameter_.type_ = schema::PrimitiveType_BiasAdd; + + return reinterpret_cast(arithmetic_param); +} +} // namespace + +Registry g_biasAddV0ParameterRegistry(schema::v0::PrimitiveType_BiasAdd, PopulateBiasAddParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/bias_grad_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/bias_grad_populate_v0.cc new file mode 100644 index 0000000000..d90d9dd4cb --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/bias_grad_populate_v0.cc @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBiasGradParameter(const void *prim) { + ArithmeticParameter *arithmetic_param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); + if (arithmetic_param == nullptr) { + MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; + return nullptr; + } + memset(arithmetic_param, 0, sizeof(ArithmeticParameter)); + arithmetic_param->op_parameter_.type_ = schema::PrimitiveType_BiasAddGrad; + + return reinterpret_cast(arithmetic_param); +} +} // namespace + +Registry g_biasGradV0ParameterParameterRegistry(schema::v0::PrimitiveType_BiasGrad, PopulateBiasGradParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_grad_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_grad_populate_v0.cc new file mode 100644 index 0000000000..9dadf89add --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_grad_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/binary_cross_entropy_grad.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBinaryCrossEntropyGradParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto binary_cross_entropy_grad_prim = primitive->value_as_BinaryCrossEntropyGrad(); + BinaryCrossEntropyGradParameter *bce_param = + reinterpret_cast(malloc(sizeof(BinaryCrossEntropyGradParameter))); + if (bce_param == nullptr) { + MS_LOG(ERROR) << "malloc BinaryCrossEntropyGrad Parameter failed."; + return nullptr; + } + memset(bce_param, 0, sizeof(BinaryCrossEntropyGradParameter)); + bce_param->op_parameter_.type_ = schema::PrimitiveType_BinaryCrossEntropyGrad; + + bce_param->reduction = binary_cross_entropy_grad_prim->reduction(); + return reinterpret_cast(bce_param); +} +} // namespace + +Registry g_binaryCrossEntropyGradV0ParameterRegistry(schema::v0::PrimitiveType_BinaryCrossEntropyGrad, + PopulateBinaryCrossEntropyGradParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_populate_v0.cc new file mode 100644 index 0000000000..2f58d24cad --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/binary_cross_entropy_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/binary_cross_entropy.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBinaryCrossEntropyParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto binary_cross_entropy_prim = primitive->value_as_BinaryCrossEntropy(); + BinaryCrossEntropyParameter *bce_param = + reinterpret_cast(malloc(sizeof(BinaryCrossEntropyParameter))); + if (bce_param == nullptr) { + MS_LOG(ERROR) << "malloc BinaryCrossEntropy Parameter failed."; + return nullptr; + } + memset(bce_param, 0, sizeof(BinaryCrossEntropyParameter)); + bce_param->op_parameter_.type_ = schema::PrimitiveType_BinaryCrossEntropy; + + bce_param->reduction = binary_cross_entropy_prim->reduction(); + return reinterpret_cast(bce_param); +} +} // namespace + +Registry g_binaryCrossEntropyV0ParameterRegistry(schema::v0::PrimitiveType_BinaryCrossEntropy, + PopulateBinaryCrossEntropyParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/broadcast_to_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/broadcast_to_populate_v0.cc new file mode 100644 index 0000000000..2d9a37bad6 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/broadcast_to_populate_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/broadcast_to_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateBroadcastToParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto broadcast_to_prim = primitive->value_as_BroadcastTo(); + BroadcastToParameter *broadcast_param = + reinterpret_cast(malloc(sizeof(BroadcastToParameter))); + if (broadcast_param == nullptr) { + MS_LOG(ERROR) << "malloc BroadcastToParameter failed."; + return nullptr; + } + memset(broadcast_param, 0, sizeof(BroadcastToParameter)); + + broadcast_param->op_parameter_.type_ = schema::PrimitiveType_BroadcastTo; + auto dst_shape = broadcast_to_prim->dst_shape(); + broadcast_param->shape_size_ = dst_shape->size(); + for (size_t i = 0; i < broadcast_param->shape_size_; ++i) { + broadcast_param->shape_[i] = *(dst_shape->begin() + i); + } + return reinterpret_cast(broadcast_param); +} +} // namespace + +Registry g_broadcastToV0ParameterRegistry(schema::v0::PrimitiveType_BroadcastTo, PopulateBroadcastToParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/cast_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/cast_populate_v0.cc new file mode 100644 index 0000000000..8f269f8c47 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/cast_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/cast_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateCastParameter(const void *prim) { + CastParameter *cast_param = reinterpret_cast(malloc(sizeof(CastParameter))); + if (cast_param == nullptr) { + MS_LOG(ERROR) << "malloc CastParameter failed."; + return nullptr; + } + memset(cast_param, 0, sizeof(CastParameter)); + cast_param->op_parameter_.type_ = schema::PrimitiveType_Cast; + return reinterpret_cast(cast_param); +} +} // namespace + +Registry g_castV0ParameterRegistry(schema::v0::PrimitiveType_Cast, PopulateCastParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/clip_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/clip_populate_v0.cc new file mode 100644 index 0000000000..5dc3f4e123 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/clip_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateClipParameter(const void *prim) { + OpParameter *act_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (act_param == nullptr) { + MS_LOG(ERROR) << "malloc ClipParameter failed."; + return nullptr; + } + memset(act_param, 0, sizeof(OpParameter)); + act_param->type_ = schema::PrimitiveType_Clip; + return reinterpret_cast(act_param); +} +} // namespace + +Registry g_clipV0ParameterRegistry(schema::v0::PrimitiveType_Clip, PopulateClipParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/common_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/common_populate_v0.cc new file mode 100644 index 0000000000..c8792e143f --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/common_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateCommonParameter(const void *prim) { + auto *common_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); + if (common_parameter == nullptr) { + MS_LOG(ERROR) << "malloc OpParameter failed."; + return nullptr; + } + memset(common_parameter, 0, sizeof(OpParameter)); + auto type = reinterpret_cast(prim)->value_type(); + if (type == schema::v0::PrimitiveType_ZerosLike) { + common_parameter->type_ = schema::PrimitiveType_ZerosLike; + } else { + common_parameter->type_ = type; + } + return common_parameter; +} +} // namespace + +Registry g_zerosLikeV0ParameterRegistry(schema::v0::PrimitiveType_ZerosLike, PopulateCommonParameter, SCHEMA_V0); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc new file mode 100644 index 0000000000..76e362de84 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/concat_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/concat_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateConcatParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto concat_prim = primitive->value_as_Concat(); + ConcatParameter *concat_param = reinterpret_cast(malloc(sizeof(ConcatParameter))); + if (concat_param == nullptr) { + MS_LOG(ERROR) << "malloc ConcatParameter failed."; + return nullptr; + } + memset(concat_param, 0, sizeof(ConcatParameter)); + concat_param->op_parameter_.type_ = schema::PrimitiveType_Concat; + + concat_param->axis_ = concat_prim->axis(); + return reinterpret_cast(concat_param); +} +} // namespace + +Registry g_concatV0ParameterRegistry(schema::v0::PrimitiveType_Concat, PopulateConcatParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/constant_of_shape_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/constant_of_shape_populate_v0.cc new file mode 100644 index 0000000000..761def4190 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/constant_of_shape_populate_v0.cc @@ -0,0 +1,57 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/constant_of_shape.h" + +namespace mindspore::lite { +namespace { +OpParameter *PopulateConstantOfShapeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto constant_of_shape_prim = primitive->value_as_ConstantOfShape(); + + ConstantOfShapeParameter *param = + reinterpret_cast(malloc(sizeof(ConstantOfShapeParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc ConstantOfShapeParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(ConstantOfShapeParameter)); + param->op_parameter_.type_ = schema::PrimitiveType_ConstantOfShape; + auto value = constant_of_shape_prim->value(); + param->data_type_ = constant_of_shape_prim->dataType(); + if (value->size() == 0 || value->size() > 1) { + MS_LOG(ERROR) << "The value of constant of shape is empty or more than 1."; + } else { + switch (param->data_type_) { + case kNumberTypeFloat32: + param->value_.f32_value_ = constant_of_shape_prim->value()->data()[0]; + break; + case kNumberTypeInt32: + param->value_.int32_value_ = constant_of_shape_prim->value()->data()[0]; + break; + default: + MS_LOG(ERROR) << "The value of constant of shape is invalid"; + } + } + return reinterpret_cast(param); +} +} // namespace + +Registry g_constantOfShapeV0ParameterRegistry(schema::v0::PrimitiveType_ConstantOfShape, + PopulateConstantOfShapeParameter, SCHEMA_V0); +} // namespace mindspore::lite diff --git a/mindspore/lite/src/ops/populate/v0/conv2d_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/conv2d_populate_v0.cc new file mode 100644 index 0000000000..fa4292e79b --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/conv2d_populate_v0.cc @@ -0,0 +1,82 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/conv_parameter.h" +#include "nnacl/op_base.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateConvParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto conv2d_prim = primitive->value_as_Conv2D(); + ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); + if (conv_param == nullptr) { + MS_LOG(ERROR) << "malloc ConvParameter failed."; + return nullptr; + } + memset(conv_param, 0, sizeof(ConvParameter)); + conv_param->op_parameter_.type_ = schema::PrimitiveType_Conv2DFusion; + + conv_param->kernel_h_ = conv2d_prim->kernelH(); + conv_param->kernel_w_ = conv2d_prim->kernelW(); + conv_param->group_ = conv2d_prim->group(); + conv_param->stride_h_ = conv2d_prim->strideH(); + conv_param->stride_w_ = conv2d_prim->strideW(); + + conv_param->pad_u_ = conv2d_prim->padUp(); + conv_param->pad_d_ = conv2d_prim->padDown(); + conv_param->pad_l_ = conv2d_prim->padLeft(); + conv_param->pad_r_ = conv2d_prim->padRight(); + conv_param->dilation_h_ = conv2d_prim->dilateH(); + conv_param->dilation_w_ = conv2d_prim->dilateW(); + conv_param->input_channel_ = conv2d_prim->channelIn(); + conv_param->output_channel_ = conv2d_prim->channelOut(); + conv_param->group_ = conv2d_prim->group(); + auto pad_mode = conv2d_prim->padMode(); + + switch (pad_mode) { + case schema::v0::PadMode_SAME_UPPER: + conv_param->pad_mode_ = Pad_same; + break; + case schema::v0::PadMode_VALID: + conv_param->pad_mode_ = Pad_valid; + break; + default: + conv_param->pad_mode_ = Pad_pad; + break; + } + auto act_type = conv2d_prim->activationType(); + switch (act_type) { + case schema::v0::ActivationType_RELU: + conv_param->act_type_ = ActType_Relu; + break; + case schema::v0::ActivationType_RELU6: + conv_param->act_type_ = ActType_Relu6; + break; + default: + conv_param->act_type_ = ActType_No; + break; + } + return reinterpret_cast(conv_param); +} +} // namespace + +Registry g_conv2DV0ParameterRegistry(schema::v0::PrimitiveType_Conv2D, PopulateConvParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/crop_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/crop_populate_v0.cc new file mode 100644 index 0000000000..f597dfb745 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/crop_populate_v0.cc @@ -0,0 +1,51 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/crop_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateCropParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto crop_prim = primitive->value_as_Crop(); + + auto param_offset = crop_prim->offsets(); + if (param_offset->size() > COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "crop_param offset size(" << param_offset->size() << ") should <= " << COMM_SHAPE_SIZE; + return nullptr; + } + CropParameter *crop_param = reinterpret_cast(malloc(sizeof(CropParameter))); + if (crop_param == nullptr) { + MS_LOG(ERROR) << "malloc CropParameter failed."; + return nullptr; + } + memset(crop_param, 0, sizeof(CropParameter)); + crop_param->op_parameter_.type_ = schema::PrimitiveType_Crop; + crop_param->axis_ = crop_prim->axis(); + crop_param->offset_size_ = param_offset->size(); + for (size_t i = 0; i < param_offset->size(); ++i) { + crop_param->offset_[i] = *(param_offset->begin() + i); + } + return reinterpret_cast(crop_param); +} +} // namespace + +Registry g_cropV0ParameterRegistry(schema::v0::PrimitiveType_Crop, PopulateCropParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/custom_extract_features_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/custom_extract_features_populate_v0.cc new file mode 100644 index 0000000000..06c5ba9d45 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/custom_extract_features_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateExtractFeaturesParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "new OpParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + auto type = reinterpret_cast(prim)->value_type(); + if (type == schema::v0::PrimitiveType_CustomExtractFeatures) { + param->type_ = schema::PrimitiveType_CustomExtractFeatures; + } else { + param->type_ = type; + } + return param; +} +} // namespace + +Registry g_customExtractFeaturesV0ParameterRegistry(schema::v0::PrimitiveType_CustomExtractFeatures, + PopulateExtractFeaturesParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/custom_normalize_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/custom_normalize_populate_v0.cc new file mode 100644 index 0000000000..2ee2c36d1f --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/custom_normalize_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateCustomNormalizeParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "new OpParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + auto type = reinterpret_cast(prim)->value_type(); + if (type == schema::v0::PrimitiveType_CustomNormalize) { + param->type_ = schema::PrimitiveType_CustomNormalize; + } else { + param->type_ = type; + } + return param; +} +} // namespace + +Registry g_customNormalizeV0ParameterRegistry(schema::v0::PrimitiveType_CustomNormalize, + PopulateCustomNormalizeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/custom_predict_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/custom_predict_populate_v0.cc new file mode 100644 index 0000000000..990e8e5c1e --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/custom_predict_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/predict_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateCustomPredictParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto custom_predict_prim = primitive->value_as_CustomPredict(); + PredictParameter *param = reinterpret_cast(malloc(sizeof(PredictParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc param failed."; + return nullptr; + } + memset(param, 0, sizeof(PredictParameter)); + param->op_parameter_.type_ = schema::PrimitiveType_CustomPredict; + + param->output_num = custom_predict_prim->outputNum(); + param->weight_threshold = custom_predict_prim->weightThreshold(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_customPredictV0ParameterRegistry(schema::v0::PrimitiveType_CustomPredict, PopulateCustomPredictParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/deconv2d_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/deconv2d_populate_v0.cc new file mode 100644 index 0000000000..f25ee53454 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/deconv2d_populate_v0.cc @@ -0,0 +1,77 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/conv_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateDeconvParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto deconv2d_prim = primitive->value_as_DeConv2D(); + ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); + if (conv_param == nullptr) { + MS_LOG(ERROR) << "malloc ConvParameter failed."; + return nullptr; + } + memset(conv_param, 0, sizeof(ConvParameter)); + conv_param->op_parameter_.type_ = schema::PrimitiveType_Conv2dTransposeFusion; + conv_param->group_ = 1; + + conv_param->kernel_h_ = deconv2d_prim->kernelH(); + conv_param->kernel_w_ = deconv2d_prim->kernelW(); + conv_param->stride_h_ = deconv2d_prim->strideH(); + conv_param->stride_w_ = deconv2d_prim->strideW(); + + conv_param->pad_u_ = deconv2d_prim->padUp(); + conv_param->pad_d_ = deconv2d_prim->padDown(); + conv_param->pad_l_ = deconv2d_prim->padLeft(); + conv_param->pad_r_ = deconv2d_prim->padRight(); + conv_param->dilation_h_ = deconv2d_prim->dilateH(); + conv_param->dilation_w_ = deconv2d_prim->dilateW(); + auto act_type = deconv2d_prim->activationType(); + switch (act_type) { + case schema::v0::ActivationType_RELU: + conv_param->act_type_ = ActType_Relu; + break; + case schema::v0::ActivationType_RELU6: + conv_param->act_type_ = ActType_Relu6; + break; + default: + conv_param->act_type_ = ActType_No; + break; + } + auto pad_mode = deconv2d_prim->padMode(); + switch (pad_mode) { + case schema::v0::PadMode_SAME_UPPER: + conv_param->pad_mode_ = Pad_same; + break; + case schema::v0::PadMode_VALID: + conv_param->pad_mode_ = Pad_valid; + break; + default: + conv_param->pad_mode_ = Pad_pad; + break; + } + return reinterpret_cast(conv_param); +} +} // namespace + +Registry g_deConv2DV0ParameterRegistry(schema::v0::PrimitiveType_DeConv2D, PopulateDeconvParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/dedepthwise_conv2d_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/dedepthwise_conv2d_populate_v0.cc new file mode 100644 index 0000000000..5463f2520a --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/dedepthwise_conv2d_populate_v0.cc @@ -0,0 +1,82 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/conv_parameter.h" + +namespace mindspore { +namespace lite { +namespace { + +OpParameter *PopulateDeconvDwParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto dedepthwise_conv2d_prim = primitive->value_as_DeDepthwiseConv2D(); + ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); + if (conv_param == nullptr) { + MS_LOG(ERROR) << "malloc ConvParameter failed."; + return nullptr; + } + memset(conv_param, 0, sizeof(ConvParameter)); + conv_param->op_parameter_.type_ = schema::PrimitiveType_Conv2dTransposeFusion; + + conv_param->group_ = dedepthwise_conv2d_prim->channelIn(); + + conv_param->kernel_h_ = dedepthwise_conv2d_prim->kernelH(); + conv_param->kernel_w_ = dedepthwise_conv2d_prim->kernelW(); + conv_param->stride_h_ = dedepthwise_conv2d_prim->strideH(); + conv_param->stride_w_ = dedepthwise_conv2d_prim->strideW(); + + conv_param->pad_u_ = dedepthwise_conv2d_prim->padUp(); + conv_param->pad_d_ = dedepthwise_conv2d_prim->padDown(); + conv_param->pad_l_ = dedepthwise_conv2d_prim->padLeft(); + conv_param->pad_r_ = dedepthwise_conv2d_prim->padRight(); + conv_param->dilation_h_ = dedepthwise_conv2d_prim->dilateH(); + conv_param->dilation_w_ = dedepthwise_conv2d_prim->dilateW(); + auto act_type = dedepthwise_conv2d_prim->activationType(); + switch (act_type) { + case schema::v0::ActivationType_RELU: + conv_param->act_type_ = ActType_Relu; + break; + case schema::v0::ActivationType_RELU6: + conv_param->act_type_ = ActType_Relu6; + break; + default: + conv_param->act_type_ = ActType_No; + break; + } + + auto pad_mode = dedepthwise_conv2d_prim->padMode(); + switch (pad_mode) { + case schema::v0::PadMode_SAME_UPPER: + conv_param->pad_mode_ = Pad_same; + break; + case schema::v0::PadMode_VALID: + conv_param->pad_mode_ = Pad_valid; + break; + default: + conv_param->pad_mode_ = Pad_pad; + break; + } + conv_param->channel_multiplie_ = dedepthwise_conv2d_prim->channelMultiplier(); + return reinterpret_cast(conv_param); +} +} // namespace + +Registry g_deDepthwiseConv2DV0ParameterRegistry(schema::v0::PrimitiveType_DeDepthwiseConv2D, PopulateDeconvDwParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/depth_to_space_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/depth_to_space_populate_v0.cc new file mode 100644 index 0000000000..0f5e4975f2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/depth_to_space_populate_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/depth_to_space_parameter.h" + +namespace mindspore { +namespace lite { +namespace { + +OpParameter *PopulateDepthToSpaceParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto depth_to_space_prim = primitive->value_as_DepthToSpace(); + DepthToSpaceParameter *depth_space_param = + reinterpret_cast(malloc(sizeof(DepthToSpaceParameter))); + if (depth_space_param == nullptr) { + MS_LOG(ERROR) << "malloc DepthToSpaceParameter failed."; + return nullptr; + } + memset(depth_space_param, 0, sizeof(DepthToSpaceParameter)); + + depth_space_param->op_parameter_.type_ = schema::PrimitiveType_DepthToSpace; + depth_space_param->block_size_ = depth_to_space_prim->blockSize(); + return reinterpret_cast(depth_space_param); +} +} // namespace + +Registry g_depthToSpaceV0ParameterRegistry(schema::v0::PrimitiveType_DepthToSpace, PopulateDepthToSpaceParameter, + SCHEMA_V0); +} // namespace lite + +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/depthwise_conv2d_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/depthwise_conv2d_populate_v0.cc new file mode 100644 index 0000000000..0a42852a85 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/depthwise_conv2d_populate_v0.cc @@ -0,0 +1,83 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/conv_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateConvDwParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto depthwise_conv2d_prim = primitive->value_as_DepthwiseConv2D(); + ConvParameter *conv_param = reinterpret_cast(malloc(sizeof(ConvParameter))); + if (conv_param == nullptr) { + MS_LOG(ERROR) << "malloc ConvParameter failed."; + return nullptr; + } + memset(conv_param, 0, sizeof(ConvParameter)); + conv_param->op_parameter_.type_ = schema::PrimitiveType_Conv2DFusion; + + conv_param->group_ = depthwise_conv2d_prim->channelIn(); + + conv_param->kernel_h_ = depthwise_conv2d_prim->kernelH(); + conv_param->kernel_w_ = depthwise_conv2d_prim->kernelW(); + conv_param->stride_h_ = depthwise_conv2d_prim->strideH(); + conv_param->stride_w_ = depthwise_conv2d_prim->strideW(); + + conv_param->pad_u_ = depthwise_conv2d_prim->padUp(); + conv_param->pad_d_ = depthwise_conv2d_prim->padDown(); + conv_param->pad_l_ = depthwise_conv2d_prim->padLeft(); + conv_param->pad_r_ = depthwise_conv2d_prim->padRight(); + conv_param->input_channel_ = depthwise_conv2d_prim->channelIn(); + conv_param->output_channel_ = depthwise_conv2d_prim->channelIn(); + conv_param->dilation_h_ = depthwise_conv2d_prim->dilateH(); + conv_param->dilation_w_ = depthwise_conv2d_prim->dilateW(); + + auto pad_mode = depthwise_conv2d_prim->padMode(); + switch (pad_mode) { + case schema::v0::PadMode_SAME_UPPER: + conv_param->pad_mode_ = Pad_same; + break; + case schema::v0::PadMode_VALID: + conv_param->pad_mode_ = Pad_valid; + break; + default: + conv_param->pad_mode_ = Pad_pad; + break; + } + auto act_type = depthwise_conv2d_prim->activationType(); + switch (act_type) { + case schema::v0::ActivationType_RELU: + conv_param->act_type_ = ActType_Relu; + break; + case schema::v0::ActivationType_RELU6: + conv_param->act_type_ = ActType_Relu6; + break; + default: + conv_param->act_type_ = ActType_No; + break; + } + conv_param->channel_multiplie_ = depthwise_conv2d_prim->channelMultiplier(); + return reinterpret_cast(conv_param); +} +} // namespace + +Registry g_depthwiseConv2DV0ParameterRegistry(schema::v0::PrimitiveType_DepthwiseConv2D, PopulateConvDwParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/detection_post_process_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/detection_post_process_populate_v0.cc new file mode 100644 index 0000000000..499ff04900 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/detection_post_process_populate_v0.cc @@ -0,0 +1,55 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/detection_post_process_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateDetectionPostProcessParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto detection_post_process_prim = primitive->value_as_DetectionPostProcess(); + DetectionPostProcessParameter *detection_post_process_parameter = + reinterpret_cast(malloc(sizeof(DetectionPostProcessParameter))); + if (detection_post_process_parameter == nullptr) { + MS_LOG(ERROR) << "malloc EluParameter failed."; + return nullptr; + } + memset(detection_post_process_parameter, 0, sizeof(DetectionPostProcessParameter)); + detection_post_process_parameter->op_parameter_.type_ = schema::PrimitiveType_DetectionPostProcess; + + detection_post_process_parameter->h_scale_ = detection_post_process_prim->hScale(); + detection_post_process_parameter->w_scale_ = detection_post_process_prim->wScale(); + detection_post_process_parameter->x_scale_ = detection_post_process_prim->xScale(); + detection_post_process_parameter->y_scale_ = detection_post_process_prim->yScale(); + detection_post_process_parameter->nms_iou_threshold_ = + detection_post_process_prim->NmsIouThreshold(); // why is not lower start letter + detection_post_process_parameter->nms_score_threshold_ = detection_post_process_prim->NmsScoreThreshold(); + detection_post_process_parameter->max_detections_ = detection_post_process_prim->MaxDetections(); + detection_post_process_parameter->detections_per_class_ = detection_post_process_prim->DetectionsPerClass(); + detection_post_process_parameter->max_classes_per_detection_ = detection_post_process_prim->MaxClassesPerDetection(); + detection_post_process_parameter->num_classes_ = detection_post_process_prim->NumClasses(); + detection_post_process_parameter->use_regular_nms_ = detection_post_process_prim->UseRegularNms(); + return reinterpret_cast(detection_post_process_parameter); +} +} // namespace + +Registry g_detectionPostProcessV0ParameterRegistry(schema::v0::PrimitiveType_DetectionPostProcess, + PopulateDetectionPostProcessParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/div_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/div_populate_v0.cc new file mode 100644 index 0000000000..ead45f94c7 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/div_populate_v0.cc @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/ops/populate/arithmetic_populate.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateDivParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto div_prim = primitive->value_as_Div(); + ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + param->op_parameter_.type_ = schema::PrimitiveType_DivFusion; + param->activation_type_ = div_prim->activationType(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_divV0ParameterRegistry(schema::v0::PrimitiveType_Div, PopulateDivParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/eltwise_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/eltwise_populate_v0.cc new file mode 100644 index 0000000000..327c708347 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/eltwise_populate_v0.cc @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/ops/populate/v0/arithmetic_populate_v0.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateEltwiseParameter(const void *prim) { + auto *primitive = static_cast(prim); + ArithmeticParameter *param = PopulateArithmeticV0CommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticV0CommonPara failed."; + return nullptr; + } + param->eltwise_mode_ = primitive->value_as_Eltwise()->mode(); + param->op_parameter_.type_ = schema::PrimitiveType_Eltwise; + return reinterpret_cast(param); +} +} // namespace + +Registry g_eltwiseV0ParameterRegistry(schema::v0::PrimitiveType_Eltwise, PopulateEltwiseParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/elu_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/elu_populate_v0.cc new file mode 100644 index 0000000000..a65326a742 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/elu_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/elu_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateEluParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto elu_prim = primitive->value_as_Elu(); + EluParameter *elu_parameter = reinterpret_cast(malloc(sizeof(EluParameter))); + if (elu_parameter == nullptr) { + MS_LOG(ERROR) << "malloc EluParameter failed."; + return nullptr; + } + memset(elu_parameter, 0, sizeof(EluParameter)); + elu_parameter->op_parameter_.type_ = schema::PrimitiveType_Elu; + + elu_parameter->alpha_ = elu_prim->alpha(); + return reinterpret_cast(elu_parameter); +} +} // namespace + +Registry g_eluV0ParameterRegistry(schema::v0::PrimitiveType_Elu, PopulateEluParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/embedding_lookup_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/embedding_lookup_populate_v0.cc new file mode 100644 index 0000000000..8ab55d43f2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/embedding_lookup_populate_v0.cc @@ -0,0 +1,50 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/embedding_lookup_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateEmbeddingLookupParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto embedding_lookup_prim = primitive->value_as_EmbeddingLookup(); + EmbeddingLookupParameter *embedding_lookup_parameter = + reinterpret_cast(malloc(sizeof(EmbeddingLookupParameter))); + if (embedding_lookup_parameter == nullptr) { + MS_LOG(ERROR) << "malloc EmbeddingLookupParameter failed."; + return nullptr; + } + memset(embedding_lookup_parameter, 0, sizeof(EmbeddingLookupParameter)); + embedding_lookup_parameter->op_parameter_.type_ = schema::PrimitiveType_EmbeddingLookupFusion; + + embedding_lookup_parameter->max_norm_ = embedding_lookup_prim->maxNorm(); + if (embedding_lookup_parameter->max_norm_ < 0) { + MS_LOG(ERROR) << "Embedding lookup max norm should be positive number, got " + << embedding_lookup_parameter->max_norm_; + free(embedding_lookup_parameter); + return nullptr; + } + return reinterpret_cast(embedding_lookup_parameter); +} +} // namespace + +Registry g_embeddingLookupV0ParameterRegistry(schema::v0::PrimitiveType_EmbeddingLookup, + PopulateEmbeddingLookupParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/exp_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/exp_populate_v0.cc new file mode 100644 index 0000000000..165e0db3dc --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/exp_populate_v0.cc @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/exp_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateExpParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto exp_prim = primitive->value_as_Exp(); + ExpParameter *exp_parameter = reinterpret_cast(malloc(sizeof(ExpParameter))); + if (exp_parameter == nullptr) { + MS_LOG(ERROR) << "malloc ExpParameter failed."; + return nullptr; + } + memset(exp_parameter, 0, sizeof(ExpParameter)); + exp_parameter->op_parameter_.type_ = schema::PrimitiveType_ExpFusion; + + exp_parameter->base_ = exp_prim->base(); + exp_parameter->scale_ = exp_prim->scale(); + exp_parameter->shift_ = exp_prim->shift(); + if (exp_parameter->base_ != -1 && exp_parameter->base_ <= 0) { + MS_LOG(ERROR) << "Exp base must be strictly positive, got " << exp_parameter->base_; + free(exp_parameter); + return nullptr; + } + return reinterpret_cast(exp_parameter); +} +} // namespace + +Registry g_expV0ParameterRegistry(schema::v0::PrimitiveType_Exp, PopulateExpParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/expand_dims_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/expand_dims_populate_v0.cc new file mode 100644 index 0000000000..4ad78d4118 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/expand_dims_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateExpandDimsParameter(const void *prim) { + OpParameter *expand_dims_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (expand_dims_param == nullptr) { + MS_LOG(ERROR) << "malloc ExpandDimsParameter failed."; + return nullptr; + } + memset(expand_dims_param, 0, sizeof(OpParameter)); + expand_dims_param->type_ = schema::PrimitiveType_ExpandDims; + return reinterpret_cast(expand_dims_param); +} +} // namespace + +Registry g_expandDimsV0ParameterRegistry(schema::v0::PrimitiveType_ExpandDims, PopulateExpandDimsParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/fill_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/fill_populate_v0.cc new file mode 100644 index 0000000000..41f2992ce5 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/fill_populate_v0.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/populate_register.h" +#include "schema/model_v0_generated.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateFillParameter(const void *prim) { + OpParameter *fill_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (fill_param == nullptr) { + MS_LOG(ERROR) << "malloc FillParameter failed."; + return nullptr; + } + memset(fill_param, 0, sizeof(OpParameter)); + fill_param->type_ = schema::PrimitiveType_Fill; + return reinterpret_cast(fill_param); +} +} // namespace + +Registry g_fillV0ParameterRegistry(schema::v0::PrimitiveType_Fill, PopulateFillParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/flatten_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/flatten_populate_v0.cc new file mode 100644 index 0000000000..f3e425d14a --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/flatten_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateFlattenParameter(const void *prim) { + OpParameter *flatten_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (flatten_param == nullptr) { + MS_LOG(ERROR) << "malloc FlattenParameter failed."; + return nullptr; + } + memset(flatten_param, 0, sizeof(OpParameter)); + flatten_param->type_ = schema::PrimitiveType_Flatten; + return reinterpret_cast(flatten_param); +} +} // namespace + +Registry g_flattenV0ParameterRegistry(schema::v0::PrimitiveType_Flatten, PopulateFlattenParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/full_connection_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/full_connection_populate_v0.cc new file mode 100644 index 0000000000..4956967daf --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/full_connection_populate_v0.cc @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/matmul_parameter.h" + +namespace mindspore { +namespace lite { +namespace { + +OpParameter *PopulateFullconnectionParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto full_connection_prim = primitive->value_as_FullConnection(); + + MatMulParameter *matmul_param = reinterpret_cast(malloc(sizeof(MatMulParameter))); + if (matmul_param == nullptr) { + MS_LOG(ERROR) << "malloc MatMulParameter failed."; + return nullptr; + } + memset(matmul_param, 0, sizeof(MatMulParameter)); + matmul_param->op_parameter_.type_ = schema::PrimitiveType_FullConnection; + matmul_param->b_transpose_ = true; + matmul_param->a_transpose_ = false; + matmul_param->has_bias_ = full_connection_prim->hasBias(); + if (full_connection_prim->activationType() == schema::v0::ActivationType_RELU) { + matmul_param->act_type_ = ActType_Relu; + } else if (full_connection_prim->activationType() == schema::v0::ActivationType_RELU6) { + matmul_param->act_type_ = ActType_Relu6; + } else { + matmul_param->act_type_ = ActType_No; + } + + matmul_param->use_axis_ = full_connection_prim->useAxis(); + matmul_param->axis_ = full_connection_prim->axis(); + return reinterpret_cast(matmul_param); +} +} // namespace + +Registry g_fullConnectionV0ParameterRegistry(schema::v0::PrimitiveType_FullConnection, PopulateFullconnectionParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/fused_batchnorm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/fused_batchnorm_populate_v0.cc new file mode 100644 index 0000000000..f164e3c747 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/fused_batchnorm_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/batchnorm_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateFusedBatchNormParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto fused_batchnorm_prim = primitive->value_as_FusedBatchNorm(); + BatchNormParameter *batch_norm_param = reinterpret_cast(malloc(sizeof(BatchNormParameter))); + if (batch_norm_param == nullptr) { + MS_LOG(ERROR) << "malloc BatchNormParameter failed."; + return nullptr; + } + memset(batch_norm_param, 0, sizeof(BatchNormParameter)); + batch_norm_param->op_parameter_.type_ = schema::PrimitiveType_FusedBatchNorm; + + batch_norm_param->epsilon_ = fused_batchnorm_prim->epsilon(); + batch_norm_param->momentum_ = fused_batchnorm_prim->momentum(); + batch_norm_param->fused_ = true; + return reinterpret_cast(batch_norm_param); +} +} // namespace + +Registry g_fusedBatchNormV0ParameterRegistry(schema::v0::PrimitiveType_FusedBatchNorm, PopulateFusedBatchNormParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/gather_nd_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/gather_nd_populate_v0.cc new file mode 100644 index 0000000000..3be3eeede9 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/gather_nd_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/gatherNd_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateGatherNdParameter(const void *prim) { + GatherNdParameter *gather_nd_param = reinterpret_cast(malloc(sizeof(GatherNdParameter))); + if (gather_nd_param == nullptr) { + MS_LOG(ERROR) << "malloc GatherNdParameter failed."; + return nullptr; + } + memset(gather_nd_param, 0, sizeof(GatherNdParameter)); + gather_nd_param->op_parameter_.type_ = schema::PrimitiveType_GatherNd; + return reinterpret_cast(gather_nd_param); +} +} // namespace + +Registry g_gatherNdV0ParameterRegistry(schema::v0::PrimitiveType_GatherNd, PopulateGatherNdParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/gather_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/gather_populate_v0.cc new file mode 100644 index 0000000000..34ecda0034 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/gather_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/gather_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateGatherParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto gather_prim = primitive->value_as_Gather(); + + GatherParameter *gather_param = reinterpret_cast(malloc(sizeof(GatherParameter))); + if (gather_param == nullptr) { + MS_LOG(ERROR) << "malloc GatherParameter failed."; + return nullptr; + } + memset(gather_param, 0, sizeof(GatherParameter)); + gather_param->op_parameter_.type_ = schema::PrimitiveType_Gather; + if (gather_prim->axis() < 0) { + MS_LOG(ERROR) << "axis should be >= 0."; + free(gather_param); + return nullptr; + } + gather_param->axis_ = gather_prim->axis(); + return reinterpret_cast(gather_param); +} +} // namespace + +Registry g_gatherV0ParameterRegistry(schema::v0::PrimitiveType_Gather, PopulateGatherParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/hashtable_lookup_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/hashtable_lookup_populate_v0.cc new file mode 100644 index 0000000000..2d1b302985 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/hashtable_lookup_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateHashtableLookupParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "new OpParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_HashtableLookup; + return param; +} +} // namespace + +Registry g_hashtableLookupV0ParameterRegistry(schema::v0::PrimitiveType_HashtableLookup, + PopulateHashtableLookupParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/instance_norm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/instance_norm_populate_v0.cc new file mode 100644 index 0000000000..b2603c1a67 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/instance_norm_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/instance_norm_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateInstanceNormParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto instance_norm_prim = primitive->value_as_InstanceNorm(); + InstanceNormParameter *instance_norm_param = + reinterpret_cast(malloc(sizeof(InstanceNormParameter))); + if (instance_norm_param == nullptr) { + MS_LOG(ERROR) << "malloc InstanceNormParameter failed."; + return nullptr; + } + memset(instance_norm_param, 0, sizeof(InstanceNormParameter)); + instance_norm_param->op_parameter_.type_ = schema::PrimitiveType_LayerNormFusion; + instance_norm_param->epsilon_ = instance_norm_prim->epsilon(); + return reinterpret_cast(instance_norm_param); +} +} // namespace + +Registry g_instanceNormV0ParameterRegistry(schema::v0::PrimitiveType_InstanceNorm, PopulateInstanceNormParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/l2_norm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/l2_norm_populate_v0.cc new file mode 100644 index 0000000000..33ab4c9967 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/l2_norm_populate_v0.cc @@ -0,0 +1,65 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/l2_norm_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateL2NormParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto l2_norm_prim = primitive->value_as_L2Norm(); + L2NormParameter *l2_norm_parameter = reinterpret_cast(malloc(sizeof(L2NormParameter))); + if (l2_norm_parameter == nullptr) { + MS_LOG(ERROR) << "malloc L2NormParameter failed."; + return nullptr; + } + memset(l2_norm_parameter, 0, sizeof(L2NormParameter)); + l2_norm_parameter->op_parameter_.type_ = schema::PrimitiveType_L2NormalizeFusion; + + MS_ASSERT(l2_norm_prim != nullptr); + auto axis_vec = l2_norm_prim->axis(); + l2_norm_parameter->axis_num_ = axis_vec->size(); + if (((size_t)axis_vec->size()) > SIZE_MAX / sizeof(int)) { + MS_LOG(ERROR) << "axis_vec size too big"; + free(l2_norm_parameter); + return nullptr; + } + + for (size_t i = 0; i < axis_vec->size(); i++) { + l2_norm_parameter->axis_[i] = *(axis_vec->begin() + i); + } + if (l2_norm_prim->epsilon() < 1e-6) { + l2_norm_parameter->epsilon_ = 1e-6; + } else { + l2_norm_parameter->epsilon_ = l2_norm_prim->epsilon(); + } + if (l2_norm_prim->activationType() == static_cast(schema::v0::ActivationType_RELU)) { + l2_norm_parameter->act_type_ = ActType_Relu; + } else if (l2_norm_prim->activationType() == static_cast(schema::v0::ActivationType_RELU6)) { + l2_norm_parameter->act_type_ = ActType_Relu6; + } else { + l2_norm_parameter->act_type_ = ActType_No; + } + return reinterpret_cast(l2_norm_parameter); +} +} // namespace + +Registry g_l2NormV0ParameterRegistry(schema::v0::PrimitiveType_L2Norm, PopulateL2NormParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.cc new file mode 100644 index 0000000000..a0139f1ee2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.cc @@ -0,0 +1,54 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/v0/layer_norm_populate_v0.h" +#include "nnacl/layer_norm_parameter.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateLayerNormParameterV0(const void *prim) { + auto *primitive = static_cast(prim); + auto layer_norm_prim = primitive->value_as_LayerNorm(); + auto layer_norm_parameter = reinterpret_cast(malloc(sizeof(LayerNormParameter))); + if (layer_norm_parameter == nullptr) { + MS_LOG(ERROR) << "malloc LayerNormParameter failed."; + return nullptr; + } + memset(layer_norm_parameter, 0, sizeof(LayerNormParameter)); + layer_norm_parameter->op_parameter_.type_ = schema::PrimitiveType_LayerNormFusion; + auto normalized_shape = layer_norm_prim->normalizedShape(); + if (normalized_shape != nullptr) { + layer_norm_parameter->normalized_dims_ = normalized_shape->size(); + if (((size_t)normalized_shape->size()) > SIZE_MAX / sizeof(int)) { + MS_LOG(ERROR) << "normalized_shape size too big"; + free(layer_norm_parameter); + return nullptr; + } + for (size_t i = 0; i < normalized_shape->size(); i++) { + layer_norm_parameter->normalized_shape_[i] = *(normalized_shape->begin() + i); + } + } + layer_norm_parameter->epsilon_ = layer_norm_prim->epsilon(); + layer_norm_parameter->elementwise_affine_ = layer_norm_prim->elementwiseAffine(); + + return reinterpret_cast(layer_norm_parameter); +} + +Registry g_layerNormV0ParameterRegistry(schema::v0::PrimitiveType_LayerNorm, PopulateLayerNormParameterV0, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.h b/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.h new file mode 100644 index 0000000000..ebad3560d0 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/layer_norm_populate_v0.h @@ -0,0 +1,27 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_LAYER_NORM_POPULATE_H_ +#define MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_LAYER_NORM_POPULATE_H_ + +#include "nnacl/arithmetic.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateLayerNormParameterV0(const void *prim); + +} // namespace lite +} // namespace mindspore +#endif // MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_LAYER_NORM_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/v0/local_response_normalization_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/local_response_normalization_populate_v0.cc new file mode 100644 index 0000000000..8a3f028c48 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/local_response_normalization_populate_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/local_response_norm_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateLocalResponseNormParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto local_response_normalization_prim = primitive->value_as_LocalResponseNormalization(); + + LocalResponseNormParameter *lrn_param = + reinterpret_cast(malloc(sizeof(LocalResponseNormParameter))); + if (lrn_param == nullptr) { + MS_LOG(ERROR) << "malloc LocalResponseNormParameter failed."; + return nullptr; + } + memset(lrn_param, 0, sizeof(LocalResponseNormParameter)); + lrn_param->op_parameter_.type_ = schema::PrimitiveType_LRN; + lrn_param->depth_radius_ = local_response_normalization_prim->depth_radius(); + lrn_param->bias_ = local_response_normalization_prim->bias(); + lrn_param->alpha_ = local_response_normalization_prim->alpha(); + lrn_param->beta_ = local_response_normalization_prim->beta(); + return reinterpret_cast(lrn_param); +} +} // namespace + +Registry g_localResponseNormalizationV0ParameterRegistry(schema::v0::PrimitiveType_LocalResponseNormalization, + PopulateLocalResponseNormParameter, SCHEMA_V0); +Registry g_lrnV0ParameterRegistry(schema::v0::PrimitiveType_Lrn, PopulateLocalResponseNormParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/lsh_projection_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/lsh_projection_populate_v0.cc new file mode 100644 index 0000000000..1292c5e013 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/lsh_projection_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/lsh_projection_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateLshProjectionParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto lsh_projection_prim = primitive->value_as_LshProjection(); + LshProjectionParameter *lsh_project_param = + reinterpret_cast(malloc(sizeof(LshProjectionParameter))); + if (lsh_project_param == nullptr) { + MS_LOG(ERROR) << "malloc LshProjectionParameter failed."; + return nullptr; + } + memset(lsh_project_param, 0, sizeof(LshProjectionParameter)); + lsh_project_param->op_parameter_.type_ = schema::PrimitiveType_LshProjection; + + lsh_project_param->lsh_type_ = lsh_projection_prim->type(); + return reinterpret_cast(lsh_project_param); +} +} // namespace + +Registry g_lshProjectionV0ParameterRegistry(schema::v0::PrimitiveType_LshProjection, PopulateLshProjectionParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/lstm_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/lstm_populate_v0.cc new file mode 100644 index 0000000000..701909fd14 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/lstm_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/lstm_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateLstmParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto lstm_prim = primitive->value_as_Lstm(); + LstmParameter *lstm_param = reinterpret_cast(malloc(sizeof(LstmParameter))); + if (lstm_param == nullptr) { + MS_LOG(ERROR) << "malloc LstmParameter failed."; + return nullptr; + } + memset(lstm_param, 0, sizeof(LstmParameter)); + lstm_param->op_parameter_.type_ = schema::PrimitiveType_LSTM; + + if (lstm_prim == nullptr) { + free(lstm_param); + MS_LOG(ERROR) << "get Lstm param nullptr."; + return nullptr; + } + lstm_param->bidirectional_ = lstm_prim->bidirection(); + return reinterpret_cast(lstm_param); +} +} // namespace + +Registry g_lstmV0ParameterRegistry(schema::v0::PrimitiveType_Lstm, PopulateLstmParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/matmul_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/matmul_populate_v0.cc new file mode 100644 index 0000000000..41d100e922 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/matmul_populate_v0.cc @@ -0,0 +1,46 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/matmul_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateMatMulParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto matmul_prim = primitive->value_as_MatMul(); + + MatMulParameter *matmul_param = reinterpret_cast(malloc(sizeof(MatMulParameter))); + if (matmul_param == nullptr) { + MS_LOG(ERROR) << "malloc MatMulParameter failed."; + return nullptr; + } + memset(matmul_param, 0, sizeof(MatMulParameter)); + matmul_param->op_parameter_.type_ = schema::PrimitiveType_MatMul; + matmul_param->b_transpose_ = matmul_prim->transposeB(); + matmul_param->a_transpose_ = matmul_prim->transposeA(); + matmul_param->has_bias_ = false; + matmul_param->act_type_ = ActType_No; + + return reinterpret_cast(matmul_param); +} +} // namespace + +Registry g_MatMulPV0arameterRegistry(schema::v0::PrimitiveType_MatMul, PopulateMatMulParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/mul_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/mul_populate_v0.cc new file mode 100644 index 0000000000..a8b5b3a9ea --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/mul_populate_v0.cc @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/ops/populate/arithmetic_populate.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateMulParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto mul_prim = primitive->value_as_Mul(); + ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + param->op_parameter_.type_ = schema::PrimitiveType_MulFusion; + param->activation_type_ = mul_prim->activationType(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_mulV0ParameterRegistry(schema::v0::PrimitiveType_Mul, PopulateMulParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/nchw2nhwc_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/nchw2nhwc_populate_v0.cc new file mode 100644 index 0000000000..33035d5371 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/nchw2nhwc_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/common.h" +#include "nnacl/transpose.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateNchw2NhwcParameter(const void *prim) { + TransposeParameter *parameter = reinterpret_cast(malloc(sizeof(TransposeParameter))); + if (parameter == nullptr) { + MS_LOG(ERROR) << "malloc OpParameter failed."; + return nullptr; + } + memset(parameter, 0, sizeof(OpParameter)); + parameter->op_parameter_.type_ = schema::PrimitiveType_Transpose; + parameter->num_axes_ = 4; + parameter->perm_[0] = 0; + parameter->perm_[1] = 2; + parameter->perm_[2] = 3; + parameter->perm_[3] = 1; + return reinterpret_cast(parameter); +} +} // namespace + +Registry g_nchw2NhwcV0ParameterRegistry(schema::v0::PrimitiveType_Nchw2Nhwc, PopulateNchw2NhwcParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/nhwc2nchw_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/nhwc2nchw_populate_v0.cc new file mode 100644 index 0000000000..11f2b11fa5 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/nhwc2nchw_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/common.h" +#include "nnacl/transpose.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateNhwc2NchwParameter(const void *prim) { + TransposeParameter *parameter = reinterpret_cast(malloc(sizeof(TransposeParameter))); + if (parameter == nullptr) { + MS_LOG(ERROR) << "malloc OpParameter failed."; + return nullptr; + } + memset(parameter, 0, sizeof(OpParameter)); + parameter->op_parameter_.type_ = schema::PrimitiveType_Transpose; + parameter->num_axes_ = 4; + parameter->perm_[0] = 0; + parameter->perm_[1] = 3; + parameter->perm_[2] = 1; + parameter->perm_[3] = 2; + return reinterpret_cast(parameter); +} +} // namespace + +Registry g_nhwc2NchwV0ParameterRegistry(schema::v0::PrimitiveType_Nhwc2Nchw, PopulateNhwc2NchwParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc new file mode 100644 index 0000000000..3193862a4c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/non_max_suppression_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/non_max_suppression_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateNonMaxSuppressionParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto non_max_suppression_prim = primitive->value_as_NonMaxSuppression(); + NMSParameter *param = reinterpret_cast(malloc(sizeof(NMSParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc param failed."; + return nullptr; + } + memset(param, 0, sizeof(NMSParameter)); + param->op_parameter_.type_ = schema::PrimitiveType_NonMaxSuppression; + + param->center_point_box_ = non_max_suppression_prim->centerPointBox(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_nonMaxSuppressionV0ParameterRegistry(schema::v0::PrimitiveType_NonMaxSuppression, + PopulateNonMaxSuppressionParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/one_hot_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/one_hot_populate_v0.cc new file mode 100644 index 0000000000..332c903f91 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/one_hot_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/one_hot_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateOneHotParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto one_hot_prim = primitive->value_as_OneHot(); + OneHotParameter *one_hot_param = reinterpret_cast(malloc(sizeof(OneHotParameter))); + if (one_hot_param == nullptr) { + MS_LOG(ERROR) << "malloc OneHotParameter failed."; + return nullptr; + } + memset(one_hot_param, 0, sizeof(OneHotParameter)); + one_hot_param->op_parameter_.type_ = schema::PrimitiveType_OneHot; + + if (one_hot_prim == nullptr) { + free(one_hot_param); + MS_LOG(ERROR) << "get OneHot param nullptr."; + return nullptr; + } + one_hot_param->axis_ = one_hot_prim->axis(); + return reinterpret_cast(one_hot_param); +} +} // namespace + +Registry g_oneHotV0ParameterRegistry(schema::v0::PrimitiveType_OneHot, PopulateOneHotParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/oneslike_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/oneslike_populate_v0.cc new file mode 100644 index 0000000000..da034b0322 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/oneslike_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateOnesLikeParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc OnesLike Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_OnesLike; + return param; +} +} // namespace + +Registry g_onesLikeV0ParameterRegistry(schema::v0::PrimitiveType_OnesLike, PopulateOnesLikeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/p_relu_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/p_relu_populate_v0.cc new file mode 100644 index 0000000000..3cee353dde --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/p_relu_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/prelu_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulatePReLUParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto p_relu_prim = primitive->value_as_PReLU(); + + PReluParameter *prelu_param = reinterpret_cast(malloc(sizeof(PReluParameter))); + if (prelu_param == nullptr) { + MS_LOG(ERROR) << "malloc PReluParameter failed."; + return nullptr; + } + memset(prelu_param, 0, sizeof(PReluParameter)); + prelu_param->op_parameter_.type_ = schema::PrimitiveType_PReLUFusion; + prelu_param->channelShared = p_relu_prim->channelShared(); + return reinterpret_cast(prelu_param); +} +} // namespace + +Registry g_pReLUV0ParameterRegistry(schema::v0::PrimitiveType_PReLU, PopulatePReLUParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc new file mode 100644 index 0000000000..1ed05e2a61 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/pad_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/pad_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulatePadParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto pad_prim = primitive->value_as_Pad(); + PadParameter *pad_param = reinterpret_cast(malloc(sizeof(PadParameter))); + if (pad_param == nullptr) { + MS_LOG(ERROR) << "malloc PadParameter failed."; + return nullptr; + } + memset(pad_param, 0, sizeof(PadParameter)); + pad_param->op_parameter_.type_ = schema::PrimitiveType_PadFusion; + + pad_param->pad_mode_ = pad_prim->paddingMode(); + pad_param->constant_value_ = pad_prim->constantValue(); + return reinterpret_cast(pad_param); +} +} // namespace + +Registry g_padV0ParameterRegistry(schema::v0::PrimitiveType_Pad, PopulatePadParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/partial_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/partial_populate_v0.cc new file mode 100644 index 0000000000..553f371915 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/partial_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +typedef struct PartialParameter { + OpParameter op_parameter_; + int sub_graph_index_; +} PartialParameter; + +OpParameter *PopulatePartialParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto partial_prim = primitive->value_as_Partial(); + PartialParameter *partial_parameter = reinterpret_cast(malloc(sizeof(PartialParameter))); + if (partial_parameter == nullptr) { + MS_LOG(ERROR) << "malloc partial parameter failed."; + return nullptr; + } + memset(partial_parameter, 0, sizeof(PartialParameter)); + partial_parameter->op_parameter_.type_ = schema::PrimitiveType_PartialFusion; + + partial_parameter->sub_graph_index_ = partial_prim->subGraphIndex(); + + return reinterpret_cast(partial_parameter); +} +} // namespace + +Registry g_partialV0ParameterRegistry(schema::v0::PrimitiveType_Partial, PopulatePartialParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc new file mode 100644 index 0000000000..3c074bc7bf --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/pooling_populate_v0.cc @@ -0,0 +1,100 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/pooling_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulatePoolingParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto pooling_prim = primitive->value_as_Pooling(); + + PoolingParameter *pooling_param = reinterpret_cast(malloc(sizeof(PoolingParameter))); + if (pooling_param == nullptr) { + MS_LOG(ERROR) << "malloc PoolingParameter failed."; + return nullptr; + } + memset(pooling_param, 0, sizeof(PoolingParameter)); + pooling_param->global_ = pooling_prim->global(); + pooling_param->window_w_ = pooling_prim->windowW(); + pooling_param->window_h_ = pooling_prim->windowH(); + pooling_param->pad_u_ = pooling_prim->padUp(); + pooling_param->pad_d_ = pooling_prim->padDown(); + pooling_param->pad_l_ = pooling_prim->padLeft(); + pooling_param->pad_r_ = pooling_prim->padRight(); + pooling_param->stride_w_ = pooling_prim->strideW(); + pooling_param->stride_h_ = pooling_prim->strideH(); + pooling_param->avg_mode_ = pooling_prim->avgMode(); + + auto is_global = pooling_prim->global(); + pooling_param->global_ = is_global; + auto pool_mode = pooling_prim->poolingMode(); + switch (pool_mode) { + case schema::v0::PoolMode_MAX_POOLING: + pooling_param->pool_mode_ = PoolMode_MaxPool; + pooling_param->op_parameter_.type_ = schema::PrimitiveType_MaxPoolFusion; + break; + case schema::v0::PoolMode_MEAN_POOLING: + pooling_param->pool_mode_ = PoolMode_AvgPool; + pooling_param->op_parameter_.type_ = schema::PrimitiveType_AvgPoolFusion; + break; + default: + pooling_param->pool_mode_ = PoolMode_No; + pooling_param->op_parameter_.type_ = primitive->value_type(); + break; + } + + auto round_mode = pooling_prim->roundMode(); + switch (round_mode) { + case schema::v0::RoundMode_FLOOR: + pooling_param->round_mode_ = RoundMode_Floor; + break; + case schema::v0::RoundMode_CEIL: + pooling_param->round_mode_ = RoundMode_Ceil; + break; + default: + pooling_param->round_mode_ = RoundMode_No; + break; + } + + if (pooling_prim->activationType() == schema::v0::ActivationType_RELU) { + pooling_param->act_type_ = ActType_Relu; + } else if (pooling_prim->activationType() == schema::v0::ActivationType_RELU6) { + pooling_param->act_type_ = ActType_Relu6; + } else { + pooling_param->act_type_ = ActType_No; + } + switch (pooling_prim->padMode()) { + case schema::v0::PadMode_SAME_UPPER: + pooling_param->pad_mode_ = Pad_same; + break; + case schema::v0::PadMode_VALID: + pooling_param->pad_mode_ = Pad_valid; + break; + default: + pooling_param->pad_mode_ = Pad_pad; + break; + } + return reinterpret_cast(pooling_param); +} +} // namespace + +Registry g_poolingV0ParameterRegistry(schema::v0::PrimitiveType_Pooling, PopulatePoolingParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/power_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/power_populate_v0.cc new file mode 100644 index 0000000000..b7391b0828 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/power_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/power_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulatePowerParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto power_prim = primitive->value_as_Power(); + PowerParameter *power_param = reinterpret_cast(malloc(sizeof(PowerParameter))); + if (power_param == nullptr) { + MS_LOG(ERROR) << "malloc PowerParameter failed."; + return nullptr; + } + memset(power_param, 0, sizeof(PowerParameter)); + power_param->op_parameter_.type_ = schema::PrimitiveType_PowFusion; + + power_param->scale_ = power_prim->scale(); + power_param->shift_ = power_prim->shift(); + return reinterpret_cast(power_param); +} +} // namespace + +Registry g_powerV0ParameterRegistry(schema::v0::PrimitiveType_Power, PopulatePowerParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/prior_box_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/prior_box_populate_v0.cc new file mode 100644 index 0000000000..7c5c1ef78c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/prior_box_populate_v0.cc @@ -0,0 +1,83 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/prior_box_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulatePriorBoxParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto prior_box_prim = primitive->value_as_PriorBox(); + PriorBoxParameter *prior_box_param = reinterpret_cast(malloc(sizeof(PriorBoxParameter))); + if (prior_box_param == nullptr) { + MS_LOG(ERROR) << "malloc PriorBoxParameter failed."; + return nullptr; + } + memset(prior_box_param, 0, sizeof(PriorBoxParameter)); + prior_box_param->op_parameter_.type_ = schema::PrimitiveType_PriorBox; + + if (prior_box_prim->min_sizes()->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox min_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " + << prior_box_prim->min_sizes(); + free(prior_box_param); + return nullptr; + } + prior_box_param->min_sizes_size = prior_box_prim->min_sizes()->size(); + if (prior_box_prim->max_sizes()->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox max_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " + << prior_box_prim->max_sizes(); + free(prior_box_param); + return nullptr; + } + prior_box_param->max_sizes_size = prior_box_prim->max_sizes()->size(); + memcpy(prior_box_param->max_sizes, prior_box_prim->max_sizes()->data(), + prior_box_prim->max_sizes()->size() * sizeof(int32_t)); + memcpy(prior_box_param->min_sizes, prior_box_prim->min_sizes()->data(), + prior_box_prim->min_sizes()->size() * sizeof(int32_t)); + + if (prior_box_prim->aspect_ratios()->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox aspect_ratios size exceeds max num " << MAX_SHAPE_SIZE << ", got " + << prior_box_prim->aspect_ratios(); + free(prior_box_param); + return nullptr; + } + prior_box_param->aspect_ratios_size = prior_box_prim->aspect_ratios()->size(); + memcpy(prior_box_param->aspect_ratios, prior_box_prim->aspect_ratios()->data(), + prior_box_prim->aspect_ratios()->size() * sizeof(float)); + if (prior_box_prim->variances()->size() != COMM_SHAPE_SIZE) { + MS_LOG(ERROR) << "PriorBox variances size should be " << COMM_SHAPE_SIZE << ", got " + << prior_box_prim->variances()->size(); + free(prior_box_param); + return nullptr; + } + memcpy(prior_box_param->variances, prior_box_prim->variances()->data(), COMM_SHAPE_SIZE * sizeof(float)); + prior_box_param->flip = prior_box_prim->flip(); + prior_box_param->clip = prior_box_prim->clip(); + prior_box_param->offset = prior_box_prim->offset(); + prior_box_param->image_size_h = prior_box_prim->image_size_h(); + prior_box_param->image_size_w = prior_box_prim->image_size_w(); + prior_box_param->step_h = prior_box_prim->step_h(); + prior_box_param->step_w = prior_box_prim->step_w(); + return reinterpret_cast(prior_box_param); +} +} // namespace + +Registry g_priorBoxV0ParameterRegistry(schema::v0::PrimitiveType_PriorBox, PopulatePriorBoxParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/quant_dtype_cast_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/quant_dtype_cast_populate_v0.cc new file mode 100644 index 0000000000..b4df8a9cc5 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/quant_dtype_cast_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/int8/quant_dtype_cast_int8.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateQuantDTypeCastParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto quant_dtype_cast_prim = primitive->value_as_QuantDTypeCast(); + QuantDTypeCastParameter *parameter = + reinterpret_cast(malloc(sizeof(QuantDTypeCastParameter))); + if (parameter == nullptr) { + MS_LOG(ERROR) << "malloc QuantDTypeCastParameter failed."; + return nullptr; + } + memset(parameter, 0, sizeof(QuantDTypeCastParameter)); + parameter->op_parameter_.type_ = schema::PrimitiveType_QuantDTypeCast; + + parameter->srcT = quant_dtype_cast_prim->srcT(); + parameter->dstT = quant_dtype_cast_prim->dstT(); + return reinterpret_cast(parameter); +} +} // namespace + +Registry g_quantDTypeCastV0ParameterRegistry(schema::v0::PrimitiveType_QuantDTypeCast, PopulateQuantDTypeCastParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/range_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/range_populate_v0.cc new file mode 100644 index 0000000000..52200f24e7 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/range_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/range_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateRangeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto range_prim = primitive->value_as_Range(); + + RangeParameter *range_param = reinterpret_cast(malloc(sizeof(RangeParameter))); + if (range_param == nullptr) { + MS_LOG(ERROR) << "malloc RangeParameter failed."; + return nullptr; + } + memset(range_param, 0, sizeof(RangeParameter)); + range_param->op_parameter_.type_ = schema::PrimitiveType_Range; + range_param->start_ = range_prim->start(); + range_param->limit_ = range_prim->limit(); + range_param->delta_ = range_prim->delta(); + range_param->dType_ = range_prim->dType(); + return reinterpret_cast(range_param); +} +} // namespace + +Registry g_rangeV0ParameterRegistry(schema::v0::PrimitiveType_Range, PopulateRangeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/rank_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/rank_populate_v0.cc new file mode 100644 index 0000000000..b001f6453a --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/rank_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateRankParameter(const void *prim) { + OpParameter *rank_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (rank_param == nullptr) { + MS_LOG(ERROR) << "malloc RankParameter failed."; + return nullptr; + } + memset(rank_param, 0, sizeof(OpParameter)); + rank_param->type_ = schema::PrimitiveType_Rank; + return reinterpret_cast(rank_param); +} +} // namespace + +Registry g_rankV0ParameterRegistry(schema::v0::PrimitiveType_Rank, PopulateRankParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/reduce_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/reduce_populate_v0.cc new file mode 100644 index 0000000000..c23faa5299 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/reduce_populate_v0.cc @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/reduce_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateReduceParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto reduce_prim = primitive->value_as_Reduce(); + ReduceParameter *reduce_param = reinterpret_cast(malloc(sizeof(ReduceParameter))); + if (reduce_param == nullptr) { + MS_LOG(ERROR) << "malloc ReduceParameter failed."; + return nullptr; + } + memset(reduce_param, 0, sizeof(ReduceParameter)); + reduce_param->op_parameter_.type_ = schema::PrimitiveType_ReduceFusion; + + reduce_param->keep_dims_ = reduce_prim->keepDims(); + reduce_param->reduce_to_end_ = reduce_prim->reduceToEnd(); + reduce_param->coeff = reduce_prim->coeff(); + auto axisVector = reduce_prim->axes(); + if (axisVector->size() > MAX_SHAPE_SIZE) { + MS_LOG(ERROR) << "Reduce axes size " << axisVector->size() << " exceed limit " << MAX_SHAPE_SIZE; + free(reduce_param); + return nullptr; + } + reduce_param->num_axes_ = static_cast(axisVector->size()); + int i = 0; + for (auto iter = axisVector->begin(); iter != axisVector->end(); iter++) { + reduce_param->axes_[i++] = *iter; + } + reduce_param->mode_ = static_cast(reduce_prim->mode()); + return reinterpret_cast(reduce_param); +} +} // namespace + +Registry g_reduceV0ParameterRegistry(schema::v0::PrimitiveType_Reduce, PopulateReduceParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/reshape_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/reshape_populate_v0.cc new file mode 100644 index 0000000000..9426688978 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/reshape_populate_v0.cc @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/log_adapter.h" +#include "nnacl/reshape_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateReshapeParameter(const void *prim) { + ReshapeParameter *reshape_param = reinterpret_cast(malloc(sizeof(ReshapeParameter))); + if (reshape_param == nullptr) { + MS_LOG(ERROR) << "malloc ReshapeParameter failed."; + return nullptr; + } + memset(reshape_param, 0, sizeof(ReshapeParameter)); + reshape_param->op_parameter_.type_ = schema::PrimitiveType_Reshape; + return reinterpret_cast(reshape_param); +} +} // namespace + +Registry g_reshapeV0ParameterRegistry(schema::v0::PrimitiveType_Reshape, PopulateReshapeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/resize_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/resize_populate_v0.cc new file mode 100644 index 0000000000..a759c153b7 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/resize_populate_v0.cc @@ -0,0 +1,50 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/resize_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateResizeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto resize_prim = primitive->value_as_Resize(); + ResizeParameter *resize_param = reinterpret_cast(malloc(sizeof(ResizeParameter))); + if (resize_param == nullptr) { + MS_LOG(ERROR) << "malloc ResizeParameter failed."; + return nullptr; + } + memset(resize_param, 0, sizeof(ResizeParameter)); + resize_param->op_parameter_.type_ = schema::PrimitiveType_Resize; + + resize_param->method_ = static_cast(resize_prim->method()); + resize_param->new_height_ = resize_prim->newHeight(); + resize_param->new_width_ = resize_prim->newWidth(); + if (resize_prim->alignCorners()) { + resize_param->coordinate_transform_mode_ = 1; + } else { + resize_param->coordinate_transform_mode_ = 0; + } + resize_param->preserve_aspect_ratio_ = resize_prim->preserveAspectRatio(); + return reinterpret_cast(resize_param); +} +} // namespace + +Registry g_resizeV0ParameterRegistry(schema::v0::PrimitiveType_Resize, PopulateResizeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/reverse_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/reverse_populate_v0.cc new file mode 100644 index 0000000000..6f45635fd8 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/reverse_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/reverse_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateReverseParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto reverse_prim = primitive->value_as_Reverse(); + + ReverseParameter *reverse_param = reinterpret_cast(malloc(sizeof(ReverseParameter))); + if (reverse_param == nullptr) { + MS_LOG(ERROR) << "malloc ReverseParameter failed."; + return nullptr; + } + memset(reverse_param, 0, sizeof(ReverseParameter)); + reverse_param->op_parameter_.type_ = schema::PrimitiveType_ReverseV2; + auto flatAxis = reverse_prim->axis(); + reverse_param->num_axis_ = flatAxis->size(); + int i = 0; + for (auto iter = flatAxis->begin(); iter != flatAxis->end(); iter++) { + reverse_param->axis_[i++] = *iter; + } + return reinterpret_cast(reverse_param); +} +} // namespace + +Registry g_reverseV0ParameterRegistry(schema::v0::PrimitiveType_Reverse, PopulateReverseParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc new file mode 100644 index 0000000000..f4782daea7 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/reverse_sequence_populate_v0.cc @@ -0,0 +1,45 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/reverse_sequence_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateReverseSequenceParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto reverse_sequence_prim = primitive->value_as_ReverseSequence(); + ReverseSequenceParameter *reverse_sequence_param = + reinterpret_cast(malloc(sizeof(ReverseSequenceParameter))); + if (reverse_sequence_param == nullptr) { + MS_LOG(ERROR) << "malloc ReverseSequenceParameter failed."; + return nullptr; + } + memset(reverse_sequence_param, 0, sizeof(ReverseSequenceParameter)); + + reverse_sequence_param->op_parameter_.type_ = schema::PrimitiveType_ReverseSequence; + reverse_sequence_param->seq_axis_ = reverse_sequence_prim->seqAxis(); + reverse_sequence_param->batch_axis_ = reverse_sequence_prim->batchAxis(); + return reinterpret_cast(reverse_sequence_param); +} +} // namespace + +Registry g_reverseSequenceV0ParameterRegistry(schema::v0::PrimitiveType_ReverseSequence, + PopulateReverseSequenceParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc new file mode 100644 index 0000000000..59d07c90d2 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/roi_pooling_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/roi_pooling_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateROIPoolingParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto roi_pooling_prim = primitive->value_as_ROIPooling(); + + ROIPoolingParameter *roi_pooling_param = reinterpret_cast(malloc(sizeof(ROIPoolingParameter))); + if (roi_pooling_param == nullptr) { + MS_LOG(ERROR) << "malloc ROIPoolingParameter failed."; + return nullptr; + } + memset(roi_pooling_param, 0, sizeof(ROIPoolingParameter)); + roi_pooling_param->op_parameter_.type_ = schema::PrimitiveType_ROIPooling; + roi_pooling_param->pooledH_ = roi_pooling_prim->pooledH(); + roi_pooling_param->pooledW_ = roi_pooling_prim->pooledW(); // note: origin is pooledH + roi_pooling_param->scale_ = roi_pooling_prim->scale(); + return reinterpret_cast(roi_pooling_param); +} +} // namespace + +Registry g_ROIPoolingV0ParameterRegistry(schema::v0::PrimitiveType_ROIPooling, PopulateROIPoolingParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/scale_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/scale_populate_v0.cc new file mode 100644 index 0000000000..59d778a101 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/scale_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/scale.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateScaleParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto scale_prim = primitive->value_as_Scale(); + if (primitive == nullptr) { + MS_LOG(ERROR) << "input primitive is nullptr"; + return nullptr; + } + ScaleParameter *scale_param = reinterpret_cast(malloc(sizeof(ScaleParameter))); + if (scale_param == nullptr) { + MS_LOG(ERROR) << "malloc ScaleParameter failed."; + return nullptr; + } + memset(scale_param, 0, sizeof(ScaleParameter)); + scale_param->op_parameter_.type_ = schema::PrimitiveType_ScaleFusion; + + scale_param->axis_ = scale_prim->axis(); + scale_param->activation_type_ = scale_prim->activationType(); + return reinterpret_cast(scale_param); +} +} // namespace + +Registry g_scaleV0ParameterRegistry(schema::v0::PrimitiveType_Scale, PopulateScaleParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/scatter_nd_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/scatter_nd_populate_v0.cc new file mode 100644 index 0000000000..5222f6d485 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/scatter_nd_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateScatterNDParameter(const void *prim) { + OpParameter *scatter_nd_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (scatter_nd_param == nullptr) { + MS_LOG(ERROR) << "malloc ScatterNDParameter failed."; + return nullptr; + } + memset(scatter_nd_param, 0, sizeof(OpParameter)); + scatter_nd_param->type_ = schema::PrimitiveType_ScatterNd; + return reinterpret_cast(scatter_nd_param); +} +} // namespace + +Registry g_scatterNDV0ParameterRegistry(schema::v0::PrimitiveType_ScatterND, PopulateScatterNDParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/shape_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/shape_populate_v0.cc new file mode 100644 index 0000000000..6e32434d26 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/shape_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "src/common/log_adapter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateShapeParameter(const void *prim) { + OpParameter *shape_param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (shape_param == nullptr) { + MS_LOG(ERROR) << "malloc ShapeParameter failed."; + return nullptr; + } + memset(shape_param, 0, sizeof(OpParameter)); + shape_param->type_ = schema::PrimitiveType_Shape; + return reinterpret_cast(shape_param); +} +} // namespace + +Registry g_shapeV0ParameterRegistry(schema::v0::PrimitiveType_Shape, PopulateShapeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/skip_gram_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/skip_gram_populate_v0.cc new file mode 100644 index 0000000000..5d8d7988a8 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/skip_gram_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/skip_gram_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSkipGramParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto skip_gram_prim = primitive->value_as_SkipGram(); + SkipGramParameter *skipGramParameter = reinterpret_cast(malloc(sizeof(SkipGramParameter))); + if (skipGramParameter == nullptr) { + MS_LOG(ERROR) << "malloc SkipGramParameter failed."; + return nullptr; + } + memset(skipGramParameter, 0, sizeof(SkipGramParameter)); + skipGramParameter->op_parameter_.type_ = schema::PrimitiveType_SkipGram; + + skipGramParameter->ngram_size = skip_gram_prim->ngramSize(); + skipGramParameter->max_skip_size = skip_gram_prim->maxSkipSize(); + skipGramParameter->include_all_ngrams = skip_gram_prim->includeAllGrams(); + return reinterpret_cast(skipGramParameter); +} +} // namespace + +Registry g_skipGramV0ParameterRegistry(schema::v0::PrimitiveType_SkipGram, PopulateSkipGramParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/slice_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/slice_populate_v0.cc new file mode 100644 index 0000000000..0d2742d211 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/slice_populate_v0.cc @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/slice_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSliceParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto slice_prim = primitive->value_as_Slice(); + SliceParameter *slice_param = reinterpret_cast(malloc(sizeof(SliceParameter))); + if (slice_param == nullptr) { + MS_LOG(ERROR) << "malloc SliceParameter failed."; + return nullptr; + } + memset(slice_param, 0, sizeof(SliceParameter)); + + slice_param->op_parameter_.type_ = schema::PrimitiveType_SliceFusion; + auto param_begin = slice_prim->begin(); + auto param_size = slice_prim->size(); + auto param_axis = slice_prim->axes(); + if (param_begin->size() != param_size->size() || param_begin->size() != param_axis->size()) { + free(slice_param); + return nullptr; + } + + slice_param->param_length_ = static_cast(param_begin->size()); + for (int32_t i = 0; i < slice_param->param_length_; ++i) { + slice_param->begin_[i] = *(param_begin->begin() + i); + slice_param->size_[i] = *(param_size->begin() + i); + slice_param->axis_[i] = *(param_axis->begin() + i); + } + + return reinterpret_cast(slice_param); +} +} // namespace + +Registry g_sliceV0ParameterRegistry(schema::v0::PrimitiveType_Slice, PopulateSliceParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/softmax_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/softmax_populate_v0.cc new file mode 100644 index 0000000000..390ddaeb39 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/softmax_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/softmax_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSoftmaxParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto softmax_prim = primitive->value_as_SoftMax(); + + SoftmaxParameter *softmax_param = reinterpret_cast(malloc(sizeof(SoftmaxParameter))); + if (softmax_param == nullptr) { + MS_LOG(ERROR) << "malloc SoftmaxParameter failed."; + return nullptr; + } + memset(softmax_param, 0, sizeof(SoftmaxParameter)); + softmax_param->op_parameter_.type_ = schema::PrimitiveType_Softmax; + softmax_param->axis_ = softmax_prim->axis(); + return reinterpret_cast(softmax_param); +} +} // namespace + +Registry g_softMaxV0ParameterRegistry(schema::v0::PrimitiveType_SoftMax, PopulateSoftmaxParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/space_to_batch_nd_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/space_to_batch_nd_populate_v0.cc new file mode 100644 index 0000000000..81bacedd49 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/space_to_batch_nd_populate_v0.cc @@ -0,0 +1,56 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/space_to_batch_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSpaceToBatchNDParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto space_to_batch_nd_prim = primitive->value_as_SpaceToBatchND(); + auto *space_batch_param_nd = reinterpret_cast(malloc(sizeof(SpaceToBatchParameter))); + if (space_batch_param_nd == nullptr) { + MS_LOG(ERROR) << "malloc SpaceToBatchParameter failed."; + return nullptr; + } + + space_batch_param_nd->op_parameter_.type_ = schema::PrimitiveType_SpaceToBatchND; + auto block_sizes = space_to_batch_nd_prim->blockShape(); + space_batch_param_nd->m_ = block_sizes->size(); + if (((size_t)block_sizes->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of block_sizes.size() is too big"; + free(space_batch_param_nd); + return nullptr; + } + memcpy(space_batch_param_nd->block_sizes_, (block_sizes->data()), block_sizes->size() * sizeof(int)); + auto paddings = space_to_batch_nd_prim->paddings(); + if (((size_t)paddings->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of paddings.size() is too big"; + free(space_batch_param_nd); + return nullptr; + } + memcpy(space_batch_param_nd->paddings_, (paddings->data()), paddings->size() * sizeof(int)); + return reinterpret_cast(space_batch_param_nd); +} +} // namespace + +Registry g_SpaceToBatchNDV0ParameterRegistry(schema::v0::PrimitiveType_SpaceToBatchND, PopulateSpaceToBatchNDParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/space_to_batch_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/space_to_batch_populate_v0.cc new file mode 100644 index 0000000000..c01154da13 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/space_to_batch_populate_v0.cc @@ -0,0 +1,63 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/space_to_batch_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSpaceToBatchParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto space_to_batch_prim = primitive->value_as_SpaceToBatch(); + SpaceToBatchParameter *space_batch_param = + reinterpret_cast(malloc(sizeof(SpaceToBatchParameter))); + if (space_batch_param == nullptr) { + MS_LOG(ERROR) << "malloc SpaceToBatchParameter failed."; + return nullptr; + } + memset(space_batch_param, 0, sizeof(SpaceToBatchParameter)); + space_batch_param->op_parameter_.type_ = schema::PrimitiveType_SpaceToBatch; + auto block_sizes = space_to_batch_prim->blockShape(); // maybe error + space_batch_param->m_ = block_sizes->size(); + if (((size_t)block_sizes->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of block_sizes.size() is too big"; + free(space_batch_param); + return nullptr; + } + memcpy(space_batch_param->block_sizes_, (block_sizes->data()), block_sizes->size() * sizeof(int)); + auto paddings = space_to_batch_prim->paddings(); + if (((size_t)paddings->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of paddings.size() is too big"; + free(space_batch_param); + return nullptr; + } + memcpy(space_batch_param->paddings_, (paddings->data()), paddings->size() * sizeof(int)); + + space_batch_param->m_ = space_to_batch_prim->blockShape()->size(); + for (int i = 0; i < space_batch_param->m_; i++) { + space_batch_param->block_sizes_[i] = space_to_batch_prim->blockShape()->data()[i]; + } + + return reinterpret_cast(space_batch_param); +} +} // namespace + +Registry g_spaceToBatchV0ParameterRegistry(schema::v0::PrimitiveType_SpaceToBatch, PopulateSpaceToBatchParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/space_to_depth_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/space_to_depth_populate_v0.cc new file mode 100644 index 0000000000..9306c97fc8 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/space_to_depth_populate_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/space_to_depth_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSpaceToDepthParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto space_to_depth_prim = primitive->value_as_SpaceToDepth(); + SpaceToDepthParameter *space_depth_param = + reinterpret_cast(malloc(sizeof(SpaceToDepthParameter))); + if (space_depth_param == nullptr) { + MS_LOG(ERROR) << "malloc SpaceToDepthParameter failed."; + return nullptr; + } + memset(space_depth_param, 0, sizeof(SpaceToDepthParameter)); + space_depth_param->op_parameter_.type_ = schema::PrimitiveType_SpaceToDepth; + space_depth_param->block_size_ = space_to_depth_prim->blockSize(); + if (space_to_depth_prim->format() != schema::v0::Format::Format_NHWC) { + MS_LOG(ERROR) << "Currently only NHWC format is supported."; + free(space_depth_param); + return nullptr; + } + return reinterpret_cast(space_depth_param); +} +} // namespace + +Registry g_spaceToDepthV0ParameterRegistry(schema::v0::PrimitiveType_SpaceToDepth, PopulateSpaceToDepthParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/sparse_to_dense_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/sparse_to_dense_populate_v0.cc new file mode 100644 index 0000000000..dca503b095 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/sparse_to_dense_populate_v0.cc @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/sparse_to_dense_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSparseToDenseParameter(const void *prim) { + auto *sparse_to_dense_param = reinterpret_cast(malloc(sizeof(SparseToDenseParameter))); + if (sparse_to_dense_param == nullptr) { + MS_LOG(ERROR) << "malloc SparseToDenseParameter failed."; + return nullptr; + } + memset(sparse_to_dense_param, 0, sizeof(SparseToDenseParameter)); + sparse_to_dense_param->op_parameter_.type_ = schema::PrimitiveType_SparseToDense; + return reinterpret_cast(sparse_to_dense_param); +} +} // namespace + +Registry g_sparseToDenseV0ParameterRegistry(schema::v0::PrimitiveType_SparseToDense, PopulateSparseToDenseParameter, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/split_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/split_populate_v0.cc new file mode 100644 index 0000000000..9a619deb21 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/split_populate_v0.cc @@ -0,0 +1,64 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/split_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSplitParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto split_prim = primitive->value_as_Split(); + auto *split_param = reinterpret_cast(malloc(sizeof(SplitParameter))); + if (split_param == nullptr) { + MS_LOG(ERROR) << "malloc SplitParameter failed."; + return nullptr; + } + memset(split_param, 0, sizeof(SplitParameter)); + split_param->op_parameter_.type_ = schema::PrimitiveType_Split; + split_param->num_split_ = split_prim->numberSplit(); + if (split_param->num_split_ > std::numeric_limits::max() / static_cast(sizeof(int))) { + MS_LOG(ERROR) << "The value of split_param->num_split_ is too big"; + return nullptr; + } + int *split_sizes = reinterpret_cast(malloc(split_param->num_split_ * sizeof(int))); + if (split_sizes == nullptr) { + MS_LOG(ERROR) << "malloc split size of SplitParameter failed."; + free(split_param); + return nullptr; + } + memset(split_sizes, 0, split_param->num_split_ * sizeof(int)); + split_param->split_sizes_ = split_sizes; + auto split_sizes_vector_ = split_prim->sizeSplits(); + if (split_sizes_vector_ != NULL) { + int i = 0; + for (auto iter = split_sizes_vector_->begin(); iter != split_sizes_vector_->end(); iter++) { + split_param->split_sizes_[i++] = *iter; + } + split_param->split_count_ = split_param->num_split_; + } else { + split_param->split_count_ = 0; + } + split_param->split_dim_ = split_prim->splitDim(); + return reinterpret_cast(split_param); +} +} // namespace + +Registry g_splitV0ParameterRegistry(schema::v0::PrimitiveType_Split, PopulateSplitParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/squared_difference_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/squared_difference_populate_v0.cc new file mode 100644 index 0000000000..6d50809c76 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/squared_difference_populate_v0.cc @@ -0,0 +1,40 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic.h" +#include "src/ops/populate/v0/arithmetic_populate_v0.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSquaredDifferenceParameter(const void *prim) { + auto *primitive = static_cast(prim); + ArithmeticParameter *param = PopulateArithmeticV0CommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + param->op_parameter_.type_ = schema::PrimitiveType_SquaredDifference; + return reinterpret_cast(param); +} +} // namespace + +Registry g_squaredDifferenceV0ParameterRegistry(schema::v0::PrimitiveType_SquaredDifference, + PopulateSquaredDifferenceParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/squeeze_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/squeeze_populate_v0.cc new file mode 100644 index 0000000000..154ff6bd7c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/squeeze_populate_v0.cc @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/squeeze_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSqueezeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto squeeze_prim = primitive->value_as_Squeeze(); + SqueezeParameter *squeeze_param = reinterpret_cast(malloc(sizeof(SqueezeParameter))); + if (squeeze_param == nullptr) { + MS_LOG(ERROR) << "malloc SqueezeParameter failed."; + return nullptr; + } + memset(squeeze_param, 0, sizeof(SqueezeParameter)); + squeeze_param->op_parameter_.type_ = schema::PrimitiveType_Squeeze; + if (squeeze_prim->axis() != nullptr) { + squeeze_param->axis_size_ = squeeze_prim->axis()->size(); + for (size_t i = 0; i < squeeze_param->axis_size_; i++) { + squeeze_param->axis_[i] = *(squeeze_prim->axis()->begin() + i); + } + } else { + squeeze_param->axis_size_ = 0; + } + + return reinterpret_cast(squeeze_param); +} +} // namespace + +Registry g_squeezeV0ParameterRegistry(schema::v0::PrimitiveType_Squeeze, PopulateSqueezeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/stack_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/stack_populate_v0.cc new file mode 100644 index 0000000000..1c11ce9a3c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/stack_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/stack_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateStackParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto stack_prim = primitive->value_as_Stack(); + StackParameter *stack_param = reinterpret_cast(malloc(sizeof(StackParameter))); + if (stack_param == nullptr) { + MS_LOG(ERROR) << "malloc StackParameter failed."; + return nullptr; + } + memset(stack_param, 0, sizeof(StackParameter)); + + stack_param->op_parameter_.type_ = schema::PrimitiveType_Stack; + stack_param->axis_ = stack_prim->axis(); + return reinterpret_cast(stack_param); +} +} // namespace + +Registry g_stackV0ParameterRegistry(schema::v0::PrimitiveType_Stack, PopulateStackParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.cc new file mode 100644 index 0000000000..933a5ce02c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.cc @@ -0,0 +1,75 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "src/ops/populate/v0/strided_slice_populate_v0.h" +#include +#include "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/strided_slice_parameter.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateStridedSliceParameterV0(const void *prim) { + auto *primitive = static_cast(prim); + auto strided_slice_prim = primitive->value_as_StridedSlice(); + StridedSliceParameter *strided_slice_param = + reinterpret_cast(malloc(sizeof(StridedSliceParameter))); + if (strided_slice_param == nullptr) { + MS_LOG(ERROR) << "malloc StridedSliceParameter failed."; + return nullptr; + } + memset(strided_slice_param, 0, sizeof(StridedSliceParameter)); + strided_slice_param->op_parameter_.type_ = schema::PrimitiveType_StridedSlice; + + auto begin = strided_slice_prim->begin(); + if (begin != nullptr) { + if (((size_t)begin->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of begin.size() is too big"; + free(strided_slice_param); + return nullptr; + } + memcpy(strided_slice_param->begins_, (begin->data()), begin->size() * sizeof(int)); + } + auto end = strided_slice_prim->end(); + if (end != nullptr) { + if (((size_t)end->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of end.size() is too big"; + free(strided_slice_param); + return nullptr; + } + memcpy(strided_slice_param->ends_, (end->data()), end->size() * sizeof(int)); + } + auto stride = strided_slice_prim->stride(); + if (stride != nullptr) { + if (((size_t)stride->size()) > std::numeric_limits::max() / sizeof(int)) { + MS_LOG(ERROR) << "The value of stride.size() is too big"; + free(strided_slice_param); + return nullptr; + } + memcpy(strided_slice_param->strides_, (stride->data()), stride->size() * sizeof(int)); + } + strided_slice_param->begins_mask_ = strided_slice_prim->beginMask(); + strided_slice_param->ends_mask_ = strided_slice_prim->endMask(); + strided_slice_param->ellipsisMask_ = strided_slice_prim->ellipsisMask(); + strided_slice_param->newAxisMask_ = strided_slice_prim->newAxisMask(); + strided_slice_param->shrinkAxisMask_ = strided_slice_prim->shrinkAxisMask(); + + return reinterpret_cast(strided_slice_param); +} + +Registry g_stridedSliceV0ParameterRegistry(schema::v0::PrimitiveType_StridedSlice, PopulateStridedSliceParameterV0, + SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.h b/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.h new file mode 100644 index 0000000000..6a2e89ac0a --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/strided_slice_populate_v0.h @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ +#define MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ + +#include "nnacl/strided_slice_parameter.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateStridedSliceParameterV0(const void *prim); + +} // namespace lite +} // namespace mindspore +#endif // MINDSPORE_LITE_SRC_OPS_POPULATE_STRIDED_SLICE_POPULATE_H_ diff --git a/mindspore/lite/src/ops/populate/v0/sub_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/sub_populate_v0.cc new file mode 100644 index 0000000000..8694f24641 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/sub_populate_v0.cc @@ -0,0 +1,41 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic.h" +#include "src/ops/populate/arithmetic_populate.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSubParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto sub_prim = primitive->value_as_Sub(); + ArithmeticParameter *param = PopulateArithmeticCommonPara(primitive); + if (param == nullptr) { + MS_LOG(ERROR) << "PopulateArithmeticCommonPara failed."; + return nullptr; + } + param->op_parameter_.type_ = schema::PrimitiveType_SubFusion; // note: maybe error noneed? + param->activation_type_ = sub_prim->activationType(); + return reinterpret_cast(param); +} +} // namespace + +Registry g_subV0ParameterRegistry(schema::v0::PrimitiveType_Sub, PopulateSubParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/switch_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/switch_populate_v0.cc new file mode 100644 index 0000000000..3cda18f091 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/switch_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateSwitchParameter(const void *prim) { + OpParameter *switch_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); + if (switch_parameter == nullptr) { + MS_LOG(ERROR) << "malloc SwitchParameter failed."; + return nullptr; + } + memset(switch_parameter, 0, sizeof(OpParameter)); + switch_parameter->type_ = schema::PrimitiveType_Switch; + + return reinterpret_cast(switch_parameter); +} +} // namespace + +Registry g_switchv0ParameterRegistry(schema::v0::PrimitiveType_Switch, PopulateSwitchParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tensorlistfromtensor_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tensorlistfromtensor_populate_v0.cc new file mode 100644 index 0000000000..b806fe45ee --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tensorlistfromtensor_populate_v0.cc @@ -0,0 +1,44 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "nnacl/tensorlist_parameter.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTensorListFromTensorParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tensorList = primitive->value_as_TensorListFromTensor(); + TensorListParameter *TensorList_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); + if (TensorList_param == nullptr) { + MS_LOG(ERROR) << "malloc TensorListParameter failed."; + return nullptr; + } + memset(TensorList_param, 0, sizeof(TensorListParameter)); + TensorList_param->op_parameter_.type_ = schema::PrimitiveType_TensorListFromTensor; + TensorList_param->shape_type_ = (TypeId)(tensorList->shapeType()); + TensorList_param->element_dtype_ = (TypeId)(tensorList->elementDType()); + return reinterpret_cast(TensorList_param); +} +} // namespace + +Registry g_tensorListFromTensorV0ParameterRegistry(schema::v0::PrimitiveType_TensorListFromTensor, + PopulateTensorListFromTensorParameter, SCHEMA_V0); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tensorlistgetitem_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tensorlistgetitem_populate_v0.cc new file mode 100644 index 0000000000..7722fd50ef --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tensorlistgetitem_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/tensorlist_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTensorListGetItemParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tensorList_prim = primitive->value_as_TensorListGetItem(); + TensorListParameter *getItem_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); + if (getItem_param == nullptr) { + MS_LOG(ERROR) << "malloc TensorListParameter failed."; + return nullptr; + } + memset(getItem_param, 0, sizeof(TensorListParameter)); + getItem_param->op_parameter_.type_ = schema::PrimitiveType_TensorListGetItem; + getItem_param->element_dtype_ = (TypeId)tensorList_prim->elementDType(); + return reinterpret_cast(getItem_param); +} +} // namespace + +Registry g_tensorListGetItemV0ParameterRegistry(schema::v0::PrimitiveType_TensorListGetItem, + PopulateTensorListGetItemParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tensorlistreserve_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tensorlistreserve_populate_v0.cc new file mode 100644 index 0000000000..bc311f0f74 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tensorlistreserve_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/tensorlist_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTensorListReserveParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tensorList_prim = primitive->value_as_TensorListReserve(); + TensorListParameter *reserve_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); + if (reserve_param == nullptr) { + MS_LOG(ERROR) << "malloc TensorListParameter failed."; + return nullptr; + } + memset(reserve_param, 0, sizeof(TensorListParameter)); + reserve_param->op_parameter_.type_ = schema::PrimitiveType_TensorListReserve; + reserve_param->element_dtype_ = (TypeId)tensorList_prim->elementDType(); + return reinterpret_cast(reserve_param); +} +} // namespace + +Registry g_tensorListReserveV0ParameterRegistry(schema::v0::PrimitiveType_TensorListReserve, + PopulateTensorListReserveParameter, SCHEMA_V0); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tensorlistsetlitem_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tensorlistsetlitem_populate_v0.cc new file mode 100644 index 0000000000..7f947aae1b --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tensorlistsetlitem_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/tensorlist_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTensorListSetItemParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tensorList_prim = primitive->value_as_TensorListSetItem(); + TensorListParameter *setItem_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); + if (setItem_param == nullptr) { + MS_LOG(ERROR) << "malloc TensorListParameter failed."; + return nullptr; + } + memset(setItem_param, 0, sizeof(TensorListParameter)); + setItem_param->op_parameter_.type_ = schema::PrimitiveType_TensorListSetItem; + setItem_param->element_dtype_ = (TypeId)tensorList_prim->elementDType(); + return reinterpret_cast(setItem_param); +} +} // namespace + +Registry g_tensorListSetItemV0ParameterRegistry(schema::v0::PrimitiveType_TensorListSetItem, + PopulateTensorListSetItemParameter, SCHEMA_V0); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tensorliststack_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tensorliststack_populate_v0.cc new file mode 100644 index 0000000000..cf5495efa4 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tensorliststack_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/tensorlist_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTensorListStackParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tensorList_prim = primitive->value_as_TensorListStack(); + TensorListParameter *stack_param = reinterpret_cast(malloc(sizeof(TensorListParameter))); + if (stack_param == nullptr) { + MS_LOG(ERROR) << "malloc TensorListParameter failed."; + return nullptr; + } + memset(stack_param, 0, sizeof(TensorListParameter)); + stack_param->op_parameter_.type_ = schema::PrimitiveType_TensorListStack; + stack_param->element_dtype_ = (TypeId)tensorList_prim->elementDType(); + stack_param->num_element_ = tensorList_prim->numElements(); + return reinterpret_cast(stack_param); +} +} // namespace + +Registry g_tensorListStackV0ParameterRegistry(schema::v0::PrimitiveType_TensorListStack, + PopulateTensorListStackParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/tile_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/tile_populate_v0.cc new file mode 100644 index 0000000000..1fba50856f --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/tile_populate_v0.cc @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/base/tile_base.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTileParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto tile_prim = primitive->value_as_Tile(); + TileParameter *tile_param = reinterpret_cast(malloc(sizeof(TileParameter))); + if (tile_param == nullptr) { + MS_LOG(ERROR) << "malloc TileParameter failed."; + return nullptr; + } + memset(tile_param, 0, sizeof(TileParameter)); + tile_param->op_parameter_.type_ = schema::PrimitiveType_TileFusion; +#ifdef SUPPORT_TRAIN + auto multiples = tile_prim->multiples(); + tile_param->in_dim_ = multiples->size(); + for (int i = 0; i < tile_param->in_dim_; ++i) { + tile_param->multiples_[i] = *(multiples->begin() + i); + } +#else + if (tile_prim->dims() != nullptr) { + auto dims = tile_prim->dims(); + if (dims != nullptr) { + for (size_t i = 0; i < dims->size(); i++) { + tile_param->dims_[i] = static_cast(dims->Get(i)); + } + } + tile_param->dims_size_ = tile_prim->dims()->size(); + } + +#endif + return reinterpret_cast(tile_param); +} +} // namespace + +Registry g_tileV0ParameterRegistry(schema::v0::PrimitiveType_Tile, PopulateTileParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/topk_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/topk_populate_v0.cc new file mode 100644 index 0000000000..f87f03e590 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/topk_populate_v0.cc @@ -0,0 +1,43 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/topk_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTopKParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto topk_prim = primitive->value_as_TopK(); + TopkParameter *topk_param = reinterpret_cast(malloc(sizeof(TopkParameter))); + if (topk_param == nullptr) { + MS_LOG(ERROR) << "malloc TopkParameter failed."; + return nullptr; + } + memset(topk_param, 0, sizeof(TopkParameter)); + topk_param->op_parameter_.type_ = schema::PrimitiveType_TopKFusion; + + topk_param->k_ = topk_prim->k(); + topk_param->sorted_ = topk_prim->sorted(); + return reinterpret_cast(topk_param); +} +} // namespace + +Registry g_topKV0ParameterRegistry(schema::v0::PrimitiveType_TopK, PopulateTopKParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/transpose_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/transpose_populate_v0.cc new file mode 100644 index 0000000000..defa29b26c --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/transpose_populate_v0.cc @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/transpose.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateTransposeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto transpose_prim = primitive->value_as_Transpose(); + TransposeParameter *transpose_param = reinterpret_cast(malloc(sizeof(TransposeParameter))); + if (transpose_param == nullptr) { + MS_LOG(ERROR) << "malloc TransposeParameter failed."; + return nullptr; + } + memset(transpose_param, 0, sizeof(TransposeParameter)); + + transpose_param->op_parameter_.type_ = schema::PrimitiveType_Transpose; + auto perm_vector_ = transpose_prim->perm(); + int i = 0; + for (auto iter = perm_vector_->begin(); iter != perm_vector_->end(); iter++) { + transpose_param->perm_[i++] = *iter; + } + transpose_param->num_axes_ = i; + transpose_param->perm_size_ = transpose_prim->perm()->size(); + + return reinterpret_cast(transpose_param); +} +} // namespace + +Registry g_transposeV0ParameterRegistry(schema::v0::PrimitiveType_Transpose, PopulateTransposeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/unique_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/unique_populate_v0.cc new file mode 100644 index 0000000000..f7819fa84d --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/unique_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32/unique_fp32.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateUniqueParameter(const void *prim) { + UniqueParameter *unique_param = reinterpret_cast(malloc(sizeof(UniqueParameter))); + if (unique_param == nullptr) { + MS_LOG(ERROR) << "malloc UniqueParameter failed."; + return nullptr; + } + memset(unique_param, 0, sizeof(UniqueParameter)); + unique_param->op_parameter_.type_ = schema::PrimitiveType_Unique; + return reinterpret_cast(unique_param); +} +} // namespace + +Registry g_uniqueV0ParameterRegistry(schema::v0::PrimitiveType_Unique, PopulateUniqueParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/unsorted_segment_sum_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/unsorted_segment_sum_populate_v0.cc new file mode 100644 index 0000000000..27881842f0 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/unsorted_segment_sum_populate_v0.cc @@ -0,0 +1,38 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateUnsortedSegmentSumParameter(const void *prim) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc UnsortedSegmentSum Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = schema::PrimitiveType_UnsortedSegmentSum; + return param; +} +} // namespace + +Registry g_unsortedSegmentSumV0ParameterRegistry(schema::v0::PrimitiveType_UnsortedSegmentSum, + PopulateUnsortedSegmentSumParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/unsqueeze_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/unsqueeze_populate_v0.cc new file mode 100644 index 0000000000..93ea17f0c6 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/unsqueeze_populate_v0.cc @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/unsqueeze_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateUnsqueezeParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto unsqueeze_prim = primitive->value_as_Unsqueeze(); + + UnSqueezeParameter *unsqueeze_param = reinterpret_cast(malloc(sizeof(UnSqueezeParameter))); + if (unsqueeze_param == nullptr) { + MS_LOG(ERROR) << "malloc UnSqueezeParameter failed."; + return nullptr; + } + memset(unsqueeze_param, 0, sizeof(UnSqueezeParameter)); + unsqueeze_param->op_parameter_.type_ = schema::PrimitiveType_Unsqueeze; + auto flatAxis = unsqueeze_prim->axis(); + unsqueeze_param->num_dim_ = flatAxis->size(); + int i = 0; + for (auto iter = flatAxis->begin(); iter != flatAxis->end(); iter++) { + unsqueeze_param->dims_[i++] = *iter; + } + return reinterpret_cast(unsqueeze_param); +} +} // namespace + +Registry g_unsqueezeV0ParameterRegistry(schema::v0::PrimitiveType_Unsqueeze, PopulateUnsqueezeParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc new file mode 100644 index 0000000000..80dbfa3254 --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/unstack_populate_v0.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/unstack_parameter.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateUnstackParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto unstack_prim = primitive->value_as_Unstack(); + UnstackParameter *unstack_param = reinterpret_cast(malloc(sizeof(UnstackParameter))); + if (unstack_param == nullptr) { + MS_LOG(ERROR) << "malloc UnstackParameter failed."; + return nullptr; + } + memset(unstack_param, 0, sizeof(UnstackParameter)); + + unstack_param->op_parameter_.type_ = schema::PrimitiveType_Unstack; + unstack_param->axis_ = unstack_prim->axis(); + return reinterpret_cast(unstack_param); +} +} // namespace + +Registry g_unstackV0ParameterRegistry(schema::v0::PrimitiveType_Unstack, PopulateUnstackParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/where_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/where_populate_v0.cc new file mode 100644 index 0000000000..77e3035d9a --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/where_populate_v0.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +OpParameter *PopulateWhereParameter(const void *prim) { + OpParameter *where_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); + if (where_parameter == nullptr) { + MS_LOG(ERROR) << "malloc Where parameter failed."; + return nullptr; + } + memset(where_parameter, 0, sizeof(OpParameter)); + where_parameter->type_ = schema::PrimitiveType_Where; + return reinterpret_cast(where_parameter); +} +} // namespace + +Registry g_whereV0ParameterRegistry(schema::v0::PrimitiveType_Where, PopulateWhereParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/v0/while_populate_v0.cc b/mindspore/lite/src/ops/populate/v0/while_populate_v0.cc new file mode 100644 index 0000000000..0231f800bf --- /dev/null +++ b/mindspore/lite/src/ops/populate/v0/while_populate_v0.cc @@ -0,0 +1,48 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "schema/model_v0_generated.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +namespace { +typedef struct WhileParemeter { + OpParameter op_parameter_; + int body_subgraph_index; + int cond_subgraph_index; +} WhileParemeter; + +OpParameter *PopulateWhileParameter(const void *prim) { + auto *primitive = static_cast(prim); + auto while_prim = primitive->value_as_While(); + WhileParemeter *while_paremeter = reinterpret_cast(malloc(sizeof(WhileParemeter))); + if (while_paremeter == nullptr) { + MS_LOG(ERROR) << "malloc WhileParemeter failed."; + return nullptr; + } + memset(while_paremeter, 0, sizeof(WhileParemeter)); + + while_paremeter->op_parameter_.type_ = schema::PrimitiveType_While; + while_paremeter->body_subgraph_index = while_prim->bodySubgraphIndex(); + while_paremeter->cond_subgraph_index = while_prim->condSubgraphIndex(); + return reinterpret_cast(while_paremeter); +} +} // namespace + +Registry g_whileV0ParemeterRegistry(schema::v0::PrimitiveType_While, PopulateWhileParameter, SCHEMA_V0); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/where_populate.cc b/mindspore/lite/src/ops/populate/where_populate.cc index a759447ca2..5f1790f839 100644 --- a/mindspore/lite/src/ops/populate/where_populate.cc +++ b/mindspore/lite/src/ops/populate/where_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,23 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" -#include "mindspore/lite/nnacl/fp32/where_fp32.h" namespace mindspore { namespace lite { - -OpParameter *PopulateWhereParameter(const mindspore::lite::PrimitiveC *primitive) { - WhereParameter *where_parameter = reinterpret_cast(malloc(sizeof(WhereParameter))); +namespace { +OpParameter *PopulateWhereParameter(const void *prim) { + OpParameter *where_parameter = reinterpret_cast(malloc(sizeof(OpParameter))); if (where_parameter == nullptr) { MS_LOG(ERROR) << "malloc Where parameter failed."; return nullptr; } memset(where_parameter, 0, sizeof(OpParameter)); - where_parameter->op_parameter_.type_ = primitive->Type(); + auto primitive = static_cast(prim); + where_parameter->type_ = primitive->value_type(); return reinterpret_cast(where_parameter); } -Registry WhereParameterRegistry(schema::PrimitiveType_Where, PopulateWhereParameter); +} // namespace +Registry g_whereParameterRegistry(schema::PrimitiveType_Where, PopulateWhereParameter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/while_populate.cc b/mindspore/lite/src/ops/populate/while_populate.cc index efcb64d177..f040731015 100644 --- a/mindspore/lite/src/ops/populate/while_populate.cc +++ b/mindspore/lite/src/ops/populate/while_populate.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2020 Huawei Technologies Co., Ltd + * Copyright 2019-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,9 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "src/ops/while.h" -#include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" namespace mindspore { @@ -27,19 +24,20 @@ typedef struct WhileParemeter { int cond_subgraph_index; } WhileParemeter; -OpParameter *PopulateWhileParemeter(const mindspore::lite::PrimitiveC *primitive) { +OpParameter *PopulateWhileParemeter(const void *prim) { WhileParemeter *while_paremeter = reinterpret_cast(malloc(sizeof(WhileParemeter))); if (while_paremeter == nullptr) { MS_LOG(ERROR) << "malloc WhileParemeter failed."; return nullptr; } memset(while_paremeter, 0, sizeof(WhileParemeter)); - auto param = reinterpret_cast(const_cast(primitive)); - while_paremeter->op_parameter_.type_ = primitive->Type(); - while_paremeter->body_subgraph_index = param->GetBodySubgraphIndex(); - while_paremeter->cond_subgraph_index = param->GetCondSubgraphIndex(); + auto primitive = static_cast(prim); + auto value = primitive->value_as_While(); + while_paremeter->op_parameter_.type_ = primitive->value_type(); + while_paremeter->body_subgraph_index = value->body_subgraph_index(); + while_paremeter->cond_subgraph_index = value->cond_subgraph_index(); return reinterpret_cast(while_paremeter); } -Registry WhileParemeterRegistry(schema::PrimitiveType_While, PopulateWhileParemeter); +Registry WhileParemeterRegistry(schema::PrimitiveType_While, PopulateWhileParemeter, SCHEMA_CUR); } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/ops/power.cc b/mindspore/lite/src/ops/power.cc deleted file mode 100644 index 492b2ff37f..0000000000 --- a/mindspore/lite/src/ops/power.cc +++ /dev/null @@ -1,133 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/power.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float Power::GetPower() const { return this->primitive_->value.AsPower()->power; } -float Power::GetScale() const { return this->primitive_->value.AsPower()->scale; } -float Power::GetShift() const { return this->primitive_->value.AsPower()->shift; } - -void Power::SetPower(float power) { this->primitive_->value.AsPower()->power = power; } -void Power::SetScale(float scale) { this->primitive_->value.AsPower()->scale = scale; } -void Power::SetShift(float shift) { this->primitive_->value.AsPower()->shift = shift; } - -int Power::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Power; - } - if (this->primitive_->value.type != schema::PrimitiveType_Power) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PowerT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - delete this->primitive_; - this->primitive_ = nullptr; - return RET_ERROR; - } - - if (prim.GetAttr("scale") == nullptr) { - MS_LOG(INFO) << "Power's attr scale is set to default"; - attr->scale = 1.0f; - } else { - attr->scale = GetValue(prim.GetAttr("scale")); - } - if (prim.GetAttr("power") == nullptr) { - MS_LOG(INFO) << "Power's attr power is set to default"; - attr->power = 1.0f; - } else { - attr->power = GetValue(prim.GetAttr("power")); - } - if (prim.GetAttr("shift") == nullptr) { - MS_LOG(INFO) << "Power's attr shift is set to default"; - attr->shift = 0; - } else { - attr->shift = GetValue(prim.GetAttr("shift")); - } - this->primitive_->value.value = attr; - } - return RET_OK; -} - -#else - -float Power::GetPower() const { return this->primitive_->value_as_Power()->power(); } -float Power::GetScale() const { return this->primitive_->value_as_Power()->scale(); } -float Power::GetShift() const { return this->primitive_->value_as_Power()->shift(); } -int Power::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Power(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Power return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreatePower(*fbb, attr->power(), attr->scale(), attr->shift()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Power, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PowerCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry PowerRegistry(schema::PrimitiveType_Power, PowerCreator); -#endif - -int Power::InferShape(std::vector inputs, std::vector outputs) { - MS_ASSERT(this->primitive_ != nullptr); - auto x_tensor = inputs.at(0); - MS_ASSERT(x_tensor != nullptr); - Tensor *exp_tensor = nullptr; - if (inputs.size() == 2) { - exp_tensor = inputs.at(1); - MS_ASSERT(exp_tensor != nullptr); - } - auto output_tensor = outputs.at(0); - MS_ASSERT(output_tensor != nullptr); - output_tensor->set_data_type(x_tensor->data_type()); - output_tensor->set_format(x_tensor->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if (exp_tensor != nullptr) { - if ((exp_tensor->shape().size() > 1 && exp_tensor->shape() != x_tensor->shape()) || - (exp_tensor->shape().size() == 1 && exp_tensor->shape().at(0) != 1) || - exp_tensor->data_type() != x_tensor->data_type()) { - MS_LOG(ERROR) << "Power inputs shape or type is not equal!"; - return RET_INPUT_TENSOR_ERROR; - } - } - - output_tensor->set_shape(x_tensor->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/power.h b/mindspore/lite/src/ops/power.h deleted file mode 100644 index 2da7dcb86a..0000000000 --- a/mindspore/lite/src/ops/power.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_POWER_H_ -#define LITE_MINDSPORE_LITE_C_OPS_POWER_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Power : public PrimitiveC { - public: - Power() = default; - ~Power() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Power, PrimitiveC); - explicit Power(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetPower(float power); - void SetScale(float scale); - void SetShift(float shift); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - float GetPower() const; - float GetScale() const; - float GetShift() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_POWER_H_ diff --git a/mindspore/lite/src/ops/power_grad.cc b/mindspore/lite/src/ops/power_grad.cc deleted file mode 100644 index e95a3fcabf..0000000000 --- a/mindspore/lite/src/ops/power_grad.cc +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/power_grad.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -float PowerGrad::GetPower() const { return this->primitive_->value.AsPowerGrad()->power; } -float PowerGrad::GetScale() const { return this->primitive_->value.AsPowerGrad()->scale; } -float PowerGrad::GetShift() const { return this->primitive_->value.AsPowerGrad()->shift; } - -void PowerGrad::SetPower(float power) { this->primitive_->value.AsPowerGrad()->power = power; } -void PowerGrad::SetScale(float scale) { this->primitive_->value.AsPowerGrad()->scale = scale; } -void PowerGrad::SetShift(float shift) { this->primitive_->value.AsPowerGrad()->shift = shift; } -int PowerGrad::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_PowerGrad; - } - if (this->primitive_->value.type != schema::PrimitiveType_PowerGrad) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::PowerGradT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - attr->power = GetValue(prim.GetAttr("power")); - attr->scale = GetValue(prim.GetAttr("scale")); - attr->shift = GetValue(prim.GetAttr("shift")); - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -float PowerGrad::GetPower() const { return this->primitive_->value_as_PowerGrad()->power(); } -float PowerGrad::GetScale() const { return this->primitive_->value_as_PowerGrad()->scale(); } -float PowerGrad::GetShift() const { return this->primitive_->value_as_PowerGrad()->shift(); } - -int PowerGrad::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto attr = primitive->value_as_PowerGrad(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_PowerGrad return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreatePowerGrad(*fbb, attr->power(), attr->scale(), attr->shift()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_PowerGrad, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PowerGradCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry PowerGradRegistry(schema::PrimitiveType_PowerGrad, PowerGradCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/power_grad.h b/mindspore/lite/src/ops/power_grad.h deleted file mode 100644 index 48e67994fd..0000000000 --- a/mindspore/lite/src/ops/power_grad.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_POWER_GRAD_H_ -#define MINDSPORE_LITE_SRC_OPS_POWER_GRAD_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class PowerGrad : public PrimitiveC { - public: - PowerGrad() = default; - ~PowerGrad() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(PowerGrad, PrimitiveC); - explicit PowerGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetPower(float power); - void SetScale(float scale); - void SetShift(float shift); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - float GetPower() const; - float GetScale() const; - float GetShift() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_POWER_GRAD_H_ diff --git a/mindspore/lite/src/ops/primitive_c.cc b/mindspore/lite/src/ops/primitive_c.cc deleted file mode 100644 index 3b7965c472..0000000000 --- a/mindspore/lite/src/ops/primitive_c.cc +++ /dev/null @@ -1,1178 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/primitive_c.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#include -#include "tools/converter/quantizer/quantize_util.h" -#include "src/ops/assert_op.h" -#include "src/ops/space_to_batch.h" -#include "src/ops/space_to_batch_nd.h" -#include "src/ops/conv2d.h" -#include "src/ops/roi_pooling.h" -#include "src/ops/topk.h" -#include "src/ops/broadcast_to.h" -#include "src/ops/unsqueeze.h" -#include "src/ops/unstack.h" -#include "src/ops/depth_to_space.h" -#include "src/ops/batch_to_space.h" -#include "src/ops/prior_box.h" -#include "src/ops/lstm.h" -#include "src/ops/softmax.h" -#include "src/ops/activation.h" -#include "src/ops/deconv2d.h" -#include "src/ops/reduce.h" -#include "src/ops/pooling.h" -#include "src/ops/fused_batchnorm.h" -#include "src/ops/batch_norm.h" -#include "src/ops/power.h" -#include "src/ops/range.h" -#include "src/ops/add.h" -#include "src/ops/sub.h" -#include "src/ops/div.h" -#include "src/ops/bias_add.h" -#include "src/ops/expand_dims.h" -#include "src/ops/full_connection.h" -#include "src/ops/shape.h" -#include "src/ops/elu.h" -#include "src/ops/embedding_lookup.h" -#include "src/ops/quant_dtype_cast.h" -#include "src/ops/matmul.h" -#include "src/ops/resize.h" -#include "src/ops/tile.h" -#include "src/ops/one_hot.h" -#include "src/ops/space_to_depth.h" -#include "src/ops/split.h" -#include "src/ops/argmax.h" -#include "src/ops/argmin.h" -#include "src/ops/cast.h" -#include "src/ops/reshape.h" -#include "src/ops/scale.h" -#include "src/ops/concat.h" -#include "src/ops/nchw2nhwc.h" -#include "src/ops/slice.h" -#include "src/ops/squeeze.h" -#include "src/ops/flatten.h" -#include "src/ops/nhwc2nchw.h" -#include "src/ops/stack.h" -#include "src/ops/crop.h" -#include "src/ops/addn.h" -#include "src/ops/gather.h" -#include "src/ops/gather_nd.h" -#include "src/ops/local_response_normalization.h" -#include "src/ops/pad.h" -#include "src/ops/p_relu.h" -#include "src/ops/leaky_relu.h" -#include "src/ops/reverse_sequence.h" -#include "src/ops/dedepthwise_conv2d.h" -#include "src/ops/depthwise_conv2d.h" -#include "src/ops/mul.h" -#include "src/ops/eltwise.h" -#include "src/ops/fill.h" -#include "src/ops/transpose.h" -#include "src/ops/log.h" -#include "src/ops/abs.h" -#include "src/ops/sin.h" -#include "src/ops/cos.h" -#include "src/ops/sqrt.h" -#include "src/ops/square.h" -#include "src/ops/exp.h" -#include "src/ops/rsqrt.h" -#include "src/ops/maximum.h" -#include "src/ops/minimum.h" -#include "src/ops/strided_slice.h" -#include "src/ops/reverse.h" -#include "src/ops/logical_and.h" -#include "src/ops/logical_or.h" -#include "src/ops/logical_not.h" -#include "src/ops/floor_div.h" -#include "src/ops/floor_mod.h" -#include "src/ops/mod.h" -#include "src/ops/equal.h" -#include "src/ops/not_equal.h" -#include "src/ops/less.h" -#include "src/ops/less_equal.h" -#include "src/ops/greater_equal.h" -#include "src/ops/greater.h" -#include "src/ops/floor.h" -#include "src/ops/squared_difference.h" -#include "src/ops/ceil.h" -#include "src/ops/round.h" -#include "src/ops/unique.h" -#include "src/ops/zeros_like.h" -#include "src/ops/return.h" -#include "src/ops/where.h" -#include "src/ops/scatter_nd.h" -#include "src/ops/constant_of_shape.h" -#include "src/ops/dequant.h" -#include "src/ops/make_tuple.h" -#include "src/ops/quant.h" -#include "src/ops/tuple_get_item.h" -#include "src/ops/l2_norm.h" -#include "src/ops/neg.h" -#include "src/ops/sparse_to_dense.h" -#include "src/ops/detection_post_process.h" -#include "src/ops/dropout.h" -#include "src/ops/real_div.h" -#include "src/ops/lsh_projection.h" -#include "src/ops/hashtable_lookup.h" -#include "src/ops/skip_gram.h" -#include "src/ops/clip.h" -#include "src/ops/adder.h" -#include "src/ops/custom_predict.h" -#include "src/ops/custom_normalize.h" -#include "src/ops/custom_extract_features.h" -#include "src/ops/upsample.h" -#include "src/ops/layer_norm.h" -#include "src/ops/non_max_suppression.h" -#include "src/ops/rfft.h" -#include "src/ops/fft_real.h" -#include "src/ops/fft_imag.h" -#include "src/ops/audio_spectrogram.h" -#include "src/ops/mfcc.h" -#include "src/ops/identity.h" -#include "src/ops/instance_norm.h" -#include "src/ops/while.h" -#include "src/ops/oneslike.h" -#include "src/ops/unsorted_segment_sum.h" -#include "src/ops/reciprocal.h" -#include "src/ops/constant.h" -#include "src/ops/tensorlist_fromtensor.h" -#include "src/ops/tensorlist_getitem.h" -#include "src/ops/tensorlist_setitem.h" -#include "src/ops/tensorlist_reserve.h" -#include "src/ops/tensorlist_stack.h" -#include "src/ops/merge.h" -#include "src/ops/switch.h" -#include "src/ops/partial.h" -#include "src/ops/if.h" -#include "src/ops/select.h" -#include "src/ops/gelu.h" -#include "src/ops/gru.h" -#include "src/ops/size.h" -#include "src/ops/random_standard_normal.h" -#include "src/ops/invert_permutation.h" -#include "src/ops/crop_and_resize.h" -#include "src/ops/nonzero.h" -#include "src/ops/erf.h" -#include "src/ops/lin_space.h" -#include "src/ops/uniform_real.h" -#include "src/ops/rank.h" -#include "src/ops/is_finite.h" -#include "src/ops/neg_grad.h" -#include "src/ops/activation_grad.h" -#include "src/ops/apply_momentum.h" -#include "src/ops/bias_grad.h" -#include "src/ops/pooling_grad.h" -#include "src/ops/conv2d_grad_filter.h" -#include "src/ops/conv2d_grad_input.h" -#include "src/ops/group_conv2d_grad_input.h" -#include "src/ops/power_grad.h" -#include "src/ops/softmax_cross_entropy.h" -#include "src/ops/sparse_softmax_cross_entropy.h" -#include "src/ops/bn_grad.h" -#include "src/ops/arithmetic_grad.h" -#include "src/ops/depend.h" -#include "src/ops/flatten_grad.h" -#include "src/ops/log_grad.h" -#include "src/ops/abs_grad.h" -#include "src/ops/sgd.h" -#include "src/ops/adam.h" -#include "src/ops/assign.h" -#include "src/ops/dropout_grad.h" -#include "src/ops/maximum_grad.h" -#include "src/ops/minimum_grad.h" -#include "src/ops/control_depend.h" -#include "src/ops/assign_add.h" -#include "src/ops/binary_cross_entropy.h" -#include "src/ops/binary_cross_entropy_grad.h" -#include "src/ops/smooth_l1_loss.h" -#include "src/ops/smooth_l1_loss_grad.h" -#include "src/ops/sigmoid_cross_entropy_with_logits.h" -#include "src/ops/sigmoid_cross_entropy_with_logits_grad.h" -#include "src/ops/strided_slice_grad.h" -#endif -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector CastToInt(const ValuePtr &value) { - if (value == nullptr) { - MS_LOG(WARNING) << "valueptr is nullptr."; - return {}; - } - std::vector cur_value; - if (utils::isa(value)) { - if (value->cast()->value().front()->type()->number_type() == kNumberTypeInt64) { - auto origin_value = GetValue>(value); - for (size_t index = 0; index < origin_value.size(); ++index) { - cur_value.push_back(static_cast(origin_value[index])); - } - } else { - cur_value = GetValue>(value); - } - } else { - if (value->type()->number_type() == kNumberTypeInt64) { - cur_value.push_back(static_cast(GetValue(value))); - } else { - cur_value.push_back(GetValue(value)); - } - } - return cur_value; -} - -void PrimitiveC::CalFloatScopeByMeanAndStddev(const double &mean, const double &stdDev, float *mMin, float *mMax) { - const float qmin = 0; - const float qmax = 255; - *mMin = static_cast((qmin - mean) / stdDev); - *mMax = static_cast((qmax - mean) / stdDev); -} - -void PrimitiveC::FillDefaultInputQuantParamIfNeed(const size_t &inputSize) { - std::vector quants; - schema::QuantParamT quantParam; - - if (input_quant_param_.size() == kDoubleNum) { - quants.clear(); - quantParam.min = 0.0; - quantParam.max = 0.0; - quantParam.zeroPoint = 0; - quantParam.scale = input_quant_param_.at(0).at(0).scale * input_quant_param_.at(1).at(0).scale; - quants.emplace_back(quantParam); - input_quant_param_.emplace_back(quants); - } - // fill input_quant_param_ by not inited quant_parm - if (input_quant_param_.size() < inputSize) { - schema::QuantParamT tmpQuantParam; - quants.emplace_back(tmpQuantParam); - input_quant_param_.insert(input_quant_param_.end(), inputSize - input_quant_param_.size(), quants); - } -} - -void PrimitiveC::PopulaterInputQuantParam(const Primitive &prim, const std::vector &inputs, - bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam) { - std::vector quants; - schema::QuantParamT quantParam; - auto inputMin = prim.GetAttr("input_minq"); - auto inputMax = prim.GetAttr("input_maxq"); - if (inputMin != nullptr && inputMax != nullptr) { - auto inputMinPtr = inputMin->cast(); - auto inputMaxPtr = inputMax->cast(); - auto *minBuf = static_cast(inputMinPtr->data_c()); - auto *maxBuf = static_cast(inputMaxPtr->data_c()); - quantParam.min = *minBuf; - quantParam.max = *maxBuf; - auto ret = quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, - numbitsRangeQuantParam); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Can't calculate quant parameters"; - return; - } - quants.emplace_back(quantParam); - input_quant_param_.emplace_back(quants); - } - - quants.clear(); - auto filterMin = prim.GetAttr("filter_minq"); - auto filterMax = prim.GetAttr("filter_maxq"); - if (filterMin != nullptr && filterMax != nullptr) { - auto filterMinPtr = filterMin->cast(); - auto filterMaxPtr = filterMax->cast(); - auto *minBuf = static_cast(filterMinPtr->data_c()); - auto *maxBuf = static_cast(filterMaxPtr->data_c()); - quantParam.min = FLT_MAX; - quantParam.max = FLT_MIN; - for (int i = 0; i < filterMinPtr->ElementsNum(); ++i) { - quantParam.min = (*(minBuf) < quantParam.min) ? (*minBuf) : quantParam.min; - quantParam.max = (*(maxBuf) > quantParam.max) ? (*maxBuf) : quantParam.max; - minBuf++; - maxBuf++; - } - auto ret = quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, true, numbitsRangeQuantParam); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Can't calculate quant parameters"; - return; - } - quants.emplace_back(quantParam); - input_quant_param_.emplace_back(quants); - } - FillDefaultInputQuantParamIfNeed(inputs.size()); -} - -void PrimitiveC::PopulaterOutputQuantParam(const Primitive &prim, bool narrowRangeQuantParam, - int32_t numbitsRangeQuantParam) { - std::vector quants; - schema::QuantParamT quantParam; - auto outputMin = prim.GetAttr("output_minq"); - auto outputMax = prim.GetAttr("output_maxq"); - if (outputMin != nullptr && outputMax != nullptr) { - auto outputMinPtr = outputMin->cast(); - auto outputMaxPtr = outputMax->cast(); - auto *minBuf = static_cast(outputMinPtr->data_c()); - auto *maxBuf = static_cast(outputMaxPtr->data_c()); - quantParam.min = *minBuf; - quantParam.max = *maxBuf; - auto ret = quant::CalQuantizationParams(&quantParam, quantParam.min, quantParam.max, narrowRangeQuantParam, - numbitsRangeQuantParam); - if (ret != RET_OK) { - MS_LOG(ERROR) << "Can't calculate quant parameters"; - return; - } - quants.emplace_back(quantParam); - output_quant_param_.emplace_back(quants); - } else { - schema::QuantParamT tmpQuantParam; - quants.emplace_back(tmpQuantParam); - output_quant_param_.emplace_back(quants); - } -} - -void PrimitiveC::PopulaterQuantParam(const Primitive &prim, const std::vector &inputs) { - auto narrow_range = prim.GetAttr("narrow_range"); - bool narrowRangeQuantParam = false; - if (narrow_range != nullptr) { - if (utils::isa(narrow_range)) { - auto narrow_range_tensor = narrow_range->cast(); - narrowRangeQuantParam = *reinterpret_cast(narrow_range_tensor->data_c()); - } else if (utils::isa::type>(narrow_range)) { - narrowRangeQuantParam = GetValue(narrow_range); - } else { - MS_LOG(ERROR) << "valueptr is invalid."; - return; - } - } - auto num_bits = prim.GetAttr("num_bits"); - int32_t numbitsRangeQuantParam = 8; - if (num_bits != nullptr) { - if (utils::isa(num_bits)) { - auto num_bits_tensor = num_bits->cast(); - numbitsRangeQuantParam = *reinterpret_cast(num_bits_tensor->data_c()); - } else if (utils::isa::type>(num_bits)) { - numbitsRangeQuantParam = GetValue(num_bits); - } - } - PopulaterInputQuantParam(prim, inputs, narrowRangeQuantParam, numbitsRangeQuantParam); - PopulaterOutputQuantParam(prim, narrowRangeQuantParam, numbitsRangeQuantParam); -} - -void PrimitiveC::GetAttrDataFromInput(const AnfNodePtr &inputNode, std::vector *data) { - if (inputNode->isa()) { - auto valNode = inputNode->cast(); - MS_ASSERT(valNode != nullptr); - auto val = valNode->value(); - MS_ASSERT(val != nullptr); - if (val->isa()) { - auto tuple = val->cast(); - MS_ASSERT(tuple != nullptr); - for (size_t i = 0; i < tuple->size(); i++) { - auto elem = tuple->value().at(i); - MS_ASSERT(elem != nullptr); - data->emplace_back(CastToInt(elem).front()); - } - } - } -} - -schema::PrimitiveT *PrimitiveC::primitiveT() const { return this->primitive_; } - -void PrimitiveC::ClearPrimitiveT() { this->primitive_ = nullptr; } - -void PrimitiveC::set_input_quant_params(const std::vector> &input_quant_param) { - this->input_quant_param_ = input_quant_param; -} - -void PrimitiveC::set_input_quant_param(const size_t &index, const std::vector &input_quant_param) { - if (index >= this->input_quant_param_.size()) { - this->input_quant_param_.resize(index + 1); - } - this->input_quant_param_.at(index) = input_quant_param; -} - -void PrimitiveC::set_output_quant_params(const std::vector> &output_quant_param) { - this->output_quant_param_ = output_quant_param; -} - -void PrimitiveC::set_output_quant_param(const size_t &index, - const std::vector &output_quant_param) { - MS_ASSERT(index < this->output_quant_param_.size()); - this->output_quant_param_.at(index) = output_quant_param; -} - -bool PrimitiveC::IsInputQuantParamsInited() { - if (this->input_quant_param_.empty()) { - return false; - } - for (auto &quant_param : this->input_quant_param_) { - if (!quant_param.front().inited) { - return false; - } - } - return true; -} - -bool PrimitiveC::IsOutputQuantParamsInited() { - if (this->output_quant_param_.empty()) { - return false; - } - for (auto &quant_param : this->output_quant_param_) { - if (!quant_param.front().inited) { - return false; - } - } - return true; -} - -void PrimitiveC::ClearInputOutputQuantParam() { - input_quant_param_.clear(); - output_quant_param_.clear(); -} - -void PrimitiveC::AddInputQuantParam(const std::vector &quant_param) { - this->input_quant_param_.emplace_back(quant_param); -} -std::vector> PrimitiveC::input_quant_params() const { return input_quant_param_; } - -void PrimitiveC::AddOutputQuantParam(const std::vector &quant_param) { - this->output_quant_param_.emplace_back(quant_param); -} -std::vector> PrimitiveC::output_quant_params() const { return output_quant_param_; } - -void PrimitiveC::set_quant_type(const schema::QuantType &quant_type) { this->quant_type_ = quant_type; } - -schema::QuantType PrimitiveC::quant_type() const { return quant_type_; } - -bool PrimitiveC::enable_huffman_code() const { return enable_huffman_code_; } - -void PrimitiveC::set_enable_huffman_code(bool enable_huffman_code) { this->enable_huffman_code_ = enable_huffman_code; } - -std::shared_ptr GetReturnPrim() { - auto return_primitiveT = new (std::nothrow) schema::PrimitiveT; - if (return_primitiveT == nullptr) { - MS_LOG(ERROR) << "new PrimitiveT failed"; - return nullptr; - } - return_primitiveT->value.type = schema::PrimitiveType_Return; - return_primitiveT->value.value = new (std::nothrow) schema::ReturnT; - if (return_primitiveT->value.value == nullptr) { - MS_LOG(ERROR) << "new ReturnT failed"; - delete (return_primitiveT); - return nullptr; - } - return std::make_shared(return_primitiveT); -} - -std::shared_ptr GetMakeTuplePrim() { - auto make_tuple_primitiveT = new (std::nothrow) schema::PrimitiveT; - if (make_tuple_primitiveT == nullptr) { - MS_LOG(ERROR) << "new PrimitiveT failed"; - return nullptr; - } - make_tuple_primitiveT->value.type = schema::PrimitiveType_MakeTuple; - make_tuple_primitiveT->value.value = new (std::nothrow) schema::MakeTupleT; - if (make_tuple_primitiveT->value.value == nullptr) { - MS_LOG(ERROR) << "new MakeTupleT failed"; - delete (make_tuple_primitiveT); - return nullptr; - } - return std::make_shared(make_tuple_primitiveT); -} - -std::shared_ptr GetTupleGetItemPrim() { - auto tuple_get_item_primitiveT = new (std::nothrow) schema::PrimitiveT(); - if (tuple_get_item_primitiveT == nullptr) { - MS_LOG(ERROR) << "new PrimitiveT failed"; - return nullptr; - } - tuple_get_item_primitiveT->value.type = schema::PrimitiveType_TupleGetItem; - tuple_get_item_primitiveT->value.value = new (std::nothrow) schema::TupleGetItemT; - if (tuple_get_item_primitiveT->value.value == nullptr) { - MS_LOG(ERROR) << "new TupleGetItemT failed"; - delete (tuple_get_item_primitiveT); - return nullptr; - } - return std::make_shared(tuple_get_item_primitiveT); -} - -template ::value>> -std::shared_ptr NewPrimitiveC(const mindspore::Primitive &prim, const std::vector &inputs, - const schema::QuantType &quantType, bool train_flag = false) { - auto primc = std::make_shared(); - if (primc == nullptr) { - MS_LOG(ERROR) << "make_shared PrimitiveC failed"; - return nullptr; - } - primc->set_quant_type(quantType); - primc->set_train_flag(train_flag); - auto ret = primc->UnPackAttr(prim, inputs); - if (ret != RET_OK) { - MS_LOG(ERROR) << "UnPackAttr failed"; - return nullptr; - } - return primc; -} - -std::shared_ptr PrimitiveC::Create(const Primitive &prim, const std::vector &inputs, - const schema::QuantType &quantType, bool train_flag) { - const auto &op_type = prim.name(); - if (op_type == "ReLU" || op_type == "ReLU6" || op_type == "Sigmoid" || op_type == "HSwish" || op_type == "HSigmoid") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Abs") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "AddN") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "BatchNorm") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "BiasAdd") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Concat") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Conv2D") { - return NewPrimitiveC(prim, inputs, quantType, train_flag); - } else if (op_type == "Cos") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "DepthwiseConv2dNative" || op_type == "DepthwiseConv2D") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Dequant") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Flatten") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "FloorDiv") { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "FusedBatchNorm") || (op_type == "FusedBatchNormEx")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "make_tuple") || (op_type == "MakeTuple")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "MatMul" || op_type == "BatchMatMul") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Mul") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "MaxPool" || op_type == "AvgPool") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Quant") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "RealDiv") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Reciprocal") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceMax") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceMean") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceMin") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceProd") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceSum") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ReduceSumSquare") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Reshape") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Rsqrt") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Sin") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Slice") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Squeeze") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "TensorAdd") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Transpose") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Elu") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Log") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Exp") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Neg") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "DeConv2D") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "tuple_getitem") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Softmax") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "StridedSlice") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Cast") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Maximum") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Split") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "OneHot") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Dropout") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "While") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "MirrorPad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "InstanceNorm") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Gather") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "OnesLike") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Pow") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Sub") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ExpandDims") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "UnsortedSegmentSum") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ResizeNearestNeighbor") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ResizeBilinear") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Floor") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Minimum") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Div") { - return NewPrimitiveC
(prim, inputs, quantType); - } else if (op_type == "Tanh") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Equal") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "TopK") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Mod") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ArgMin" || op_type == "ArgMinWithValue") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Range") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Tile") { - return NewPrimitiveC(prim, inputs, quantType, train_flag); - } else if (op_type == "GatherNd") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Square") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Sqrt") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Greater") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Switch") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Partial") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Merge") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "LayerNorm") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ArgMax" || op_type == "ArgMaxWithValue") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Gelu") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SoftmaxCrossEntropyWithLogits") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SparseSoftmaxCrossEntropyWithLogits") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "BiasAddGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ApplyMomentum") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Depend") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "ControlDepend") { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "ReluGrad" || op_type == "ReLU6Grad" || op_type == "SigmoidGrad" || - op_type == "HSigmoidGrad" || op_type == "HSwishGrad")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "MaxPoolGrad") || (op_type == "AvgPoolGrad") || (op_type == "AvgPoolGradGpu") || - (op_type == "AvgPoolGradCpu")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Conv2DBackpropFilter") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Conv2DBackpropInput" && train_flag) { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "BatchNormGrad") || (op_type == "FusedBatchNormGradEx")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "FlattenGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if ((op_type == "FusedBatchNormGrad") || (op_type == "FusedBatchNormGradCpu")) { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "PowerGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SGD") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Adam") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Assign") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "DropoutGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "MaximumGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "MinimumGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "AssignAdd") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "BinaryCrossEntropy") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "BinaryCrossEntropyGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SmoothL1Loss") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SmoothL1LossGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SigmoidCrossEntropyWithLogits") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "SigmoidCrossEntropyWithLogitsGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Pad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "StridedSliceGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "AbsGrad") { - return NewPrimitiveC(prim, inputs, quantType); - } else if (op_type == "Conv2DBackpropInput" && !train_flag) { - return NewPrimitiveC(prim, inputs, quantType); - } else { - MS_LOG(ERROR) << "Unsupported primitive type in Create : " << op_type; - return nullptr; - } -} - -PrimitiveC *PrimitiveC::Create(mindspore::schema::PrimitiveT *primitive) { - MS_ASSERT(primitive != nullptr); - auto op_type = primitive->value.type; - switch (op_type) { - case schema::PrimitiveType_SoftMax: - return new (std::nothrow) SoftMax(primitive); - case schema::PrimitiveType_Activation: - return new (std::nothrow) Activation(primitive); - case schema::PrimitiveType_Conv2D: - return new (std::nothrow) Conv2D(primitive); - case schema::PrimitiveType_DeConv2D: - return new (std::nothrow) DeConv2D(primitive); - case schema::PrimitiveType_Reduce: - return new (std::nothrow) Reduce(primitive); - case schema::PrimitiveType_Pooling: - return new (std::nothrow) Pooling(primitive); - case schema::PrimitiveType_ROIPooling: - return new (std::nothrow) ROIPooling(primitive); - case schema::PrimitiveType_DepthwiseConv2D: - return new (std::nothrow) DepthwiseConv2D(primitive); - case schema::PrimitiveType_FusedBatchNorm: - return new (std::nothrow) FusedBatchNorm(primitive); - case schema::PrimitiveType_BatchNorm: - return new (std::nothrow) BatchNorm(primitive); - case schema::PrimitiveType_FullConnection: - return new (std::nothrow) FullConnection(primitive); - case schema::PrimitiveType_Power: - return new (std::nothrow) Power(primitive); - case schema::PrimitiveType_Pad: - return new (std::nothrow) Pad(primitive); - case schema::PrimitiveType_Range: - return new (std::nothrow) Range(primitive); - case schema::PrimitiveType_Mul: - return new (std::nothrow) Mul(primitive); - case schema::PrimitiveType_Add: - return new (std::nothrow) Add(primitive); - case schema::PrimitiveType_Sub: - return new (std::nothrow) Sub(primitive); - case schema::PrimitiveType_Div: - return new (std::nothrow) Div(primitive); - case schema::PrimitiveType_BiasAdd: - return new (std::nothrow) BiasAdd(primitive); - case schema::PrimitiveType_ExpandDims: - return new (std::nothrow) ExpandDims(primitive); - case schema::PrimitiveType_ArgMax: - return new (std::nothrow) ArgMax(primitive); - case schema::PrimitiveType_ArgMin: - return new (std::nothrow) ArgMin(primitive); - case schema::PrimitiveType_Cast: - return new (std::nothrow) Cast(primitive); - case schema::PrimitiveType_Reshape: - return new (std::nothrow) Reshape(primitive); - case schema::PrimitiveType_Scale: - return new (std::nothrow) Scale(primitive); - case schema::PrimitiveType_Eltwise: - return new (std::nothrow) Eltwise(primitive); - case schema::PrimitiveType_Ceil: - return new (std::nothrow) Ceil(primitive); - case schema::PrimitiveType_Concat: - return new (std::nothrow) Concat(primitive); - case schema::PrimitiveType_Fill: - return new (std::nothrow) Fill(primitive); - case schema::PrimitiveType_Nhwc2Nchw: - return new (std::nothrow) Nhwc2Nchw(primitive); - case schema::PrimitiveType_Nchw2Nhwc: - return new (std::nothrow) Nchw2Nhwc(primitive); - case schema::PrimitiveType_Transpose: - return new (std::nothrow) Transpose(primitive); - case schema::PrimitiveType_Slice: - return new (std::nothrow) Slice(primitive); - case schema::PrimitiveType_Squeeze: - return new (std::nothrow) Squeeze(primitive); - case schema::PrimitiveType_Flatten: - return new (std::nothrow) Flatten(primitive); - case schema::PrimitiveType_Stack: - return new (std::nothrow) Stack(primitive); - case schema::PrimitiveType_Crop: - return new (std::nothrow) Crop(primitive); - case schema::PrimitiveType_SquaredDifference: - return new (std::nothrow) SquaredDifference(primitive); - case schema::PrimitiveType_AddN: - return new (std::nothrow) AddN(primitive); - case schema::PrimitiveType_Abs: - return new (std::nothrow) Abs(primitive); - case schema::PrimitiveType_Sin: - return new (std::nothrow) Sin(primitive); - case schema::PrimitiveType_Cos: - return new (std::nothrow) Cos(primitive); - case schema::PrimitiveType_Log: - return new (std::nothrow) Log(primitive); - case schema::PrimitiveType_Sqrt: - return new (std::nothrow) Sqrt(primitive); - case schema::PrimitiveType_Rsqrt: - return new (std::nothrow) Rsqrt(primitive); - case schema::PrimitiveType_Square: - return new (std::nothrow) Square(primitive); - case schema::PrimitiveType_Exp: - return new (std::nothrow) Exp(primitive); - case schema::PrimitiveType_Gather: - return new (std::nothrow) Gather(primitive); - case schema::PrimitiveType_GatherNd: - return new (std::nothrow) GatherNd(primitive); - case schema::PrimitiveType_LocalResponseNormalization: - return new (std::nothrow) LocalResponseNormalization(primitive); - case schema::PrimitiveType_Maximum: - return new (std::nothrow) Maximum(primitive); - case schema::PrimitiveType_Minimum: - return new (std::nothrow) Minimum(primitive); - case schema::PrimitiveType_StridedSlice: - return new (std::nothrow) StridedSlice(primitive); - case schema::PrimitiveType_LeakyReLU: - return new (std::nothrow) LeakyReLU(primitive); - case schema::PrimitiveType_PReLU: - return new (std::nothrow) PReLU(primitive); - case schema::PrimitiveType_Round: - return new (std::nothrow) Round(primitive); - case schema::PrimitiveType_Reverse: - return new (std::nothrow) Reverse(primitive); - case schema::PrimitiveType_ReverseSequence: - return new (std::nothrow) ReverseSequence(primitive); - case schema::PrimitiveType_LogicalAnd: - return new (std::nothrow) LogicalAnd(primitive); - case schema::PrimitiveType_LogicalOr: - return new (std::nothrow) LogicalOr(primitive); - case schema::PrimitiveType_LogicalNot: - return new (std::nothrow) LogicalNot(primitive); - case schema::PrimitiveType_FloorDiv: - return new (std::nothrow) FloorDiv(primitive); - case schema::PrimitiveType_FloorMod: - return new (std::nothrow) FloorMod(primitive); - case schema::PrimitiveType_Mod: - return new (std::nothrow) Mod(primitive); - case schema::PrimitiveType_Equal: - return new (std::nothrow) Equal(primitive); - case schema::PrimitiveType_NotEqual: - return new (std::nothrow) NotEqual(primitive); - case schema::PrimitiveType_Less: - return new (std::nothrow) Less(primitive); - case schema::PrimitiveType_LessEqual: - return new (std::nothrow) LessEqual(primitive); - case schema::PrimitiveType_Greater: - return new (std::nothrow) Greater(primitive); - case schema::PrimitiveType_GreaterEqual: - return new (std::nothrow) GreaterEqual(primitive); - case schema::PrimitiveType_Floor: - return new (std::nothrow) Floor(primitive); - case schema::PrimitiveType_Split: - return new (std::nothrow) Split(primitive); - case schema::PrimitiveType_OneHot: - return new (std::nothrow) OneHot(primitive); - case schema::PrimitiveType_PriorBox: - return new (std::nothrow) PriorBox(primitive); - case schema::PrimitiveType_SpaceToDepth: - return new (std::nothrow) SpaceToDepth(primitive); - case schema::PrimitiveType_Tile: - return new (std::nothrow) Tile(primitive); - case schema::PrimitiveType_Resize: - return new (std::nothrow) Resize(primitive); - case schema::PrimitiveType_Unstack: - return new (std::nothrow) Unstack(primitive); - case schema::PrimitiveType_Unique: - return new (std::nothrow) Unique(primitive); - case schema::PrimitiveType_TopK: - return new (std::nothrow) TopK(primitive); - case schema::PrimitiveType_MatMul: - return new (std::nothrow) MatMul(primitive); - case schema::PrimitiveType_QuantDTypeCast: - return new (std::nothrow) QuantDTypeCast(primitive); - case schema::PrimitiveType_EmbeddingLookup: - return new (std::nothrow) EmbeddingLookup(primitive); - case schema::PrimitiveType_Elu: - return new (std::nothrow) Elu(primitive); - case schema::PrimitiveType_DeDepthwiseConv2D: - return new (std::nothrow) DeDepthwiseConv2D(primitive); - case schema::PrimitiveType_Shape: - return new (std::nothrow) Shape(primitive); - case schema::PrimitiveType_Unsqueeze: - return new (std::nothrow) Unsqueeze(primitive); - case schema::PrimitiveType_BatchToSpace: - case schema::PrimitiveType_BatchToSpaceND: - return new (std::nothrow) BatchToSpace(primitive); - case schema::PrimitiveType_SpaceToBatch: - return new (std::nothrow) SpaceToBatch(primitive); - case schema::PrimitiveType_SpaceToBatchND: - return new (std::nothrow) SpaceToBatchND(primitive); - case schema::PrimitiveType_BroadcastTo: - return new (std::nothrow) BroadcastTo(primitive); - case schema::PrimitiveType_DepthToSpace: - return new (std::nothrow) DepthToSpace(primitive); - case schema::PrimitiveType_Lstm: - return new (std::nothrow) Lstm(primitive); - case schema::PrimitiveType_ZerosLike: - return new (std::nothrow) ZerosLike(primitive); - case schema::PrimitiveType_MakeTuple: - return new (std::nothrow) MakeTuple(primitive); - case schema::PrimitiveType_Where: - return new (std::nothrow) Where(primitive); - case schema::PrimitiveType_ScatterND: - return new (std::nothrow) ScatterND(primitive); - case schema::PrimitiveType_ConstantOfShape: - return new (std::nothrow) ConstantOfShape(primitive); - case schema::PrimitiveType_L2Norm: - return new (std::nothrow) L2Norm(primitive); - case schema::PrimitiveType_SparseToDense: - return new (std::nothrow) SparseToDense(primitive); - case schema::PrimitiveType_DetectionPostProcess: - return new (std::nothrow) DetectionPostProcess(primitive); - case schema::PrimitiveType_Dropout: - return new (std::nothrow) Dropout(primitive); - case schema::PrimitiveType_Neg: - return new (std::nothrow) Neg(primitive); - case schema::PrimitiveType_RealDiv: - return new (std::nothrow) RealDiv(primitive); - case schema::PrimitiveType_LshProjection: - return new (std::nothrow) LshProjection(primitive); - case schema::PrimitiveType_HashtableLookup: - return new (std::nothrow) HashtableLookup(primitive); - case schema::PrimitiveType_SkipGram: - return new (std::nothrow) SkipGram(primitive); - case schema::PrimitiveType_Clip: - return new (std::nothrow) Clip(primitive); - case schema::PrimitiveType_Adder: - return new (std::nothrow) Adder(primitive); - case schema::PrimitiveType_CustomPredict: - return new (std::nothrow) CustomPredict(primitive); - case schema::PrimitiveType_CustomNormalize: - return new (std::nothrow) CustomNormalize(primitive); - case schema::PrimitiveType_CustomExtractFeatures: - return new (std::nothrow) CustomExtractFeatures(primitive); - case schema::PrimitiveType_Upsample: - return new (std::nothrow) Upsample(primitive); - case schema::PrimitiveType_LayerNorm: - return new (std::nothrow) LayerNorm(primitive); - case schema::PrimitiveType_NonMaxSuppression: - return new (std::nothrow) NonMaxSuppression(primitive); - case schema::PrimitiveType_Identity: - return new (std::nothrow) Identity(primitive); - case schema::PrimitiveType_Rfft: - return new (std::nothrow) Rfft(primitive); - case schema::PrimitiveType_FftReal: - return new (std::nothrow) FftReal(primitive); - case schema::PrimitiveType_FftImag: - return new (std::nothrow) FftImag(primitive); - case schema::PrimitiveType_AudioSpectrogram: - return new (std::nothrow) AudioSpectrogram(primitive); - case schema::PrimitiveType_Mfcc: - return new (std::nothrow) Mfcc(primitive); - case schema::PrimitiveType_InstanceNorm: - return new (std::nothrow) InstanceNorm(primitive); - case schema::PrimitiveType_While: - return new (std::nothrow) While(primitive); - case schema::PrimitiveType_OnnxInt8Quantize: - return new (std::nothrow) Quant(primitive); - case schema::PrimitiveType_OnnxInt8Dequantize: - return new (std::nothrow) Dequant(primitive); - case schema::PrimitiveType_Reciprocal: - return new (std::nothrow) Reciprocal(primitive); - case schema::PrimitiveType_Constant: - return new (std::nothrow) Constant(primitive); - case schema::PrimitiveType_TensorListFromTensor: - return new (std::nothrow) TensorListFromTensor(primitive); - case schema::PrimitiveType_TensorListGetItem: - return new (std::nothrow) TensorListGetItem(primitive); - case schema::PrimitiveType_TensorListSetItem: - return new (std::nothrow) TensorListSetItem(primitive); - case schema::PrimitiveType_TensorListReserve: - return new (std::nothrow) TensorListReserve(primitive); - case schema::PrimitiveType_TensorListStack: - return new (std::nothrow) TensorListStack(primitive); - case schema::PrimitiveType_Switch: - return new (std::nothrow) Switch(primitive); - case schema::PrimitiveType_Merge: - return new (std::nothrow) Merge(primitive); - case schema::PrimitiveType_Partial: - return new (std::nothrow) Partial(primitive); - case schema::PrimitiveType_Assert: - return new (std::nothrow) AssertOP(primitive); - case schema::PrimitiveType_GeLU: - return new (std::nothrow) GeLU(primitive); - case schema::PrimitiveType_If: - return new (std::nothrow) If(primitive); - case schema::PrimitiveType_Select: - return new (std::nothrow) Select(primitive); - case schema::PrimitiveType_Gru: - return new (std::nothrow) Gru(primitive); - case schema::PrimitiveType_Size: - return new (std::nothrow) Size(primitive); - case schema::PrimitiveType_InvertPermutation: - return new (std::nothrow) InvertPermutation(primitive); - case schema::PrimitiveType_RandomStandardNormal: - return new (std::nothrow) RandomStandardNormal(primitive); - case schema::PrimitiveType_CropAndResize: - return new (std::nothrow) CropAndResize(primitive); - case schema::PrimitiveType_NonZero: - return new (std::nothrow) NonZero(primitive); - case schema::PrimitiveType_Erf: - return new (std::nothrow) Erf(primitive); - case schema::PrimitiveType_IsFinite: - return new (std::nothrow) IsFinite(primitive); - case schema::PrimitiveType_LinSpace: - return new (std::nothrow) LinSpace(primitive); - case schema::PrimitiveType_UniformReal: - return new (std::nothrow) UniformReal(primitive); - case schema::PrimitiveType_Rank: - return new (std::nothrow) Rank(primitive); - case schema::PrimitiveType_ActivationGrad: - return new (std::nothrow) ActivationGrad(primitive); - case schema::PrimitiveType_PoolingGrad: - return new (std::nothrow) PoolingGrad(primitive); - case schema::PrimitiveType_Conv2DGradFilter: - return new (std::nothrow) Conv2DGradFilter(primitive); - case schema::PrimitiveType_Conv2DGradInput: - return new (std::nothrow) Conv2DGradInput(primitive); - case schema::PrimitiveType_GroupConv2DGradInput: - return new (std::nothrow) GroupConv2DGradInput(primitive); - case schema::PrimitiveType_BiasGrad: - return new (std::nothrow) BiasGrad(primitive); - case schema::PrimitiveType_ApplyMomentum: - return new (std::nothrow) ApplyMomentum(primitive); - case schema::PrimitiveType_BNGrad: - return new (std::nothrow) BNGrad(primitive); - case schema::PrimitiveType_AddGrad: - return new (std::nothrow) ArithmeticGrad(primitive); - case schema::PrimitiveType_SubGrad: - return new (std::nothrow) ArithmeticGrad(primitive); - case schema::PrimitiveType_MulGrad: - return new (std::nothrow) ArithmeticGrad(primitive); - case schema::PrimitiveType_DivGrad: - return new (std::nothrow) ArithmeticGrad(primitive); - case schema::PrimitiveType_SoftmaxCrossEntropy: - return new (std::nothrow) SoftmaxCrossEntropy(primitive); - case schema::PrimitiveType_SparseSoftmaxCrossEntropy: - return new (std::nothrow) SparseSoftmaxCrossEntropy(primitive); - case schema::PrimitiveType_PowerGrad: - return new (std::nothrow) PowerGrad(primitive); - case schema::PrimitiveType_Depend: - return new (std::nothrow) Depend(primitive); - case schema::PrimitiveType_ControlDepend: - return new (std::nothrow) ControlDepend(primitive); - case schema::PrimitiveType_FlattenGrad: - return new (std::nothrow) FlattenGrad(primitive); - case schema::PrimitiveType_NegGrad: - return new (std::nothrow) NegGrad(primitive); - case schema::PrimitiveType_LogGrad: - return new (std::nothrow) LogGrad(primitive); - case schema::PrimitiveType_AbsGrad: - return new (std::nothrow) AbsGrad(primitive); - case schema::PrimitiveType_Sgd: - return new (std::nothrow) Sgd(primitive); - case schema::PrimitiveType_Adam: - return new (std::nothrow) Adam(primitive); - case schema::PrimitiveType_Assign: - return new (std::nothrow) Assign(primitive); - case schema::PrimitiveType_AssignAdd: - return new (std::nothrow) AssignAdd(primitive); - case schema::PrimitiveType_OnesLike: - return new (std::nothrow) OnesLike(primitive); - case schema::PrimitiveType_UnsortedSegmentSum: - return new (std::nothrow) UnsortedSegmentSum(primitive); - case schema::PrimitiveType_BinaryCrossEntropyGrad: - return new (std::nothrow) BinaryCrossEntropyGrad(primitive); - case schema::PrimitiveType_BinaryCrossEntropy: - return new (std::nothrow) BinaryCrossEntropy(primitive); - case schema::PrimitiveType_DropoutGrad: - return new (std::nothrow) DropoutGrad(primitive); - case schema::PrimitiveType_MaximumGrad: - return new (std::nothrow) MaximumGrad(primitive); - case schema::PrimitiveType_MinimumGrad: - return new (std::nothrow) MinimumGrad(primitive); - case schema::PrimitiveType_SmoothL1Loss: - return new (std::nothrow) SmoothL1Loss(primitive); - case schema::PrimitiveType_SmoothL1LossGrad: - return new (std::nothrow) SmoothL1LossGrad(primitive); - case schema::PrimitiveType_SigmoidCrossEntropyWithLogits: - return new (std::nothrow) SigmoidCrossEntropyWithLogits(primitive); - case schema::PrimitiveType_SigmoidCrossEntropyWithLogitsGrad: - return new (std::nothrow) SigmoidCrossEntropyWithLogitsGrad(primitive); - case schema::PrimitiveType_StridedSliceGrad: - return new (std::nothrow) StridedSliceGrad(primitive); - default: - MS_LOG(ERROR) << "Unsupported primitive type in Create : " << schema::EnumNamePrimitiveType(op_type); - break; - } - return nullptr; -} - -#else -void PrimitiveC::set_quant_type(schema::QuantType quant_type) { this->quant_type_ = quant_type; } -schema::QuantType PrimitiveC::quant_type() const { return quant_type_; } -#endif - -int PrimitiveC::Type() const { - if (this->primitive_ == nullptr && this->op_type_ == OP_TYPE_NOT_SET) { - return schema::PrimitiveType_NONE; - } -#ifdef PRIMITIVE_WRITEABLE - if (op_type_ != OP_TYPE_NOT_SET) { - return op_type_; - } - return this->primitive_->value.type; -#else - return this->primitive_->value_type(); -#endif -} -bool PrimitiveC::infer_flag() const { return this->infer_flag_; } - -void PrimitiveC::set_infer_flag(bool flag) { this->infer_flag_ = flag; } - -bool PrimitiveC::train_flag() const { return this->train_flag_; } - -void PrimitiveC::set_train_flag(bool flag) { this->train_flag_ = flag; } - -int PrimitiveC::InferShape(std::vector inputs, std::vector outputs) { - auto input = inputs.front(); - MS_ASSERT(input != nullptr); - auto output = outputs.front(); - MS_ASSERT(output != nullptr); - output->set_shape(input->shape()); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - return 0; -} - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/primitive_c.h b/mindspore/lite/src/ops/primitive_c.h deleted file mode 100644 index 469992a7b9..0000000000 --- a/mindspore/lite/src/ops/primitive_c.h +++ /dev/null @@ -1,259 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_PRIMITIVE_C_H_ -#define MINDSPORE_LITE_SRC_OPS_PRIMITIVE_C_H_ -#include -#include -#include -#include -#include -#ifdef PRIMITIVE_WRITEABLE -#include "ir/primitive.h" -#include "schema/inner/model_generated.h" -#include "schema/inner/ops_generated.h" -#include "schema/ops_generated.h" -#include "tools/converter/ops/ops_def.h" -#else -#include "schema/model_generated.h" -#endif -#include "nnacl/op_base.h" -#include "src/tensor.h" -#include "include/errorcode.h" -#include "src/common/log_adapter.h" - -namespace mindspore { -namespace lite { -constexpr const int OP_TYPE_NOT_SET = -1; -constexpr uint32_t kSingleNum = 1; -constexpr uint32_t kDoubleNum = 2; -constexpr uint32_t kTripleNum = 3; -constexpr uint32_t kQuadrupleNum = 4; - -const std::set kSupportDataType = {kNumberTypeBool, kNumberTypeUInt8, kNumberTypeInt8, - kNumberTypeInt32, kNumberTypeFloat32, kNumberTypeFloat16}; - -#ifdef PRIMITIVE_WRITEABLE -using TensorPtr = std::shared_ptr; -constexpr int kAnfPopulaterInputNumOne = 1; -constexpr int kAnfPopulaterInputNumTwo = 2; -constexpr int kAnfPopulaterInputNumThree = 3; -static std::map kActivationTypeMap{ - {"ReLU", schema::ActivationType_RELU}, - {"ReLU6", schema::ActivationType_RELU6}, - {"Sigmoid", schema::ActivationType_SIGMOID}, - {"HSwish", schema::ActivationType_HSWISH}, - {"HSigmoid", schema::ActivationType_HSIGMOID}, - {"Swish", schema::ActivationType_SWISH}, - {"LeakyRelu", schema::ActivationType_LEAKY_RELU}, - {"Tanh", schema::ActivationType_TANH}, - {"Logistic", schema::ActivationType_SIGMOID}}; -std::vector CastToInt(const ValuePtr &value); -class PrimitiveC : public mindspore::Primitive { - public: - // Argument primitive is deliverd into PrimitiveC and will be deleted in ~PrimitiveC(). - // Caller should not delete primitive. - explicit PrimitiveC(schema::PrimitiveT *primitive) : Primitive(""), primitive_(primitive) {} - - explicit PrimitiveC(const Primitive &prim) : Primitive(prim) {} - - // Argument primitive is deliverd into PrimitiveC and will be deleted in ~PrimitiveC(). - // Caller should not delete primitive. - PrimitiveC(const std::string &name, schema::PrimitiveT *primitive) : Primitive(name), primitive_(primitive) {} - - PrimitiveC() : Primitive(""), primitive_(nullptr) {} - - MS_DECLARE_PARENT(PrimitiveC, Primitive); - - ~PrimitiveC() override { delete this->primitive_; } - - int Type() const; - - schema::PrimitiveT *primitiveT() const; - - void ClearPrimitiveT(); - - bool operator==(const Value &rhs) const override { - if (rhs.isa()) { - auto other_prim = dynamic_cast(rhs); - auto a = this->primitive_->value.type; - auto b = other_prim.primitive_->value.type; - return a == b; - } else { - return false; - } - } - - void set_input_quant_params(const std::vector> &input_quant_param); - - void set_input_quant_param(const size_t &index, const std::vector &input_quant_param); - - void set_output_quant_params(const std::vector> &output_quant_param); - - void set_output_quant_param(const size_t &index, const std::vector &output_quant_param); - - bool IsInputQuantParamsInited(); - - bool IsOutputQuantParamsInited(); - - void ClearInputOutputQuantParam(); - - void AddInputQuantParam(const std::vector &quant_param); - - std::vector> input_quant_params() const; - - void AddOutputQuantParam(const std::vector &quant_param); - - std::vector> output_quant_params() const; - - void set_quant_type(const schema::QuantType &quant_type); - - schema::QuantType quant_type() const; - - bool enable_huffman_code() const; - - void set_enable_huffman_code(bool enable_huffman_code); - - virtual int InferShape(std::vector inputs, std::vector outputs); - - bool infer_flag() const; - - void set_infer_flag(bool flag); - - bool train_flag() const; - - void set_train_flag(bool flag); - - static PrimitiveC *Create(mindspore::schema::Primitive *primitive) { return Create(primitive->UnPack()); } - - static PrimitiveC *Create(mindspore::schema::PrimitiveT *primitive); - - static void GetAttrDataFromInput(const AnfNodePtr &inputNode, std::vector *data); - - static std::shared_ptr Create(const Primitive &prim, const std::vector &inputs, - const schema::QuantType &quantType, bool train_flag = false); - void PopulaterQuantParam(const Primitive &prim, const std::vector &inputs); - void FillDefaultInputQuantParamIfNeed(const size_t &inputSize); - void PopulaterInputQuantParam(const Primitive &prim, const std::vector &inputs, - bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam); - void PopulaterOutputQuantParam(const Primitive &prim, bool narrowRangeQuantParam, int32_t numbitsRangeQuantParam); - static void CalFloatScopeByMeanAndStddev(const double &mean, const double &stdDev, float *mMin, float *mMax); - - protected: - virtual int UnPackAttr(const Primitive &prim, const std::vector &inputs) { return RET_ERROR; } - - protected: - schema::PrimitiveT *primitive_ = nullptr; - std::vector> input_quant_param_; - std::vector> output_quant_param_; - schema::QuantType quant_type_{schema::QuantType_QUANT_NONE}; - bool infer_flag_ = true; - int op_type_ = OP_TYPE_NOT_SET; - bool enable_huffman_code_ = false; - bool train_flag_ = false; -}; -std::shared_ptr GetReturnPrim(); - -std::shared_ptr GetMakeTuplePrim(); - -std::shared_ptr GetTupleGetItemPrim(); - -#else -class PrimitiveC { - public: - PrimitiveC() = default; - - virtual ~PrimitiveC() { free(this->primitive_buf_); } - - static PrimitiveC *Create(const schema::Primitive *primitive); - - bool infer_flag() const; - - void set_infer_flag(bool flag); - - bool train_flag() const; - - void set_train_flag(bool flag); - - virtual int InferShape(std::vector inputs, std::vector outputs); - - int Type() const; - - void set_quant_type(schema::QuantType quant_type); - schema::QuantType quant_type() const; - - template ::value>> - static PrimitiveC *NewPrimitiveC(const schema::Primitive *primitive) { - auto primc = new (std::nothrow) T(); - if (primc == nullptr) { - MS_LOG(ERROR) << "new PrimitiveC failed"; - return nullptr; - } - auto ret = primc->UnPackSchemaPrimitive(primitive); - if (ret != RET_OK) { - delete primc; - MS_LOG(ERROR) << "UnPackSchemaPrimitive failed"; - return nullptr; - } - return primc; - } - - protected: - int UnPackSchemaPrimitive(const schema::Primitive *primitive) { - flatbuffers::FlatBufferBuilder fbb(1024); - if (UnPackToFlatBuilder(primitive, &fbb) != RET_OK) { - MS_LOG(ERROR) << "UnPackToFlatBuilder failde"; - fbb.Clear(); - return RET_ERROR; - } - auto buf = fbb.GetBufferPointer(); - if (buf == nullptr) { - MS_LOG(ERROR) << "GetBufferPointer return nullptr"; - fbb.Clear(); - return RET_ERROR; - } - primitive_buf_ = reinterpret_cast(malloc(fbb.GetSize())); - if (primitive_buf_ == nullptr) { - MS_LOG(ERROR) << "malloc primitive_buf_ failed"; - fbb.Clear(); - return RET_ERROR; - } - memcpy(primitive_buf_, buf, fbb.GetSize()); - this->primitive_ = flatbuffers::GetRoot(primitive_buf_); - fbb.Clear(); - return RET_OK; - } - - virtual int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - return RET_ERROR; - } - - protected: - const schema::Primitive *primitive_ = nullptr; - char *primitive_buf_ = nullptr; - bool infer_flag_ = true; - schema::QuantType quant_type_{schema::QuantType_QUANT_NONE}; - int op_type_ = OP_TYPE_NOT_SET; - bool train_flag_ = false; -}; -using PrimitiveCPtr = std::shared_ptr; -typedef PrimitiveC *(*PrimitiveCCreator)(const schema::Primitive *primitive); -#endif -typedef OpParameter *(*ParameterCreator)(const PrimitiveC *primitive); - -} // namespace lite -} // namespace mindspore -#endif // MINDSPORE_LITE_SRC_OPS_PRIMITIVE_C_H_ diff --git a/mindspore/lite/src/ops/prior_box.cc b/mindspore/lite/src/ops/prior_box.cc deleted file mode 100644 index 1d70ad4edf..0000000000 --- a/mindspore/lite/src/ops/prior_box.cc +++ /dev/null @@ -1,165 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/prior_box.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector PriorBox::GetMinSizes() const { return this->primitive_->value.AsPriorBox()->max_sizes; } -std::vector PriorBox::GetMaxSizes() const { return this->primitive_->value.AsPriorBox()->max_sizes; } -std::vector PriorBox::GetAspectRatios() const { return this->primitive_->value.AsPriorBox()->aspect_ratios; } -std::vector PriorBox::GetVariances() const { return this->primitive_->value.AsPriorBox()->variances; } -int PriorBox::GetImageSizeW() const { return this->primitive_->value.AsPriorBox()->image_size_w; } -int PriorBox::GetImageSizeH() const { return this->primitive_->value.AsPriorBox()->image_size_h; } -float PriorBox::GetStepW() const { return this->primitive_->value.AsPriorBox()->step_w; } -float PriorBox::GetStepH() const { return this->primitive_->value.AsPriorBox()->step_h; } -bool PriorBox::GetClip() const { return this->primitive_->value.AsPriorBox()->clip; } -bool PriorBox::GetFlip() const { return this->primitive_->value.AsPriorBox()->flip; } -float PriorBox::GetOffset() const { return this->primitive_->value.AsPriorBox()->offset; } - -void PriorBox::SetMinSizes(const std::vector &min_sizes) { - this->primitive_->value.AsPriorBox()->min_sizes = min_sizes; -} -void PriorBox::SetMaxSizes(const std::vector &max_sizes) { - this->primitive_->value.AsPriorBox()->max_sizes = max_sizes; -} -void PriorBox::SetAspectRatios(const std::vector &aspect_ratios) { - this->primitive_->value.AsPriorBox()->aspect_ratios = aspect_ratios; -} -void PriorBox::SetVariances(const std::vector &variances) { - this->primitive_->value.AsPriorBox()->variances = variances; -} -void PriorBox::SetImageSizeW(int image_size_w) { this->primitive_->value.AsPriorBox()->image_size_w = image_size_w; } -void PriorBox::SetImageSizeH(int image_size_h) { this->primitive_->value.AsPriorBox()->image_size_h = image_size_h; } -void PriorBox::SetStepW(float step_w) { this->primitive_->value.AsPriorBox()->step_w = step_w; } -void PriorBox::SetStepH(float step_h) { this->primitive_->value.AsPriorBox()->step_h = step_h; } -void PriorBox::SetClip(bool clip) { this->primitive_->value.AsPriorBox()->clip = clip; } -void PriorBox::SetFlip(bool flip) { this->primitive_->value.AsPriorBox()->flip = flip; } -void PriorBox::SetOffset(float offset) { this->primitive_->value.AsPriorBox()->offset = offset; } - -#else - -std::vector PriorBox::GetMinSizes() const { - auto fb_vector = this->primitive_->value_as_PriorBox()->min_sizes(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -std::vector PriorBox::GetMaxSizes() const { - auto fb_vector = this->primitive_->value_as_PriorBox()->max_sizes(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -std::vector PriorBox::GetAspectRatios() const { - auto fb_vector = this->primitive_->value_as_PriorBox()->aspect_ratios(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -std::vector PriorBox::GetVariances() const { - auto fb_vector = this->primitive_->value_as_PriorBox()->variances(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int PriorBox::GetImageSizeW() const { return this->primitive_->value_as_PriorBox()->image_size_w(); } -int PriorBox::GetImageSizeH() const { return this->primitive_->value_as_PriorBox()->image_size_h(); } -float PriorBox::GetStepW() const { return this->primitive_->value_as_PriorBox()->step_w(); } -float PriorBox::GetStepH() const { return this->primitive_->value_as_PriorBox()->step_h(); } -bool PriorBox::GetClip() const { return this->primitive_->value_as_PriorBox()->clip(); } -bool PriorBox::GetFlip() const { return this->primitive_->value_as_PriorBox()->flip(); } -float PriorBox::GetOffset() const { return this->primitive_->value_as_PriorBox()->offset(); } - -int PriorBox::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_PriorBox(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_PriorBox return nullptr"; - return RET_ERROR; - } - std::vector min_sizes; - if (attr->min_sizes() != nullptr) { - for (int i = 0; i < static_cast(attr->min_sizes()->size()); i++) { - min_sizes.push_back(attr->min_sizes()->data()[i]); - } - } - std::vector max_sizes; - if (attr->max_sizes() != nullptr) { - for (int i = 0; i < static_cast(attr->max_sizes()->size()); i++) { - max_sizes.push_back(attr->max_sizes()->data()[i]); - } - } - std::vector aspect_ratios; - if (attr->aspect_ratios() != nullptr) { - for (int i = 0; i < static_cast(attr->aspect_ratios()->size()); i++) { - aspect_ratios.push_back(attr->aspect_ratios()->data()[i]); - } - } - std::vector variances; - if (attr->variances() != nullptr) { - for (int i = 0; i < static_cast(attr->variances()->size()); i++) { - variances.push_back(attr->variances()->data()[i]); - } - } - auto val_offset = schema::CreatePriorBoxDirect(*fbb, &min_sizes, &max_sizes, &aspect_ratios, &variances); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_PriorBox, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *PriorBoxCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry PriorBoxRegistry(schema::PrimitiveType_PriorBox, PriorBoxCreator); -#endif - -namespace { -constexpr int kPriorBoxPoints = 4; -constexpr int kPriorBoxN = 1; -constexpr int kPriorBoxW = 1; -constexpr int kPriorBoxC = 2; -} // namespace -int PriorBox::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.at(0); - MS_ASSERT(input != nullptr); - auto output = outputs_.at(0); - MS_ASSERT(output != nullptr); - output->set_data_type(kNumberTypeFloat32); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector different_aspect_ratios{1.0f}; - auto aspect_ratios = GetAspectRatios(); - for (size_t i = 0; i < aspect_ratios.size(); i++) { - float ratio = aspect_ratios[i]; - bool exist = std::any_of(different_aspect_ratios.begin(), different_aspect_ratios.end(), - [&](float v) { return abs(ratio - v) < 1e-6; }); - if (!exist) { - different_aspect_ratios.emplace_back(ratio); - if (GetFlip()) { - different_aspect_ratios.emplace_back(1.0f / ratio); - } - } - } - int32_t num_priors_box = GetMinSizes().size() * different_aspect_ratios.size() + GetMaxSizes().size(); - int32_t h = input->Height() * input->Width() * num_priors_box * kPriorBoxPoints; - std::vector output_shape{kPriorBoxN, h, kPriorBoxW, kPriorBoxC}; - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/prior_box.h b/mindspore/lite/src/ops/prior_box.h deleted file mode 100644 index 4976ea425f..0000000000 --- a/mindspore/lite/src/ops/prior_box.h +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ -#define LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class PriorBox : public PrimitiveC { - public: - PriorBox() = default; - ~PriorBox() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(PriorBox, PrimitiveC); - explicit PriorBox(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetMinSizes(const std::vector &min_sizes); - void SetMaxSizes(const std::vector &max_sizes); - void SetAspectRatios(const std::vector &aspect_ratios); - void SetVariances(const std::vector &variances); - void SetImageSizeW(int image_size_w); - void SetImageSizeH(int image_size_h); - void SetStepW(float step_w); - void SetStepH(float step_h); - void SetClip(bool clip); - void SetFlip(bool flip); - void SetOffset(float offset); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetMinSizes() const; - std::vector GetMaxSizes() const; - std::vector GetAspectRatios() const; - std::vector GetVariances() const; - int GetImageSizeW() const; - int GetImageSizeH() const; - float GetStepW() const; - float GetStepH() const; - bool GetClip() const; - bool GetFlip() const; - float GetOffset() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ diff --git a/mindspore/lite/src/ops/quant.cc b/mindspore/lite/src/ops/quant.cc deleted file mode 100644 index 9df5c609bb..0000000000 --- a/mindspore/lite/src/ops/quant.cc +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/quant.h" -#include -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Quant::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_OnnxInt8Quantize; - } - if (this->primitive_->value.type != schema::PrimitiveType_OnnxInt8Quantize) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::OnnxInt8QuantizeT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/quant.h b/mindspore/lite/src/ops/quant.h deleted file mode 100644 index dd854768cf..0000000000 --- a/mindspore/lite/src/ops/quant.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_SRC_OPS_QUANT_H_ -#define LITE_MINDSPORE_LITE_SRC_OPS_QUANT_H_ -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Quant : public PrimitiveC { - public: - Quant() = default; - ~Quant() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Quant, PrimitiveC); - explicit Quant(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_SRC_OPS_QUANT_H_ diff --git a/mindspore/lite/src/ops/quant_dtype_cast.cc b/mindspore/lite/src/ops/quant_dtype_cast.cc deleted file mode 100644 index e7fa5a97c1..0000000000 --- a/mindspore/lite/src/ops/quant_dtype_cast.cc +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/quant_dtype_cast.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int QuantDTypeCast::GetSrcT() const { return this->primitive_->value.AsQuantDTypeCast()->srcT; } -int QuantDTypeCast::GetDstT() const { return this->primitive_->value.AsQuantDTypeCast()->dstT; } - -void QuantDTypeCast::SetSrcT(int src_t) { this->primitive_->value.AsQuantDTypeCast()->srcT = src_t; } -void QuantDTypeCast::SetDstT(int dst_t) { this->primitive_->value.AsQuantDTypeCast()->dstT = dst_t; } - -#else - -int QuantDTypeCast::GetSrcT() const { return this->primitive_->value_as_QuantDTypeCast()->srcT(); } -int QuantDTypeCast::GetDstT() const { return this->primitive_->value_as_QuantDTypeCast()->dstT(); } -int QuantDTypeCast::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_QuantDTypeCast(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_QuantDTypeCast return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateQuantDTypeCast(*fbb, attr->srcT(), attr->dstT()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_QuantDTypeCast, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *QuantDTypeCastCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry QuantDTypeCastRegistry(schema::PrimitiveType_QuantDTypeCast, QuantDTypeCastCreator); -#endif - -int QuantDTypeCast::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - MS_ASSERT(input->data_type() == this->GetSrcT()); - output->set_data_type(static_cast(GetDstT())); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - output->set_shape(input->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/quant_dtype_cast.h b/mindspore/lite/src/ops/quant_dtype_cast.h deleted file mode 100644 index ec9f75c18f..0000000000 --- a/mindspore/lite/src/ops/quant_dtype_cast.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ -#define LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class QuantDTypeCast : public PrimitiveC { - public: - QuantDTypeCast() = default; - ~QuantDTypeCast() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(QuantDTypeCast, PrimitiveC); - explicit QuantDTypeCast(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetSrcT(int src_t); - void SetDstT(int dst_t); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetSrcT() const; - int GetDstT() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ diff --git a/mindspore/lite/src/ops/random_standard_normal.cc b/mindspore/lite/src/ops/random_standard_normal.cc deleted file mode 100644 index d17b1984ed..0000000000 --- a/mindspore/lite/src/ops/random_standard_normal.cc +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/random_standard_normal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int RandomStandardNormal::GetSeed() const { return this->primitive_->value.AsRandomStandardNormal()->seed; } - -int RandomStandardNormal::GetSeed2() const { return this->primitive_->value.AsRandomStandardNormal()->seed2; } - -int RandomStandardNormal::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_RandomStandardNormal; - } - if (this->primitive_->value.type != schema::PrimitiveType_RandomStandardNormal) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::RandomStandardNormalT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int RandomStandardNormal::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_RandomStandardNormal(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_RandomStandardNormal return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateRandomStandardNormal(*fbb, attr->seed(), attr->seed2()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_RandomStandardNormal, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -int RandomStandardNormal::GetSeed() const { return this->primitive_->value_as_RandomStandardNormal()->seed(); } - -int RandomStandardNormal::GetSeed2() const { return this->primitive_->value_as_RandomStandardNormal()->seed2(); } - -PrimitiveC *RandomStandardNormalCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry RandomStandardNormalRegistry(schema::PrimitiveType_RandomStandardNormal, RandomStandardNormalCreator); -#endif - -int RandomStandardNormal::InferShape(std::vector inputs_, std::vector outputs_) { - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_data = static_cast(inputs_[0]->data_c()); - if (input_data == nullptr) { - return RET_INFER_INVALID; - } - auto input_num = inputs_[0]->ElementsNum(); - std::vector output_shape = {}; - for (int i = 0; i < input_num; i++) { - output_shape.push_back(input_data[i]); - } - outputs_[0]->set_shape(output_shape); - outputs_[0]->set_data_type(kNumberTypeFloat32); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/random_standard_normal.h b/mindspore/lite/src/ops/random_standard_normal.h deleted file mode 100644 index 5cd60748aa..0000000000 --- a/mindspore/lite/src/ops/random_standard_normal.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RANDOM_STANDARD_NORMAL_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RANDOM_STANDARD_NORMAL_H_ - -#include -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class RandomStandardNormal : public PrimitiveC { - public: - RandomStandardNormal() = default; - ~RandomStandardNormal() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(RandomStandardNormal, PrimitiveC); - explicit RandomStandardNormal(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetSeed() const; - int GetSeed2() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RANDOM_STANDARD_NORMAL_H_ diff --git a/mindspore/lite/src/ops/range.cc b/mindspore/lite/src/ops/range.cc deleted file mode 100644 index 8014d62cd0..0000000000 --- a/mindspore/lite/src/ops/range.cc +++ /dev/null @@ -1,149 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 -#include "src/ops/range.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Range::GetDType() const { return this->primitive_->value.AsRange()->dType; } -int Range::GetStart() const { return this->primitive_->value.AsRange()->start; } -int Range::GetLimit() const { return this->primitive_->value.AsRange()->limit; } -int Range::GetDelta() const { return this->primitive_->value.AsRange()->delta; } - -void Range::SetDType(int d_type) { this->primitive_->value.AsRange()->dType = d_type; } -void Range::SetStart(int start) { this->primitive_->value.AsRange()->start = start; } -void Range::SetLimit(int limit) { this->primitive_->value.AsRange()->limit = limit; } -void Range::SetDelta(int delta) { this->primitive_->value.AsRange()->delta = delta; } -int Range::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Range; - } - if (this->primitive_->value.type != schema::PrimitiveType_Range) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::RangeT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - attr->dType = 0; - if (prim.GetAttr("start") != nullptr) { - attr->start = static_cast(GetValue(prim.GetAttr("start"))); - } - if (prim.GetAttr("limit") != nullptr) { - attr->limit = static_cast(GetValue(prim.GetAttr("limit"))); - } - if (prim.GetAttr("delta") != nullptr) { - attr->delta = static_cast(GetValue(prim.GetAttr("delta"))); - } - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -int Range::GetDType() const { return this->primitive_->value_as_Range()->dType(); } -int Range::GetStart() const { return this->primitive_->value_as_Range()->start(); } -int Range::GetLimit() const { return this->primitive_->value_as_Range()->limit(); } -int Range::GetDelta() const { return this->primitive_->value_as_Range()->delta(); } -int Range::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Range(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Range return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateRange(*fbb, attr->dType(), attr->start(), attr->limit(), attr->delta()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Range, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *RangeCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RangeRegistry(schema::PrimitiveType_Range, RangeCreator); -#endif - -int Range::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - - if (inputs_.size() == 3) { - output->set_data_type(input->data_type()); - } else { - output->set_data_type(mindspore::kNumberTypeInt32); - } - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - int shape_size = 0; - if (inputs_.size() == 3) { - if ((inputs_.at(0)->data_c() == nullptr) || (inputs_.at(1)->data_c() == nullptr) || - (inputs_.at(2)->data_c() == nullptr)) { - return RET_INFER_INVALID; - } - switch (inputs_.at(0)->data_type()) { - case kNumberTypeInt: - case kNumberTypeInt32: { - auto start = *reinterpret_cast(inputs_.at(0)->data_c()); - auto limit = *reinterpret_cast(inputs_.at(1)->data_c()); - auto delta = *reinterpret_cast(inputs_.at(2)->data_c()); - shape_size = std::max(static_cast(std::ceil(static_cast(limit - start) / delta)), 0); - } break; - case kNumberTypeFloat32: - case kNumberTypeFloat: { - auto start = *reinterpret_cast(inputs_.at(0)->data_c()); - auto limit = *reinterpret_cast(inputs_.at(1)->data_c()); - auto delta = *reinterpret_cast(inputs_.at(2)->data_c()); - shape_size = std::max(static_cast(std::ceil(static_cast(limit - start) / delta)), 0); - } break; - default: { - MS_LOG(ERROR) << "Range has unsupported dataType: " << inputs_.at(0)->data_type(); - return RET_INFER_ERR; - } - } - } else { - shape_size = std::ceil(static_cast(GetLimit() - GetStart()) / GetDelta()); - } - - std::vector in_shape = {shape_size}; - output->set_shape(in_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/range.h b/mindspore/lite/src/ops/range.h deleted file mode 100644 index 8f1adafcc6..0000000000 --- a/mindspore/lite/src/ops/range.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Range : public PrimitiveC { - public: - Range() = default; - ~Range() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Range, PrimitiveC); - explicit Range(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetDType(int d_type); - void SetStart(int start); - void SetLimit(int limit); - void SetDelta(int delta); - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetDType() const; - int GetStart() const; - int GetLimit() const; - int GetDelta() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ diff --git a/mindspore/lite/src/ops/rank.cc b/mindspore/lite/src/ops/rank.cc deleted file mode 100644 index c0633e2d92..0000000000 --- a/mindspore/lite/src/ops/rank.cc +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/rank.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int Rank::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateRank(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Rank, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *RankCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RankRegistry(schema::PrimitiveType_Rank, RankCreator); -#endif -int Rank::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - std::vector in_shape(1, 1); - output->set_shape(in_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/rank.h b/mindspore/lite/src/ops/rank.h deleted file mode 100644 index 4ee203ef88..0000000000 --- a/mindspore/lite/src/ops/rank.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RANK_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RANK_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Rank : public PrimitiveC { - public: - Rank() = default; - ~Rank() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Rank, PrimitiveC); - explicit Rank(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RANK_H_ diff --git a/mindspore/lite/src/ops/real_div.cc b/mindspore/lite/src/ops/real_div.cc deleted file mode 100644 index 2b36e748ed..0000000000 --- a/mindspore/lite/src/ops/real_div.cc +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/real_div.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE - -int RealDiv::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_RealDiv; - } - if (this->primitive_->value.type != schema::PrimitiveType_RealDiv) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::RealDivT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -int RealDiv::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateRank(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_RealDiv, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *RealDivCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RealDivRegistry(schema::PrimitiveType_RealDiv, RealDivCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/real_div.h b/mindspore/lite/src/ops/real_div.h deleted file mode 100644 index 97e1e8c74f..0000000000 --- a/mindspore/lite/src/ops/real_div.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_REAL_DIV_H_ -#define MINDSPORE_LITE_SRC_OPS_REAL_DIV_H_ - -#include -#include -#include - -#include "src/ops/arithmetic.h" - -namespace mindspore { -namespace lite { -class RealDiv : public Arithmetic { - public: - RealDiv() = default; - ~RealDiv() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(RealDiv, Arithmetic); - explicit RealDiv(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_REAL_DIV_H_ diff --git a/mindspore/lite/src/ops/reciprocal.cc b/mindspore/lite/src/ops/reciprocal.cc deleted file mode 100644 index 5944b1a2f1..0000000000 --- a/mindspore/lite/src/ops/reciprocal.cc +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/reciprocal.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Reciprocal::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Reciprocal; - } - if (this->primitive_->value.type != schema::PrimitiveType_Reciprocal) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::ReciprocalT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else -PrimitiveC *ReciprocalCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ReciprocalRegistry(schema::PrimitiveType_Reciprocal, ReciprocalCreator); -#endif -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/reciprocal.h b/mindspore/lite/src/ops/reciprocal.h deleted file mode 100644 index 838a8d4ccd..0000000000 --- a/mindspore/lite/src/ops/reciprocal.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_RECIPROCAL_H_ -#define MINDSPORE_LITE_SRC_OPS_RECIPROCAL_H_ - -#include "src/ops/arithmetic_self.h" -#ifdef PRIMITIVE_WRITEABLE -#include -#endif - -namespace mindspore { -namespace lite { -class Reciprocal : public ArithmeticSelf { - public: - Reciprocal() = default; - ~Reciprocal() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Reciprocal, ArithmeticSelf); - explicit Reciprocal(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateReciprocal(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Reciprocal, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; - } -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_RECIPROCAL_H_ diff --git a/mindspore/lite/src/ops/reduce.cc b/mindspore/lite/src/ops/reduce.cc deleted file mode 100644 index ed9d2cff1c..0000000000 --- a/mindspore/lite/src/ops/reduce.cc +++ /dev/null @@ -1,221 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/reduce.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector Reduce::GetAxes() const { return this->primitive_->value.AsReduce()->axes; } -int Reduce::GetKeepDims() const { return this->primitive_->value.AsReduce()->keepDims; } -int Reduce::GetMode() const { return this->primitive_->value.AsReduce()->mode; } -bool Reduce::GetReduceToEnd() const { return this->primitive_->value.AsReduce()->reduceToEnd; } -float Reduce::GetCoeff() const { return this->primitive_->value.AsReduce()->coeff; } - -void Reduce::SetAxes(const std::vector &axes) { this->primitive_->value.AsReduce()->axes = axes; } -void Reduce::SetKeepDims(int keep_dims) { this->primitive_->value.AsReduce()->keepDims = keep_dims; } -void Reduce::SetMode(int mode) { this->primitive_->value.AsReduce()->mode = (schema::ReduceMode)mode; } -void Reduce::SetReduceToEnd(bool reduce_to_end) { this->primitive_->value.AsReduce()->reduceToEnd = reduce_to_end; } -void Reduce::SetCoeff(float coeff) { this->primitive_->value.AsReduce()->coeff = coeff; } - -int Reduce::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Reduce; - } - if (this->primitive_->value.type != schema::PrimitiveType_Reduce) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ReduceT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - if (prim.name() == "ReduceMean") { - attr->mode = schema::ReduceMode_ReduceMean; - } else if (prim.name() == "ReduceSum") { - attr->mode = schema::ReduceMode_ReduceSum; - } else if (prim.name() == "ReduceMax") { - attr->mode = schema::ReduceMode_ReduceMax; - } else if (prim.name() == "ReduceMin") { - attr->mode = schema::ReduceMode_ReduceMin; - } else if (prim.name() == "ReduceProd") { - attr->mode = schema::ReduceMode_ReduceProd; - } else if (prim.name() == "ReduceSumSquare") { - attr->mode = schema::ReduceMode_ReduceSumSquare; - } else if (prim.name() == "ReduceAll") { - attr->mode = schema::ReduceMode_ReduceAll; - } else { - MS_LOG(ERROR) << "Not supported reduce mode: " << prim.name(); - return RET_ERROR; - } - - attr->keepDims = GetValue(prim.GetAttr("keep_dims")); - if (inputs.size() == kAnfPopulaterInputNumTwo) { - auto inputNode = inputs.at(kAnfPopulaterInputNumOne); - MS_ASSERT(inputNode != nullptr); - if (inputNode->isa()) { - auto valueNode = inputNode->cast(); - MS_ASSERT(valueNode != nullptr); - auto value = valueNode->value(); - MS_ASSERT(value != nullptr); - if (value->isa()) { - auto valTuplPtr = dyn_cast(value); - MS_ASSERT(valTuplPtr != nullptr); - for (size_t i = 0; i < valTuplPtr->size(); i++) { - auto elem = (*valTuplPtr)[i]; - MS_ASSERT(elem != nullptr); - attr->axes.emplace_back(CastToInt(elem).front()); - } - } else { - int axes_item = CastToInt(value).front(); - attr->axes.push_back(axes_item); - } - } - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else - -std::vector Reduce::GetAxes() const { - auto fb_vector = this->primitive_->value_as_Reduce()->axes(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Reduce::GetKeepDims() const { return this->primitive_->value_as_Reduce()->keepDims(); } -int Reduce::GetMode() const { return this->primitive_->value_as_Reduce()->mode(); } -bool Reduce::GetReduceToEnd() const { return this->primitive_->value_as_Reduce()->reduceToEnd(); } -float Reduce::GetCoeff() const { return this->primitive_->value_as_Reduce()->coeff(); } -int Reduce::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Reduce(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Reduce return nullptr"; - return RET_ERROR; - } - std::vector axes; - if (attr->axes() != nullptr) { - for (int i = 0; i < static_cast(attr->axes()->size()); i++) { - axes.push_back(attr->axes()->data()[i]); - } - } - auto val_offset = - schema::CreateReduceDirect(*fbb, &axes, attr->keepDims(), attr->mode(), attr->reduceToEnd(), attr->coeff()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Reduce, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ReduceCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ReduceRegistry(schema::PrimitiveType_Reduce, ReduceCreator); -#endif - -namespace { -constexpr size_t kInputSize = 1; -constexpr size_t kOutputSize = 1; -} // namespace -int Reduce::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() < kInputSize || outputs_.size() != kOutputSize) { - return RET_ERROR; - } - auto input = inputs_.front(); - auto output = outputs_.front(); - if (input == nullptr || output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if (this->primitive_ == nullptr) { - return RET_NULL_PTR; - } - - bool keep_dims = static_cast(GetKeepDims()); - std::vector in_shape = input->shape(); - std::vector out_shape; - const auto &axes = GetAxes(); - auto num_axes = axes.size(); - int rank = static_cast(in_shape.size()); - std::vector actual_axes(axes.begin(), axes.end()); - - if (GetReduceToEnd()) { - if (num_axes != 1) { - MS_LOG(ERROR) << "Reduce when reduce_to_end, num of axis should be 1, got " << num_axes; - return RET_ERROR; - } - - int begin_axis; - begin_axis = axes.at(0) < 0 ? axes.at(0) + rank : axes.at(0); - for (auto i = begin_axis + 1; i < rank; ++i) { - actual_axes.emplace_back(i); - } - num_axes = rank - begin_axis; - keep_dims = false; - } - // reduce on all axes - if (num_axes == 0) { - if (keep_dims) { - for (size_t i = 0; i < in_shape.size(); i++) { - out_shape.push_back(1); - } - } - output->set_shape(out_shape); - output->set_data_type(input->data_type()); - return RET_OK; - } - // reduce on selected axes - for (size_t i = 0; i < in_shape.size(); i++) { - bool reduce_axis = false; - for (size_t idx = 0; idx < num_axes; ++idx) { - if (static_cast(actual_axes.at(idx)) == i || - static_cast(actual_axes.at(idx) + in_shape.size()) == i) { - reduce_axis = true; - break; - } - } - if (reduce_axis) { - if (keep_dims) { - out_shape.push_back(1); - } - } else { - out_shape.push_back(in_shape.at(i)); - } - } - output->set_shape(out_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/reduce.h b/mindspore/lite/src/ops/reduce.h deleted file mode 100644 index 321c942a2d..0000000000 --- a/mindspore/lite/src/ops/reduce.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" -#include "schema/model_generated.h" - -namespace mindspore { -namespace lite { -class Reduce : public PrimitiveC { - public: - Reduce() = default; - ~Reduce() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Reduce, PrimitiveC); - explicit Reduce(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetAxes(const std::vector &axes); - void SetKeepDims(int keep_dims); - void SetMode(int mode); - void SetReduceToEnd(bool reduce_to_end); - void SetCoeff(float coeff); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - std::vector GetAxes() const; - int GetKeepDims() const; - int GetMode() const; - bool GetReduceToEnd() const; - float GetCoeff() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ diff --git a/mindspore/lite/src/ops/reshape.cc b/mindspore/lite/src/ops/reshape.cc deleted file mode 100644 index afe2164105..0000000000 --- a/mindspore/lite/src/ops/reshape.cc +++ /dev/null @@ -1,242 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/reshape.h" -#include -#include -#include "include/errorcode.h" -#include "src/common/log_adapter.h" -#include "src/tensor.h" -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Reshape::GetFormat() const { return this->primitive_->value.AsReshape()->format; } -std::vector Reshape::GetShape() const { return this->primitive_->value.AsReshape()->shape; } - -void Reshape::SetFormat(int format) { this->primitive_->value.AsReshape()->format = (schema::Format)format; } -void Reshape::SetShape(const std::vector &shape) { this->primitive_->value.AsReshape()->shape = shape; } -int Reshape::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Reshape; - } - if (this->primitive_->value.type != schema::PrimitiveType_Reshape) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ReshapeT(); - MS_ASSERT(inputs.size() == kAnfPopulaterInputNumThree - 1); - auto inputNode = inputs.at(kAnfPopulaterInputNumTwo - 1); - if (inputNode->isa()) { - auto valueNode = inputNode->cast(); - MS_ASSERT(valueNode != nullptr); - auto val = valueNode->value(); - MS_ASSERT(val != nullptr); - if (val->isa()) { - auto tuple = val->cast(); - MS_ASSERT(tuple != nullptr); - for (size_t i = 0; i < tuple->size(); ++i) { - auto elem = tuple->value().at(i); - MS_ASSERT(elem != nullptr); - attr->shape.emplace_back(CastToInt(elem).front()); - } - } else { - int dim = CastToInt(val).front(); - attr->shape = {dim}; - } - } - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} - -#else - -int Reshape::GetFormat() const { return this->primitive_->value_as_Reshape()->format(); } -std::vector Reshape::GetShape() const { - auto fb_vector = this->primitive_->value_as_Reshape()->shape(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Reshape::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Reshape(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Reshape return nullptr"; - return RET_ERROR; - } - std::vector shape; - if (attr->shape() != nullptr) { - for (int i = 0; i < static_cast(attr->shape()->size()); i++) { - shape.push_back(attr->shape()->data()[i]); - } - } - auto val_offset = schema::CreateReshapeDirect(*fbb, attr->format(), &shape); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Reshape, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ReshapeCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ReshapeRegistry(schema::PrimitiveType_Reshape, ReshapeCreator); -#endif - -int Reshape::CalNewShape(const Tensor *in_tensor, std::vector *out_shape) const { - size_t in_shape_size = 1; - for (size_t i = 0; i < in_tensor->shape().size(); i++) { - in_shape_size *= in_tensor->shape().at(i); - } - int64_t infer_index = -1; - size_t out_shape_size = 1; - for (size_t i = 0; i < out_shape->size(); i++) { - if (out_shape->at(i) == -1) { - if (infer_index == -1) { - infer_index = i; - } else { - MS_LOG(ERROR) << "output shape should has no more than one dim which need infer"; - return RET_INFER_ERR; - } - } else if (out_shape->at(i) < 0) { - MS_LOG(ERROR) << "output shape dim should be non-negative"; - return RET_INFER_ERR; - } else if (out_shape->at(i) == 0) { - if (in_tensor->ElementsNum() != 0) { - out_shape->at(i) = in_tensor->shape().at(i); - out_shape_size *= out_shape->at(i); - } else { - out_shape_size = 0; - break; - } - } else { - out_shape_size *= out_shape->at(i); - } - } - if (infer_index == -1 && out_shape_size != in_shape_size) { - MS_LOG(ERROR) << "output shapeSize: " << out_shape_size << " should be equal to input shapeSize: " << in_shape_size; - return RET_INFER_ERR; - } - if (infer_index != -1) { - out_shape->at(infer_index) = in_shape_size / out_shape_size; - } - return RET_OK; -} -template -void CalShape(const T *data, const std::vector &inputs, std::vector *out_shape, int shape_size) { - int input_count = inputs[0]->ElementsNum(); - int index = 0; - int size = 1; - for (int i = 0; i < shape_size; i++) { - if (static_cast(data[i]) == -1) { - index = i; - } else if (static_cast(data[i]) == 0) { - size *= inputs[0]->shape().at(i); - } else { - size *= data[i]; - } - out_shape->push_back(data[i]); - } - if (static_cast(data[index]) == -1) { - (*out_shape).at(index) = input_count / size; - } -} -int Reshape::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - out_shape_.clear(); - if (inputs_.size() == kDoubleNum) { - auto shape_tensor = inputs_.at(1); - if (shape_tensor->IsConst()) { - if (shape_tensor->data_c() == nullptr || (shape_tensor->shape().size() == 1 && shape_tensor->shape()[0] == 0)) { - MS_LOG(DEBUG) << "reshape to a scalar."; - output->set_shape(out_shape_); - return RET_OK; - } - } - if (shape_tensor->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - size_t shape_size = shape_tensor->ElementsNum(); - switch (shape_tensor->data_type()) { - case kNumberTypeInt8: { - auto data = reinterpret_cast(shape_tensor->MutableData()); - CalShape(data, inputs_, &out_shape_, shape_size); - } break; - case kNumberTypeInt32: { - auto data = reinterpret_cast(shape_tensor->MutableData()); - CalShape(data, inputs_, &out_shape_, shape_size); - } break; - case kNumberTypeInt64: { - auto data = reinterpret_cast(shape_tensor->MutableData()); - CalShape(data, inputs_, &out_shape_, shape_size); - } break; - case kNumberTypeFloat: { - auto data = reinterpret_cast(shape_tensor->MutableData()); - CalShape(data, inputs_, &out_shape_, shape_size); - } break; - case kNumberTypeUInt32: { - auto data = reinterpret_cast(shape_tensor->MutableData()); - CalShape(data, inputs_, &out_shape_, shape_size); - } break; - default: { - MS_LOG(ERROR) << "Reshape weight tensor has unsupported dataType: " << shape_tensor->data_type(); - return RET_INFER_ERR; - } - } - } else if (inputs_.size() == kSingleNum) { - for (size_t i = 0; i < GetShape().size(); ++i) { - out_shape_.push_back(GetShape().at(i)); - } - } else { - MS_LOG(ERROR) << "inputs tensor size invalid."; - return RET_INFER_ERR; - } - auto ret = CalNewShape(inputs_.front(), &out_shape_); - if (ret != RET_OK) { - MS_LOG(ERROR) << "CalNewShape error"; - return ret; - } - output->set_shape(out_shape_); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/reshape.h b/mindspore/lite/src/ops/reshape.h deleted file mode 100644 index 38bdcd7691..0000000000 --- a/mindspore/lite/src/ops/reshape.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_RESHAPE_H_ -#define MINDSPORE_LITE_SRC_OPS_RESHAPE_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Reshape : public PrimitiveC { - public: - Reshape() = default; - ~Reshape() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Reshape, PrimitiveC); - explicit Reshape(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; - void SetFormat(int format); - void SetShape(const std::vector &shape); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - std::vector GetShape() const; - std::vector GetOutputShape() { return out_shape_; } - - private: - int CalNewShape(const lite::Tensor *in_tensor, std::vector *out_shape) const; - std::vector out_shape_; -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_RESHAPE_H_ diff --git a/mindspore/lite/src/ops/resize.cc b/mindspore/lite/src/ops/resize.cc deleted file mode 100644 index daff37aef1..0000000000 --- a/mindspore/lite/src/ops/resize.cc +++ /dev/null @@ -1,261 +0,0 @@ -/** - * Copyright 2020-2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/resize.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Resize::GetFormat() const { return this->primitive_->value.AsResize()->format; } -int Resize::GetMethod() const { return this->primitive_->value.AsResize()->method; } -int64_t Resize::GetNewHeight() const { return this->primitive_->value.AsResize()->newHeight; } -int64_t Resize::GetNewWidth() const { return this->primitive_->value.AsResize()->newWidth; } -bool Resize::GetPreserveAspectRatio() const { return this->primitive_->value.AsResize()->preserveAspectRatio; } -int Resize::GetCoordinateTransformMode() const { return this->primitive_->value.AsResize()->coordinateTransformMode; } - -void Resize::SetFormat(int format) { this->primitive_->value.AsResize()->format = (schema::Format)format; } -void Resize::SetMethod(int method) { this->primitive_->value.AsResize()->method = (schema::ResizeMethod)method; } -void Resize::SetNewHeight(int64_t new_height) { this->primitive_->value.AsResize()->newHeight = new_height; } -void Resize::SetNewWidth(int64_t new_width) { this->primitive_->value.AsResize()->newWidth = new_width; } -void Resize::SetCoordinateTransformMode(int coordinate_transform_mode) { - this->primitive_->value.AsResize()->coordinateTransformMode = - static_cast(coordinate_transform_mode); -} -void Resize::SetPreserveAspectRatio(bool preserve_aspect_ratio) { - this->primitive_->value.AsResize()->preserveAspectRatio = preserve_aspect_ratio; -} - -int Resize::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Resize; - } - if (this->primitive_->value.type != schema::PrimitiveType_Resize) { - MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ResizeT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new attr value failed"; - return RET_ERROR; - } - if (prim.instance_name() == "ResizeNearestNeighbor") { - attr->method = schema::ResizeMethod_NEAREST; - } else if (prim.instance_name() == "ResizeBilinear") { - attr->method = schema::ResizeMethod_LINEAR; - } else { - delete attr; - MS_LOG(ERROR) << "wrong resize type"; - return RET_ERROR; - } - std::vector targetSize = CastToInt(prim.GetAttr("size")); - attr->newHeight = targetSize.at(0); - attr->newWidth = targetSize.at(1); - attr->alignCorners = GetValue(prim.GetAttr("align_corners")); - if (attr->alignCorners) { - attr->coordinateTransformMode = schema::CoordinateTransformMode_ALIGN_CORNERS; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - if (attr != nullptr) { - delete attr; - } - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else - -int Resize::GetFormat() const { return this->primitive_->value_as_Resize()->format(); } -int Resize::GetMethod() const { return this->primitive_->value_as_Resize()->method(); } -int64_t Resize::GetNewHeight() const { return this->primitive_->value_as_Resize()->newHeight(); } -int64_t Resize::GetNewWidth() const { return this->primitive_->value_as_Resize()->newWidth(); } -int Resize::GetCoordinateTransformMode() const { - return this->primitive_->value_as_Resize()->coordinateTransformMode(); -} -bool Resize::GetPreserveAspectRatio() const { return this->primitive_->value_as_Resize()->preserveAspectRatio(); } -int Resize::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Resize(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Resize return nullptr"; - return RET_ERROR; - } - auto val_offset = - schema::CreateResize(*fbb, attr->format(), attr->method(), attr->newHeight(), attr->newWidth(), - attr->alignCorners(), attr->preserveAspectRatio(), attr->coordinateTransformMode(), - attr->cubicCoeff(), attr->excludeOutside(), attr->extrapolationValue(), attr->nearestMode()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Resize, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ResizeCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ResizeRegistry(schema::PrimitiveType_Resize, ResizeCreator); -#endif - -namespace { -constexpr int kInputRank = 4; -} // namespace -int64_t Resize::new_height() const { return new_height_; } -int64_t Resize::new_width() const { return new_width_; } -int Resize::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - auto input = inputs_.front(); - if (input == nullptr) { - return RET_ERROR; - } - if (!input->shape().empty() && input->shape().size() != kInputRank) { - MS_LOG(ERROR) << "Size of input shape is wrong."; - return RET_ERROR; - } - - auto output = outputs_.front(); - if (output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - std::vector output_shape; - output_shape.push_back(input->Batch()); - auto ret = CalculateNewHeightAndWidth(inputs_); - if (ret == RET_OK) { - output_shape.push_back(new_height_); - output_shape.push_back(new_width_); - output_shape.push_back(input->Channel()); - output->set_shape(output_shape); - } - return ret; -} - -int Resize::CalculateNewHeightAndWidth(const std::vector &inputs) { - auto input = inputs.front(); - if (inputs.size() == kDoubleNum) { - auto shape_tensor = inputs.at(1); - if (shape_tensor->data_c() == nullptr) { - MS_LOG(INFO) << "Do infer shape in runtime."; - return RET_INFER_INVALID; - } - size_t shape_size = shape_tensor->ElementsNum(); - switch (shape_size) { - case kQuadrupleNum: { - if (shape_tensor->data_type() == kNumberTypeInt32) { - auto data = reinterpret_cast(shape_tensor->data_c()); - if (data == nullptr) { - MS_LOG(INFO) << "Resize op size can't cast int."; - return RET_INFER_INVALID; - } - switch (shape_tensor->format()) { - case schema::Format_NCHW: - new_height_ = data[2]; - new_width_ = data[3]; - break; - case schema::Format_NHWC: - new_height_ = data[1]; - new_width_ = data[2]; - break; - default: - MS_LOG(INFO) << "Resize don't support tensor format."; - return RET_INFER_INVALID; - } - } else if (shape_tensor->data_type() == kNumberTypeFloat32) { - auto data = reinterpret_cast(shape_tensor->data_c()); - if (data == nullptr) { - MS_LOG(INFO) << "Resize op size can't cast float."; - return RET_INFER_INVALID; - } - switch (shape_tensor->format()) { - case schema::Format_NCHW: - new_height_ = data[2] * input->Height(); - new_width_ = data[3] * input->Width(); - break; - case schema::Format_NHWC: - new_height_ = data[1] * input->Height(); - new_width_ = data[2] * input->Width(); - break; - default: - MS_LOG(INFO) << "Resize don't support tensor format."; - return RET_INFER_INVALID; - } - } - break; - } - case kDoubleNum: { - auto data = reinterpret_cast(shape_tensor->data_c()); - if (data == nullptr) { - MS_LOG(INFO) << "Resize op size can't cast float."; - return RET_INFER_INVALID; - } - new_height_ = data[0]; - new_width_ = data[1]; - break; - } - case kSingleNum: { - // caffe zoom_factor - int scale; - if (shape_tensor->data_type() == kNumberTypeInt32) { - auto data = reinterpret_cast(shape_tensor->data_c()); - if (data == nullptr) { - MS_LOG(INFO) << "Resize op size can't cast int."; - return RET_INFER_INVALID; - } - scale = data[0]; - } else { - MS_LOG(ERROR) << "Unsupported data type:" << shape_tensor->data_type(); - return RET_INFER_ERR; - } - new_height_ = input->Height() + (input->Height() - 1) * (scale - 1); - new_width_ = input->Width() + (input->Width() - 1) * (scale - 1); - break; - } - default: { - MS_LOG(ERROR) << "Unsupported shape size:" << shape_size; - return RET_INFER_ERR; - } - } - } else if (inputs.size() == kSingleNum) { - new_height_ = GetNewHeight(); - new_width_ = GetNewWidth(); - } else if (inputs.size() == kQuadrupleNum) { - if (inputs[3]->data_c() == nullptr) { - return RET_INFER_INVALID; - } - new_height_ = static_cast(inputs.at(3)->data_c())[0]; - new_height_ = static_cast(inputs.at(3)->data_c())[1]; - } else { - MS_LOG(ERROR) << "inputs tensor size invalid."; - return RET_INFER_ERR; - } - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/resize.h b/mindspore/lite/src/ops/resize.h deleted file mode 100644 index 275ac07399..0000000000 --- a/mindspore/lite/src/ops/resize.h +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright 2020-2021 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Resize : public PrimitiveC { - public: - Resize() = default; - ~Resize() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Resize, PrimitiveC); - explicit Resize(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFormat(int format); - void SetMethod(int method); - void SetNewHeight(int64_t new_height); - void SetNewWidth(int64_t new_width); - void SetPreserveAspectRatio(bool preserve_aspect_ratio); - void SetCoordinateTransformMode(int coordinate_transform_mode); - int UnPackAttr(const Primitive &prim, const std::vector &inputs); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetFormat() const; - int GetMethod() const; - int64_t GetNewHeight() const; - int64_t GetNewWidth() const; - bool GetPreserveAspectRatio() const; - int GetCoordinateTransformMode() const; - - int64_t new_height() const; - int64_t new_width() const; - - private: - int CalculateNewHeightAndWidth(const std::vector &inputs); - int64_t new_height_; - int64_t new_width_; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ diff --git a/mindspore/lite/src/ops/return.cc b/mindspore/lite/src/ops/return.cc deleted file mode 100644 index 401c886001..0000000000 --- a/mindspore/lite/src/ops/return.cc +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/return.h" -#include - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Return::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Return; - } - if (this->primitive_->value.type != schema::PrimitiveType_Return) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::ReturnT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -PrimitiveC *ReturnCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ReturnRegistry(schema::PrimitiveType_Return, ReturnCreator); -#endif - -namespace { -constexpr size_t kInputSize = 1; -constexpr size_t kOutputSize = 1; -} // namespace -int Return::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kInputSize || outputs_.size() != kOutputSize) { - return RET_ERROR; - } - auto input = inputs_.front(); - auto output = outputs_.front(); - if (input == nullptr || output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - if (this->primitive_ == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_shape(input->shape()); - output->set_format(input->format()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/return.h b/mindspore/lite/src/ops/return.h deleted file mode 100644 index f1c4c389c6..0000000000 --- a/mindspore/lite/src/ops/return.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RETURN_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RETURN_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Return : public PrimitiveC { - public: - Return() = default; - ~Return() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Return, PrimitiveC); - explicit Return(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RETURN_H_ diff --git a/mindspore/lite/src/ops/reverse.cc b/mindspore/lite/src/ops/reverse.cc deleted file mode 100644 index 26efd182a5..0000000000 --- a/mindspore/lite/src/ops/reverse.cc +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/reverse.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -std::vector Reverse::GetAxis() const { return this->primitive_->value.AsReverse()->axis; } - -void Reverse::SetAxis(const std::vector &axis) { this->primitive_->value.AsReverse()->axis = axis; } - -#else - -std::vector Reverse::GetAxis() const { - auto fb_vector = this->primitive_->value_as_Reverse()->axis(); - return std::vector(fb_vector->begin(), fb_vector->end()); -} -int Reverse::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Reverse(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Reverse return nullptr"; - return RET_ERROR; - } - std::vector axis; - if (attr->axis() != nullptr) { - for (int i = 0; i < static_cast(attr->axis()->size()); i++) { - axis.push_back(attr->axis()->data()[i]); - } - } - auto val_offset = schema::CreateReverseDirect(*fbb, &axis); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Reverse, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ReverseCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ReverseRegistry(schema::PrimitiveType_Reverse, ReverseCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/reverse.h b/mindspore/lite/src/ops/reverse.h deleted file mode 100644 index f29d3414a6..0000000000 --- a/mindspore/lite/src/ops/reverse.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Reverse : public PrimitiveC { - public: - Reverse() = default; - ~Reverse() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Reverse, PrimitiveC); - explicit Reverse(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(const std::vector &axis); - -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - std::vector GetAxis() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ diff --git a/mindspore/lite/src/ops/reverse_sequence.cc b/mindspore/lite/src/ops/reverse_sequence.cc deleted file mode 100644 index 08c52ebcd4..0000000000 --- a/mindspore/lite/src/ops/reverse_sequence.cc +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/reverse_sequence.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ReverseSequence::GetSeqAxis() const { return this->primitive_->value.AsReverseSequence()->seqAxis; } -int ReverseSequence::GetBatchAxis() const { return this->primitive_->value.AsReverseSequence()->batchAxis; } - -void ReverseSequence::SetSeqAxis(int seq_axis) { this->primitive_->value.AsReverseSequence()->seqAxis = seq_axis; } -void ReverseSequence::SetBatchAxis(int batch_axis) { - this->primitive_->value.AsReverseSequence()->batchAxis = batch_axis; -} - -#else - -int ReverseSequence::GetSeqAxis() const { return this->primitive_->value_as_ReverseSequence()->seqAxis(); } -int ReverseSequence::GetBatchAxis() const { return this->primitive_->value_as_ReverseSequence()->batchAxis(); } -int ReverseSequence::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto attr = primitive->value_as_ReverseSequence(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ReverseSequence return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateReverseSequence(*fbb, attr->seqAxis(), attr->batchAxis()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ReverseSequence, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ReverseSequenceCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ReverseSequenceRegistry(schema::PrimitiveType_ReverseSequence, ReverseSequenceCreator); - -#endif - -int ReverseSequence::InferShape(std::vector inputs, std::vector outputs) { - auto input = inputs.front(); - auto output = outputs.front(); - MS_ASSERT(input != nullptr); - MS_ASSERT(output != nullptr); - - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - output->set_shape(input->shape()); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/reverse_sequence.h b/mindspore/lite/src/ops/reverse_sequence.h deleted file mode 100644 index dd473d1d6f..0000000000 --- a/mindspore/lite/src/ops/reverse_sequence.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ - -#include -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ReverseSequence : public PrimitiveC { - public: - ReverseSequence() = default; - ~ReverseSequence() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ReverseSequence, PrimitiveC); - explicit ReverseSequence(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetSeqAxis(int seq_axis); - void SetBatchAxis(int batch_axis); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetSeqAxis() const; - int GetBatchAxis() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ diff --git a/mindspore/lite/src/ops/rfft.cc b/mindspore/lite/src/ops/rfft.cc deleted file mode 100644 index 0fe7734e7b..0000000000 --- a/mindspore/lite/src/ops/rfft.cc +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/rfft.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Rfft::GetFftLength() const { return this->primitive_->value.AsRfft()->fftLength; } - -void Rfft::SetFftLength(int fft_length) { this->primitive_->value.AsRfft()->fftLength = fft_length; } - -#else -int Rfft::GetFftLength() const { return this->primitive_->value_as_Rfft()->fftLength(); } -int Rfft::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Rfft(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Add return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateRfft(*fbb, attr->fftLength()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Rfft, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *RfftCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RfftRegistry(schema::PrimitiveType_Rfft, RfftCreator); -#endif -int Rfft::InferShape(std::vector inputs_, std::vector outputs_) { - auto input = inputs_.front(); - MS_ASSERT(input != nullptr); - auto output = outputs_.front(); - MS_ASSERT(output != nullptr); - output->set_data_type(TypeId::kNumberTypeComplex64); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto input_shape = input->shape(); - input_shape.at(input_shape.size() - 1) = GetFftLength() / 2 + 1; - input_shape.push_back(2); - outputs_.front()->set_shape(input_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/rfft.h b/mindspore/lite/src/ops/rfft.h deleted file mode 100644 index 0ec0ccd877..0000000000 --- a/mindspore/lite/src/ops/rfft.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_RFFT_H_ -#define LITE_MINDSPORE_LITE_C_OPS_RFFT_H_ - -#include -#include -#include -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Rfft : public PrimitiveC { - public: - Rfft() = default; - ~Rfft() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Rfft, PrimitiveC); - explicit Rfft(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetFftLength(int fft_length); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetFftLength() const; - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_RFFT_H_ diff --git a/mindspore/lite/src/ops/roi_pooling.cc b/mindspore/lite/src/ops/roi_pooling.cc deleted file mode 100644 index 6a0704392e..0000000000 --- a/mindspore/lite/src/ops/roi_pooling.cc +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/roi_pooling.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int ROIPooling::GetPooledH() const { return this->primitive_->value.AsROIPooling()->pooledH; } -int ROIPooling::GetPooledW() const { return this->primitive_->value.AsROIPooling()->pooledW; } -float ROIPooling::GetScale() const { return this->primitive_->value.AsROIPooling()->scale; } - -void ROIPooling::SetPooledH(int pooled_h) { this->primitive_->value.AsROIPooling()->pooledH = pooled_h; } -void ROIPooling::SetPooledW(int pooled_w) { this->primitive_->value.AsROIPooling()->pooledW = pooled_w; } -void ROIPooling::SetScale(float scale) { this->primitive_->value.AsROIPooling()->scale = scale; } - -#else - -int ROIPooling::GetPooledH() const { return this->primitive_->value_as_ROIPooling()->pooledH(); } -int ROIPooling::GetPooledW() const { return this->primitive_->value_as_ROIPooling()->pooledW(); } -float ROIPooling::GetScale() const { return this->primitive_->value_as_ROIPooling()->scale(); } -int ROIPooling::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto attr = primitive->value_as_ROIPooling(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_ROIPooling return nullptr"; - return RET_ERROR; - } - - auto val_offset = schema::CreateROIPooling(*fbb, attr->pooledH(), attr->pooledW(), attr->scale()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ROIPooling, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ROIPoolingCreator(const schema::Primitive *primitive) { - return PrimitiveC::NewPrimitiveC(primitive); -} -Registry ROIPoolingRegistry(schema::PrimitiveType_ROIPooling, ROIPoolingCreator); -#endif - -int ROIPooling::InferShape(std::vector inputs_, std::vector outputs_) { - MS_ASSERT(this->primitive_ != nullptr); - if (inputs_.size() != kDoubleNum) { - MS_LOG(ERROR) << "inputs number is not equal to " << kDoubleNum; - return RET_ERROR; - } - auto input = inputs_.front(); - if (input == nullptr) { - return RET_NULL_PTR; - } - auto roi = inputs_.at(1); - if (roi == nullptr) { - return RET_NULL_PTR; - } - auto output = outputs_.front(); - if (output == nullptr) { - return RET_NULL_PTR; - } - output->set_data_type(input->data_type()); - output->set_format(input->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - - auto new_h = GetPooledH(); - auto new_w = GetPooledW(); - auto shape_data = roi->shape(); - std::vector output_shape; - output_shape.push_back(shape_data[0]); - output_shape.push_back(new_h); - output_shape.push_back(new_w); - output_shape.push_back(input->Channel()); - output->set_shape(output_shape); - return RET_OK; -} -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/roi_pooling.h b/mindspore/lite/src/ops/roi_pooling.h deleted file mode 100644 index c1b942fb61..0000000000 --- a/mindspore/lite/src/ops/roi_pooling.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ -#define LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ROIPooling : public PrimitiveC { - public: - ROIPooling() = default; - ~ROIPooling() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ROIPooling, PrimitiveC); - explicit ROIPooling(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetPooledH(int pooled_h); - void SetPooledW(int pooled_w); - void SetScale(float scale); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; - int GetPooledH() const; - int GetPooledW() const; - float GetScale() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ diff --git a/mindspore/lite/src/ops/round.cc b/mindspore/lite/src/ops/round.cc deleted file mode 100644 index 35512ef604..0000000000 --- a/mindspore/lite/src/ops/round.cc +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/round.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -#else -int Round::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto val_offset = schema::CreateRound(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Round, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *RoundCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RoundRegistry(schema::PrimitiveType_Round, RoundCreator); - -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/round.h b/mindspore/lite/src/ops/round.h deleted file mode 100644 index 9586a797fe..0000000000 --- a/mindspore/lite/src/ops/round.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_ROUND_H_ -#define LITE_MINDSPORE_LITE_C_OPS_ROUND_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Round : public ArithmeticSelf { - public: - Round() = default; - ~Round() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Round, ArithmeticSelf); - explicit Round(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_ROUND_H_ diff --git a/mindspore/lite/src/ops/rsqrt.cc b/mindspore/lite/src/ops/rsqrt.cc deleted file mode 100644 index c2ae73ff52..0000000000 --- a/mindspore/lite/src/ops/rsqrt.cc +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/rsqrt.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Rsqrt::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Rsqrt; - } - if (this->primitive_->value.type != schema::PrimitiveType_Rsqrt) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - this->primitive_->value.value = new (std::nothrow) schema::RsqrtT(); - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Rsqrt::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto val_offset = schema::CreateRsqrt(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Rsqrt, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -PrimitiveC *RsqrtCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry RsqrtRegistry(schema::PrimitiveType_Rsqrt, RsqrtCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/rsqrt.h b/mindspore/lite/src/ops/rsqrt.h deleted file mode 100644 index 720975bf2e..0000000000 --- a/mindspore/lite/src/ops/rsqrt.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef MINDSPORE_LITE_SRC_OPS_RSQRT_H_ -#define MINDSPORE_LITE_SRC_OPS_RSQRT_H_ - -#include -#include -#include - -#include "src/ops/arithmetic_self.h" - -namespace mindspore { -namespace lite { -class Rsqrt : public ArithmeticSelf { - public: - Rsqrt() = default; - ~Rsqrt() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Rsqrt, ArithmeticSelf); - explicit Rsqrt(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} - int UnPackAttr(const Primitive &prim, const std::vector &inputs) override; -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif -}; -} // namespace lite -} // namespace mindspore - -#endif // MINDSPORE_LITE_SRC_OPS_RSQRT_H_ diff --git a/mindspore/lite/src/ops/scale.cc b/mindspore/lite/src/ops/scale.cc deleted file mode 100644 index 26362b1d3c..0000000000 --- a/mindspore/lite/src/ops/scale.cc +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/scale.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Scale::GetAxis() const { return this->primitive_->value.AsScale()->axis; } -void Scale::SetAxis(int axis) { this->primitive_->value.AsScale()->axis = axis; } -int Scale::GetActivationType() const { return this->primitive_->value.AsScale()->activationType; } -void Scale::SetActivationType(int activation_type) { - this->primitive_->value.AsScale()->activationType = (schema::ActivationType)activation_type; -} - -#else - -int Scale::GetAxis() const { return this->primitive_->value_as_Scale()->axis(); } -int Scale::GetActivationType() const { return this->primitive_->value_as_Scale()->activationType(); } -int Scale::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Scale(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Scale return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateScale(*fbb, attr->axis(), attr->activationType()); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Scale, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *ScaleCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC(primitive); } -Registry ScaleRegistry(schema::PrimitiveType_Scale, ScaleCreator); -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/scale.h b/mindspore/lite/src/ops/scale.h deleted file mode 100644 index b0d42762c1..0000000000 --- a/mindspore/lite/src/ops/scale.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ -#define LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class Scale : public PrimitiveC { - public: - Scale() = default; - ~Scale() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(Scale, PrimitiveC); - explicit Scale(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} - void SetAxis(int axis); - void SetActivationType(int activation_type); -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int GetAxis() const; - int GetActivationType() const; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ diff --git a/mindspore/lite/src/ops/scatter_nd.cc b/mindspore/lite/src/ops/scatter_nd.cc deleted file mode 100644 index fb5239fdd0..0000000000 --- a/mindspore/lite/src/ops/scatter_nd.cc +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "src/ops/scatter_nd.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif - -namespace mindspore { -namespace lite { - -namespace { -constexpr int kScatterNDInputNum = 3; -constexpr int kScatterNDOutputNum = 1; -constexpr int kScatterShapeIndex = 0; -constexpr int kScatterIndicesIndex = 1; -constexpr int kScatterUpdateIndex = 2; -} // namespace -int ScatterND::InferShape(std::vector inputs_, std::vector outputs_) { - if (inputs_.size() != kScatterNDInputNum) { - MS_LOG(ERROR) << "inputs number is not equal to " << kScatterNDInputNum; - return RET_ERROR; - } - if (outputs_.size() != kScatterNDOutputNum) { - MS_LOG(ERROR) << "outputs number is not equal to " << kScatterNDInputNum; - return RET_ERROR; - } - auto shape = inputs_.at(kScatterShapeIndex); - if (shape == nullptr) { - MS_LOG(ERROR) << "shape null pointer dereferencing."; - return RET_ERROR; - } - auto indices = inputs_.at(kScatterIndicesIndex); - if (indices == nullptr) { - MS_LOG(ERROR) << "indices null pointer dereferencing."; - return RET_ERROR; - } - auto update = inputs_.at(kScatterUpdateIndex); - if (update == nullptr) { - MS_LOG(ERROR) << "update null pointer dereferencing."; - return RET_ERROR; - } - auto output = outputs_.front(); - if (output == nullptr) { - MS_LOG(ERROR) << "output null pointer dereferencing."; - return RET_ERROR; - } - output->set_data_type(update->data_type()); - output->set_format(update->format()); - if (!infer_flag()) { - return RET_INFER_INVALID; - } - auto shape_data = reinterpret_cast(shape->MutableData()); - std::vector out_shape(shape_data, shape_data + shape->ElementsNum()); - output->set_shape(out_shape); - return RET_OK; -} -#ifdef PRIMITIVE_WRITEABLE -#else -int ScatterND::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - - auto val_offset = schema::CreateScatterND(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_ScatterND, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} -#endif - -} // namespace lite -} // namespace mindspore diff --git a/mindspore/lite/src/ops/scatter_nd.h b/mindspore/lite/src/ops/scatter_nd.h deleted file mode 100644 index 35d33cb540..0000000000 --- a/mindspore/lite/src/ops/scatter_nd.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ - -#ifndef LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ -#define LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ - -#include -#include -#include - -#include "src/ops/primitive_c.h" - -namespace mindspore { -namespace lite { -class ScatterND : public PrimitiveC { - public: - ScatterND() = default; - ~ScatterND() = default; -#ifdef PRIMITIVE_WRITEABLE - MS_DECLARE_PARENT(ScatterND, PrimitiveC); - explicit ScatterND(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} -#else - int UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) override; -#endif - int InferShape(std::vector inputs_, std::vector outputs_) override; -}; -} // namespace lite -} // namespace mindspore - -#endif // LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ diff --git a/mindspore/lite/src/ops/schema_def.h b/mindspore/lite/src/ops/schema_def.h deleted file mode 100644 index 2231471b4a..0000000000 --- a/mindspore/lite/src/ops/schema_def.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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. - */ -#ifndef MINDSPORE_LITE_SRC_OPS_SCHEMA_DEF_H_ -#define MINDSPORE_LITE_SRC_OPS_SCHEMA_DEF_H_ -#include -#include "src/ops/schema_register.h" -#ifdef PRIMITIVE_WRITEABLE -#include "ops/conv2d.h" -#include "schema/inner/model_generated.h" -#endif - -#ifdef GEN_SCHEMA_DEF -#define OP_SCHEMA_DEF(OP) \ - namespace mindspore::lite::ops { \ - std::string Gen##OP##Def() { \ - std::string op_def = "table "; \ - op_def.append(#OP); \ - op_def.append(" {\n"); -#elif PRIMITIVE_WRITEABLE -#define OP_SCHEMA_DEF(OP) \ - namespace mindspore::lite::ops { \ - mindspore::schema::OP##T *PrimitiveOp2SchemaOp(const mindspore::OP *op) { \ - mindspore::schema::OP##T *result_op = new (std::nothrow) mindspore::schema::OP##T(); -#else -#define OP_SCHEMA_DEF(OP) -#endif - -#ifdef GEN_SCHEMA_DEF -#define OP_ATTR(key, type) op_def.append(#key).append(": ").append(#type).append(";\n"); -#elif PRIMITIVE_WRITEABLE -#define OP_ATTR(key, type) result_op->key = op->get_##key(); -#else -#define OP_ATTR(key, type) -#endif - -#ifdef GEN_SCHEMA_DEF -#define OP_ATTR_WITH_VALUE(key, type, value) \ - op_def.append(#key).append(": ").append(#type).append(" = ").append(#value).append(";\n"); -#elif PRIMITIVE_WRITEABLE -#define OP_ATTR_WITH_VALUE(key, type, value) result_op->key = op->get_##key(); -#else -#define OP_ATTR_WITH_VALUE(key, type, value) -#endif - -#ifdef GEN_SCHEMA_DEF -#define OP_SCHEMA_DEF_END(OP) \ - op_def.append("}\n\n"); \ - return op_def; \ - } \ - SchemaOpRegister g_schema_op_##OP(Gen##OP##Def); \ - } // namespace mindspore::lite::ops -#elif PRIMITIVE_WRITEABLE -#define OP_SCHEMA_DEF_END(OP) \ - return result_op; \ - } \ - } // namespace mindspore::lite::ops -#else -#define OP_SCHEMA_DEF_END(OP) -#endif -#endif // MINDSPORE_LITE_SRC_OPS_SCHEMA_DEF_H_ diff --git a/mindspore/lite/src/ops/schema_register.h b/mindspore/lite/src/ops/schema_register.h index 1f70762650..6c993be9ee 100644 --- a/mindspore/lite/src/ops/schema_register.h +++ b/mindspore/lite/src/ops/schema_register.h @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,21 +31,26 @@ class SchemaRegisterImpl { void OpPush(GetSchemaDef func) { op_def_funcs_.push_back(func); } - void TypePush(GetSchemaDef func) { type_def_funcs_.push_back(func); } - const std::vector &GetAllOpDefCreateFuncs() const { return op_def_funcs_; } - const std::vector &GetAllTypeDefCreateFuncs() const { return type_def_funcs_; } + void SetPrimTypeGenFunc(GetSchemaDef func) { prim_type_gen_ = func; } + + GetSchemaDef GetPrimTypeGenFunc() const { return prim_type_gen_; } private: std::vector op_def_funcs_; - std::vector type_def_funcs_; + GetSchemaDef prim_type_gen_; }; class SchemaOpRegister { public: explicit SchemaOpRegister(GetSchemaDef func) { SchemaRegisterImpl::Instance()->OpPush(func); } }; + +class PrimitiveTypeRegister { + public: + explicit PrimitiveTypeRegister(GetSchemaDef func) { SchemaRegisterImpl::Instance()->SetPrimTypeGenFunc(func); } +}; } // namespace mindspore::lite::ops #endif // MINDSPORE_LITE_SRC_OPS_SCHEMA_REGISTER_H_ diff --git a/mindspore/lite/src/ops/select.cc b/mindspore/lite/src/ops/select.cc deleted file mode 100644 index 1bcb18dd67..0000000000 --- a/mindspore/lite/src/ops/select.cc +++ /dev/null @@ -1,104 +0,0 @@ -/** - * Copyright 2021 Huawei Technologies Co., Ltd - * - * 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 "src/ops/select.h" - -#ifndef PRIMITIVE_WRITEABLE -#include "src/ops/ops_register.h" -#endif -#include "src/tensorlist.h" - -namespace mindspore { -namespace lite { -#ifdef PRIMITIVE_WRITEABLE -int Select::UnPackAttr(const Primitive &prim, const std::vector &inputs) { - if (this->primitive_ == nullptr) { - this->primitive_ = new (std::nothrow) schema::PrimitiveT; - if (this->primitive_ == nullptr) { - MS_LOG(ERROR) << "new primitiveT failed"; - return RET_ERROR; - } - this->primitive_->value.type = schema::PrimitiveType_Select; - } - if (this->primitive_->value.type != schema::PrimitiveType_Select) { - MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; - return RET_ERROR; - } - if (this->primitive_->value.value == nullptr) { - auto attr = new (std::nothrow) schema::SelectT(); - if (attr == nullptr) { - MS_LOG(ERROR) << "new primitiveT value failed"; - return RET_ERROR; - } - - this->primitive_->value.value = attr; - if (this->primitive_->value.value == nullptr) { - MS_LOG(ERROR) << "primitive value is nullptr"; - return RET_ERROR; - } - } - return RET_OK; -} -#else -int Select::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { - MS_ASSERT(nullptr != primitive); - MS_ASSERT(nullptr != fbb); - auto attr = primitive->value_as_Select(); - if (attr == nullptr) { - MS_LOG(ERROR) << "value_as_Select return nullptr"; - return RET_ERROR; - } - auto val_offset = schema::CreateSelect(*fbb); - auto prim_offset = schema::CreatePrimitive(*fbb, schema::PrimitiveType_Select, val_offset.o); - fbb->Finish(prim_offset); - return RET_OK; -} - -PrimitiveC *SelectCreator(const schema::Primitive *primitive) { return PrimitiveC::NewPrimitiveC