in the end
Browse files- models/__init__.py +23 -1
models/__init__.py
CHANGED
|
@@ -89,9 +89,26 @@ def _has_cuda() -> bool:
|
|
| 89 |
|
| 90 |
def _pick_device(env_key: str) -> str:
|
| 91 |
requested = os.environ.get(env_key, "").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if requested in {"cuda", "cpu"}:
|
|
|
|
| 93 |
return requested
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# --------------------------------------------------------------------------------------
|
| 97 |
# Basic Utilities
|
|
@@ -353,6 +370,11 @@ def _try_build(cfg_path: str):
|
|
| 353 |
logger.info(f"Calling build_sam2 with kwargs: {kwargs}")
|
| 354 |
result = build_sam2(**kwargs)
|
| 355 |
logger.info(f"build_sam2 succeeded with kwargs")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
return result
|
| 357 |
except TypeError as e:
|
| 358 |
logger.info(f"build_sam2 kwargs failed: {e}, trying positional args")
|
|
|
|
| 89 |
|
| 90 |
def _pick_device(env_key: str) -> str:
|
| 91 |
requested = os.environ.get(env_key, "").strip().lower()
|
| 92 |
+
has_cuda = _has_cuda()
|
| 93 |
+
|
| 94 |
+
# Log all CUDA-related environment variables
|
| 95 |
+
cuda_env_vars = {
|
| 96 |
+
'FORCE_CUDA_DEVICE': os.environ.get('FORCE_CUDA_DEVICE', ''),
|
| 97 |
+
'CUDA_MEMORY_FRACTION': os.environ.get('CUDA_MEMORY_FRACTION', ''),
|
| 98 |
+
'PYTORCH_CUDA_ALLOC_CONF': os.environ.get('PYTORCH_CUDA_ALLOC_CONF', ''),
|
| 99 |
+
'REQUIRE_CUDA': os.environ.get('REQUIRE_CUDA', ''),
|
| 100 |
+
'SAM2_DEVICE': os.environ.get('SAM2_DEVICE', ''),
|
| 101 |
+
'MATANY_DEVICE': os.environ.get('MATANY_DEVICE', ''),
|
| 102 |
+
}
|
| 103 |
+
logger.info(f"CUDA environment variables: {cuda_env_vars}")
|
| 104 |
+
|
| 105 |
+
logger.info(f"_pick_device({env_key}): requested='{requested}', has_cuda={has_cuda}")
|
| 106 |
if requested in {"cuda", "cpu"}:
|
| 107 |
+
logger.info(f"Using requested device: {requested}")
|
| 108 |
return requested
|
| 109 |
+
result = "cuda" if has_cuda else "cpu"
|
| 110 |
+
logger.info(f"Auto-selected device: {result}")
|
| 111 |
+
return result
|
| 112 |
|
| 113 |
# --------------------------------------------------------------------------------------
|
| 114 |
# Basic Utilities
|
|
|
|
| 370 |
logger.info(f"Calling build_sam2 with kwargs: {kwargs}")
|
| 371 |
result = build_sam2(**kwargs)
|
| 372 |
logger.info(f"build_sam2 succeeded with kwargs")
|
| 373 |
+
# Log actual device of the model
|
| 374 |
+
if hasattr(result, 'device'):
|
| 375 |
+
logger.info(f"SAM2 model device: {result.device}")
|
| 376 |
+
elif hasattr(result, 'image_encoder') and hasattr(result.image_encoder, 'device'):
|
| 377 |
+
logger.info(f"SAM2 model device: {result.image_encoder.device}")
|
| 378 |
return result
|
| 379 |
except TypeError as e:
|
| 380 |
logger.info(f"build_sam2 kwargs failed: {e}, trying positional args")
|