1 /** 2 * Copyright 2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_PYTORCH_PYTORCH_NODE_PARSER_H_ 18 #define MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_PYTORCH_PYTORCH_NODE_PARSER_H_ 19 20 #include <string> 21 #include <vector> 22 #include <utility> 23 #include <algorithm> 24 #include "torch/script.h" 25 #include "include/errorcode.h" 26 #include "ir/dtype/type_id.h" 27 #include "ops/primitive_c.h" 28 #include "ops/op_name.h" 29 #include "src/common/log_adapter.h" 30 #include "tools/common/tensor_util.h" 31 #include "tools/converter/parser/parser_utils.h" 32 #include "nnacl/op_base.h" 33 34 namespace mindspore { 35 namespace lite { 36 class PytorchNodeParser { 37 public: PytorchNodeParser(std::string node_name)38 explicit PytorchNodeParser(std::string node_name) : name_(std::move(node_name)) {} 39 40 virtual ~PytorchNodeParser() = default; 41 Parse(const torch::jit::Node * torch_node,std::vector<size_t> * input_indices)42 virtual PrimitiveCPtr Parse(const torch::jit::Node *torch_node, std::vector<size_t> *input_indices) { 43 return nullptr; 44 } 45 46 static std::string GetTorchNodeType(const torch::jit::Node *torch_node); 47 48 static TypeId GetDataTypeFromTorch(const at::ScalarType torch_data_type); 49 50 template <typename T> GetValueFromConstNode(const torch::jit::Value * value_node)51 static T GetValueFromConstNode(const torch::jit::Value *value_node) { 52 T data{}; 53 auto ivalue = torch::jit::toIValue(value_node); 54 MS_CHECK_TRUE_RET(ivalue.has_value(), data); 55 auto value = ivalue.value(); 56 auto optional_value = value.toOptional<T>(); 57 MS_CHECK_TRUE_RET(optional_value, data); 58 return optional_value.value(); 59 } 60 61 protected: 62 const std::string name_{}; 63 }; 64 } // namespace lite 65 } // namespace mindspore 66 #endif // MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_PYTORCH_PYTORCH_NODE_PARSER_H_ 67