Last active
November 30, 2019 18:18
-
-
Save monocongo/35328ef88af9f4ff8a0eb7e5f6a2854b to your computer and use it in GitHub Desktop.
Inability to assign values into shared memory objects using ray, i.e. it results in ValueError: assignment destination is read-only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import psutil\n", | |
"import numpy as np\n", | |
"import ray" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2019-11-30 12:47:34,442\tWARNING worker.py:1268 -- WARNING: Not updating worker name since `setproctitle` is not installed. Install this with `pip install setproctitle` (or ray[debug]) to enable monitoring of worker processes.\n", | |
"2019-11-30 12:47:34,445\tINFO resource_spec.py:205 -- Starting Ray with 13.82 GiB memory available for workers and up to 6.92 GiB for objects. You can adjust these settings with ray.init(memory=<bytes>, object_store_memory=<bytes>).\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{'node_ip_address': '192.168.0.111',\n", | |
" 'redis_address': '192.168.0.111:48348',\n", | |
" 'object_store_address': '/tmp/ray/session_2019-11-30_12-47-34_443599_4531/sockets/plasma_store',\n", | |
" 'raylet_socket_name': '/tmp/ray/session_2019-11-30_12-47-34_443599_4531/sockets/raylet',\n", | |
" 'webui_url': None,\n", | |
" 'session_dir': '/tmp/ray/session_2019-11-30_12-47-34_443599_4531'}" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"num_cpus = psutil.cpu_count(logical=False)\n", | |
"ray.init(num_cpus=num_cpus, ignore_reinit_error=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def add_average(\n", | |
" in_ary: np.ndarray,\n", | |
" out_ary: np.ndarray,\n", | |
" lat_index: int,\n", | |
" lon_index: int,\n", | |
"):\n", | |
" ary = in_ary[lat_index, lon_index]\n", | |
" out_ary[lat_index, lon_index] = ary + np.mean(ary)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def compute_with_loop(\n", | |
" input_array: np.ndarray,\n", | |
") -> np.ndarray:\n", | |
" output_array = np.full(shape=input_array.shape, fill_value=np.NaN)\n", | |
" for lat_index in range(input_array.shape[0]):\n", | |
" for lon_index in range(input_array.shape[0]):\n", | |
" add_average(input_array, output_array, lat_index, lon_index)\n", | |
" return output_array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"@ray.remote\n", | |
"def add_average_ray(\n", | |
" in_ary: np.ndarray,\n", | |
" out_ary: np.ndarray,\n", | |
" lat_index: int,\n", | |
" lon_index: int,\n", | |
"):\n", | |
" ary = in_ary[lat_index, lon_index]\n", | |
" out_ary[lat_index, lon_index] = ary + np.mean(ary)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def compute_with_ray(\n", | |
" input_array: np.ndarray,\n", | |
") -> np.ndarray:\n", | |
" output_array = np.full(shape=input_array.shape, fill_value=np.NaN)\n", | |
" in_array_id = ray.put(input_array)\n", | |
" out_array_id = ray.put(output_array)\n", | |
" futures = []\n", | |
" for lat_index in range(input_array.shape[0]):\n", | |
" for lon_index in range(input_array.shape[1]):\n", | |
" futures.append(add_average_ray.remote(in_array_id, out_array_id, lat_index, lon_index))\n", | |
"\n", | |
" ray.get(futures)\n", | |
"\n", | |
" return output_array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# create an array that can be used to represent a 2x2 cell lat/lon map with 3 times\n", | |
"tst_ary_1 = np.array([[[1, 6, 5], [3, 2, 7]], [[8, 4., 6.], [9, 4, 2]]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.14 ms, sys: 0 ns, total: 1.14 ms\n", | |
"Wall time: 2.49 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[ 5., 10., 9.],\n", | |
" [ 7., 6., 11.]],\n", | |
"\n", | |
" [[14., 10., 12.],\n", | |
" [14., 9., 7.]]])" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"average_added_loop = compute_with_loop(tst_ary_1)\n", | |
"average_added_loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "RayTaskError(ValueError)", | |
"evalue": "\u001b[36mray_worker\u001b[39m (pid=4569, host=skypilot)\n File \"<ipython-input-5-0bf9c2bf3f2e>\", line 9, in add_average_ray\nValueError: assignment destination is read-only", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mRayTaskError(ValueError)\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;32m<ipython-input-6-2fc392838a4e>\u001b[0m in \u001b[0;36mcompute_with_ray\u001b[0;34m(input_array)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mfutures\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0madd_average_ray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremote\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0min_array_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout_array_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlat_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlon_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mray\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfutures\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0moutput_array\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m~/miniconda3/envs/spi_ray/lib/python3.7/site-packages/ray/worker.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(object_ids)\u001b[0m\n\u001b[1;32m 2119\u001b[0m \u001b[0mworker\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdump_object_store_memory_usage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2120\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mRayTaskError\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2121\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_instanceof_cause\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2122\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2123\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mRayTaskError(ValueError)\u001b[0m: \u001b[36mray_worker\u001b[39m (pid=4569, host=skypilot)\n File \"<ipython-input-5-0bf9c2bf3f2e>\", line 9, in add_average_ray\nValueError: assignment destination is read-only" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"average_added_ray = compute_with_ray(tst_ary_1)\n", | |
"average_added_ray" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'average_added_ray' is not defined", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-10-ef1521416d6f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mallclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maverage_added_loop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maverage_added_ray\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'average_added_ray' is not defined" | |
] | |
} | |
], | |
"source": [ | |
"np.allclose(average_added_loop, average_added_ray)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment