Spaces:
Paused
Paused
Tonic commited on
Commit ·
b94ef5d
1
Parent(s): f2f7fa2
attempt to solve gpu error
Browse files
app.py
CHANGED
|
@@ -394,120 +394,118 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
| 394 |
|
| 395 |
# Move any additional components to GPU
|
| 396 |
if hasattr(pipe, 'tokenizer'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
for name, value in pipe.tokenizer.__dict__.items():
|
| 398 |
if isinstance(value, torch.Tensor):
|
| 399 |
setattr(pipe.tokenizer, name, value.to(device))
|
| 400 |
-
|
| 401 |
-
# Ensure all inputs are on the same device
|
| 402 |
-
with torch.cuda.device(device):
|
| 403 |
-
# Force synchronization to ensure all tensors are on GPU
|
| 404 |
-
torch.cuda.synchronize()
|
| 405 |
-
|
| 406 |
-
# Ensure all model components are on GPU
|
| 407 |
-
pipe.model = pipe.model.to(device)
|
| 408 |
-
|
| 409 |
-
# Move any additional tensors in the model to GPU
|
| 410 |
-
for name, module in pipe.model.named_modules():
|
| 411 |
-
if hasattr(module, 'to'):
|
| 412 |
-
module.to(device)
|
| 413 |
-
# Move any tensors in the module's __dict__
|
| 414 |
-
for key, value in module.__dict__.items():
|
| 415 |
-
if isinstance(value, torch.Tensor):
|
| 416 |
-
setattr(module, key, value.to(device))
|
| 417 |
-
|
| 418 |
-
# Move any additional tensors in the pipeline to GPU
|
| 419 |
-
for name, value in pipe.__dict__.items():
|
| 420 |
-
if isinstance(value, torch.Tensor):
|
| 421 |
-
setattr(pipe, name, value.to(device))
|
| 422 |
|
| 423 |
-
#
|
| 424 |
-
pipe.
|
|
|
|
|
|
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
if isinstance(value, torch.Tensor):
|
| 430 |
-
setattr(pipe.model.config, key, value.to(device))
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
for key in state_dict:
|
| 436 |
-
if isinstance(state_dict[key], torch.Tensor):
|
| 437 |
-
state_dict[key] = state_dict[key].to(device)
|
| 438 |
-
pipe.model.load_state_dict(state_dict)
|
| 439 |
|
| 440 |
-
# Move any additional tensors in the
|
| 441 |
-
for name,
|
| 442 |
-
if buffer is not None:
|
| 443 |
-
pipe.model.register_buffer(name, buffer.to(device))
|
| 444 |
-
|
| 445 |
-
# Move any additional tensors in the model's parameters to GPU
|
| 446 |
-
for name, param in pipe.model.named_parameters():
|
| 447 |
-
if param is not None:
|
| 448 |
-
param.data = param.data.to(device)
|
| 449 |
-
|
| 450 |
-
# Move any additional tensors in the model's attributes to GPU
|
| 451 |
-
for name, value in pipe.model.__dict__.items():
|
| 452 |
if isinstance(value, torch.Tensor):
|
| 453 |
-
pipe.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
# Move
|
| 456 |
-
for name,
|
| 457 |
-
if
|
| 458 |
-
|
| 459 |
-
# Move any tensors in the module's __dict__
|
| 460 |
-
for key, value in module.__dict__.items():
|
| 461 |
-
if isinstance(value, torch.Tensor):
|
| 462 |
-
setattr(module, key, value.to(device))
|
| 463 |
|
| 464 |
-
#
|
| 465 |
-
|
|
|
|
|
|
|
| 466 |
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
if hasattr(pipe.tokenizer, 'to'):
|
| 471 |
-
pipe.tokenizer = pipe.tokenizer.to(device)
|
| 472 |
-
|
| 473 |
-
# Move all tokenizer tensors to GPU
|
| 474 |
-
for name, value in pipe.tokenizer.__dict__.items():
|
| 475 |
-
if isinstance(value, torch.Tensor):
|
| 476 |
-
setattr(pipe.tokenizer, name, value.to(device))
|
| 477 |
-
|
| 478 |
-
# Move any additional tensors in the tokenizer's modules to GPU
|
| 479 |
-
for name, module in pipe.tokenizer.named_modules():
|
| 480 |
-
if hasattr(module, 'to'):
|
| 481 |
-
module.to(device)
|
| 482 |
-
# Move any tensors in the module's __dict__
|
| 483 |
-
for key, value in module.__dict__.items():
|
| 484 |
-
if isinstance(value, torch.Tensor):
|
| 485 |
-
setattr(module, key, value.to(device))
|
| 486 |
-
|
| 487 |
-
# Move any additional tensors in the tokenizer's buffers to GPU
|
| 488 |
-
for name, buffer in pipe.tokenizer.named_buffers():
|
| 489 |
-
if buffer is not None:
|
| 490 |
-
pipe.tokenizer.register_buffer(name, buffer.to(device))
|
| 491 |
-
|
| 492 |
-
# Move any additional tensors in the tokenizer's parameters to GPU
|
| 493 |
-
for name, param in pipe.tokenizer.named_parameters():
|
| 494 |
-
if param is not None:
|
| 495 |
-
param.data = param.data.to(device)
|
| 496 |
-
|
| 497 |
-
# Move any additional tensors in the tokenizer's attributes to GPU
|
| 498 |
-
for name, value in pipe.tokenizer.__dict__.items():
|
| 499 |
-
if isinstance(value, torch.Tensor):
|
| 500 |
-
pipe.tokenizer.__dict__[name] = value.to(device)
|
| 501 |
|
| 502 |
-
|
| 503 |
-
|
|
|
|
| 504 |
|
| 505 |
-
#
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
|
| 512 |
if quantiles is None or mean is None:
|
| 513 |
raise ValueError("Chronos returned empty prediction")
|
|
|
|
| 394 |
|
| 395 |
# Move any additional components to GPU
|
| 396 |
if hasattr(pipe, 'tokenizer'):
|
| 397 |
+
# Move tokenizer to GPU if it supports it
|
| 398 |
+
if hasattr(pipe.tokenizer, 'to'):
|
| 399 |
+
pipe.tokenizer = pipe.tokenizer.to(device)
|
| 400 |
+
|
| 401 |
+
# Move all tokenizer tensors to GPU
|
| 402 |
for name, value in pipe.tokenizer.__dict__.items():
|
| 403 |
if isinstance(value, torch.Tensor):
|
| 404 |
setattr(pipe.tokenizer, name, value.to(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
|
| 406 |
+
# Handle MeanScaleUniformBins specific attributes
|
| 407 |
+
if hasattr(pipe.tokenizer, 'bins'):
|
| 408 |
+
if isinstance(pipe.tokenizer.bins, torch.Tensor):
|
| 409 |
+
pipe.tokenizer.bins = pipe.tokenizer.bins.to(device)
|
| 410 |
|
| 411 |
+
if hasattr(pipe.tokenizer, 'scale'):
|
| 412 |
+
if isinstance(pipe.tokenizer.scale, torch.Tensor):
|
| 413 |
+
pipe.tokenizer.scale = pipe.tokenizer.scale.to(device)
|
|
|
|
|
|
|
| 414 |
|
| 415 |
+
if hasattr(pipe.tokenizer, 'mean'):
|
| 416 |
+
if isinstance(pipe.tokenizer.mean, torch.Tensor):
|
| 417 |
+
pipe.tokenizer.mean = pipe.tokenizer.mean.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
+
# Move any additional tensors in the tokenizer's attributes to GPU
|
| 420 |
+
for name, value in pipe.tokenizer.__dict__.items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
if isinstance(value, torch.Tensor):
|
| 422 |
+
pipe.tokenizer.__dict__[name] = value.to(device)
|
| 423 |
+
|
| 424 |
+
# Force synchronization again to ensure all tensors are on GPU
|
| 425 |
+
torch.cuda.synchronize()
|
| 426 |
+
|
| 427 |
+
# Ensure all model components are in eval mode
|
| 428 |
+
pipe.model.eval()
|
| 429 |
+
|
| 430 |
+
# Move any additional tensors in the model's config to GPU
|
| 431 |
+
if hasattr(pipe.model, 'config'):
|
| 432 |
+
for key, value in pipe.model.config.__dict__.items():
|
| 433 |
+
if isinstance(value, torch.Tensor):
|
| 434 |
+
setattr(pipe.model.config, key, value.to(device))
|
| 435 |
+
|
| 436 |
+
# Move any additional tensors in the model's state dict to GPU
|
| 437 |
+
if hasattr(pipe.model, 'state_dict'):
|
| 438 |
+
state_dict = pipe.model.state_dict()
|
| 439 |
+
for key in state_dict:
|
| 440 |
+
if isinstance(state_dict[key], torch.Tensor):
|
| 441 |
+
state_dict[key] = state_dict[key].to(device)
|
| 442 |
+
pipe.model.load_state_dict(state_dict)
|
| 443 |
+
|
| 444 |
+
# Move any additional tensors in the model's buffers to GPU
|
| 445 |
+
for name, buffer in pipe.model.named_buffers():
|
| 446 |
+
if buffer is not None:
|
| 447 |
+
pipe.model.register_buffer(name, buffer.to(device))
|
| 448 |
+
|
| 449 |
+
# Move any additional tensors in the model's parameters to GPU
|
| 450 |
+
for name, param in pipe.model.named_parameters():
|
| 451 |
+
if param is not None:
|
| 452 |
+
param.data = param.data.to(device)
|
| 453 |
+
|
| 454 |
+
# Move any additional tensors in the model's attributes to GPU
|
| 455 |
+
for name, value in pipe.model.__dict__.items():
|
| 456 |
+
if isinstance(value, torch.Tensor):
|
| 457 |
+
pipe.model.__dict__[name] = value.to(device)
|
| 458 |
+
|
| 459 |
+
# Move any additional tensors in the model's modules to GPU
|
| 460 |
+
for name, module in pipe.model.named_modules():
|
| 461 |
+
if hasattr(module, 'to'):
|
| 462 |
+
module.to(device)
|
| 463 |
+
# Move any tensors in the module's __dict__
|
| 464 |
+
for key, value in module.__dict__.items():
|
| 465 |
+
if isinstance(value, torch.Tensor):
|
| 466 |
+
setattr(module, key, value.to(device))
|
| 467 |
+
|
| 468 |
+
# Force synchronization again to ensure all tensors are on GPU
|
| 469 |
+
torch.cuda.synchronize()
|
| 470 |
+
|
| 471 |
+
# Ensure tokenizer is on GPU and all its tensors are on GPU
|
| 472 |
+
if hasattr(pipe, 'tokenizer'):
|
| 473 |
+
# Move tokenizer to GPU if it supports it
|
| 474 |
+
if hasattr(pipe.tokenizer, 'to'):
|
| 475 |
+
pipe.tokenizer = pipe.tokenizer.to(device)
|
| 476 |
|
| 477 |
+
# Move all tokenizer tensors to GPU
|
| 478 |
+
for name, value in pipe.tokenizer.__dict__.items():
|
| 479 |
+
if isinstance(value, torch.Tensor):
|
| 480 |
+
setattr(pipe.tokenizer, name, value.to(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
+
# Handle MeanScaleUniformBins specific attributes
|
| 483 |
+
if hasattr(pipe.tokenizer, 'bins'):
|
| 484 |
+
if isinstance(pipe.tokenizer.bins, torch.Tensor):
|
| 485 |
+
pipe.tokenizer.bins = pipe.tokenizer.bins.to(device)
|
| 486 |
|
| 487 |
+
if hasattr(pipe.tokenizer, 'scale'):
|
| 488 |
+
if isinstance(pipe.tokenizer.scale, torch.Tensor):
|
| 489 |
+
pipe.tokenizer.scale = pipe.tokenizer.scale.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
+
if hasattr(pipe.tokenizer, 'mean'):
|
| 492 |
+
if isinstance(pipe.tokenizer.mean, torch.Tensor):
|
| 493 |
+
pipe.tokenizer.mean = pipe.tokenizer.mean.to(device)
|
| 494 |
|
| 495 |
+
# Move any additional tensors in the tokenizer's attributes to GPU
|
| 496 |
+
for name, value in pipe.tokenizer.__dict__.items():
|
| 497 |
+
if isinstance(value, torch.Tensor):
|
| 498 |
+
pipe.tokenizer.__dict__[name] = value.to(device)
|
| 499 |
+
|
| 500 |
+
# Force synchronization again to ensure all tensors are on GPU
|
| 501 |
+
torch.cuda.synchronize()
|
| 502 |
+
|
| 503 |
+
# Make prediction
|
| 504 |
+
quantiles, mean = pipe.predict_quantiles(
|
| 505 |
+
context=context,
|
| 506 |
+
prediction_length=actual_prediction_length,
|
| 507 |
+
quantile_levels=[0.1, 0.5, 0.9]
|
| 508 |
+
)
|
| 509 |
|
| 510 |
if quantiles is None or mean is None:
|
| 511 |
raise ValueError("Chronos returned empty prediction")
|