Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/notebooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
pip install .[dev]
pip install ipykernel nbval
pip install -r docs/requirements.txt
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip freeze
- name: Register Python3 kernel
run: |
Expand All @@ -46,4 +47,4 @@ jobs:
env:
AI_STACK_TEST_MODE: true
run: |
pytest --tb=short --nbval docs/source/ --ignore=docs/source/data_loaders_on_cpu_with_jax.ipynb --ignore=docs/source/data_loaders_on_gpu_with_jax.ipynb --ignore=docs/source/JAX_for_PyTorch_users.ipynb --ignore=docs/source/JAX_porting_PyTorch_model.ipynb
pytest --tb=short --nbval docs/source/
159 changes: 125 additions & 34 deletions docs/source/JAX_for_PyTorch_users.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "f_hNMIu0sL5l",
"outputId": "838ea182-2a22-4e9a-b3bc-3d91506e9ecf"
"outputId": "838ea182-2a22-4e9a-b3bc-3d91506e9ecf",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -182,7 +185,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "PQhflPwAImj1",
"outputId": "a75045a7-3a97-49d0-a215-42426f4e0ff4"
"outputId": "a75045a7-3a97-49d0-a215-42426f4e0ff4",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -260,13 +266,25 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iWb6OdUpJvE0",
"outputId": "7483461a-fb29-4e7f-d892-231397419834"
"outputId": "7483461a-fb29-4e7f-d892-231397419834",
"tags": [
"nbval-skip"
]
},
"outputs": [
{
Expand All @@ -283,8 +301,6 @@
}
],
"source": [
"import torch\n",
"\n",
"x_torch = torch.rand(3, 4)\n",
"\n",
"# Create JAX Array as a copy of x_torch tensor\n",
Expand Down Expand Up @@ -317,7 +333,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "4AwolCS1OHw2",
"outputId": "3defd0bb-4548-4afa-fe9c-3e84b446df24"
"outputId": "3defd0bb-4548-4afa-fe9c-3e84b446df24",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -357,7 +376,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "cDVk9NjNKqJ2",
"outputId": "3769b2fe-c241-447a-a9d9-c622b568e963"
"outputId": "3769b2fe-c241-447a-a9d9-c622b568e963",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -417,7 +439,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "s0k84UEMQSwL",
"outputId": "71b3658b-45ee-4fe1-a972-986f2e0da950"
"outputId": "71b3658b-45ee-4fe1-a972-986f2e0da950",
"tags": [
"nbval-skip"
]
},
"outputs": [
{
Expand Down Expand Up @@ -454,7 +479,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "9ZSyG8Q6Q5Jw",
"outputId": "68da1b2b-6803-4382-c851-b1c1062c60dd"
"outputId": "68da1b2b-6803-4382-c851-b1c1062c60dd",
"tags": [
"nbval-skip"
]
},
"outputs": [
{
Expand Down Expand Up @@ -485,7 +513,11 @@
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -514,7 +546,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [],
"source": [
"try:\n",
Expand All @@ -533,7 +569,11 @@
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -774,7 +814,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "hzm2u1MpYmVN",
"outputId": "a55ae511-45b3-49ab-d7cf-816448b1b0e3"
"outputId": "a55ae511-45b3-49ab-d7cf-816448b1b0e3",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -834,7 +877,10 @@
"base_uri": "https://localhost:8080/"
},
"id": "glqqg02VvP-W",
"outputId": "549934e4-823b-48e1-c7f0-04ecd2b6c0d5"
"outputId": "549934e4-823b-48e1-c7f0-04ecd2b6c0d5",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand All @@ -860,7 +906,11 @@
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"metadata": {
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -916,7 +966,10 @@
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "tiTLDUvjvO_s"
"id": "tiTLDUvjvO_s",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -1059,7 +1112,10 @@
"cell_type": "code",
"execution_count": 26,
"metadata": {
"id": "-dRZPUx-vLBk"
"id": "-dRZPUx-vLBk",
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
Expand Down Expand Up @@ -1146,16 +1202,12 @@
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Files already downloaded and verified\n"
]
}
],
"metadata": {
"tags": [
"nbval-ignore-output"
]
},
"outputs": [],
"source": [
"# CIFAR10 training/testing datasets setup\n",
"import numpy as np\n",
Expand Down Expand Up @@ -1222,7 +1274,11 @@
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"metadata": {
"tags": [
"nbval-ignore-output"
]
},
"outputs": [
{
"name": "stderr",
Expand Down Expand Up @@ -1272,7 +1328,7 @@
"learning_rate = 0.005\n",
"momentum = 0.9\n",
"\n",
"optimizer = nnx.ModelAndOptimizer(model, optax.adamw(learning_rate, momentum))"
"optimizer = nnx.Optimizer(model, optax.adamw(learning_rate, momentum), wrt=nnx.Param)"
]
},
{
Expand Down Expand Up @@ -1300,7 +1356,7 @@
"\n",
" metrics.update(loss=loss, logits=logits, labels=y_true) # In-place updates.\n",
"\n",
" optimizer.update(grads) # In-place updates.\n",
" optimizer.update(model, grads) # In-place updates.\n",
" return loss\n",
"\n",
"\n",
Expand Down Expand Up @@ -1344,7 +1400,11 @@
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -1451,6 +1511,29 @@
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"# @title [hidden cell; used for testing]\n",
"# This cell is run only in the JAX AI Stack's CI testing and should otherwise be ignored.\n",
"import os\n",
"AI_STACK_TEST_MODE = os.getenv('AI_STACK_TEST_MODE') == 'true'\n",
"if AI_STACK_TEST_MODE:\n",
" model.train()\n",
" batch = next(iter(train_loader))\n",
" loss = train_step(model, optimizer, metrics, batch)\n",
" model.eval()\n",
" test_batch = next(iter(test_loader))\n",
" eval_step(model, metrics, test_batch)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1504,7 +1587,11 @@
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -1533,7 +1620,11 @@
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [
{
"name": "stdout",
Expand Down
Loading