stackformers 5.0.0rc10__tar.gz → 5.0.0rc14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/CHANGELOG.md +38 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/PKG-INFO +29 -1
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/README.md +28 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/pyproject.toml +1 -1
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/DEVLOG.md +166 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/__init__.py +31 -0
- stackformers-5.0.0rc14/stackformers/_projection_packing.py +72 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/README.md +37 -3
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/__init__.py +15 -1
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/config.py +66 -6
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/cross_attn.py +7 -0
- stackformers-5.0.0rc14/stackformers/attention/factory.py +65 -0
- stackformers-5.0.0rc14/stackformers/attention/packed_cross_attn.py +199 -0
- stackformers-5.0.0rc14/stackformers/attention/packed_self_attn.py +163 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/self_attn.py +7 -0
- stackformers-5.0.0rc14/stackformers/feedforward/DEVLOG.md +81 -0
- stackformers-5.0.0rc14/stackformers/feedforward/README.md +39 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/__init__.py +12 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/config.py +26 -1
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/factory.py +12 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/geglu.py +10 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/hardswish_glu.py +8 -0
- stackformers-5.0.0rc14/stackformers/feedforward/packed_geglu.py +71 -0
- stackformers-5.0.0rc14/stackformers/feedforward/packed_hardswish_glu.py +73 -0
- stackformers-5.0.0rc14/stackformers/feedforward/packed_swiglu.py +71 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/swiglu.py +8 -0
- stackformers-5.0.0rc14/stackformers/packing.py +222 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/README.md +29 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/cross_attender.py +4 -4
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/decoder.py +11 -7
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/encoder.py +4 -4
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/variable_width_encoder.py +4 -4
- stackformers-5.0.0rc14/tests/attention/test_factory.py +109 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_legacy_api.py +14 -3
- stackformers-5.0.0rc14/tests/attention/test_packed_cross_attn.py +155 -0
- stackformers-5.0.0rc14/tests/attention/test_packed_self_attn.py +140 -0
- stackformers-5.0.0rc14/tests/feedforward/legacy_swiglu_4_6.json +47 -0
- stackformers-5.0.0rc14/tests/feedforward/test_factory.py +41 -0
- stackformers-5.0.0rc14/tests/feedforward/test_packed_geglu.py +129 -0
- stackformers-5.0.0rc14/tests/feedforward/test_packed_hardswish_glu.py +133 -0
- stackformers-5.0.0rc14/tests/feedforward/test_packed_swiglu.py +130 -0
- stackformers-5.0.0rc14/tests/presets/test_projection_packing.py +236 -0
- stackformers-5.0.0rc14/tests/test_packing.py +419 -0
- stackformers-5.0.0rc14/tests/test_projection_packing.py +82 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/uv.lock +1 -1
- stackformers-5.0.0rc10/stackformers/attention/factory.py +0 -24
- stackformers-5.0.0rc10/stackformers/feedforward/DEVLOG.md +0 -27
- stackformers-5.0.0rc10/stackformers/feedforward/README.md +0 -13
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/.claudeignore +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/.gitignore +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/.python-version +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/.vscode/settings.json +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/AGENTS.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/Justfile +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/LICENSE +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/PLAN.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/README.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/causal_language_model.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/encoder_decoder_onnxruntime.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/image_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/padded_and_packed.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/point_cloud_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/examples/subclassed_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/pyrefly.toml +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/pytest.ini +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/ruff.toml +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/run-remote-tests.sh +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/_result.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/bias.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/cache.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/cache_validation.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/cached.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/distance_bias.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/layout.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/masking.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/ops.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/protocols.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/attention/varlen_backend.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/config.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/cross_attender.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/decoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/decoder_cache.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/gelu.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/protocols.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/feedforward/relu_squared.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/layers.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/README.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/config.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/head.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/head_cosine.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/masking.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/protocols.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/mlm/wrapper.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/norm/README.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/norm/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/norm/config.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/norm/factory.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/norm/protocols.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/README.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/config.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/factory.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/learned.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/none.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/protocols.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/rope1d.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/rope2d.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/rope_nd.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/positional/validation.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/presets/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/py.typed +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/result/README.md +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/result/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/stackformers/sequence.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/legacy_api_rc1.json +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_contracts.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_cross_attn.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_cross_attn_cache.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_distance_bias.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_empty_rows.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_geometry.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_kernels.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_masking.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_ops.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_sdpa_export.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_self_attn.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/attention/test_varlen_backend.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/conftest.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/export_utils.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/test_geglu.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/test_gelu.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/test_hardswish_glu.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/test_relu_squared.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/feedforward/test_swiglu.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/test_empty_selection.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/test_head.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/test_head_cosine.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/test_masking.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/mlm/test_wrapper.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/norm_topology_helpers.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/positional/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/positional/test_cacheable.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/positional/test_learned_pos.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/positional/test_rope.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/positional/test_rope_nd.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/presets/__init__.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/presets/test_cross_attender.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/presets/test_decoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/presets/test_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/presets/test_variable_width_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_core_results.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_cross_attender.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_decoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_decoder_cache.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_encoder.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_encoder_decoder_example.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_examples.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_layers.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_norm.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_public_api.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_result.py +0 -0
- {stackformers-5.0.0rc10 → stackformers-5.0.0rc14}/tests/test_sequence.py +0 -0
|
@@ -6,6 +6,44 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and
|
|
|
6
6
|
adheres to [Semantic Versioning](https://semver.org/): MAJOR for breaking public API changes,
|
|
7
7
|
MINOR for backwards-compatible features, PATCH for bug fixes and internal changes.
|
|
8
8
|
|
|
9
|
+
## [5.0.0rc14]
|
|
10
|
+
|
|
11
|
+
### New Features
|
|
12
|
+
|
|
13
|
+
- Add recursive `to_packed(nn.Module)` and `to_unpacked(nn.Module)` helpers for supported
|
|
14
|
+
attention and gated feed-forward layers. Replace registered children in place and return
|
|
15
|
+
the root, preserving weights, training flags, and shared layer references.
|
|
16
|
+
- Synchronize built-in preset configs with converted checkpoint layouts without mutating
|
|
17
|
+
caller-owned configs. Check incompatible trainability and cache wrappers before mutation.
|
|
18
|
+
|
|
19
|
+
## [5.0.0rc13]
|
|
20
|
+
|
|
21
|
+
### New Features
|
|
22
|
+
|
|
23
|
+
- Add `PackedGEGLU` and `PackedHardSwishGLU` with matching concrete configs, factory and
|
|
24
|
+
preset selection, and JSON discriminators. Existing GEGLU and HardSwishGLU checkpoints
|
|
25
|
+
load normally before `to_packed()`; `unpack()` restores the original layout.
|
|
26
|
+
|
|
27
|
+
## [5.0.0rc12]
|
|
28
|
+
|
|
29
|
+
### New Features
|
|
30
|
+
|
|
31
|
+
- Select packed projections directly with `PackedSelfAttentionConfig`,
|
|
32
|
+
`PackedCrossAttentionConfig`, and `PackedSwiGLUConfig` through factories and presets.
|
|
33
|
+
JSON discriminators reconstruct the matching layout before loading packed checkpoints.
|
|
34
|
+
- Keep legacy untagged attention configs loading as ordinary projections. Conversion and
|
|
35
|
+
unpacking produce matching concrete config types; each constructor accepts its own config.
|
|
36
|
+
|
|
37
|
+
## [5.0.0rc11]
|
|
38
|
+
|
|
39
|
+
### New Features
|
|
40
|
+
|
|
41
|
+
- Add `to_packed()` to self-attention, cross-attention and SwiGLU for opt-in packed
|
|
42
|
+
QKV, context KV, and gate/value projections. GQA/MQA retain explicit Q/KV widths.
|
|
43
|
+
- Preserve ordinary forward paths and checkpoint names. Load old checkpoints normally,
|
|
44
|
+
then convert; `packed.unpack()` restores an independently owned ordinary module.
|
|
45
|
+
Convert before creating an optimizer; optimizer state and parameter aliases are not migrated.
|
|
46
|
+
|
|
9
47
|
## [5.0.0rc10]
|
|
10
48
|
|
|
11
49
|
### Developers
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: stackformers
|
|
3
|
-
Version: 5.0.
|
|
3
|
+
Version: 5.0.0rc14
|
|
4
4
|
Summary: Typed, composable, SOLID transformer library for PyTorch
|
|
5
5
|
Project-URL: Homepage, https://github.com/Red-Eyed/stackformers
|
|
6
6
|
Project-URL: Repository, https://github.com/Red-Eyed/stackformers
|
|
@@ -256,6 +256,34 @@ encoder = Encoder(layers=layers, final_norm=RMSNorm(RMSNormConfig(dim=512)))
|
|
|
256
256
|
|
|
257
257
|
---
|
|
258
258
|
|
|
259
|
+
## Convert projection layouts
|
|
260
|
+
|
|
261
|
+
Load an ordinary checkpoint, then recursively pack supported self-attention, cross-attention,
|
|
262
|
+
SwiGLU, GEGLU, and HardSwishGLU layers:
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
from stackformers import to_packed, to_unpacked
|
|
266
|
+
|
|
267
|
+
model.load_state_dict(checkpoint)
|
|
268
|
+
model = to_packed(model)
|
|
269
|
+
# Run or save the packed model.
|
|
270
|
+
model = to_unpacked(model)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
These helpers replace registered children in place and return the root, which may itself be
|
|
274
|
+
replaced. Repeated calls keep layers already in the requested layout. Shared layer references
|
|
275
|
+
and retained child modules stay shared. Built-in preset configs follow the new layout; save
|
|
276
|
+
`model.config.model_dump_json()` alongside its state dict to reconstruct that layout later.
|
|
277
|
+
Caller-owned config objects are left unchanged. Custom subclasses keep their own behavior
|
|
278
|
+
and metadata; supported children are still visited.
|
|
279
|
+
|
|
280
|
+
Weights round-trip exactly; outputs match within dtype tolerance because matrix multiplication
|
|
281
|
+
rounding can change. Convert before constructing an optimizer: replaced projection parameters
|
|
282
|
+
do not migrate optimizer state, existing gradients, hooks, or cross-layer parameter aliases.
|
|
283
|
+
Groups with mixed `requires_grad` fail before mutation. Existing cache wrappers require ordinary
|
|
284
|
+
attention, so packing an already-wrapped cache executor is rejected before mutation.
|
|
285
|
+
Projection packing is independent of the `PackedInput` sequence layout.
|
|
286
|
+
|
|
259
287
|
## What's included
|
|
260
288
|
|
|
261
289
|
| Area | Variants |
|
|
@@ -207,6 +207,34 @@ encoder = Encoder(layers=layers, final_norm=RMSNorm(RMSNormConfig(dim=512)))
|
|
|
207
207
|
|
|
208
208
|
---
|
|
209
209
|
|
|
210
|
+
## Convert projection layouts
|
|
211
|
+
|
|
212
|
+
Load an ordinary checkpoint, then recursively pack supported self-attention, cross-attention,
|
|
213
|
+
SwiGLU, GEGLU, and HardSwishGLU layers:
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
from stackformers import to_packed, to_unpacked
|
|
217
|
+
|
|
218
|
+
model.load_state_dict(checkpoint)
|
|
219
|
+
model = to_packed(model)
|
|
220
|
+
# Run or save the packed model.
|
|
221
|
+
model = to_unpacked(model)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
These helpers replace registered children in place and return the root, which may itself be
|
|
225
|
+
replaced. Repeated calls keep layers already in the requested layout. Shared layer references
|
|
226
|
+
and retained child modules stay shared. Built-in preset configs follow the new layout; save
|
|
227
|
+
`model.config.model_dump_json()` alongside its state dict to reconstruct that layout later.
|
|
228
|
+
Caller-owned config objects are left unchanged. Custom subclasses keep their own behavior
|
|
229
|
+
and metadata; supported children are still visited.
|
|
230
|
+
|
|
231
|
+
Weights round-trip exactly; outputs match within dtype tolerance because matrix multiplication
|
|
232
|
+
rounding can change. Convert before constructing an optimizer: replaced projection parameters
|
|
233
|
+
do not migrate optimizer state, existing gradients, hooks, or cross-layer parameter aliases.
|
|
234
|
+
Groups with mixed `requires_grad` fail before mutation. Existing cache wrappers require ordinary
|
|
235
|
+
attention, so packing an already-wrapped cache executor is rejected before mutation.
|
|
236
|
+
Projection packing is independent of the `PackedInput` sequence layout.
|
|
237
|
+
|
|
210
238
|
## What's included
|
|
211
239
|
|
|
212
240
|
| Area | Variants |
|
|
@@ -1,5 +1,171 @@
|
|
|
1
1
|
# Transformer model development log
|
|
2
2
|
|
|
3
|
+
## 2026-10-18 — Recursive projection conversion (5.0.0rc14)
|
|
4
|
+
|
|
5
|
+
### Observation and decision
|
|
6
|
+
|
|
7
|
+
The per-layer conversions require callers to replace every nested attention and gated
|
|
8
|
+
feed-forward manually and keep checkpoint configs in sync. Add public `to_packed(nn.Module)`
|
|
9
|
+
and `to_unpacked(nn.Module)` adapters that reuse concrete conversion methods. Prepare each
|
|
10
|
+
replacement once per module identity before changing registered child slots. Reconnect retained
|
|
11
|
+
children so shared layer references, positional encodings, biases, and other collaborators
|
|
12
|
+
remain shared. Use the returned root because it may itself be replaced.
|
|
13
|
+
|
|
14
|
+
Copy built-in preset configs into the selected variants without changing caller-owned config
|
|
15
|
+
objects. Leave custom subclass behavior and metadata under the caller's control. No new
|
|
16
|
+
projection protocol, base class, or forward dispatch is introduced. Mixed trainability and
|
|
17
|
+
existing cache wrappers fail before tree mutation; packed cached execution remains unsupported.
|
|
18
|
+
|
|
19
|
+
### Verified result and limits
|
|
20
|
+
|
|
21
|
+
Focused tests pass for all five supported layer types, masked padded and packed sequence inputs,
|
|
22
|
+
all four presets and normalization topologies, all gated variants and a non-gated FFN, nested
|
|
23
|
+
containers, shared siblings/collaborators, standalone roots, idempotence, and mixed layouts.
|
|
24
|
+
Exact weight restoration and matching config/checkpoint reloads are verified independently
|
|
25
|
+
of output tolerance. `just check` passes: 2,220 passed, 21 skipped, 172 expected failures,
|
|
26
|
+
and 111 optional-case unexpected passes; formatting, lint, and types also pass.
|
|
27
|
+
Trainability and training modes survive conversion. Output parity uses
|
|
28
|
+
dtype tolerances because packing can change floating-point rounding. Convert before creating
|
|
29
|
+
an optimizer; replaced projection parameters do not migrate optimizer state, gradients,
|
|
30
|
+
hooks, or cross-layer parameter aliases. No new performance or model-quality claim is made.
|
|
31
|
+
|
|
32
|
+
## 2026-10-18 — Extend packed feed-forward selection (5.0.0rc13)
|
|
33
|
+
|
|
34
|
+
GEGLU and HardSwishGLU now have independent packed variants and concrete configs selected
|
|
35
|
+
through the existing feed-forward factory, including preset construction. Their gate and
|
|
36
|
+
value projections share one matrix multiplication while retaining their original activation,
|
|
37
|
+
dropout, and output projection. Original implementations remain available for old checkpoints;
|
|
38
|
+
`to_packed()` and `unpack()` provide explicit transitions. No preset wiring or new behavior
|
|
39
|
+
abstraction is required. `just check` passes with 1,799 tests passed, 21 skipped, 172 expected
|
|
40
|
+
failures, and 111 optional-case unexpected passes. See the feed-forward DEVLOG for verification scope. No speedup or
|
|
41
|
+
model-quality claim is established for these variants.
|
|
42
|
+
|
|
43
|
+
## 2026-10-18 — Construct packed layouts from concrete configs (5.0.0rc12)
|
|
44
|
+
|
|
45
|
+
### Observation and decision
|
|
46
|
+
|
|
47
|
+
The explicit conversion API in `67f5b40` preserves old checkpoints, but reconstructing a
|
|
48
|
+
packed model should not require constructing and replacing ordinary projections first.
|
|
49
|
+
Introduce separate `PackedSelfAttentionConfig`, `PackedCrossAttentionConfig`, and
|
|
50
|
+
`PackedSwiGLUConfig` records. Factories exhaustively match discriminated unions; constructors
|
|
51
|
+
accept only their matching concrete config. Existing presets delegate to these factories.
|
|
52
|
+
Shared config fields remain in private data bases without adding behavioral abstractions.
|
|
53
|
+
|
|
54
|
+
Persist `kind` in JSON so saved configs reconstruct the matching state-dict layout. Adapt
|
|
55
|
+
legacy untagged attention JSON to the ordinary variant at validation. Conversion and
|
|
56
|
+
unpacking create the destination config explicitly. Ordinary forward implementations and
|
|
57
|
+
checkpoint names remain intact; existing cache wrappers still require ordinary attention.
|
|
58
|
+
A converted model's state dict must be saved alongside a matching packed preset config.
|
|
59
|
+
|
|
60
|
+
### Verified result and limits
|
|
61
|
+
|
|
62
|
+
Focused checks cover factory dispatch, config transitions, legacy JSON, and strict checkpoint
|
|
63
|
+
reload/output parity for all four presets, all normalization topologies, independent attention
|
|
64
|
+
and SwiGLU layout selections, and supported device/dtype combinations. `just check` passes:
|
|
65
|
+
1,769 passed, 21 skipped, 172 expected failures, and 111 optional-case unexpected passes;
|
|
66
|
+
formatting, lint, and types also pass. Existing conversion and numerical parity tests pass.
|
|
67
|
+
This change does not alter projection equations or
|
|
68
|
+
establish new performance results; the previous complete-block experiment remains the evidence.
|
|
69
|
+
|
|
70
|
+
## 2026-10-18 — Opt-in projection packing (5.0.0rc11)
|
|
71
|
+
|
|
72
|
+
### Observation
|
|
73
|
+
|
|
74
|
+
Self-attention projects the same input three times; cross-attention projects its context
|
|
75
|
+
twice; SwiGLU projects gate and value independently. QuantSmith's isolated QKV measurements
|
|
76
|
+
suggested packing could reduce integer-projection overhead, but did not establish complete-block gains.
|
|
77
|
+
|
|
78
|
+
### Decision
|
|
79
|
+
|
|
80
|
+
Add `to_packed()` to the existing attention and SwiGLU classes. Preserve their original forward
|
|
81
|
+
implementations, parameter names, factory dispatch, and cache wrappers. Put experimental
|
|
82
|
+
`PackedSelfAttention`, `PackedCrossAttention`, and `PackedSwiGLU` in separate modules, using
|
|
83
|
+
ordinary `nn.Linear` and explicit split widths. No new protocol or abstract hierarchy is needed.
|
|
84
|
+
|
|
85
|
+
Load old checkpoints into the original classes, then convert. Conversion copies parameters,
|
|
86
|
+
preserves dtype/device, frozen parameters and training modes, and leaves the source untouched.
|
|
87
|
+
`unpack()` returns an independently owned original module with its original checkpoint names.
|
|
88
|
+
Projection groups must share trainability. Convert before creating an optimizer; optimizer
|
|
89
|
+
state, existing gradients, hooks and parameter aliases are not migrated. Sequence packing and
|
|
90
|
+
projection packing are independent: both projection layouts accept padded and packed inputs.
|
|
91
|
+
|
|
92
|
+
### Verified
|
|
93
|
+
|
|
94
|
+
`just check` passes: 1,614 tests passed, 21 skipped, 172 expected failures, and 111 optional-case
|
|
95
|
+
unexpected passes. New checks cover forward and gradient parity, MHA/GQA/MQA, masks, learned
|
|
96
|
+
bias, local visibility, checkpoint serialization, frozen/tied parameters, training dropout,
|
|
97
|
+
and fresh optimizer steps. Released attention references from 5.0.0rc1 retain exact checkpoint
|
|
98
|
+
keys and output/input-gradient behavior after conversion. A SwiGLU reference captured from
|
|
99
|
+
the installed 4.6.0 wheel verifies the old checkpoint transition independently of current code.
|
|
100
|
+
|
|
101
|
+
All 12 attention dynamic-export tests pass in QuantSmith's PyTorch 2.14 environment. Four
|
|
102
|
+
PyTorch 2.11 GQA cases remain expected failures only after confirming that the ordinary
|
|
103
|
+
implementation encounters the same symbolic stride-guard error. The other new export cases pass.
|
|
104
|
+
|
|
105
|
+
### Complete-block ORT CPU experiment
|
|
106
|
+
|
|
107
|
+
QuantSmith's existing exporter, integer lowering, profiler and timing runner were reused with
|
|
108
|
+
an experiment adapter under ignored `work_dir/projection_packing/`. This run uses the local
|
|
109
|
+
5.0.0rc11 source checkout (file hashes recorded in `source.json`), PyTorch 2.14.0, ORT 1.30.0,
|
|
110
|
+
ONNX 1.23.0, arm64 macOS 27.0.1, batch 1, width 768 and 12 heads. The causal encoder has one
|
|
111
|
+
RoPE/RMSNorm/SwiGLU layer and a final norm; cross-attention reprojects a 128-token context.
|
|
112
|
+
Neither workload uses a KV cache. Weights and inputs are identical across projection layouts.
|
|
113
|
+
|
|
114
|
+
Each candidate uses independent native opset-18/21 exports with dynamic sequence axes,
|
|
115
|
+
CPUExecutionProvider, ORT_ENABLE_ALL, one intra/inter-op thread, 10 warmups and three
|
|
116
|
+
alternating-order sweeps of 300 calls. Profiling uses a separate 10-call session. Initialization
|
|
117
|
+
and weight packing are excluded. Values below are opset-18 medians of repetition medians.
|
|
118
|
+
|
|
119
|
+
| Block/layout | Query tokens | FP32 µs | Native dynamic INT8 µs |
|
|
120
|
+
|---|---:|---:|---:|
|
|
121
|
+
| Encoder, unchanged | 1 | 393.90 | 137.10 |
|
|
122
|
+
| Encoder, attention packed | 1 | 389.25 | 125.71 |
|
|
123
|
+
| Encoder, SwiGLU packed | 1 | 391.21 | 120.79 |
|
|
124
|
+
| Encoder, both packed | 1 | 388.06 | 106.85 |
|
|
125
|
+
| Encoder, unchanged | 128 | 1758.96 | 3672.31 |
|
|
126
|
+
| Encoder, attention packed | 128 | 1780.42 | 2950.37 |
|
|
127
|
+
| Encoder, SwiGLU packed | 128 | 1897.25 | 2353.94 |
|
|
128
|
+
| Encoder, both packed | 128 | 1831.33 | 1637.06 |
|
|
129
|
+
| Cross-attention, unchanged | 1 | 241.56 | 727.67 |
|
|
130
|
+
| Cross-attention, KV packed | 1 | 260.29 | 263.67 |
|
|
131
|
+
| Cross-attention, unchanged | 128 | 590.44 | 1135.12 |
|
|
132
|
+
| Cross-attention, KV packed | 128 | 610.44 | 672.44 |
|
|
133
|
+
|
|
134
|
+
Combined packing improves the encoder's native INT8 path by 1.28× at one token and 2.24×
|
|
135
|
+
at 128 tokens, reducing integer projections from seven to four. FP32 improvements are not
|
|
136
|
+
consistent: combined packing is about 4% slower at 128 tokens, and packing context KV also
|
|
137
|
+
regresses FP32 here. Native INT8 cross-attention remains slower than its FP32 baseline despite
|
|
138
|
+
its improvement from packing. Keep all layouts explicit rather than changing defaults.
|
|
139
|
+
|
|
140
|
+
All 144 graph variants execute at query/context pairs (1,1), (2,7), (7,2), and (31,13):
|
|
141
|
+
576 graph/length validations. FP32 matches PyTorch; native dynamic and four fixed-grid integer
|
|
142
|
+
paths return finite FP32 outputs with correct shapes. All 72 graph pairs have identical hashes
|
|
143
|
+
between the one-token and 128-token measurements. Only FP32/native dynamic candidates are
|
|
144
|
+
timed in this run; fixed-grid integer paths are correctness diagnostics.
|
|
145
|
+
|
|
146
|
+
Native INT8 relative MSE against unchanged FP32 is about 0.000033 for the combined one-token
|
|
147
|
+
encoder and 0.000018 at 128 tokens. These synthetic errors are not task-accuracy budgets.
|
|
148
|
+
|
|
149
|
+
Raw reports, graphs, profiles, the generated adapter, and source fingerprints remain in
|
|
150
|
+
`work_dir/projection_packing/`. Re-run the adapter from that directory with the adjacent
|
|
151
|
+
QuantSmith checkout and its locked example dependencies:
|
|
152
|
+
|
|
153
|
+
```sh
|
|
154
|
+
PYTHONPATH=../..:../../../QuantSmith \
|
|
155
|
+
uv run --locked --group examples --project ../../../QuantSmith benchmark.py
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`environment.json` reports installed distribution metadata (including Stackformers 4.6.0 in
|
|
159
|
+
QuantSmith's pinned environment); `source.json` identifies the actual selected local 5.0.0rc11
|
|
160
|
+
code and its hashes. The QuantSmith dependency pin was not changed.
|
|
161
|
+
|
|
162
|
+
### Unproven
|
|
163
|
+
|
|
164
|
+
CUDA execution, trained-model task quality, optimizer-state migration, and cached execution of
|
|
165
|
+
the packed variants have not been established. Existing cache wrappers continue to use ordinary
|
|
166
|
+
attention. Quantization and workload-dependent precision selection remain deployment concerns.
|
|
167
|
+
|
|
168
|
+
|
|
3
169
|
## 2026-09-26 — Precise cache and vendor input contracts (5.0.0rc7)
|
|
4
170
|
|
|
5
171
|
### Observation
|
|
@@ -12,17 +12,25 @@ from stackformers.attention import (
|
|
|
12
12
|
CrossAttention,
|
|
13
13
|
CrossAttentionConfig,
|
|
14
14
|
CrossAttentionKVCache,
|
|
15
|
+
CrossAttentionVariantConfig,
|
|
15
16
|
CrossAttn,
|
|
16
17
|
DecoderCrossAttentionCache,
|
|
17
18
|
DecoderStepOutput,
|
|
18
19
|
DistanceBiasConfig,
|
|
19
20
|
NoAttnBias,
|
|
20
21
|
NoAttnBiasConfig,
|
|
22
|
+
PackedCrossAttention,
|
|
23
|
+
PackedCrossAttentionConfig,
|
|
24
|
+
PackedSelfAttention,
|
|
25
|
+
PackedSelfAttentionConfig,
|
|
21
26
|
RelativeDistanceBias,
|
|
22
27
|
SelfAttention,
|
|
23
28
|
SelfAttentionConfig,
|
|
29
|
+
SelfAttentionVariantConfig,
|
|
24
30
|
SelfAttn,
|
|
25
31
|
build_attn_bias,
|
|
32
|
+
build_cross_attn,
|
|
33
|
+
build_self_attn,
|
|
26
34
|
)
|
|
27
35
|
from stackformers.attention.masking import CrossAttentionMask, SelfAttentionMask
|
|
28
36
|
from stackformers.config import DecoderConfig, EncoderConfig, LayerConfig
|
|
@@ -53,6 +61,12 @@ from stackformers.feedforward import (
|
|
|
53
61
|
GELUConfig,
|
|
54
62
|
HardSwishGLU,
|
|
55
63
|
HardSwishGLUConfig,
|
|
64
|
+
PackedGEGLU,
|
|
65
|
+
PackedGEGLUConfig,
|
|
66
|
+
PackedHardSwishGLU,
|
|
67
|
+
PackedHardSwishGLUConfig,
|
|
68
|
+
PackedSwiGLU,
|
|
69
|
+
PackedSwiGLUConfig,
|
|
56
70
|
ReluSquaredConfig,
|
|
57
71
|
ReluSquaredFF,
|
|
58
72
|
SwiGLU,
|
|
@@ -75,6 +89,7 @@ from stackformers.mlm.wrapper import MLMOutput, MLMWrapper
|
|
|
75
89
|
from stackformers.norm.config import LayerNormConfig, NormPlacement, RMSNormConfig
|
|
76
90
|
from stackformers.norm.factory import NormConfig, build_norm
|
|
77
91
|
from stackformers.norm.protocols import Norm
|
|
92
|
+
from stackformers.packing import to_packed, to_unpacked
|
|
78
93
|
from stackformers.positional import (
|
|
79
94
|
LearnedPosEncoding,
|
|
80
95
|
LearnedPosEncodingConfig,
|
|
@@ -132,6 +147,8 @@ from stackformers.sequence import (
|
|
|
132
147
|
)
|
|
133
148
|
|
|
134
149
|
__all__ = [
|
|
150
|
+
"to_packed",
|
|
151
|
+
"to_unpacked",
|
|
135
152
|
"SelfAttentionMask",
|
|
136
153
|
"CrossAttentionMask",
|
|
137
154
|
"__version__",
|
|
@@ -203,12 +220,24 @@ __all__ = [
|
|
|
203
220
|
"RotaryEmbeddingND",
|
|
204
221
|
"LearnedPosEncoding",
|
|
205
222
|
# attention
|
|
223
|
+
"PackedSelfAttentionConfig",
|
|
224
|
+
"PackedCrossAttentionConfig",
|
|
225
|
+
"SelfAttentionVariantConfig",
|
|
226
|
+
"CrossAttentionVariantConfig",
|
|
227
|
+
"PackedSelfAttention",
|
|
228
|
+
"PackedCrossAttention",
|
|
206
229
|
"SelfAttention",
|
|
207
230
|
"CrossAttention",
|
|
208
231
|
"NoAttnBias",
|
|
209
232
|
"RelativeDistanceBias",
|
|
210
233
|
# feedforward
|
|
211
234
|
"HardSwishGLU",
|
|
235
|
+
"PackedGEGLU",
|
|
236
|
+
"PackedGEGLUConfig",
|
|
237
|
+
"PackedHardSwishGLU",
|
|
238
|
+
"PackedHardSwishGLUConfig",
|
|
239
|
+
"PackedSwiGLUConfig",
|
|
240
|
+
"PackedSwiGLU",
|
|
212
241
|
"SwiGLU",
|
|
213
242
|
"GEGLU",
|
|
214
243
|
"GELUFFN",
|
|
@@ -218,6 +247,8 @@ __all__ = [
|
|
|
218
247
|
"build_ff",
|
|
219
248
|
"build_pos_encoding",
|
|
220
249
|
"build_attn_bias",
|
|
250
|
+
"build_cross_attn",
|
|
251
|
+
"build_self_attn",
|
|
221
252
|
# transformer blocks
|
|
222
253
|
"TransformerLayerBase",
|
|
223
254
|
"TransformerLayer",
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Owned weight conversion between separate and packed projection representations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
import torch.nn as nn
|
|
11
|
+
from torch import Tensor
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _load_owned_state(
|
|
15
|
+
source: nn.Module,
|
|
16
|
+
target: nn.Module,
|
|
17
|
+
state: dict[str, Tensor],
|
|
18
|
+
trainable: dict[str, bool],
|
|
19
|
+
) -> None:
|
|
20
|
+
"""Restore weights, frozen parameters, and individual module training modes."""
|
|
21
|
+
target.load_state_dict(state, assign=True)
|
|
22
|
+
for name, parameter in target.named_parameters():
|
|
23
|
+
parameter.requires_grad_(trainable[name])
|
|
24
|
+
source_modes = {name: module.training for name, module in source.named_modules()}
|
|
25
|
+
for name, module in target.named_modules():
|
|
26
|
+
module.training = source_modes.get(name, source.training)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load_packed_state(
|
|
30
|
+
source: nn.Module, target: nn.Module, separate: tuple[str, ...], packed: str
|
|
31
|
+
) -> None:
|
|
32
|
+
"""Copy weights into a new layout; reject incompatible frozen projection groups.
|
|
33
|
+
|
|
34
|
+
Parameter identities, existing gradients, optimizer state, and hooks are not transferred.
|
|
35
|
+
Convert before constructing an optimizer or loading its state.
|
|
36
|
+
"""
|
|
37
|
+
trainable = {
|
|
38
|
+
name: parameter.requires_grad
|
|
39
|
+
for name, parameter in source.named_parameters(remove_duplicate=False)
|
|
40
|
+
}
|
|
41
|
+
flags = {trainable[name] for name in separate}
|
|
42
|
+
if len(flags) != 1:
|
|
43
|
+
raise ValueError("packed projections must share requires_grad")
|
|
44
|
+
source_state = source.state_dict()
|
|
45
|
+
state = {name: value.clone() for name, value in source_state.items() if name not in separate}
|
|
46
|
+
state[packed] = torch.cat([source_state[name] for name in separate], dim=0)
|
|
47
|
+
for name in separate:
|
|
48
|
+
del trainable[name]
|
|
49
|
+
trainable[packed] = flags.pop()
|
|
50
|
+
_load_owned_state(source, target, state, trainable)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def load_unpacked_state(
|
|
54
|
+
source: nn.Module,
|
|
55
|
+
target: nn.Module,
|
|
56
|
+
packed: str,
|
|
57
|
+
separate: tuple[str, ...],
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Copy packed rows back to independently owned, checkpoint-compatible parameters."""
|
|
60
|
+
source_state = source.state_dict()
|
|
61
|
+
state = {name: value.clone() for name, value in source_state.items() if name != packed}
|
|
62
|
+
widths = tuple(target.get_parameter(name).shape[0] for name in separate)
|
|
63
|
+
chunks = source_state[packed].split(widths, dim=0)
|
|
64
|
+
trainable = {
|
|
65
|
+
name: parameter.requires_grad
|
|
66
|
+
for name, parameter in source.named_parameters(remove_duplicate=False)
|
|
67
|
+
}
|
|
68
|
+
flag = trainable.pop(packed)
|
|
69
|
+
for name, value in zip(separate, chunks, strict=True):
|
|
70
|
+
state[name] = value.clone()
|
|
71
|
+
trainable[name] = flag
|
|
72
|
+
_load_owned_state(source, target, state, trainable)
|
|
@@ -51,11 +51,45 @@ decoder, preset, and positional classes contain no cache-specific methods.
|
|
|
51
51
|
|
|
52
52
|
**Why not `flex_attention`.** It fuses the bias into the kernel and would make it O(n) instead of O(n²) — attempted, and parked on the `flex-attention` branch. It needs `dim_head >= 16`; its `score_mod` must lower as a *pointwise* subgraph, which forbids both a `vector_norm` over coordinates and a sum over shells; a captured tensor requiring grad may be indexed exactly once, which forbids unrolling that sum instead; called eagerly it is unfused and catastrophic (measured 64 s/step at n=2048, OOM at n=4096, against ~40 ms for SDPA), so it is viable only inside `torch.compile`; and under `torch.compile` with *dynamic* shapes it bails back to SDPA anyway. With node counts that vary per batch, the fused kernel would rarely be the one that runs.
|
|
53
53
|
|
|
54
|
+
## Packed projection experiments
|
|
55
|
+
|
|
56
|
+
Ordinary attention keeps its original forward path and checkpoint names. Call
|
|
57
|
+
`attention.to_packed()` to create an independent `PackedSelfAttention` or
|
|
58
|
+
`PackedCrossAttention`. These optional modules combine Q/K/V or context K/V with
|
|
59
|
+
`nn.Linear` and explicit splits; GQA/MQA retain unequal query and KV widths.
|
|
60
|
+
They support both padded and packed sequence inputs. No new protocol or base class is required.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
attention.load_state_dict(old_checkpoint)
|
|
64
|
+
packed = attention.to_packed()
|
|
65
|
+
output = packed(sequence)
|
|
66
|
+
ordinary = packed.unpack()
|
|
67
|
+
ordinary_checkpoint = ordinary.state_dict()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Convert after loading ordinary weights and before creating an optimizer. Conversion owns
|
|
71
|
+
new parameters, preserves dtype/device, frozen parameters and training modes, and leaves
|
|
72
|
+
the source untouched. Packing requires all projections in a group to share `requires_grad`.
|
|
73
|
+
Optimizer state, existing gradients, hooks and parameter aliases are not migrated.
|
|
74
|
+
`unpack()` restores ordinary checkpoint names; packed checkpoints load into their matching
|
|
75
|
+
packed class. Use `PackedSelfAttentionConfig` or `PackedCrossAttentionConfig` to construct
|
|
76
|
+
packed projections directly through `build_self_attn` or `build_cross_attn`. Each constructor
|
|
77
|
+
accepts its own concrete config; only factory inputs use the discriminated variant union.
|
|
78
|
+
The `kind` field survives JSON serialization so presets reconstruct the checkpoint layout.
|
|
79
|
+
Legacy attention JSON without `kind` selects ordinary attention. Conversion also changes the
|
|
80
|
+
module's config to the destination variant. Use ordinary attention with the existing cache wrappers.
|
|
81
|
+
|
|
82
|
+
Keep another experimental projection layout in its own module, with explicit conversion
|
|
83
|
+
and parity tests against ordinary attention. Measure complete blocks before selecting a
|
|
84
|
+
layout: fewer projections do not guarantee faster FP32 or integer inference. Quantization
|
|
85
|
+
and workload-dependent precision selection belong in deployment tooling.
|
|
86
|
+
|
|
54
87
|
## Extending
|
|
55
88
|
|
|
56
89
|
To add a new attention variant, create a new `nn.Module` that satisfies the `SelfAttn` or
|
|
57
|
-
`CrossAttn` protocol, including its keyword-only visibility argument.
|
|
58
|
-
|
|
59
|
-
using an override requires the collaborator to implement the
|
|
90
|
+
`CrossAttn` protocol, including its keyword-only visibility argument. For config-based construction,
|
|
91
|
+
add a discriminated config variant and a matching factory arm. Default calls preserve legacy
|
|
92
|
+
attention collaborator calls; using an override requires the collaborator to implement the
|
|
93
|
+
updated protocol.
|
|
60
94
|
|
|
61
95
|
To add a new attention bias, write an `nn.Module` satisfying `AttnBias` (take `PaddedInput`, return a broadcastable `(b, h, n, s)` tensor or `None`), give it a config with a `kind` discriminator, join it to the `AttnBiasConfig` union, and add one `match` arm to `build_attn_bias`.
|
|
@@ -8,14 +8,20 @@ from stackformers.attention.cached import CachedCrossAttentionWrapper, CachedSel
|
|
|
8
8
|
from stackformers.attention.config import (
|
|
9
9
|
AttnBiasConfig,
|
|
10
10
|
CrossAttentionConfig,
|
|
11
|
+
CrossAttentionVariantConfig,
|
|
11
12
|
DistanceBiasConfig,
|
|
12
13
|
NoAttnBiasConfig,
|
|
14
|
+
PackedCrossAttentionConfig,
|
|
15
|
+
PackedSelfAttentionConfig,
|
|
13
16
|
SelfAttentionConfig,
|
|
17
|
+
SelfAttentionVariantConfig,
|
|
14
18
|
)
|
|
15
19
|
from stackformers.attention.cross_attn import CrossAttention
|
|
16
20
|
from stackformers.attention.distance_bias import RelativeDistanceBias
|
|
17
|
-
from stackformers.attention.factory import build_attn_bias
|
|
21
|
+
from stackformers.attention.factory import build_attn_bias, build_cross_attn, build_self_attn
|
|
18
22
|
from stackformers.attention.masking import CrossAttentionMask, SelfAttentionMask
|
|
23
|
+
from stackformers.attention.packed_cross_attn import PackedCrossAttention
|
|
24
|
+
from stackformers.attention.packed_self_attn import PackedSelfAttention
|
|
19
25
|
from stackformers.attention.protocols import (
|
|
20
26
|
AttnBias,
|
|
21
27
|
CrossAttn,
|
|
@@ -24,6 +30,12 @@ from stackformers.attention.protocols import (
|
|
|
24
30
|
from stackformers.attention.self_attn import SelfAttention
|
|
25
31
|
|
|
26
32
|
__all__ = [
|
|
33
|
+
"PackedSelfAttentionConfig",
|
|
34
|
+
"PackedCrossAttentionConfig",
|
|
35
|
+
"SelfAttentionVariantConfig",
|
|
36
|
+
"CrossAttentionVariantConfig",
|
|
37
|
+
"PackedCrossAttention",
|
|
38
|
+
"PackedSelfAttention",
|
|
27
39
|
"SelfAttentionMask",
|
|
28
40
|
"CrossAttentionMask",
|
|
29
41
|
"SelfAttentionConfig",
|
|
@@ -44,4 +56,6 @@ __all__ = [
|
|
|
44
56
|
"NoAttnBias",
|
|
45
57
|
"RelativeDistanceBias",
|
|
46
58
|
"build_attn_bias",
|
|
59
|
+
"build_cross_attn",
|
|
60
|
+
"build_self_attn",
|
|
47
61
|
]
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import warnings
|
|
6
|
-
from typing import Annotated, Literal
|
|
6
|
+
from typing import Annotated, Literal, Self
|
|
7
7
|
|
|
8
|
-
from pydantic import BaseModel, Field, model_validator
|
|
8
|
+
from pydantic import BaseModel, BeforeValidator, Field, JsonValue, model_validator
|
|
9
9
|
|
|
10
10
|
from stackformers._result import unwrap_or_raise
|
|
11
11
|
from stackformers.result import Err, Ok, Result
|
|
@@ -44,7 +44,7 @@ def _warn_attn_dims(dim: int, heads: int, dim_head: int) -> None:
|
|
|
44
44
|
)
|
|
45
45
|
|
|
46
46
|
|
|
47
|
-
class
|
|
47
|
+
class _SelfAttentionConfigBase(BaseModel, frozen=True):
|
|
48
48
|
"""Immutable attention geometry and execution settings fixed at construction."""
|
|
49
49
|
|
|
50
50
|
dim: int = Field(
|
|
@@ -95,7 +95,7 @@ class SelfAttentionConfig(BaseModel, frozen=True):
|
|
|
95
95
|
)
|
|
96
96
|
|
|
97
97
|
@model_validator(mode="after")
|
|
98
|
-
def _validate(self) ->
|
|
98
|
+
def _validate(self) -> Self:
|
|
99
99
|
"""Translate core validation failures into the existing Pydantic error contract."""
|
|
100
100
|
unwrap_or_raise(_validate_head_grouping(self.heads, self.kv_heads))
|
|
101
101
|
_warn_attn_dims(self.dim, self.heads, self.dim_head)
|
|
@@ -110,6 +110,36 @@ class SelfAttentionConfig(BaseModel, frozen=True):
|
|
|
110
110
|
return self.heads // self.effective_kv_heads
|
|
111
111
|
|
|
112
112
|
|
|
113
|
+
class SelfAttentionConfig(_SelfAttentionConfigBase):
|
|
114
|
+
"""Self-attention with separate Q/K/V projections and ordinary checkpoint names."""
|
|
115
|
+
|
|
116
|
+
kind: Literal["self_attention"] = "self_attention"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class PackedSelfAttentionConfig(_SelfAttentionConfigBase):
|
|
120
|
+
"""Self-attention with one QKV projection, including unequal GQA/MQA widths."""
|
|
121
|
+
|
|
122
|
+
kind: Literal["packed_self_attention"] = "packed_self_attention"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _legacy_self_attention_kind(
|
|
126
|
+
value: SelfAttentionConfig | PackedSelfAttentionConfig | dict[str, JsonValue],
|
|
127
|
+
) -> SelfAttentionConfig | PackedSelfAttentionConfig | dict[str, JsonValue]:
|
|
128
|
+
"""Read legacy untagged attention JSON as the original separate-projection layout."""
|
|
129
|
+
match value:
|
|
130
|
+
case dict() if "kind" not in value:
|
|
131
|
+
return {**value, "kind": "self_attention"}
|
|
132
|
+
case _:
|
|
133
|
+
return value
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
SelfAttentionVariantConfig = Annotated[
|
|
137
|
+
SelfAttentionConfig | PackedSelfAttentionConfig,
|
|
138
|
+
Field(discriminator="kind"),
|
|
139
|
+
BeforeValidator(_legacy_self_attention_kind),
|
|
140
|
+
]
|
|
141
|
+
|
|
142
|
+
|
|
113
143
|
class NoAttnBiasConfig(BaseModel):
|
|
114
144
|
"""No attention bias — leaves varlen_attn available on the packed path."""
|
|
115
145
|
|
|
@@ -152,7 +182,7 @@ AttnBiasConfig = Annotated[
|
|
|
152
182
|
]
|
|
153
183
|
|
|
154
184
|
|
|
155
|
-
class
|
|
185
|
+
class _CrossAttentionConfigBase(BaseModel, frozen=True):
|
|
156
186
|
"""Immutable cross-attention settings shared with the constructed projections."""
|
|
157
187
|
|
|
158
188
|
dim: int = Field(
|
|
@@ -190,7 +220,7 @@ class CrossAttentionConfig(BaseModel, frozen=True):
|
|
|
190
220
|
)
|
|
191
221
|
|
|
192
222
|
@model_validator(mode="after")
|
|
193
|
-
def _validate(self) ->
|
|
223
|
+
def _validate(self) -> Self:
|
|
194
224
|
"""Translate core validation failures into the existing Pydantic error contract."""
|
|
195
225
|
unwrap_or_raise(_validate_head_grouping(self.heads, self.kv_heads))
|
|
196
226
|
_warn_attn_dims(self.dim, self.heads, self.dim_head)
|
|
@@ -203,3 +233,33 @@ class CrossAttentionConfig(BaseModel, frozen=True):
|
|
|
203
233
|
@property
|
|
204
234
|
def groups(self) -> int:
|
|
205
235
|
return self.heads // self.effective_kv_heads
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class CrossAttentionConfig(_CrossAttentionConfigBase):
|
|
239
|
+
"""Cross-attention with separate query, key, and value projections."""
|
|
240
|
+
|
|
241
|
+
kind: Literal["cross_attention"] = "cross_attention"
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class PackedCrossAttentionConfig(_CrossAttentionConfigBase):
|
|
245
|
+
"""Cross-attention with a separate query projection and one context KV projection."""
|
|
246
|
+
|
|
247
|
+
kind: Literal["packed_cross_attention"] = "packed_cross_attention"
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _legacy_cross_attention_kind(
|
|
251
|
+
value: CrossAttentionConfig | PackedCrossAttentionConfig | dict[str, JsonValue],
|
|
252
|
+
) -> CrossAttentionConfig | PackedCrossAttentionConfig | dict[str, JsonValue]:
|
|
253
|
+
"""Read legacy untagged cross-attention JSON without changing its checkpoint layout."""
|
|
254
|
+
match value:
|
|
255
|
+
case dict() if "kind" not in value:
|
|
256
|
+
return {**value, "kind": "cross_attention"}
|
|
257
|
+
case _:
|
|
258
|
+
return value
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
CrossAttentionVariantConfig = Annotated[
|
|
262
|
+
CrossAttentionConfig | PackedCrossAttentionConfig,
|
|
263
|
+
Field(discriminator="kind"),
|
|
264
|
+
BeforeValidator(_legacy_cross_attention_kind),
|
|
265
|
+
]
|