Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for StringType const node conversion in PyTorch model converter #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ STATUS PytorchModelParser::ConvertConstNode(const torch::jit::Node *torch_node,
auto data = static_cast<float>(value.value().toDouble());
parameter = opt::BuildFloatValueParameterNode(anf_graph, data, output->debugName());
} break;
case c10::TypeKind::StringType: {
auto data = static_cast<std::string>(value.value().toStringRef());
parameter = opt::BuildStringValueParameterNode(anf_graph, data, output->debugName());
} break;
case c10::TypeKind::ListType: {
auto element_type = value->toList().elementType()->kind();
switch (element_type) {
Expand Down
21 changes: 21 additions & 0 deletions mindspore/lite/tools/optimizer/common/gllo_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,27 @@ ParameterPtr BuildIntVec2DParameterNode(const FuncGraphPtr &func_graph, const st
return param_node;
}

ParameterPtr BuildStringValueParameterNode(const FuncGraphPtr &func_graph, const std::string &data,
const std::string &node_name, bool empty_shape) {
MS_CHECK_TRUE_RET(func_graph != nullptr, nullptr);
auto param_node = func_graph->add_parameter();
MS_CHECK_TRUE_RET(param_node != nullptr, nullptr);
param_node->set_name(node_name);

std::vector<int64_t> shape_vector{static_cast<int64_t>(data.size())};
auto tensor_info = lite::CreateTensorInfo(data.c_str(), data.size(), shape_vector, kObjectTypeString);
if (tensor_info == nullptr) {
MS_LOG(ERROR) << "Create tensor info failed";
return nullptr;
}
auto status = lite::InitParameterFromTensorInfo(param_node, tensor_info);
if (status != RET_OK) {
MS_LOG(ERROR) << "init parameter from tensor info failed";
return nullptr;
}
return param_node;
}

ParameterPtr BuildFloatValueParameterNode(const FuncGraphPtr &func_graph, const float &data,
const std::string &node_name, bool empty_shape) {
MS_CHECK_TRUE_RET(func_graph != nullptr, nullptr);
Expand Down
3 changes: 3 additions & 0 deletions mindspore/lite/tools/optimizer/common/gllo_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ ParameterPtr BuildIntVecParameterNode(const FuncGraphPtr &func_graph, const std:
ParameterPtr BuildIntVec2DParameterNode(const FuncGraphPtr &func_graph, const std::vector<std::vector<int32_t>> &data,
const std::string &node_name);

ParameterPtr BuildStringValueParameterNode(const FuncGraphPtr &func_graph, const std::string &data,
const std::string &node_name, bool empty_shape = false);

ParameterPtr BuildFloatValueParameterNode(const FuncGraphPtr &func_graph, const float &data,
const std::string &node_name, bool empty_shape = false);

Expand Down