扩展磁盘-GCP VM


[TOC]

Weeks ago, I created a GCP VM for the VPN and Jupyter notebook server, however I used the minimum spec in order to save the cost. The disk was only 10G, which could not meet the requirements of Pytorch installation, so I got to extend the capacity of my vm disk to which is 20G in my case.

The Pytorch is required in transformers.GPT2LMHeadModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 # Import the necessary libraries
2 from transformers import GPT2Tokenizer, GPT2LMHeadModel
3
4 # Load the pre-trained GPT-2 tokenizer and model
5 tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
6 model = GPT2LMHeadModel.from_pretrained('gpt2')
7
8 # Set the model to evaluation mode
9 model.eval()
10
11 # Define a prompt for the model to complete
12 prompt = input("You: ")
13
14 # Tokenize the prompt and generate text
15 input_ids = tokenizer.encode(prompt, return_tensors='pt')
16 output = model.generate(input_ids, max_length=50, do_sample=True)
17
18 # Decode the generated text and print it to the console
19 generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
20 print("AI: " + generated_text)

Install Pytorch

Installation

Refer to PyTorch

1
pip3 install torch torchvision --no-cache-dir

By default, pip will download all the packages into memory instead of the disk for installation, use --no-cache-dir to avoid downloading them into memory.

VERIFICATION

To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.

1
2
3
import torch
x = torch.rand(5, 3)
print(x)

The output should be something similar to:

1
2
3
4
5
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])

Extend disk of GCP VM

Refer to Increase the size of a persistent disk


扩展磁盘-GCP VM
https://blog.excelsre.com/2023/06/11/kuo-zhan-ci-pan-gcp-vm/
作者
Felix Yang
发布于
2023年6月12日
许可协议