{"id":30348964,"url":"https://github.com/mil-tokyo/wgpy","last_synced_at":"2025-08-18T19:45:04.799Z","repository":{"id":280602654,"uuid":"616220034","full_name":"mil-tokyo/wgpy","owner":"mil-tokyo","description":"WebGPU/WebGL accelerated numpy-compatible array library for web browser","archived":false,"fork":false,"pushed_at":"2025-03-04T09:16:13.000Z","size":757,"stargazers_count":37,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-06-26T16:38:05.466Z","etag":null,"topics":["javascript","machine-learning","python","webgl","webgpu"],"latest_commit_sha":null,"homepage":"https://wgpy-demo.web.app/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mil-tokyo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-19T23:54:00.000Z","updated_at":"2025-06-16T04:59:27.000Z","dependencies_parsed_at":"2025-03-04T10:36:17.114Z","dependency_job_id":null,"html_url":"https://github.com/mil-tokyo/wgpy","commit_stats":null,"previous_names":["mil-tokyo/wgpy"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mil-tokyo/wgpy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mil-tokyo%2Fwgpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mil-tokyo%2Fwgpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mil-tokyo%2Fwgpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mil-tokyo%2Fwgpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mil-tokyo","download_url":"https://codeload.github.com/mil-tokyo/wgpy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mil-tokyo%2Fwgpy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271050819,"owners_count":24691184,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","machine-learning","python","webgl","webgpu"],"created_at":"2025-08-18T19:45:03.857Z","updated_at":"2025-08-18T19:45:04.757Z","avatar_url":"https://github.com/mil-tokyo.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# wgpy: WebGL accelerated numpy-compatible array library for web browser\n\nwgpy is a WebGL accelerated numpy-compatible array library for web browsers. It runs on [Pyodide](https://pyodide.org/), the python runtime which runs on the web browser. Deep learning can also be performed on GPUs in conjunction with [Chainer](https://github.com/chainer/chainer).\n\nFor core implementation technique, please refer to [our arXiv paper](https://arxiv.org/abs/2503.00279).\n\n# Demo\n\nYou can actually run WgPy in your web browser at [the demo site](https://wgpy-demo.web.app/).\n\n\u003cimg src=\"images/mandelbrot.png\" width=\"50%\"\u003e\n\n# Example\n\nWith WgPy, such CuPy-compatible Python code can be executed on the GPU in a web browser.\n\n```python\ndef mandelbrot(real, imag, n_iters):\n    xp = cp.get_array_module(real) # get numpy or wgpy depending on the input\n    xs = xp.zeros((real.size, imag.size), dtype=np.float32)\n    ys = xp.zeros((real.size, imag.size), dtype=np.float32)\n    count = xp.zeros((real.size, imag.size), dtype=np.int32)\n    for _ in range(n_iters):\n        xs, ys = xs * xs - ys * ys + real, xs * ys * 2.0 + imag\n        count += ((xs * xs + ys * ys) \u003c 4.0).astype(np.int32)\n    return count\n```\n\nFor efficient execution, a custom kernel can also be implemented.\n\n```python\n# WebGPU custom kernel\nElementwiseKernel(\n    in_params=\"f32 real, f32 imag\",\n    out_params=\"i32 c\",\n    operation=\"\"\"\nc = 0;\nvar x: f32 = 0.0;\nvar y: f32 = 0.0;\nfor(var k: u32 = 0u; k \u003c 500u; k = k + 1u) {\n    var nx: f32 = x * x - y * y + real;\n    var ny: f32 = x * y * 2.0 + imag;\n    x = nx;\n    y = ny;\n    if (x * x + y * y \u003c 4.0) {\n        c = c + 1;\n    }\n}\n    \"\"\",\n    name=f\"mandelbrot\",\n)\n```\n\n```python\n# WebGL custom kernel\nElementwiseKernel(\n    in_params=\"float real, float imag\",\n    out_params=\"int c\",\n    operation=\"\"\"\nc = 0;\nfloat x = 0.0;\nfloat y = 0.0;\nfor (int k = 0; k \u003c 500; k++) {\n    float nx = x * x - y * y + real;\n    float ny = x * y * 2.0 + imag;\n    x = nx;\n    y = ny;\n    if (x * x + y * y \u003c 4.0) {\n        c = c + 1;\n    }\n}\n    \"\"\",\n    name=f\"mandelbrot_500\",\n)\n```\n\n# System Structure\n\nWgPy runs on the web browser. The following figure shows the system structure.\n\n\u003cimg src=\"images/mandelbrot.png\" width=\"50%\"\u003e\n\n# Setup Environment\n\nPython 3.11 and Node.js 16 are required.\n\n## Downloading Pyodide\n\n```\n./scripts/download-pyodide.sh\n```\n\n## Install dependencies\n\n```\npip install -r requirements.txt\nnpm install\n```\n\n# Build\n\n```\nnpm run build\n```\n\nThe output is placed in the `dist` directory.\nThe Pyodide is expected to work on WebWorker, not the main thread, because wgpy is mainly forcusing for running heavy tasks. \n`{wgpy-main.js,wgpy-worker.js}` are the JavaScript library. The `wgpy-main.js` needs to be loaded in the main thread (using `\u003cscript\u003e` tag). It calls the WebGL API following the commands from WebWorker. `wgpy-worker.js` has to be loaded in WebWorker thread (using `importScripts`). This script exposes an interface to the wgpy python library and proxy it to the main thread. `wgpy_webgl-\u003cversion\u003e-py3-none-any.whl` is the python library that is loaded using `await pyodide.loadPackage('path/to/wgpy_webgl-\u003cversion\u003e-py3-none-any.whl');` in the JavaScript code or `micropip.install('path/to/wgpy_webgl-\u003cversion\u003e-py3-none-any.whl')` in the Python code. It gives a python API that has the same interface as numpy. The wgpy can be imported as `import wgpy as wp`. A wrapper of wgpy to provide the same API as cupy is also implemented to minimize changes to Chainer (e.g. `import cupy as cp`). `wgpy_test-\u003cversion\u003e-py3-none-any.whl` contains test code.\n\nHere is the minimum sample code:\n\n```python\nimport wgpy as wp\n# transfer array to GPU\ngpu_in_data = wp.asarray([1.0, 2.0, 2.5])\n# compute on GPU\ngpu_out_data = gpu_in_data * 2.0\n# transfer array to CPU (numpy ndarray)\nout_data = wp.asnumpy(gpu_out_data)\n```\n\nFor compatibility with CuPy, you can also use the following code:\n\n```python\nimport cupy as cp\n# transfer array to GPU\ngpu_in_data = cp.asarray([1.0, 2.0, 2.5])\n# compute on GPU\ngpu_out_data = gpu_in_data * 2.0\n# transfer array to CPU (numpy ndarray)\nout_data = cp.asnumpy(gpu_out_data)\n```\n\n# Run Examples\n\nStart HTTP server:\n\n```\nnpm start\n```\n\nOpen http://localhost:8000 and click link to a sample.\n\nIf you access to the server from another device such as smartphones, you need to access the server by a URL starting with `https://`. Even if a URL such as `http://192.168.0.2` seems to display the page, the logic does not work. To access to the server via https URL, you can use online service such as [ngrok](https://ngrok.com/).\n\nAfter you install ngrok, run the command in a new terminal:\n\n```\nngrok http 8000\n```\n\nThen access to the displayed URL (`https://*.ngrok.io`).\n\nThe reason why URLs beginning with https are required is that secure origin is needed to use the SharedArrayBuffer that this library uses internally.\n\n# Test\n\n## Build\n\n```\nnpm run build:test\n```\n\n# Run\n\n```\nnpm start\n```\n\nOpen \u003chttp://localhost:8000/test/\u003e and click TEST button. If you run only one test file, specify it in the URL such as \u003chttp://localhost:8000/test/?test_path=/test_dot.py\u003e .\n\n# About samples which uses Chainer\n\nTo run MNIST sample, you need the following steps.\n\n## Creating a wheel with Chainer modified for Pyodide\n\n```\n./scripts/make-chainer-wheel.sh\n```\n\n`lib/chainer-5.4.0-py3-none-any.whl` is generated.\n\nChainer 5.4.0 is the last version of Chainer which contains pure-python code only. Extra work is needed to support Chainer versions with native (C++) extensions in Pyodide.\n\n## Download and preprocess MNIST dataset\n\nChainer 5.4.0 is needed.\n\n```\npip install chainer==5.4.0\n```\n\nRun:\n\n```\npython scripts/pack_mnist.py\n```\n\n`lib/mnist.zip` is generated.\n\n\n## Download and preprocess CIFAR-100 dataset\n\nChainer 5.4.0 is needed.\n\n```\npip install chainer==5.4.0\n```\n\nRun:\n\n```\npython scripts/pack_cifar100.py\n```\n\n`lib/cifar100.zip` is generated.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmil-tokyo%2Fwgpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmil-tokyo%2Fwgpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmil-tokyo%2Fwgpy/lists"}