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

Fix version-converter to generate valid identifiers #5628

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions onnx/common/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@

namespace ONNX_NAMESPACE {

namespace { // internal/private API

Check warning on line 42 in onnx/common/ir.h

View workflow job for this annotation

GitHub Actions / Optional Lint

[cpplint] reported by reviewdog 🐶 At least two spaces is best between code and comments [whitespace/comments] [2] Raw Output: onnx/common/ir.h:42: At least two spaces is best between code and comments [whitespace/comments] [2]

Check warning on line 42 in onnx/common/ir.h

View workflow job for this annotation

GitHub Actions / Optional Lint

[cpplint] reported by reviewdog 🐶 Do not use unnamed namespaces in header files. See https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces for more information. [build/namespaces_headers] [4] Raw Output: onnx/common/ir.h:42: Do not use unnamed namespaces in header files. See https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces for more information. [build/namespaces_headers] [4]

std::string toVarName(size_t i) {
std::ostringstream oss;
oss << "_v_" << i;
gramalingam marked this conversation as resolved.
Show resolved Hide resolved
return oss.str();
}

} // namespace

Check warning on line 50 in onnx/common/ir.h

View workflow job for this annotation

GitHub Actions / Optional Lint

[cpplint] reported by reviewdog 🐶 At least two spaces is best between code and comments [whitespace/comments] [2] Raw Output: onnx/common/ir.h:50: At least two spaces is best between code and comments [whitespace/comments] [2]

// Graph represents one "function" of computation.
// It uses a simple ownership model where the graph owns all the nodes inside it.
// All references inside the graph are raw pointers.
Expand Down Expand Up @@ -346,7 +356,7 @@
std::string uniqueName() const {
if (has_unique_name())
return unique_name_;
return ONNX_NAMESPACE::to_string(unique());
return toVarName(unique());
}
Value* setUniqueName(const std::string& name, bool rename_subgraph_captured_nodes = true);
Value* setStage(size_t s) {
Expand Down Expand Up @@ -1034,9 +1044,9 @@
}

size_t getNextUnique() {
std::string next_unique_name = ONNX_NAMESPACE::to_string(++next_unique_);
std::string next_unique_name = toVarName(++next_unique_);
while (!isNameUnique(next_unique_name)) {
next_unique_name = ONNX_NAMESPACE::to_string(++next_unique_);
next_unique_name = toVarName(++next_unique_);
}
return next_unique_;
}
Expand Down Expand Up @@ -1311,7 +1321,7 @@
newValue->setUniqueName(unique_name);
// The "unique" semantic of unique_name should be kept or uses()
// will return an incorrect result when the value is used in subgraph
this->setUniqueName(ONNX_NAMESPACE::to_string(graph->getNextUnique()), false);
this->setUniqueName(toVarName(graph->getNextUnique()), false);
}
newValue->uses_in_current_graph_.reserve(this->uses_in_current_graph_.size());
for (auto u : uses_in_current_graph_) {
Expand Down
60 changes: 60 additions & 0 deletions onnx/test/cpp/ir_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) ONNX Project Contributors
Fixed Show fixed Hide fixed

/*
* SPDX-License-Identifier: Apache-2.0
*/

#include <iostream>

#include "gtest/gtest.h"

Check warning on line 9 in onnx/test/cpp/ir_test.cc

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: 'gtest/gtest.h' file not found [clang-diagnostic-error] ```cpp #include "gtest/gtest.h" ^ ```
#include "onnx/common/ir.h"
#include "onnx/common/ir_pb_converter.h"
#include "onnx/defs/printer.h"

namespace ONNX_NAMESPACE {
namespace Test {

static bool IsValidIdentifier(const std::string& name) {
if (name.empty()) {
return false;
}
if (!isalpha(name[0]) && name[0] != '_') {
return false;
}
for (size_t i = 1; i < name.size(); ++i) {
if (!isalnum(name[i]) && name[i] != '_') {
return false;
}
}
return true;
}

TEST(IR, ValidIdentifierTest) {
Graph* g = new Graph();
g->setName("test");
Value* x = g->addInput();
x->setUniqueName("x");
x->setElemType(ONNX_NAMESPACE::TensorProto_DataType_FLOAT);
x->setSizes({Dimension("M"), Dimension("N")});
Node* node1 = g->create(kNeg, 1);
node1->addInput(x);
g->appendNode(node1);
Value* temp1 = node1->outputs()[0];
Node* node2 = g->create(kNeg, 1);
node2->addInput(temp1);
g->appendNode(node2);
Value* y = node2->outputs()[0];
g->registerOutput(y);

ModelProto model;
ExportModelProto(&model, std::shared_ptr<Graph>(g));

for (auto& node : model.graph().node()) {
for (auto& name : node.output()) {
EXPECT_TRUE(IsValidIdentifier(name));
}
}
}

} // namespace Test

Check warning on line 59 in onnx/test/cpp/ir_test.cc

View workflow job for this annotation

GitHub Actions / Optional Lint

[cpplint] reported by reviewdog 🐶 At least two spaces is best between code and comments [whitespace/comments] [2] Raw Output: onnx/test/cpp/ir_test.cc:59: At least two spaces is best between code and comments [whitespace/comments] [2]
} // namespace ONNX_NAMESPACE

Check warning on line 60 in onnx/test/cpp/ir_test.cc

View workflow job for this annotation

GitHub Actions / Optional Lint

[cpplint] reported by reviewdog 🐶 At least two spaces is best between code and comments [whitespace/comments] [2] Raw Output: onnx/test/cpp/ir_test.cc:60: At least two spaces is best between code and comments [whitespace/comments] [2]