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
split layer loop
  • Loading branch information
guarin committed Nov 19, 2026
commit a6a4af276c808c761736490a287121b2ba77097b
82 changes: 47 additions & 35 deletions src/transformers/models/inkling2_loop/modeling_inkling2_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,54 +1234,66 @@ def forward(
"linear_attention": create_recurrent_attention_mask(**mask_kwargs),
}

loop_layer_index_start, loop_layer_index_end = (
self.config.loop_layer_index_start,
self.config.loop_layer_index_end,
)
# pre loop layers, then `num_passes` times the loop layers, then post loop layers
layer_and_loop_indices = [(layer_idx, 0) for layer_idx in range(loop_layer_index_start)]
layer_and_loop_indices += [
(layer_idx, loop_idx)
for loop_idx in range(self.config.loop_num_passes)
for layer_idx in range(loop_layer_index_start, loop_layer_index_end)
]
layer_and_loop_indices += [
(layer_idx, 0) for layer_idx in range(loop_layer_index_end, self.config.num_hidden_layers)
]

cache_layer_indices = [[] for _ in range(self.config.num_hidden_layers)]
for cache_layer_idx, layer_idx in enumerate(self.config.cache_layer_indices):
cache_layer_indices[layer_idx].append(cache_layer_idx)
loop_start = self.config.loop_layer_index_start
loop_end = self.config.loop_layer_index_end
num_loop_layers = loop_end - loop_start

hidden_states = inputs_embeds
block_states = inputs_embeds.new_zeros(*inputs_embeds.shape[:-1], 0, inputs_embeds.shape[-1])
for layer_idx, loop_idx in layer_and_loop_indices:
decoder_layer = self.layers[layer_idx]

if layer_idx == loop_layer_index_start and loop_idx == 0:
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
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
for decoder_layer in self.layers[:loop_start]:
new_hidden_states = decoder_layer(
hidden_states,
block_states,
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
position_ids=position_ids,
**kwargs,
)
if decoder_layer.is_block_boundary:
block_states = torch.cat([block_states.to(hidden_states.device), hidden_states.unsqueeze(-2)], dim=-2)
hidden_states = new_hidden_states

