[HMC] OverflowError: Python int too large to convert to C long



I am using HMC to sample parameters for my model. However, I got a very strange error as follows.

Traceback (most recent call last):
  File "run.py", line 159, in <module>
    trace = m.sample(n=args.n_sample, tune=args.n_tune, step='HamiltonianMC', target_accept=args.target_accept)
  File "/scratch/cs/emom_gdm/emom/model.py", line 104, in sample
    return pm.sample(n, tune=tune, step=s, cores=2)
  File "/home/zhangg1/.local/lib/python3.6/site-packages/pymc3/sampling.py", line 449, in sample
    trace = _mp_sample(**sample_args)
  File "/home/zhangg1/.local/lib/python3.6/site-packages/pymc3/sampling.py", line 1002, in _mp_sample
    trace.record(draw.point, draw.stats)
  File "/home/zhangg1/.local/lib/python3.6/site-packages/pymc3/backends/ndarray.py", line 240, in record
    data[key][self.draw_idx] = val
OverflowError: Python int too large to convert to C long

I added a print statement print(key, val) before the line that causes this error, and found that it is due to the unusually large step size.

accept 0.0
energy_error -inf
energy 5308.407877548653
accepted False
step_size 5.504611954890628e-20
step_size_bar 2.097985346224451e-14
tune True
diverging True
path_length 0.5927896879687209
n_steps 10768964149090490368

Could you give me some advice about this? I have checked the document for HMC and haven’t seen any argument to limit the size of the steps.
Thanks for your help.


That’s pretty strange - what system are you on?


I am running the program in a server with CentOS Linux release 7.6.1810.


I probably find the reason, tho not solve it yet.
While HMC checks the energy of every point calculated by the integrator and throws an error in the case of infinite energy, it still accepts points of inf energy as its samples. Because of an infinite energy, HMC fails to accept any subsequent sample, and keeps reducing step size to find a more precise point, leading to the overflow.


Thanks for reporting back - I think I met similar error once but not able to reproduce it. Could you find a short example?


I came across this in my project, which is quite complicated…I don’t know how to reproduce this with simplified code. I will report back if I can.


I located the problem, which is shown in the following image pointed by the red arrow. It turns out HMC gets stuck at the starting sample and diverges at every proposal. Do you have any idea about how to make the integrator converge without changing the model?


For your use case, you should first try: Frequently Asked Questions