Semi-Managed Jupyter Lab With Access To Google Cloud Resources



Just imagine the following use case:

You are opening a link to private Jupyter Lab in the cloud that authenticates you with your GCP account. From the Jupyter Lab, you are uploading data from the private file on Google Cloud Storage to the BigQuery (right from the Lab). Later, you are executing several queries to BQ and visualizing data right from the Jupyter Lab. Finally, you are executing the training of the model with TensorFlow and the data from BQ on the GPU attached to the Jupyter Lab. Now you are uploading the trained model back to GCS.

Moreover, all this without leaving managed Jupyter Lab. Sounds like magic, doesn’t it? Now let me show the screenshot of what you will have by the time you finished reading this article:

I am going to guide you through the setup process step by step with many pictures!

First, let’s start with…

Creating A GCE Instance With Semi-Managed Jupyter Lab

The first thing that you need to do is to create an instance with managed Jupyter Lab. Let’s go to the main GCE page (if you don’t have a GCP account yet you can sign up for a free trial here).

Your page should look like this:

Now it’s time to press the “Create” button. You should see a dialog of instance creation, and it looks like this:

I know it has a lot here, but do not worry, I am going to walk you step by step. First, you need to set a name of the instance and the region. In my case, it is “jupiter-lab” and “us-central1” (set by default). The name is something that you can specify any. The region, on the other hand, depends on the GPU that you are planning to use. BTW, if you are not planning to use the GPU with the instance, you can skip the following section and jump straight to the OS pick section.

Attaching GPU To The Instance

Not all GPUs are available in all regions. General rule of thumb, region that you pick should be the region that is closest to you and has the GPU you need (and has the required quotas). List of all available GPUs on GCP with the regions where they are available is here. In my case, I want to use Tesla P100, and I’m on the west coast. Now, looking on the doc …

I can see that I need to use us-west1-b. So now I can set details for my new instance like this:

Okay, we now know that we have picked the right region. However, we also need to be sure that we have the right quotas to create an instance with P100 in that region. Quotas are protecting you from using more resources that you are intended to. To check and edit your quotas go to the following page. It should look like this:

Now, click on the “All metric” drop-down and select “none”:

In the search box write “P100” and select “NVIDIA P100 GPUs”:

Be careful, do not select “Preemptible”, unless you intend to use preemptible instances. At this moment your list of quotes should look like this(with only P100):

Now we can select the region (yes quotas are per region). Click on “Locations” and pick “None”:

From the list, scroll down and find the location that you need (in my case it’s us-west1) and select it:

At this point your screen should look like this:

First of all, if you already have quotas (in my case quota is 3, and I am using 0), you can skip the rest of this part. If your quota is 0 you need to check the checkbox next to the quota item and press the “Edit quotas” button:

On the right side you see the pop-up with the fields that you need to fill in:

Press next and fill out how much GPUs do you need:

After filling in all the information, you can submit the request and wait for the confirmation letter from Google. I think it might take ~1 business day to fulfill the request. When the request is fulfilled you will get the magic confirmation letter:

Now, when we are finally done with the quota, we can go back to our instance creation dialog and press the “Customize” button:

You should see something that looks like this:

In here we can start adjusting values, I’m going to use 4 cores instance with 15 GB of memory:

I would strongly advise you to set CPU type from “automatic” to SkyLake to get the fastest possible CPU (unless you are planning to use K80 GPU since SkyLake is NOT available with K80):

Now, as for the GPU, we are going to use one P100 GPU:

So GPU type, respectively P100:

The end screen should look like the following one:

Picking The OS

This step is very very important. The thing is, only some of GCP OS comes with pre-configured, and instance managed Jupyter Lab that is automatically running. Default OS is not one of them, therefore pressing the “change” button:

You now should see something like this:

Scroll down until you see a “Deep Learning” images:

All of them (Base/PyTorch/TensorFlow) have pre-configured and managed Jupyter Lab. The main difference in-preinstalled DL frameworks. The Base has only necessary frameworks like numpy/sklearn/etc. TensorFlow has everything that Base have plus the TensorFlow. PyTorch, respectively has everything that Base have plus the PyTorch.

In my example, I am going to use “Deep Learning image: TensorFlow 1.10.1 m7 CUDA 9.2”. Also on the same page, I would advise you to:

  • change the “Boot disk type” to SSD and
  • set disk size to 100 GB

so the setting should look like this:

