Skip to content

Commit ca7dcfa

Browse files
committed
refactor-attention
1 parent 52b4483 commit ca7dcfa

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

examples/models/llama/attention.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ def forward(
162162

163163
@register_attention("mha")
164164
class AttentionMHA(Attention):
165-
def __init__(self, args: ModelArgs, layer_id: int, rope: Rope):
165+
def __init__(
166+
self,
167+
args: ModelArgs,
168+
layer_id: int,
169+
rope: Rope,
170+
):
166171
super().__init__()
167172
self.use_kv_cache = args.use_kv_cache
168173
self.n_heads = args.n_heads

examples/models/llama/llama_transformer.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import torch
1313
import torch.nn.functional as F
1414

15-
from executorch.examples.models.llama.attention import (
16-
ATTENTION_REGISTRY,
17-
ForwardOptions,
18-
)
15+
from executorch.examples.models.llama.attention import Attention, ForwardOptions
1916

2017
from executorch.examples.models.llama.model_args import ModelArgs
2118
from executorch.examples.models.llama.norm import RMSNorm
@@ -83,19 +80,13 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
8380

8481

8582
class TransformerBlock(nn.Module):
86-
def __init__(self, layer_id: int, args: ModelArgs, rope: Rope):
83+
def __init__(self, args: ModelArgs, attention: Attention):
8784
super().__init__()
8885
self.use_kv_cache = args.use_kv_cache
8986
self.n_heads = args.n_heads
9087
self.dim = args.dim
9188
self.head_dim = args.head_dim
92-
if args.attention_type not in ATTENTION_REGISTRY:
93-
raise ValueError(
94-
f"Unknown attention type: {args.attention_type}. "
95-
f"Available: {list(ATTENTION_REGISTRY.keys())}"
96-
)
97-
cls = ATTENTION_REGISTRY[args.attention_type]
98-
self.attention = cls(args, layer_id, rope)
89+
self.attention = attention
9990
if args.moe:
10091
self.block_sparse_moe = MOEFeedForward(args)
10192
else:
@@ -117,7 +108,7 @@ def forward(self, x, freqs_cos, freqs_sin, attn_options: ForwardOptions): # x:
117108

118109

119110
class Transformer(nn.Module):
120-
def __init__(self, params: ModelArgs):
111+
def __init__(self, params: ModelArgs, layers: nn.ModuleList, rope: Rope):
121112
super().__init__()
122113
self.params = params
123114
self.vocab_size = params.vocab_size
@@ -130,10 +121,8 @@ def __init__(self, params: ModelArgs):
130121
if self.apply_embedding
131122
else None
132123
)
133-
self.rope = Rope(params)
134-
self.layers = torch.nn.ModuleList()
135-
for layer_id in range(params.n_layers):
136-
self.layers.append(TransformerBlock(layer_id, params, self.rope))
124+
self.layers = layers
125+
self.rope = rope
137126
self.norm = RMSNorm(params.dim, eps=params.norm_eps)
138127
self.output = (
139128
nn.Linear(params.dim, params.vocab_size, bias=False)

examples/models/llama/model.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
get_checkpoint_dtype,
1616
get_default_model_resource_dir,
1717
)
18-
from executorch.examples.models.llama.llama_transformer import Transformer
19-
18+
from executorch.examples.models.llama.attention import ATTENTION_REGISTRY
19+
from executorch.examples.models.llama.llama_transformer import (
20+
Transformer,
21+
TransformerBlock,
22+
)
2023
from executorch.examples.models.llama.model_args import ModelArgs
24+
from executorch.examples.models.llama.rope import Rope
2125

2226
try:
2327
from .fairseq2 import convert_to_llama_checkpoint
@@ -173,7 +177,24 @@ def __init__(self, **kwargs):
173177
# They possess all other metadata a tensor carries such as size, stride, requires_grad.
174178
with torch.device("meta"):
175179
# Model itself is loaded in default dtype, fp32.
176-
self.model_ = Transformer(model_args)
180+
181+
# Construct attention layers.
182+
rope = Rope(model_args)
183+
if model_args.attention_type not in ATTENTION_REGISTRY:
184+
raise ValueError(
185+
f"Unknown attention type: {model_args.attention_type}. "
186+
f"Available: {list(ATTENTION_REGISTRY.keys())}"
187+
)
188+
layers = torch.nn.ModuleList()
189+
cls = ATTENTION_REGISTRY[model_args.attention_type]
190+
for layer_id in range(model_args.n_layers):
191+
attention = cls(model_args, layer_id, rope)
192+
transformer_block = TransformerBlock(model_args, attention)
193+
layers.append(transformer_block)
194+
195+
# Construct transformer model.
196+
self.model_ = Transformer(model_args, layers, rope)
197+
177198
# Get checkpoint dtype.
178199
if checkpoint:
179200
self.model_.checkpoint_dtype = get_checkpoint_dtype(checkpoint)

0 commit comments

Comments
 (0)