fix: enforce memory residency for all model tensors (#564)

Partially addresses #563
This commit is contained in:
Philipp Emanuel Weidmann 2026-11-05 14:47:51 +05:30 committed by GitHub
parent 21c68d0a94
commit 6fc6074c61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

3
.gitignore vendored
View File

@ -15,6 +15,9 @@ wheels/
# Editors # Editors
/.vscode/ /.vscode/
# ripgrep/fd-specific ignore file
/.ignore
# Configuration file (root only, not ignored in test directories) # Configuration file (root only, not ignored in test directories)
/config.toml /config.toml

View File

@ -44,6 +44,29 @@ def get_model_class(
return AutoModelForCausalLM return AutoModelForCausalLM
def check_model_resident(model: PreTrainedModel | PeftModel) -> None:
"""
Ensure that all of the model's weights are resident in memory.
"""
offloaded = [name for name, param in model.named_parameters() if param.is_meta]
if not offloaded:
return
raise Exception(
"The model does not fit into the available device memory: "
f"{len(offloaded)} weights (such as {', '.join(offloaded[:3])}) had to "
"be offloaded to CPU or disk. Heretic cannot process offloaded models "
"because it requires direct, persistent access to all weights, whereas "
"offloaded weights only exist temporarily during forward passes.\n"
"Possible remedies:\n"
"* Free up memory, e.g. by closing other applications.\n"
"* Change the device map (device_map/max_memory settings) so that all "
"weights fit into device memory.\n"
'* Enable 4-bit quantization (quantization = "bnb_4bit").'
)
def _collapse_flattened_residual_streams( def _collapse_flattened_residual_streams(
hidden_states: tuple[FloatTensor, ...], hidden_states: tuple[FloatTensor, ...],
config: Any, config: Any,
@ -233,6 +256,8 @@ class Model:
if self.model is None: if self.model is None:
raise Exception("Failed to load model with all configured dtypes.") raise Exception("Failed to load model with all configured dtypes.")
check_model_resident(self.model)
print(f"* Transformer model with [bold]{len(self.get_layers())}[/] layers") print(f"* Transformer model with [bold]{len(self.get_layers())}[/] layers")
all_components = {} all_components = {}
@ -419,6 +444,8 @@ class Model:
**extra_kwargs, **extra_kwargs,
) )
check_model_resident(self.model)
self.needs_reload = False self.needs_reload = False
return False return False