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 layer names #432

Merged
merged 3 commits into from
Oct 19, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

### Added

- Added names for TensorRT layers
- Replaced Tensor.ndim references with len(tensor.shape) to support older pytorch versions
- Added reduced precision documentation page
38 changes: 36 additions & 2 deletions torch2trt/torch2trt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import copy
import numpy as np
import io
from collections import defaultdict

from .calibration import (
TensorBatchDataset,
Expand Down Expand Up @@ -326,10 +327,43 @@ def default_input_names(num_inputs):

def default_output_names(num_outputs):
return ["output_%d" % i for i in range(num_outputs)]



class LayerNamingNetworkWrapper(object):
def __init__(self, ctx, network):
self._ctx = ctx
self._network = network
self._layer_counts = defaultdict(lambda: 0)

def _set_layer_name(self, layer):
def arg_str(arg):
if isinstance(arg, torch.Tensor):
return "tensor(shape=%s, dtype=%s)" % (str(list(arg.shape)), str(arg.dtype))
return str(arg)

self._layer_counts[layer.type.name] += 1
args = [arg_str(arg) for arg in self._ctx.method_args]
kwargs = ["%s=%s" % (key, arg_str(arg)) for key, arg in self._ctx.method_kwargs.items()]
layer.name = "[%s #%d] %s(%s)" % (layer.type.name, self._layer_counts[layer.type.name],
self._ctx.method_str, ", ".join(args + kwargs))

def __getattr__(self, name):
attr = getattr(self._network, name)
if callable(attr):
def wrapper(*args, **kwargs):
ret = attr(*args, **kwargs)
if isinstance(ret, trt.ILayer):
self._set_layer_name(ret)
return ret

return wrapper
else:
return attr


class ConversionContext(object):
def __init__(self, network, converters=CONVERTERS):
self.network = network
self.network = LayerNamingNetworkWrapper(self, network)
self.lock = False
self.method_args = None
self.method_kwargs = None
Expand Down