Hitting a weird error to do with RNGs in Scan in a custom function inside a Potential

Fan-fxxxxxing-tastic! Thank you @ricardoV94 - this is some deep expertise!

You’re dead right, I tried a new version of test2, passing in m_mu and m_s and passing them into grad as consider_constant - is that right btw? Seems to work

def test4_scan(f_inv_x, x, consts):
    """4. Focus on a single scalar cost `c[i, 0]` and `strict=True`: 
    Pass precursors RVs
    """
    n = f_inv_x.shape[0]
    idx = pt.arange(n, dtype="int32")
    
    def get_grads(i, c, w, *args):
        return tg.grad(cost=c[i, 0], wrt=[w], consider_constant=[*args])

    grads, _ = pytensor.scan(get_grads, 
                sequences=idx, non_sequences=[f_inv_x, x, *consts],
                n_steps=n, name="get_grads", strict=True)
    
    return grads

t4 = test4_scan(mdl.y_c, mdl.y, consts=[mdl.m_mu, mdl.m_s]).eval()

And now I dont need y_c_vals = mdl.replace_rvs_by_values([mdl.y_c])[0] … happy days!