Python & GPUs

There are currently three popular python modules which support offloading to GPUs: PyTorch, TensorFlow and JAX. JAX and PyTorch are described further below.

JAX

JAX is the most modern of the three, and is being actively developed by Google. But its GitHub page states "this is a research project, not an official Google product. Expect sharp edges."

One potential surprise is that JAX defaults to supporting single precision only. To use double precision at all one has to add

jax.config.update("jax_enable_x64", True)

immediately after importing the module.

In some ways JAX is quite easy to use. In particular, it replicates most of Numpy. But there are some surprising differences. For example, arrays are immutable -- one can not modify individual elements by a simple assignment. Arrays created by JAX default to residing on the GPU (if JAX is being used with a GPU -- it can reasonably be used on a CPU-only host).

JAX not only has library functions which execute on an accelerator, but it can JIT (some) pure python functions and offload them too.

Considering an example of Linpack in python/numpy, one could convert this to JAX with minimal changes. The result is Linpack with JAX and numpy.

All that changed was adding

  import jax
  jax.config.update("jax_enable_x64", True)

and changing

  X=linalg.solve(A,B)

to

  X=jax.numpy.linalg.solve(A,B)
  X=matrix(X)

the final line being necessary to ensure that the result was still a numpy matrix. No conversion was needed for the arguments.

But as JAX includes the functionality of numpy, one could dispense with numpy altogether, and use JAX for everything, giving Linpack with JAX. In this case no significant benefit, but the flow of the code is quite different. The minimally-modified version does everything on the CPU until the call to linalg.solve causes data to be copied to the GPU. The pure JAX version has all its arrays resident on the GPU. The initial random matrix is created on the GPU by the GPU, no host to device copy is needed when the solver is called, and the answer is validated on the GPU.

In this version, if one comments out the jax.config.update line, everything becomes single precision. On consumer GPUs it will run about ten times faster.

PyTorch

Just as with JAX, we can choose to make minimal changes to the numpy code, and produce Linpack with torch and numpy.

There is slightly more work than there was for JAX, mostly because the documentation claimed that offloading was not supported for linalg.solve, but was for linalg.lu_factor and linalg.lu_solve. This is odd, because solve just calls lu_factor followed by lu_solve.

Torch is content to have its arguments reside on the host, and to return the result to the host. However, when chaining two library calls like this, it makes much more sense to keep the intermediate results on the GPU. So the inputs were explicitly copied to the GPU, and which point the output is put on the GPU ready to be used by the next GPU routine, and then the final output needs copying back.

Again one can dispense with numpy entirely, and do everything explicitly on the GPU. This is shown in Linpack with torch.

To change this version to single precision, change the one instance of torch.float64 to torch.float32.