pymc-devs / pymc-devs/pytensor

BUG: matrix multiplication causes compiler error on Windows

Open
#2,321 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Describe the issue:

I'm getting a compiler error when trying to use a dot product on a Windows install of PyTensor (see version information). The issue goes away when either removing blas__ldflags=-lblas or setting optimizer=None in the config but I'd rather not do that if possible. From what I can understand the error message seems to be related specifically to the Windows compiler.

Some notes:

  • I'm not using the latest version of PyTensor, but this is the latest version I could get on conda-forge with m2w64-toolchain
  • This uses a very old version of gcc (5.3.0 from 2015)
Reproducable code example:
import pytensor.tensor as pt
import pytensor

# Same problem with pt.dmatrix
a = pt.dvector("a")
b = pt.dvector("b")
c = a @ b

fn = pytensor.function([a, b], c)

print(fn([1, 2], [3, 4]))
Error message:
You can find the C code in this temporary file: C:\Users\TOMHAM~1\AppData\Local\Temp\pytensor_compilation_error_aq1rh79d
library ine>:0:0: is not found.
library blas is not found.
library lapack is not found.
Traceback (most recent call last):
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\vm.py", line 1230, in make_all
    node.op.make_thunk(node, storage_map, compute_map, [], impl=impl)
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\op.py", line 119, in make_thunk
    return self.make_c_thunk(node, storage_map, compute_map, no_recycling)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\op.py", line 84, in make_c_thunk
    outputs = cl.make_thunk(
              ^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1186, in make_thunk
    cthunk, module, in_storage, out_storage, error_storage = self.__compile__(
                                                             ^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1103, in __compile__
    thunk, module = self.cthunk_factory(
                    ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1631, in cthunk_factory
    module = cache.module_from_key(key=key, lnk=self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\cmodule.py", line 1250, in module_from_key
    module = lnk.compile_cmodule(location)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1532, in compile_cmodule
    module = c_compiler.compile_str(
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\cmodule.py", line 2676, in compile_str
    raise CompileError(
pytensor.link.c.exceptions.CompileError: Compilation failed (return status=1):
"C:\Users\TomHammond\miniconda3\envs\test_env_4\Library\mingw-w64\bin\g++.EXE" -shared -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -m64 -DMS_WIN64 -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\numpy\core\include" -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\include" -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\c_code" -L"C:\Users\TomHammond\miniconda3\envs\test_env_4\libs" -L"C:\Users\TomHammond\miniconda3\envs\test_env_4" -o "C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\me74e37e9c42976fda662dc2a17d4af4617dce1e2c92b41a904b6ced701075905.pyd" "C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\mod.cpp" -lblas -llapack "C:\Users\TomHammond\miniconda3\envs\test_env_4\python312.dll"
In file included from C:\Users\TomHammond\miniconda3\envs\test_env_4\include/Python.h:12:0,
                 from C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\mod.cpp:1:
C:\Users\TomHammond\miniconda3\envs\test_env_4\include/pyconfig.h:238:0: warning: "MS_WIN64" redefined
 #define MS_WIN64
 ^
<command-line>:0:0: note: this is the location of the previous definition
C:/Users/TomHammond/miniconda3/envs/test_env_4/Library/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lblas
C:/Users/TomHammond/miniconda3/envs/test_env_4/Library/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -llapack
collect2.exe: error: ld returned 1 exit status


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\TomHammond\Documents\PROTAC notebook v0.11.0\ternary_algos\test.py", line 8, in <module>
    fn = pytensor.function([a, b], c)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\compile\function\__init__.py", line 318, in function
    fn = pfunc(
         ^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\compile\function\pfunc.py", line 465, in pfunc
    return orig_function(
           ^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\compile\function\types.py", line 1769, in orig_function
    fn = m.create(defaults)
         ^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\compile\function\types.py", line 1661, in create
    _fn, _i, _o = self.linker.make_thunk(
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\basic.py", line 245, in make_thunk
    return self.make_all(
           ^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\vm.py", line 1239, in make_all
    raise_with_op(fgraph, node)
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\utils.py", line 526, in raise_with_op
    raise exc_value.with_traceback(exc_trace)
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\vm.py", line 1230, in make_all
    node.op.make_thunk(node, storage_map, compute_map, [], impl=impl)
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\op.py", line 119, in make_thunk
    return self.make_c_thunk(node, storage_map, compute_map, no_recycling)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\op.py", line 84, in make_c_thunk
    outputs = cl.make_thunk(
              ^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1186, in make_thunk
    cthunk, module, in_storage, out_storage, error_storage = self.__compile__(
                                                             ^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1103, in __compile__
    thunk, module = self.cthunk_factory(
                    ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1631, in cthunk_factory
    module = cache.module_from_key(key=key, lnk=self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\cmodule.py", line 1250, in module_from_key
    module = lnk.compile_cmodule(location)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\basic.py", line 1532, in compile_cmodule
    module = c_compiler.compile_str(
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\cmodule.py", line 2676, in compile_str
    raise CompileError(
pytensor.link.c.exceptions.CompileError: Compilation failed (return status=1):
"C:\Users\TomHammond\miniconda3\envs\test_env_4\Library\mingw-w64\bin\g++.EXE" -shared -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -m64 -DMS_WIN64 -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\numpy\core\include" -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\include" -I"C:\Users\TomHammond\miniconda3\envs\test_env_4\Lib\site-packages\pytensor\link\c\c_code" -L"C:\Users\TomHammond\miniconda3\envs\test_env_4\libs" -L"C:\Users\TomHammond\miniconda3\envs\test_env_4" -o "C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\me74e37e9c42976fda662dc2a17d4af4617dce1e2c92b41a904b6ced701075905.pyd" "C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\mod.cpp" -lblas -llapack "C:\Users\TomHammond\miniconda3\envs\test_env_4\python312.dll"
In file included from C:\Users\TomHammond\miniconda3\envs\test_env_4\include/Python.h:12:0,
                 from C:\Users\TomHammond\AppData\Local\PyTensor\compiledir_Windows-11-10.0.26200-SP0-Intel64_Family_6_Model_186_Stepping_2_GenuineIntel-3.12.13-64\tmp8d_ea1n_\mod.cpp:1:
C:\Users\TomHammond\miniconda3\envs\test_env_4\include/pyconfig.h:238:0: warning: "MS_WIN64" redefined
 #define MS_WIN64
 ^
<command-line>:0:0: note: this is the location of the previous definition
C:/Users/TomHammond/miniconda3/envs/test_env_4/Library/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lblas
C:/Users/TomHammond/miniconda3/envs/test_env_4/Library/mingw-w64/bin/../lib/gcc/x86_64-w64-mingw32/5.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -llapack
collect2.exe: error: ld returned 1 exit status

Apply node that caused the error: CGemv{inplace}(AllocEmpty{dtype='float64'}.0, 1.0, ExpandDims{axis=0}.0, a, 0.0)
Toposort index: 2
Inputs types: [TensorType(float64, shape=(1,)), TensorType(float64, shape=()), TensorType(float64, shape=(1, None)), TensorType(float64, shape=(None,)), TensorType(float64, shape=())]

HINT: Use a linker other than the C linker to print the inputs' shapes and strides.
HINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.
PyTensor version information:

On Windows 11 install using conda:
conda config --set channel_priority strict
conda config --prepend channels conda-forge
conda create -n test_env pytensor m2w64-toolchain

then fix a problem locating g++ by commenting out a line as described here: https://github.com/pymc-devs/pytensor/issues/234

conda list dump:

blas 2.135 mkl conda-forge

blas-devel 3.9.0 35_h85df5b5_mkl conda-forge
bzip2 1.0.8 h0ad9c76_10 conda-forge
ca-certificates 2026.7.22 h4c7d964_0 conda-forge
cons 0.4.7 pyhd8ed1ab_2 conda-forge
distutils-activate-mingw 2.3 py310h57928b3_6 conda-forge
etuples 0.3.10 pyhd8ed1ab_1 conda-forge
filelock 3.32.2 pyhd8ed1ab_0 conda-forge
libblas 3.9.0 35_h5709861_mkl conda-forge
libcblas 3.9.0 35_h2a3cdd5_mkl conda-forge
libexpat 2.8.1 hac47afa_1 conda-forge
libffi 3.5.2 h3d046cb_0 conda-forge
libhwloc 2.11.2 default_hc8275d1_1000 conda-forge
libiconv 1.18 hc1393d2_2 conda-forge
liblapack 3.9.0 35_hf9ab0e9_mkl conda-forge
liblapacke 3.9.0 35_h3ae206f_mkl conda-forge
liblzma 5.8.3 hfd05255_0 conda-forge
libpython 2.3 h57928b3_6 conda-forge
libsqlite 3.53.4 hf5d6505_0 conda-forge
libxml2 2.13.9 h741aa76_0 conda-forge
libzlib 1.3.2 hfd05255_3 conda-forge
llvm-openmp 22.1.8 h4fa8253_0 conda-forge
logical-unification 0.4.7 pyhd8ed1ab_0 conda-forge
m2w64-binutils 2.25.1 5 conda-forge
m2w64-bzip2 1.0.6 6 conda-forge
m2w64-crt-git 5.0.0.4636.2595836 2 conda-forge
m2w64-gcc 5.3.0 6 conda-forge
m2w64-gcc-ada 5.3.0 6 conda-forge
m2w64-gcc-fortran 5.3.0 6 conda-forge
m2w64-gcc-libgfortran 5.3.0 6 conda-forge
m2w64-gcc-libs 5.3.0 7 conda-forge
m2w64-gcc-libs-core 5.3.0 7 conda-forge
m2w64-gcc-objc 5.3.0 6 conda-forge
m2w64-gmp 6.1.0 2 conda-forge
m2w64-headers-git 5.0.0.4636.c0ad18a 2 conda-forge
m2w64-isl 0.16.1 2 conda-forge
m2w64-libiconv 1.14 6 conda-forge
m2w64-libmangle-git 5.0.0.4509.2e5a9a2 2 conda-forge
m2w64-libwinpthread-git 5.0.0.4634.697f757 2 conda-forge
m2w64-make 4.1.2351.a80a8b8 2 conda-forge
m2w64-mpc 1.0.3 3 conda-forge
m2w64-mpfr 3.1.4 4 conda-forge
m2w64-pkg-config 0.29.1 2 conda-forge
m2w64-toolchain 5.3.0 7 conda-forge
m2w64-toolchain_win-64 2.5.0 h57928b3_0 conda-forge
m2w64-tools-git 5.0.0.4592.90b8472 2 conda-forge
m2w64-windows-default-manifest 6.4 3 conda-forge
m2w64-winpthreads-git 5.0.0.4634.697f757 2 conda-forge
m2w64-zlib 1.2.8 10 conda-forge
minikanren 1.0.5 pyhd8ed1ab_1 conda-forge
mkl 2024.2.2 h57928b3_16 conda-forge
mkl-devel 2024.2.2 h57928b3_16 conda-forge
mkl-include 2024.2.2 h57928b3_16 conda-forge
mkl-service 2.5.2 py312h887bc7d_1 conda-forge
msys2-conda-epoch 20160418 1 conda-forge
multipledispatch 0.6.0 pyhd8ed1ab_1 conda-forge
numpy 1.26.4 py312h8753938_0 conda-forge
openssl 3.6.3 hf411b9b_1 conda-forge
packaging 26.3 pyhc364b38_0 conda-forge
pip 26.2.1 pyh8b19718_0 conda-forge
pthreads-win32 2.9.1 h2466b09_4 conda-forge
pytensor 2.27.1 py312hec84d0a_0 conda-forge
pytensor-base 2.27.1 py312hd2403df_0 conda-forge
python 3.12.13 h0159041_0_cpython conda-forge
python_abi 3.12 8_cp312 conda-forge
scipy 1.17.1 py312h9b3c559_1 conda-forge
setuptools 83.0.0 pyh332efcf_0 conda-forge
six 1.17.0 pyhe01879c_1 conda-forge
tbb 2021.13.0 h62715c5_1 conda-forge
tk 8.6.13 h967ab96_3 conda-forge
toolz 1.1.0 pyhd8ed1ab_1 conda-forge
typing_extensions 4.16.0 pyhcf101f3_0 conda-forge
tzdata 2026c h151e31d_0 conda-forge
ucrt 10.0.26100.0 h57928b3_0 conda-forge
vc 14.5 ha367084_41 conda-forge
vc14_runtime 14.51.36247 habf1de7_41 conda-forge
vcomp14 14.51.36247 habf1de7_41 conda-forge
wheel 0.47.0 pyhd8ed1ab_0 conda-forge

Context for the issue:

Found when rewriting/updating a program from PyMC3 with lots of other dependencies so it would be useful to have a fix that works on Windows/conda

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the dot-product example with the Windows conda environment described in the issue, then inspect pytensor/link/c/basic.py and pytensor/link/c/cmodule.py around C compilation and the reported -lblas/-llapack linker arguments. Compare the behavior with and without blas__ldflags; done means the example compiles and evaluates successfully without requiring those workarounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.