Upload handler.py with huggingface_hub
Browse files- handler.py +45 -7
handler.py
CHANGED
|
@@ -82,7 +82,34 @@ class EndpointHandler:
|
|
| 82 |
gpus_to_use=[0],
|
| 83 |
bpe_path=bpe_path
|
| 84 |
)
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
elapsed = time.time() - start_time
|
| 87 |
logger.info(f"✓ SAM3 video predictor loaded successfully in {elapsed:.2f}s")
|
| 88 |
|
|
@@ -453,15 +480,26 @@ class EndpointHandler:
|
|
| 453 |
Returns path to the BPE file.
|
| 454 |
"""
|
| 455 |
logger.info("Checking for BPE tokenizer file...")
|
| 456 |
-
|
| 457 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
assets_dir = Path("/repository/assets")
|
| 459 |
bpe_file = assets_dir / "bpe_simple_vocab_16e6.txt.gz"
|
| 460 |
|
| 461 |
-
if bpe_file.exists():
|
| 462 |
-
logger.info(f" ✓ BPE file found: {bpe_file}")
|
| 463 |
-
return str(bpe_file)
|
| 464 |
-
|
| 465 |
logger.warning(f" BPE file not found at {bpe_file}")
|
| 466 |
logger.info(" Downloading from HuggingFace...")
|
| 467 |
|
|
|
|
| 82 |
gpus_to_use=[0],
|
| 83 |
bpe_path=bpe_path
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
# Fix dtype mismatch: Convert all model parameters and buffers to float32
|
| 87 |
+
# This fixes: "Input type (c10::BFloat16) and bias type (float) should be the same"
|
| 88 |
+
logger.info("Converting model to float32 to avoid dtype mismatch...")
|
| 89 |
+
|
| 90 |
+
dtype_conversion_count = 0
|
| 91 |
+
|
| 92 |
+
# SAM3 predictor has a 'model' attribute that contains the actual model
|
| 93 |
+
if hasattr(self.predictor, 'model') and self.predictor.model is not None:
|
| 94 |
+
# Convert model to float32
|
| 95 |
+
self.predictor.model = self.predictor.model.float()
|
| 96 |
+
|
| 97 |
+
# Ensure all parameters are float32
|
| 98 |
+
for name, param in self.predictor.model.named_parameters():
|
| 99 |
+
if param.dtype != torch.float32:
|
| 100 |
+
param.data = param.data.float()
|
| 101 |
+
dtype_conversion_count += 1
|
| 102 |
+
|
| 103 |
+
# Convert buffers to float32 (important for batch norm, etc.)
|
| 104 |
+
for buffer_name, buffer in self.predictor.model.named_buffers():
|
| 105 |
+
if buffer.dtype != torch.float32 and buffer.dtype in [torch.float16, torch.bfloat16]:
|
| 106 |
+
self.predictor.model.register_buffer(buffer_name, buffer.float())
|
| 107 |
+
dtype_conversion_count += 1
|
| 108 |
+
|
| 109 |
+
logger.info(f"✓ Model converted to float32 ({dtype_conversion_count} tensors converted)")
|
| 110 |
+
else:
|
| 111 |
+
logger.warning("⚠ Could not find model attribute in predictor - dtype fix may not have been applied")
|
| 112 |
+
|
| 113 |
elapsed = time.time() - start_time
|
| 114 |
logger.info(f"✓ SAM3 video predictor loaded successfully in {elapsed:.2f}s")
|
| 115 |
|
|
|
|
| 480 |
Returns path to the BPE file.
|
| 481 |
"""
|
| 482 |
logger.info("Checking for BPE tokenizer file...")
|
| 483 |
+
|
| 484 |
+
# Try multiple possible paths
|
| 485 |
+
possible_paths = [
|
| 486 |
+
Path("/repository/assets/bpe_simple_vocab_16e6.txt.gz"),
|
| 487 |
+
Path("./assets/bpe_simple_vocab_16e6.txt.gz"),
|
| 488 |
+
Path("../assets/bpe_simple_vocab_16e6.txt.gz"),
|
| 489 |
+
Path("/app/assets/bpe_simple_vocab_16e6.txt.gz"),
|
| 490 |
+
]
|
| 491 |
+
|
| 492 |
+
for bpe_file in possible_paths:
|
| 493 |
+
if bpe_file.exists():
|
| 494 |
+
logger.info(f" ✓ BPE file found: {bpe_file}")
|
| 495 |
+
return str(bpe_file)
|
| 496 |
+
|
| 497 |
+
logger.warning(" BPE file not found in any expected location")
|
| 498 |
+
|
| 499 |
+
# Use first path as default for download
|
| 500 |
assets_dir = Path("/repository/assets")
|
| 501 |
bpe_file = assets_dir / "bpe_simple_vocab_16e6.txt.gz"
|
| 502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
logger.warning(f" BPE file not found at {bpe_file}")
|
| 504 |
logger.info(" Downloading from HuggingFace...")
|
| 505 |
|