We now ready to press select and go back to the instance creation screen. The last thing that needs to be made is permissions adjustment for the instance that we are about to create. I’m going to use my Jupyter Lab with many different resources on GCP; therefore I’m going to grant full access to GCP APIs to my instance like this:

We are finally done with preparing our instance. Here is how my instance setting looks at this moment:

So we are finally ready to press the “Create” button.

IMPORTANT: It is possible that the zone does not have requested GPUs available at the moment. In this case, you will see an error during the creation of the instance. In such case, you need to go back to the square one and re-create instance in a different region where required GPUs are available.

It will take some time for the instance to be created. Let’s wait till the instance is fully created.

Getting The Jupyter Lab Link

As soon as your instance has been created, you can get the link to the Jupyter Lab. Here is how you can do this. First, you need to open Cloud Shell by clicking on the icon at the top-right corner:

By clicking on the button, you should see actual shell opened on the screen:

Now you need to execute only one small command in the shell:

same command for copy-pasting:

gcloud compute ssh jupyter-lab --zone=us-west1-b -- -L 8080:localhost:8080

Execution might take some time. After the execution, you should see the following:

Press yes and wait. It will take 2–3 minutes to install Nvidia drivers and reboot the instance. Luckily this needs to be done only once. So let’s wait…

2 minutes later, let’s execute the same command, now it should give the different result:

We are almost ready! There is a WebPreview button in the cloud shell, let’s press it:

And press “Preview on port 8080”:

and voila, here is your Jupyter Lab running on the instance with GPU, that has access to all the GCP resources and accessible via the simple link (guarded by the Google Cloud Authentification):

You can try to open the same URL in incognito mode:

BigQuery Integration With Jupyter Lab

Starting with the release M7 of Deep Learning images they have out-of-the-box integration with the BigQuery. Let’s see this in action. First, I am going to create the new dataset in BigQuery that I will use later in this demo. Dataset is called “examples”. The command to do so:

!bq --location=US mk -d --default_table_expiration 3600 --description "Dataset for examples." examples

Actual execution results:

Now, let’s do something more interesting, let’s create a new table in the dataset from the data that I have in CSV file stored in my Google Cloud Storage. Here is the command and execution:

!bq --location=US load --autodetect --source_format=CSV examples.house_pricing gs://b0noi-tmp/house-prices.csv

Now we are ready to query data from the table and experiment with the data in the Jupyter Lab. Here is where the integration kicks in. To do so first, we need to load BigQuery module like this:

%load_ext google.cloud.bigquery

So now we can load data into the memory:

This will create the variable “house_pricing” with the data from the BigQuery:

Amazing, isn’t it? Now, let’s use TensorFlow to train a linear regressor on our instance with this data. I know it is a toy example for powerful GPU like P100, but I’m sure you will be building a way more complex model with your data.

The best part that our Deep Learning images come with different tutorials and TensorFlow source code preinstalled:

So we can look at the existing example of the linear regressor and make your own. It can be found in the following directory:

src/tensorflow/tensorflow/examples/get_started/regression/linear_regression.py

After experimenting with the example I have created a small snippet for training:

I probably have made several mistakes there since it has not converged(with a small amount of data that I had in my CSV file it is probably expected, though I might have a bug somewhere too:) ) but the point of the exercise to show how quickly you can use data from the BigQuery:

Now let’s upload our model back to GCS, right from the Jupyter Lab:

It is just amazing how simple you can use all GCP resources right from the Jupyter Lab!

Work is done, it is almost time for the beer, however first…

Do Not Forget To Stop The Instance!

No need to run the instance constantly. You can stop it so it will not consume your money and restart it later (you will have to do Cloud Shell step again to get the link). So let’s stop the instance:

By stopping the instance, all your data (including the notebooks) will be safely persisted until the next time.

But if I no longer need a GPU, simply want to get access to my notebooks?

I’m glad you’ve asked:) After stopping the instance you can always detach the GPU. This can be done through several simple steps:

  1. click on the stopped instance:

2. click the “edit” button on the top:

3. now you can select the“customize” button:

now you should see the familiar instance config dialog:

4. you can now set Number of GPUs to “none”

And press the “save” button. That is it! You now have the same instance with all your data, without the GPU attached!

I hope that you have enjoyed reading this article. If so, please clap for it, share it, follow me and/or leave a comment:)! See you at GCP!

Last modified Sep 15 2018

Source: Deep Learning on Medium