Download code_src/tdmpc2_rise/td_repro_compat.py from rli14/rsm_rise_code: direct link, hf CLI and curl.
- Browser
- Download file 4.97 kB
-
https://huggingface.co/rli14/rsm_rise_code/resolve/main/code_src/tdmpc2_rise/td_repro_compat.py
- Command line
-
hf download hf://rli14/rsm_rise_code/code_src/tdmpc2_rise/td_repro_compat.py
-
curl -L -o td_repro_compat.py https://huggingface.co/rli14/rsm_rise_code/resolve/main/code_src/tdmpc2_rise/td_repro_compat.py
4.97 kB
| """gen_q#274 -- the SECOND torch/tensordict compatibility shim needed to run TD-MPC2's own | |
| reproduction check (the released walker-walk checkpoint through the repo's own evaluate.py). | |
| MEASURED FAILURE (status/nb/td_repro_eval.txt, LSF 1880432 12:48Z), after install_safe_to() had | |
| already fixed the meta-tensor `.to()`: | |
| File "code/tdmpc2/tdmpc2/common/layers.py", line 199, in api_model_conversion | |
| new_state_dict[new_key] = target_state_dict[new_key] | |
| KeyError: '_Qs.params.__batch_size' | |
| WHAT IT IS. `api_model_conversion` upgrades a checkpoint saved with TD-MPC2's pre-torch.compile API. | |
| Its last step copies the tensordict METADATA entries `__batch_size` / `__device` out of the LIVE | |
| model's `state_dict()`. Those string-keyed metadata entries were emitted by the tensordict of 2024 | |
| (~0.3/0.4); this image ships tensordict 0.14.2, whose `TensorDictParams.state_dict()` no longer emits | |
| them. So the key is missing from the TARGET, not from the checkpoint: it is a library-version drift | |
| in the vendored checkout, exactly like the meta-tensor `.to()`, and NOT a property of the method or | |
| of our data. | |
| THE SHIM. A faithful re-implementation of upstream's function with one change: metadata keys are | |
| copied only when the live model actually has them, and any Qs key the live model has but the | |
| converted dict lacks is filled from the live model. Both directions are LOGGED, so the reproduction | |
| number is never quoted without the record of what was skipped. Weight tensors are untouched -- every | |
| parameter still comes from the released checkpoint. Nothing in code/tdmpc2 is edited. | |
| """ | |
| from __future__ import annotations | |
| _META = ("__batch_size", "__device") | |
| _INSTALLED = False | |
| def _tolerant_api_model_conversion(target_state_dict, source_state_dict): | |
| if "_detach_Qs_params.0.weight" in source_state_dict: | |
| return source_state_dict | |
| name_map = ["weight", "bias", "ln.weight", "ln.bias"] | |
| new_state_dict = dict() | |
| for key, val in list(source_state_dict.items()): | |
| if key.startswith("_Qs."): | |
| num = key[len("_Qs.params."):] | |
| if num in _META: # old checkpoints carried metadata too | |
| del source_state_dict[key] | |
| continue | |
| new_key = str(int(num) // 4) + "." + name_map[int(num) % 4] | |
| del source_state_dict[key] | |
| new_state_dict["_Qs.params." + new_key] = val | |
| new_state_dict["_detach_Qs_params." + new_key] = val | |
| elif key.startswith("_target_Qs."): | |
| num = key[len("_target_Qs.params."):] | |
| if num in _META: | |
| del source_state_dict[key] | |
| continue | |
| new_key = str(int(num) // 4) + "." + name_map[int(num) % 4] | |
| del source_state_dict[key] | |
| new_state_dict["_target_Qs_params." + new_key] = val | |
| skipped, filled = [], [] | |
| for prefix in ("_Qs.", "_detach_Qs_", "_target_Qs_"): | |
| for key in _META: | |
| k = prefix + "params." + key | |
| if k in target_state_dict: | |
| new_state_dict[k] = target_state_dict[k] | |
| else: | |
| skipped.append(k) | |
| for key in target_state_dict: | |
| if "Qs" in key and key not in new_state_dict: | |
| new_state_dict[key] = target_state_dict[key] | |
| filled.append(key) | |
| for key in new_state_dict: | |
| assert key in target_state_dict, f"key {key} not in target_state_dict" | |
| for key in source_state_dict: | |
| assert "Qs" not in key, f"key {key} contains 'Qs'" | |
| new_state_dict["log_std_min"] = target_state_dict["log_std_min"] | |
| new_state_dict["log_std_dif"] = target_state_dict["log_std_dif"] | |
| if "_action_masks" in target_state_dict: | |
| new_state_dict["_action_masks"] = target_state_dict["_action_masks"] | |
| # merge in upstream's own direction (source.update(new), return source) so the result is | |
| # byte-for-byte what the unpatched function would have produced had the metadata keys existed | |
| source_state_dict.update(new_state_dict) | |
| print(f"[td_repro_compat] tensordict metadata keys absent from the live model, skipped: " | |
| f"{skipped or 'none'}", flush=True) | |
| print(f"[td_repro_compat] Qs keys filled from the live model (not in the released checkpoint): " | |
| f"{filled or 'none'}", flush=True) | |
| print(f"[td_repro_compat] converted state dict: {len(source_state_dict)} entries", flush=True) | |
| return source_state_dict | |
| def install_api_conversion_compat(): | |
| """Patch `common.layers.api_model_conversion` (and the name `tdmpc2.py` imported from it).""" | |
| global _INSTALLED | |
| if _INSTALLED: | |
| return | |
| from common import layers | |
| import tdmpc2 as _td | |
| layers.api_model_conversion = _tolerant_api_model_conversion | |
| if hasattr(_td, "api_model_conversion"): | |
| _td.api_model_conversion = _tolerant_api_model_conversion | |
| _INSTALLED = True | |
| print("[td_repro_compat] tolerant api_model_conversion installed", flush=True) | |