Skip to content

Block attention residuals support - #1277

Draft
odilf wants to merge 4 commits into
looped-transformerfrom
block-attention-residuals
Draft

odilf wants to merge 4 commits into
looped-transformerfrom
block-attention-residuals

Conversation

@odilf

@odilf odilf commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

support for block residuals. Changes include:

  • stream is now a ResidualSteam<B>
  • sublayer input is now an enum which can contain separate KQV
  • pre-norm moved to the residual connection rather than the layer (incidentally, this makes it nicer to re-fuse the kernels for the additive residuals).

@odilf
odilf added this pull request to stack #1266 November 30, 2026 14:32
@odilf
odilf force-pushed the block-attention-residuals branch from 62f5adf to 3edb67d Compare November 30, 2026 15:03
struct CacheStep {
layer: LayerId,
state: StepState,
// `Some` iff we're using block attention residuals.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the comment doesn't seem to match the code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I was on drugs or something 🤠 . fixed

reads,
starts_block,
message_gates,
read: <B::Kernels as Kernels>::AttnResReadKernel::new(context, data_type, data_type, 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

all three call sites build the read kernel with query_count = 1 so maybe simplify the code given that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh, you're right. That's there because of lalamo but lalamo has that to do the KQV projections in one, while I'm doing a loop because I'm storing them as three buffers, but that's actually worse for performance. Hopefully it's not too bad. I think I'll leave the query_count for now but also add a TODO with the fact that the buffers can just be merged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just made the change to have everything in one buffer now.

parameter_tree: &ParameterTree<'_, B>,
context: &B::Context,
) -> Result<Self, ResidualNewError<B>> {
let rank = config.rank;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

MessageGateConfig.linear_config doesn't seem to be read anywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LinearConfig is an empty struct, it's that way everywhere, and it's never read anywhere 🤷

@odilf
odilf force-pushed the block-attention-residuals branch from 3edb67d to 37de35c Compare November 30, 2026 18:36
@odilf
odilf force-pushed the block-attention-residuals branch from 37de35c to 7e68170 Compare December 1, 2026 19:15
@odilf
odilf force-pushed the block-attention-residuals branch from 909d3b4 to 816357a Compare December 1, 2026 20:17
@odilf
odilf force-pushed the block-attention-residuals branch from 816357a to 9375e1e Compare December 2, 2026 11:02
@odilf
odilf force-pushed the block-attention-residuals branch 3 times, most recently from bd28c7d to db19692 Compare December 2, 2026 18:24
@odilf
odilf force-pushed the block-attention-residuals branch from db19692 to 8117721 Compare December 2, 2026 22:42
// The Metal and Vulkan kernels keep every source's score in threadgroup memory until the softmax, so they
// need a fixed size. Their `MAX_SOURCES` must match this one (like `MAX_K` in `radix_top_k_small.rs`). 64 is
// arbitrary; raising it just uses more threadgroup memory.
pub const MAX_SOURCES: u32 = 64;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move to common/gpu_type.rs ?

Comment on lines +648 to +650
let start_index = start_index.unwrap();
let slot_size = blocks.size() / self.schedule.block_source_capacity as usize;
let pass_range = start_index * slot_size..(*blocks_len + 1) * slot_size;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we move the block save and rewind into the residual module? will be easier for future fusion

Comment on lines +96 to +97
VARIANTS(ReadT, float, half, bfloat)
VARIANTS(T, float, half, bfloat)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do our models really need all these types? same for all other kernels

#define BLOCK_SIZE 1024
// Must match `MAX_SOURCES` in `backends/common/kernel/attention_residual.rs`, which explains the limit.
constant uint MAX_SOURCES = 64;
#define MAX_QUERIES 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also move to gpu_type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or a variant


using namespace metal;

#define BLOCK_SIZE 1024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also move to gpu_type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or a variant

Comment on lines +15 to +16
VARIANTS(SourceT, float, half, bfloat)
VARIANTS(OutputT, float, half, bfloat)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

probably don't need all these types?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants