johnmarktaylor91 / torchlens

Package for extracting and mapping the results of every single tensor operation in a PyTorch model in one line of code.
GNU General Public License v3.0
456 stars 16 forks source link

NotImplementedError #21

Open 91Abdullah opened 4 months ago

91Abdullah commented 4 months ago
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
File [~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1781](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1781), in ModelHistory.run_and_log_inputs_through_model(self, model, input_args, input_kwargs, layers_to_save, random_seed)
   [1780](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1780) for i, t in enumerate(input_tensors):
-> [1781](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1781)     self.log_source_tensor(t, "input", input_tensor_addresses[i])
   [1782](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1782) self.prepare_model(model, module_orig_forward_funcs, decorated_func_mapper)

File [~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1912](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1912), in ModelHistory.log_source_tensor(self, t, source, extra_address)
   [1911](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1911) if self.logging_mode == "exhaustive":
-> [1912](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1912)     self.log_source_tensor_exhaustive(t, source, extra_address)
   [1913](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1913) elif self.logging_mode == "fast":

File [~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:2000](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:2000), in ModelHistory.log_source_tensor_exhaustive(self, t, source, extra_addr)
   [1966](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1966)     raise ValueError("source must be either 'input' or 'buffer'")
   [1968](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1968) fields_dict = {
   [1969](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1969)     # General info:
   [1970](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1970)     "tensor_label_raw": tensor_label,
   [1971](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1971)     "layer_label_raw": tensor_label,
   [1972](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1972)     "realtime_tensor_num": realtime_tensor_num,
   [1973](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1973)     "operation_num": None,
   [1974](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1974)     "source_model_history": self,
   [1975](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1975)     "pass_finished": False,
   [1976](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1976)     # Label Info:
   [1977](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1977)     "layer_label": None,
   [1978](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1978)     "layer_label_short": None,
...
   [1492](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1492)     model, decorated_func_mapper=decorated_func_mapper, attribute_keyword="tl"
   [1493](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1493) )
   [1494](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/abasit/pytorch/FractAtlas/~/miniconda3/envs/pytorch/lib/python3.10/site-packages/torchlens/model_history.py:1494) self.undecorate_model_tensors(model)

KeyError: Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
johnmarktaylor91 commented 4 months ago

Thanks for posting--what's the model you're trying to look at? Will help me debug.

johnmarktaylor91 commented 4 months ago

Are you trying to run ModelHistory.run_and_log_inputs_through_model by itself? This is meant to be a function that's called internally, not called by the user. If you want to save activations for a new input, the function to use is ModelHistory.save_new_activations. I should've marked the former function as internal with an underscore so that's my mistake. Let me know if this answers your question.

91Abdullah commented 4 months ago

@johnmarktaylor91 the model I was trying to use was MobileNetV3. But IMO the problem is with the last layer. What I did was I removed the last layer of MobileNetV3Large and added a custom implemented QuantumLayer (using pennylane) and because of that it may not be working. I didn't test without the custom layer but I will try and let you know if it works.

johnmarktaylor91 commented 4 months ago

I was able to get this minimum example to work with a QuantumLayer, though the visualization is odd (no direct path from input to output). Unfortunately I'm not at all familiar with this kind of architecture and I would have to look into how PennyLane interfaces with PyTorch under the hood. I think what's happening is that there are some operations performed in the QuantumLayers that aren't being tracked by TorchLens for whatever reason--I will ask the PennyLane folks for some insight.

import torch
import pennylane as qml
import torchlens as tl
from os.path import join as opj

n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]

n_layers = 6
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.TorchLayer(qnode, weight_shapes)

clayer_1 = torch.nn.Linear(2, 2)
clayer_2 = torch.nn.Linear(2, 2)
softmax = torch.nn.Softmax(dim=1)
layers = [clayer_1, qlayer, clayer_2, softmax]
model = torch.nn.Sequential(*layers)

model_history = tl.log_forward_pass(model, torch.rand(1, 2), vis_opt='unrolled',
                                    vis_outpath=opj('/home/jtaylor/projects/torchlens/local_jmt/Debugging Scripts/qnn'))

qnn.pdf