@@ -145,44 +145,64 @@ How do I configure/test my BLAS library
145
145
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146
146
147
147
There are many ways to configure BLAS for PyTensor. This is done with the PyTensor
148
- flags ``blas__ldflags `` (:ref: `libdoc_config `). The default is to use the BLAS
149
- installation information in NumPy, accessible via
150
- ``numpy.__config__.show() ``. You can tell pytensor to use a different
151
- version of BLAS, in case you did not compile NumPy with a fast BLAS or if NumPy
152
- was compiled with a static library of BLAS (the latter is not supported in
153
- PyTensor).
148
+ flags ``blas__ldflags `` (:ref: `libdoc_config `). If not specified, PyTensor will
149
+ attempt to find a local BLAS library to link against, prioritizing specialized implementations.
150
+ The details can be found in :func: `pytensor.link.c.cmodule.default_blas_ldflags `.
154
151
155
- The short way to configure the PyTensor flags ``blas__ldflags `` is by setting the
156
- environment variable :envvar: ` PYTENSOR_FLAGS ` to `` blas__ldflags=XXX `` (in bash
157
- `` export PYTENSOR_FLAGS=blas__ldflags=XXX ``)
152
+ Users can manually set the PyTensor flags ``blas__ldflags `` to link against a
153
+ specific version. This is useful even if the default version is the desired one,
154
+ as it will avoid the costly work of trying to find the best BLAS library at runtime.
158
155
159
- The `` ${HOME}/.pytensorrc `` file is the simplest way to set a relatively
160
- permanent option like this one. Add a `` [blas] `` section with an `` ldflags ``
161
- entry like this:
156
+ The PyTensor flags can be set in a few ways:
157
+
158
+ 1. In the `` ${HOME}/.pytensorrc `` file.
162
159
163
160
.. code-block :: cfg
164
161
165
162
# other stuff can go here
166
163
[blas]
167
- ldflags = -lf77blas -latlas -lgfortran # put your flags here
164
+ ldflags = -llapack -lblas -lcblas # put your flags here
168
165
169
166
# other stuff can go here
170
167
171
- For more information on the formatting of ``~/.pytensorrc `` and the
172
- configuration options that you can put there, see :ref: `libdoc_config `.
168
+ 2. In BASH before running your script:
169
+
170
+ .. code-block :: bash
171
+
172
+ export PYTENSOR_FLAGS=" blas__ldflags='-llapack -lblas -lcblas'"
173
+
174
+ 3. In an Ipython/Jupyter notebook before importing PyTensor:
175
+
176
+ .. code-block :: python
177
+
178
+ % set_env PYTENSOR_FLAGS = blas__ldflags= ' -llapack -lblas -lcblas'
179
+
180
+
181
+ 4. In `pytensor.config ` directly:
182
+
183
+ .. code-block :: python
184
+
185
+ import pytensor
186
+ pytensor.config.blas__ldflags = ' -llapack -lblas -lcblas'
187
+
188
+
189
+ (For more information on the formatting of ``~/.pytensorrc `` and the
190
+ configuration options that you can put there, see :ref: `libdoc_config `.)
191
+
192
+ You can find the default BLAS library that PyTensor is linking against by
193
+ checking ``pytensor.config.blas__ldflags ``
194
+ or running :func: `pytensor.link.c.cmodule.default_blas_ldflags `.
173
195
174
196
Here are some different way to configure BLAS:
175
197
176
- 0) Do nothing and use the default config, which is to link against the same
177
- BLAS against which NumPy was built. This does not work in the case NumPy was
178
- compiled with a static library (e.g. ATLAS is compiled by default only as a
179
- static library).
198
+ 0) Do nothing and use the default config.
199
+ This will usually work great for installation via conda/mamba/pixi (conda-forge channel).
200
+ It will usually fail to link altogether for installation via pip.
180
201
181
202
1) Disable the usage of BLAS and fall back on NumPy for dot products. To do
182
- this, set the value of ``blas__ldflags `` as the empty string (ex: ``export
183
- PYTENSOR_FLAGS=blas__ldflags= ``). Depending on the kind of matrix operations your
184
- PyTensor code performs, this might slow some things down (vs. linking with BLAS
185
- directly).
203
+ this, set the value of ``blas__ldflags `` as the empty string.
204
+ Depending on the kind of matrix operations your PyTensor code performs,
205
+ this might slow some things down (vs. linking with BLAS directly).
186
206
187
207
2) You can install the default (reference) version of BLAS if the NumPy version
188
208
(against which PyTensor links) does not work. If you have root or sudo access in
@@ -208,10 +228,29 @@ correctly (for example, for MKL this might be ``-lmkl -lguide -lpthread`` or
208
228
``-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -lguide -liomp5 -lmkl_mc
209
229
-lpthread ``).
210
230
231
+ 5) Use another backend such as Numba or JAX that perform their own BLAS optimizations,
232
+ by setting the configuration mode to ``"NUMBA" `` or ``"JAX" `` and making sure those packages are installed.
233
+ This configuration mode can be set in all the ways that the BLAS flags can be set, described above.
234
+
235
+ Alternatively, you can pass `mode='NUMBA' ` when compiling individual PyTensor functions without changing the default.
236
+ or use the ``config.change_flags `` context manager.
237
+
238
+ .. code-block :: python
239
+
240
+ from pytensor import function, config
241
+ from pytensor.tensor import matrix
242
+
243
+ x = matrix(' x' )
244
+ y = x @ x.T
245
+ f = function([x], y, mode = ' NUMBA' )
246
+
247
+ with config.change_flags(mode = ' NUMBA' ):
248
+ # compiling function that benefits from BLAS using NUMBA
249
+ f = function([x], y)
250
+
211
251
.. note ::
212
252
213
- Make sure your BLAS
214
- libraries are available as dynamically-loadable libraries.
253
+ Make sure your BLAS libraries are available as dynamically-loadable libraries.
215
254
ATLAS is often installed only as a static library. PyTensor is not able to
216
255
use this static library. Your ATLAS installation might need to be modified
217
256
to provide dynamically loadable libraries. (On Linux this
@@ -267,7 +306,7 @@ configuration information. Then, it will print the running time of the same
267
306
benchmarks for your installation. Try to find a CPU similar to yours in
268
307
the table, and check that the single-threaded timings are roughly the same.
269
308
270
- PyTensor should link to a parallel version of Blas and use all cores
309
+ PyTensor should link to a parallel version of BLAS and use all cores
271
310
when possible. By default it should use all cores. Set the environment
272
311
variable "OMP_NUM_THREADS=N" to specify to use N threads.
273
312
0 commit comments