SDV icon indicating copy to clipboard operation
SDV copied to clipboard

How to make PARSynthesizer sampling use cuda?

Open srini-ar opened this issue 1 year ago • 2 comments

Environment details

  • SDV version: 1.20.1
  • Python version: 3.12.2
  • Operating System: Ubuntu 25.04
  • Instance Type: GCP - g2-standard-16 (16 vCPUs, 64 GB Memory, 1 NVIDIA L4 GPU)

Problem description

I'm experimenting with SDV to generate Sequential Data. For this, I'm using a sample datset from Kaggle for my experiments (download link)

I observed that GPU was getting used during fit (training phase), but not during sample (generation) phase, due to which data generation is very slow

What I already tried

I have the below 3 files:

  1. train.py - to train the PARSynthesizer model
  2. generate.py - to generate synthetic data from the already trained model
  3. monitor.py - to monitor the resorce usage of GPU

train.py

import pandas as pd

print(sdv.version.public)

data = pd.read_csv('/home/srini_gcp_20250512/poc/data/training/stocks_data_2020_till_2020_09_03.csv')
print(data.shape)
data.head()
print(data['scrip_id'].nunique())
print(data.groupby('scrip_id').count())

from sdv.metadata import Metadata
metadata = Metadata.detect_from_dataframe(data=data)
metadata.set_sequence_key(column_name='scrip_id')
metadata.set_sequence_index(column_name='timestamp')


from sdv.sequential import PARSynthesizer
import time
import math

start = time.time()
epochs = 128
cuda = True
synthesizer = PARSynthesizer(metadata, cuda=cuda, epochs=epochs)
synthesizer.fit(data)
t = time.time() - start

with open("/home/srini_gcp_20250512/poc/data/log.log", "a") as file:
    file.write(f'Training completed - cuda-{cuda}, epochs-{epochs}, {math.ceil(t/60)} min\n')
	
synthesizer.save(filepath='/home/srini_gcp_20250512/poc/models/timeseries.pkl')

generate.py

import sdv
import pandas as pd
from sdv.sequential import PARSynthesizer
import time
import math

print(sdv.version.public)
synthesizer = PARSynthesizer.load(filepath='/home/srini_gcp_20250512/poc/models/timeseries.pkl')

start = time.time()
num_sequences=10
synthetic_data = synthesizer.sample(num_sequences=num_sequences)
t = time.time() - start

with open("/home/srini_gcp_20250512/poc/data/log.log", "a") as file:
    file.write(f'Generation completed - num_sequences-{num_sequences}, {math.ceil(t/60)} min\n')

monitor.py

import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0))

import pynvml
import time

pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)  # GPU 0

while True:
    mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
    util = pynvml.nvmlDeviceGetUtilizationRates(handle)
    print(f"Used: {mem_info.used / 1024**2:.2f} MB | Utilization: {util.gpu}%")
    time.sleep(1)

Commands Used:

1. python train.py # got completed in ~15 min
2. python generate.py # each sequence took about 4 minutes for geneation and only 1 CPU core was 100 consumed. Looks like generation happened sequentially for each Sequence Key
3. python monitor.py # Showed finite usage during training phase, but negligible usage during generation
4. nvidia-smi # Similar output was observed as monitor.py

Output from monitor.py

srini_gcp_20250512@instance:~/poc/code$ python monitor.py 
True
NVIDIA L4
Used: 462.75 MB | Utilization: 0%
Used: 462.75 MB | Utilization: 0%
Used: 462.75 MB | Utilization: 0%

srini-ar avatar May 13 '25 18:05 srini-ar

I tested again and could see that GPU is being used during sampling/generation phase too. Marking this as closed.

srini-ar avatar May 14 '25 20:05 srini-ar

Thanks for confirming @zanpakuto. Please feel free to file new issues if you notice anything else.

npatki avatar May 14 '25 22:05 npatki