Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unstricted model loading #938

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/fairseq2/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def __call__(
dtype: DataType | None = None,
force: bool = False,
progress: bool = True,
strict_state_dict: bool = True,
) -> ModelT_co:
"""
:param model_name_or_card:
Expand All @@ -98,6 +99,9 @@ def __call__(
cache.
:param progress:
If ``True``, displays a progress bar to stderr.
:param strict_state_dict:
If ``True``, checkpoint' parameters and layers must be identical to
the model state dict)

:returns:
A model loaded from the checkpoint of ``model_name_or_card``.
Expand Down Expand Up @@ -201,6 +205,7 @@ def __call__(
dtype: DataType | None = None,
force: bool = False,
progress: bool = True,
strict_state_dict: bool = True,
) -> ModelT:
if isinstance(model_name_or_card, AssetCard):
card = model_name_or_card
Expand Down Expand Up @@ -355,7 +360,7 @@ def __call__(
consume_prefix_in_state_dict_if_present(state_dict, prefix="module.")

try:
load_state_dict(model, state_dict)
load_state_dict(model, state_dict, strict=strict_state_dict)
except (KeyError, ValueError) as ex:
raise AssetError(
f"{card.name} cannot be loaded. See nested exception for details."
Expand Down Expand Up @@ -396,6 +401,7 @@ def __call__(
dtype: DataType | None = None,
force: bool = False,
progress: bool = True,
strict_state_dict: bool = True,
) -> ModelT:
if isinstance(model_name_or_card, AssetCard):
card = model_name_or_card
Expand All @@ -419,6 +425,7 @@ def __call__(
dtype=dtype,
force=force,
progress=progress,
strict_state_dict=strict_state_dict,
)

def register(self, family: str, loader: ModelLoader[ModelT]) -> None:
Expand Down
13 changes: 7 additions & 6 deletions src/fairseq2/nn/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,17 @@ def broadcast_module(
_broadcast_coalesced(pg, tensors, bucket_size, source_rank)


def load_state_dict(module: Module, state_dict: Mapping[str, object]) -> None:
def load_state_dict(
module: Module, state_dict: Mapping[str, object], strict: bool = True
) -> None:
"""Copy parameters and buffers from ``state_dict`` into ``module`` and its
descendant modules.

This implementation internally calls :meth:`Module.load_state_dict()` with
``strict`` set to ``True``, and also enforces that ``state_dict`` does not
contain any keys corresponding to descendants that are set to ``None`` via
:meth:`Module.register_module()`.
This implementation internally calls :meth:`Module.load_state_dict()`, and also enforces that
``state_dict`` does not contain any keys corresponding to descendants that are set to ``None``
via :meth:`Module.register_module()`.
"""
module.load_state_dict(state_dict, strict=True)
module.load_state_dict(state_dict, strict=strict)

unexpected_keys = []

Expand Down
Loading