PyTorch in Google Cloud Functions

PyTorch in Google Cloud Functions

My CloudNewsReposted, after being pretty dormant for a while, I needed a new home and while I am waiting to get a new harddrive to run such stuff at home, I opted to try and run the script on Google Cloud

Let’s start with the trickiest part, and maybe the one which brought you here: PyTorch.

Getting PyTorch to run took maybe most of the time. I found some tutorials but installing of torch always failed. The requirements.txt entry which worked best was this one:


In case that the version ever changes, please take a look at the error message, as it will show you which versions are available to use.

When it comes to sizing the specifications you need can be a bit tricky, because trying to execute a torch based cloud function with less than 4 GB of memory will not work and you easily get a:

joerg@cloudshell:~ (xxxxx)$ gcloud functions call CloudNewsReposted
error: 'Error: memory limit exceeded.'

But 4 GB seems to do the trick.

So how does the cloud function look like:

With 4 GB ram I had no issues so far, also keep in mind that 4GB will come with 4.8 GHz CPU power according to the pricing calculator. Also make sure to set the “Minimum instances” to zero (0), otherwise GCP will always have one instance active, which adds to the monthly base costs.

Small issue I faced is the time out of the gcloud http request, the server script will definitely run longer than 5 minutes, so you might not see the result:

joerg@cloudshell:~ (xxxxx)$ gcloud functions call CloudNewsReposted
ERROR: gcloud crashed (ReadTimeout): HTTPSConnectionPool(host='', port=443): Read timed out. (read timeout=300)

While creating this blog post and figuring out the details, I of course also created a blog post, enjoy reading:

The rest of the setup is quite straight forward, it requires a and a requirements.txt to be provided.

That’s it for today, please leave a 👏 and be excellent to each other.