Tonic commited on
Commit
8f98e80
·
1 Parent(s): 70c86c3

add decorator to load_pipeline for ZerGPU

Browse files
Files changed (1) hide show
  1. app.py +41 -3
app.py CHANGED
@@ -61,7 +61,7 @@ def load_pipeline():
61
  print("Loading Chronos model...")
62
  pipeline = ChronosPipeline.from_pretrained(
63
  "amazon/chronos-t5-large",
64
- device_map="auto", # Let the model decide the best device mapping
65
  torch_dtype=torch.float16,
66
  low_cpu_mem_usage=True,
67
  trust_remote_code=True,
@@ -313,8 +313,8 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
313
  pipe = load_pipeline()
314
 
315
  # Get the model's device and dtype
316
- device = next(pipe.model.parameters()).device
317
- dtype = next(pipe.model.parameters()).dtype
318
  print(f"Model device: {device}")
319
  print(f"Model dtype: {dtype}")
320
 
@@ -384,6 +384,44 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
384
  # Ensure prediction length is on GPU
385
  prediction_length = torch.tensor(actual_prediction_length, device=device, dtype=torch.long)
386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  quantiles, mean = pipe.predict_quantiles(
388
  context=context,
389
  prediction_length=actual_prediction_length,
 
61
  print("Loading Chronos model...")
62
  pipeline = ChronosPipeline.from_pretrained(
63
  "amazon/chronos-t5-large",
64
+ device_map="cuda", # Force CUDA device mapping
65
  torch_dtype=torch.float16,
66
  low_cpu_mem_usage=True,
67
  trust_remote_code=True,
 
313
  pipe = load_pipeline()
314
 
315
  # Get the model's device and dtype
316
+ device = torch.device("cuda:0") # Force CUDA device
317
+ dtype = torch.float16 # Force float16
318
  print(f"Model device: {device}")
319
  print(f"Model dtype: {dtype}")
320
 
 
384
  # Ensure prediction length is on GPU
385
  prediction_length = torch.tensor(actual_prediction_length, device=device, dtype=torch.long)
386
 
387
+ # Force all model components to GPU
388
+ pipe.model = pipe.model.to(device)
389
+ pipe.tokenizer = pipe.tokenizer.to(device) if hasattr(pipe, 'tokenizer') else None
390
+
391
+ # Ensure all model states are on GPU
392
+ if hasattr(pipe.model, 'state_dict'):
393
+ state_dict = pipe.model.state_dict()
394
+ for key in state_dict:
395
+ if isinstance(state_dict[key], torch.Tensor):
396
+ state_dict[key] = state_dict[key].to(device)
397
+ pipe.model.load_state_dict(state_dict)
398
+
399
+ # Ensure all model attributes are on GPU
400
+ for attr_name in dir(pipe.model):
401
+ attr = getattr(pipe.model, attr_name)
402
+ if isinstance(attr, torch.Tensor):
403
+ setattr(pipe.model, attr_name, attr.to(device))
404
+
405
+ # Ensure all model submodules are on GPU
406
+ for name, module in pipe.model.named_modules():
407
+ if hasattr(module, 'to'):
408
+ module.to(device)
409
+
410
+ # Ensure all model buffers are on GPU
411
+ for name, buffer in pipe.model.named_buffers():
412
+ if buffer is not None:
413
+ pipe.model.register_buffer(name, buffer.to(device))
414
+
415
+ # Ensure all model parameters are on GPU
416
+ for name, param in pipe.model.named_parameters():
417
+ if param is not None:
418
+ param.data = param.data.to(device)
419
+
420
+ # Ensure all model attributes that might contain tensors are on GPU
421
+ for name, value in pipe.model.__dict__.items():
422
+ if isinstance(value, torch.Tensor):
423
+ pipe.model.__dict__[name] = value.to(device)
424
+
425
  quantiles, mean = pipe.predict_quantiles(
426
  context=context,
427
  prediction_length=actual_prediction_length,