Something wrong in the code from `PyMC 4.0 with labeled coords and dims`

I am trying to run the code given in THIS PAGE, regarding the example: 1st example: rugby analytics.

Running the code gives me an error:

TypeError: You are calling an Aesara function with PyTensor variables.
Starting with PyMC 5.0, Aesara was replaced by PyTensor (see PyMC forked Aesara to PyTensor — PyMC project website).
Replace your import of aesara.tensor with pytensor.tensor.

So, I replaced the line import aesara.tensor as at by the line import pytensor.tensor as at, as suggested.

And now the program stops with saying:

ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_IncSubtensor_serialize
ERROR (pytensor.graph.rewriting.basic): node: Elemwise{add,no_inplace}(AdvancedIncSubtensor{inplace=False, set_instead_of_inc=False}.0, AdvancedIncSubtensor{inplace=False, set_instead_of_inc=False}.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
File “C:\Users\thaly\anaconda3\lib\site-packages\pytensor\graph\rewriting\basic.py”, line 1933, in process_node
replacements = node_rewriter.transform(fgraph, node)
File “C:\Users\thaly\anaconda3\lib\site-packages\pytensor\graph\rewriting\basic.py”, line 1092, in transform
return self.fn(fgraph, node)
File “C:\Users\thaly\anaconda3\lib\site-packages\pytensor\tensor\rewriting\subtensor.py”, line 1197, in local_IncSubtensor_serialize
assert mi.owner.inputs[0].type.is_super(tip.type)
AssertionError

Wouldn’t it be possible for the honored developers to choose once and for all between aesara and pytensor and develop the appropriate packages…?

I don’t understand what is wrong in my directory ...\site-packages\pytensor... which appears in the ERROR lines.

Any hint to make the code running?

@OriolAbril

For the record, the devs did pick a computational backend – it’s pytensor only moving forward. That said, there is a lot of old content out there that was made using theano and aesara. Luckily, it’s all mostly just drop-in replacements.

This particular issue seems to be a new bug related to indexing the same child random variable multiple ways in the same model. It’s quite niche, because you have to 1) define a random variable, 2) perform some intermediate computations on it, then 3) index it in multiple different ways. I ran into it myself yesterday, it seems to be quite fresh.

EDIT: I was unable to reproduce it on newer versions, see below. My testing was initially done on Mac OS running PyMC version 5.0.1

As an interim solution, you can avoid the error by compiling the model to JAX or Rust. JAX is only an option if you’re not on windows, but if you aren’t you can add:

import pymc.sampling

with pm.Model(coords=coords) as rugby_model:
    # nothing changes in model definition

    rugby_idata = pymc.sampling_jax.sample_numpyro_nuts()

If you are on windows, you can use nutpie to sample with rust. Remove the pm.sample line entirely, then outside the model context compile to rust and sample:

with pm.Model(coords=coords) as rugby_model:
    # nothing changes in model definition
    # don't run pm.sample!

import nutpie
numba_mod = nutpie.compile_pymc_model(rugby_model)
rugby_idata = nutpie.sample(numba_mod)

I believe the API for accessing these alternative samplers has improved or will be improved soon, but I’m still on 5.0.1. These are all just work-arounds, because you did stumble on a bug. But they’re also nice to know, because they are often much faster than the C backend!

EDIT: After some more tests, I could not reproduce the bug on my Windows computer running pymc 5.0.2. What version/platform are you running?

1 Like

Eventually I was able to solve this problem, but in a weird way…

I first created a new environment (distinct from my base one), say PyMC_env:

(base) PS C:\Users\thaly> conda create -c conda-forge -n pymc_env “pymc>=4”

(Don’t ask me where I found the statement used… I don’t remember)

Then I opened a Jupyter notebook and change the Kernel from Python 3 (ipkernel) which in the default in my installation, to pymc_env and than ran the code shown in the Oriol Unraveled web page, case: 1st example: rugby analytics, using statement import pytensor.tensor as at instead of import aesara.tensor as at, as I already explained above.

Now, running the code generates a series of pop-up error warnings, just as the one displayed in the image below:

Screenshot 2023-04-02 145333

I just click on the OK button and let the things go. Amazingly, I counted 8 such pop-ups appearing successively, displaying each time a different try_blas_*****.exe line (I mean, the characters in ***** are always changing…).

I click on OK every time, until the pop-ups stop appearing…

Then, the code ends up executing to completion, and I am able to get the plots by the usual az.plot_trace(rugby_idata)

I do not understand anything. It ends up working, that’s good, but what do these pop-up windows mean, for example try_blas_on24wqw9.exe and all the others?

Why do they appear, and why do they stop appearing?

I have:

Versions:
Running on PyMC v5.1.1
Running on ArviZ v0.15.1
Running on Numpy v1.24.2
Running on Pandas v1.5.3
Running on Xarray v2023.2.0
Running on Matplotlib v1.24.2
And:
Selected Jupyter core packages…
IPython : 7.31.1
ipykernel : 6.19.2
ipywidgets : 7.6.5
jupyter_client : 7.4.8
jupyter_core : 5.1.1
jupyter_server : 1.13.5
jupyterlab : 3.3.2
nbclient : not installed
nbconvert : 5.4.1
nbformat : 5.7.0
notebook : 6.4.12
qtconsole : 5.3.2
traitlets : 5.7.1

That install line came from the installation instructions, so you’re definitely on the right path.

It seems like something went wrong in installation. You are getting errors related to BLAS, which is a linear algebra acceleration library used by numpy and scipy, as well as PyMC. If you had previously installed PyMC directly into your base environment, I’d completely uninstall conda, reinstall conda, then reinstall pymc into a new environment. It’s never recommended to install anything into the base env.

Given how exotic your error is, I’d do this regardless.

One other thing; you can avoid switching the kernel in Jupyter by activating the relevant environment before you launch notebook.

1 Like