Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
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
588 changes: 588 additions & 0 deletions docs/sources/01-get_started.ipynb

Large diffs are not rendered by default.

182 changes: 182 additions & 0 deletions docs/sources/02-dpnp_numpy_fallback.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "be340585",
"metadata": {},
"source": [
"# Usage of NumPy* functions in the Data Parallel Extension for NumPy* library"
]
},
{
"cell_type": "markdown",
"id": "9008dbe5",
"metadata": {},
"source": [
"1. An example of using the `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK` environment variable and finding specific values in an array based on a condition with the dpnp library"
]
},
{
"cell_type": "markdown",
"id": "5d4300fe",
"metadata": {},
"source": [
"Not all functions are yet implemented in the Data Parallel Extension for NumPy* library, some of them require enabling of the direct fallback to the NumPy* library. \n",
"One of the example can be the \"dpnp.full ()\" function and the like parameter in the non-default state. \n",
"Let's look on the example where we want to create a two-dimensional array with a singular element and the array like option. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c78511cd",
"metadata": {},
"outputs": [
{
"ename": "NotImplementedError",
"evalue": "Requested funtion=full with args=((2, 2), 3, None, 'C') and kwargs={'like': array([[0.],\n [0.]], dtype=float32)} isn't currently supported and would fall back on NumPy implementation. Define enviroment variable `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK` to `0` if the fall back is required to be supported without rasing an exception.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdpnp\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Create an two dimencial array with singular element and array like option\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m#a = np.full((2,2),3, like = np.zeros((2, 0)))\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfull\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlike\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArray a is located on the device:\u001b[39m\u001b[38;5;124m\"\u001b[39m, a\u001b[38;5;241m.\u001b[39mdevice)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mArray a is\u001b[39m\u001b[38;5;124m\"\u001b[39m, a)\n",
"File \u001b[1;32m~\\Anaconda3\\envs\\my_env\\lib\\site-packages\\dpnp\\dpnp_iface_arraycreation.py:747\u001b[0m, in \u001b[0;36mfull\u001b[1;34m(shape, fill_value, dtype, order, like, device, usm_type, sycl_queue)\u001b[0m\n\u001b[0;32m 738\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 739\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dpnp_container\u001b[38;5;241m.\u001b[39mfull(shape,\n\u001b[0;32m 740\u001b[0m fill_value,\n\u001b[0;32m 741\u001b[0m dtype\u001b[38;5;241m=\u001b[39mdtype,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 744\u001b[0m usm_type\u001b[38;5;241m=\u001b[39musm_type,\n\u001b[0;32m 745\u001b[0m sycl_queue\u001b[38;5;241m=\u001b[39msycl_queue)\n\u001b[1;32m--> 747\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcall_origin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnumpy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfull\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mshape\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfill_value\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43morder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlike\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlike\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mdpnp\\dpnp_utils\\dpnp_algo_utils.pyx:132\u001b[0m, in \u001b[0;36mdpnp.dpnp_utils.dpnp_algo_utils.call_origin\u001b[1;34m()\u001b[0m\n",
"\u001b[1;31mNotImplementedError\u001b[0m: Requested funtion=full with args=((2, 2), 3, None, 'C') and kwargs={'like': array([[0.],\n [0.]], dtype=float32)} isn't currently supported and would fall back on NumPy implementation. Define enviroment variable `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK` to `0` if the fall back is required to be supported without rasing an exception."
]
}
],
"source": [
"import dpnp as np\n",
"\n",
"# Create a two-dimensional array with a singular element and the array like option\n",
"a = np.full((2,2),3, like = np.zeros((2, 1)))\n",
"print (\"Array a is located on the device:\", a.device)\n",
"print (\"Array a is\", a)"
]
},
{
"cell_type": "markdown",
"id": "ae026021",
"metadata": {},
"source": [
"As you can see, the function \"dpnp.full ()\" and the like parameter in non-default state are not implemented in the Data Parallel Extension for NumPy* library.\n",
"We got the following error message: \"Requested funtion=full with args=((2, 2), 3, None, 'C') and kwargs={'like': <dpnp.dpnp_array.dpnp_array object at 0x0000021C7FAAF8B0>} isn't currently supported and would fall back on NumPy* implementation. \n",
"By default, the `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK` environment variable is not null, and this allows only to use the Data Parallel Extension for NumPy* library. "
]
},
{
"cell_type": "markdown",
"id": "5964ed7a",
"metadata": {},
"source": [
"To avoid this situation, you can try to rewrite your example using supported functions. E.g.:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a144f319",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Array a is located on the device: Device(level_zero:gpu:0)\n",
"Array a is [[3 3]\n",
" [3 3]]\n"
]
}
],
"source": [
"import dpnp as np\n",
"\n",
"# Create a two-dimensional array\n",
"a = np.zeros((2, 1))\n",
"b = np.full_like(a, 3, dtype=np.int64)\n",
"a = np.broadcast_to(b, (2, 2))\n",
"print (\"Array a is located on the device:\", a.device)\n",
"print (\"Array a is\", a)"
]
},
{
"cell_type": "markdown",
"id": "9a835a0c",
"metadata": {},
"source": [
"If we want to call the NumPy* library for functions not supported in the Data Parallel Extension for NumPy* library, we need to change the `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK` environment variable to `0`. The example will then work without causing an exception."
]
},
{
"cell_type": "markdown",
"id": "210440c3",
"metadata": {},
"source": [
"Let's rewrite the same example using the `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK = 0` environment variable."
]
},
{
"cell_type": "markdown",
"id": "bca7490d",
"metadata": {},
"source": [
"`Note:` Please pay attention that if you are working in the Jupyter Notebook, you must restart the kernel in Jupyter Notebook before running the example with the setting `DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK = 0 `."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "05ad1565",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK = 0\n",
"Array a is located on the device: Device(level_zero:gpu:0)\n",
"Array a is [[3 3]\n",
" [3 3]]\n"
]
}
],
"source": [
"import os\n",
"# call numpy if not null than we will use dpnp, by default not null\n",
"os.environ[\"DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK\"] = \"0\" \n",
"\n",
"import dpnp as np\n",
"\n",
"# Expect result 0\n",
"print (\"DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK =\", np.config.__DPNP_RAISE_EXCEPION_ON_NUMPY_FALLBACK__) \n",
"\n",
"# Create an two dimencial array with singular element and array like option\n",
"a = np.full((2,2),3, like = np.zeros((2, 1)))\n",
"print (\"Array a is located on the device:\", a.device)\n",
"print (\"Array a is\", a)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading