Skip to content

Commit

Permalink
refresh Jupyter Notebook examples (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer authored Feb 26, 2024
1 parent e711753 commit b449dd7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
47 changes: 36 additions & 11 deletions 11_notebooks/basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install modal-client\n",
"%pip install --upgrade modal\n",
"%pip install ipywidgets"
]
},
Expand All @@ -18,7 +18,8 @@
"source": [
"import modal\n",
"\n",
"assert modal.__version__ > \"0.49.0\""
"assert modal.__version__ > \"0.49.0\"\n",
"modal.__version__"
]
},
{
Expand All @@ -29,7 +30,7 @@
"source": [
"import modal\n",
"\n",
"stub = modal.Stub(name=\"example-basic-notebook\")"
"stub = modal.Stub(name=\"example-basic-notebook-app\")"
]
},
{
Expand Down Expand Up @@ -65,10 +66,7 @@
"the function with the Modal stub.\n",
"\n",
"To demonstrate that Modal functions you define in the notebook can be called by _other_ Modal functions, there's another function, `quadruple`, which uses `double` and `double_with_modal`.\n",
"For numbers greater than 1 million, this function spins up containers that run in Modal, which is a _very_ inefficient way to multiply a number by four, but you can do it if you please!\n",
"\n",
"Be aware that all Modal functions are defined and run using `with stub.run()` in a single cell. Currently, putting all Modal functions in a single-cell is a limitation of the Modal client.\n",
"We aim to make notebook code organization more flexible in the future."
"For numbers greater than 1 million, this function spins up containers that run in Modal, which is a _very_ inefficient way to multiply a number by four, but you can do it if you please!"
]
},
{
Expand All @@ -91,8 +89,9 @@
"\n",
"\n",
"with stub.run():\n",
" print(quadruple.local(100))\n",
" print(quadruple.local(100)) # running locally\n",
" print(quadruple.remote(100)) # run remotely\n",
" print(\"Doing a very inefficient remote multiplication just for fun!\")\n",
" result = quadruple.remote(10_000_000)"
]
},
Expand All @@ -102,21 +101,47 @@
"metadata": {},
"outputs": [],
"source": [
"# Evaluate the result created above.\n",
"# Evaluate the result created in above cell\n",
"result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### GPU-powered notebook cells!\n",
"\n",
"Thanks to Modal's remote execution capabilities, your notebook can be running on your laptop or a cheap CPU-only instance and take advantage of serverless GPU container execution. Here's the basics."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# Define a Modal function with a GPU attached.\n",
"@stub.function(gpu=\"any\")\n",
"def hello_gpu():\n",
" import subprocess\n",
" subprocess.run(\"nvidia-smi\", shell=True, check=True)\n",
" return \"hello from a remote GPU!\"\n",
"\n",
"\n",
"# Start and run an ephemeral modal.App and execute the GPU-powered modal Function!\n",
"with stub.run():\n",
" result = hello_gpu.remote()\n",
" assert result == \"hello from a remote GPU!\"\n",
"\n",
"# After the app is finished you can continue executing other function's defined in your notebook and\n",
"# use the results of your GPU functions!\n",
"\"This is the remote GPU's return value: \" + result"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.5 ('.venv': venv)",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand Down
6 changes: 5 additions & 1 deletion 11_notebooks/jupyter_inside_modal.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# ---
# args: ["--timeout", 10]
# ---

# ## Overview
#
# Quick snippet to connect to a Jupyter notebook server running inside a Modal container,
# Quick snippet showing how to connect to a Jupyter notebook server running inside a Modal container,
# especially useful for exploring the contents of Modal network file systems.
# This uses [Modal Tunnels](https://modal.com/docs/guide/tunnels#tunnels-beta)
# to create a tunnel between the running Jupyter instance and the internet.
#
# If you want to your Jupyter notebook to run _locally_ and execute remote Modal Functions in certain cells, see the `basic.ipynb` example :)

import os
import subprocess
Expand Down

0 comments on commit b449dd7

Please sign in to comment.