Compare commits

...
3 changed files with 40 additions and 10 deletions

No files matched your search

+3
View File
@@ -15,6 +15,9 @@ wheels/
# Editors
/.vscode/
# ripgrep/fd-specific ignore file
/.ignore
# Configuration file (root only, not ignored in test directories)
/config.toml
+10 -10
View File
@@ -1005,7 +1005,6 @@ def run():
save_directory,
max_shard_size=settings.max_shard_size,
)
print(f"Model saved to [bold]{save_directory}[/].")
else:
print("Saving merged model...")
merged_model = model.get_merged_model()
@@ -1018,10 +1017,8 @@ def run():
model.tokenizer.save_pretrained(save_directory)
if model.processor is not None:
model.processor.save_pretrained(save_directory)
print(f"Model saved to [bold]{save_directory}[/].")
# Merging replaced the trial's adapter in place.
# Restore it only after the save has been reported.
reset_trial_model()
print(f"Model saved to [bold]{save_directory}[/].")
if reproduction_mode:
print("Verifying hashes of weight files...")
@@ -1048,6 +1045,9 @@ def run():
f"[bold]{filename}:[/] [red]File not found[/]"
)
if strategy != ExportStrategy.ADAPTER:
reset_trial_model()
case "upload":
# We don't use huggingface_hub.login() because that stores the token on disk,
# and since this program will often be run on rented or shared GPU servers,
@@ -1175,7 +1175,6 @@ def run():
max_shard_size=settings.max_shard_size,
token=token,
)
print(f"Model uploaded to [bold]{repo_id}[/].")
else:
print("Uploading merged model...")
merged_model = model.get_merged_model()
@@ -1198,10 +1197,6 @@ def run():
private=private,
token=token,
)
print(f"Model uploaded to [bold]{repo_id}[/].")
# Merging replaced the trial's adapter in place.
# Restore it only after the upload has been reported.
reset_trial_model()
if is_hf_path(settings.model):
card = ModelCard.load(settings.model)
@@ -1269,6 +1264,8 @@ def run():
finally:
settings.export_strategy = current_export_strategy
print(f"Model uploaded to [bold]{repo_id}[/].")
if reproduction_mode:
print("Verifying hashes of weight files...")
@@ -1320,6 +1317,9 @@ def run():
f"[bold]{filename}:[/] [red]File not found[/]"
)
if strategy != ExportStrategy.ADAPTER:
reset_trial_model()
case "chat":
print()
print(
+27
View File
@@ -44,6 +44,29 @@ def get_model_class(
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(
hidden_states: tuple[FloatTensor, ...],
config: Any,
@@ -233,6 +256,8 @@ class Model:
if self.model is None:
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")
all_components = {}
@@ -419,6 +444,8 @@ class Model:
**extra_kwargs,
)
check_model_resident(self.model)
self.needs_reload = False
return False