Skip to content
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
28 changes: 12 additions & 16 deletions mlx_lm/tuner/dora.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ def fuse(self, dequantize: bool = False):
bias = "bias" in linear
weight = self._dequantized_weight()

# Use the same type as the linear weight
dtype = weight.dtype

output_dims, input_dims = weight.shape
fused_linear = nn.Linear(input_dims, output_dims, bias=False)

lora_b = (self.scale * self.lora_b.T).astype(dtype)
lora_a = self.lora_a.T.astype(dtype)
weight = weight + lora_b @ lora_a
lora_b = self.scale * self.lora_b.T
lora_a = self.lora_a.T
weight = weight + (lora_b @ lora_a).astype(weight.dtype)
norm_scale = self.m / mx.linalg.norm(weight, axis=1)
fused_linear.weight = norm_scale[:, None] * weight

Expand All @@ -52,8 +49,9 @@ def fuse(self, dequantize: bool = False):
if self._is_quantized() and not dequantize:
fused_linear = nn.QuantizedLinear.from_linear(
fused_linear,
linear.group_size,
linear.bits,
group_size=linear.group_size,
bits=linear.bits,
mode=linear.mode,
)
return fused_linear

Expand Down Expand Up @@ -101,8 +99,9 @@ def _dequantized_weight(self):
weight,
self.linear.scales,
self.linear.biases,
self.linear.group_size,
self.linear.bits,
group_size=self.linear.group_size,
bits=self.linear.bits,
mode=self.linear.mode,
)
return weight

Expand Down Expand Up @@ -155,15 +154,12 @@ def fuse(self, dequantize: bool = False):
embedding = self.embedding
weight = embedding.weight

# Use the same type as the linear weight if not quantized
dtype = weight.dtype

num_embeddings, dims = weight.shape
fused_embedding = nn.Embedding(num_embeddings, dims)

lora_a = (self.scale * self.lora_a).astype(dtype)
lora_b = self.lora_b.astype(dtype)
weight = weight + lora_a @ lora_b
lora_a = self.scale * self.lora_a
lora_b = self.lora_b
weight = weight + (lora_a @ lora_b).astype(weight.dtype)
norm_scale = self.m / mx.linalg.norm(weight, axis=1)
fused_embedding.weight = norm_scale[:, None] * weight

Expand Down
51 changes: 23 additions & 28 deletions mlx_lm/tuner/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,19 @@ def fuse(self, dequantize: bool = False):
weight = linear.weight
is_quantized = isinstance(linear, nn.QuantizedLinear)

# Use the same type as the linear weight if not quantized
dtype = weight.dtype

if is_quantized:
dtype = linear.scales.dtype
weight = mx.dequantize(
weight,
linear.scales,
linear.biases,
linear.group_size,
linear.bits,
group_size=linear.group_size,
bits=linear.bits,
mode=linear.mode,
)
output_dims, input_dims = weight.shape
fused_linear = nn.Linear(input_dims, output_dims, bias=bias)

delta = ((self.scale * self.lora_b.T) @ self.lora_a.T).astype(dtype)
delta = ((self.scale * self.lora_b.T) @ self.lora_a.T).astype(weight.dtype)
fused_linear.weight = weight + delta
if bias:
fused_linear.bias = linear.bias
Expand All @@ -62,6 +59,7 @@ def fuse(self, dequantize: bool = False):
fused_linear,
linear.group_size,
linear.bits,
mode=linear.mode,
)

return fused_linear
Expand Down Expand Up @@ -125,29 +123,28 @@ def fuse(self, dequantize: bool = False):
weight = linear.weight
is_quantized = isinstance(linear, QuantizedSwitchLinear)

# Use the same type as the linear weight if not quantized
dtype = weight.dtype

if is_quantized:
dtype = mx.float16
weight = mx.dequantize(
weight,
linear.scales,
linear.biases,
linear.group_size,
linear.bits,
group_size=linear.group_size,
bits=linear.bits,
mode=linear.mode,
)
num_experts, output_dims, input_dims = weight.shape
fused_linear = SwitchLinear(input_dims, output_dims, num_experts, bias=bias)

lora_b = (self.scale * self.lora_b).astype(dtype)
lora_a = self.lora_a.reshape(num_experts, -1, input_dims).astype(dtype)
fused_linear.weight = weight + lora_b @ lora_a
lora_b = self.scale * self.lora_b
lora_a = self.lora_a.reshape(num_experts, -1, input_dims)
fused_linear.weight = weight + (lora_b @ lora_a).astype(weight.dtype)
if bias:
fused_linear.bias = linear.bias

if is_quantized and not dequantize:
fused_linear = fused_linear.to_quantized(linear.group_size, linear.bits)
fused_linear = fused_linear.to_quantized(
group_size=linear.group_size, bits=linear.bits, mode=linear.mode
)

return fused_linear

Expand Down Expand Up @@ -224,30 +221,28 @@ def fuse(self, dequantize: bool = False):
weight = embedding.weight
is_quantized = isinstance(embedding, nn.QuantizedEmbedding)

# Use the same type as the linear weight if not quantized
dtype = weight.dtype

if is_quantized:
dtype = embedding.scales.dtype
weight = mx.dequantize(
weight,
embedding.scales,
embedding.biases,
embedding.group_size,
embedding.bits,
group_size=embedding.group_size,
bits=embedding.bits,
mode=embedding.mode,
)
num_embeddings, dims = weight.shape
fused_embedding = nn.Embedding(num_embeddings, dims)

lora_a = (self.scale * self.lora_a).astype(dtype)
lora_b = self.lora_b.astype(dtype)
fused_embedding.weight = weight + lora_a @ lora_b
lora_a = self.scale * self.lora_a
lora_b = self.lora_b
fused_embedding.weight = weight + (lora_a @ lora_b).astype(weight.dtype)

if is_quantized and not dequantize:
fused_embedding = nn.QuantizedEmbedding.from_embedding(
fused_embedding,
embedding.group_size,
embedding.bits,
group_size=embedding.group_size,
bits=embedding.bits,
mode=embedding.mode,
)

return fused_embedding
Expand Down
Loading