<jupyter_start><jupyter_text>torchsnooperWe can use torchsnooper to print the grad and data information for every line in the function by using a decorator to modify the function.This package cannot print the .grad and .grad_fn, but can print the shape, dtype and device of the modified or created tensors in order to do better debugging.<jupyter_code>import torch
import torchsnooper
import snoop
torchsnooper.register_snoop()

@snoop
def myfunc(mask, x):
    y = torch.zeros(6)
    y.masked_scatter_(mask, x)
    return y

mask = torch.tensor([0, 1, 0, 1, 1, 0],dtype=torch.uint8)
source = torch.tensor([1.0, 2.0, 3.0])
y = myfunc(mask, source)
class Model(torch.nn.Module):

    def __init__(self):
        super().__init__()
        self.layer = torch.nn.Linear(2, 1)

    def forward(self, x):
        return self.layer(x)

    
x = torch.tensor([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
y = torch.tensor([3.0, 5.0, 4.0, 6.0])

model = Model()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

with torchsnooper.snoop():
    for _ in range(10):
        optimizer.zero_grad()
        pred = model(x)
        squared_diff = (y - pred) ** 2
        loss = squared_diff.mean()
        print(loss.item())
        loss.backward()
        optimizer.step()<jupyter_output>16.305837631225586
8.760947227478027
5.225284099578857
3.553119659423828
2.748087167739868
2.3474631309509277
2.136295795440674
2.0146775245666504
1.9361211061477661
1.8789491653442383
{'fraction_non_alphanumeric': 0.12670299727520437, 'fraction_numerical': 0.14305177111716622, 'mean_word_length': 3.5061349693251533, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '273514', 'license': 'MIT', 'n_tokens_mistral': 672, 'n_tokens_neox': 558, 'n_words': 141, 'path': 'debug_tools/.ipynb_checkpoints/use_torchsnooper-checkpoint.ipynb', 'repo_name': 'lwaekfjlk/Light-the-Torch', 'size': '1468'}