/** * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INC_FRAMEWORK_GENERATOR_GE_GENERATOR_H_ #define INC_FRAMEWORK_GENERATOR_GE_GENERATOR_H_ #include #include #include #include #include "external/ge/ge_ir_build.h" #include "framework/common/ge_inner_error_codes.h" #include "framework/common/ge_types.h" #include "graph/ge_tensor.h" #include "graph/graph.h" #include "graph/op_desc.h" #include "framework/omg/omg_inner_types.h" #include "graph/detail/attributes_holder.h" namespace ge { const std::string kAttrSupportDynamicShape = "support_dynamicshape"; class GeRootModel; class GE_FUNC_VISIBILITY GeGenerator { public: using InOutTensorRef = std::pair &, const std::vector &>; static GeGenerator &GetInstance() { static GeGenerator Instance; return Instance; } GeGenerator() = default; ~GeGenerator() { (void)Finalize(); } GeGenerator(const GeGenerator &) = delete; GeGenerator &operator=(const GeGenerator &) = delete; Status Initialize(const std::map &options); Status Initialize(const std::map &options, OmgContext &context); Status Finalize(); Status GenerateOfflineModel(const Graph &graph, const std::string &file_name_prefix, const std::vector &inputs = std::vector()); Status GenerateOnlineModel(const Graph &graph, const std::vector &inputs, ge::ModelBufferData &model); Status GenerateInfershapeGraph(const Graph &graph); /// /// @ingroup ge /// @brief: Build single OP in Model. /// @param [in] op_desc: the OP description. /// @param [in] inputs: input tensors. /// @param [in] outputs: output tensors. /// @param [in] model_file_name: name of model file. /// @param [in] compile_flag: op build flag, accurate build is 0, fuzz build is 1 /// @return SUCCESS or FAILED /// Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, const std::string &model_file_name, int32_t compile_flag = 0); /// /// @ingroup ge /// @brief: Build single Op into model buff. /// @param [in] op_desc: the OP description. /// @param [in] inputs: input tensors. /// @param [in] outputs: output tensors. /// @param [in] engine_type: engine type. /// @param [in] compile_flag: op build flag, accurate build is 0, fuzz build is 1 /// @param [out] model_buff: model buff of op. /// @return SUCCESS or FAILED Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, OpEngineType engine_type, ModelBufferData &model_buff); Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, OpEngineType engine_type, int32_t compile_flag, ModelBufferData &model_buff); Status BuildSingleOpModel(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, OpEngineType engine_type, int32_t compile_flag, ModelBufferData &model_buff, GraphStage graph_stage, ComputeGraphPtr &compute_graph); /// /// @ingroup ge /// @brief: Build single Op into model buff. /// @param [in] op_desc: the OP description. /// @param [in] inputs: input tensors. /// @param [in] outputs: output tensors. /// @param [in] graph_name: graph name. /// @param [out] graph: graph of single op. /// @return SUCCESS or FAILED Status BuildSingleOpGraph(const OpDescPtr &op_desc, const InOutTensorRef &inputs_outputs, std::string graph_name, Graph &graph, std::vector> &inputs_name_type) const; Status BuildOriginalGraphInfo(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, const std::string &model_file_name, bool is_offline, GraphStage graph_stage, Graph &graph, ComputeGraphPtr &compute_graph, std::vector> &inputs_name_type); private: Status GenerateModel(const Graph &graph, const std::string &file_name_prefix, const std::vector &inputs, ge::ModelBufferData &model, bool is_offline = true); Status BuildSingleOp(OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs, const std::string &model_file_name, OpEngineType engine_type, ModelBufferData &model_buff, ComputeGraphPtr &comp_graph, bool is_offline = true, int32_t compile_flag = 0, GraphStage graph_stage = GraphStage::GRAPH_STAGE_RESERVED); bool CheckNoAicore(const ComputeGraphPtr &graph); void RemoveConst(const std::vector &inputs, std::vector &outputs) const; Status CheckForSingleOp(const OpDescPtr &op_desc, const std::vector &inputs, const std::vector &outputs) const; Status InferFormatForSingleOp(const OpDescPtr &op_desc, const Graph &graph) const; Status ResetAiCpuToDynamicShape(const ComputeGraphPtr &graph) const; using GeRootModelPtr = std::shared_ptr; Status SetModelNameForDump(const GeRootModelPtr &ge_root_model); Status CreateGeneralizedBuildAttrs(const GeRootModelPtr &ge_root_model, const std::vector &inputs, const std::vector &outputs, const std::vector> &inputs_name_type, std::vector &generalized_build_attrs) const; void AddExcludeEnginesOption(const OpDescPtr &op_desc, std::map &graph_options) const; void AddShapeGeneralizedOption(std::map &graph_options); void SetFuzzCompile(const std::vector &inputs, int32_t compile_flag); bool IsFuzzCompileEnable(); void ConvertOpInfosToOptions(const OpDescPtr &op_desc); class Impl; std::shared_ptr impl_; }; } // namespace ge #endif // INC_FRAMEWORK_GENERATOR_GE_GENERATOR_H_