# PYMC3 within an Azure Function - Timing out (30mins+) at pm.sample() stage

**URL:** <https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641>\
**Category:** Questions\
**Created:** [August 17, 2020, 3:07pm UTC](https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641 "2020-08-17T15:07:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thalantyr](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Thalantyr](https://discourse.pymc.io/u/Thalantyr)\
**Post date:** [August 17, 2020, 3:07pm UTC](https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641/1 "2020-08-17T15:07:45Z")

</div>

Hi there,  
I have been using PYMC for a while and have never had any had any problems setting up and managing environments for it until I have recently wanted to deploy it within an Azure Function.

I have replicated the environment locally and the code works fine (and has done for ages under similar environments too); however, I am at a loss in how to get it to work on Azure.

The section of code is based upon [https://docs.pymc.io/notebooks/rugby\_analytics.html](https://docs.pymc.io/notebooks/rugby_analytics.html) and is as follows:

```
  model = pm.Model()
  with pm.Model() as model:
    home = pm.Flat('home')
    tau_att = pm.Gamma('tau_att', .1, .1)
    tau_def = pm.Gamma('tau_def', .1, .1)
    intercept = pm.Flat('intercept')

    #team-specific parameters
    atts_star = pm.Normal("atts_star", 
                            mu=0.0, 
                            tau=tau_att, 
                            shape=num_teams)
    defs_star = pm.Normal("defs_star", 
                            mu=0.0, 
                            tau=tau_def, 
                            shape=num_teams)

    atts = pm.Deterministic('atts', atts_star - tt.mean(atts_star))
    defs = pm.Deterministic('defs', defs_star - tt.mean(defs_star))
    home_theta = tt.exp(intercept + home + atts[home_team] + defs[away_team])
    away_theta = tt.exp(intercept + atts[away_team] + defs[home_team])

    home_goals = pm.Poisson('home_goals', mu=home_theta, observed=observed_home_goals)
    away_goals = pm.Poisson('away_goals', mu=away_theta, observed=observed_away_goals)
  
  logging.info("Running PYMC Process")

  with model:
    # step = pm.Metropolis()
    # trace = pm.sample(2000,step=step, cores=1)
    trace = pm.sample(1000, tune=1000, cores=1)

```

When I run this locally it completes in around 11 seconds - _(Sampling 2 chains for 1\_000 tune and 1\_000 draw iterations (2\_000 + 2\_000 draws total) took 11 seconds.)._

When I run this within an Azure Function It basically gets to pm.sample() before timing out / never returning, ie:

![Screenshot 2020-08-17 at 15.19.38](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/6/6122ebf835d11b6d0d6c2101aed7657ab525da67.png)

I have tried running with different step functions, num cores, draws, chains, different versions of requisites & PYMC3 versions and have had no luck.

I am currently using Python 3.7 on Ubuntu 16.04 x64 and have added a bash script to the build pipeline to add pre-reqs:

```
sudo apt install libatlas-base-dev
sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git graphviz
sudo pip install Theano

sudo apt-get install g++-4.9

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++
pip install --target="./.python_packages/lib/site-packages" -r requirements.txt

```

requirements.txt:  
azure-functions  
scipy==1.3.1  
snowflake-connector-python==2.2.2  
snowflake-sqlalchemy==1.2.2  
SQLAlchemy==1.3.7  
pandas==1.0.1  
sklearn==0.0  
matplotlib==3.1.1  
numpy==1.16.4  
Theano==1.0.5  
Cython==0.29.16  
pymc3==3.9.3  
statsmodels==0.10.1

I get no other import errors / warnings and am now completely out of ideas, any help will be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Thalantyr](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Thalantyr](https://discourse.pymc.io/u/Thalantyr)\
**Post date:** [August 17, 2020, 8:13pm UTC](https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641/2 "2020-08-17T20:13:31Z")

</div>

Update: Pip installing the above libraries in requirements.txt, and using the exact same code, works perfectly on a fresh Google Collab setup:

Auto-assigning NUTS sampler…  
INFO:pymc3:Auto-assigning NUTS sampler…  
Initializing NUTS using jitter+adapt\_diag…  
INFO:pymc3:Initializing NUTS using jitter+adapt\_diag…  
Multiprocess sampling (4 chains in 4 jobs)  
INFO:pymc3:Multiprocess sampling (4 chains in 4 jobs)  
NUTS: [defs\_star, atts\_star, intercept, tau\_def, tau\_att, home]  
INFO:pymc3:NUTS: [defs\_star, atts\_star, intercept, tau\_def, tau\_att, home]  
Sampling 4 chains for 1\_000 tune and 2\_000 draw iterations (4\_000 + 8\_000 draws total) took 32 seconds.  
INFO:pymc3:Sampling 4 chains for 1\_000 tune and 2\_000 draw iterations (4\_000 + 8\_000 draws total) took 32 seconds.

Would be interested if anyone has ever got this working within an Azure Function as there must be something weird going on. Thanks.

---

<div class="post-metadata">

**Author:** ![aseyboldt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aseyboldt/32/5795_2.png) [@aseyboldt](https://discourse.pymc.io/u/aseyboldt)\
**Post date:** [August 18, 2020, 8:25am UTC](https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641/3 "2020-08-18T08:25:26Z")

</div>

This sounds infuriating 🙂  
I don’t have any idea what might be causing this, but if you want to debug further you could try playing with theano options a bit.

If you set `theano.config.mode = "FAST_COMPILE"` theano will not use a compiler at all. It should be _much_ slower, but if this samples fine then we at least know that the problem is related to that.

You could also try to install pymc using conda-forge and see if that changes anything.

---

<div class="post-metadata">

**Author:** ![Thalantyr](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Thalantyr](https://discourse.pymc.io/u/Thalantyr)\
**Post date:** [August 18, 2020, 9:41am UTC](https://discourse.pymc.io/t/pymc3-within-an-azure-function-timing-out-30mins-at-pm-sample-stage/5641/4 "2020-08-18T09:41:26Z")

</div>

Thanks for the reply!

I did think it was something to do with the compiler, I have tried several ways to install them using the bash script in the past and the above is just my latest attempt.

Using ‘FAST\_COMPILE’ (I had read about this but didn’t try it for some reason) PYMC3 does actually sample fine now but pretty slow, which is expected:

![Screenshot 2020-08-18 at 10.38.24](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/c/c4c68152a1fa6c823804f600a54091d66a6130b1.png)

I’ll try via conda-forge and see if that changes anything and will also look into seeing if I can install / point to the compilers any better. At least this works now, thanks!
