Skip to content

Commit 0eb8247

Browse files
authored
Extract _lora_call into a shared free function in lora.py (#20306)
Differential Revision: D108757232 Pull Request resolved: #20306
1 parent ef5c8a7 commit 0eb8247

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

examples/models/llama/lora.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ def forward(
6969
z = self.lora_a(self.dropout(x))
7070
z = (self.alpha / self.rank) * self.lora_b(z)
7171
return out + z
72+
73+
74+
def lora_call(linear, x_in, lora_blob):
75+
if lora_blob is not None:
76+
key = getattr(linear, "_lora_key", None)
77+
if key is not None and key in lora_blob:
78+
a, b = lora_blob[key]
79+
return linear(x_in, a, b)
80+
return linear(x_in)

examples/models/llama/static_attention.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ForwardOptions,
1414
register_attention,
1515
)
16-
from executorch.examples.models.llama.lora import LoRALinear
16+
from executorch.examples.models.llama.lora import lora_call, LoRALinear
1717
from executorch.examples.models.llama.model_args import ModelArgs
1818
from executorch.examples.models.llama.norm import ScalelessRMSNorm
1919
from executorch.examples.models.llama.rope import Rope
@@ -1014,14 +1014,6 @@ def from_attention_mha(
10141014

10151015
return instance
10161016

1017-
def _lora_call(self, linear, x_in, lora_blob):
1018-
if lora_blob is not None:
1019-
key = getattr(linear, "_lora_key", None)
1020-
if key is not None and key in lora_blob:
1021-
a, b = lora_blob[key]
1022-
return linear(x_in, a, b)
1023-
return linear(x_in)
1024-
10251017
def forward(
10261018
self,
10271019
x: torch.Tensor,
@@ -1044,7 +1036,7 @@ def forward(
10441036
# Default behavior (no blob, or no `_lora_key`) is unchanged.
10451037
_lora_blob = kwargs.get("__lora_io_blob__")
10461038

1047-
new_qs = [self._lora_call(wq, x, _lora_blob) for wq in self.wqs]
1039+
new_qs = [lora_call(wq, x, _lora_blob) for wq in self.wqs]
10481040

10491041
shared_kv = kwargs.get("shared_kv")
10501042
if shared_kv is not None:
@@ -1054,8 +1046,8 @@ def forward(
10541046
new_ks = []
10551047
new_vs = []
10561048
else:
1057-
new_ks = [self._lora_call(wk, x, _lora_blob) for wk in self.wks]
1058-
new_vs = [self._lora_call(wv, x, _lora_blob) for wv in self.wvs]
1049+
new_ks = [lora_call(wk, x, _lora_blob) for wk in self.wks]
1050+
new_vs = [lora_call(wv, x, _lora_blob) for wv in self.wvs]
10591051

10601052
if self.use_conv2d:
10611053

@@ -1092,7 +1084,7 @@ def from_conv2ds(ts):
10921084

10931085
if self.use_conv2d:
10941086
y = (
1095-
self._lora_call(
1087+
lora_call(
10961088
self.wo,
10971089
y.reshape(bsz, -1, 1, self.n_heads * self.head_dim).transpose(1, 3),
10981090
_lora_blob,
@@ -1101,7 +1093,7 @@ def from_conv2ds(ts):
11011093
.reshape(bsz, -1, self.dim)
11021094
)
11031095
else:
1104-
y = self._lora_call(self.wo, y, _lora_blob)
1096+
y = lora_call(self.wo, y, _lora_blob)
11051097

11061098
update = {"out_cache_state": out_cache_state}
11071099
if kv_to_share is not None:

0 commit comments

Comments
 (0)