fix legacy model loading

This commit is contained in:
Alex Cheema
2024-08-02 14:18:31 +01:00
parent 08e8cacf01
commit eafed8e16e
3 changed files with 12 additions and 3 deletions
+5 -1
View File
@@ -106,7 +106,11 @@ class Model(nn.Module):
shard_state_dict[key] = value
elif self.args.shard.is_first_layer() and key.startswith('model.embed_tokens'):
shard_state_dict[key] = value
elif self.args.shard.is_last_layer() and (key.startswith('model.norm') or key.startswith('lm_head')):
elif (self.args.shard.is_last_layer() and self.args.tie_word_embeddings) and key.startswith('model.embed_tokens'):
shard_state_dict[key] = value
elif (self.args.shard.is_last_layer() and not self.args.tie_word_embeddings) and key.startswith('lm_head'):
shard_state_dict[key] = value
elif self.args.shard.is_last_layer() and (key.startswith('model.norm')):
shard_state_dict[key] = value
return shard_state_dict
+6 -1
View File
@@ -144,10 +144,15 @@ def load_model_shard(
weights = model.sanitize(weights)
if (quantization := config.get("quantization", None)) is not None:
# Handle legacy models which may not have everything quantized
def class_predicate(p, m):
if not hasattr(m, "to_quantized"):
return False
return f"{p}.scales" in weights
nn.quantize(
model,
**quantization,
class_predicate=None,
class_predicate=class_predicate,
)
model.load_weights(list(weights.items()), strict=True)
+1 -1
View File
@@ -44,7 +44,7 @@ asyncio.run(
test_inference_engine(
MLXDynamicShardInferenceEngine(),
MLXDynamicShardInferenceEngine(),
"mlx-community/Meta-Llama-3.1-8B-Instruct-4bit",
"mlx-community/Meta-Llama-3-8B-Instruct-4bit",
)
)