messages = []
pre_loop_hidden_states, pre_loop_block_states = hidden_states, block_states
num_pre_loop_blocks = pre_loop_block_states.shape[-2]
for loop_idx in range(self.config.loop_num_passes):
cache_layer_start = loop_start
if loop_idx > 0:
messages.append(
self.message_input(
previous_block_states, previous_hidden_states, layer_idx - loop_layer_index_start
)
# +1 to skip the states appended by the first loop block
previous_block_states = block_states[..., num_pre_loop_blocks + 1 :, :]
previous_hidden_states = hidden_states
# Pass i > 0 uses its own copies of the loop cache layers, appended after the model's layers
cache_layer_start = self.config.num_hidden_layers + (loop_idx - 1) * num_loop_layers

hidden_states, block_states = pre_loop_hidden_states, pre_loop_block_states
for loop_layer_idx, decoder_layer in enumerate(self.layers[loop_start:loop_end]):
messages = None
if loop_idx > 0:
messages = self.message_input(previous_block_states, previous_hidden_states, loop_layer_idx)
new_hidden_states = decoder_layer(
hidden_states,
block_states,
messages, # pass positionally for gradient checkpointing
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
cache_layer_idx=cache_layer_start + loop_layer_idx,
position_ids=position_ids,
**kwargs,
)
if decoder_layer.is_block_boundary:
block_states = torch.cat(
[block_states.to(hidden_states.device), hidden_states.unsqueeze(-2)], dim=-2
)
hidden_states = new_hidden_states

for decoder_layer in self.layers[loop_end:]:
new_hidden_states = decoder_layer(
hidden_states,
block_states,
*messages, # pass positionally for gradient checkpointing
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
cache_layer_idx=cache_layer_indices[layer_idx][loop_idx],
position_ids=position_ids,
**kwargs,
)
Expand Down
82 changes: 47 additions & 35 deletions src/transformers/models/inkling2_loop/modular_inkling2_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,54 +782,66 @@ def forward(
"linear_attention": create_recurrent_attention_mask(**mask_kwargs),
}

loop_layer_index_start, loop_layer_index_end = (
self.config.loop_layer_index_start,
self.config.loop_layer_index_end,
)
# pre loop layers, then `num_passes` times the loop layers, then post loop layers
layer_and_loop_indices = [(layer_idx, 0) for layer_idx in range(loop_layer_index_start)]
layer_and_loop_indices += [
(layer_idx, loop_idx)
for loop_idx in range(self.config.loop_num_passes)
for layer_idx in range(loop_layer_index_start, loop_layer_index_end)
]
layer_and_loop_indices += [
(layer_idx, 0) for layer_idx in range(loop_layer_index_end, self.config.num_hidden_layers)
]

cache_layer_indices = [[] for _ in range(self.config.num_hidden_layers)]
for cache_layer_idx, layer_idx in enumerate(self.config.cache_layer_indices):
cache_layer_indices[layer_idx].append(cache_layer_idx)
loop_start = self.config.loop_layer_index_start
loop_end = self.config.loop_layer_index_end
num_loop_layers = loop_end - loop_start

hidden_states = inputs_embeds
block_states = inputs_embeds.new_zeros(*inputs_embeds.shape[:-1], 0, inputs_embeds.shape[-1])
for layer_idx, loop_idx in layer_and_loop_indices:
decoder_layer = self.layers[layer_idx]

if layer_idx == loop_layer_index_start and loop_idx == 0:
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
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
for decoder_layer in self.layers[:loop_start]:
new_hidden_states = decoder_layer(
hidden_states,
block_states,
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
position_ids=position_ids,
**kwargs,
)
if decoder_layer.is_block_boundary:
block_states = torch.cat([block_states.to(hidden_states.device), hidden_states.unsqueeze(-2)], dim=-2)
hidden_states = new_hidden_states

messages = []
pre_loop_hidden_states, pre_loop_block_states = hidden_states, block_states
num_pre_loop_blocks = pre_loop_block_states.shape[-2]
for loop_idx in range(self.config.loop_num_passes):
cache_layer_start = loop_start
if loop_idx > 0:
messages.append(
self.message_input(
previous_block_states, previous_hidden_states, layer_idx - loop_layer_index_start
)
# +1 to skip the states appended by the first loop block
previous_block_states = block_states[..., num_pre_loop_blocks + 1 :, :]
previous_hidden_states = hidden_states
# Pass i > 0 uses its own copies of the loop cache layers, appended after the model's layers
cache_layer_start = self.config.num_hidden_layers + (loop_idx - 1) * num_loop_layers

hidden_states, block_states = pre_loop_hidden_states, pre_loop_block_states
for loop_layer_idx, decoder_layer in enumerate(self.layers[loop_start:loop_end]):
messages = None
if loop_idx > 0:
messages = self.message_input(previous_block_states, previous_hidden_states, loop_layer_idx)
new_hidden_states = decoder_layer(
hidden_states,
block_states,
messages, # pass positionally for gradient checkpointing
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
cache_layer_idx=cache_layer_start + loop_layer_idx,
position_ids=position_ids,
**kwargs,
)
if decoder_layer.is_block_boundary:
block_states = torch.cat(
[block_states.to(hidden_states.device), hidden_states.unsqueeze(-2)], dim=-2
)
hidden_states = new_hidden_states

for decoder_layer in self.layers[loop_end:]:
new_hidden_states = decoder_layer(
hidden_states,
block_states,
*messages, # pass positionally for gradient checkpointing
attention_mask=causal_mask_mapping[decoder_layer.attention_type],
conv_mask=causal_mask_mapping["linear_attention"],
cache=cache,
cache_layer_idx=cache_layer_indices[layer_idx][loop_idx],
position_ids=position_ids,
**kwargs,
)
Expand Down