Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default env section to env tutorial #13

Merged
merged 1 commit into from
May 20, 2024
Merged
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
159 changes: 157 additions & 2 deletions docs/api-envs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"source": [
"### Envs on the Cluster\n",
"\n",
"Runhouse environments are generic environments, and the object itself is not associated with a cluster. However, it is a core component of Runhouse services, like functions and modules, which are associated with a cluster. As such, it is set up remotely when these services are sent over to the cluster -- packags are installed, working directory and env vars/secrets synced over, and cached on the cluster."
"Runhouse environments are generic environments, and the object itself is not associated with a cluster. However, it is easy to set up an environment on the cluster, by simply calling the `env.to(cluster)` API, or by sending your module/function to the env with the `<rh_fn>.to(cluster=cluster, env=env)` API, which will construct and cache the environment on the remote cluster."
]
},
{
Expand Down Expand Up @@ -245,6 +245,161 @@
"source": [
"On the cluster, each environment is associated with its own Ray Actor servlet, which handles all the activities within the environment (installing packages, getting or putting objects, calling functions, etc). Each env servlet has its own local object store where objects persist in Python, and lives in its own process, reducing interprocess overhead and eliminating launch overhead for calls made in the same env."
]
},
{
"cell_type": "markdown",
"id": "d4095b6a-2c70-4901-b6b1-2f27bf853bfc",
"metadata": {},
"source": [
"#### Cluster Default Env\n",
"\n",
"The cluster also has a concept of a base default env, which is the environment on which the runhouse server will be started from. It is the environment in which cluster calls and computations, such as commands and functions, will default to running on, if no other env is specified.\n",
"\n",
"During cluster initialization, you can specify the default env for the cluster. It is constructed as with any other runhouse env, using `rh.env()`, and contains any package installations, commands to run, or env vars to set prior to starting the Runhouse server, or even a particular conda env to isolate your Runhouse environment. If no default env is specified, runs on the base environment on the cluster (after sourcing bash)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7fe21d29-eb43-4936-91a0-367cb046c75e",
"metadata": {},
"outputs": [],
"source": [
"import runhouse as rh"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6bff8df0-7ee0-4ed4-a15c-435c1efb978d",
"metadata": {},
"outputs": [],
"source": [
"default_env = rh.conda_env(\n",
" name=\"cluster_default\",\n",
" reqs=[\"skypilot\"], # to enable autostop, which requires skypilot library\n",
" working_dir=\"./\",\n",
" env_vars={\"my_token\": \"TOKEN_VAL\"}\n",
")\n",
"cluster = rh.ondemand_cluster(\n",
" name=\"rh-cpu\",\n",
" instance_type=\"CPU:2+\",\n",
" provider=\"aws\",\n",
" default_env=default_env,\n",
")\n",
"cluster.up_if_not()"
]
},
{
"cell_type": "markdown",
"id": "76d78173-09f9-4a8a-ab5f-f4a0a69110db",
"metadata": {},
"source": [
"Now, as we see in the examples below, running a command or sending over a function without specifying an env will default the default conda env that we have specified for the cluster."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9e6a9469-1845-46c7-a5ac-351fd78b6748",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO | 2024-05-20 18:08:42.460946 | Calling cluster_default._run_command\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[36mRunning command in cluster_default: conda run -n cluster_default conda env list | grep '*'\n",
"\u001b[0m\u001b[36mcluster_default * /opt/conda/envs/cluster_default\n",
"\u001b[0m"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO | 2024-05-20 18:08:45.130137 | Time to call cluster_default._run_command: 2.67 seconds\n"
]
},
{
"data": {
"text/plain": [
"[(0, 'cluster_default * /opt/conda/envs/cluster_default\\n', '')]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cluster.run(\"conda env list | grep '*'\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "755802bc-2eec-47fc-88c9-b08cf404fd92",
"metadata": {},
"outputs": [],
"source": [
"def check_import():\n",
" import sky\n",
" return \"import succeeded\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7f8ee06-e5cf-496a-bcdb-36b7e75bc45e",
"metadata": {},
"outputs": [],
"source": [
"check_remote_import = rh.function(check_import).to(cluster)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "398357ac-7676-4924-a9fe-35cda0cf2773",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO | 2024-05-20 18:30:05.128009 | Calling check_import.call\n",
"INFO | 2024-05-20 18:30:05.691348 | Time to call check_import.call: 0.56 seconds\n"
]
},
{
"data": {
"text/plain": [
"'import succeeded'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_remote_import()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ab2c7ef-9040-47d0-9a0b-15795530091f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -263,7 +418,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down