Skip to content

Commit f3daa57

Browse files
authored
Refactor LoRAFeedForward.forward to use shared lora_call (#20831)
Differential Revision: D110890567 Pull Request resolved: #20831
1 parent 5869165 commit f3daa57

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

examples/models/llama/feed_forward.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import torch.nn.functional as F
22

3-
from executorch.examples.models.llama.lora import LoRALinear
3+
from executorch.examples.models.llama.lora import lora_call, LoRALinear
44
from executorch.examples.models.llama.model_args import ModelArgs
55
from torch import nn
66

@@ -65,15 +65,8 @@ def __init__(self, dim: int, hidden_dim: int, args: ModelArgs):
6565
)
6666

6767
def forward(self, x, lora_blob=None):
68-
# CoreML LoRA-as-IO Path-2: when `lora_blob` is provided, route per-
69-
# projection slices to LoRALinear instances tagged with `_lora_key`.
70-
# Default behavior (lora_blob=None) is unchanged.
71-
def _call(linear, x_in):
72-
if lora_blob is not None:
73-
key = getattr(linear, "_lora_key", None)
74-
if key is not None and key in lora_blob:
75-
a, b = lora_blob[key]
76-
return linear(x_in, a, b)
77-
return linear(x_in)
78-
79-
return _call(self.w2, F.silu(_call(self.w1, x)) * _call(self.w3, x))
68+
return lora_call(
69+
self.w2,
70+
F.silu(lora_call(self.w1, x, lora_blob)) * lora_call(self.w3, x, lora_blob),
71+
lora_blob,
72+
)

0 commit comments

Comments
 (0)