Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
78762b2
add initial tests
guarin Nov 17, 2026
0bab3c2
init draft version
guarin Nov 17, 2026
01147ff
Merge branch 'main' into add-inkling2-loop
guarin Nov 17, 2026
a69e017
update tests
guarin Nov 18, 2026
02ee726
more todo
guarin Nov 18, 2026
39b88b8
update cache layer handling
guarin Nov 18, 2026
aa0cc26
cleanup
guarin Nov 18, 2026
ef5f4bb
split layers
guarin Nov 18, 2026
3f2a85f
cleanup message passing
guarin Nov 18, 2026
f848297
update gates
guarin Nov 18, 2026
aea1583
refactor message passing
guarin Nov 18, 2026
7434736
cleanup
guarin Nov 18, 2026
1c79cce
update
guarin Nov 19, 2026
9354250
cleanup
guarin Nov 19, 2026
abe7127
remove defensive checks
guarin Nov 19, 2026
e2abf0f
move to correct place
guarin Nov 19, 2026
29b216e
dequant step 1
guarin Nov 19, 2026
737f980
flatten loop config
guarin Nov 19, 2026
6fa0050
merge message queries
guarin Nov 19, 2026
6f3482c
fix
guarin Nov 19, 2026
1aa26ba
norm
guarin Nov 19, 2026
5a3dab4
update init weights
guarin Nov 19, 2026
a6a4af2
split layer loop
guarin Nov 19, 2026
2983e57
more splitting
guarin Nov 19, 2026
c1846e6
fix tests
guarin Nov 19, 2026
ff6dc64
rename
guarin Nov 19, 2026
d03e8d3
update inkling2 attentions
guarin Nov 19, 2026
9507570
remove unneeded layers
guarin Nov 19, 2026
06661c0
more cleanup
guarin Nov 19, 2026
ef20ef1
cleanup
guarin Nov 19, 2026
e23ef0f
refactor relative logits
guarin Nov 19, 2026
908b61b
merge k/v proj
guarin Nov 19, 2026
cb6894c
move things
guarin Nov 19, 2026
1a8cad7
fix packed sequences
guarin Nov 20, 2026
cc4f307
add Inkling2GatedOutputProjection
guarin Nov 20, 2026
d425f55
drop sdpa
guarin Nov 20, 2026
6427d4f
refactor scales
guarin Nov 20, 2026
6620ca6
refactor rel pos bias
guarin Nov 20, 2026
eb892bf
minor
guarin Nov 20, 2026
32049ef
fix repo
guarin Nov 20, 2026
ed58c54
handle unpadded_vocab_size
guarin Nov 20, 2026
3add884
merge kv proj
guarin Nov 20, 2026
6e657c3
fix repo
guarin Nov 20, 2026
9a94031
fix head loading
guarin Nov 20, 2026
3b75972
fix mxfp8 loading
guarin Nov 20, 2026
b01cd5f
use attn interface
guarin Nov 20, 2026
48141e7
text_hidden_size is now in the config
guarin Nov 20, 2026
1a15086
use dory caching
guarin Nov 20, 2026
c9ccbdb
move norm to proj
guarin Nov 20, 2026
9aed9db
move test
guarin Nov 20, 2026
e843e8f
update docs
guarin Nov 20, 2026
9ed3671
add todo
guarin Nov 20, 2026
18c8ac8
update tests
guarin Nov 20, 2026
3ed39b6
update docs
guarin Nov 20, 2026
46c36e5
add blog link
guarin Nov 20, 2026
6b1044a
tiny checkpoint
guarin Nov 20, 2026
9a1ff82
Merge branch 'main' into add-inkling2-loop
guarin Nov 20, 2026
eb4b4a3
cleanup
guarin Nov 20, 2026
6819273
make linter happy
guarin Nov 20, 2026
8d628c7
cleanup
guarin Nov 20, 2026
09c356e
add a10 expectations
guarin Nov 20, 2026
f0fd853
skip export tests
guarin Nov 20, 2026
e916f57
Merge branch 'main' into add-inkling2-mini
guarin Nov 20, 2026
c5b376c
update links
guarin Nov 20, 2026
e18dccc
update links
guarin Nov 20, 2026
e3a65e2
Merge branch 'add-inkling2-mini' of https://github.com/huggingface/tr…
guarin Nov 20, 2026
6173a2f
update tests
guarin Nov 20, 2026
bc3f715
fix fp32 loading
guarin Nov 21, 2026
a10ebc3
add kernels note
guarin Nov 21, 2026
430050f
consistency
Cyrilvallez Nov 21, 2026
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
Prev Previous commit
Next Next commit
refactor message passing
  • Loading branch information
