diff --git a/mlx_lm/models/apertus.py b/mlx_lm/models/apertus.py index 4d8afa2..13d845c 100644 --- a/mlx_lm/models/apertus.py +++ b/mlx_lm/models/apertus.py @@ -167,7 +167,8 @@ class Model(nn.Module): self.args = args self.model_type = args.model_type self.model = ApertusModel(args) - self.lm_head = nn.Linear(args.hidden_size, args.vocab_size, bias=False) + if not args.tie_word_embeddings: + self.lm_head = nn.Linear(args.hidden_size, args.vocab_size, bias=False) def __call__( self, @@ -175,12 +176,18 @@ class Model(nn.Module): cache: Optional[Any] = None, ) -> mx.array: out = self.model(inputs, cache) - return self.lm_head(out) + if self.args.tie_word_embeddings: + out = self.model.embed_tokens.as_linear(out) + else: + out = self.lm_head(out) + return out def sanitize(self, weights): for k, v in weights.items(): if k.endswith("alpha_p") or k.endswith("alpha_n"): weights[k] = v.squeeze() + if self.args.tie_word_embeddings: + weights.pop("lm_head.weight", None) return weights @property