nqx
v0.0.4
Published
Numerical computing extensions for JavaScript.
Downloads
10
Readme
nqx
A NumPy-like library in the browser, running on the GPU, with automatic differentiation and JIT compilation.
Accelerated numerical computing extensions for JavaScript.
This is a numerical computing library that runs in the browser through WebGPU, enabling fast matrix and linear algebra operations for machine learning, computer graphics, generative art, and scientific computing.
Getting Started
Install the package to add it to your JavaScript project.
npm install nqx
Then, you can start writing code:
import { createEngine } from nqx;
const nqx = createEngine("webgpu");
await nqx.ready();
const x = nqx.array([1, 2, 3]);
const y = nqx.array([4, 5, 6]);
const z = x.add(y);
await z.js(); // [5, 7, 9]
await z.print(); // array([5., 7., 9.], dtype=float32)
await x.mul(y.add(1)).print(); // array([5., 12., 21.], dtype=float32)
Non-Bundler Usage
If you don't want to use a bundler or don't have an appropriate toolchain, you
can import nqx
directly from a CDN.
<script type="module">
import { createEngine } from "https://unpkg.com/nqx/dist/nqx.js";
const nqx = createEngine("webgpu");
await nqx.ready();
const x = nqx.array([1, 2, 3]);
await x.print();
</script>
If you're not using JavaScript modules, you can still use a script tag. This
allows you to access the library from the NQX
global variable.
<script src="https://unpkg.com/nqx/dist/nqx.iife.js"></script>
<script>
(async () => {
const nqx = NQX.createEngine("webgpu");
await nqx.ready();
const x = nqx.array([1, 2, 3]);
await x.print();
})();
</script>
Development
This library is written in TypeScript and developed primarily using Vite. Run
npm install
to download dependencies and npm test
to run tests, fully in the
browser. You may need to install Chrome web driver components as prompted.
To bundle the library for distribution, run npm run build
. This produces build
artifacts in the dist
folder.
Comparisons
This library is inspired by influential work in the Python ecosystem: NumPy and JAX, as well as the related libraries derived from them (to varying degrees: PyTorch, Tensorflow, Xarray, Dask, etc.). It's meant to fill the void of a simple, fast, and reliable system for numerical coding. What would be the JavaScript or web-based equivalent of that?
The state-of-the-art for GPU-accelerated computing in JavaScript has developers writing their own WebGL or WebGPU shaders, which is awkward and time-consuming. No Python developer would ever write their own CUDA kernels or BLAS implementations!
Compared to TensorFlow.js, nqx is much simpler and smaller, and it provides some key advantages:
- Uses native JavaScript data types and has a concise API.
- Not limited to machine learning use cases.
- More flexible, offering forward and reverse-mode automatic differentiation for arbitrary functions, including second and higher derivatives.
- (Planned) Includes a tracing JIT compiler for numerical operations with automatic kernel fusion.
- (Planned) Has a multithreaded WebAssembly backend with clean exchange between arrays stored on CPU and GPU devices.
However, there are some drawbacks:
- Newer library, not backed by a team of paid developers at Google.
- No support for binary Node.js and legacy WebGL backends.
- No plans for adding a Keras-like API.
License
nqx is provided under the MIT license. See LICENSE.