UnboundLocalError: local variable 'diverging' referenced before assignment

I am trying to do the BEST (Bayesian Estimation Supersedes the t-Test) analysis using two 2d-t-distributions, for a population of objects with two parameters, width and fluence. For testing purpose I am trying the data with only one 2d-t-distribution. Below is my model:

with pm.Model() as test2d:

nu = pm.Exponential('nu_minus_one', 1/29.) + 1

group1_mu = pm.Normal('group1_mu', np.array([group1_log_width_mean,group1_log_fluence_mean]), 
               np.array([group1_log_width_std,group1_log_fluence_std]), shape=2)

group1_sd_dist = pm.HalfCauchy.dist(beta=2.5, shape=2)
group1_chol_packed = pm.LKJCholeskyCov('group1_chol_packed', n=2, eta=1, sd_dist=group1_sd_dist)
group1_chol = pm.expand_packed_triangular(2, group1_chol_packed)

group1_likelihood = pm.MvStudentT('group1_likelihood', nu=nu, mu=group1_mu, chol=group1_chol, observed=y)

with test2d:
    db_test2d = pm.backends.Text('test2d')
    trace_test2d = pm.sample(5000, tune=500, cores=1, trace=db_test2d)

However, I received the error:

    UnboundLocalError                         Traceback (most recent call last)
    <ipython-input-14-aa323992271f> in <module>
          1 with test2d:
          2     db_test2d = pm.backends.Text('test2d')
    ----> 3     trace_test2d = pm.sample(5000, tune=500, cores=1, trace=db_test2d)

    /usr/local/lib/python3.7/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, **kwargs)
        487             _log.info("Sequential sampling ({} chains in 1 job)".format(chains))
        488             _print_step_hierarchy(step)
    --> 489             trace = _sample_many(**sample_args)
        490 
        491     discard = tune if discard_tuned_samples else 0

    /usr/local/lib/python3.7/site-packages/pymc3/sampling.py in _sample_many(draws, chain, chains, start, random_seed, step, **kwargs)
        537             step=step,
        538             random_seed=random_seed[i],
    --> 539             **kwargs
        540         )
        541         if trace is None:

    /usr/local/lib/python3.7/site-packages/pymc3/sampling.py in _sample(chain, progressbar, random_seed, start, draws, step, trace, tune, model, **kwargs)
        603     try:
        604         strace = None
    --> 605         for it, (strace, diverging) in enumerate(sampling):
        606             if it >= skip_first:
        607                 trace = MultiTrace([strace])

    /usr/local/lib/python3.7/site-packages/tqdm/std.py in __iter__(self)
       1106                 fp_write=getattr(self.fp, 'write', sys.stderr.write))
       1107 
    -> 1108         for obj in iterable:
       1109             try:
       1110                 yield obj

    /usr/local/lib/python3.7/site-packages/pymc3/sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
        708                 strace.record(point)
        709                 diverging = False
    --> 710             yield strace, diverging
        711     except KeyboardInterrupt:
        712         strace.close()

    UnboundLocalError: local variable 'diverging' referenced before assignment

As I have not previously encountered this error, I am wondering does anyone have any idea?

Hi David,
Have you tried sampling with:

with test2d:
    trace_test2d = pm.sample(5000, tune=500, cores=1)

i.e without pm.backends.Text('test2d') and the trace argument in pm.sample