In this notebook we'll see quickly how to leverage the GPU computing power with pyfaust.
Since pyfaust 2.9.0 the API has been modified to make the GPU available directly from the python wrapper. Indeed, an independent GPU module (aka gpu_mod) has been developed for this purpose.

The first question you might ask is: does it work on my computer? Here is the answer: the loading of this module is quite transparent, if an NVIDIA GPU is available and CUDA is properly installed on your system, you have normally nothing to do except installing pyfaust to get the GPU implementations at your fingertips. We'll see in the end of this notebook how to load manually the module and how to get further information in case of an error.

It is worthy to note two drawbacks about the pyfaust GPU support:

In addition to these drawbacks, please notice that the GPU module support is still considered in beta status as the code is relatively young and still evolving. However the API shouldn't evolve that much in a near future.

Creating a GPU Faust object

Let's start with some basic Faust creations on the GPU. Almost all the ways of creating a Faust object in CPU memory are also available to create a GPU Faust.
First of all, creating a Faust using the constructor works seamlessly on GPU, the only need is to specify the dev keyword argument, as follows:

It's clearly indicated in the output that the Faust object is instantiated in GPU memory (the N and M numpy arrays are copied from the CPU to the GPU memory). However it's also possible to check this programmatically:

While for a CPU Faust you'll get:

In gpuF the factors are dense matrices but it's totally possible to instantiate sparse matrices on the GPU as you can do on CPU side.

You can also create a GPU Faust by explicitly copying a CPU Faust to the GPU memory. Actually, at anytime you can copy a CPU Faust to GPU and conversely. The clone() member function is here precisely for this purpose. Below we copy gpuF to CPU and back again to GPU in the new Faust gpuF2.

Generating a GPU Faust

Many of the functions for generating a Faust object on CPU are available on GPU too. It is always the same, you precise the dev argument by assigning the 'gpu' value and you'll get a GPU Faust instead of a CPU Faust.

For example, the code below will successively create a random GPU Faust, a Hadamard transform GPU Faust, a identity GPU Faust and finally a DFT GPU Faust.

Manipulating GPU Fausts and CPU interoperability

Once you've created GPU Faust objects, you can perform operations on them staying in GPU world (that is, with no array transfer to CPU memory). That's of course not always possible. For example, let's consider Faust-scalar multiplication and Faust-matrix product. In the first case the scalar is copied to the GPU memory and likewise in the second case the matrix is copied from CPU to GPU in order to proceed to the computation. However in both cases the Faust factors stay into GPU memory and don't move during the computation.

As you see the first factor's address has changed in the result compared to what it was in gpuF. Indeed, when you make a scalar multiplication only one factor is multiplied, the others don't change, they are shared between the Faust being multiplied and the resulting Faust. This is an optimization and to go further in this direction the factor chosen to be multiplied is the smallest in memory (not necessarily the first one).

On the contrary, and that matters for optimization, there is no CPU-GPU transfer at all when you create another GPU Faust named for example gpuF2 on the GPU and decide to multiply the two of them like this:

Besides, it's important to note that gpuF3 factors are not duplicated in memory because they already exist for gpuF and gpuF2, that's an extra optimization: gpuF3 is just a memory view of the factors of gpuF and gpuF2 (the same GPU arrays are shared between Faust objects). That works pretty well the same for CPU Faust objects.

Finally, please notice that CPU Faust objects are not directly interoperable with GPU Fausts objects. You can try, it'll end up with an error.

Benchmarking your GPU with pyfaust!

Of course when we run some code on GPU rather than on CPU, it is clearly to enhance the performance. So let's try your GPU and find out if it is worth it or not compared to your CPU.

First, measure how much time it takes on CPU to compute a Faust norm and the dense array corresponding to the product of its factors:

Now let's make some GPU heat with norms and matrix products!

Of course not all GPUs are equal, below are the results I got using a Tesla V100:

6.85 ms ± 9.06 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
6.82 ms ± 90.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Likewise let's compare the performance obtained for a sparse Faust:

On a Tesla V100 it gives these results:

9.86 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
13.8 ms ± 39.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Running some FAµST algorithms on GPU

Some of the FAµST algorithms implemented in the C++ core are now also available in pure GPU mode. For example, let's compare the factorization times taken by the hierarchical factorization when launched on CPU and GPU. When running on GPU, the matrix to factorize is copied in GPU memory and almost all operations executed during the algorithm don't imply the CPU in any manner (the only exception at this stage of development is the proximal operators that only run on CPU).


Depending on you GPU card and CPU the results may vary, so below are shown some results obtained on specific hardware.

Implementation Hardware Time (s) Error Faust vs MEG matrix
CPU Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz 616.16 .130
GPU NVIDIA GTX980 147.81 .130
GPU RTX2080 73.88 .130

Manually loading the pyfaust GPU module

If something goes wrong when trying to use the GPU pyfaust extension, here is how to manually load the module and obtain more information.

The key is the function enable_gpu_mod. This function allows to give another try to gpu_mod loading with the verbose mode enabled.

Afterward you can call pyfaust.is_gpu_mod_enabled() to verify if it works in your script.

Below I copy outputs that show what it should look like when it doesn't work:

1) If you asked a fatal error using enable_gpu_mod(silent=False, fatal=True) an exception will be raised and your code won't be able to continue after this call:

python -c "import pyfaust; pyfaust.enable_gpu_mod(silent=False, fatal=True)"
WARNING: you must call enable_gpu_mod() before using GPUModHandler singleton.
loading libgm cannot open shared object file: No such file or directory
Exception: Can't load gpu_mod library, maybe the path (/home/test/venv_pyfaust-2.10.14/lib/python3.7/site-packages/pyfaust/lib/ is not correct or the backend (cuda) is not installed or configured properly so the libraries are not found.

2) If you just want a warning, you must use enable_gpu_mod(silent=False), the code will continue after with no gpu_mod enabled but you'll get some information about what is going wrong (here the CUDA toolkit version 9.2 is not installed) :

python -c "import pyfaust; pyfaust.enable_gpu_mod(silent=False)"
WARNING: you must call enable_gpu_mod() before using GPUModHandler singleton.
loading libgm cannot open shared object file: No such file or directory

Thanks for reading this notebook! Many other are available at

Note: this notebook was executed using the following pyfaust version: