From cf5de6d6473850c42c51d471bfedbf0a440a9a42 Mon Sep 17 00:00:00 2001 From: Jonathon Belotti Date: Mon, 26 Feb 2024 21:31:45 +0000 Subject: [PATCH] refresh Jupyter Notebook examples --- 11_notebooks/basic.ipynb | 47 +++++++++++++++++++++------- 11_notebooks/jupyter_inside_modal.py | 6 +++- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/11_notebooks/basic.ipynb b/11_notebooks/basic.ipynb index 0e77e9893..1089d0cdc 100644 --- a/11_notebooks/basic.ipynb +++ b/11_notebooks/basic.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "%pip install modal-client\n", + "%pip install --upgrade modal\n", "%pip install ipywidgets" ] }, @@ -18,7 +18,8 @@ "source": [ "import modal\n", "\n", - "assert modal.__version__ > \"0.49.0\"" + "assert modal.__version__ > \"0.49.0\"\n", + "modal.__version__" ] }, { @@ -29,7 +30,7 @@ "source": [ "import modal\n", "\n", - "stub = modal.Stub(name=\"example-basic-notebook\")" + "stub = modal.Stub(name=\"example-basic-notebook-app\")" ] }, { @@ -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!" ] }, { @@ -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)" ] }, @@ -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" }, diff --git a/11_notebooks/jupyter_inside_modal.py b/11_notebooks/jupyter_inside_modal.py index 5a0e215ce..9d7a2225b 100644 --- a/11_notebooks/jupyter_inside_modal.py +++ b/11_notebooks/jupyter_inside_modal.py @@ -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