Sparse Gradient

I was able to run your code as-is with the MAP optimization finishing in 2 seconds on a 2020 Macbook Pro with PyMC3 version 3.11.4, SciPy version 1.7.3 and Theano version 1.1.2. I noticed that this Github issue appears to be related and mentions that computing test values may trigger the dense computation. However, I was able to run the script successfully with the “raise” setting for theano.config.compute_test_value which should have reproduced your problem.