Skip to content

Refactor attention v2 #10623

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 4 commits into from
May 6, 2025
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
73 changes: 60 additions & 13 deletions examples/models/llama/llama_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import torch.nn.functional as F

from executorch.examples.models.llama.attention import (
Attention,
ATTENTION_REGISTRY,
ForwardOptions,
)
Expand Down Expand Up @@ -83,26 +84,46 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:


class TransformerBlock(nn.Module):
def __init__(self, layer_id: int, args: ModelArgs, rope: Rope):
def __init__(self, args: ModelArgs, attention: Attention):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add doc string on each argument, especially the attention? I think it makes sense to me that Attention type is required, so that the API of user-defined attention is compatible with our transformer.

"""
Transformer block with support for pre-norm and post-norm.
Args:
args (ModelArgs): model configuration parameters.
attention (Attention): attention object to use in the transformer
block. See `attention.py` for types of attention. Make sure
the attention type is registered in the ATTENTION_REGISTRY.
"""
super().__init__()
self.use_kv_cache = args.use_kv_cache
self.n_heads = args.n_heads
self.dim = args.dim
self.head_dim = args.head_dim
if args.attention_type not in ATTENTION_REGISTRY:
raise ValueError(
f"Unknown attention type: {args.attention_type}. "
f"Available: {list(ATTENTION_REGISTRY.keys())}"
)
cls = ATTENTION_REGISTRY[args.attention_type]
self.attention = cls(args, layer_id, rope)
self.attention = attention
if args.moe:
self.block_sparse_moe = MOEFeedForward(args)
else:
self.feed_forward = FeedForward(args)
self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps)
self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps)

@classmethod
def from_type(cls, layer_id, args, rope) -> "TransformerBlock":
"""
Create a TransformerBlock with the legacy constructor.
Args:
layer_id (int): the index of the layer.
args (ModelArgs): model configuration parameters.
rope (Rope): the rope object to use for rotary embeddings.
"""
if args.attention_type not in ATTENTION_REGISTRY:
raise ValueError(
f"Unknown attention type: {args.attention_type}. "
f"Available: {list(ATTENTION_REGISTRY.keys())}"
)
cls = ATTENTION_REGISTRY[args.attention_type]
attention = cls(args, layer_id, rope)
return TransformerBlock(args, attention)

