waleedka / hiddenlayer

Neural network graphs and training metrics for PyTorch, Tensorflow, and Keras.
MIT License
1.79k stars 266 forks source link

AttributeError: module 'torch.onnx' has no attribute '_optimize_trace' #100

Open Abermal opened 1 year ago

Abermal commented 1 year ago

The name of the error says it all. Probably a version error.

Installed hiddenlayer-0.2 from source. OS: Linux ubuntu 20 lts torch.version = '1.13.0+cu117'

papy-ganjha commented 1 year ago

Hello, You have to change the following : _optimize_trace to _optimize_graph in pytorch_builder.py line 71 I will if there is no pull request make one to fix this little bug Hope it helps!

janspiegel commented 1 year ago

Hello, I have made this change manually in pytorch_builder.py but now get the below

`---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in <cell line: 11>() 9 # see:https://github.com/waleedka/hiddenlayer/issues/100 10 # Jupyter Notebook renders it automatically ---> 11 hl.build_graph(model_resnet, torch.zeros([4,3,256,256]).to(device))

2 frames

/usr/local/lib/python3.10/dist-packages/hiddenlayer/pytorch_builder.py in (.0) 80 op = torch_node.kind() 81 # Parameters ---> 82 params = {k: torch_node[k] for k in torch_node.attributeNames()} 83 # Inputs/outputs 84 # TODO: inputs = [i.unique() for i in node.inputs()]

TypeError: 'torch._C.Node' object is not subscriptable`

code-acrobat commented 1 year ago

You need to hack more.

try:
  params = {k: torch_node[k] for k in torch_node.attributeNames()}
except Exception: 
  params = {}

If you follow various old tutorials with Jupyther notebooks you might need to change stuff like

hl.build_graph(model,torch.zeros([1]))

to

hl.build_graph(model,torch.zeros([1])).build_dot()

Hello, I have made this change manually in pytorch_builder.py but now get the below

`---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

in <cell line: 11>() 9 # see:#100 10 # Jupyter Notebook renders it automatically ---> 11 hl.build_graph(model_resnet, torch.zeros([4,3,256,256]).to(device))

2 frames

/usr/local/lib/python3.10/dist-packages/hiddenlayer/pytorch_builder.py in (.0) 80 op = torch_node.kind() 81 # Parameters ---> 82 params = {k: torch_node[k] for k in torch_node.attributeNames()} 83 # Inputs/outputs 84 # TODO: inputs = [i.unique() for i in node.inputs()]

TypeError: 'torch._C.Node' object is not subscriptable`

pikarpov commented 1 year ago

While that works, it also loses kernel size information from parameters to be included in the graph. Alternatively, you can follow the error and change __getitem__ to _node_get. To do that, include the following in the beginning of your pytorch_builder.py

# From https://github.com/pytorch/pytorch/blob/2efe4d809fdc94501fc38bf429e9a8d4205b51b6/torch/utils/tensorboard/_pytorch_graph.py#L384
def _node_get(node: torch._C.Node, key: str):
    """Gets attributes of a node which is polymorphic over return type."""
    sel = node.kindOf(key)
    return getattr(node, sel)(key)

torch._C.Node.__getitem__ = _node_get

Now, you should be able to extract from the nodes just fine, so there should be no issues with this line.

params = {k: torch_node[k] for k in torch_node.attributeNames()}

Credit: TypeError: 'torch._C.Node' object is not subscriptable

annahedstroem commented 7 months ago

+1 with the same problem

martin3398 commented 5 months ago

+1 I have the same problem

DonggeJia commented 1 month ago

While that works, it also loses kernel size information from parameters to be included in the graph. Alternatively, you can follow the error and change __getitem__ to _node_get. To do that, include the following in the beginning of your pytorch_builder.py

# From https://github.com/pytorch/pytorch/blob/2efe4d809fdc94501fc38bf429e9a8d4205b51b6/torch/utils/tensorboard/_pytorch_graph.py#L384
def _node_get(node: torch._C.Node, key: str):
    """Gets attributes of a node which is polymorphic over return type."""
    sel = node.kindOf(key)
    return getattr(node, sel)(key)

torch._C.Node.__getitem__ = _node_get

Now, you should be able to extract from the nodes just fine, so there should be no issues with this line.

params = {k: torch_node[k] for k in torch_node.attributeNames()}

Credit: TypeError: 'torch._C.Node' object is not subscriptable

still do not show data flow dimension