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

Use is_constant_initializer #83

Merged
merged 2 commits into from
Jul 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions onnxoptimizer/passes/eliminate_nop_expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ struct EliminateNopExpand final : public PredicateBasedPass {
return "eliminate_nop_expand";
}

bool isConstantTensor(Graph& graph, const std::string& name) {
const auto& initializer_names = graph.initializer_names();
return std::find(initializer_names.cbegin(), initializer_names.cend(),
name) != initializer_names.cend();
}

bool isABroadcastToB(const std::vector<int64_t>& dims_a,
const std::vector<Dimension>& dims_b) {
int ndim_a = dims_a.size();
Expand Down Expand Up @@ -59,10 +53,11 @@ struct EliminateNopExpand final : public PredicateBasedPass {
bool runTransform(Node* node, Graph& graph,
NodeDestroyType& destroy_current) override {
auto& input_value = node->inputs()[0];
const auto shape_tensor_name = node->inputs()[1]->uniqueName();
const auto* shape_value = node->input(1);
const auto shape_tensor_name = shape_value->uniqueName();
const auto shape_tensor = graph.getInitializer(shape_tensor_name);

if (!isConstantTensor(graph, shape_tensor_name) ||
if (!graph.is_constant_initializer(shape_value) ||
!isABroadcastToB(ParseData<int64_t>(&*shape_tensor),
input_value->sizes()) ||
!tryReplacingAllUsesWith(node->output(), input_value)) {
Expand Down
5 changes: 1 addition & 4 deletions onnxoptimizer/passes/eliminate_nop_reshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ struct EliminateNopReshape final : public PredicateBasedPass {
NodeDestroyType &destroy_current) override {
const auto &data_input_dims = node->inputs()[0]->sizes();
const auto *shape_input = node->inputs()[1];
const auto &initializer_names = graph.initializer_names();

const Tensor *shape_tensor = nullptr;
if (shape_input->node()->kind() == kConstant) {
shape_tensor = &shape_input->node()->t(kvalue);
} else if (shape_input->node()->kind() == kParam &&
std::find(initializer_names.cbegin(), initializer_names.cend(),
shape_input->uniqueName()) != initializer_names.cend()) {
} else if (graph.is_constant_initializer(shape_input)) {
shape_tensor = &*graph.getInitializer(shape_input->uniqueName());
} else {
return false;
Expand Down
5 changes: 1 addition & 4 deletions onnxoptimizer/passes/eliminate_shape_gather.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ struct EliminateShapeGather final : public PredicateBasedPass {
NodeDestroyType &destroy_current) override {
auto *x = node->inputs()[0];
auto *indices = node->inputs()[1];
const auto &initializer_names = graph.initializer_names();
Node *shape = x->node();
const auto &dims = shape->input()->sizes();

const Tensor *indices_tensor = nullptr;
if (indices->node()->kind() == kConstant) {
indices_tensor = &indices->node()->t(kvalue);
} else if (indices->node()->kind() == kParam &&
std::find(initializer_names.cbegin(), initializer_names.cend(),
indices->uniqueName()) != initializer_names.cend()) {
} else if (graph.is_constant_initializer(indices)) {
indices_tensor = &*graph.getInitializer(indices->uniqueName());
} else {
return false;
Expand Down
5 changes: 1 addition & 4 deletions onnxoptimizer/passes/fuse_concat_and_reshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ struct FuseConcatAndReshape final : public PredicateBasedPass {
NodeDestroyType &destroy_current) override {
auto *shape_value = node->inputs()[1];
auto *concat = shape_value->node();
const auto &initializer_names = graph.initializer_names();

std::vector<int64_t> shapes;
for (const auto *v : concat->inputs()) {
const uint32_t kind = v->node()->kind();
const Tensor *tensor = nullptr;
if (kind == kConstant) {
tensor = &v->node()->t(kvalue);
} else if (kind == kParam &&
std::find(initializer_names.cbegin(), initializer_names.cend(),
v->uniqueName()) != initializer_names.cend()) {
} else if (graph.is_constant_initializer(v)) {
tensor = &*graph.getInitializer(v->uniqueName());
} else {
if (v->sizes().size() != 1) {
Expand Down