From af174ae3d021a6bfbd3ebc276ad372332c38e2d1 Mon Sep 17 00:00:00 2001 From: wangzhe Date: Thu, 17 Sep 2020 15:05:43 +0800 Subject: [PATCH] internal/model.h add ReduceMode & add operator new and operator delete for internal/MSTensor --- mindspore/lite/internal/include/model.h | 12 ++++++++++++ mindspore/lite/internal/include/ms_tensor.h | 9 +++++++++ .../internal/src/kernel/fp32/arithmetic.cc | 9 ++++----- .../lite/internal/src/kernel/fp32/reduce.cc | 5 ++--- mindspore/lite/internal/src/ms_tensor.cc | 19 ++++++++++++++++++- .../lite/test/ut/internal/CMakeLists.txt | 1 + mindspore/lite/test/ut/internal/infer_test.cc | 2 ++ 7 files changed, 48 insertions(+), 9 deletions(-) diff --git a/mindspore/lite/internal/include/model.h b/mindspore/lite/internal/include/model.h index 9142898c43..ba926a8cb5 100644 --- a/mindspore/lite/internal/include/model.h +++ b/mindspore/lite/internal/include/model.h @@ -205,6 +205,18 @@ enum ActivationType { UNKNOW = 16 }; +enum ReduceMode { + ReduceMode_ReduceMean = 0, + ReduceMode_ReduceMax = 1, + ReduceMode_ReduceMin = 2, + ReduceMode_ReduceProd = 3, + ReduceMode_ReduceSum = 4, + ReduceMode_ReduceSumSquare = 5, + ReduceMode_ReduceASum = 6, + ReduceMode_MIN = ReduceMode_ReduceMean, + ReduceMode_MAX = ReduceMode_ReduceASum +}; + typedef struct Node { String name_; NodeType node_type_; diff --git a/mindspore/lite/internal/include/ms_tensor.h b/mindspore/lite/internal/include/ms_tensor.h index 2a1612ff2e..f395e662c1 100644 --- a/mindspore/lite/internal/include/ms_tensor.h +++ b/mindspore/lite/internal/include/ms_tensor.h @@ -136,7 +136,16 @@ typedef struct MSTensor { /// /// \return Byte size of data in MSTensor. size_t Size() const; + + static void *operator new(std::size_t sz); + + static void *operator new[](std::size_t sz); + + static void operator delete(void *ptr, std::size_t sz); + + static void operator delete[](void *ptr, std::size_t sz); } MSTensor; MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape); +void DestroyTensor(MSTensor *ptr); #endif // MINDSPORE_LITE_INCLUDE_MS_TENSOR_H_ diff --git a/mindspore/lite/internal/src/kernel/fp32/arithmetic.cc b/mindspore/lite/internal/src/kernel/fp32/arithmetic.cc index a5f2dd10dc..119ea96bbb 100644 --- a/mindspore/lite/internal/src/kernel/fp32/arithmetic.cc +++ b/mindspore/lite/internal/src/kernel/fp32/arithmetic.cc @@ -22,7 +22,6 @@ #include "src/runtime/allocator.h" #include "nnacl/arithmetic_common.h" #include "nnacl/fp32/arithmetic.h" -#include "schema/ops_generated.h" typedef int (*ArithmeticRun)(float *input0, float *input1, float *output, int element_size); typedef int (*ArithmeticOptRun)(float *input0, float *input1, float *output, int element_size, @@ -167,9 +166,9 @@ int DoArithmeticInferShape(const TensorPtrVector &in_tensors, const TensorPtrVec int ChooseKernel(const int kernel_type, ArithmeticRun *arithmetic_run, ArithmeticParameter *params) { if (kernel_type == KernelType::Mul) { - if (params->activation_type_ == mindspore::schema::ActivationType_RELU) { + if (params->activation_type_ == ActivationType::RELU) { *arithmetic_run = ElementMulRelu; - } else if (params->activation_type_ == mindspore::schema::ActivationType_RELU6) { + } else if (params->activation_type_ == ActivationType::RELU6) { *arithmetic_run = ElementMulRelu6; } else { *arithmetic_run = ElementMul; @@ -183,9 +182,9 @@ int ChooseKernel(const int kernel_type, ArithmeticRun *arithmetic_run, Arithmeti int ChooseOptKernel(const int kernel_type, ArithmeticOptRun *arithmetic_opt_run, ArithmeticParameter *params) { if (kernel_type == KernelType::Mul) { - if (params->activation_type_ == mindspore::schema::ActivationType_RELU) { + if (params->activation_type_ == ActivationType::RELU) { *arithmetic_opt_run = ElementOptMulRelu; - } else if (params->activation_type_ == mindspore::schema::ActivationType_RELU6) { + } else if (params->activation_type_ == ActivationType::RELU6) { *arithmetic_opt_run = ElementOptMulRelu6; } else { *arithmetic_opt_run = ElementOptMul; diff --git a/mindspore/lite/internal/src/kernel/fp32/reduce.cc b/mindspore/lite/internal/src/kernel/fp32/reduce.cc index 77062814dd..9d6cbb679a 100644 --- a/mindspore/lite/internal/src/kernel/fp32/reduce.cc +++ b/mindspore/lite/internal/src/kernel/fp32/reduce.cc @@ -23,7 +23,6 @@ #include "internal/include/errorcode.h" #include "nnacl/reduce_parameter.h" #include "nnacl/fp32/reduce.h" -#include "schema/ops_generated.h" typedef int (*Reducer)(const int outer_size, const int inner_size, const int axis_size, const float *src_data, float *dst_data, const int tid, const int thread_num); @@ -202,9 +201,9 @@ int DoReduce(const TensorPtrVector &in_tensors, const TensorPtrVector &out_tenso ReduceParameter *params = reinterpret_cast(node->primitive_); Reducer reducer = NULL; - if (params->mode_ == mindspore::schema::ReduceMode::ReduceMode_ReduceSum) { + if (params->mode_ == ReduceMode::ReduceMode_ReduceSum) { reducer = ReduceSum; - } else if (params->mode_ == mindspore::schema::ReduceMode::ReduceMode_ReduceMean) { + } else if (params->mode_ == ReduceMode::ReduceMode_ReduceMean) { reducer = ReduceMean; } diff --git a/mindspore/lite/internal/src/ms_tensor.cc b/mindspore/lite/internal/src/ms_tensor.cc index 8f2ab66174..16142a4ce1 100644 --- a/mindspore/lite/internal/src/ms_tensor.cc +++ b/mindspore/lite/internal/src/ms_tensor.cc @@ -17,8 +17,9 @@ #include #include #include "internal/include/ms_tensor.h" + MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape) { - MSTensor *tensor = new (std::nothrow) MSTensor(); + MSTensor *tensor = new MSTensor(); if (tensor == NULL) { return NULL; } @@ -27,6 +28,8 @@ MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape) { return tensor; } +void DestroyTensor(MSTensor *ptr) { delete ptr; } + int MSTensor::ElementsNum() const { int result = 1; for (size_t i = 0; i < shape_.size(); ++i) { @@ -200,3 +203,17 @@ int MSTensor::ElementsC4Num() const { } return result; } + +void *MSTensor::operator new(std::size_t sz) { + void *storage = malloc(sz); + return storage; +} + +void *MSTensor::operator new[](std::size_t sz) { + void *storage = malloc(sz); + return storage; +} + +void MSTensor::operator delete(void *ptr, std::size_t sz) { free(ptr); } + +void MSTensor::operator delete[](void *ptr, std::size_t sz) { free(ptr); } diff --git a/mindspore/lite/test/ut/internal/CMakeLists.txt b/mindspore/lite/test/ut/internal/CMakeLists.txt index 848d63cc74..48ef01de61 100644 --- a/mindspore/lite/test/ut/internal/CMakeLists.txt +++ b/mindspore/lite/test/ut/internal/CMakeLists.txt @@ -40,6 +40,7 @@ set(TEST_LITE_SRC ${LITE_DIR}/internal/src/lite_session.cc ${LITE_DIR}/src/runtime/allocator.cc ${LITE_DIR}/internal/src/ms_tensor.cc + ${LITE_DIR}/internal/src/common/string.cc ${TOP_DIR}/mindspore/core/utils/log_adapter.cc ${TOP_DIR}/mindspore/core/gvar/logging_level.cc ) diff --git a/mindspore/lite/test/ut/internal/infer_test.cc b/mindspore/lite/test/ut/internal/infer_test.cc index b8a5416bad..d9e98c0d66 100644 --- a/mindspore/lite/test/ut/internal/infer_test.cc +++ b/mindspore/lite/test/ut/internal/infer_test.cc @@ -69,6 +69,8 @@ TEST_F(InferTest, TestSession) { } std::cout << "\n"; CompareOutputData(reinterpret_cast(outvec.at(0)->data_), expect_out, kOutSize, 0.000001); + DestroyTensor(in); + DestroyTensor(out); } } // namespace mindspore