Skip to content

Commit

Permalink
Updated public server ndif.dev. Gradients feature guide
Browse files Browse the repository at this point in the history
  • Loading branch information
JadenFiotto-Kaufman committed Dec 20, 2023
1 parent ccaa081 commit c0abed6
Show file tree
Hide file tree
Showing 40 changed files with 619 additions and 151 deletions.
172 changes: 170 additions & 2 deletions public/_sources/notebooks/features/gradients.ipynb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,181 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Coming Soon!"
"There are a couple of ways we can interact with the gradients during and after a backward pass."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following example, we save the hidden states of the last layer and do a backward pass on the sum of the logits.\n",
"\n",
"Note two things:\n",
"\n",
"1. We use `inference=False` in the `.forward` call to turn off inference mode. This allows gradients to be calculated. \n",
"2. We can all `.backward()` on a value within the tracing context just like you normally would."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You're using a GPT2TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[[ 0.5216, -1.1755, -0.4617, ..., -1.1919, 0.0204, -2.0075],\n",
" [ 0.9841, 2.2175, 3.5851, ..., 0.5212, -2.2286, 5.7334]]],\n",
" device='cuda:0', grad_fn=<SliceBackward0>)\n"
]
}
],
"source": [
"from nnsight import LanguageModel\n",
"\n",
"model = LanguageModel('gpt2', device_map='cuda')\n",
"\n",
"with model.forward(inference=False) as runner:\n",
" with runner.invoke('Hello World') as invoker:\n",
"\n",
" hidden_states = model.transformer.h[-1].output[0].save()\n",
"\n",
" logits = model.lm_head.output\n",
"\n",
" logits.sum().backward()\n",
"\n",
"print(hidden_states.value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we wanted to see the gradients for the hidden_states, we can call `.retain_grad()` on it and access the `.grad` attribute after execution. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You're using a GPT2TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[[ 0.5216, -1.1755, -0.4617, ..., -1.1919, 0.0204, -2.0075],\n",
" [ 0.9841, 2.2175, 3.5851, ..., 0.5212, -2.2286, 5.7334]]],\n",
" device='cuda:0', grad_fn=<AsStridedBackward0>)\n",
"tensor([[[ 28.7976, -282.5977, 868.7343, ..., 120.1742, 52.2264,\n",
" 168.6447],\n",
" [ 79.4183, -253.6227, 1322.1290, ..., 208.3981, -19.5544,\n",
" 509.9856]]], device='cuda:0')\n"
]
}
],
"source": [
"from nnsight import LanguageModel\n",
"\n",
"model = LanguageModel('gpt2', device_map='cuda')\n",
"\n",
"with model.forward(inference=False) as runner:\n",
" with runner.invoke('Hello World') as invoker:\n",
"\n",
" hidden_states = model.transformer.h[-1].output[0].save()\n",
" hidden_states.retain_grad()\n",
"\n",
" logits = model.lm_head.output\n",
"\n",
" logits.sum().backward()\n",
"\n",
"print(hidden_states.value)\n",
"print(hidden_states.value.grad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Torch also provides hooks into the backward process via the inputs and outputs. NNsight uses these in a similar way as `.input` and `.output` by also providing `.backward_input` and `.backward_output`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You're using a GPT2TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[[ 0.5216, -1.1755, -0.4617, ..., -1.1919, 0.0204, -2.0075],\n",
" [ 0.9841, 2.2175, 3.5851, ..., 0.5212, -2.2286, 5.7334]]],\n",
" device='cuda:0', grad_fn=<SliceBackward0>)\n",
"tensor([[[ 28.7976, -282.5977, 868.7343, ..., 120.1742, 52.2264,\n",
" 168.6447],\n",
" [ 79.4183, -253.6227, 1322.1290, ..., 208.3981, -19.5544,\n",
" 509.9856]]], device='cuda:0')\n"
]
}
],
"source": [
"from nnsight import LanguageModel\n",
"\n",
"model = LanguageModel('gpt2', device_map='cuda')\n",
"\n",
"with model.forward(inference=False) as runner:\n",
" with runner.invoke('Hello World') as invoker:\n",
"\n",
" hidden_states = model.transformer.h[-1].output[0].save()\n",
" hidden_states_grad = model.transformer.h[-1].backward_output[0].save()\n",
" logits = model.lm_head.output\n",
"\n",
" logits.sum().backward()\n",
"\n",
"print(hidden_states.value)\n",
"print(hidden_states_grad.value)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ndif",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion public/_sources/notebooks/walkthrough.ipynb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2623,7 +2623,7 @@
"source": [
"# Next steps\n",
"\n",
"Check out the [Features](/tutorials/features/) and [Documentation](/documentation/) pages and the [README](https://github.com/JadenFiotto-Kaufman/nnsight/blob/main/README.md) for more guides."
"Check out the [Features](/features/) and [Documentation](/documentation/) pages and the [README](https://github.com/JadenFiotto-Kaufman/nnsight/blob/main/README.md) for more guides."
]
}
],
Expand Down
39 changes: 24 additions & 15 deletions public/_static/css/custom.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.button-group {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
align-content: stretch;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
align-content: stretch;
gap: 10px;
}