guarin committed Nov 18, 2026
commit aea158376aac57269de201aad87df5e59895f020
9 changes: 5 additions & 4 deletions src/transformers/conversion_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -2593,12 +2593,13 @@ def _build_checkpoint_conversion_mapping():
WeightRenaming(
source_patterns=r"_message_gate\.forget_weight", target_patterns=r"_message_gate.forget_gate_proj.weight"
),
# (2 * num_looped_layers, hidden_size) each, rows `2 * i` and `2 * i + 1` of core layer `i` hold its query and
# MLP queries in the first and its key and value queries in the second. Side by side, core layer `i` holds
# its query, key, MLP and value queries back to back.
# The original checkpoint stores two (2 * num_layers, hidden_dim) queries parameters:
# - middle_cycle_cross_pass_queries: contains query and mlp queries
# - middle_cycle_cross_pass_kv_queries: contains key and value queries
# We merge them into a single "query" param.
WeightConverter(
source_patterns=[r"middle_cycle_cross_pass_queries", r"middle_cycle_cross_pass_kv_queries"],
target_patterns=r"cross_pass_messages.queries",
target_patterns=r"message_input.query",
operations=[Concatenate(dim=1)],
),
] + mapping["inkling2_text"].copy()
Expand Down
45 changes: 25 additions & 20 deletions src/transformers/models/inkling2_loop/modeling_inkling2_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,30 @@ def forward(self, hidden_states: torch.Tensor, message: torch.Tensor) -> torch.T
return (1 - forget_gate) * hidden_states + message_gate * message


class Inkling2LoopCrossPassMessages(nn.Module):
"""Retrieves messages from the message bank (residual blocks from previous pass) using learned queries."""
class Inkling2LoopMessageAttentionResidual(nn.Module):
"""Retrieves messages from the block states of the previous block using learned queries."""

