# Assertion Error in PyTensor

**URL:** <https://discourse.pymc.io/t/assertion-error-in-pytensor/12086>\
**Category:** Questions\
**Created:** [May 8, 2023, 2:42pm UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086 "2023-05-08T14:42:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaFroeli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/safroeli/32/3501_2.png) [@SaFroeli](https://discourse.pymc.io/u/SaFroeli)\
**Post date:** [May 8, 2023, 2:42pm UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086/1 "2023-05-08T14:42:40Z")

</div>

I am going through the PyTensor tutorial on the [scan mechanism](https://github.com/lamblin/ccw_tutorial/blob/master/Scan_W2016/scan_tutorial.ipynb) and am trying to run the code as given in the tutorial:

```auto
import theano
import theano.tensor as T
import numpy as np

vector1 = T.vector('vector1')
vector2 = T.vector('vector2')

output, updates = theano.scan(fn=lambda a, b : a * b, sequences=[vector1, vector2])

f = theano.function(inputs=[vector1, vector2],
                    outputs=output,
                    updates=updates)

```

This results in an Assertion Error that seems to come from theano:

```auto
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Input In [54], in <cell line: 22>()
     15 output, updates = theano.scan(fn=lambda a, b : a * b, sequences=[vector1, vector2])
     17 # The first output contains the outputs of fn from every timestep concatenated into a tensor. 
     18 # In our case, the output of a single timestep is a scalar so output is a vector where output[i] is the output of the i-th iteration.
     19 # The second output details if and how the execution of the Scan updates any shared variable in the graph. 
     20 # It should be provided as an argument when compiling the Theano function.
---> 22 f = theano.function(inputs=[vector1, vector2],
     23 outputs=output,
     24 updates=updates)

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/compile/function/ __init__.py:337, in function(inputs, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input)
    331 fn = orig_function(
    332 inputs, outputs, mode=mode, accept_inplace=accept_inplace, name=name
    333 )
    334 else:
    335 # note: pfunc will also call orig_function -- orig_function is
    336 # a choke point that all compilation must pass through
--> 337 fn = pfunc(
    338 params=inputs,
    339 outputs=outputs,
    340 mode=mode,
    341 updates=updates,
    342 givens=givens,
    343 no_default_updates=no_default_updates,
    344 accept_inplace=accept_inplace,
    345 name=name,
    346 rebuild_strict=rebuild_strict,
    347 allow_input_downcast=allow_input_downcast,
    348 on_unused_input=on_unused_input,
    349 profile=profile,
    350 output_keys=output_keys,
    351 )
    352 return fn

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/compile/function/pfunc.py:524, in pfunc(params, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input, output_keys)
    519 si = In(
    520 variable=sv, value=sv.container, mutable=False, borrow=True, shared=True
    521 )
    522 inputs.append(si)
--> 524 return orig_function(
    525 inputs,
    526 cloned_outputs,
    527 mode,
    528 accept_inplace=accept_inplace,
    529 name=name,
    530 profile=profile,
    531 on_unused_input=on_unused_input,
    532 output_keys=output_keys,
    533 )

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/compile/function/types.py:1981, in orig_function(inputs, outputs, mode, accept_inplace, name, profile, on_unused_input, output_keys)
   1970 m = Maker(
   1971 inputs,
   1972 outputs,
   (...)
   1978 name=name,
   1979 )
   1980 with config.change_flags(compute_test_value="off"):
-> 1981 fn = m.create(defaults)
   1982 finally:
   1983 t2 = time.time()

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/compile/function/types.py:1836, in FunctionMaker.create(self, input_storage, trustme, storage_map)
   1833 start_import_time = theano.link.c.cmodule.import_time
   1835 with config.change_flags(traceback __limit=config.traceback__ compile_limit):
-> 1836 _fn, _i, _o = self.linker.make_thunk(
   1837 input_storage=input_storage_lists, storage_map=storage_map
   1838 )
   1840 end_linker = time.time()
   1842 linker_time = end_linker - start_linker

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/link/basic.py:266, in LocalLinker.make_thunk(self, input_storage, output_storage, storage_map)
    265 def make_thunk(self, input_storage=None, output_storage=None, storage_map=None):
--> 266 return self.make_all(
    267 input_storage=input_storage,
    268 output_storage=output_storage,
    269 storage_map=storage_map,
    270 )[:3]

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/link/vm.py:1188, in VMLinker.make_all(self, profiler, input_storage, output_storage, storage_map)
   1185 else:
   1186 post_thunk_clear = None
-> 1188 vm = self.make_vm(
   1189 order,
   1190 thunks,
   1191 input_storage,
   1192 output_storage,
   1193 storage_map,
   1194 post_thunk_clear,
   1195 computed,
   1196 compute_map,
   1197 self.updated_vars,
   1198 )
   1200 vm.storage_map = storage_map
   1201 vm.compute_map = compute_map

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/link/vm.py:893, in VMLinker.make_vm(self, nodes, thunks, input_storage, output_storage, storage_map, post_thunk_clear, computed, compute_map, updated_vars)
    890 pre_call_clear = [storage_map[v] for v in self.no_recycling]
    892 try:
--> 893 from theano.link.c.cvm import CVM
    894 except (MissingGXX, ImportError):
    895 CVM = None

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/link/c/cvm.py:13, in <module>
      9 if not config.cxx:
     10 raise MissingGXX(
     11 "lazylinker will not be imported if theano.config.cxx is not set."
     12 )
---> 13 from theano.link.c import lazylinker_c
     15 class CVM(lazylinker_c.CLazyLinker, VM):
     16 def __init__ (self, fgraph, *args, **kwargs):

File ~/anaconda3/envs/vb_model_2/lib/python3.9/site-packages/theano/link/c/lazylinker_c.py:157, in <module>
    153 _logger.info(f"New version {lazylinker_ext._version}")
    155 from lazylinker_ext.lazylinker_ext import * # noqa
--> 157 assert force_compile or (version == get_version())

AssertionError: 

```

I have tried changing the pytensor.config.optimizer settings, but it doesn’t make a difference.  
I’m using python 3.9.12, pytensor 2.11.2, theano 1.1.2, pymc 3.11.4, numpy 1.21.5

---

<div class="post-metadata">

**Author:** ![SaFroeli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/safroeli/32/3501_2.png) [@SaFroeli](https://discourse.pymc.io/u/SaFroeli)\
**Post date:** [May 10, 2023, 3:36pm UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086/2 "2023-05-10T15:36:19Z")

</div>

I have learned that pytensor is a fork of theano for newer PyMC versions, so I updated to a newer version of PyMC (5.0.1), deleted theano and replaced `theano` with `pytensor` in the code. Now it works.

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [May 10, 2023, 3:37pm UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086/3 "2023-05-10T15:37:39Z")

</div>

Just for your reference, the most recent version of PyMC is currently 5.3.1.

---

<div class="post-metadata">

**Author:** ![KS\_19](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ks_19/32/7239_2.png) [@KS\_19](https://discourse.pymc.io/u/KS_19)\
**Post date:** [October 27, 2023, 2:53am UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086/4 "2023-10-27T02:53:18Z")

</div>

[When I try to import pymc and run MCMC, it throws an assertion error, as below:

This is just last 2 lines:let me know if traceback is needed  
167 from lazylinker\_ext.lazylinker\_ext import CLazyLinker, get\_version # noqa  
168 from lazylinker\_ext.lazylinker\_ext import \* # noqa  
 → 170 assert force\_compile or (version == get\_version())

AssertionError:

When i import pymc3, the mcmc runs fine but then arviz does not work

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [October 27, 2023, 11:47am UTC](https://discourse.pymc.io/t/assertion-error-in-pytensor/12086/5 "2023-10-27T11:47:02Z")

</div>

It may be easier to open a new thread and provide the details (e.g., versions of different packages you have installed) there.
