Merge pull request !6007 from zhengjun10/mastertags/v1.0.0
| @@ -15,6 +15,8 @@ | |||
| */ | |||
| #include "src/ops/deconv2d.h" | |||
| #include <memory> | |||
| #include <string> | |||
| namespace mindspore { | |||
| namespace lite { | |||
| @@ -56,7 +58,86 @@ void DeConv2D::SetHasBias(bool has_bias) { this->primitive_->value.AsDeConv2D()- | |||
| void DeConv2D::SetActivationType(int activation_type) { | |||
| this->primitive_->value.AsDeConv2D()->activationType = (schema::ActivationType)activation_type; | |||
| } | |||
| void DeConv2D::PopulaterDeConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group) { | |||
| auto attr = std::make_unique<schema::DeConv2DT>(); | |||
| attr->group = group; | |||
| auto format = GetValue<std::string>(prim.GetAttr("data_format")); | |||
| if (format == "NCHW") { | |||
| attr->format = schema::Format_NCHW; | |||
| } else if (format == "NHWC") { | |||
| attr->format = schema::Format_NHWC; | |||
| } else { | |||
| attr->format = schema::Format_NUM_OF_FORMAT; | |||
| } | |||
| auto pad_list = GetValue<std::vector<int>>(prim.GetAttr("pad_list")); | |||
| attr->padUp = pad_list[0]; | |||
| attr->padDown = pad_list[1]; | |||
| attr->padLeft = pad_list[2]; | |||
| attr->padRight = pad_list[3]; | |||
| auto dilation = GetValue<std::vector<int>>(prim.GetAttr("dilation")); | |||
| attr->dilateH = dilation[0]; | |||
| attr->dilateW = dilation[1]; | |||
| auto kernel_size = GetValue<std::vector<int>>(prim.GetAttr("kernel_size")); | |||
| attr->kernelH = kernel_size[0]; | |||
| attr->kernelW = kernel_size[1]; | |||
| auto stride = GetValue<std::vector<int>>(prim.GetAttr("stride")); | |||
| attr->strideH = stride[0]; | |||
| attr->strideW = stride[1]; | |||
| attr->channelOut = GetValue<int>(prim.GetAttr("out_channel")); | |||
| auto pad_mode = GetValue<std::string>(prim.GetAttr("pad_mode")); | |||
| if (pad_mode == "valid" || pad_mode == "VALID") { | |||
| attr->padMode = schema::PadMode_VALID; | |||
| } else if (pad_mode == "same" || pad_mode == "SAME") { | |||
| attr->padMode = schema::PadMode_SAME; | |||
| } else { | |||
| attr->padMode = schema::PadMode_NOTSET; | |||
| } | |||
| if (prim.GetAttr("activation_name") != nullptr) { | |||
| std::string activate_name = GetValue<std::string>(prim.GetAttr("activation_name")); | |||
| attr->activationType = kActivationTypeMap[activate_name]; | |||
| } else { | |||
| attr->activationType = schema::ActivationType_NO_ACTIVATION; | |||
| } | |||
| // attr->padMode = schema::PadMode_SAME; | |||
| // attr->activationType = schema::ActivationType_RELU; | |||
| primitive->value.type = schema::PrimitiveType_DeConv2D; | |||
| primitive->value.value = attr.release(); | |||
| } | |||
| int DeConv2D::UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) { | |||
| if (this->primitive_ == nullptr) { | |||
| this->primitive_ = new (std::nothrow) schema::PrimitiveT; | |||
| if (this->primitive_ == nullptr) { | |||
| MS_LOG(ERROR) << "new primitiveT failed"; | |||
| return RET_ERROR; | |||
| } | |||
| this->primitive_->value.type = schema::PrimitiveType_DeConv2D; | |||
| } | |||
| if (this->primitive_->value.type != schema::PrimitiveType_DeConv2D) { | |||
| MS_LOG(ERROR) << "primitive_ type is error:" << this->primitive_->value.type; | |||
| return RET_ERROR; | |||
| } | |||
| int group = GetValue<int>(prim.GetAttr("group")); | |||
| if (group == 1) { | |||
| PopulaterDeConv2DSingleGroup(prim, this->primitive_, group); | |||
| } | |||
| if (GetQuantType() == schema::QuantType_AwareTraining) { | |||
| std::vector<std::vector<schema::QuantParamT>> vecInputQuantParam; | |||
| std::vector<std::vector<schema::QuantParamT>> vecOutputQuantParam; | |||
| PopulaterQuantParam(prim, &vecInputQuantParam, &vecOutputQuantParam); | |||
| SetInputQuantParam(vecInputQuantParam); | |||
| SetOutputQuantParam(vecOutputQuantParam); | |||
| } | |||
| return RET_OK; | |||
| } | |||
| #else | |||
| int DeConv2D::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { | |||
| MS_ASSERT(nullptr != primitive); | |||
| @@ -48,6 +48,9 @@ class DeConv2D : public PrimitiveC { | |||
| void SetDilateH(int dilate_h); | |||
| void SetHasBias(bool has_bias); | |||
| void SetActivationType(int activation_type); | |||
| void PopulaterDeConv2DSingleGroup(const Primitive &prim, schema::PrimitiveT *primitive, const int &group); | |||
| int UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) override; | |||
| #else | |||
| DeConv2D() = default; | |||
| @@ -15,6 +15,7 @@ | |||
| */ | |||
| #include "src/ops/elu.h" | |||
| #include <memory> | |||
| namespace mindspore { | |||
| namespace lite { | |||
| @@ -23,6 +24,27 @@ float Elu::GetAlpha() const { return this->primitive_->value.AsElu()->alpha; } | |||
| void Elu::SetAlpha(float alpha) { this->primitive_->value.AsElu()->alpha = alpha; } | |||
| int Elu::UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) { | |||
| if (this->primitive_ == nullptr) { | |||
| this->primitive_ = new (std::nothrow) schema::PrimitiveT; | |||
| if (this->primitive_ == nullptr) { | |||
| MS_LOG(ERROR) << "new primitiveT failed"; | |||
| return RET_ERROR; | |||
| } | |||
| this->primitive_->value.type = schema::PrimitiveType_Elu; | |||
| } | |||
| if (this->primitive_->value.type != schema::PrimitiveType_Elu) { | |||
| MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; | |||
| return RET_ERROR; | |||
| } | |||
| auto attr = std::make_unique<schema::EluT>(); | |||
| this->primitive_->value.value = attr.release(); | |||
| if (this->primitive_->value.value == nullptr) { | |||
| MS_LOG(ERROR) << "new primitiveT value failed"; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| #else | |||
| int Elu::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { | |||
| MS_ASSERT(nullptr != primitive); | |||
| @@ -32,7 +32,7 @@ class Elu : public PrimitiveC { | |||
| Elu() = default; | |||
| explicit Elu(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} | |||
| void SetAlpha(float alpha); | |||
| int UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) override; | |||
| #else | |||
| Elu() = default; | |||
| @@ -15,10 +15,32 @@ | |||
| */ | |||
| #include "src/ops/log.h" | |||
| #include <memory> | |||
| namespace mindspore { | |||
| namespace lite { | |||
| #ifdef PRIMITIVE_WRITEABLE | |||
| int Log::UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) { | |||
| if (this->primitive_ == nullptr) { | |||
| this->primitive_ = new (std::nothrow) schema::PrimitiveT; | |||
| if (this->primitive_ == nullptr) { | |||
| MS_LOG(ERROR) << "new primitiveT failed"; | |||
| return RET_ERROR; | |||
| } | |||
| this->primitive_->value.type = schema::PrimitiveType_Log; | |||
| } | |||
| if (this->primitive_->value.type != schema::PrimitiveType_Log) { | |||
| MS_LOG(ERROR) << "Primitive type is error :" << this->primitive_->value.type; | |||
| return RET_ERROR; | |||
| } | |||
| auto attr = std::make_unique<schema::LogT>(); | |||
| this->primitive_->value.value = attr.release(); | |||
| if (this->primitive_->value.value == nullptr) { | |||
| MS_LOG(ERROR) << "new primitiveT value failed"; | |||
| return RET_ERROR; | |||
| } | |||
| return RET_OK; | |||
| } | |||
| #else | |||
| int Log::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffers::FlatBufferBuilder *fbb) { | |||
| MS_ASSERT(nullptr != primitive); | |||
| @@ -31,6 +31,7 @@ class Log : public ArithmeticSelf { | |||
| MS_DECLARE_PARENT(Log, ArithmeticSelf); | |||
| Log() = default; | |||
| explicit Log(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} | |||
| int UnPackAttr(const Primitive &prim, const std::vector<AnfNodePtr> &inputs) override; | |||
| #else | |||
| Log() = default; | |||
| @@ -369,6 +369,12 @@ std::shared_ptr<PrimitiveC> PrimitiveC::Create(const Primitive &prim, const std: | |||
| return NewPrimitiveC<Add>(prim, inputs, quantType); | |||
| } else if (op_type == "Transpose") { | |||
| return NewPrimitiveC<Transpose>(prim, inputs, quantType); | |||
| } else if (op_type == "Elu") { | |||
| return NewPrimitiveC<Elu>(prim, inputs, quantType); | |||
| } else if (op_type == "Log") { | |||
| return NewPrimitiveC<Log>(prim, inputs, quantType); | |||
| } else if (op_type == "Conv2DBackpropInput") { | |||
| return NewPrimitiveC<DeConv2D>(prim, inputs, quantType); | |||
| } else if (op_type == "tuple_getitem") { | |||
| return NewPrimitiveC<TupleGetItem>(prim, inputs, quantType); | |||
| } else if (op_type == "Softmax") { | |||
| @@ -380,8 +386,6 @@ std::shared_ptr<PrimitiveC> PrimitiveC::Create(const Primitive &prim, const std: | |||
| return NewPrimitiveC<PoolingGrad>(prim, inputs, quantType); | |||
| } else if (op_type == "Conv2DBackpropFilter") { | |||
| return NewPrimitiveC<Conv2DGradFilter>(prim, inputs, quantType); | |||
| } else if (op_type == "Conv2DBackpropInput") { | |||
| return NewPrimitiveC<Conv2DGradInput>(prim, inputs, quantType); | |||
| } else if (op_type == "BiasAddGrad") { | |||
| return NewPrimitiveC<BiasGrad>(prim, inputs, quantType); | |||
| } else if (op_type == "ApplyMomentum") { | |||
| @@ -1,2 +1,3 @@ | |||
| ssd.mindir | |||
| mobilenetv2_438.mindir | |||
| gate_u_net_small-1_110.mindir | |||
| @@ -403,8 +403,8 @@ NodeIter InsertNodeBefore(schema::MetaGraphT *graphT, NodeIter existNodeIter, si | |||
| preTensor->refCount = 0; | |||
| preTensor->data.clear(); | |||
| if (toAddNodeIn->primitive->value.type == schema::PrimitiveType_QuantDTypeCast) { | |||
| preTensor->dataType = toAddNodeIn->primitive->value.AsQuantDTypeCast()->dstT; | |||
| toAddTensor->dataType = toAddNodeIn->primitive->value.AsQuantDTypeCast()->srcT; | |||
| preTensor->dataType = toAddNodeIn->primitive->value.AsQuantDTypeCast()->srcT; | |||
| toAddTensor->dataType = toAddNodeIn->primitive->value.AsQuantDTypeCast()->dstT; | |||
| } | |||
| graphT->allTensors.emplace_back(std::move(toAddTensor)); | |||
| size_t toAddTensorIdx = graphT->allTensors.size() - 1; | |||
| @@ -415,10 +415,10 @@ NodeIter InsertNodeBefore(schema::MetaGraphT *graphT, NodeIter existNodeIter, si | |||
| return graphT->nodes.end(); | |||
| } | |||
| toAddNode->inputIndex.clear(); | |||
| toAddNode->inputIndex.push_back(toAddTensorIdx); | |||
| toAddNode->inputIndex.push_back(preTensorIdx); | |||
| toAddNode->outputIndex.clear(); | |||
| toAddNode->outputIndex.push_back(preTensorIdx); | |||
| for (auto iter = graphT->inputIndex.begin(); iter != graphT->inputIndex.end(); iter++) { | |||
| toAddNode->outputIndex.push_back(toAddTensorIdx); | |||
| for (auto iter = existNode->inputIndex.begin(); iter != existNode->inputIndex.end(); iter++) { | |||
| if (*iter == preTensorIdx) { | |||
| *iter = toAddTensorIdx; | |||
| break; | |||
| @@ -58,6 +58,7 @@ STATUS FormatTransPass::DoModelInputFormatTrans(schema::MetaGraphT *graph) { | |||
| } | |||
| auto graphInputIdxes = graph->inputIndex; | |||
| for (size_t i = 0; i < graphInputIdxes.size(); i++) { | |||
| bool transed = false; | |||
| auto inputIdx = graphInputIdxes.at(i); | |||
| MS_ASSERT(inputIdx < subGraph->allTensors.size()); | |||
| auto &tensor = graph->allTensors.at(inputIdx); | |||
| @@ -84,7 +85,10 @@ STATUS FormatTransPass::DoModelInputFormatTrans(schema::MetaGraphT *graph) { | |||
| graphInTensor->format = schema::Format::Format_NHWC; | |||
| // assume parser not reformat shape | |||
| auto oldDims = graphInTensor->dims; | |||
| graphInTensor->dims = {oldDims[NCHW_N], oldDims[NCHW_H], oldDims[NCHW_W], oldDims[NCHW_C]}; | |||
| if (!transed) { | |||
| graphInTensor->dims = {oldDims[NCHW_N], oldDims[NCHW_H], oldDims[NCHW_W], oldDims[NCHW_C]}; | |||
| transed = true; | |||
| } | |||
| break; | |||
| } | |||
| } | |||
| @@ -58,40 +58,72 @@ std::vector<Tensor *> ConvertTensorToLiteTensor(MetaGraphT *graph, const std::ve | |||
| } | |||
| return lite_tensors; | |||
| } | |||
| void PrintTensorShape(const std::vector<Tensor *> &input_tensors, const std::vector<Tensor *> &output_tensors) { | |||
| int i = 0; | |||
| for (auto input_tensor : input_tensors) { | |||
| std::ostringstream oss; | |||
| for (auto &dim : input_tensor->shape()) { | |||
| oss << " " << dim; | |||
| } | |||
| MS_LOG(DEBUG) << "input shape " << i++ << ":" << oss.str(); | |||
| } | |||
| i = 0; | |||
| for (auto output_tensor : output_tensors) { | |||
| std::ostringstream oss; | |||
| for (auto &dim : output_tensor->shape()) { | |||
| oss << " " << dim; | |||
| } | |||
| MS_LOG(DEBUG) << "output shape" << i++ << ":" << oss.str(); | |||
| } | |||
| } | |||
| void FreeTensors(std::vector<Tensor *> input_tensors, std::vector<Tensor *> output_tensors) { | |||
| input_tensors.clear(); | |||
| input_tensors.shrink_to_fit(); | |||
| output_tensors.clear(); | |||
| output_tensors.shrink_to_fit(); | |||
| } | |||
| } // namespace | |||
| STATUS InferShapePass::Run(MetaGraphT *graph) { | |||
| MS_ASSERT(graph != nullptr); | |||
| for (auto iter = graph->nodes.begin(); iter != graph->nodes.end(); iter++) { | |||
| auto &node = *iter; | |||
| auto input_tensors = ConvertTensorToLiteTensor(graph, node->inputIndex, node->primitive->value.type); | |||
| std::vector<Tensor *> output_tensors; | |||
| if (input_tensors.empty() || input_tensors.size() != node->inputIndex.size()) { | |||
| MS_LOG(ERROR) << "convert input lite tensor error"; | |||
| FreeTensors(input_tensors, output_tensors); | |||
| return RET_INFER_ERR; | |||
| } | |||
| auto output_tensors = ConvertTensorToLiteTensor(graph, node->outputIndex, node->primitive->value.type); | |||
| output_tensors = ConvertTensorToLiteTensor(graph, node->outputIndex, node->primitive->value.type); | |||
| if (output_tensors.empty() || output_tensors.size() != node->outputIndex.size()) { | |||
| MS_LOG(ERROR) << "convert output lite tensor error"; | |||
| FreeTensors(input_tensors, output_tensors); | |||
| return RET_INFER_ERR; | |||
| } | |||
| std::unique_ptr<PrimitiveT> primitiveT(new (std::nothrow) PrimitiveT(*node->primitive)); | |||
| std::unique_ptr<PrimitiveT> primitiveT(new(std::nothrow) PrimitiveT(*node->primitive)); | |||
| if (primitiveT == nullptr) { | |||
| MS_LOG(ERROR) << "copy primitiveT error"; | |||
| FreeTensors(input_tensors, output_tensors); | |||
| return RET_ERROR; | |||
| } | |||
| auto primitiveC = std::shared_ptr<PrimitiveC>(PrimitiveC::Create(primitiveT.release())); | |||
| if (primitiveC == nullptr) { | |||
| MS_LOG(ERROR) << "unpack primitiveT error"; | |||
| FreeTensors(input_tensors, output_tensors); | |||
| return RET_ERROR; | |||
| } | |||
| auto ret = primitiveC->InferShape(input_tensors, output_tensors); | |||
| MS_LOG(DEBUG) << "cur node:" << node->name; | |||
| if (ret == RET_INFER_INVALID) { | |||
| MS_LOG(INFO) << "InferShape shouldn't be done before runtime, name: " << node->name | |||
| << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type) << "flag set to false."; | |||
| } else if (ret != RET_OK) { | |||
| MS_LOG(WARNING) << "InferShape failed, name: " << node->name | |||
| << ", type: " << schema::EnumNamePrimitiveType(node->primitive->value.type); | |||
| FreeTensors(input_tensors, output_tensors); | |||
| return RET_INFER_ERR; | |||
| } | |||
| PrintTensorShape(input_tensors, output_tensors); | |||
| // copy output shape to tensorT | |||
| for (size_t i = 0; i < output_tensors.size(); i++) { | |||
| auto output_dims = output_tensors[i]->shape(); | |||
| @@ -100,12 +132,7 @@ STATUS InferShapePass::Run(MetaGraphT *graph) { | |||
| output_tensor->format = output_tensors[i]->GetFormat(); | |||
| output_tensor->dataType = output_tensors[i]->data_type(); | |||
| } | |||
| for (auto input_tensor : input_tensors) { | |||
| delete input_tensor; | |||
| } | |||
| for (auto output_tensor : output_tensors) { | |||
| delete output_tensor; | |||
| } | |||
| FreeTensors(input_tensors, output_tensors); | |||
| } | |||
| return RET_OK; | |||
| } | |||
| @@ -170,7 +170,9 @@ STATUS WeightFormatHardCodePass::HardCodeMS(const std::unique_ptr<CNodeT> &node, | |||
| if (opType == PrimitiveType_Conv2D) { | |||
| weightTensor->format = schema::Format::Format_KCHW; | |||
| } else if (opType == PrimitiveType_DepthwiseConv2D) { | |||
| weightTensor->format = schema::Format::Format_CKHW; | |||
| weightTensor->format = Format_CKHW; | |||
| } else if (opType == PrimitiveType_DeConv2D) { | |||
| weightTensor->format = Format_KCHW; | |||
| } else { | |||
| MS_LOG(ERROR) << "Unsupported opType: " << EnumNamePrimitiveType(opType) << ", node: " << node->name; | |||
| return RET_ERROR; | |||
| @@ -41,12 +41,12 @@ bool NodePass::Run(const FuncGraphPtr &func_graph) { | |||
| if (seen_node.count(node) > 0 || !manager->all_nodes().contains(node)) { | |||
| continue; | |||
| } | |||
| (void)seen_node.insert(node); | |||
| (void) seen_node.insert(node); | |||
| AnfNodePtr new_node = Run(func_graph, node); | |||
| bool change = (new_node != nullptr); | |||
| if (new_node != nullptr && new_node != node) { | |||
| (void)manager->Replace(node, new_node); | |||
| (void)seen_node.erase(node); | |||
| (void) manager->Replace(node, new_node); | |||
| (void) seen_node.erase(node); | |||
| } else if (new_node == nullptr) { | |||
| new_node = node; | |||
| } | |||
| @@ -61,9 +61,12 @@ bool NodePass::Run(const FuncGraphPtr &func_graph) { | |||
| auto cnode = new_node->cast<CNodePtr>(); | |||
| MS_EXCEPTION_IF_NULL(cnode); | |||
| auto inputs = cnode->inputs(); | |||
| (void)to_process.insert(to_process.end(), inputs.begin(), inputs.end()); | |||
| (void) to_process.insert(to_process.end(), inputs.begin(), inputs.end()); | |||
| } | |||
| changes = changes || change; | |||
| if (changes) { | |||
| MS_LOG(DEBUG) << "pass " << this->name() << "changed node:" << new_node->fullname_with_scope(); | |||
| } | |||
| } | |||
| return changes; | |||
| } | |||
| @@ -174,7 +174,7 @@ const AnfNodePtr ConstFoldPass::Process(const FuncGraphPtr &func_graph, const An | |||
| CheckIfFuncGraphIsNull(func_graph); | |||
| CheckIfAnfNodeIsNull(node); | |||
| if (!node->isa<CNode>()) { | |||
| return node; | |||
| return nullptr; | |||
| } | |||
| auto any_node = node->cast<CNodePtr>(); | |||
| CheckIfCNodeIsNull(any_node); | |||
| @@ -191,7 +191,6 @@ const AnfNodePtr ConstFoldPass::Process(const FuncGraphPtr &func_graph, const An | |||
| continue; | |||
| } | |||
| changed = true; | |||
| MS_LOG(INFO) << "Begin fold node:" << input_node->fullname_with_scope(); | |||
| auto output_nums = GetOutputTensorNum(input_cnode); | |||
| std::vector<Tensor *> output_tensors{output_nums, new Tensor()}; | |||
| auto lite_primitive = GetValueNode<std::shared_ptr<PrimitiveC>>(input_cnode->input(0)); | |||
| @@ -254,6 +253,7 @@ const AnfNodePtr ConstFoldPass::Process(const FuncGraphPtr &func_graph, const An | |||
| MS_LOG(ERROR) << "constant_folding replace cnode failed"; | |||
| return nullptr; | |||
| } | |||
| MS_LOG(DEBUG) << "fold node:" << input_node->fullname_with_scope() << " success "; | |||
| FreeTensors(&input_tensors, &output_tensors); | |||
| delete (lite_kernel); | |||
| } | |||