Skip to content

fix duplicate names bug in ImporterContext#467

Merged
kevinch-nv merged 1 commit intoonnx:masterfrom
tdp2110:master
Jul 10, 2020
Merged

fix duplicate names bug in ImporterContext#467
kevinch-nv merged 1 commit intoonnx:masterfrom
tdp2110:master

Conversation

@tdp2110
Copy link
Copy Markdown
Contributor

@tdp2110 tdp2110 commented May 28, 2020

Signed-off-by: Tom Peters thomas.d.peters@gmail.com

This is the fix I described from #466

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 28, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor Author

@tdp2110 tdp2110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if we have N layers with the same name, we'll have to do O(N^2) lookups in the set. This could be avoided at the cost of additional memory by maintaining per-basname counters (similar to original implementation, but a loop of lookups into the used names set would need to be added). If you didn't want to maintain basenames in the case of count one layer names, you could keep the original maps but change lines like

uniqueName = mLayerNameCounts[name] ? (name + "_" + std::to_string(mLayerNameCounts[name])) : name;

to always append a counter, eg

uniqueName = name + "_" + std::to_string(mLayerNameCounts[name]);

I don't think that approach would require keeping track of a set of used names. I don't know what's appropriate for this library.

Comment thread ImporterContext.hpp Outdated
mLayerNameCounts; // Keep track of how many times a tensor name shows up, to avoid duplicate naming in TRT.
std::set<std::string>
mTensorNames; // keep track of tensor names used so far, to avoid
// duplicate avoid duplicate naming in TRT.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. I duplicated duplicate!

@tdp2110
Copy link
Copy Markdown
Contributor Author

tdp2110 commented Jul 7, 2020

Is it worth resolving conflicts on this PR or will this just be ignored?

@rmccorm4
Copy link
Copy Markdown

rmccorm4 commented Jul 7, 2020

CC @kevinch-nv

@tdp2110
Copy link
Copy Markdown
Contributor Author

tdp2110 commented Jul 8, 2020

FYI LLVM has to solve a similar problem. For example, see https://llvm.org/docs/tutorial/OCamlLangImpl3.html

One nice thing about LLVM is that the name is just a hint. For instance, if the code above emits multiple “addtmp” variables, LLVM will automatically provide each one with an increasing, unique numeric suffix.

You can see their implementation of value naming here, which in the case of name conflicts dispatches here. The basic pattern of the uniquification matches what's done here: there's a map of all used names, and they loop with increasing integer suffixes until something isn't in the map. The particular implementation of the map is not a std::set as here, but the idea is the same.

Comment thread ImporterContext.hpp Outdated
}

private:
static std::string freshName(std::set<std::string> &namesSet,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer a more verbose function name, such as generateUniqueName

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 done

Suppose we have a network with (not all distinct) layer names

layer
layer_1
layer

When ImporterContext sees "layer", it sees it's not in mLayerNameCounts,
and sets mLayerNameCounts["layer"] = 1 and adds a TRT layer with name "layer".
It then sees "layer_1", concludes it's not in mLayerNameCounts, so it sets
mLayerNameCounts["layer_1"] = 1 and adds a TRT layer with name "layer_1".
NOW when it sees "layer", it sees that mLayerNameCounts["layer"] == 1,
so we produce a "uniqueName" of
"layer" + "_" + std::to_string(mLayerNameCounts["layer"] ), ie "layer_1",
which is a name conflict for the TRT net.

This change keeps track of all inserted names in a set and in the case of
duplicates, tries suffix-appended modifications of the duplicated name
by ever increasing integers until a name appears which has not been used.
@tdp2110
Copy link
Copy Markdown
Contributor Author

tdp2110 commented Jul 9, 2020

I changed the impl since I last pushed this: now the ImporterContext holds a single integer suffix counter. In the case of repeated names in an onnx graph, you won't get the same n^2 search for suffixes anymore.

Also, I realize clang-format may have done something weird with the comments in this file. I used the .clang-format from TensorRT (there's no .clang-format in this repo). I tried multiple versions of clang-format (7,8,9) and they all did the comment-wrapping like this (sometimes clang-format version matters!). If this is a problem, I can manually edit these lines.

@kevinch-nv kevinch-nv merged commit 1e591b9 into onnx:master Jul 10, 2020
kevinch-nv added a commit that referenced this pull request Oct 20, 2020
* Prefix logging messages with [TRT] (#497)

* Add local build instructions (#498)

* fix duplicate layer names bug (#446) (#467)

Suppose we have a network with (not all distinct) layer names

layer
layer_1
layer

When ImporterContext sees "layer", it sees it's not in mLayerNameCounts,
and sets mLayerNameCounts["layer"] = 1 and adds a TRT layer with name "layer".
It then sees "layer_1", concludes it's not in mLayerNameCounts, so it sets
mLayerNameCounts["layer_1"] = 1 and adds a TRT layer with name "layer_1".
NOW when it sees "layer", it sees that mLayerNameCounts["layer"] == 1,
so we produce a "uniqueName" of
"layer" + "_" + std::to_string(mLayerNameCounts["layer"] ), ie "layer_1",
which is a name conflict for the TRT net.

This change keeps track of all inserted names in a set and in the case of
duplicates, tries suffix-appended modifications of the duplicated name
by ever increasing integers until a name appears which has not been used.

* Support Dynamic and 3D instanceNormalization (#515)

* Add restrictions on multi-input convolutions (#521)

* Update resize limitations in TensorRT (#538)

Signed-off-by: Kevin Chen <kevinch@nvidia.com>

Co-authored-by: Thomas Peters <thomas.d.peters@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants