Fix local loading of all three ESMC checkpoints - #372
Open
bviggiano wants to merge 2 commits into
Open
Conversation
300M/600M weights are a nested data/weights/*.pth with no index at the snapshot root; 6B keys come from the HF ESMCForMaskedLM wrapper. Because load_torch_model defaults to strict=False, the 6B keys were dropped silently, leaving meta tensors that failed later in .to(device). Load state dicts directly and apply them with assign=True, matching the ESM3 builders in the same file.
ESMC.from_pretrained("esm3_sm_open_v1") resolves to the ESM3_sm_open_v0
builder and returns an ESM3, so it always trips assert isinstance(model,
ESMC). The following two lines already load esmc_300m.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Loading any ESMC checkpoint locally currently fails for me on
main. I implemented this fix to get this to work and thought I would make a PR just in case.esmc_300mValueError: Directory '...' does not contain a valid checkpointesmc_600mValueError: Directory '...' does not contain a valid checkpointesmc_6bNotImplementedError: Cannot copy out of meta tensor; no data!Three independent causes (notes from Claude):
pretrained.py#L75and#L89callload_torch_modelon the snapshot root, butesmc-300m-2024-12andesmc-600m-2024-12have a single nesteddata/weights/*.pthwith no index file at the root.#L103loadsesmc-6b-2024-12, whose 808 keys are exported from the HFESMCForMaskedLMwrapper (esmc.*,lm_head.*);ESMCexpects them unprefixed with the head assequence_head.load_torch_modeldefaults tostrict=False, so every key is dropped silently, the parameters stay meta tensors frominit_empty_weights(), and the failure only surfaces atmodel.to(device).esmc.py#L139passes an ESM3 checkpoint name toESMC.from_pretrained, which resolves to theESM3_sm_open_v0builder and returns anESM3: never anESMC, so the assert fails.Fix
esm/pretrained.py— all three ESMC builders load the state dict directly and apply it withassign=True, the patternESM3_sm_open_v0and the three ESM3 decoder builders above it already use. 6B additionally walksmodel.safetensors.index.jsonand mapsesmc.→ `` andlm_head.→ `sequence_head.`. `load_torch_model` has no remaining callers, so the import is swapped for `load_state_dict_from_file`.assign=Truekeepsload_state_dict's defaultstrict=True, so future key drift fails at the load site instead of silently.cookbook/snippets/esmc.py— drop theesm3_sm_open_v1call. The two lines after it already loadesmc_300mand pass it tomain()andraw_forward(), so it is redundant as well as incorrect.I verified these now work in a clean micromamba env, Python 3.12 via the
pip install -e .set up path.Notes
I found a few other things that appear to be out of date!
data_root()returnsPath("")whenINFRA_PROVIDERis set, andtests/Makefile#L4always sets it.CONTRIBUTIONS.md#L16specifies Python 3.10, butpyproject.toml#L6requires>=3.12,<3.13, sopip install -e .fails on 3.10.CONTRIBUTIONS.md#L20referencesexamples/requirements.txt; there is noexamples/directory.