def __init__(self, config: Inkling2LoopTextConfig):
super().__init__()
# 4 * num_loop_layers queries, shape is (2 * num_loop_layers, 2 * hidden_dim) because of original checkpoint shapes.
self.queries = nn.Parameter(
self.query = nn.Parameter(
torch.zeros(
2 * (config.loop_config["core_end"] - config.loop_config["core_start"]), 2 * config.hidden_size
)
)
self.norm = Inkling2LoopUnweightedRMSNorm()

def forward(self, message_bank: torch.Tensor, loop_layer_idx: int) -> torch.Tensor:
bank_states = self.norm(message_bank.float())
hidden_size = bank_states.shape[-1]
def forward(
self, block_states: torch.Tensor, partial_block_states: torch.Tensor, loop_layer_idx: int
) -> torch.Tensor:
stacked_states = torch.cat([block_states, partial_block_states.unsqueeze(-2)], dim=-2).float()
stacked_states = self.norm(stacked_states)

hidden_size = stacked_states.shape[-1]
# 4 query states for query, key, mlp, and value queries
query_states = self.queries.view(-1, 4, hidden_size)[loop_layer_idx].float()
query_states = query_states.expand(*bank_states.shape[:-2], -1, -1)
return F.scaled_dot_product_attention(query_states, bank_states, bank_states, scale=hidden_size**-0.5)
query_states = self.query.view(-1, 4, hidden_size)[loop_layer_idx].float()
query_states = query_states.expand(*stacked_states.shape[:-2], -1, -1)
return F.scaled_dot_product_attention(query_states, stacked_states, stacked_states, scale=hidden_size**-0.5)


def apply_mask_to_padding_states(hidden_states, attention_mask):
Expand Down Expand Up @@ -1027,7 +1031,7 @@ def forward(
)

if messages is not None:
query_message, key_message, _, value_message = messages.unbind(-2)
query_message, key_message, mlp_message, value_message = messages.unbind(-2)
query_hidden_states = self.q_message_gate(query_hidden_states, query_message)
key_hidden_states = self.k_message_gate(key_hidden_states, key_message)
value_hidden_states = self.v_message_gate(value_hidden_states, value_message)
Expand All @@ -1054,7 +1058,7 @@ def forward(

hidden_states = self.mlp_input(block_states, hidden_states)[0]
if messages is not None:
hidden_states = self.mlp_message_gate(hidden_states, messages[..., 2, :])
hidden_states = self.mlp_message_gate(hidden_states, mlp_message)
hidden_states = self.mlp(self.post_attention_layernorm(hidden_states).to(dtype))
hidden_states = self.post_mlp_convolution(
hidden_states, cache=cache, conv_mask=conv_mask, cache_layer_idx=cache_layer_idx
Expand Down Expand Up @@ -1114,7 +1118,7 @@ class Inkling2LoopPreTrainedModel(PreTrainedModel):
"attention_input",
"mlp_input",
"final_input",
"cross_pass_messages",
"message_input",
"message_gate_proj",
"forget_gate_proj",
]
Expand Down Expand Up @@ -1147,10 +1151,8 @@ def _init_weights(self, module):
init.ones_(module.scale)
elif isinstance(module, Inkling2LoopDynamicShortConvolution):
init.zeros_(module.static_weight)
elif isinstance(module, Inkling2LoopBlockAttentionResidual):
elif isinstance(module, (Inkling2LoopBlockAttentionResidual, Inkling2LoopMessageAttentionResidual)):
init.zeros_(module.query)
elif isinstance(module, Inkling2LoopCrossPassMessages):
init.zeros_(module.queries)


class Inkling2LoopNormedEmbedding(nn.Embedding):
Expand Down Expand Up @@ -1187,7 +1189,7 @@ def __init__(self, config: Inkling2LoopTextConfig):
else:
layer_class = Inkling2LoopSlidingDecoderLayer if is_sliding else Inkling2LoopFullDecoderLayer
self.layers.append(layer_class(config, layer_idx))
self.cross_pass_messages = Inkling2LoopCrossPassMessages(config)
self.message_input = Inkling2LoopMessageAttentionResidual(config)

# Initialize weights and apply final processing
self.post_init()
Expand Down Expand Up @@ -1260,14 +1262,17 @@ def forward(
pre_loop_hidden_states, pre_loop_block_states = hidden_states, block_states
elif layer_idx == loop_layer_index_start:
num_pre_loop_blocks = pre_loop_block_states.shape[-2] + 1
message_bank = torch.cat(
[block_states[..., num_pre_loop_blocks:, :], hidden_states.unsqueeze(-2)], dim=-2
)
previous_block_states = block_states[..., num_pre_loop_blocks:, :]
previous_hidden_states = hidden_states
hidden_states, block_states = pre_loop_hidden_states, pre_loop_block_states

messages = []
if loop_idx > 0:
messages.append(self.cross_pass_messages(message_bank, layer_idx - loop_layer_index_start))
messages.append(
self.message_input(
previous_block_states, previous_hidden_states, layer_idx - loop_layer_index_start
)
)

new_hidden_states = decoder_layer(
hidden_states,
Expand Down
48 changes: 26 additions & 22 deletions src/transformers/models/inkling2_loop/modular_inkling2_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,29 @@ def forward(self, hidden_states: torch.Tensor, message: torch.Tensor) -> torch.T
return (1 - forget_gate) * hidden_states + message_gate * message


class Inkling2LoopCrossPassMessages(nn.Module):
"""Retrieves messages from the message bank (residual blocks from previous pass) using learned queries."""
class Inkling2LoopMessageAttentionResidual(Inkling2BlockAttentionResidual):
"""Retrieves messages from the block states of the previous block using learned queries."""

def __init__(self, config: Inkling2LoopTextConfig):
super().__init__()
super().__init__(config)
# 4 * num_loop_layers queries, shape is (2 * num_loop_layers, 2 * hidden_dim) because of original checkpoint shapes.
self.queries = nn.Parameter(
self.query = nn.Parameter(
torch.zeros(
2 * (config.loop_config["core_end"] - config.loop_config["core_start"]), 2 * config.hidden_size
)
)
self.norm = Inkling2LoopUnweightedRMSNorm()

def forward(self, message_bank: torch.Tensor, loop_layer_idx: int) -> torch.Tensor:
bank_states = self.norm(message_bank.float())
hidden_size = bank_states.shape[-1]
def forward(
self, block_states: torch.Tensor, partial_block_states: torch.Tensor, loop_layer_idx: int
) -> torch.Tensor:
stacked_states = torch.cat([block_states, partial_block_states.unsqueeze(-2)], dim=-2).float()
stacked_states = self.norm(stacked_states)

hidden_size = stacked_states.shape[-1]
# 4 query states for query, key, mlp, and value queries
query_states = self.queries.view(-1, 4, hidden_size)[loop_layer_idx].float()
query_states = query_states.expand(*bank_states.shape[:-2], -1, -1)
return F.scaled_dot_product_attention(query_states, bank_states, bank_states, scale=hidden_size**-0.5)
query_states = self.query.view(-1, 4, hidden_size)[loop_layer_idx].float()
query_states = query_states.expand(*stacked_states.shape[:-2], -1, -1)
return F.scaled_dot_product_attention(query_states, stacked_states, stacked_states, scale=hidden_size**-0.5)


@use_kernelized_func([causal_conv1d_update, causal_conv1d_fn])
Expand Down Expand Up @@ -638,7 +641,7 @@ def forward(
)

if messages is not None:
query_message, key_message, _, value_message = messages.unbind(-2)
query_message, key_message, mlp_message, value_message = messages.unbind(-2)
query_hidden_states = self.q_message_gate(query_hidden_states, query_message)
key_hidden_states = self.k_message_gate(key_hidden_states, key_message)
value_hidden_states = self.v_message_gate(value_hidden_states, value_message)
Expand All @@ -665,7 +668,7 @@ def forward(

hidden_states = self.mlp_input(block_states, hidden_states)[0]
if messages is not None:
hidden_states = self.mlp_message_gate(hidden_states, messages[..., 2, :])
hidden_states = self.mlp_message_gate(hidden_states, mlp_message)
hidden_states = self.mlp(self.post_attention_layernorm(hidden_states).to(dtype))
hidden_states = self.post_mlp_convolution(
hidden_states, cache=cache, conv_mask=conv_mask, cache_layer_idx=cache_layer_idx
Expand Down Expand Up @@ -695,7 +698,7 @@ class Inkling2LoopPreTrainedModel(Inkling2PreTrainedModel):
"attention_input",
"mlp_input",
"final_input",
"cross_pass_messages",
"message_input",
"message_gate_proj",
"forget_gate_proj",
]
Expand Down Expand Up @@ -728,10 +731,8 @@ def _init_weights(self, module):
init.ones_(module.scale)
elif isinstance(module, Inkling2LoopDynamicShortConvolution):
init.zeros_(module.static_weight)
elif isinstance(module, Inkling2LoopBlockAttentionResidual):
elif isinstance(module, (Inkling2LoopBlockAttentionResidual, Inkling2LoopMessageAttentionResidual)):
init.zeros_(module.query)
elif isinstance(module, Inkling2LoopCrossPassMessages):
init.zeros_(module.queries)


@auto_docstring
Expand All @@ -748,7 +749,7 @@ def __init__(self, config: Inkling2LoopTextConfig):
else:
layer_class = Inkling2LoopSlidingDecoderLayer if is_sliding else Inkling2LoopFullDecoderLayer
self.layers.append(layer_class(config, layer_idx))
self.cross_pass_messages = Inkling2LoopCrossPassMessages(config)
self.message_input = Inkling2LoopMessageAttentionResidual(config)

@merge_with_config_defaults
@capture_outputs
Expand Down Expand Up @@ -818,14 +819,17 @@ def forward(
pre_loop_hidden_states, pre_loop_block_states = hidden_states, block_states
elif layer_idx == loop_layer_index_start:
num_pre_loop_blocks = pre_loop_block_states.shape[-2] + 1
message_bank = torch.cat(
[block_states[..., num_pre_loop_blocks:, :], hidden_states.unsqueeze(-2)], dim=-2
)
previous_block_states = block_states[..., num_pre_loop_blocks:, :]
previous_hidden_states = hidden_states
hidden_states, block_states = pre_loop_hidden_states, pre_loop_block_states

messages = []
if loop_idx > 0:
messages.append(self.cross_pass_messages(message_bank, layer_idx - loop_layer_index_start))
messages.append(
self.message_input(
previous_block_states, previous_hidden_states, layer_idx - loop_layer_index_start
)
)

new_hidden_states = decoder_layer(
hidden_states,
Expand Down