-
Notifications
You must be signed in to change notification settings - Fork 10
/
measuring-GPU-utilization.py
94 lines (69 loc) · 3.1 KB
/
measuring-GPU-utilization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Databricks notebook source
!pip install py3nvml
import pandas as pd
import numpy as np
import transformers
import mlflow
import torch
import random
import py3nvml
# COMMAND ----------
from huggingface_hub import snapshot_download
# Download the model snapshot from huggingface
snapshot_location = snapshot_download(repo_id="textattack/bert-base-uncased-imdb")
# COMMAND ----------
class BertSentimentClassifier(mlflow.pyfunc.PythonModel):
def load_context(self, context):
self.tokenizer = transformers.AutoTokenizer.from_pretrained(context.artifacts['repository'])
self.model = transformers.AutoModelForSequenceClassification.from_pretrained(context.artifacts['repository']).to('cuda')
self.model.eval()
def predict(self, context, model_input):
import random
message = model_input["review"][0]
encoded_input = self.tokenizer.encode(message, return_tensors='pt', max_length=512, truncation=True).to('cuda')
with torch.no_grad():
output = self.model(encoded_input)[0]
_, prediction = torch.max(output, dim=1)
#Check GPU memory usage and utilization rates and print the results if a random condition is met.
if random.random() < 0.10:
py3nvml.py3nvml.nvmlInit()
device = py3nvml.py3nvml.nvmlDeviceGetHandleByIndex(0)
info = py3nvml.py3nvml.nvmlDeviceGetMemoryInfo(device)
util = py3nvml.py3nvml.nvmlDeviceGetUtilizationRates(device)
print(f"Percentage of GPU memory used: {info.used / info.total * 100:.2f}%, GPU Utilization: {util.gpu:.2f}%", flush=True)
py3nvml.py3nvml.nvmlShutdown()
return "Positive" if prediction == 1 else "Negative"
# COMMAND ----------
# Log the model with its details such as artifacts, pip requirements and input example
from mlflow.models.signature import ModelSignature
from mlflow.types import DataType, Schema, ColSpec
# Define input and output schema
input_schema = Schema([ColSpec(DataType.string, "review")])
output_schema = Schema([ColSpec(DataType.string, "label")])
signature = ModelSignature(inputs=input_schema, outputs=output_schema)
# Define the input example
input_example = pd.DataFrame({"review":["I love this movie."]})
# Log the model with details such as artifacts, pip requirements, input example, and signature
with mlflow.start_run() as run:
mlflow.pyfunc.log_model(
"model",
python_model=BertSentimentClassifier(),
artifacts={'repository' : snapshot_location},
pip_requirements=["torch", "transformers", "py3nvml"],
input_example=input_example,
signature=signature,
)
# COMMAND ----------
# Register model in MLflow Model Registry
result = mlflow.register_model(
"runs:/"+run.info.run_id+"/model",
"bert-base-uncased-imdb"
)
# COMMAND ----------
# Load the logged model
loaded_model = mlflow.pyfunc.load_model(f"models:/{result.name}/{result.version}")
# COMMAND ----------
# Make a prediction using the loaded model
input_example=pd.DataFrame({"review":["I love this movie."]})
print(loaded_model.predict(input_example))
# COMMAND ----------