1be168c0dSopenharmony_ciFrom aa38d5a95960e60f6a90a1ffa2958a6ebcee2b4e Mon Sep 17 00:00:00 2001 2be168c0dSopenharmony_ciFrom: chengfeng27 <chengfeng27@huawei.com> 3be168c0dSopenharmony_ciDate: Thu, 18 Apr 2024 09:39:33 +0800 4be168c0dSopenharmony_ciSubject: [PATCH] nnrt litegraph dequant 5be168c0dSopenharmony_ci 6be168c0dSopenharmony_ci--- 7be168c0dSopenharmony_ci mindspore/lite/mindir/include/mindir_tensor.h | 6 +- 8be168c0dSopenharmony_ci mindspore/lite/mindir/include/mindir_types.h | 28 ++- 9be168c0dSopenharmony_ci mindspore/lite/mindir/inner_headers/utils.h | 2 +- 10be168c0dSopenharmony_ci mindspore/lite/mindir/src/mindir.cc | 93 ++++++++++ 11be168c0dSopenharmony_ci mindspore/lite/mindir/src/mindir_tensor.cc | 14 +- 12be168c0dSopenharmony_ci mindspore/lite/mindir/src/utils.cc | 27 +-- 13be168c0dSopenharmony_ci .../src/litert/delegate/nnrt/nnrt_delegate.cc | 166 ++++++++++++------ 14be168c0dSopenharmony_ci .../src/litert/delegate/nnrt/nnrt_delegate.h | 12 +- 15be168c0dSopenharmony_ci mindspore/lite/src/litert/scheduler.cc | 1 + 16be168c0dSopenharmony_ci 9 files changed, 270 insertions(+), 79 deletions(-) 17be168c0dSopenharmony_ci 18be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/include/mindir_tensor.h b/mindspore/lite/mindir/include/mindir_tensor.h 19be168c0dSopenharmony_ciindex c1ac89bf..43c1478c 100644 20be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/include/mindir_tensor.h 21be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/include/mindir_tensor.h 22be168c0dSopenharmony_ci@@ -8,9 +8,9 @@ namespace lite { 23be168c0dSopenharmony_ci 24be168c0dSopenharmony_ci // ********** Tensor ********** 25be168c0dSopenharmony_ci TensorPtr MindIR_Tensor_Create(); 26be168c0dSopenharmony_ci-TensorPtr MindIR_Tensor_Create(const std::string &name, DataType data_type, const std::vector<int32_t> &dims, 27be168c0dSopenharmony_ci- Format format, const std::vector<uint8_t> &data, 28be168c0dSopenharmony_ci- const std::vector<QuantParam> &quant_params); 29be168c0dSopenharmony_ci+TensorPtr MindIR_Tensor_Create(const char *name, DataType data_type, const int32_t *dims, uint32_t dims_size, 30be168c0dSopenharmony_ci+ Format format, const uint8_t *data, uint32_t data_size, 31be168c0dSopenharmony_ci+ const QuantParam *quant_params, uint32_t quant_params_size); 32be168c0dSopenharmony_ci std::string MindIR_Tensor_GetName(ConstTensorPtr tensor); 33be168c0dSopenharmony_ci void MindIR_Tensor_SetName(TensorPtr *tensor, const std::string &name); 34be168c0dSopenharmony_ci DataType MindIR_Tensor_GetDataType(ConstTensorPtr tensor); 35be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/include/mindir_types.h b/mindspore/lite/mindir/include/mindir_types.h 36be168c0dSopenharmony_ciindex 5744441a..196995fa 100644 37be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/include/mindir_types.h 38be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/include/mindir_types.h 39be168c0dSopenharmony_ci@@ -44,11 +44,35 @@ enum DataType : int8_t { 40be168c0dSopenharmony_ci enum Format : int8_t { 41be168c0dSopenharmony_ci FORMAT_NCHW = 0, 42be168c0dSopenharmony_ci FORMAT_NHWC = 1, 43be168c0dSopenharmony_ci+ FORMAT_NHWC4 = 2, 44be168c0dSopenharmony_ci+ FORMAT_HWKC = 3, 45be168c0dSopenharmony_ci+ FORMAT_HWCK = 4, 46be168c0dSopenharmony_ci+ FORMAT_KCHW = 5, 47be168c0dSopenharmony_ci+ FORMAT_CKHW = 6, 48be168c0dSopenharmony_ci+ FORMAT_KHWC = 7, 49be168c0dSopenharmony_ci+ FORMAT_CHWK = 8, 50be168c0dSopenharmony_ci+ FORMAT_HW = 9, 51be168c0dSopenharmony_ci+ FORMAT_HW4 = 10, 52be168c0dSopenharmony_ci+ FORMAT_NC = 11, 53be168c0dSopenharmony_ci+ FORMAT_NC4 = 12, 54be168c0dSopenharmony_ci+ FORMAT_NC4HW4 = 13, 55be168c0dSopenharmony_ci+ FORMAT_NUM_OF_FORMAT = 14, 56be168c0dSopenharmony_ci+ FORMAT_NCDHW = 15, 57be168c0dSopenharmony_ci+ FORMAT_NWC = 16, 58be168c0dSopenharmony_ci+ FORMAT_NCW = 17, 59be168c0dSopenharmony_ci+ FORMAT_NC8HW8 = 18, 60be168c0dSopenharmony_ci+ FORMAT_MIN = FORMAT_NCHW, 61be168c0dSopenharmony_ci+ FORMAT_MAX = FORMAT_NC8HW8 62be168c0dSopenharmony_ci }; 63be168c0dSopenharmony_ci 64be168c0dSopenharmony_ci enum QuantType : int8_t { 65be168c0dSopenharmony_ci- QUANT_TYPE_NONE, 66be168c0dSopenharmony_ci- QUANT_TYPE_ALL, 67be168c0dSopenharmony_ci+ QUANT_TYPE_NONE = 0, 68be168c0dSopenharmony_ci+ QUANT_TYPE_AWARETRAINING = 1, 69be168c0dSopenharmony_ci+ QUANT_TYPE_WEIGHTQUANT = 2, 70be168c0dSopenharmony_ci+ QUANT_TYPE_POSTTRAINING = 3, 71be168c0dSopenharmony_ci+ QUANT_TYPE_WEIGHT = 4, 72be168c0dSopenharmony_ci+ QUANT_TYPE_ALL = 5, 73be168c0dSopenharmony_ci+ QUANT_TYPE_DYNAMIC = 6 74be168c0dSopenharmony_ci }; 75be168c0dSopenharmony_ci 76be168c0dSopenharmony_ci enum NodeType : uint32_t { 77be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/inner_headers/utils.h b/mindspore/lite/mindir/inner_headers/utils.h 78be168c0dSopenharmony_ciindex 0e6eb35d..0d150f80 100644 79be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/inner_headers/utils.h 80be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/inner_headers/utils.h 81be168c0dSopenharmony_ci@@ -17,7 +17,7 @@ flatbuffers::Offset<schema::Vec2D> CreateVec2D(flatbuffers::FlatBufferBuilder &f 82be168c0dSopenharmony_ci mindspore::schema::PrimitiveType MindIR_GetPrimitiveType(PrimitivePtr prim); 83be168c0dSopenharmony_ci 84be168c0dSopenharmony_ci flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>>> ConvertQuantParams( 85be168c0dSopenharmony_ci- flatbuffers::FlatBufferBuilder &fbb, const std::vector<QuantParam> &quant_params); 86be168c0dSopenharmony_ci+ flatbuffers::FlatBufferBuilder &fbb, const QuantParam *quant_params, uint32_t quant_params_size); 87be168c0dSopenharmony_ci 88be168c0dSopenharmony_ci flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>>> ConvertQuantParams( 89be168c0dSopenharmony_ci flatbuffers::FlatBufferBuilder &fbb, 90be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/src/mindir.cc b/mindspore/lite/mindir/src/mindir.cc 91be168c0dSopenharmony_ciindex 7041498a..a1f86671 100644 92be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/src/mindir.cc 93be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/src/mindir.cc 94be168c0dSopenharmony_ci@@ -398,6 +398,9 @@ std::vector<int64_t> MindIR_AvgPoolFusion_GetKernelSize(ConstPrimitivePtr primit 95be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 96be168c0dSopenharmony_ci std::vector<int64_t> result; 97be168c0dSopenharmony_ci auto src = value->kernel_size(); 98be168c0dSopenharmony_ci+ if (src == nullptr) { 99be168c0dSopenharmony_ci+ return {}; 100be168c0dSopenharmony_ci+ } 101be168c0dSopenharmony_ci result.resize(src->size()); 102be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 103be168c0dSopenharmony_ci return result; 104be168c0dSopenharmony_ci@@ -437,6 +440,9 @@ std::vector<int64_t> MindIR_AvgPoolFusion_GetStrides(ConstPrimitivePtr primitive 105be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 106be168c0dSopenharmony_ci std::vector<int64_t> result; 107be168c0dSopenharmony_ci auto src = value->strides(); 108be168c0dSopenharmony_ci+ if (src == nullptr) { 109be168c0dSopenharmony_ci+ return {}; 110be168c0dSopenharmony_ci+ } 111be168c0dSopenharmony_ci result.resize(src->size()); 112be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 113be168c0dSopenharmony_ci return result; 114be168c0dSopenharmony_ci@@ -476,6 +482,9 @@ std::vector<int64_t> MindIR_AvgPoolFusion_GetPad(ConstPrimitivePtr primitive) { 115be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 116be168c0dSopenharmony_ci std::vector<int64_t> result; 117be168c0dSopenharmony_ci auto src = value->pad(); 118be168c0dSopenharmony_ci+ if (src == nullptr) { 119be168c0dSopenharmony_ci+ return {}; 120be168c0dSopenharmony_ci+ } 121be168c0dSopenharmony_ci result.resize(src->size()); 122be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 123be168c0dSopenharmony_ci return result; 124be168c0dSopenharmony_ci@@ -712,6 +721,9 @@ std::vector<int64_t> MindIR_BatchToSpaceND_GetBlockShape(ConstPrimitivePtr primi 125be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 126be168c0dSopenharmony_ci std::vector<int64_t> result; 127be168c0dSopenharmony_ci auto src = value->block_shape(); 128be168c0dSopenharmony_ci+ if (src == nullptr) { 129be168c0dSopenharmony_ci+ return {}; 130be168c0dSopenharmony_ci+ } 131be168c0dSopenharmony_ci result.resize(src->size()); 132be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 133be168c0dSopenharmony_ci return result; 134be168c0dSopenharmony_ci@@ -747,6 +759,9 @@ std::vector<std::vector<int64_t>> MindIR_BatchToSpaceND_GetCrops(ConstPrimitiveP 135be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 136be168c0dSopenharmony_ci std::vector<std::vector<int64_t>> out; 137be168c0dSopenharmony_ci auto src = value->crops(); 138be168c0dSopenharmony_ci+ if (src == nullptr) { 139be168c0dSopenharmony_ci+ return {}; 140be168c0dSopenharmony_ci+ } 141be168c0dSopenharmony_ci for (auto sub_list : *src->data()) { 142be168c0dSopenharmony_ci std::vector<int64_t> result_tmp; 143be168c0dSopenharmony_ci result_tmp.resize(sub_list->data()->size()); 144be168c0dSopenharmony_ci@@ -871,6 +886,9 @@ std::vector<int64_t> MindIR_Conv2DFusion_GetKernelSize(ConstPrimitivePtr primiti 145be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 146be168c0dSopenharmony_ci std::vector<int64_t> result; 147be168c0dSopenharmony_ci auto src = value->kernel_size(); 148be168c0dSopenharmony_ci+ if (src == nullptr) { 149be168c0dSopenharmony_ci+ return {}; 150be168c0dSopenharmony_ci+ } 151be168c0dSopenharmony_ci result.resize(src->size()); 152be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 153be168c0dSopenharmony_ci return result; 154be168c0dSopenharmony_ci@@ -911,6 +929,9 @@ std::vector<int64_t> MindIR_Conv2DFusion_GetStride(ConstPrimitivePtr primitive) 155be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 156be168c0dSopenharmony_ci std::vector<int64_t> result; 157be168c0dSopenharmony_ci auto src = value->stride(); 158be168c0dSopenharmony_ci+ if (src == nullptr) { 159be168c0dSopenharmony_ci+ return {}; 160be168c0dSopenharmony_ci+ } 161be168c0dSopenharmony_ci result.resize(src->size()); 162be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 163be168c0dSopenharmony_ci return result; 164be168c0dSopenharmony_ci@@ -952,6 +973,9 @@ std::vector<int64_t> MindIR_Conv2DFusion_GetDilation(ConstPrimitivePtr primitive 165be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 166be168c0dSopenharmony_ci std::vector<int64_t> result; 167be168c0dSopenharmony_ci auto src = value->dilation(); 168be168c0dSopenharmony_ci+ if (src == nullptr) { 169be168c0dSopenharmony_ci+ return {}; 170be168c0dSopenharmony_ci+ } 171be168c0dSopenharmony_ci result.resize(src->size()); 172be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 173be168c0dSopenharmony_ci return result; 174be168c0dSopenharmony_ci@@ -1030,6 +1054,9 @@ std::vector<int64_t> MindIR_Conv2DFusion_GetPadList(ConstPrimitivePtr primitive) 175be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 176be168c0dSopenharmony_ci std::vector<int64_t> result; 177be168c0dSopenharmony_ci auto src = value->pad_list(); 178be168c0dSopenharmony_ci+ if (src == nullptr) { 179be168c0dSopenharmony_ci+ return {}; 180be168c0dSopenharmony_ci+ } 181be168c0dSopenharmony_ci result.resize(src->size()); 182be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 183be168c0dSopenharmony_ci return result; 184be168c0dSopenharmony_ci@@ -1281,6 +1308,9 @@ std::vector<int64_t> MindIR_Conv2dTransposeFusion_GetKernelSize(ConstPrimitivePt 185be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 186be168c0dSopenharmony_ci std::vector<int64_t> result; 187be168c0dSopenharmony_ci auto src = value->kernel_size(); 188be168c0dSopenharmony_ci+ if (src == nullptr) { 189be168c0dSopenharmony_ci+ return {}; 190be168c0dSopenharmony_ci+ } 191be168c0dSopenharmony_ci result.resize(src->size()); 192be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 193be168c0dSopenharmony_ci return result; 194be168c0dSopenharmony_ci@@ -1322,6 +1352,9 @@ std::vector<int64_t> MindIR_Conv2dTransposeFusion_GetStride(ConstPrimitivePtr pr 195be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 196be168c0dSopenharmony_ci std::vector<int64_t> result; 197be168c0dSopenharmony_ci auto src = value->stride(); 198be168c0dSopenharmony_ci+ if (src == nullptr) { 199be168c0dSopenharmony_ci+ return {}; 200be168c0dSopenharmony_ci+ } 201be168c0dSopenharmony_ci result.resize(src->size()); 202be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 203be168c0dSopenharmony_ci return result; 204be168c0dSopenharmony_ci@@ -1364,6 +1397,9 @@ std::vector<int64_t> MindIR_Conv2dTransposeFusion_GetDilation(ConstPrimitivePtr 205be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 206be168c0dSopenharmony_ci std::vector<int64_t> result; 207be168c0dSopenharmony_ci auto src = value->dilation(); 208be168c0dSopenharmony_ci+ if (src == nullptr) { 209be168c0dSopenharmony_ci+ return {}; 210be168c0dSopenharmony_ci+ } 211be168c0dSopenharmony_ci result.resize(src->size()); 212be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 213be168c0dSopenharmony_ci return result; 214be168c0dSopenharmony_ci@@ -1444,6 +1480,9 @@ std::vector<int64_t> MindIR_Conv2dTransposeFusion_GetPadList(ConstPrimitivePtr p 215be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 216be168c0dSopenharmony_ci std::vector<int64_t> result; 217be168c0dSopenharmony_ci auto src = value->pad_list(); 218be168c0dSopenharmony_ci+ if (src == nullptr) { 219be168c0dSopenharmony_ci+ return {}; 220be168c0dSopenharmony_ci+ } 221be168c0dSopenharmony_ci result.resize(src->size()); 222be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 223be168c0dSopenharmony_ci return result; 224be168c0dSopenharmony_ci@@ -1640,6 +1679,9 @@ std::vector<int64_t> MindIR_Conv2dTransposeFusion_GetOutputPaddings(ConstPrimiti 225be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 226be168c0dSopenharmony_ci std::vector<int64_t> result; 227be168c0dSopenharmony_ci auto src = value->output_paddings(); 228be168c0dSopenharmony_ci+ if (src == nullptr) { 229be168c0dSopenharmony_ci+ return {}; 230be168c0dSopenharmony_ci+ } 231be168c0dSopenharmony_ci result.resize(src->size()); 232be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 233be168c0dSopenharmony_ci return result; 234be168c0dSopenharmony_ci@@ -2273,6 +2315,9 @@ std::vector<int64_t> MindIR_MaxPoolFusion_GetKernelSize(ConstPrimitivePtr primit 235be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 236be168c0dSopenharmony_ci std::vector<int64_t> result; 237be168c0dSopenharmony_ci auto src = value->kernel_size(); 238be168c0dSopenharmony_ci+ if (src == nullptr) { 239be168c0dSopenharmony_ci+ return {}; 240be168c0dSopenharmony_ci+ } 241be168c0dSopenharmony_ci result.resize(src->size()); 242be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 243be168c0dSopenharmony_ci return result; 244be168c0dSopenharmony_ci@@ -2312,6 +2357,9 @@ std::vector<int64_t> MindIR_MaxPoolFusion_GetStrides(ConstPrimitivePtr primitive 245be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 246be168c0dSopenharmony_ci std::vector<int64_t> result; 247be168c0dSopenharmony_ci auto src = value->strides(); 248be168c0dSopenharmony_ci+ if (src == nullptr) { 249be168c0dSopenharmony_ci+ return {}; 250be168c0dSopenharmony_ci+ } 251be168c0dSopenharmony_ci result.resize(src->size()); 252be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 253be168c0dSopenharmony_ci return result; 254be168c0dSopenharmony_ci@@ -2351,6 +2399,9 @@ std::vector<int64_t> MindIR_MaxPoolFusion_GetPad(ConstPrimitivePtr primitive) { 255be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 256be168c0dSopenharmony_ci std::vector<int64_t> result; 257be168c0dSopenharmony_ci auto src = value->pad(); 258be168c0dSopenharmony_ci+ if (src == nullptr) { 259be168c0dSopenharmony_ci+ return {}; 260be168c0dSopenharmony_ci+ } 261be168c0dSopenharmony_ci result.resize(src->size()); 262be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 263be168c0dSopenharmony_ci return result; 264be168c0dSopenharmony_ci@@ -2680,6 +2731,9 @@ std::vector<std::vector<int64_t>> MindIR_PadFusion_GetPaddings(ConstPrimitivePtr 265be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 266be168c0dSopenharmony_ci std::vector<std::vector<int64_t>> out; 267be168c0dSopenharmony_ci auto src = value->paddings(); 268be168c0dSopenharmony_ci+ if (src == nullptr) { 269be168c0dSopenharmony_ci+ return {}; 270be168c0dSopenharmony_ci+ } 271be168c0dSopenharmony_ci for (auto sub_list : *src->data()) { 272be168c0dSopenharmony_ci std::vector<int64_t> result_tmp; 273be168c0dSopenharmony_ci result_tmp.resize(sub_list->data()->size()); 274be168c0dSopenharmony_ci@@ -3601,6 +3655,9 @@ std::vector<int64_t> MindIR_SliceFusion_GetAxes(ConstPrimitivePtr primitive) { 275be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 276be168c0dSopenharmony_ci std::vector<int64_t> result; 277be168c0dSopenharmony_ci auto src = value->axes(); 278be168c0dSopenharmony_ci+ if (src == nullptr) { 279be168c0dSopenharmony_ci+ return {}; 280be168c0dSopenharmony_ci+ } 281be168c0dSopenharmony_ci result.resize(src->size()); 282be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 283be168c0dSopenharmony_ci return result; 284be168c0dSopenharmony_ci@@ -3646,6 +3703,9 @@ std::vector<int64_t> MindIR_Softmax_GetAxis(ConstPrimitivePtr primitive) { 285be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 286be168c0dSopenharmony_ci std::vector<int64_t> result; 287be168c0dSopenharmony_ci auto src = value->axis(); 288be168c0dSopenharmony_ci+ if (src == nullptr) { 289be168c0dSopenharmony_ci+ return {}; 290be168c0dSopenharmony_ci+ } 291be168c0dSopenharmony_ci result.resize(src->size()); 292be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 293be168c0dSopenharmony_ci return result; 294be168c0dSopenharmony_ci@@ -3694,6 +3754,9 @@ std::vector<int64_t> MindIR_SpaceToBatchND_GetBlockShape(ConstPrimitivePtr primi 295be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 296be168c0dSopenharmony_ci std::vector<int64_t> result; 297be168c0dSopenharmony_ci auto src = value->block_shape(); 298be168c0dSopenharmony_ci+ if (src == nullptr) { 299be168c0dSopenharmony_ci+ return {}; 300be168c0dSopenharmony_ci+ } 301be168c0dSopenharmony_ci result.resize(src->size()); 302be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 303be168c0dSopenharmony_ci return result; 304be168c0dSopenharmony_ci@@ -3729,6 +3792,9 @@ std::vector<std::vector<int64_t>> MindIR_SpaceToBatchND_GetPaddings(ConstPrimiti 305be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 306be168c0dSopenharmony_ci std::vector<std::vector<int64_t>> out; 307be168c0dSopenharmony_ci auto src = value->paddings(); 308be168c0dSopenharmony_ci+ if (src == nullptr) { 309be168c0dSopenharmony_ci+ return {}; 310be168c0dSopenharmony_ci+ } 311be168c0dSopenharmony_ci for (auto sub_list : *src->data()) { 312be168c0dSopenharmony_ci std::vector<int64_t> result_tmp; 313be168c0dSopenharmony_ci result_tmp.resize(sub_list->data()->size()); 314be168c0dSopenharmony_ci@@ -3812,6 +3878,9 @@ std::vector<int64_t> MindIR_Split_GetSizeSplits(ConstPrimitivePtr primitive) { 315be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 316be168c0dSopenharmony_ci std::vector<int64_t> result; 317be168c0dSopenharmony_ci auto src = value->size_splits(); 318be168c0dSopenharmony_ci+ if (src == nullptr) { 319be168c0dSopenharmony_ci+ return {}; 320be168c0dSopenharmony_ci+ } 321be168c0dSopenharmony_ci result.resize(src->size()); 322be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 323be168c0dSopenharmony_ci return result; 324be168c0dSopenharmony_ci@@ -3912,6 +3981,9 @@ std::vector<int64_t> MindIR_Squeeze_GetAxis(ConstPrimitivePtr primitive) { 325be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 326be168c0dSopenharmony_ci std::vector<int64_t> result; 327be168c0dSopenharmony_ci auto src = value->axis(); 328be168c0dSopenharmony_ci+ if (src == nullptr) { 329be168c0dSopenharmony_ci+ return {}; 330be168c0dSopenharmony_ci+ } 331be168c0dSopenharmony_ci result.resize(src->size()); 332be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 333be168c0dSopenharmony_ci return result; 334be168c0dSopenharmony_ci@@ -4212,6 +4284,9 @@ std::vector<int64_t> MindIR_TileFusion_GetDims(ConstPrimitivePtr primitive) { 335be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 336be168c0dSopenharmony_ci std::vector<int64_t> result; 337be168c0dSopenharmony_ci auto src = value->dims(); 338be168c0dSopenharmony_ci+ if (src == nullptr) { 339be168c0dSopenharmony_ci+ return {}; 340be168c0dSopenharmony_ci+ } 341be168c0dSopenharmony_ci result.resize(src->size()); 342be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 343be168c0dSopenharmony_ci return result; 344be168c0dSopenharmony_ci@@ -4342,6 +4417,9 @@ std::vector<int64_t> MindIR_Unsqueeze_GetAxis(ConstPrimitivePtr primitive) { 345be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 346be168c0dSopenharmony_ci std::vector<int64_t> result; 347be168c0dSopenharmony_ci auto src = value->axis(); 348be168c0dSopenharmony_ci+ if (src == nullptr) { 349be168c0dSopenharmony_ci+ return {}; 350be168c0dSopenharmony_ci+ } 351be168c0dSopenharmony_ci result.resize(src->size()); 352be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 353be168c0dSopenharmony_ci return result; 354be168c0dSopenharmony_ci@@ -4399,6 +4477,9 @@ std::vector<int64_t> MindIR_BroadcastTo_GetShape(ConstPrimitivePtr primitive) { 355be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 356be168c0dSopenharmony_ci std::vector<int64_t> result; 357be168c0dSopenharmony_ci auto src = value->shape(); 358be168c0dSopenharmony_ci+ if (src == nullptr) { 359be168c0dSopenharmony_ci+ return {}; 360be168c0dSopenharmony_ci+ } 361be168c0dSopenharmony_ci result.resize(src->size()); 362be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 363be168c0dSopenharmony_ci return result; 364be168c0dSopenharmony_ci@@ -4477,6 +4558,9 @@ std::vector<float> MindIR_ConstantOfShape_GetValue(ConstPrimitivePtr primitive) 365be168c0dSopenharmony_ci if (prim != nullptr && value_ != nullptr) { 366be168c0dSopenharmony_ci std::vector<float> result; 367be168c0dSopenharmony_ci auto src = value_->value(); 368be168c0dSopenharmony_ci+ if (src == nullptr) { 369be168c0dSopenharmony_ci+ return {}; 370be168c0dSopenharmony_ci+ } 371be168c0dSopenharmony_ci result.resize(src->size()); 372be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](float item) { return item; }); 373be168c0dSopenharmony_ci return result; 374be168c0dSopenharmony_ci@@ -5889,6 +5973,9 @@ std::vector<int64_t> MindIR_L2NormalizeFusion_GetAxis(ConstPrimitivePtr primitiv 375be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 376be168c0dSopenharmony_ci std::vector<int64_t> result; 377be168c0dSopenharmony_ci auto src = value->axis(); 378be168c0dSopenharmony_ci+ if (src == nullptr) { 379be168c0dSopenharmony_ci+ return {}; 380be168c0dSopenharmony_ci+ } 381be168c0dSopenharmony_ci result.resize(src->size()); 382be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 383be168c0dSopenharmony_ci return result; 384be168c0dSopenharmony_ci@@ -6238,6 +6325,9 @@ std::vector<int64_t> MindIR_Crop_GetOffsets(ConstPrimitivePtr primitive) { 385be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 386be168c0dSopenharmony_ci std::vector<int64_t> result; 387be168c0dSopenharmony_ci auto src = value->offsets(); 388be168c0dSopenharmony_ci+ if (src == nullptr) { 389be168c0dSopenharmony_ci+ return {}; 390be168c0dSopenharmony_ci+ } 391be168c0dSopenharmony_ci result.resize(src->size()); 392be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](int64_t item) { return item; }); 393be168c0dSopenharmony_ci return result; 394be168c0dSopenharmony_ci@@ -6348,6 +6438,9 @@ std::vector<float> MindIR_DetectionPostProcess_GetScale(ConstPrimitivePtr primit 395be168c0dSopenharmony_ci if (prim != nullptr && value != nullptr) { 396be168c0dSopenharmony_ci std::vector<float> result; 397be168c0dSopenharmony_ci auto src = value->scale(); 398be168c0dSopenharmony_ci+ if (src == nullptr) { 399be168c0dSopenharmony_ci+ return {}; 400be168c0dSopenharmony_ci+ } 401be168c0dSopenharmony_ci result.resize(src->size()); 402be168c0dSopenharmony_ci std::transform(src->begin(), src->end(), result.begin(), [](float item) { return item; }); 403be168c0dSopenharmony_ci return result; 404be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/src/mindir_tensor.cc b/mindspore/lite/mindir/src/mindir_tensor.cc 405be168c0dSopenharmony_ciindex 9575f8c2..8888e2c9 100644 406be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/src/mindir_tensor.cc 407be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/src/mindir_tensor.cc 408be168c0dSopenharmony_ci@@ -36,15 +36,15 @@ TensorPtr MindIR_Tensor_Create() { 409be168c0dSopenharmony_ci return ret_value; 410be168c0dSopenharmony_ci } 411be168c0dSopenharmony_ci 412be168c0dSopenharmony_ci-TensorPtr MindIR_Tensor_Create(const std::string &name, DataType data_type, const std::vector<int32_t> &dims, 413be168c0dSopenharmony_ci- Format format, const std::vector<uint8_t> &data, 414be168c0dSopenharmony_ci- const std::vector<QuantParam> &quant_params) { 415be168c0dSopenharmony_ci+TensorPtr MindIR_Tensor_Create(const char *name, DataType data_type, const int32_t *dims, uint32_t dims_size, 416be168c0dSopenharmony_ci+ Format format, const uint8_t *data, uint32_t data_size, 417be168c0dSopenharmony_ci+ const QuantParam *quant_params, uint32_t quant_params_size) { 418be168c0dSopenharmony_ci flatbuffers::FlatBufferBuilder fbb; 419be168c0dSopenharmony_ci 420be168c0dSopenharmony_ci auto ops_offset = 421be168c0dSopenharmony_ci- schema::CreateTensor(fbb, 0, data_type, fbb.CreateVector(dims.data(), dims.size()), 422be168c0dSopenharmony_ci- static_cast<schema::Format>(format), 0, 0, fbb.CreateVector(data.data(), data.size()), 423be168c0dSopenharmony_ci- ConvertQuantParams(fbb, quant_params), 0, fbb.CreateString(name.c_str(), name.size())); 424be168c0dSopenharmony_ci+ schema::CreateTensor(fbb, 0, data_type, fbb.CreateVector(dims, dims_size), 425be168c0dSopenharmony_ci+ static_cast<schema::Format>(format), 0, 0, fbb.CreateVector(data, data_size), 426be168c0dSopenharmony_ci+ ConvertQuantParams(fbb, quant_params, quant_params_size), 0, fbb.CreateString(name, strlen(name))); 427be168c0dSopenharmony_ci fbb.Finish(ops_offset); 428be168c0dSopenharmony_ci auto new_addr = MindIRMemoryManager::GetInstance()->CreateTensorFromBuilder(fbb, nullptr); 429be168c0dSopenharmony_ci auto ret_value = flatbuffers::GetMutableRoot<schema::Tensor>(new_addr); 430be168c0dSopenharmony_ci@@ -332,7 +332,7 @@ void MindIR_Tensor_SetQuantParams(TensorPtr *tensor, const std::vector<QuantPara 431be168c0dSopenharmony_ci } 432be168c0dSopenharmony_ci auto ops_offset = 433be168c0dSopenharmony_ci schema::CreateTensor(fbb, 0, value->dataType(), dims, static_cast<schema::Format>(value->format()), 0, 0, data, 434be168c0dSopenharmony_ci- ConvertQuantParams(fbb, quant_params), 0, name); 435be168c0dSopenharmony_ci+ ConvertQuantParams(fbb, quant_params.data(), quant_params.size()), 0, name); 436be168c0dSopenharmony_ci fbb.Finish(ops_offset); 437be168c0dSopenharmony_ci auto new_addr = MindIRMemoryManager::GetInstance()->CreateTensorFromBuilder(fbb, value); 438be168c0dSopenharmony_ci auto ret_value = flatbuffers::GetMutableRoot<schema::Primitive>(new_addr); 439be168c0dSopenharmony_cidiff --git a/mindspore/lite/mindir/src/utils.cc b/mindspore/lite/mindir/src/utils.cc 440be168c0dSopenharmony_ciindex b044f414..870802a9 100644 441be168c0dSopenharmony_ci--- a/mindspore/lite/mindir/src/utils.cc 442be168c0dSopenharmony_ci+++ b/mindspore/lite/mindir/src/utils.cc 443be168c0dSopenharmony_ci@@ -63,21 +63,24 @@ flatbuffers::Offset<schema::Vec2D> CreateVec2D(flatbuffers::FlatBufferBuilder &f 444be168c0dSopenharmony_ci } 445be168c0dSopenharmony_ci flatbuffers::Offset<schema::Vec2D> CreateVec2D(flatbuffers::FlatBufferBuilder &fbb, 446be168c0dSopenharmony_ci const mindspore::schema::Vec2D *data) { 447be168c0dSopenharmony_ci- auto data_inner = data->data(); 448be168c0dSopenharmony_ci std::vector<flatbuffers::Offset<schema::Vec>> vet2d; 449be168c0dSopenharmony_ci- vet2d.reserve(data_inner->size()); 450be168c0dSopenharmony_ci- for (const auto data_one : *data_inner) { 451be168c0dSopenharmony_ci- vet2d.emplace_back(schema::CreateVec(fbb, fbb.CreateVector(data_one->data()->data(), data_one->data()->size()))); 452be168c0dSopenharmony_ci+ if (data != nullptr) { 453be168c0dSopenharmony_ci+ auto data_inner = data->data(); 454be168c0dSopenharmony_ci+ vet2d.reserve(data_inner->size()); 455be168c0dSopenharmony_ci+ for (const auto data_one : *data_inner) { 456be168c0dSopenharmony_ci+ vet2d.emplace_back(schema::CreateVec(fbb, fbb.CreateVector(data_one->data()->data(), data_one->data()->size()))); 457be168c0dSopenharmony_ci+ } 458be168c0dSopenharmony_ci } 459be168c0dSopenharmony_ci flatbuffers::Offset<schema::Vec2D> v2d = schema::CreateVec2D(fbb, fbb.CreateVector(vet2d)); 460be168c0dSopenharmony_ci return v2d; 461be168c0dSopenharmony_ci } 462be168c0dSopenharmony_ci 463be168c0dSopenharmony_ci flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>>> ConvertQuantParams( 464be168c0dSopenharmony_ci- flatbuffers::FlatBufferBuilder &fbb, const std::vector<QuantParam> &quant_params) { 465be168c0dSopenharmony_ci+ flatbuffers::FlatBufferBuilder &fbb, const QuantParam *quant_params, uint32_t quant_params_size) { 466be168c0dSopenharmony_ci std::vector<flatbuffers::Offset<mindspore::schema::QuantParam>> tmp_vec; 467be168c0dSopenharmony_ci- tmp_vec.reserve(quant_params.size()); 468be168c0dSopenharmony_ci- for (auto q_param : quant_params) { 469be168c0dSopenharmony_ci+ tmp_vec.reserve(quant_params_size); 470be168c0dSopenharmony_ci+ for (uint32_t i = 0; i < quant_params_size; i++) { 471be168c0dSopenharmony_ci+ QuantParam q_param = quant_params[i]; 472be168c0dSopenharmony_ci tmp_vec.emplace_back(schema::CreateQuantParam(fbb, q_param.scale, q_param.zeroPoint, 0, 0, true, q_param.numBits)); 473be168c0dSopenharmony_ci } 474be168c0dSopenharmony_ci flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>>> ret_quant_param = 475be168c0dSopenharmony_ci@@ -89,10 +92,12 @@ flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>> 476be168c0dSopenharmony_ci flatbuffers::FlatBufferBuilder &fbb, 477be168c0dSopenharmony_ci const flatbuffers::Vector<flatbuffers::Offset<mindspore::schema::QuantParam>> *quant_params) { 478be168c0dSopenharmony_ci std::vector<flatbuffers::Offset<mindspore::schema::QuantParam>> tmp_vec; 479be168c0dSopenharmony_ci- tmp_vec.reserve(quant_params->size()); 480be168c0dSopenharmony_ci- for (auto q_param : *quant_params) { 481be168c0dSopenharmony_ci- tmp_vec.emplace_back( 482be168c0dSopenharmony_ci- schema::CreateQuantParam(fbb, q_param->scale(), q_param->zeroPoint(), 0, 0, true, q_param->numBits())); 483be168c0dSopenharmony_ci+ if (quant_params != nullptr && quant_params->size() != 0) { 484be168c0dSopenharmony_ci+ tmp_vec.reserve(quant_params->size()); 485be168c0dSopenharmony_ci+ for (auto q_param : *quant_params) { 486be168c0dSopenharmony_ci+ tmp_vec.emplace_back( 487be168c0dSopenharmony_ci+ schema::CreateQuantParam(fbb, q_param->scale(), q_param->zeroPoint(), 0, 0, true, q_param->numBits())); 488be168c0dSopenharmony_ci+ } 489be168c0dSopenharmony_ci } 490be168c0dSopenharmony_ci flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<schema::QuantParam>>> ret_quant_param = 491be168c0dSopenharmony_ci fbb.CreateVector(tmp_vec.data(), tmp_vec.size()); 492be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.cc b/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.cc 493be168c0dSopenharmony_ciindex ca195af4..d8450141 100644 494be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.cc 495be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.cc 496be168c0dSopenharmony_ci@@ -52,6 +52,12 @@ void NNRTDelegate::InitCachePath() { 497be168c0dSopenharmony_ci } 498be168c0dSopenharmony_ci 499be168c0dSopenharmony_ci Status NNRTDelegate::Build(DelegateModel<schema::Primitive> *model) { 500be168c0dSopenharmony_ci+ // dequant litegraph 501be168c0dSopenharmony_ci+ auto ret_dequant = DequantLiteGraph(lite_graph_); 502be168c0dSopenharmony_ci+ if (ret_dequant != kSuccess) { 503be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "Dequant litegraph failed."; 504be168c0dSopenharmony_ci+ return kLiteError; 505be168c0dSopenharmony_ci+ } 506be168c0dSopenharmony_ci #ifdef SUPPORT_NNRT_METAGRAPH 507be168c0dSopenharmony_ci if (IsKirinNPU()) { 508be168c0dSopenharmony_ci MS_LOG(DEBUG) << "Choose to build nnrt model with Metagraph"; 509be168c0dSopenharmony_ci@@ -121,22 +127,11 @@ Status NNRTDelegate::BuildKirinNPUModel(DelegateModel<schema::Primitive> *model) 510be168c0dSopenharmony_ci MS_LOG_DEBUG << "set extension, item name: " << dst_extension.name << ", value size: " << dst_extension.valueSize; 511be168c0dSopenharmony_ci } 512be168c0dSopenharmony_ci 513be168c0dSopenharmony_ci- if (IsCustomModel()) { 514be168c0dSopenharmony_ci- auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, lite_graph_); 515be168c0dSopenharmony_ci- if (ret != OH_NN_SUCCESS) { 516be168c0dSopenharmony_ci- MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret; 517be168c0dSopenharmony_ci- OH_NNModel_Destroy(&nn_model); 518be168c0dSopenharmony_ci- return kLiteError; 519be168c0dSopenharmony_ci- } 520be168c0dSopenharmony_ci- } else { 521be168c0dSopenharmony_ci- SetKirinModelInputsAndOutputs(nn_model); 522be168c0dSopenharmony_ci- auto ret = OH_NNModel_BuildFromMetaGraph(nn_model, meta_graph_, extensions.data(), extensions.size()); 523be168c0dSopenharmony_ci- FreeLiteGraph(&lite_graph_); 524be168c0dSopenharmony_ci- if (ret != OH_NN_SUCCESS) { 525be168c0dSopenharmony_ci- MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret; 526be168c0dSopenharmony_ci- OH_NNModel_Destroy(&nn_model); 527be168c0dSopenharmony_ci- return kLiteError; 528be168c0dSopenharmony_ci- } 529be168c0dSopenharmony_ci+ auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, lite_graph_, extensions.data(), extensions.size()); 530be168c0dSopenharmony_ci+ if (ret != OH_NN_SUCCESS) { 531be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret; 532be168c0dSopenharmony_ci+ OH_NNModel_Destroy(&nn_model); 533be168c0dSopenharmony_ci+ return kLiteError; 534be168c0dSopenharmony_ci } 535be168c0dSopenharmony_ci 536be168c0dSopenharmony_ci auto ret2 = CreateFullModelKernel(model, nn_model); 537be168c0dSopenharmony_ci@@ -147,36 +142,6 @@ Status NNRTDelegate::BuildKirinNPUModel(DelegateModel<schema::Primitive> *model) 538be168c0dSopenharmony_ci return kSuccess; 539be168c0dSopenharmony_ci } 540be168c0dSopenharmony_ci 541be168c0dSopenharmony_ci-std::vector<OH_NN_TensorInfo> NNRTDelegate::CreateNNTensorInfos(const std::vector<uint32_t> &indices) const { 542be168c0dSopenharmony_ci- std::vector<OH_NN_TensorInfo> nn_tensor_infos; 543be168c0dSopenharmony_ci- for (auto index: indices) { 544be168c0dSopenharmony_ci- auto tensor = lite_graph_->all_tensors_[index]; 545be168c0dSopenharmony_ci- auto shape = tensor->dims(); 546be168c0dSopenharmony_ci- auto data_type = tensor->dataType(); 547be168c0dSopenharmony_ci- auto name = tensor->name(); 548be168c0dSopenharmony_ci- auto format = tensor->format(); 549be168c0dSopenharmony_ci- 550be168c0dSopenharmony_ci- OH_NN_TensorInfo info; 551be168c0dSopenharmony_ci- info.dataType = CastToNNRTDataType(static_cast<mindspore::DataType>(data_type)); 552be168c0dSopenharmony_ci- info.dimensions = shape->data(); 553be168c0dSopenharmony_ci- info.dimensionCount = shape->size(); 554be168c0dSopenharmony_ci- strcpy(info.name, name->c_str()); 555be168c0dSopenharmony_ci- info.format = CastToNNRTFormat(static_cast<Format>(format)); 556be168c0dSopenharmony_ci- nn_tensor_infos.push_back(info); 557be168c0dSopenharmony_ci- } 558be168c0dSopenharmony_ci- return nn_tensor_infos; 559be168c0dSopenharmony_ci-} 560be168c0dSopenharmony_ci- 561be168c0dSopenharmony_ci-Status NNRTDelegate::SetKirinModelInputsAndOutputs(OH_NNModel *nn_model) { 562be168c0dSopenharmony_ci- std::vector<OH_NN_TensorInfo> inputInfos; 563be168c0dSopenharmony_ci- std::vector<OH_NN_TensorInfo> outputInfos; 564be168c0dSopenharmony_ci- auto input_infos = CreateNNTensorInfos(lite_graph_->input_indices_); 565be168c0dSopenharmony_ci- auto output_infos = CreateNNTensorInfos(lite_graph_->output_indices_); 566be168c0dSopenharmony_ci- OH_NNModel_SetInputsAndOutputsInfo(nn_model, input_infos.data(), input_infos.size(), output_infos.data(), 567be168c0dSopenharmony_ci- output_infos.size()); 568be168c0dSopenharmony_ci- return kSuccess; 569be168c0dSopenharmony_ci-} 570be168c0dSopenharmony_ci- 571be168c0dSopenharmony_ci Status NNRTDelegate::CreateFullModelKernel(DelegateModel<schema::Primitive> *model, OH_NNModel *nn_model) { 572be168c0dSopenharmony_ci OH_NNCompilation *nn_compilation = OH_NNCompilation_Construct(nn_model); 573be168c0dSopenharmony_ci if (nn_compilation == nullptr) { 574be168c0dSopenharmony_ci@@ -277,7 +242,7 @@ OH_NNModel *NNRTDelegate::CreateFullNNModel() { 575be168c0dSopenharmony_ci return nullptr; 576be168c0dSopenharmony_ci } 577be168c0dSopenharmony_ci 578be168c0dSopenharmony_ci- auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, lite_graph_); 579be168c0dSopenharmony_ci+ auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, lite_graph_, nullptr, 0); 580be168c0dSopenharmony_ci if (ret != OH_NN_SUCCESS) { 581be168c0dSopenharmony_ci MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret; 582be168c0dSopenharmony_ci OH_NNModel_Destroy(&nn_model); 583be168c0dSopenharmony_ci@@ -531,7 +496,7 @@ Status NNRTDelegate::CreateNNRTSubgraphKernels(DelegateModel<schema::Primitive> 584be168c0dSopenharmony_ci auto sub_lite_graph = sub_lite_graphs[i]; 585be168c0dSopenharmony_ci 586be168c0dSopenharmony_ci OH_NNModel *nn_model = OH_NNModel_Construct(); 587be168c0dSopenharmony_ci- auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, sub_lite_graph); 588be168c0dSopenharmony_ci+ auto ret = OH_NNModel_BuildFromLiteGraph(nn_model, sub_lite_graph, nullptr, 0); 589be168c0dSopenharmony_ci if (ret != OH_NN_SUCCESS) { 590be168c0dSopenharmony_ci MS_LOG(ERROR) << "Build NNModel failed, ret: " << ret; 591be168c0dSopenharmony_ci OH_NNModel_Destroy(&nn_model); 592be168c0dSopenharmony_ci@@ -735,10 +700,6 @@ OH_NN_DataType NNRTDelegate::CastToNNRTDataType(DataType data_type) { 593be168c0dSopenharmony_ci return iter->second; 594be168c0dSopenharmony_ci } 595be168c0dSopenharmony_ci 596be168c0dSopenharmony_ci-OH_NN_Format NNRTDelegate::CastToNNRTFormat(Format format) { 597be168c0dSopenharmony_ci- return OH_NN_FORMAT_NHWC; 598be168c0dSopenharmony_ci-} 599be168c0dSopenharmony_ci- 600be168c0dSopenharmony_ci Status NNRTDelegate::PrepareOutputs(DelegateModel<schema::Primitive> *model, 601be168c0dSopenharmony_ci OH_NNExecutor *oh_nn_executor) { 602be168c0dSopenharmony_ci auto output_tensors = model->outputs(); 603be168c0dSopenharmony_ci@@ -754,6 +715,103 @@ Status NNRTDelegate::PrepareOutputs(DelegateModel<schema::Primitive> *model, 604be168c0dSopenharmony_ci return kSuccess; 605be168c0dSopenharmony_ci } 606be168c0dSopenharmony_ci 607be168c0dSopenharmony_ci+schema::Tensor *NNRTDelegate::TensorToSchemaTensor(Tensor *lite_tensor, schema::Tensor *schema_tensor) { 608be168c0dSopenharmony_ci+ flatbuffers::FlatBufferBuilder fbb(1024); 609be168c0dSopenharmony_ci+ auto shape = lite_tensor->shape(); 610be168c0dSopenharmony_ci+ std::vector<int32_t> dim_vec(shape.begin(), shape.end()); 611be168c0dSopenharmony_ci+ 612be168c0dSopenharmony_ci+ auto quant_params = lite_tensor->quant_params(); 613be168c0dSopenharmony_ci+ std::vector<flatbuffers::Offset<mindspore::schema::QuantParam>> quant_vec; 614be168c0dSopenharmony_ci+ quant_vec.reserve(quant_params.size()); 615be168c0dSopenharmony_ci+ for (auto q_param : quant_params) { 616be168c0dSopenharmony_ci+ quant_vec.emplace_back(schema::CreateQuantParam(fbb, q_param.scale, q_param.zeroPoint, 0, 0, true, q_param.bitNum)); 617be168c0dSopenharmony_ci+ } 618be168c0dSopenharmony_ci+ auto quant_clusters = lite_tensor->quant_clusters(); 619be168c0dSopenharmony_ci+ 620be168c0dSopenharmony_ci+ auto external_data = schema_tensor->externalData(); 621be168c0dSopenharmony_ci+ std::vector<flatbuffers::Offset<mindspore::schema::ExternalData>> external_data_vec; 622be168c0dSopenharmony_ci+ if (external_data != nullptr) { 623be168c0dSopenharmony_ci+ for (auto ed : *external_data) { 624be168c0dSopenharmony_ci+ external_data_vec.emplace_back(schema::CreateExternalDataDirect(fbb, ed->checkSum()->c_str(), ed->location()->c_str(), 0, ed->length())); 625be168c0dSopenharmony_ci+ } 626be168c0dSopenharmony_ci+ } 627be168c0dSopenharmony_ci+ uint8_t *data_src = reinterpret_cast<uint8_t *>(lite_tensor->data()); 628be168c0dSopenharmony_ci+ std::vector<uint8_t> data_vec(data_src, data_src + lite_tensor->Size()); 629be168c0dSopenharmony_ci+ auto tensor_offset = schema::CreateTensorDirect(fbb, schema_tensor->nodeType(), lite_tensor->data_type(), &dim_vec, 630be168c0dSopenharmony_ci+ schema_tensor->format(), 0, 0, &data_vec, &quant_vec, 631be168c0dSopenharmony_ci+ &quant_clusters, schema_tensor->name()->c_str(), 632be168c0dSopenharmony_ci+ schema_tensor->enableHuffmanCode(), 633be168c0dSopenharmony_ci+ mindspore::schema::WeightQuantCompressType_NONE, &external_data_vec); 634be168c0dSopenharmony_ci+ fbb.Finish(tensor_offset); 635be168c0dSopenharmony_ci+ 636be168c0dSopenharmony_ci+ auto buf = fbb.GetBufferPointer(); 637be168c0dSopenharmony_ci+ if (buf == nullptr) { 638be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "GetBufferPointer return nullptr"; 639be168c0dSopenharmony_ci+ fbb.Clear(); 640be168c0dSopenharmony_ci+ return nullptr; 641be168c0dSopenharmony_ci+ } 642be168c0dSopenharmony_ci+ size_t byte_num = fbb.GetSize(); 643be168c0dSopenharmony_ci+ auto tensor_buf = reinterpret_cast<char *>(malloc(byte_num)); 644be168c0dSopenharmony_ci+ if (tensor_buf == nullptr) { 645be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "malloc primitive_buf_ failed"; 646be168c0dSopenharmony_ci+ fbb.Clear(); 647be168c0dSopenharmony_ci+ return nullptr; 648be168c0dSopenharmony_ci+ } 649be168c0dSopenharmony_ci+ memcpy(tensor_buf, buf, fbb.GetSize()); 650be168c0dSopenharmony_ci+ auto tensor = flatbuffers::GetRoot<schema::Tensor>(tensor_buf); 651be168c0dSopenharmony_ci+ fbb.Clear(); 652be168c0dSopenharmony_ci+ return const_cast<schema::Tensor *>(tensor); 653be168c0dSopenharmony_ci+} 654be168c0dSopenharmony_ci+ 655be168c0dSopenharmony_ci+int NNRTDelegate::DequantNodeInputs(LiteGraph::Node *node) { 656be168c0dSopenharmony_ci+ auto in_size = node->input_indices_.size(); 657be168c0dSopenharmony_ci+ int ret = RET_OK; 658be168c0dSopenharmony_ci+ for (size_t i = 0; i < in_size; i++) { 659be168c0dSopenharmony_ci+ auto tensor_index = node->input_indices_[i]; 660be168c0dSopenharmony_ci+ auto *src_tensor = lite_graph_->all_tensors_[tensor_index]; 661be168c0dSopenharmony_ci+ auto input = dequant_src_tensors_->at(tensor_index); 662be168c0dSopenharmony_ci+ if (!input->IsConst() || !(src_tensor->dataType() == kNumberTypeInt8 || 663be168c0dSopenharmony_ci+ src_tensor->dataType() == kNumberTypeInt16 || src_tensor->dataType() == kNumberTypeInt32)) { 664be168c0dSopenharmony_ci+ continue; 665be168c0dSopenharmony_ci+ } 666be168c0dSopenharmony_ci+ auto dst_tensor = TensorToSchemaTensor(input, src_tensor); 667be168c0dSopenharmony_ci+ if (dst_tensor != nullptr) { 668be168c0dSopenharmony_ci+ dequant_schema_tensors_.emplace(tensor_index, dst_tensor); 669be168c0dSopenharmony_ci+ replaced_schema_tensors_.emplace_back(src_tensor); 670be168c0dSopenharmony_ci+ } else { 671be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "create dequant schema tensor failed, node: " << node->name_ << ", tensor_index: " 672be168c0dSopenharmony_ci+ << tensor_index; 673be168c0dSopenharmony_ci+ ret = RET_ERROR; 674be168c0dSopenharmony_ci+ break; 675be168c0dSopenharmony_ci+ } 676be168c0dSopenharmony_ci+ } 677be168c0dSopenharmony_ci+ return ret; 678be168c0dSopenharmony_ci+} 679be168c0dSopenharmony_ci+ 680be168c0dSopenharmony_ci+Status NNRTDelegate::DequantLiteGraph(LiteGraph *lite_graph) { 681be168c0dSopenharmony_ci+ for (auto node_index : lite_graph->sub_graphs_[0]->node_indices_) { 682be168c0dSopenharmony_ci+ auto node = lite_graph->all_nodes_[node_index]; 683be168c0dSopenharmony_ci+ 684be168c0dSopenharmony_ci+ if (node->quant_type_ != static_cast<int>(schema::QuantType_QUANT_WEIGHT)) { 685be168c0dSopenharmony_ci+ continue; 686be168c0dSopenharmony_ci+ } 687be168c0dSopenharmony_ci+ auto ret = DequantNodeInputs(node); 688be168c0dSopenharmony_ci+ if (ret != RET_OK) { 689be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "Dequant node failed: " << ret << ", node_name: " << node->name_; 690be168c0dSopenharmony_ci+ for (auto iter : dequant_schema_tensors_) { 691be168c0dSopenharmony_ci+ delete iter.second; 692be168c0dSopenharmony_ci+ iter.second = nullptr; 693be168c0dSopenharmony_ci+ } 694be168c0dSopenharmony_ci+ return kLiteNotSupport; 695be168c0dSopenharmony_ci+ } 696be168c0dSopenharmony_ci+ node->quant_type_ = schema::QuantType_QUANT_NONE; 697be168c0dSopenharmony_ci+ } 698be168c0dSopenharmony_ci+ for (auto iter : dequant_schema_tensors_) { 699be168c0dSopenharmony_ci+ lite_graph_->all_tensors_[iter.first] = iter.second; 700be168c0dSopenharmony_ci+ } 701be168c0dSopenharmony_ci+ return kSuccess; 702be168c0dSopenharmony_ci+} 703be168c0dSopenharmony_ci+ 704be168c0dSopenharmony_ci void NNRTDelegate::ShallowCopyLiteGraph(const lite::LiteGraph &lite_graph) { 705be168c0dSopenharmony_ci Status ret; 706be168c0dSopenharmony_ci for (auto node : lite_graph.all_nodes_) { 707be168c0dSopenharmony_ci@@ -863,6 +921,10 @@ NNRTDelegate::~NNRTDelegate() { 708be168c0dSopenharmony_ci if (lite_graph_ != nullptr) { 709be168c0dSopenharmony_ci MS_LOG(ERROR) << "Delete NNRTDelegate."; 710be168c0dSopenharmony_ci } 711be168c0dSopenharmony_ci+ for (auto iter : dequant_schema_tensors_) { 712be168c0dSopenharmony_ci+ delete iter.second; 713be168c0dSopenharmony_ci+ iter.second = nullptr; 714be168c0dSopenharmony_ci+ } 715be168c0dSopenharmony_ci } 716be168c0dSopenharmony_ci } // namespace lite 717be168c0dSopenharmony_ci } // namespace mindspore 718be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.h b/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.h 719be168c0dSopenharmony_ciindex 4cf357d6..778553ef 100644 720be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.h 721be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/delegate/nnrt/nnrt_delegate.h 722be168c0dSopenharmony_ci@@ -50,6 +50,9 @@ class NNRTDelegate : public Delegate { 723be168c0dSopenharmony_ci void SetMetaGraph(const void *meta_graph) { 724be168c0dSopenharmony_ci meta_graph_ = meta_graph; 725be168c0dSopenharmony_ci } 726be168c0dSopenharmony_ci+ void SetDequantTensors(std::vector<Tensor *> *src_tensors) { 727be168c0dSopenharmony_ci+ dequant_src_tensors_ = src_tensors; 728be168c0dSopenharmony_ci+ } 729be168c0dSopenharmony_ci static std::vector<NNRTOpRange> GetNNRTSubgraphRanges(DelegateModel<schema::Primitive> *model, 730be168c0dSopenharmony_ci const std::vector<bool> &op_supports); 731be168c0dSopenharmony_ci 732be168c0dSopenharmony_ci@@ -73,14 +76,14 @@ class NNRTDelegate : public Delegate { 733be168c0dSopenharmony_ci Status PrepareOutputs(DelegateModel<schema::Primitive> *model, OH_NNExecutor *oh_nn_executor); 734be168c0dSopenharmony_ci Status InitNNCompilation(OH_NNCompilation *nn_compilation) const; 735be168c0dSopenharmony_ci static OH_NN_DataType CastToNNRTDataType(mindspore::DataType data_type); 736be168c0dSopenharmony_ci- static OH_NN_Format CastToNNRTFormat(Format format); 737be168c0dSopenharmony_ci bool IsCustomModel() const; 738be168c0dSopenharmony_ci+ Status DequantLiteGraph(LiteGraph *lite_graph); 739be168c0dSopenharmony_ci+ int DequantNodeInputs(LiteGraph::Node *node); 740be168c0dSopenharmony_ci+ schema::Tensor *TensorToSchemaTensor(Tensor *lite_tensor, schema::Tensor *schema_tensor); 741be168c0dSopenharmony_ci 742be168c0dSopenharmony_ci #ifdef SUPPORT_NNRT_METAGRAPH 743be168c0dSopenharmony_ci bool IsKirinNPU() const; 744be168c0dSopenharmony_ci Status BuildKirinNPUModel(DelegateModel<schema::Primitive> *model); 745be168c0dSopenharmony_ci- Status SetKirinModelInputsAndOutputs(OH_NNModel *nn_model); 746be168c0dSopenharmony_ci- std::vector<OH_NN_TensorInfo> CreateNNTensorInfos(const std::vector<uint32_t> &indices) const; 747be168c0dSopenharmony_ci Status CreateFullModelKernel(DelegateModel<schema::Primitive> *model, OH_NNModel *nn_model); 748be168c0dSopenharmony_ci #endif 749be168c0dSopenharmony_ci 750be168c0dSopenharmony_ci@@ -90,6 +93,9 @@ class NNRTDelegate : public Delegate { 751be168c0dSopenharmony_ci std::string cache_path_ = ""; 752be168c0dSopenharmony_ci uint32_t cache_version_ = 0; 753be168c0dSopenharmony_ci std::vector<OH_NNExecutor *> nn_executor_list_; 754be168c0dSopenharmony_ci+ std::vector<Tensor *> *dequant_src_tensors_; 755be168c0dSopenharmony_ci+ std::map<uint32_t, schema::Tensor *> dequant_schema_tensors_; 756be168c0dSopenharmony_ci+ std::vector<schema::Tensor *> replaced_schema_tensors_; 757be168c0dSopenharmony_ci }; 758be168c0dSopenharmony_ci } // namespace lite 759be168c0dSopenharmony_ci } // namespace mindspore 760be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/scheduler.cc b/mindspore/lite/src/litert/scheduler.cc 761be168c0dSopenharmony_ciindex 96efd972..d6749471 100644 762be168c0dSopenharmony_ci--- a/mindspore/lite/src/litert/scheduler.cc 763be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/scheduler.cc 764be168c0dSopenharmony_ci@@ -514,6 +514,7 @@ int Scheduler::ReplaceDelegateKernels(std::vector<kernel::KernelExec *> *dst_ker 765be168c0dSopenharmony_ci void *meta_graph = reinterpret_cast<void *>( 766be168c0dSopenharmony_ci const_cast<mindspore::schema::MetaGraph *>(mindspore::schema::GetMetaGraph(this->src_model_->buf))); 767be168c0dSopenharmony_ci delegate->SetMetaGraph(meta_graph); 768be168c0dSopenharmony_ci+ delegate->SetDequantTensors(this->src_tensors_); 769be168c0dSopenharmony_ci } 770be168c0dSopenharmony_ci #endif 771be168c0dSopenharmony_ci 772be168c0dSopenharmony_ci-- 773be168c0dSopenharmony_ci2.17.1 774be168c0dSopenharmony_ci 775