Skip to content

Commit

Permalink
fix device loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Jun 20, 2024
1 parent 8ccca3a commit 977f9c6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ch05/01_main-chapter-code/ch05.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,8 @@
"outputs": [],
"source": [
"model = GPTModel(GPT_CONFIG_124M)\n",
"model.load_state_dict(torch.load(\"model.pth\"))\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"model.load_state_dict(torch.load(\"model.pth\", map_location=device))\n",
"model.eval();"
]
},
Expand Down
3 changes: 2 additions & 1 deletion ch05/01_main-chapter-code/exercise-solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@
"checkpoint = torch.load(\"model_and_optimizer.pth\")\n",
"model = GPTModel(GPT_CONFIG_124M)\n",
"model.load_state_dict(checkpoint[\"model_state_dict\"])\n",
"model.to(device)\n",
"\n",
"optimizer = torch.optim.AdamW(model.parameters(), lr=0.0004, weight_decay=0.1)\n",
"optimizer.load_state_dict(checkpoint[\"optimizer_state_dict\"])\n",
Expand Down Expand Up @@ -958,7 +959,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.10.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 977f9c6

Please sign in to comment.