From dd3c4f8a9a052a2f5eba99d0c59d456aa7e348bf Mon Sep 17 00:00:00 2001 From: zhaozhenlong Date: Tue, 1 Dec 2020 17:22:06 +0800 Subject: [PATCH] parameter memory save check and comments --- mindspore/lite/nnacl/flatten.h | 2 ++ mindspore/lite/nnacl/fp32/exp_fp32.h | 4 +++- mindspore/lite/nnacl/fp32/expandDims_fp32.h | 1 + mindspore/lite/nnacl/fp32/fill_fp32.h | 1 + mindspore/lite/nnacl/fp32/gatherNd_fp32.h | 1 + .../lite/nnacl/fp32/local_response_norm_fp32.h | 1 + mindspore/lite/nnacl/fp32/lstm_fp32.h | 3 +++ mindspore/lite/nnacl/fp32/one_hot_fp32.h | 2 ++ mindspore/lite/nnacl/fp32/range_fp32.h | 1 + mindspore/lite/nnacl/gather_parameter.h | 1 + mindspore/lite/nnacl/instance_norm_parameter.h | 2 ++ mindspore/lite/nnacl/l2_norm_parameter.h | 7 +++++-- mindspore/lite/nnacl/layer_norm_parameter.h | 7 +++++-- mindspore/lite/nnacl/lsh_projection_parameter.h | 5 ++++- mindspore/lite/nnacl/matmul_parameter.h | 2 ++ mindspore/lite/nnacl/mul_parameter.h | 2 ++ .../lite/nnacl/non_max_suppression_parameter.h | 1 + mindspore/lite/nnacl/pad_parameter.h | 9 ++++++--- mindspore/lite/nnacl/pooling_parameter.h | 13 ++++++++----- mindspore/lite/nnacl/power_parameter.h | 4 +++- mindspore/lite/nnacl/predict_parameter.h | 2 ++ mindspore/lite/nnacl/prelu_parameter.h | 2 ++ mindspore/lite/nnacl/prior_box.h | 1 + mindspore/lite/src/ops/populate/gather_populate.cc | 5 +++++ mindspore/lite/src/ops/populate/l2_norm_populate.cc | 7 +++++++ .../lite/src/ops/populate/layer_norm_populate.cc | 7 ++++++- 26 files changed, 77 insertions(+), 16 deletions(-) diff --git a/mindspore/lite/nnacl/flatten.h b/mindspore/lite/nnacl/flatten.h index 2d818bb586f..8409b8dd130 100644 --- a/mindspore/lite/nnacl/flatten.h +++ b/mindspore/lite/nnacl/flatten.h @@ -18,7 +18,9 @@ #include "nnacl/op_base.h" typedef struct FlattenParameter { + // Primitive parameter OpParameter op_parameter_; + // other parameter int size; } FlattenParameter; diff --git a/mindspore/lite/nnacl/fp32/exp_fp32.h b/mindspore/lite/nnacl/fp32/exp_fp32.h index fa225d6487c..58686718cd4 100644 --- a/mindspore/lite/nnacl/fp32/exp_fp32.h +++ b/mindspore/lite/nnacl/fp32/exp_fp32.h @@ -20,11 +20,13 @@ #include "nnacl/op_base.h" typedef struct ExpParameter { + // Primitive parameter OpParameter op_parameter_; - int thread_num_; float base_; float scale_; float shift_; + // other parameter + int thread_num_; float in_scale_; float out_scale_; int element_num_; diff --git a/mindspore/lite/nnacl/fp32/expandDims_fp32.h b/mindspore/lite/nnacl/fp32/expandDims_fp32.h index f7c7c1f7138..37586820d21 100644 --- a/mindspore/lite/nnacl/fp32/expandDims_fp32.h +++ b/mindspore/lite/nnacl/fp32/expandDims_fp32.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct ExpandDimsParameter { + // Primitive parameter OpParameter op_parameter_; int dim_; } ExpandDimsParameter; diff --git a/mindspore/lite/nnacl/fp32/fill_fp32.h b/mindspore/lite/nnacl/fp32/fill_fp32.h index 3464a64fd54..6f7b5d7f902 100644 --- a/mindspore/lite/nnacl/fp32/fill_fp32.h +++ b/mindspore/lite/nnacl/fp32/fill_fp32.h @@ -25,6 +25,7 @@ #define FILL_DIMS_MAX_SIZE 4 typedef struct FillParameter { + // Primitive parameter OpParameter op_parameter_; int dims_[FILL_DIMS_MAX_SIZE]; int num_dims_; diff --git a/mindspore/lite/nnacl/fp32/gatherNd_fp32.h b/mindspore/lite/nnacl/fp32/gatherNd_fp32.h index c4702f00b73..ec4f4c4da3f 100644 --- a/mindspore/lite/nnacl/fp32/gatherNd_fp32.h +++ b/mindspore/lite/nnacl/fp32/gatherNd_fp32.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct GatherNdParameter { + // Primitive parameter OpParameter op_parameter_; } GatherNdParameter; diff --git a/mindspore/lite/nnacl/fp32/local_response_norm_fp32.h b/mindspore/lite/nnacl/fp32/local_response_norm_fp32.h index 9722da16730..57046bb32db 100644 --- a/mindspore/lite/nnacl/fp32/local_response_norm_fp32.h +++ b/mindspore/lite/nnacl/fp32/local_response_norm_fp32.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct LocalResponseNormParameter { + // Primitive parameter OpParameter op_parameter_; int depth_radius_; float bias_; diff --git a/mindspore/lite/nnacl/fp32/lstm_fp32.h b/mindspore/lite/nnacl/fp32/lstm_fp32.h index 8a2795aba48..68c7c941781 100644 --- a/mindspore/lite/nnacl/fp32/lstm_fp32.h +++ b/mindspore/lite/nnacl/fp32/lstm_fp32.h @@ -20,11 +20,14 @@ #include "nnacl/op_base.h" typedef struct LstmParameter { + // Primitive parameter OpParameter op_parameter_; + // shape correlative int input_size_; int hidden_size_; // output_size int seq_len_; int batch_; + // other parameter int input_step_; int output_step_; bool bidirectional_; diff --git a/mindspore/lite/nnacl/fp32/one_hot_fp32.h b/mindspore/lite/nnacl/fp32/one_hot_fp32.h index 323ca292beb..caf104a486f 100644 --- a/mindspore/lite/nnacl/fp32/one_hot_fp32.h +++ b/mindspore/lite/nnacl/fp32/one_hot_fp32.h @@ -23,8 +23,10 @@ #include "nnacl/op_base.h" typedef struct OneHotParameter { + // Primitive parameter OpParameter op_parameter_; int axis_; + // other parameter int depth_; float on_value_; float off_value_; diff --git a/mindspore/lite/nnacl/fp32/range_fp32.h b/mindspore/lite/nnacl/fp32/range_fp32.h index 712c4d4e089..0b1e44c49f0 100644 --- a/mindspore/lite/nnacl/fp32/range_fp32.h +++ b/mindspore/lite/nnacl/fp32/range_fp32.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct RangeParameter { + // Primitive parameter OpParameter op_parameter_; int dType_; int start_; diff --git a/mindspore/lite/nnacl/gather_parameter.h b/mindspore/lite/nnacl/gather_parameter.h index 3b606476b63..d300970417d 100644 --- a/mindspore/lite/nnacl/gather_parameter.h +++ b/mindspore/lite/nnacl/gather_parameter.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct GatherParameter { + // Primitive parameter OpParameter op_parameter_; int axis_; int batchDims_; diff --git a/mindspore/lite/nnacl/instance_norm_parameter.h b/mindspore/lite/nnacl/instance_norm_parameter.h index 8e188eb6d7f..30e9b814f4d 100644 --- a/mindspore/lite/nnacl/instance_norm_parameter.h +++ b/mindspore/lite/nnacl/instance_norm_parameter.h @@ -20,9 +20,11 @@ #include "nnacl/op_base.h" typedef struct InstanceNormParameter { + // Primitive parameter OpParameter op_parameter_; float epsilon_; float momentum_; + // shape correlative int channel_; } InstanceNormParameter; diff --git a/mindspore/lite/nnacl/l2_norm_parameter.h b/mindspore/lite/nnacl/l2_norm_parameter.h index 22bba27126c..d8127dfa885 100644 --- a/mindspore/lite/nnacl/l2_norm_parameter.h +++ b/mindspore/lite/nnacl/l2_norm_parameter.h @@ -20,13 +20,16 @@ #include "nnacl/quantization/quantize.h" typedef struct L2NormParameter { + // Primitive parameter OpParameter op_parameter_; - int *axis_; - size_t axis_num_; float epsilon_; + int *axis_; + // shape correlative + size_t axis_num_; int data_num_; int *shape_; size_t shape_num_; + // other parameter ActType act_type_; } L2NormParameter; diff --git a/mindspore/lite/nnacl/layer_norm_parameter.h b/mindspore/lite/nnacl/layer_norm_parameter.h index f7dfe27933f..a63d5b02e93 100644 --- a/mindspore/lite/nnacl/layer_norm_parameter.h +++ b/mindspore/lite/nnacl/layer_norm_parameter.h @@ -20,11 +20,14 @@ #include "nnacl/quantization/quantize.h" typedef struct LayerNormParameter { + // Primitive parameter OpParameter op_parameter_; - int *normalized_shape_; - int normalized_dims_; float epsilon_; bool elementwise_affine_; + // shape correlative + int *normalized_shape_; + int normalized_dims_; + // other parameter int thread_count_; int thread_outsize_; } LayerNormParameter; diff --git a/mindspore/lite/nnacl/lsh_projection_parameter.h b/mindspore/lite/nnacl/lsh_projection_parameter.h index ff81ba6dcae..9275279b053 100644 --- a/mindspore/lite/nnacl/lsh_projection_parameter.h +++ b/mindspore/lite/nnacl/lsh_projection_parameter.h @@ -20,9 +20,12 @@ #include "nnacl/op_base.h" typedef struct LshProjectionParameter { + // Primitive parameter OpParameter op_parameter_; - int lsh_type_; + // shape correlative int hash_shape_[2]; + // other parameter + int lsh_type_; int feature_num_; char **hash_buffs_; size_t hash_buff_size_; diff --git a/mindspore/lite/nnacl/matmul_parameter.h b/mindspore/lite/nnacl/matmul_parameter.h index 17a8a12955f..22a10a95970 100644 --- a/mindspore/lite/nnacl/matmul_parameter.h +++ b/mindspore/lite/nnacl/matmul_parameter.h @@ -36,7 +36,9 @@ typedef void (*MATMUL_OPT_DP_FUNC)(const int8_t *a, const int8_t *b, int8_t *dst typedef enum OutType { OutType_C8 = 0, OutType_Nhwc = 1, OutType_TileC8 = 2 } OutType; typedef struct MatMulParameter { + // Primitive parameter OpParameter op_parameter_; + // other parameter int row_; int col_; int row_4_; diff --git a/mindspore/lite/nnacl/mul_parameter.h b/mindspore/lite/nnacl/mul_parameter.h index 0f8386300cf..6b5c61cfde7 100644 --- a/mindspore/lite/nnacl/mul_parameter.h +++ b/mindspore/lite/nnacl/mul_parameter.h @@ -21,7 +21,9 @@ #include "nnacl/quantization/quantize.h" typedef struct MulParameter { + // Primitive parameter OpParameter op_parameter_; + // other parameter int thread_count_; MulQuantArg mul_quant_arg_; } MulParameter; diff --git a/mindspore/lite/nnacl/non_max_suppression_parameter.h b/mindspore/lite/nnacl/non_max_suppression_parameter.h index e655c484218..409dc9b3426 100644 --- a/mindspore/lite/nnacl/non_max_suppression_parameter.h +++ b/mindspore/lite/nnacl/non_max_suppression_parameter.h @@ -20,6 +20,7 @@ #include "nnacl/op_base.h" typedef struct NMSParameter { + // Primitive parameter OpParameter op_parameter_; int center_point_box_; } NMSParameter; diff --git a/mindspore/lite/nnacl/pad_parameter.h b/mindspore/lite/nnacl/pad_parameter.h index 107bb6c8d74..26ca358e1ad 100644 --- a/mindspore/lite/nnacl/pad_parameter.h +++ b/mindspore/lite/nnacl/pad_parameter.h @@ -23,15 +23,18 @@ #define DEFAULT_PAD_NDIMS 4 typedef struct PadParameter { + // Primitive parameter OpParameter op_parameter_; - PadQuantArg pad_quant_arg_; int paddings_[MAX_PAD_SIZE]; - int padding_length; int pad_mode_; float constant_value_; - int mirror_offset_; + // shape correlative + int padding_length; + // other parameter int in_strides[DEFAULT_PAD_NDIMS]; int out_strides[DEFAULT_PAD_NDIMS]; + int mirror_offset_; + PadQuantArg pad_quant_arg_; } PadParameter; #endif // MINDSPORE_LITE_NNACL_PAD_PARAMETER_H_ diff --git a/mindspore/lite/nnacl/pooling_parameter.h b/mindspore/lite/nnacl/pooling_parameter.h index b0839c92ec8..c8012ccb7e9 100644 --- a/mindspore/lite/nnacl/pooling_parameter.h +++ b/mindspore/lite/nnacl/pooling_parameter.h @@ -24,13 +24,18 @@ typedef enum PoolMode { PoolMode_No, PoolMode_MaxPool, PoolMode_AvgPool } PoolMo typedef enum RoundMode { RoundMode_No, RoundMode_Ceil, RoundMode_Floor } RoundMode; typedef struct PoolingParameter { + // Primitive parameter OpParameter op_parameter_; PoolMode pool_mode_; RoundMode round_mode_; ActType act_type_; - QuantArg **quant_args_; + int avg_mode_; + bool global_; int window_w_; int window_h_; + int stride_w_; + int stride_h_; + // shape correlative int input_w_; int input_h_; int input_batch_; @@ -43,11 +48,9 @@ typedef struct PoolingParameter { int pad_d_; int pad_l_; int pad_r_; - int stride_w_; - int stride_h_; + // other parameter int thread_num_; - int avg_mode_; - bool global_; + QuantArg **quant_args_; bool quantize_; } PoolingParameter; diff --git a/mindspore/lite/nnacl/power_parameter.h b/mindspore/lite/nnacl/power_parameter.h index 3c425fb25cb..a81bd24f923 100644 --- a/mindspore/lite/nnacl/power_parameter.h +++ b/mindspore/lite/nnacl/power_parameter.h @@ -21,11 +21,13 @@ #include "nnacl/quantization/quantize.h" typedef struct PowerParameter { + // Primitive parameter OpParameter op_parameter_; - PowerQuantArg quant_arg_; float power_; float scale_; float shift_; + // other parameter + PowerQuantArg quant_arg_; bool broadcast_; } PowerParameter; diff --git a/mindspore/lite/nnacl/predict_parameter.h b/mindspore/lite/nnacl/predict_parameter.h index 997523f5992..a4e0fc857b2 100644 --- a/mindspore/lite/nnacl/predict_parameter.h +++ b/mindspore/lite/nnacl/predict_parameter.h @@ -18,7 +18,9 @@ #include "nnacl/op_base.h" typedef struct { + // Primitive parameter OpParameter op_parameter_; + // other parameter int output_num; float weight_threshold; } PredictParameter; diff --git a/mindspore/lite/nnacl/prelu_parameter.h b/mindspore/lite/nnacl/prelu_parameter.h index 2227c2eaedb..5e1087f7f7d 100644 --- a/mindspore/lite/nnacl/prelu_parameter.h +++ b/mindspore/lite/nnacl/prelu_parameter.h @@ -19,7 +19,9 @@ #include "nnacl/op_base.h" typedef struct PReluParameter { + // Primitive parameter OpParameter op_parameter_; + // other parameter float *slope_; bool channelShared; int tile_block_; diff --git a/mindspore/lite/nnacl/prior_box.h b/mindspore/lite/nnacl/prior_box.h index 8742ebe7aef..fdfefd0e270 100644 --- a/mindspore/lite/nnacl/prior_box.h +++ b/mindspore/lite/nnacl/prior_box.h @@ -24,6 +24,7 @@ #define PRIOR_BOX_MAX_NUM 8 #define PRIOR_BOX_VAR_NUM 4 typedef struct PriorBoxParameter { + // Primitive parameter OpParameter op_parameter_; int32_t min_sizes_size; int32_t min_sizes[PRIOR_BOX_MAX_NUM]; diff --git a/mindspore/lite/src/ops/populate/gather_populate.cc b/mindspore/lite/src/ops/populate/gather_populate.cc index d42b1ef9e47..bec392473a4 100644 --- a/mindspore/lite/src/ops/populate/gather_populate.cc +++ b/mindspore/lite/src/ops/populate/gather_populate.cc @@ -33,6 +33,11 @@ OpParameter *PopulateGatherParameter(const mindspore::lite::PrimitiveC *primitiv } 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(); return reinterpret_cast(gather_param); diff --git a/mindspore/lite/src/ops/populate/l2_norm_populate.cc b/mindspore/lite/src/ops/populate/l2_norm_populate.cc index 9f3e8a1aa05..1d7921de954 100644 --- a/mindspore/lite/src/ops/populate/l2_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/l2_norm_populate.cc @@ -15,6 +15,7 @@ */ #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" @@ -31,8 +32,14 @@ OpParameter *PopulateL2NormParameter(const mindspore::lite::PrimitiveC *primitiv 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; + } l2_norm_parameter->axis_ = reinterpret_cast(malloc(axis_vec.size() * sizeof(int))); if (l2_norm_parameter->axis_ == nullptr) { MS_LOG(ERROR) << "malloc axis_ data failed"; diff --git a/mindspore/lite/src/ops/populate/layer_norm_populate.cc b/mindspore/lite/src/ops/populate/layer_norm_populate.cc index a2ae098d047..8acba9a0ce7 100644 --- a/mindspore/lite/src/ops/populate/layer_norm_populate.cc +++ b/mindspore/lite/src/ops/populate/layer_norm_populate.cc @@ -15,6 +15,7 @@ */ #include "nnacl/layer_norm_parameter.h" +#include #include "src/ops/layer_norm.h" #include "src/ops/primitive_c.h" #include "src/ops/populate/populate_register.h" @@ -32,11 +33,15 @@ OpParameter *PopulateLayerNormParameter(const mindspore::lite::PrimitiveC *primi auto param = reinterpret_cast(const_cast(primitive)); auto normalized_shape = param->GetNormalizedShape(); layer_norm_parameter->normalized_dims_ = normalized_shape.size(); + if (normalized_shape.size() > SIZE_MAX / sizeof(int)) { + MS_LOG(ERROR) << "normalized_shape size too big"; + free(layer_norm_parameter); + return nullptr; + } layer_norm_parameter->normalized_shape_ = reinterpret_cast(malloc(normalized_shape.size() * sizeof(int))); if (layer_norm_parameter->normalized_shape_ == nullptr) { MS_LOG(ERROR) << "malloc layer_norm_parameter->normalized_shape_ failed."; free(layer_norm_parameter); - layer_norm_parameter = nullptr; return nullptr; } for (size_t i = 0; i < normalized_shape.size(); i++) {