"<<! BUG IN FGRAPH.REPLACE OR A LISTENER !>>" during mcmc sampling, but sampling finishes correctly

Hi!

I created a custom distribution using a custom likelihood written with pytensor functions only (detailed likelihood implementation is the logp_ddm function here). I built a basic model with PyMC using this distribution and tried to perform some basic sampling like this:

with pm.Model() as ddm_pymc:
    v = pm.Uniform("v", lower=-10.0, upper=10.0)
    a = pm.HalfNormal("a", sigma=2.0)
    z = pm.Uniform("z", lower=0.01, upper=0.99)
    t = pm.Uniform("t", lower=0.0, upper=1.0, initval=0.1)

    ddm = DDM("ddm", v=v, a=a, z=z, t=t, observed=dataset.values)

    ddm_pymc_trace = pm.sample()

Sampling proceeds until it finishes, but a lot of warnings and error messages show up before sampling actually starts. Below is the detailed error message. It seems to indicate an error with pytensor with type mismatch (float32 vs float64). Forcing pytensor.config.floatX = "float32" indeed solves the problem, but the likelihood should not produce any float32 since it’s implemented with pytensor (the type of data is also double checked to be float64). Any tips on how to debug this?

Thanks!

Detailed error message:

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

<<!! BUG IN FGRAPH.REPLACE OR A LISTENER !!>> <class 'TypeError'> Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)). local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): Rewrite failure due to: local_mul_switch_sink
ERROR (pytensor.graph.rewriting.basic): node: Mul(Eq.0, Switch.0)
ERROR (pytensor.graph.rewriting.basic): TRACEBACK:
ERROR (pytensor.graph.rewriting.basic): Traceback (most recent call last):
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/rewriting/basic.py", line 1960, in process_node
    fgraph.replace_all_validate_remove(  # type: ignore
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 626, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason=reason, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/features.py", line 571, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False, **kwargs)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/graph/fg.py", line 491, in replace
    new_var = var.type.filter_variable(new_var, allow_convert=True)
  File "/Users/yxu150/HSSM/.venv/lib/python3.9/site-packages/pytensor/tensor/type.py", line 272, in filter_variable
    raise TypeError(
TypeError: Cannot convert Type Matrix(float32, shape=(2, ?)) (of Variable Switch.0) into Type Matrix(float64, shape=(2, ?)). You can try to manually convert Switch.0 into a Matrix(float64, shape=(2, ?)).

Multiprocess sampling (4 chains in 4 jobs)
NUTS: [t, a, z, v]

 100.00% [8000/8000 00:08<00:00 Sampling 4 chains, 0 divergences]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 9 seconds.

In terms of sampling you don’t have to worry, it just means that rewrite couldn’t be applied because it returned an invalid data type. We should however fix it. Can you confirm it’s present on the most recent version of PyMC (5.6.0)?

Thank you so much @ricardoV94! Yes, I can confirm that it’s present on the most recent PyMC 5.6.0. Should I create an issue for PyMC or Pytensor?

PyTensor, but it would be good if you could simplify it quite a bit and make it a standalone reproducible snippet