Skip to content

Add configurable lora_alpha parameter for LoRA in multiple Keras layers #21139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: Fix LoRA test failures by using ops to do numpy conversion
  • Loading branch information
b05505027 committed Apr 5, 2025
commit a62f75b7e8f55524e9d8013d08d96947a1042ef3
3 changes: 2 additions & 1 deletion keras/src/layers/convolutional/conv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from keras.src import constraints
from keras.src import layers
from keras.src import models
from keras.src import ops
from keras.src import saving
from keras.src import testing

Expand Down Expand Up @@ -777,7 +778,7 @@ def test_enable_lora_with_alpha(self):
expected_effective_kernel = base_kernel + scaling * delta

# Compare the effective kernel computed via the property.
actual_effective_kernel = layer.kernel.numpy()
actual_effective_kernel = ops.convert_to_numpy(layer.kernel)
self.assertAllClose(actual_effective_kernel, expected_effective_kernel)

@pytest.mark.requires_trainable_backend
Expand Down
5 changes: 3 additions & 2 deletions keras/src/layers/core/dense_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ def test_enable_lora_with_alpha(self):
# Manually compute the expected effective kernel:
# effective_kernel = base_kernel +
# (lora_alpha / lora_rank) * (lora_kernel_a @ lora_kernel_b)
base_kernel = layer._kernel.numpy()
base_kernel = ops.convert_to_numpy(layer._kernel)
lora_update = np.matmul(
layer.lora_kernel_a.numpy(), layer.lora_kernel_b.numpy()
ops.convert_to_numpy(layer.lora_kernel_a.numpy()),
ops.convert_to_numpy(layer.lora_kernel_b.numpy()),
)
effective_kernel_expected = base_kernel + (3.0 / 2) * lora_update

Expand Down
2 changes: 1 addition & 1 deletion keras/src/layers/core/einsum_dense_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def test_enable_lora_with_alpha(self):
expected_kernel = base_kernel + expected_delta

# Verify that the effective kernel property returns the expected value.
actual_kernel = layer.kernel.numpy()
actual_kernel = ops.convert_to_numpy(layer.kernel)
self.assertAllClose(actual_kernel, expected_kernel)

@pytest.mark.requires_trainable_backend
Expand Down
2 changes: 1 addition & 1 deletion keras/src/layers/core/embedding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_enable_lora_with_alpha(self):
expected_embeddings = base_emb + effective_delta

# Verify that the effective embeddings match expectation.
actual_embeddings = layer.embeddings.numpy()
actual_embeddings = ops.convert_to_numpy(layer.embeddings)
self.assertAllClose(actual_embeddings, expected_embeddings)

@pytest.mark.requires_trainable_backend
Expand Down
Loading