Tonic commited on
Commit
2c138aa
·
1 Parent(s): 548b797

move all model and components to cuda

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -352,29 +352,31 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
352
  # Move model to evaluation mode
353
  pipe.model.eval()
354
 
355
- # Ensure all model components are on the same device
356
- pipe.model = pipe.model.to(device)
357
- if hasattr(pipe, 'tokenizer'):
358
- pipe.tokenizer = pipe.tokenizer.to(device)
359
- if hasattr(pipe, 'config'):
360
- pipe.config = pipe.config.to(device)
 
 
 
361
 
362
- # Move all model parameters and buffers to the correct device
363
- for param in pipe.model.parameters():
364
- param.data = param.data.to(device)
365
- for buffer in pipe.model.buffers():
366
- buffer.data = buffer.data.to(device)
367
-
368
- # Ensure the distribution head is properly initialized
369
- if hasattr(pipe.model, 'distribution_head'):
370
- pipe.model.distribution_head = pipe.model.distribution_head.to(device)
371
 
372
  # Use predict_quantiles with proper formatting
373
- quantiles, mean = pipe.predict_quantiles(
374
- context=context,
375
- prediction_length=actual_prediction_length,
376
- quantile_levels=[0.1, 0.5, 0.9]
377
- )
 
378
 
379
  if quantiles is None or mean is None:
380
  raise ValueError("Chronos returned empty prediction")
 
352
  # Move model to evaluation mode
353
  pipe.model.eval()
354
 
355
+ # Move the main model to GPU but keep distribution head on CPU
356
+ if hasattr(pipe.model, 'encoder'):
357
+ pipe.model.encoder = pipe.model.encoder.to(device)
358
+ if hasattr(pipe.model, 'decoder'):
359
+ pipe.model.decoder = pipe.model.decoder.to(device)
360
+ if hasattr(pipe.model, 'embed_tokens'):
361
+ pipe.model.embed_tokens = pipe.model.embed_tokens.to(device)
362
+ if hasattr(pipe.model, 'final_layer_norm'):
363
+ pipe.model.final_layer_norm = pipe.model.final_layer_norm.to(device)
364
 
365
+ # Move all parameters and buffers except distribution head
366
+ for name, param in pipe.model.named_parameters():
367
+ if 'distribution_head' not in name:
368
+ param.data = param.data.to(device)
369
+ for name, buffer in pipe.model.named_buffers():
370
+ if 'distribution_head' not in name:
371
+ buffer.data = buffer.data.to(device)
 
 
372
 
373
  # Use predict_quantiles with proper formatting
374
+ with torch.cuda.amp.autocast():
375
+ quantiles, mean = pipe.predict_quantiles(
376
+ context=context,
377
+ prediction_length=actual_prediction_length,
378
+ quantile_levels=[0.1, 0.5, 0.9]
379
+ )
380
 
381
  if quantiles is None or mean is None:
382
  raise ValueError("Chronos returned empty prediction")