Expand Down Expand Up @@ -34,7 +34,7 @@ html[data-theme="light"] {
}


.features {
.features {
height: 60vh;
overflow: hidden;
}
Expand All @@ -44,7 +44,9 @@ html[data-theme="light"] {
margin-top: 50px;
}


img {
pointer-events: none;
}

.title-bot {
margin-bottom: -10px !important;
Expand All @@ -62,21 +64,28 @@ html[data-theme="light"] {
gap: 20px;
}

@media only screen and (max-width: 768px) { /* Adjust this value based on your breakpoint for mobile */
.front-container, .hero {
height: auto; /* Change from fixed height to auto */
min-height: 50vh; /* Adjust this as needed */
@media only screen and (max-width: 768px) {

/* Adjust this value based on your breakpoint for mobile */
.front-container,
.hero {
height: auto;
/* Change from fixed height to auto */
min-height: 50vh;
/* Adjust this as needed */
}

.features-container {
margin-bottom: 20px; /* Increase bottom margin */
margin-bottom: 20px;
/* Increase bottom margin */
}

.hero {
margin-bottom: 30px; /* Adjust the bottom margin of the main container */
margin-bottom: 30px;
/* Adjust the bottom margin of the main container */
}

.features {
height: 110vh;
}
}
}
6 changes: 3 additions & 3 deletions public/about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a746c00c" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css?v=0a3b3ea7" />
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=b030f2fb" />
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=969913ad" />

<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=c5ced968eda925caa686" />
Expand Down Expand Up @@ -247,7 +247,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down Expand Up @@ -394,7 +394,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down
6 changes: 3 additions & 3 deletions public/documentation/contexts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=a746c00c" />
<link rel="stylesheet" type="text/css" href="../../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css?v=0a3b3ea7" />
<link rel="stylesheet" type="text/css" href="../../_static/css/custom.css?v=b030f2fb" />
<link rel="stylesheet" type="text/css" href="../../_static/css/custom.css?v=969913ad" />

<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../../_static/scripts/bootstrap.js?digest=c5ced968eda925caa686" />
Expand Down Expand Up @@ -248,7 +248,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down Expand Up @@ -395,7 +395,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down
6 changes: 3 additions & 3 deletions public/documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a746c00c" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css?v=0a3b3ea7" />
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=b030f2fb" />
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=969913ad" />

<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=c5ced968eda925caa686" />
Expand Down Expand Up @@ -248,7 +248,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down Expand Up @@ -391,7 +391,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down
6 changes: 3 additions & 3 deletions public/documentation/intervention/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=a746c00c" />
<link rel="stylesheet" type="text/css" href="../../_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="../../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css?v=0a3b3ea7" />
<link rel="stylesheet" type="text/css" href="../../_static/css/custom.css?v=b030f2fb" />
<link rel="stylesheet" type="text/css" href="../../_static/css/custom.css?v=969913ad" />

<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../../_static/scripts/bootstrap.js?digest=c5ced968eda925caa686" />
Expand Down Expand Up @@ -248,7 +248,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down Expand Up @@ -395,7 +395,7 @@
<span><span>NDIF Status: </span><span class="ndif_status"></span></span>

<script>
fetch("https://ndif.baulab.us/ping")
fetch("https://ndif.dev/ping")
.then((response) => {
if (response.status == 200) {
Array.from(document.getElementsByClassName("ndif_status")).forEach((elm) => elm.style.backgroundColor = "green");
Expand Down
Loading

0 comments on commit c0abed6

Please sign in to comment.