-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tweak documentation and to_frame() column naming (#3)
- Loading branch information
1 parent
a0f9283
commit 8393f26
Showing
9 changed files
with
420 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ __pycache__ | |
|
||
/build | ||
/dist | ||
/doc | ||
/doc/tensor_tracker | ||
/local | ||
|
||
*.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Copyright (c) 2023 Graphcore Ltd. All rights reserved.\n", | ||
"\n", | ||
"# Usage example\n", | ||
"\n", | ||
"General setup:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"from torch import nn, Tensor\n", | ||
"\n", | ||
"class Model(nn.Module):\n", | ||
" def __init__(self):\n", | ||
" super().__init__()\n", | ||
" self.embed = nn.Embedding(10, 4)\n", | ||
" self.project = nn.Linear(4, 4)\n", | ||
" self.unembed = nn.Linear(4, 10)\n", | ||
"\n", | ||
" def forward(self, tokens: Tensor) -> Tensor:\n", | ||
" logits = self.unembed(self.project(self.embed(tokens)))\n", | ||
" return nn.functional.cross_entropy(logits, tokens)\n", | ||
"\n", | ||
"torch.manual_seed(100)\n", | ||
"module = Model()\n", | ||
"inputs = torch.randint(0, 10, (3,))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Using `tensor_tracker`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[Stash(name='embed', type=<class 'torch.nn.modules.sparse.Embedding'>, grad=False, value=tensor([[ 0.4698, 1.2426, 0.5403, -1.1454],\n", | ||
" [-0.8425, -0.6475, -0.2189, -1.1326],\n", | ||
" [ 0.1268, 1.3564, 0.5632, -0.1039]])), Stash(name='project', type=<class 'torch.nn.modules.linear.Linear'>, grad=False, value=tensor([[-0.6237, -0.1652, 0.3782, -0.8841],\n", | ||
" [-0.9278, -0.2848, -0.8688, -0.4719],\n", | ||
" [-0.3449, 0.3643, 0.3935, -0.6302]])), Stash(name='unembed', type=<class 'torch.nn.modules.linear.Linear'>, grad=False, value=tensor([[-0.2458, 1.0003, -0.8231, -0.1405, -0.2964, 0.5837, 0.2889, 0.2059,\n", | ||
" -0.6114, -0.5916],\n", | ||
" [-0.6345, 1.0882, -0.4304, -0.2196, -0.0426, 0.9428, 0.2051, 0.5897,\n", | ||
" -0.2217, -0.9132],\n", | ||
" [-0.0822, 0.9985, -0.7097, -0.3139, -0.4805, 0.6878, 0.2560, 0.3254,\n", | ||
" -0.4447, -0.3332]])), Stash(name='', type=<class '__main__.Model'>, grad=False, value=tensor(2.5663)), Stash(name='', type=<class '__main__.Model'>, grad=True, value=(tensor(1.),)), Stash(name='unembed', type=<class 'torch.nn.modules.linear.Linear'>, grad=True, value=(tensor([[ 0.0237, 0.0824, -0.3200, 0.0263, 0.0225, 0.0543, 0.0404, 0.0372,\n", | ||
" 0.0164, 0.0168],\n", | ||
" [ 0.0139, 0.0779, 0.0171, 0.0211, 0.0251, 0.0673, 0.0322, -0.2860,\n", | ||
" 0.0210, 0.0105],\n", | ||
" [-0.3066, 0.0787, 0.0143, 0.0212, 0.0179, 0.0577, 0.0374, 0.0401,\n", | ||
" 0.0186, 0.0208]]),)), Stash(name='project', type=<class 'torch.nn.modules.linear.Linear'>, grad=True, value=(tensor([[-0.1755, 0.1306, 0.0443, -0.1823],\n", | ||
" [ 0.1202, -0.0728, 0.0066, -0.0839],\n", | ||
" [-0.1863, 0.0470, -0.1055, -0.0353]]),)), Stash(name='embed', type=<class 'torch.nn.modules.sparse.Embedding'>, grad=True, value=(tensor([[-0.0108, 0.1086, -0.1304, -0.0370],\n", | ||
" [ 0.0534, -0.0029, 0.0078, -0.0074],\n", | ||
" [-0.0829, 0.0152, -0.1170, -0.0625]]),))]\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>name</th>\n", | ||
" <th>type</th>\n", | ||
" <th>grad</th>\n", | ||
" <th>std</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>embed</td>\n", | ||
" <td>torch.nn.modules.sparse.Embedding</td>\n", | ||
" <td>False</td>\n", | ||
" <td>0.853265</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>project</td>\n", | ||
" <td>torch.nn.modules.linear.Linear</td>\n", | ||
" <td>False</td>\n", | ||
" <td>0.494231</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>unembed</td>\n", | ||
" <td>torch.nn.modules.linear.Linear</td>\n", | ||
" <td>False</td>\n", | ||
" <td>0.581503</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td></td>\n", | ||
" <td>__main__.Model</td>\n", | ||
" <td>False</td>\n", | ||
" <td>NaN</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td></td>\n", | ||
" <td>__main__.Model</td>\n", | ||
" <td>True</td>\n", | ||
" <td>NaN</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>5</th>\n", | ||
" <td>unembed</td>\n", | ||
" <td>torch.nn.modules.linear.Linear</td>\n", | ||
" <td>True</td>\n", | ||
" <td>0.105266</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>6</th>\n", | ||
" <td>project</td>\n", | ||
" <td>torch.nn.modules.linear.Linear</td>\n", | ||
" <td>True</td>\n", | ||
" <td>0.112392</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>7</th>\n", | ||
" <td>embed</td>\n", | ||
" <td>torch.nn.modules.sparse.Embedding</td>\n", | ||
" <td>True</td>\n", | ||
" <td>0.068816</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" name type grad std\n", | ||
"0 embed torch.nn.modules.sparse.Embedding False 0.853265\n", | ||
"1 project torch.nn.modules.linear.Linear False 0.494231\n", | ||
"2 unembed torch.nn.modules.linear.Linear False 0.581503\n", | ||
"3 __main__.Model False NaN\n", | ||
"4 __main__.Model True NaN\n", | ||
"5 unembed torch.nn.modules.linear.Linear True 0.105266\n", | ||
"6 project torch.nn.modules.linear.Linear True 0.112392\n", | ||
"7 embed torch.nn.modules.sparse.Embedding True 0.068816" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"import tensor_tracker\n", | ||
"\n", | ||
"with tensor_tracker.track(module) as tracker:\n", | ||
" module(inputs).backward()\n", | ||
"\n", | ||
"print(list(tracker))\n", | ||
"# => [Stash(name=\"embed\", type=nn.Embedding, grad=False, value=tensor(...)),\n", | ||
"# ...]\n", | ||
"\n", | ||
"display(tracker.to_frame())" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.