Skip to content

Commit cd3760d

Browse files
committed
normalize null checkpoint paths
1 parent 2a95c6e commit cd3760d

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

src/xlm/utils/model_loading.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
2323

2424
logger = RankedLogger(__name__, rank_zero_only=True)
2525

26+
27+
def _normalize_optional_str(val: Any) -> Optional[str]:
28+
"""Return a non-empty string or None.
29+
30+
Hydra CLI ``...=null`` / YAML ``null`` sometimes surface as the literal
31+
string ``\"None\"``; treat those like missing so Hub / fallbacks work.
32+
"""
33+
if val is None:
34+
return None
35+
try:
36+
if OmegaConf.is_missing(val):
37+
return None
38+
except (ValueError, TypeError, AttributeError):
39+
pass
40+
if not isinstance(val, str):
41+
val = str(val)
42+
s = val.strip()
43+
if not s or s.lower() in ("none", "null", "~"):
44+
return None
45+
return s
46+
47+
2648
_STR_TO_DTYPE = {
2749
"float32": torch.float32,
2850
"float16": torch.float16,
@@ -271,16 +293,18 @@ def _get_full_checkpoint_path(
271293
# Build the config path to check
272294
if config_prefix:
273295
# Try both ckpt_path and checkpoint_path variants
274-
ckpt_path = OmegaConf.select(
275-
cfg, f"{config_prefix}.ckpt_path", default=None
296+
ckpt_path = _normalize_optional_str(
297+
OmegaConf.select(cfg, f"{config_prefix}.ckpt_path", default=None)
276298
)
277299
if ckpt_path is None:
278-
ckpt_path = OmegaConf.select(
279-
cfg, f"{config_prefix}.checkpoint_path", default=None
300+
ckpt_path = _normalize_optional_str(
301+
OmegaConf.select(
302+
cfg, f"{config_prefix}.checkpoint_path", default=None
303+
)
280304
)
281305
else:
282306
# Top-level: check hub_checkpoint_path
283-
ckpt_path = cfg.get("hub_checkpoint_path", None)
307+
ckpt_path = _normalize_optional_str(cfg.get("hub_checkpoint_path", None))
284308

285309
if ckpt_path is not None:
286310
if not os.path.isfile(ckpt_path):
@@ -328,17 +352,23 @@ def _get_model_only_checkpoint_path(
328352
"""
329353
# Check for model_only_checkpoint_path in config
330354
if config_prefix:
331-
model_only_path = OmegaConf.select(
332-
cfg, f"{config_prefix}.model_only_checkpoint_path", default=None
355+
model_only_path = _normalize_optional_str(
356+
OmegaConf.select(
357+
cfg, f"{config_prefix}.model_only_checkpoint_path", default=None
358+
)
333359
)
334360
else:
335361
# Top-level
336-
model_only_path = cfg.get("model_only_checkpoint_path", None)
362+
model_only_path = _normalize_optional_str(
363+
cfg.get("model_only_checkpoint_path", None)
364+
)
337365

338366
# Check for hub.repo_id if enabled
339367
hub_repo_id = None
340368
if enable_hub_support:
341-
hub_repo_id = OmegaConf.select(cfg, "hub.repo_id", default=None)
369+
hub_repo_id = _normalize_optional_str(
370+
OmegaConf.select(cfg, "hub.repo_id", default=None)
371+
)
342372

343373
# Conflict detection
344374
if full_ckpt_path is not None:

0 commit comments

Comments
 (0)