def forward(self, x, freqs_cos, freqs_sin, attn_options: ForwardOptions): # x: 1xN
h, attn_options_update = self.attention.forward(
self.attention_norm(x), freqs_cos, freqs_sin, **attn_options
Expand All @@ -117,7 +138,15 @@ def forward(self, x, freqs_cos, freqs_sin, attn_options: ForwardOptions): # x:


class Transformer(nn.Module):
def __init__(self, params: ModelArgs):
def __init__(self, params: ModelArgs, layers: nn.ModuleList, rope: Rope):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you are going to do this, might as well lift all of the major model components out as well, such as the embedding layer and rms norm, even though they are not customizable by model args at the moment

Copy link
Contributor Author

@lucylq lucylq May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can, but would prefer to have it in a separate PR if it's something we want to do. Is there a use-case, or more to make Transformer more modular?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, no use case atm, just for modularity. Just feels a bit weird to me seeing layers and rope be the only lifted inputs for Transformer

"""
Transformer model.
Args:
params (ModelArgs): model configuration parameters.
layers (nn.ModuleList): list of transformer blocks - see the
`TransformerBlock` type above.
rope (Rope): the rope object to use for rotary embeddings.
"""
super().__init__()
self.params = params
self.vocab_size = params.vocab_size
Expand All @@ -130,10 +159,8 @@ def __init__(self, params: ModelArgs):
if self.apply_embedding
else None
)
self.rope = Rope(params)
self.layers = torch.nn.ModuleList()
for layer_id in range(params.n_layers):
self.layers.append(TransformerBlock(layer_id, params, self.rope))
self.layers = layers
self.rope = rope
self.norm = RMSNorm(params.dim, eps=params.norm_eps)
self.output = (
nn.Linear(params.dim, params.vocab_size, bias=False)
Expand Down Expand Up @@ -212,3 +239,23 @@ def forward(
return logits, attn_options_update

return logits


def construct_transformer(model_args: ModelArgs) -> Transformer:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not @classmethod?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed offline; construct_transformer is likely going to be more high-level; not quite at model-creation, but will contain eg. lora instantiation so may not make sense for it to be part of the transformer class itself.

"""
Construct a Transformer model from the given model arguments.
"""
rope = Rope(model_args)
if model_args.attention_type not in ATTENTION_REGISTRY:
raise ValueError(
f"Unknown attention type: {model_args.attention_type}. "
f"Available: {list(ATTENTION_REGISTRY.keys())}"
)
layers = torch.nn.ModuleList()
cls = ATTENTION_REGISTRY[model_args.attention_type]
for layer_id in range(model_args.n_layers):
attention = cls(model_args, layer_id, rope)
transformer_block = TransformerBlock(model_args, attention)
layers.append(transformer_block)

return Transformer(model_args, layers, rope)
5 changes: 3 additions & 2 deletions examples/models/llama/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
get_checkpoint_dtype,
get_default_model_resource_dir,
)
from executorch.examples.models.llama.llama_transformer import Transformer

from executorch.examples.models.llama.llama_transformer import construct_transformer
from executorch.examples.models.llama.model_args import ModelArgs
from executorch.examples.models.llama.rope import Rope
from torchao.utils import TorchAOBaseTensor

try:
Expand Down Expand Up @@ -174,7 +175,7 @@ def __init__(self, **kwargs):
# They possess all other metadata a tensor carries such as size, stride, requires_grad.
with torch.device("meta"):
# Model itself is loaded in default dtype, fp32.
self.model_ = Transformer(model_args)
self.model_ = construct_transformer(model_args)
# Get checkpoint dtype.
if checkpoint:
self.model_.checkpoint_dtype = get_checkpoint_dtype(checkpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import unittest

import torch
from executorch.examples.models.llama.llama_transformer import Transformer
from executorch.examples.models.llama.llama_transformer import (
construct_transformer,
Transformer,
)
from executorch.examples.models.llama.model_args import ModelArgs
from executorch.examples.models.llama.source_transformation.pre_quantization import (
sanitize_checkpoint_from_pre_quantization,
Expand Down Expand Up @@ -39,7 +42,7 @@ def _prepare_dummy_model(self) -> Transformer:
vocab_size=32000,
)

model = Transformer(model_args)
model = construct_transformer(model_args)

return model

Expand Down
6 changes: 3 additions & 3 deletions examples/models/llama/tests/test_static_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import torch
from executorch.examples.models.llama.attention import AttentionMHA, ForwardOptions
from executorch.examples.models.llama.llama_transformer import Transformer
from executorch.examples.models.llama.llama_transformer import construct_transformer
from executorch.examples.models.llama.model_args import ModelArgs
from executorch.examples.models.llama.rope import Rope
from executorch.examples.models.llama.static_attention import (
Expand Down Expand Up @@ -160,10 +160,10 @@ def test_within_transformer(self):
n_layers=4,
vocab_size=128,
)
mha_transformer = Transformer(config).eval()
mha_transformer = construct_transformer(config).eval()

config.attention_type = "static"
static_transformer = Transformer(config).eval()
static_transformer = construct_transformer(config).eval()
static_transformer.load_state_dict(mha_transformer.state_dict(), strict=False)
for mha_layer, static_layer in zip(
mha_transformer.layers, static_transformer.layers
Expand Down
4 changes: 2 additions & 2 deletions examples/models/llava/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import requests
import torch
from executorch.examples.models.llama.llama_transformer import Transformer
from executorch.examples.models.llama.llama_transformer import construct_transformer
from executorch.examples.models.llama.model_args import ModelArgs

from executorch.examples.models.llama.source_transformation.custom_kv_cache import (
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(
use_hf_rope=True,
max_seq_len=max_seq_len,
)
self.text_model = Transformer(self.text_model_args)
self.text_model = construct_transformer(self.text_model_args)
# use custom op for SDPA.
if use_sdpa_with_kv_cache_op:
self.text_model = replace_kv_cache_with_custom_kv_cache(self.text_model)
Expand Down
Loading