From 431e6b81fcac3b348bc3d78a466373098a82359a Mon Sep 17 00:00:00 2001 From: nizhijie Date: Wed, 16 Oct 2024 18:04:58 +0800 Subject: [PATCH 1/2] [CNNLCORE-21117] --- core/gen_case.cpp | 2 +- core/tensor.cpp | 110 ++++++++++++++--------------- core/tensor.h | 171 +++++++++++++++++++++++++++++----------------- 3 files changed, 163 insertions(+), 120 deletions(-) diff --git a/core/gen_case.cpp b/core/gen_case.cpp index dfc103e58..8b79370f0 100644 --- a/core/gen_case.cpp +++ b/core/gen_case.cpp @@ -761,7 +761,7 @@ std::string descToString(mluOpTensorDescriptor_t desc, char delimiter) { tensor_info << " layout: " << mluop::getNameOfTensorLayout(layout) << delimiter; tensor_info << " dtype: " << mluop::getNameOfDataType(dtype) << delimiter; - if (desc->pointer_mode == MLUOP_POINTER_MODE_HOST) { + if (desc->getPointer_mode() == MLUOP_POINTER_MODE_HOST) { tensor_info << " pointer_mode: POINTER_MODE_HOST" << delimiter; if ((total_element_num != 1) || (dim != 0)) { LOG(WARNING) << "[gen_case] Tensor has been set to POINTER_MODE_HOST, " diff --git a/core/tensor.cpp b/core/tensor.cpp index 72af32672..451906727 100644 --- a/core/tensor.cpp +++ b/core/tensor.cpp @@ -390,10 +390,10 @@ mluOpStatus_t MLUOP_WIN_API mluOpCreateGroupTensorDescriptors( static inline mluOpStatus_t mluOpSetTensorDescriptorZeroDim( mluOpTensorDescriptor_t desc) { - if (desc->pointer_mode == MLUOP_POINTER_MODE_HOST) { - desc->dim = 0; - desc->total_element_num = 1; - desc->total_tensor_size = mluop::getSizeOfDataType(desc->dtype); + if (desc->getPointerMode() == MLUOP_POINTER_MODE_HOST) { + desc->setDim(0); + desc->setTotalElementNum(1); + desc->setTotalTensorSize(mluop::getSizeOfDataType(desc->getDtype())) return MLUOP_STATUS_SUCCESS; } else { LOG(ERROR) @@ -412,9 +412,8 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor( PARAM_CHECK("[mluOpSetTensorDescriptor]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptor]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dtype >= 0); - - desc->dtype = dtype; - desc->layout = layout; + desc->setDtype(dtype); + desc->setLayout(layout); if (dimNb == 0) { return mluOpSetTensorDescriptorZeroDim(desc); @@ -431,9 +430,8 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor_v2( PARAM_CHECK("[mluOpSetTensorDescriptor]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptor]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dtype >= 0); - - desc->dtype = dtype; - desc->layout = layout; + desc->setDtype(dtype); + desc->setLayout(layout); if (dimNb == 0) { return mluOpSetTensorDescriptorZeroDim(desc); @@ -448,19 +446,19 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor_v2( // Internal interface. Caller should guarantee parameter validity. static inline void mluOpSetTensorDescriptorDimBase(mluOpTensorDescriptor_t desc, int dimNb) { - if (dimNb != desc->dim) { + if (dimNb != desc->getDim()) { if MLUOP_PREDICT_FALSE (desc->dims != desc->normal_dims) { - delete[] desc->dims; - delete[] desc->strides; + desc->releaseDims(); + desc->releaseStrides(); } if MLUOP_PREDICT_FALSE (dimNb > MLUOP_DIM_MAX) { - desc->dims = new (std::nothrow) int64_t[dimNb]; - desc->strides = new (std::nothrow) int64_t[dimNb]; + desc->setDims(new (std::nothrow) int64_t[dimNb]); + desc->setstrides(new (std::nothrow) int64_t[dimNb]); } else { - desc->dims = desc->normal_dims; - desc->strides = desc->normal_strides; + desc->setDims(desc->getNormalDims) + desc->setStrides(desc->getNormalStrides) } - desc->dim = dimNb; + desc->setDim(dimNb); } } @@ -479,7 +477,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim( bool is_overflow = false; int tmp_num = 0; for (int i = dimNb - 1; i >= 0; --i) { - desc->strides[i] = stride_base; + desc->setStridesIndex(i, stride_base); is_overflow |= __builtin_smul_overflow(stride_base, dimSize[i], &tmp_num); stride_base *= dimSize[i]; } @@ -508,7 +506,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim_v2( mluOpTensorDescriptor_t desc, int dimNb, const int64_t *dimSize) { mluOpSetTensorDescriptorDimBase(desc, dimNb); - memcpy(desc->dims, dimSize, dimNb * sizeof(int64_t)); + memcpy(desc->getDims(), dimSize, dimNb * sizeof(int64_t)); // infer strides of dimNb dimensions and compute total_num & total_size uint64_t stride_base = 1; @@ -621,7 +619,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors_v2( } group_desc[i][0]->total_element_num = strideBase; group_desc[i][0]->total_tensor_size = - group_desc[i][0]->total_element_num * + group_desc[i][0]->getTotalElementNum() * mluop::getSizeOfDataType(group_dtype[i]); // compute new iterator for next loop. @@ -635,20 +633,18 @@ mluOpStatus_t MLUOP_WIN_API mluOpResetTensorDescriptor(mluOpTensorDescriptor_t desc) { PARAM_CHECK("[mluOpResetTensorDescriptor]", desc != NULL); - if MLUOP_PREDICT_FALSE (desc->dims != desc->normal_dims) { - delete[] desc->dims; - desc->dims = desc->normal_dims; + if MLUOP_PREDICT_FALSE (desc->getDims() != desc->getNormalDims()) { + desc->setDims(getNormalDims()); } - if MLUOP_PREDICT_FALSE (desc->strides != desc->normal_strides) { - delete[] desc->strides; - desc->strides = desc->normal_strides; + if MLUOP_PREDICT_FALSE (desc->getStrides() != desc->getNormalStrides()) { + desc->setDims(getNormalStrides()); } - desc->dim = 0; - desc->dtype = MLUOP_DTYPE_FLOAT; + desc->getDim() = 0; + desc->getDtype() = MLUOP_DTYPE_FLOAT; desc->onchip_dtype = MLUOP_DTYPE_INVALID; desc->layout = MLUOP_LAYOUT_ARRAY; - desc->pointer_mode = MLUOP_POINTER_MODE_DEVICE; + desc->getPointer_mode() = MLUOP_POINTER_MODE_DEVICE; desc->total_element_num = 0; desc->total_tensor_size = 0; @@ -668,8 +664,8 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dtype >= 0); - desc->dtype = dtype; - desc->layout = layout; + desc->setDtype(dtype); + desc->setLayout(layout); if (dimNb == 0) { return mluOpSetTensorDescriptorZeroDim(desc); @@ -679,7 +675,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimNb > 0); mluOpSetTensorDescriptorDimBase(desc, dimNb); - std::copy(dimSize, dimSize + dimNb, desc->dims); + std::copy(dimSize, dimSize + dimNb, desc->getDims()); std::copy(dimStride, dimStride + dimNb, desc->strides); // assign total_element_num and total_tensor_size @@ -702,8 +698,8 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dtype >= 0); - desc->dtype = dtype; - desc->layout = layout; + desc->getDtype() = dtype; + desc->setLayout(layout); if MLUOP_PREDICT_FALSE (dimNb == 0) { return mluOpSetTensorDescriptorZeroDim(desc); @@ -712,7 +708,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimStride != NULL); mluOpSetTensorDescriptorDimBase(desc, dimNb); - memcpy(desc->dims, dimSize, dimNb * sizeof(int64_t)); + memcpy(desc->getDims(), dimSize, dimNb * sizeof(int64_t)); memcpy(desc->strides, dimStride, dimNb * sizeof(int64_t)); // assign total_element_num and total_tensor_size @@ -767,7 +763,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPointerMode( PARAM_CHECK("[mluOpSetTensorDescriptorPointerMode]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptorPointerMode]", pointer_mode >= 0); - desc->pointer_mode = pointer_mode; + desc->getPointer_mode() = pointer_mode; return MLUOP_STATUS_SUCCESS; } @@ -782,11 +778,11 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx( PARAM_CHECK("[mluOpGetTensorDescriptorEx]", dimStride != NULL); *layout = desc->layout; - *dtype = desc->dtype; - *dimNb = desc->dim; + *dtype = desc->getDtype(); + *dimNb = desc->getDim(); for (int i = 0; i < *dimNb; ++i) { - dimSize[i] = static_cast(desc->dims[i]); - dimStride[i] = static_cast(desc->strides[i]); + dimSize[i] = static_cast(desc->getDimIndex(i)); + dimStride[i] = static_cast(desc->getStrideIndex(i)); } return MLUOP_STATUS_SUCCESS; @@ -803,11 +799,11 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx_v2( PARAM_CHECK("[mluOpGetTensorDescriptorEx]", dimStride != NULL); *layout = desc->layout; - *dtype = desc->dtype; - *dimNb = desc->dim; + *dtype = desc->getDtype(); + *dimNb = desc->getDim(); for (int i = 0; i < *dimNb; ++i) { - dimSize[i] = desc->dims[i]; - dimStride[i] = desc->strides[i]; + dimSize[i] = desc->getDimIndex(i); + dimStride[i] = desc->getStrideIndex(i); } return MLUOP_STATUS_SUCCESS; @@ -818,10 +814,10 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor( mluOpDataType_t *dtype, int *dimNb, int *dimSize) { PARAM_CHECK("[mluOpGetTensorDescriptor]", desc != NULL); - SET_PARAM_FOR_POINTER(layout, desc->layout); - SET_PARAM_FOR_POINTER(dtype, desc->dtype); - SET_PARAM_FOR_POINTER(dimNb, desc->dim); - SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->dims, desc->dim); + SET_PARAM_FOR_POINTER(layout, desc->getLayout()); + SET_PARAM_FOR_POINTER(dtype, desc->getDtype()); + SET_PARAM_FOR_POINTER(dimNb, desc->getDim()); + SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->getDims(), desc->getDim()); return MLUOP_STATUS_SUCCESS; } @@ -831,10 +827,10 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor_v2( mluOpDataType_t *dtype, int *dimNb, int64_t *dimSize) { PARAM_CHECK("[mluOpGetTensorDescriptor]", desc != NULL); - SET_PARAM_FOR_POINTER(layout, desc->layout); - SET_PARAM_FOR_POINTER(dtype, desc->dtype); - SET_PARAM_FOR_POINTER(dimNb, desc->dim); - SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->dims, desc->dim); + SET_PARAM_FOR_POINTER(layout, desc->getLayout()); + SET_PARAM_FOR_POINTER(dtype, desc->getDtype()); + SET_PARAM_FOR_POINTER(dimNb, desc->getDim()); + SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->getDims(), desc->getDim()); return MLUOP_STATUS_SUCCESS; } @@ -853,7 +849,7 @@ mluOpGetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int *position) { PARAM_CHECK("[mluOpGetTensorDescriptorPosition]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPosition]", position != NULL); - *position = desc->position; + *position = desc->getPosition(); return MLUOP_STATUS_SUCCESS; } @@ -863,8 +859,8 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionAndScale( PARAM_CHECK("[mluOpGetTensorDescriptorPositionAndScale]", position != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPositionAndScale]", scale != NULL); - *position = desc->position; - *scale = desc->scale; + *position = desc->getPosition(); + *scale = desc->getScale(); return MLUOP_STATUS_SUCCESS; } @@ -889,7 +885,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPointerMode( PARAM_CHECK("[mluOpGetTensorDescriptorPointerMode]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPointerMode]", pointer_mode != NULL); - SET_PARAM_FOR_POINTER(pointer_mode, desc->pointer_mode); + SET_PARAM_FOR_POINTER(pointer_mode, desc->getPointer_mode()); return MLUOP_STATUS_SUCCESS; } diff --git a/core/tensor.h b/core/tensor.h index 38fba2a67..0f159fb19 100644 --- a/core/tensor.h +++ b/core/tensor.h @@ -106,35 +106,82 @@ struct alignas(64) mluOpTensorStruct { inline bool isSameDims(const mluOpTensorStruct &other) const; inline bool isSameDims(const mluOpTensorStruct *other) const; inline bool isCpuScalar() const; - - /* Try to pack and align the struct */ - /* ------------------- 64 Bytes - 1 -------------------*/ - int64_t normal_dims[MLUOP_DIM_MAX]; - - /* ------------------- 64 Bytes - 2 -------------------*/ - int64_t normal_strides[MLUOP_DIM_MAX]; - - /* ------------------- 64 Bytes - 3 -------------------*/ - /* Offset - 0 */ - uint64_t total_element_num = 0; - uint64_t total_tensor_size = 0; - int64_t *dims = normal_dims; // point the normal dims as default - int64_t *strides = normal_strides; // point the normal strides as default - /* Offset - 32 */ - int dim = 0; - mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT; - mluOpDataType_t onchip_dtype = MLUOP_DTYPE_INVALID; - mluOpTensorLayout_t layout = MLUOP_LAYOUT_ARRAY; - mluOpPointerMode_t pointer_mode = MLUOP_POINTER_MODE_DEVICE; - - /* Offset - 52 */ - /* To be removed*/ - int position = 0; - float scale = 1; - int offset = 0; - std::vector positions; - std::vector scales; - std::vector offsets; + public: + mluOpTensorLayout_t getLayout() const{ return this->layout; } + void setLayout(mluOpDataType_t newLayout) { this->dtype = newLayout; } + inline uint64_t getTotalTensorSize() const { return this->total_tensor_size; } + inline void setTotalTensorSize(uint64_t newSize) { this->total_tensor_size = newSize; } + inline uint64_t getTotalElementNum() const { return this->total_element_num; } + inline void setTotalElementNum(uint64_t newNum) { this->total_element_num = newNum; } + inline int getPosition() const { return this->position; } + + inline mluOpDataType_t getDtype() const { return this->dtype; } + inline void setDtype(mluOpDataType_t newDtype) { this->dtype = newDtype; } + inline int getDim() const { return this->dim; } + inline void setDim(int newDim) { this->dim = newDim; } + + inline void releaseDims() { delete[] this->dims; } + inline int64_t *getDims() const { return this->dims; } + inline int64_t getDimIndex(size_t index) const { + if ( index >= this->dim ) { + throw std::out_of_range("Index out of range"); + } + return (this->dims)[index]; + } + inline void setDims(int64_t *newDims) { + delete[] this->dims; + this->dims = newDims; + } + + inline void releaseStrides() { delete[] this->strides; } + inline int64_t *getStrides() const { return this->strides; } + inline int64_t getStrideIndex(size_t index) const { + if ( index >= this->dim ) { + throw std::out_of_range("Index out of range"); + } + return (this->strides)[index]; + } + inline void setStridesIndex( size_t index, int64_t newStride ) { + this->strides[index] = newStride; + } + + inline void setStrides(int64_t *newStrides) { + delete[] this->strides; + this->strides = newStrides; + } + + inline mluOpPointerMode_t getPointerMode() const { return this->pointer_mode; } + inline int64_t *getNormalDims() { return this->normal_dims; } + inline int64_t *getNormalStrides() { return this->normal_strides; } + private: + /* Try to pack and align the struct */ + /* ------------------- 64 Bytes - 1 -------------------*/ + int64_t normal_dims[MLUOP_DIM_MAX]; + + /* ------------------- 64 Bytes - 2 -------------------*/ + int64_t normal_strides[MLUOP_DIM_MAX]; + + /* ------------------- 64 Bytes - 3 -------------------*/ + /* Offset - 0 */ + uint64_t total_element_num = 0; + uint64_t total_tensor_size = 0; + int64_t *dims = normal_dims; // point the normal dims as default + int64_t *strides = normal_strides; // point the normal strides as default + /* Offset - 32 */ + int dim = 0; + mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT; + mluOpDataType_t onchip_dtype = MLUOP_DTYPE_INVALID; + mluOpTensorLayout_t layout = MLUOP_LAYOUT_ARRAY; + mluOpPointerMode_t pointer_mode = MLUOP_POINTER_MODE_DEVICE; + + /* Offset - 52 */ + /* To be removed*/ + int position = 0; + float scale = 1; + int offset = 0; + std::vector positions; + std::vector scales; + std::vector offsets; }; // dim_set(rnn) [layer_num, direction, cap_of_cell] @@ -158,7 +205,7 @@ struct mluOpTensorSetStruct { CHECK(!this->tensor_set.empty()); size_t tensor_set_size = 0; for (int i = 0; i < tensor_set.size(); i++) { - tensor_set_size += tensor_set[i]->total_tensor_size; + tensor_set_size += tensor_set[i]->getTotalTensorSize(); } return tensor_set_size; } @@ -175,7 +222,7 @@ struct mluOpTensorSetStruct { int64_t offset = 0; int index = this->getIndex(tensorIndex); for (int i = 0; i < index; i++) { - offset += tensor_set[i]->total_tensor_size; + offset += tensor_set[i]->getTotalTensorSize(); } data_offset[index] = offset; return offset; @@ -189,12 +236,12 @@ struct mluOpTensorSetStruct { inline mluOpDataType_t getDatatype() const { CHECK(!this->tensor_set.empty()); - return this->tensor_set[0]->dtype; + return this->tensor_set[0]->getDtype(); } inline mluOpTensorLayout_t getLayout() const { CHECK(!this->tensor_set.empty()); - return this->tensor_set[0]->layout; + return this->tensor_set[0]->getLayout(); } inline void checkDataOffset() const { @@ -218,7 +265,7 @@ struct mluOpTensorSetStruct { int offset = 0; data_offset[0] = offset; for (int i = 0; i < tensor_num - 1; i++) { - offset += tensor_set[i]->total_tensor_size; + offset += tensor_set[i]->getTotalTensorSize(); data_offset[i + 1] = offset; } return data_offset; @@ -288,16 +335,16 @@ inline int mluOpDataTypeBytes(const mluOpDataType_t dt) { } inline int64_t mluOpGetTensordimN(const mluOpTensorDescriptor_t desc) { - switch (desc->layout) { + switch (desc->getLayout()) { case MLUOP_LAYOUT_NCHW: case MLUOP_LAYOUT_NHWC: case MLUOP_LAYOUT_NDHWC: case MLUOP_LAYOUT_NLC: - return desc->dims[0]; + return desc->getDimIndex(0); case MLUOP_LAYOUT_NCDHW: - return desc->dims[0]; + return desc->getDimIndex(0); case MLUOP_LAYOUT_HWCN: - return desc->dims[3]; + return desc->getDimIndex(3); default: LOG(ERROR) << "Failed to call dimN, illegal layout in TensorDescriptor.\n"; @@ -306,11 +353,11 @@ inline int64_t mluOpGetTensordimN(const mluOpTensorDescriptor_t desc) { } inline int64_t mluOpGetTensordimD(const mluOpTensorDescriptor_t desc) { - switch (desc->layout) { + switch (desc->getLayout()) { case MLUOP_LAYOUT_NDHWC: - return desc->dims[1]; + return desc->getDimIndex(1); case MLUOP_LAYOUT_NCDHW: - return desc->dims[2]; + return desc->getDimIndex(2); default: LOG(ERROR) << "Failed to call dimD, illegal layout in TensorDescriptor.\n"; @@ -319,18 +366,18 @@ inline int64_t mluOpGetTensordimD(const mluOpTensorDescriptor_t desc) { } inline int64_t mluOpGetTensordimC(const mluOpTensorDescriptor_t desc) { - switch (desc->layout) { + switch (desc->getLayout()) { case MLUOP_LAYOUT_NCHW: - return desc->dims[1]; + return desc->getDimIndex(1); case MLUOP_LAYOUT_NHWC: - return desc->dims[3]; + return desc->getDimIndex(3); case MLUOP_LAYOUT_NDHWC: - return desc->dims[4]; + return desc->getDimIndex(4); case MLUOP_LAYOUT_NCDHW: - return desc->dims[1]; + return desc->getDimIndex(1); case MLUOP_LAYOUT_HWCN: case MLUOP_LAYOUT_NLC: - return desc->dims[2]; + return desc->getDimIndex(2); default: LOG(ERROR) << "Failed to call dimC, illegal layout in TensorDescriptor.\n"; @@ -339,17 +386,17 @@ inline int64_t mluOpGetTensordimC(const mluOpTensorDescriptor_t desc) { } inline int64_t mluOpGetTensordimH(const mluOpTensorDescriptor_t desc) { - switch (desc->layout) { + switch (desc->getLayout()) { case MLUOP_LAYOUT_NCHW: - return desc->dims[2]; + return desc->getDimIndex(2); case MLUOP_LAYOUT_NHWC: - return desc->dims[1]; + return desc->getDimIndex(1); case MLUOP_LAYOUT_NDHWC: - return desc->dims[2]; + return desc->getDimIndex(2); case MLUOP_LAYOUT_NCDHW: - return desc->dims[3]; + return desc->getDimIndex(3); case MLUOP_LAYOUT_HWCN: - return desc->dims[0]; + return desc->getDimIndex(0); default: LOG(ERROR) << "Failed to call dimH, illegal layout in TensorDescriptor.\n"; @@ -358,18 +405,18 @@ inline int64_t mluOpGetTensordimH(const mluOpTensorDescriptor_t desc) { } inline int64_t mluOpGetTensordimW(const mluOpTensorDescriptor_t desc) { - switch (desc->layout) { + switch (desc->getLayout()) { case MLUOP_LAYOUT_NCHW: - return desc->dims[3]; + return desc->getDimIndex(3); case MLUOP_LAYOUT_NHWC: - return desc->dims[2]; + return desc->getDimIndex(2); case MLUOP_LAYOUT_NDHWC: - return desc->dims[3]; + return desc->getDimIndex(3); case MLUOP_LAYOUT_NCDHW: - return desc->dims[4]; + return desc->getDimIndex(4); case MLUOP_LAYOUT_HWCN: case MLUOP_LAYOUT_NLC: - return desc->dims[1]; + return desc->getDimIndex(1); default: LOG(ERROR) << "Failed to call dimW, illegal layout in TensorDescriptor.\n"; @@ -466,12 +513,12 @@ inline int64_t mluOpGetSeqDataDimC(const mluOpSeqDataDescriptor_t desc) { inline uint64_t shapeStrideCount(const mluOpTensorDescriptor_t desc) { uint64_t total = 1; - for (int i = 0; i < desc->dim; ++i) { - if (desc->dims[i] == 0) { + for (int i = 0; i < desc->getDim(); ++i) { + if (desc->getDimIndex(i) == 0) { total = 0; break; } - total += (desc->dims[i] - 1) * desc->strides[i]; + total += (desc->getDimIndex(i) - 1) * desc->getStrideIndex(i); } return total; } From be6150890b333159e1337fb3d1af47e729a15909 Mon Sep 17 00:00:00 2001 From: nizhijie Date: Wed, 16 Oct 2024 18:06:57 +0800 Subject: [PATCH 2/2] [CNNLCORE-21427] access variable in tensor struct through function in tensor.cpp and tensor.h --- core/gen_case.cpp | 2 +- core/gen_case.h | 2 +- core/tensor.cpp | 186 ++++++------ core/tensor.h | 264 +++++++++++++----- .../active_rotated_filter_forward.cpp | 2 +- .../mlu_op_gtest/pb_gtest/src/zoo/fft/fft.cpp | 4 +- .../masked_col2im_forward.cpp | 2 +- 7 files changed, 290 insertions(+), 172 deletions(-) diff --git a/core/gen_case.cpp b/core/gen_case.cpp index 8b79370f0..2ad7dbf4d 100644 --- a/core/gen_case.cpp +++ b/core/gen_case.cpp @@ -761,7 +761,7 @@ std::string descToString(mluOpTensorDescriptor_t desc, char delimiter) { tensor_info << " layout: " << mluop::getNameOfTensorLayout(layout) << delimiter; tensor_info << " dtype: " << mluop::getNameOfDataType(dtype) << delimiter; - if (desc->getPointer_mode() == MLUOP_POINTER_MODE_HOST) { + if (desc->getPointerMode() == MLUOP_POINTER_MODE_HOST) { tensor_info << " pointer_mode: POINTER_MODE_HOST" << delimiter; if ((total_element_num != 1) || (dim != 0)) { LOG(WARNING) << "[gen_case] Tensor has been set to POINTER_MODE_HOST, " diff --git a/core/gen_case.h b/core/gen_case.h index 7e60c9de7..0bfe6c405 100644 --- a/core/gen_case.h +++ b/core/gen_case.h @@ -492,7 +492,7 @@ class PbNode { uint64_t data_size = total_num * mluop::getSizeOfDataType(dtype); void *data = malloc(data_size); auto memcpy_dir = - (tensors[index].desc->pointer_mode == MLUOP_POINTER_MODE_HOST + (tensors[index].desc->getPointerMode() == MLUOP_POINTER_MODE_HOST ? cnrtMemcpyHostToHost : cnrtMemcpyDevToHost); if (cnrtSuccess == cnrtMemcpy(data, diff --git a/core/tensor.cpp b/core/tensor.cpp index 451906727..f34569052 100644 --- a/core/tensor.cpp +++ b/core/tensor.cpp @@ -388,12 +388,12 @@ mluOpStatus_t MLUOP_WIN_API mluOpCreateGroupTensorDescriptors( return MLUOP_STATUS_SUCCESS; } -static inline mluOpStatus_t mluOpSetTensorDescriptorZeroDim( +inline mluOpStatus_t mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim( mluOpTensorDescriptor_t desc) { - if (desc->getPointerMode() == MLUOP_POINTER_MODE_HOST) { - desc->setDim(0); - desc->setTotalElementNum(1); - desc->setTotalTensorSize(mluop::getSizeOfDataType(desc->getDtype())) + if (desc->pointer_mode == MLUOP_POINTER_MODE_HOST) { + desc->dim = 0; + desc->total_element_num = 1; + desc->total_tensor_size = mluop::getSizeOfDataType(desc->dtype); return MLUOP_STATUS_SUCCESS; } else { LOG(ERROR) @@ -406,17 +406,18 @@ static inline mluOpStatus_t mluOpSetTensorDescriptorZeroDim( } } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptor( mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, mluOpDataType_t dtype, int dimNb, const int *dimSize) { PARAM_CHECK("[mluOpSetTensorDescriptor]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptor]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dtype >= 0); - desc->setDtype(dtype); - desc->setLayout(layout); + + desc->dtype = dtype; + desc->layout = layout; if (dimNb == 0) { - return mluOpSetTensorDescriptorZeroDim(desc); + return mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim(desc); } else { PARAM_CHECK("[mluOpSetTensorDescriptor]", dimNb > 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dimSize != NULL); @@ -424,17 +425,18 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor( } } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptor_v2( mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, mluOpDataType_t dtype, int dimNb, const int64_t *dimSize) { PARAM_CHECK("[mluOpSetTensorDescriptor]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptor]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dtype >= 0); - desc->setDtype(dtype); - desc->setLayout(layout); + + desc->dtype = dtype; + desc->layout = layout; if (dimNb == 0) { - return mluOpSetTensorDescriptorZeroDim(desc); + return mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim(desc); } else { PARAM_CHECK("[mluOpSetTensorDescriptor]", dimNb > 0); PARAM_CHECK("[mluOpSetTensorDescriptor]", dimSize != NULL); @@ -444,29 +446,28 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor_v2( } // Internal interface. Caller should guarantee parameter validity. -static inline void mluOpSetTensorDescriptorDimBase(mluOpTensorDescriptor_t desc, - int dimNb) { - if (dimNb != desc->getDim()) { +inline void mluOpTensorStruct::mluOpSetTensorDescriptorDimBase(mluOpTensorDescriptor_t desc,int dimNb) { + if (dimNb != desc->dim) { if MLUOP_PREDICT_FALSE (desc->dims != desc->normal_dims) { - desc->releaseDims(); - desc->releaseStrides(); + delete[] desc->dims; + delete[] desc->strides; } if MLUOP_PREDICT_FALSE (dimNb > MLUOP_DIM_MAX) { - desc->setDims(new (std::nothrow) int64_t[dimNb]); - desc->setstrides(new (std::nothrow) int64_t[dimNb]); + desc->dims = new (std::nothrow) int64_t[dimNb]; + desc->strides = new (std::nothrow) int64_t[dimNb]; } else { - desc->setDims(desc->getNormalDims) - desc->setStrides(desc->getNormalStrides) + desc->dims = desc->normal_dims; + desc->strides = desc->normal_strides; } - desc->setDim(dimNb); + desc->dim = dimNb; } } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorDim( mluOpTensorDescriptor_t desc, int dimNb, const int *dimSize) { if (dimNb == 0) { CHECK_RETURN("[mluOpSetTensorDescriptorDim]", - mluOpSetTensorDescriptorZeroDim(desc)); + mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim(desc)); } else { mluOpSetTensorDescriptorDimBase(desc, dimNb); std::copy(dimSize, dimSize + dimNb, desc->dims); @@ -477,7 +478,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim( bool is_overflow = false; int tmp_num = 0; for (int i = dimNb - 1; i >= 0; --i) { - desc->setStridesIndex(i, stride_base); + desc->strides[i] = stride_base; is_overflow |= __builtin_smul_overflow(stride_base, dimSize[i], &tmp_num); stride_base *= dimSize[i]; } @@ -502,11 +503,10 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorDim_v2( mluOpTensorDescriptor_t desc, int dimNb, const int64_t *dimSize) { mluOpSetTensorDescriptorDimBase(desc, dimNb); - - memcpy(desc->getDims(), dimSize, dimNb * sizeof(int64_t)); + memcpy(desc->dims, dimSize, dimNb * sizeof(int64_t)); // infer strides of dimNb dimensions and compute total_num & total_size uint64_t stride_base = 1; @@ -538,7 +538,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim_v2( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetGroupTensorDescriptors( mluOpTensorDescriptor_t **group_desc, const mluOpTensorLayout_t *group_layout, const mluOpDataType_t *group_dtype, const int *group_dimNb, const int *group_dimSize, const int desc_num) { @@ -584,7 +584,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetGroupTensorDescriptors_v2( mluOpTensorDescriptor_t **group_desc, const mluOpTensorLayout_t *group_layout, const mluOpDataType_t *group_dtype, const int *group_dimNb, const int64_t *group_dimSize, const int desc_num) { @@ -619,7 +619,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors_v2( } group_desc[i][0]->total_element_num = strideBase; group_desc[i][0]->total_tensor_size = - group_desc[i][0]->getTotalElementNum() * + group_desc[i][0]->total_element_num * mluop::getSizeOfDataType(group_dtype[i]); // compute new iterator for next loop. @@ -630,21 +630,23 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors_v2( } mluOpStatus_t MLUOP_WIN_API -mluOpResetTensorDescriptor(mluOpTensorDescriptor_t desc) { +mluOpTensorStruct::mluOpResetTensorDescriptor(mluOpTensorDescriptor_t desc) { PARAM_CHECK("[mluOpResetTensorDescriptor]", desc != NULL); - if MLUOP_PREDICT_FALSE (desc->getDims() != desc->getNormalDims()) { - desc->setDims(getNormalDims()); + if MLUOP_PREDICT_FALSE (desc->dims != desc->normal_dims) { + delete[] desc->dims; + desc->dims = desc->normal_dims; } - if MLUOP_PREDICT_FALSE (desc->getStrides() != desc->getNormalStrides()) { - desc->setDims(getNormalStrides()); + if MLUOP_PREDICT_FALSE (desc->strides != desc->normal_strides) { + delete[] desc->strides; + desc->strides = desc->normal_strides; } - desc->getDim() = 0; - desc->getDtype() = MLUOP_DTYPE_FLOAT; + desc->dim = 0; + desc->dtype = MLUOP_DTYPE_FLOAT; desc->onchip_dtype = MLUOP_DTYPE_INVALID; desc->layout = MLUOP_LAYOUT_ARRAY; - desc->getPointer_mode() = MLUOP_POINTER_MODE_DEVICE; + desc->pointer_mode = MLUOP_POINTER_MODE_DEVICE; desc->total_element_num = 0; desc->total_tensor_size = 0; @@ -656,7 +658,7 @@ mluOpResetTensorDescriptor(mluOpTensorDescriptor_t desc) { return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorEx( mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, mluOpDataType_t dtype, int dimNb, const int *dimSize, const int *dimStride) { @@ -664,18 +666,18 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dtype >= 0); - desc->setDtype(dtype); - desc->setLayout(layout); + desc->dtype = dtype; + desc->layout = layout; if (dimNb == 0) { - return mluOpSetTensorDescriptorZeroDim(desc); + return mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim(desc); } else { PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimSize != NULL); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimStride != NULL); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimNb > 0); - mluOpSetTensorDescriptorDimBase(desc, dimNb); - std::copy(dimSize, dimSize + dimNb, desc->getDims()); + mluOpTensorStruct::mluOpSetTensorDescriptorDimBase(desc, dimNb); + std::copy(dimSize, dimSize + dimNb, desc->dims); std::copy(dimStride, dimStride + dimNb, desc->strides); // assign total_element_num and total_tensor_size @@ -690,7 +692,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( } } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorEx_v2( mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, mluOpDataType_t dtype, int dimNb, const int64_t *dimSize, const int64_t *dimStride) { @@ -698,17 +700,17 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( PARAM_CHECK("[mluOpSetTensorDescriptorEx]", layout >= 0); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dtype >= 0); - desc->getDtype() = dtype; - desc->setLayout(layout); + desc->dtype = dtype; + desc->layout = layout; if MLUOP_PREDICT_FALSE (dimNb == 0) { - return mluOpSetTensorDescriptorZeroDim(desc); + return mluOpTensorStruct::mluOpSetTensorDescriptorZeroDim(desc); } else { PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimSize != NULL); PARAM_CHECK("[mluOpSetTensorDescriptorEx]", dimStride != NULL); - mluOpSetTensorDescriptorDimBase(desc, dimNb); - memcpy(desc->getDims(), dimSize, dimNb * sizeof(int64_t)); + mluOpTensorStruct::mluOpSetTensorDescriptorDimBase(desc, dimNb); + memcpy(desc->dims, dimSize, dimNb * sizeof(int64_t)); memcpy(desc->strides, dimStride, dimNb * sizeof(int64_t)); // assign total_element_num and total_tensor_size @@ -723,7 +725,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( } } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorOnchipDataType( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorOnchipDataType( mluOpTensorDescriptor_t desc, mluOpDataType_t onchip_dtype) { PARAM_CHECK("[mluOpSetTensorDescriptorOnchipDataType]", desc != NULL); @@ -732,15 +734,15 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorOnchipDataType( } mluOpStatus_t MLUOP_WIN_API -mluOpSetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int position) { +mluOpTensorStruct::mluOpSetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int position) { PARAM_CHECK("[mluOpSetTensorDescriptorPosition]", desc != NULL); desc->position = position; return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionAndScale( - mluOpTensorDescriptor_t desc, int position, float scale) { +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorPositionAndScale( + mluOpTensorDescriptor_t desc, int position, float scale) { PARAM_CHECK("[mluOpSetTensorDescriptorPositionAndScale]", desc != NULL); desc->position = position; @@ -748,7 +750,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionAndScale( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionScaleAndOffset( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorPositionScaleAndOffset( mluOpTensorDescriptor_t desc, int position, float scale, int offset) { PARAM_CHECK("[mluOpSetTensorDescriptorPositionScaleAndOffset]", desc != NULL); @@ -758,17 +760,17 @@ mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionScaleAndOffset( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPointerMode( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpSetTensorDescriptorPointerMode( mluOpTensorDescriptor_t desc, mluOpPointerMode_t pointer_mode) { PARAM_CHECK("[mluOpSetTensorDescriptorPointerMode]", desc != NULL); PARAM_CHECK("[mluOpSetTensorDescriptorPointerMode]", pointer_mode >= 0); - desc->getPointer_mode() = pointer_mode; + desc->pointer_mode = pointer_mode; return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx( - const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorEx( + mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, mluOpDataType_t *dtype, int *dimNb, int *dimSize, int *dimStride) { PARAM_CHECK("[mluOpGetTensorDescriptorEx]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorEx]", layout != NULL); @@ -778,17 +780,17 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx( PARAM_CHECK("[mluOpGetTensorDescriptorEx]", dimStride != NULL); *layout = desc->layout; - *dtype = desc->getDtype(); - *dimNb = desc->getDim(); + *dtype = desc->dtype; + *dimNb = desc->dim; for (int i = 0; i < *dimNb; ++i) { - dimSize[i] = static_cast(desc->getDimIndex(i)); - dimStride[i] = static_cast(desc->getStrideIndex(i)); + dimSize[i] = static_cast(desc->dims[i]); + dimStride[i] = static_cast(desc->strides[i]); } return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorEx_v2( const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, mluOpDataType_t *dtype, int *dimNb, int64_t *dimSize, int64_t *dimStride) { PARAM_CHECK("[mluOpGetTensorDescriptorEx]", desc != NULL); @@ -799,43 +801,43 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx_v2( PARAM_CHECK("[mluOpGetTensorDescriptorEx]", dimStride != NULL); *layout = desc->layout; - *dtype = desc->getDtype(); - *dimNb = desc->getDim(); + *dtype = desc->dtype; + *dimNb = desc->dim; for (int i = 0; i < *dimNb; ++i) { - dimSize[i] = desc->getDimIndex(i); - dimStride[i] = desc->getStrideIndex(i); + dimSize[i] = desc->dims[i]; + dimStride[i] = desc->strides[i]; } return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptor( const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, mluOpDataType_t *dtype, int *dimNb, int *dimSize) { PARAM_CHECK("[mluOpGetTensorDescriptor]", desc != NULL); - SET_PARAM_FOR_POINTER(layout, desc->getLayout()); - SET_PARAM_FOR_POINTER(dtype, desc->getDtype()); - SET_PARAM_FOR_POINTER(dimNb, desc->getDim()); - SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->getDims(), desc->getDim()); + SET_PARAM_FOR_POINTER(layout, desc->layout); + SET_PARAM_FOR_POINTER(dtype, desc->dtype); + SET_PARAM_FOR_POINTER(dimNb, desc->dim); + SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->dims, desc->dim); return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor_v2( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptor_v2( const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, mluOpDataType_t *dtype, int *dimNb, int64_t *dimSize) { PARAM_CHECK("[mluOpGetTensorDescriptor]", desc != NULL); - SET_PARAM_FOR_POINTER(layout, desc->getLayout()); - SET_PARAM_FOR_POINTER(dtype, desc->getDtype()); - SET_PARAM_FOR_POINTER(dimNb, desc->getDim()); - SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->getDims(), desc->getDim()); + SET_PARAM_FOR_POINTER(layout, desc->layout); + SET_PARAM_FOR_POINTER(dtype, desc->dtype); + SET_PARAM_FOR_POINTER(dimNb, desc->dim); + SET_ARRAY_PARAM_FOR_POINTER(dimSize, desc->dims, desc->dim); return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorOnchipDataType( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorOnchipDataType( const mluOpTensorDescriptor_t desc, mluOpDataType_t *onchip_dtype) { PARAM_CHECK("[mluOpGetTensorDescriptorOnchipDataType]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorOnchipDataType]", onchip_dtype != NULL); @@ -845,26 +847,26 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorOnchipDataType( } mluOpStatus_t MLUOP_WIN_API -mluOpGetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int *position) { +mluOpTensorStruct::mluOpGetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int *position) { PARAM_CHECK("[mluOpGetTensorDescriptorPosition]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPosition]", position != NULL); - *position = desc->getPosition(); + *position = desc->position; return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionAndScale( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorPositionAndScale( mluOpTensorDescriptor_t desc, int *position, float *scale) { PARAM_CHECK("[mluOpGetTensorDescriptorPositionAndScale]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPositionAndScale]", position != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPositionAndScale]", scale != NULL); - *position = desc->getPosition(); - *scale = desc->getScale(); + *position = desc->position; + *scale = desc->scale; return MLUOP_STATUS_SUCCESS; } - -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionScaleAndOffset( + +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorPositionScaleAndOffset( mluOpTensorDescriptor_t desc, int *position, float *scale, int *offset) { PARAM_CHECK("[mluOpGetTensorDescriptorPositionScaleAndOffset]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPositionScaleAndOffset]", @@ -880,17 +882,17 @@ mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionScaleAndOffset( return MLUOP_STATUS_SUCCESS; } -mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPointerMode( +mluOpStatus_t MLUOP_WIN_API mluOpTensorStruct::mluOpGetTensorDescriptorPointerMode( mluOpTensorDescriptor_t desc, mluOpPointerMode_t *pointer_mode) { PARAM_CHECK("[mluOpGetTensorDescriptorPointerMode]", desc != NULL); PARAM_CHECK("[mluOpGetTensorDescriptorPointerMode]", pointer_mode != NULL); - SET_PARAM_FOR_POINTER(pointer_mode, desc->getPointer_mode()); + SET_PARAM_FOR_POINTER(pointer_mode, desc->pointer_mode); return MLUOP_STATUS_SUCCESS; } mluOpStatus_t MLUOP_WIN_API -mluOpDestroyTensorDescriptor(mluOpTensorDescriptor_t desc) { +mluOpTensorStruct::mluOpDestroyTensorDescriptor(mluOpTensorDescriptor_t desc) { PARAM_CHECK("[mluOpDestroyTensorDescriptor]", desc != NULL); #if MLUOP_TENSOR_QUEUE_ENABLE @@ -928,7 +930,7 @@ mluOpStatus_t MLUOP_WIN_API mluOpDestroyGroupTensorDescriptors( // usr interface. uint64_t MLUOP_WIN_API -mluOpGetTensorElementNum(const mluOpTensorDescriptor_t desc) { +mluOpTensorStruct::mluOpGetTensorElementNum(const mluOpTensorDescriptor_t desc) { CHECK(desc != NULL); return desc->total_element_num; } diff --git a/core/tensor.h b/core/tensor.h index 0f159fb19..7139a8f42 100644 --- a/core/tensor.h +++ b/core/tensor.h @@ -106,82 +106,198 @@ struct alignas(64) mluOpTensorStruct { inline bool isSameDims(const mluOpTensorStruct &other) const; inline bool isSameDims(const mluOpTensorStruct *other) const; inline bool isCpuScalar() const; - public: - mluOpTensorLayout_t getLayout() const{ return this->layout; } - void setLayout(mluOpDataType_t newLayout) { this->dtype = newLayout; } - inline uint64_t getTotalTensorSize() const { return this->total_tensor_size; } - inline void setTotalTensorSize(uint64_t newSize) { this->total_tensor_size = newSize; } - inline uint64_t getTotalElementNum() const { return this->total_element_num; } - inline void setTotalElementNum(uint64_t newNum) { this->total_element_num = newNum; } - inline int getPosition() const { return this->position; } - - inline mluOpDataType_t getDtype() const { return this->dtype; } - inline void setDtype(mluOpDataType_t newDtype) { this->dtype = newDtype; } - inline int getDim() const { return this->dim; } - inline void setDim(int newDim) { this->dim = newDim; } - - inline void releaseDims() { delete[] this->dims; } - inline int64_t *getDims() const { return this->dims; } - inline int64_t getDimIndex(size_t index) const { - if ( index >= this->dim ) { - throw std::out_of_range("Index out of range"); - } - return (this->dims)[index]; + + public: + inline float getOffset() const { return this->offset; } + inline void setOffset(float newOffset) { this->offset = newOffset; } + + inline float getScale() const { return this->scale; } + inline void setScale(float newScale) { this->scale = newScale; } + + inline mluOpTensorLayout_t getLayout() const { return this->layout; } + inline void setLayout(mluOpTensorLayout_t newLayout) { + this->layout = newLayout; + } + + inline uint64_t getTotalTensorSize() const { return this->total_tensor_size; } + inline void setTotalTensorSize(uint64_t newSize) { + this->total_tensor_size = newSize; + } + inline uint64_t getTotalElementNum() const { return this->total_element_num; } + inline void setTotalElementNum(uint64_t newNum) { + this->total_element_num = newNum; + } + + inline int getPosition() const { return this->position; } + inline void setPosition(int newPosition) { this->position = newPosition; } + + inline mluOpDataType_t getDtype() const { return this->dtype; } + inline void setDtype(mluOpDataType_t newDtype) { this->dtype = newDtype; } + inline mluOpDataType_t getOnchipDtype() const { return this->onchip_dtype; } + inline void setOnchipDtype(mluOpDataType_t newDtype) { + this->onchip_dtype = newDtype; + } + + inline int getDim() const { return this->dim; } + inline void setDim(int newDim) { this->dim = newDim; } + + inline void releaseDims() { delete[] this->dims; } + inline int64_t *getDims() const { return this->dims; } + inline int64_t getDimIndex(size_t index) { + if (index >= this->dim) { + throw std::out_of_range("Index out of range"); } - inline void setDims(int64_t *newDims) { - delete[] this->dims; - this->dims = newDims; - } - - inline void releaseStrides() { delete[] this->strides; } - inline int64_t *getStrides() const { return this->strides; } - inline int64_t getStrideIndex(size_t index) const { - if ( index >= this->dim ) { - throw std::out_of_range("Index out of range"); - } - return (this->strides)[index]; + return (this->dims)[index]; + } + inline void setDims(int64_t *newDims) { this->dims = newDims; } + + inline void releaseStrides() { delete[] this->strides; } + inline int64_t *getStrides() const { return this->strides; } + inline int64_t getStrideIndex(size_t index) const { + if (index >= this->dim) { + throw std::out_of_range("Index out of range"); } - inline void setStridesIndex( size_t index, int64_t newStride ) { - this->strides[index] = newStride; - } - - inline void setStrides(int64_t *newStrides) { - delete[] this->strides; - this->strides = newStrides; - } - - inline mluOpPointerMode_t getPointerMode() const { return this->pointer_mode; } - inline int64_t *getNormalDims() { return this->normal_dims; } - inline int64_t *getNormalStrides() { return this->normal_strides; } - private: - /* Try to pack and align the struct */ - /* ------------------- 64 Bytes - 1 -------------------*/ - int64_t normal_dims[MLUOP_DIM_MAX]; - - /* ------------------- 64 Bytes - 2 -------------------*/ - int64_t normal_strides[MLUOP_DIM_MAX]; - - /* ------------------- 64 Bytes - 3 -------------------*/ - /* Offset - 0 */ - uint64_t total_element_num = 0; - uint64_t total_tensor_size = 0; - int64_t *dims = normal_dims; // point the normal dims as default - int64_t *strides = normal_strides; // point the normal strides as default - /* Offset - 32 */ - int dim = 0; - mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT; - mluOpDataType_t onchip_dtype = MLUOP_DTYPE_INVALID; - mluOpTensorLayout_t layout = MLUOP_LAYOUT_ARRAY; - mluOpPointerMode_t pointer_mode = MLUOP_POINTER_MODE_DEVICE; - - /* Offset - 52 */ - /* To be removed*/ - int position = 0; - float scale = 1; - int offset = 0; - std::vector positions; - std::vector scales; - std::vector offsets; + return (this->strides)[index]; + } + inline void setStrideIndex(size_t index, int64_t newStride) { + this->strides[index] = newStride; + } + + inline void setStrides(int64_t *newStrides) { this->strides = newStrides; } + + inline mluOpPointerMode_t getPointerMode() const { + return this->pointer_mode; + } + inline void setPointerMode(mluOpPointerMode_t new_pointer_mode) { + this->pointer_mode = new_pointer_mode; + } + + inline int64_t *getNormalDims() { return this->normal_dims; } + inline int64_t *getNormalStrides() { return this->normal_strides; } + // Definition of function in tensor.cpp + static inline mluOpStatus_t mluOpSetTensorDescriptorZeroDim(mluOpTensorDescriptor_t desc); + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor( + mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, + mluOpDataType_t dtype, int dimNb, const int *dimSize); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptor_v2( + mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, + mluOpDataType_t dtype, int dimNb, const int64_t *dimSize); + + static inline void mluOpSetTensorDescriptorDimBase( + mluOpTensorDescriptor_t desc, int dimNb); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim( + mluOpTensorDescriptor_t desc, int dimNb, const int *dimSize); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorDim_v2( + mluOpTensorDescriptor_t desc, int dimNb, const int64_t *dimSize); + + mluOpStatus_t MLUOP_WIN_API + mluOpResetTensorDescriptor( mluOpTensorDescriptor_t desc); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx( + mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, + mluOpDataType_t dtype, int dimNb, const int *dimSize, + const int *dimStride); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorEx_v2( + mluOpTensorDescriptor_t desc, mluOpTensorLayout_t layout, + mluOpDataType_t dtype, int dimNb, const int64_t *dimSize, + const int64_t *dimStride); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorOnchipDataType( + mluOpTensorDescriptor_t desc, mluOpDataType_t onchip_dtype); + + mluOpStatus_t MLUOP_WIN_API + mluOpSetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int position); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionAndScale( + mluOpTensorDescriptor_t desc, int position, float scale); + + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPositionScaleAndOffset( + mluOpTensorDescriptor_t desc, int position, float scale, int offset); + + mluOpStatus_t MLUOP_WIN_API mluOpSetTensorDescriptorPointerMode( + mluOpTensorDescriptor_t desc, mluOpPointerMode_t pointer_mode); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx( + const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, + mluOpDataType_t *dtype, int *dimNb, int *dimSize, int *dimStride); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorEx_v2( + const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, + mluOpDataType_t *dtype, int *dimNb, int64_t *dimSize, int64_t *dimStride); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor( + const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, + mluOpDataType_t *dtype, int *dimNb, int *dimSize); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptor_v2( + const mluOpTensorDescriptor_t desc, mluOpTensorLayout_t *layout, + mluOpDataType_t *dtype, int *dimNb, int64_t *dimSize); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorOnchipDataType( + const mluOpTensorDescriptor_t desc, mluOpDataType_t *onchip_dtype); + + mluOpStatus_t MLUOP_WIN_API + mluOpGetTensorDescriptorPosition(mluOpTensorDescriptor_t desc, int *position); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionAndScale( + mluOpTensorDescriptor_t desc, int *position, float *scale); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPositionScaleAndOffset( + mluOpTensorDescriptor_t desc, int *position, float *scale, int *offset); + + mluOpStatus_t MLUOP_WIN_API mluOpGetTensorDescriptorPointerMode( + mluOpTensorDescriptor_t desc, mluOpPointerMode_t *pointer_mode); + + mluOpStatus_t MLUOP_WIN_API + mluOpDestroyTensorDescriptor(mluOpTensorDescriptor_t desc); + + uint64_t MLUOP_WIN_API + mluOpGetTensorElementNum(mluOpTensorDescriptor_t desc); + + mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors( + mluOpTensorDescriptor_t **group_desc, + const mluOpTensorLayout_t *group_layout, const mluOpDataType_t *group_dtype, + const int *group_dimNb, const int *group_dimSize, const int desc_num); + + mluOpStatus_t MLUOP_WIN_API mluOpSetGroupTensorDescriptors_v2( + mluOpTensorDescriptor_t **group_desc, + const mluOpTensorLayout_t *group_layout, const mluOpDataType_t *group_dtype, + const int *group_dimNb, const int64_t *group_dimSize, const int desc_num); + + // private: + /* Try to pack and align the struct */ + /* ------------------- 64 Bytes - 1 -------------------*/ + int64_t normal_dims[MLUOP_DIM_MAX]; + + /* ------------------- 64 Bytes - 2 -------------------*/ + int64_t normal_strides[MLUOP_DIM_MAX]; + + /* ------------------- 64 Bytes - 3 -------------------*/ + /* Offset - 0 */ + uint64_t total_element_num = 0; + uint64_t total_tensor_size = 0; + int64_t *dims = normal_dims; // point the normal dims as default + int64_t *strides = normal_strides; // point the normal strides as default + /* Offset - 32 */ + int dim = 0; + mluOpDataType_t dtype = MLUOP_DTYPE_FLOAT; + mluOpDataType_t onchip_dtype = MLUOP_DTYPE_INVALID; + mluOpTensorLayout_t layout = MLUOP_LAYOUT_ARRAY; + mluOpPointerMode_t pointer_mode = MLUOP_POINTER_MODE_DEVICE; + + /* Offset - 52 */ + /* To be removed*/ + int position = 0; + float scale = 1; + int offset = 0; + std::vector positions; + std::vector scales; + std::vector offsets; }; // dim_set(rnn) [layer_num, direction, cap_of_cell] diff --git a/test/mlu_op_gtest/pb_gtest/src/zoo/active_rotated_filter_forward/active_rotated_filter_forward.cpp b/test/mlu_op_gtest/pb_gtest/src/zoo/active_rotated_filter_forward/active_rotated_filter_forward.cpp index f4ac68a06..c5891bd4b 100644 --- a/test/mlu_op_gtest/pb_gtest/src/zoo/active_rotated_filter_forward/active_rotated_filter_forward.cpp +++ b/test/mlu_op_gtest/pb_gtest/src/zoo/active_rotated_filter_forward/active_rotated_filter_forward.cpp @@ -112,7 +112,7 @@ void ActiveRotatedFilterForwardExecutor::cpuCompute() { } int64_t ActiveRotatedFilterForwardExecutor::getTheoryIoSize() { - auto dtype = tensor_desc_[0].tensor->dtype; + auto dtype = tensor_desc_[0].tensor->getDtype(); std::vector indices_shape = parser_->input(1)->shape; const int64_t rotations = indices_shape[3]; diff --git a/test/mlu_op_gtest/pb_gtest/src/zoo/fft/fft.cpp b/test/mlu_op_gtest/pb_gtest/src/zoo/fft/fft.cpp index d623ce2c5..3036fba30 100644 --- a/test/mlu_op_gtest/pb_gtest/src/zoo/fft/fft.cpp +++ b/test/mlu_op_gtest/pb_gtest/src/zoo/fft/fft.cpp @@ -123,8 +123,8 @@ int64_t FftExecutor::getTheoryIoSize() { // dtype check auto input_tensor = tensor_desc_[0].tensor; auto output_tensor = tensor_desc_[1].tensor; - mluOpDataType_t input_dtype = input_tensor->dtype; - mluOpDataType_t output_dtype = output_tensor->dtype; + mluOpDataType_t input_dtype = input_tensor->getDtype(); + mluOpDataType_t output_dtype = output_tensor->getDtype(); auto fft_param = parser_->getProtoNode()->fft_param(); int rank = fft_param.rank(); diff --git a/test/mlu_op_gtest/pb_gtest/src/zoo/masked_col2im_forward/masked_col2im_forward.cpp b/test/mlu_op_gtest/pb_gtest/src/zoo/masked_col2im_forward/masked_col2im_forward.cpp index 582803a15..e1cdb18e1 100644 --- a/test/mlu_op_gtest/pb_gtest/src/zoo/masked_col2im_forward/masked_col2im_forward.cpp +++ b/test/mlu_op_gtest/pb_gtest/src/zoo/masked_col2im_forward/masked_col2im_forward.cpp @@ -114,7 +114,7 @@ void MaskedCol2imForwardExecutor::cpuCompute() { int64_t MaskedCol2imForwardExecutor::getTheoryIoSize() { int input_size = parser_->getInputDataCount(0); - auto dtype = tensor_desc_[0].tensor->dtype; + auto dtype = tensor_desc_[0].tensor->getDtype(); int dsize = 0; if (dtype == MLUOP_DTYPE_FLOAT) { dsize = 4;