Skip to content

Add AST (Audio Spectrogram Transformer) Adapter - #1484

Merged
jlarson4 merged 5 commits into
TransformerLensOrg:devfrom
dylanberens:add-ast-adapter
Jul 30, 2026
Merged

Add AST (Audio Spectrogram Transformer) Adapter#1484
jlarson4 merged 5 commits into
TransformerLensOrg:devfrom
dylanberens:add-ast-adapter

Conversation

@dylanberens

@dylanberens dylanberens commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

Refactored the AST architecture adapter to align with the V3 'TransformerBridge' architecture and target 'dev'.

Key Changes

  • V3 Bridge Architecture: Implemented 'ASTArchitectureAdapter' using hierarchical 'component_mapping' ('BlockBridge', 'AttentionBridge', 'MLPBridge', 'NormalizationBridge', and 'UnembeddingBridge') & added ASTForAudioClassification to architecture adapter factory
  • Weight Processing: Ported head-splitting tensor reshapes ('RearrangeTensorConversion') for Query/Key/Value/Output projections to handle dimensional conversion during loading
  • Dynamic Context Length: Utilized 'prepare_model' hook to dynamically extract 'n_ctx' from positional embeddings length
  • Unit Test: Updated unit tests under 'tests/unit/model_bridge/supported_architectures/test_ast_adapter.py' asserting exact bridge-vs-HF parity

Fixes # 1484

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

@dylanberens dylanberens changed the title [WIP] Add AST Adapter Add AST (Audio Spectrogram Transformer) Adapter Jul 5, 2026
@dylanberens
dylanberens marked this pull request as ready for review July 5, 2026 01:49
@jlarson4

jlarson4 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks for putting this together @dylanberens! The AST domain work is great, the Q/K/V/O and bias reshapes, MLP transposes, the overlapping-patch Conv2d, the cls-distillation-patches token order, and the pooled dual-token + dual-LayerNorm head are all correct.

We will need to address & adjust the approach: this is written against the legacy HookedTransformer convert_weights path, but TransformerBridge adapters are driven by component_mapping (wrap HF's live modules). As submitted it can't load or run, which is why your tests are failing in CI.

Primary issues with the current code:

  1. Doesn't integrate with the bridge – ASTAdapter never sets self.component_mapping (it defines no __init__, so it inherits None), and it isn't registered in the factory. Booting AST raises ValueError: Unsupported architecture. get_config_map/convert_weights are legacy-HT classmethods no bridge path ever calls.
  2. The test fails and never touches the bridge – It KeyErrors at ast.py:125, the HF keys are wrong on two axes: layers.{i} should be encoder.layer.{i}, and q_proj/k_proj/v_proj/o_proj/mlp.fc1/fc2 should be attention.attention.{query,key,value} / attention.output.dense / intermediate.dense / output.dense. So the max diff < 1e-4 claim isn't reproducible, and the test builds a raw HookedTransformer, not a bridge.

To make this mergeable:

  • Rewrite as a component_mapping adapter: I'd recommend following our Architecture Adapter Creation Guide. The hubert.py adapter is our current working audio adapter, it would be a good reference point to work off.
  • Delete ASTEmbed and both classmethods — the bridge wraps HF's own modules, so none of that math needs reimplementing.
  • Fix n_ctx = num_patches+2; read patch/stride/hidden_act from config.
  • Register ASTForAudioClassification in the factory + export it from __init__.
  • Rewrite the test to boot a real TransformerBridge and assert bridge-vs-HF fp32 parity, under tests/unit/....

Also, a couple repo items: make sure to drop poetry.lock from the PR. Starting from 3.0.0, the repo is uv-based. And, base this change to dev, we will be able to launch this as part of a 3.x release, it does not need to be gated behind version 4.

Happy to re-review once it's on the TransformerBridge pattern.

@dylanberens
dylanberens changed the base branch from dev-4.x to dev July 25, 2026 18:25
@dylanberens dylanberens reopened this Jul 25, 2026
@dylanberens

Copy link
Copy Markdown
Contributor Author

thanks @jlarson4 for the feedback and roadmap! I've refactored the AST adapter to follow the V3 TransformerBridge pattern against dev

  • removed custom embedding reimplementations and adopted hierarchical component_mapping
  • ported q/k/v/o head splitting tensor conversions
  • utilized prepare_model to resolve n_ctx from positional embeddings
  • rebase to dev, added the ASTForAudioClassification to architecture adapter factory, added FP32 bridge parity unit tests (passing locally with exact 0.0 difference)

ready for re-review when you get a chance, thanks jonah!

@jlarson4

Copy link
Copy Markdown
Collaborator

Excellent, thank you @dylanberens! I will make sure to let you know when I get around to reviewing

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey @dylanberens sorry it took me so long to get around to reviewing this. Full detailed review broken down below. The overall structure and content of your adapter are very good! There are just a couple small items that need addressing before I can merge. Once the comments below are resolved, this should be good to go!

Comment thread transformer_lens/model_bridge/supported_architectures/ast.py
Comment thread transformer_lens/model_bridge/supported_architectures/ast.py Outdated
Comment thread transformer_lens/model_bridge/supported_architectures/ast.py Outdated
Comment thread tests/unit/model_bridge/supported_architectures/test_ast_adapter.py Outdated
Comment thread tests/unit/model_bridge/supported_architectures/test_ast_adapter.py Outdated
Comment thread tests/unit/model_bridge/supported_architectures/test_ast_adapter.py Outdated
Comment thread transformer_lens/tools/model_registry/__init__.py
@jlarson4

Copy link
Copy Markdown
Collaborator

Thanks @dylanberens for the update! The fixes look good, I verified them all locally. Three things left before merge:

  1. The real load path still can't reach your fixes – AUDIO_ARCHITECTURES routes AST to bare AutoModel, so a real boot_transformers("MIT/ast-...") loads a headless ASTModel without the classifier, and the audio_spectrogram_transformer. prefix branch never fires. Visual Encoders (ViT, DeiT) Support Rollout #1546 added an perfect example: mirror VISION_CLASSIFICATION_ARCHITECTURES -> AutoModelForAudioClassification in utilities/architectures.py and sources/transformers.py. Please also add a boot test, boot_transformers(<checkpoint>, load_weights=False) is cheap and fails today, so it'll pin the fix.

  2. "ASTModel" in AUDIO_ARCHITECTURES is currently inert – it isn't registered in the factory or registry, so bare-encoder checkpoints still fail at adapter selection while the docstring claims support. Either register it in the other sites or drop the entry and narrow the docstring.

  3. The parity test belongs in the integration folder (tests/integration/model_bridge/test_ast_tiny.py). A new adapter needs one there regardless.

…test, fix audio classification load path, and add to ARCHITECTURE_DESCRIPTIONS
@dylanberens

Copy link
Copy Markdown
Contributor Author

@jlarson4 most recent commit split parity to integration folder, add load_weights boot test, fix audio classification load path, and add to ARCHITECTURE_DESCRIPTIONS in generate_report.py just to be thorough

also for ASTModel i opted to drop the entry and narrow the docstring, thanks man

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @dylanberens! Great work on this, boot_transformers is now loading and returning logits as expected! A few new requests below. I appreciate your hard work on this, sorry for all the back and forth

Comment thread transformer_lens/utilities/architectures.py Outdated
Comment thread tests/unit/model_bridge/supported_architectures/test_ast_adapter.py Outdated
Comment thread transformer_lens/model_bridge/supported_architectures/ast.py Outdated
Comment thread transformer_lens/model_bridge/supported_architectures/ast.py Outdated
@jlarson4
jlarson4 merged commit b3ec1ab into TransformerLensOrg:dev Jul 30, 2026
25 checks passed
jlarson4 added a commit that referenced this pull request Aug 7, 2026
* Add Jacobian lens fitting guide (#1544)

* Quantify Jacobian Lens causal swap success (#1545)

Co-authored-by: Dreamer431 <113128214+Dreamer431@users.noreply.github.com>

* test(integration): add oracle parity test for JacobianLens (#1539 Tier-1) (#1543)

* test(integration): add oracle parity test for JacobianLens (#1539 Tier-1)

Compares TransformerBridge JacobianLens.readout() against the reference
anthropics/jacobian-lens oracle (pinned to 581d398) on google/gemma-2-2b-it
across 75 layer x prompt cells (5 prompts x 15 sampled layers).

Pass criteria per the #1539 spec (matching #1505 spike numbers):
  - Worst-case top-8 token overlap >= 7/8 in every cell
  - Spearman rank-correlation >= 0.95 on the top-64 logit union per cell

The oracle is installed at test time via pip from the pinned commit so
the threshold is reproducible independent of upstream drift. Reuses the
bridge's original_model + tokenizer to avoid a second model copy in RAM.

* style: apply black formatting (line-length=100)

* fix: remove unused Dict, Tuple typing imports (pycln)

* style: fix black formatting for py310 target (double blank lines)

* test: use pytest.importorskip for oracle dep; add oracle-parity CI workflow

Replace subprocess pip-install fixture with pytest.importorskip so the
test skips gracefully in standard uv venvs (no pip present) and does not
mutate the developer environment with no cleanup.

Add .github/workflows/oracle-parity.yml — a dedicated workflow that reads
ORACLE_COMMIT from the test file (single source of truth) and installs
the oracle out-of-band before running the @pytest.mark.slow suite.
Triggers on workflow_dispatch and on pushes that touch the test or
workflow file, keeping oracle runs opt-in for PR checks.

* Visual Encoders (ViT, DeiT) Support Rollout (#1546)

* Add ViTArchitectureAdapter to supported architectures

* Add ViTArchitectureAdapter to architecture factory

* Add ViT and DeiT models to model registry

* Add new model descriptions for Vision Transformers and Wav2Vec2

* Add ViTArchitectureAdapter for vision models

Implement ViT/DeiT architecture adapter for model bridging.

* Create vision_embedings.py

* Add VisionClassifierHeadBridge for CLS token classification

Implement VisionClassifierHeadBridge to handle CLS token slicing for classification.

* Add visual model configuration to transformer bridge

* Clarify pixel_values usage for multimodal and vision models

Updated documentation for pixel_values parameter to clarify its use with vision models.

* Update bridge.py

* Update bridge.py

* Rename vision_embedings.py to vision_embeddings.py

* Define vision model and classification architectures

Added vision model architectures and classification heads.

* Add support for vision architectures in transformers

* Refactor VisionClassifierHeadBridge to use pooled output

Updated the VisionClassifierHeadBridge to directly use an already-pooled CLS token instead of slicing from the sequence output. Adjusted the forward method to reflect this change and improved error handling for the original component.

* Update vit.py

* Add unit tests for ViTArchitectureAdapter

This file contains unit tests for the ViTArchitectureAdapter, covering component mapping, configuration flags, weight conversions, and model preparation methods.

* Create test_vit_adapter.py

* Update transformers.py

* Update vit.py

* Update vit.py

* Update vit.py

* Fix type hint for get_remote_component method

* Fix type hint for get_remote_component method

* Change import of torch to torch.nn in vit.py

* Update vit.py

* Re-add dummy 'mlp' attribute injection for ViTLayer

Reintroduce a patch_layers function to inject a dummy 'mlp' attribute into ViTLayer blocks for MLPBridge compatibility.

* Refactor ViTLayer handling by removing patch_layers

Removed the patch_layers function and its call, which injected a dummy 'mlp' attribute into ViTLayer blocks. Updated comments for clarity regarding the MLPBridge container.

* Add dummy 'mlp' attribute to ViTLayer blocks

Inject a dummy 'mlp' attribute into ViTLayer blocks to satisfy hasattr check for TransformerLens.

* Update vit.py

* Remove TestViTConfigNCtx and related test case

Removed deprecated TestViTConfigNCtx class and its test case for n_ctx.

* Enhance ViTLayer with MLP wrapper and fix forward method

Added a non-circular MLP wrapper to ViTLayer blocks and fixed tuple-chaining bug in forward method.

* Refactor ViT layer forward pass handling

Refactor forward pass handling for ViT layers to safely unpack tuple outputs and ensure compatibility with the model's internal structure.

* Refactor ViTLayer forward pass handling

Refactor forward pass handling for ViTLayer to fix tuple-chaining bug and ensure compatibility with HF model outputs.

* Fix tuple handling in ViTLayer forward method

Modified the forward method to handle tuple inputs and outputs for ViTLayer, ensuring compatibility with Tensor expectations.

* Reorder model prefix checks for better clarity

* Update vit.py

* Detect model class name in prepare_model method

Added detection for model class name in prepare_model method.

* Simplify prefix determination for ViT models

Refactor model prefix detection logic for ViT and DeiT models.

* Implement fixture for distilled DeiT model testing

Added a fixture to load the distilled DeiT model for testing.

* Update DeiT bridge tests for bare model handling

Refactor tests for DeiT bridge to accommodate bare model behavior and update assertions accordingly.

* Set architecture in Hugging Face model configuration

* Support DeiTLayer in patch_layers function

* sort

* Replace direct attribute assignment with setattr

* black fix

* fix formatting after merge

* Update vit.py

* Update ViT adapter test paths for consistency

* Remove redundant test for n_ctx in prepare_loading

Removed test for prepare_loading not affecting n_ctx.

* black sorted

* black reorder

* Refactor vit_bridge and vit_bare_bridge fixtures

* temp support up to transformers 5.8.0

* support transformers 5.13.0

* format fixed. Unit test all passed. Intergration test all passed. should be good to go

* Update vit.py

* Clarify tokenizer support in ViTArchitectureAdapter

Added comment to clarify the lack of tokenizer support for vision models.

* Remove head_dim assignment from hf_config

Removed unused head_dim assignment from hf_config.

* Update vit.py

* Update bridge.py

* Add VisionEmbeddingsBridge and VisionClassifierHeadBridge

* Update test_vit_adapter.py

* Update test_vit_adapter.py

* Update test_vit_adapter.py

* Update test_vit_adapter.py

* Improve compatibility mode error and output handling

Updated error message for clarity and added handling for last_hidden_state in output.

* Refactor test to check output type and shape

Update test to verify that the forward method returns a tensor instead of a raw HF output object. Adjust assertions to match the expected behavior after changes in bridge.py.

* Update bridge.py

* Update test_vit_adapter.py

* Remove obsolete tests from TestViTPrepareLoading

Removed deprecated tests for prepare_loading() in TestViTPrepareLoading.

* Update test_vit_adapter.py

* Update test_vit_adapter.py

* formatted

---------

Co-authored-by: Jonah Larson <jonahalarson@comcast.net>

* Add Starcoder2 architecture adapter (#1533)

Co-authored-by: jlarson4 <jonahalarson@comcast.net>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Docs: Jlens Qwen3.5-4b Demo (#1547)

* Run experiments with Qwen-3.5 architecture support

* Run notebook cells

* Add lfm2 tiny integration test (#1552)

* Add lfm2 tiny integration test

* Fix formatting

* Add AST (Audio Spectrogram Transformer) Adapter (#1484)

* chore: save WIP on V3 transformerbridge migration

re add ast import and add to factory
:wq
y

wq
:wq

* feat(ast): migrate AST adapter to V3 TransformerBridge and component_mapping

* refactor(ast): resolve PR feedback for docstrings, prefix-awareness, unit tests

* test(ast): split parity to integration folder, add load_weights boot test, fix audio classification load path, and add to ARCHITECTURE_DESCRIPTIONS

* fix(ast): union audio classification sets, specific boot test assertions and two comment typo fixes

* Verificaiton for lapa (#1556)

* ViT and AST model verification (#1582)

* verified a few models for ViT and AST

* improved vision testing for ViT models

* fix(bridge): return W_in/W_out/W_gate in TL orientation for nn.Linear-backed models (#1558)

* feat: respect prepend_bos and add return_input_tokens flag

* fix torch orientation bug

* review changes

* review changes

* pipeline fix

* deprecate remaining hooked entry points (#1592)

* deprecate remaining hooked entry points

* test: account for hooked transformer warning in notebook

* fix: address deprecation warning review feedback

* fix: correct encoder deprecation warning stacklevel

* support loading fit checkpoints in JacobianLens.load() (#1574)

* support loading fit checkpoints in JacobianLens.load()

* fix: black formatting and update conflicting test for checkpoint load

* address jlarson4 review: preserve target_layer, add reference fixture, fix tuned-lens note

- Remove "target_layer" from _FIT_RESERVED_KEYS so it survives checkpoint
  conversion and validate_model() can refuse non-final-target lenses
- Add test_load_checkpoint_mirrors_fit_payload_schema: fixture matches the
  exact keys fit() produces so format drift causes a test failure
- Fix tuned-lens note: it is the Jacobian artifact format that has no bias
  slot, not the tuned-lens format; tuned-lens translators are affine (weight + bias)

* address jlarson4 review: read n_done key in _from_checkpoint_payload

Real checkpoint writers (reference package) store the prompt count as
n_done, not n_prompts. Prefer n_done with n_prompts as fallback so
genuine checkpoints are not rejected with n_prompts=0.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NiwNUm3YFj9yAuSBuGDnd8

* address jlarson4 review: align checkpoint loader with reference write_checkpoint() schema

- _from_checkpoint_payload: read n_done first (real checkpoints use n_done, not n_prompts)
- _from_checkpoint_payload: infer d_model from jacobian_sum matrix shape (real checkpoints have no d_model key)
- _from_checkpoint_payload: harvest top-level target_layer into metadata (reference format stores it at top level, not nested)
- _from_checkpoint_payload: guard empty jacobian_sum with a clear ValueError before attempting shape derivation
- load() docstring: update Fit checkpoint schema to reflect the real 6-key reference format
- tests: replace test_load_checkpoint_with_zero_n_prompts_raises with two tests
- tests: rewrite test_load_checkpoint_mirrors_fit_payload_schema to use verbatim 6-key reference payload
- tests: add test_load_checkpoint_harvests_flat_provenance_and_strips_fit_keys
- docs: update schema table — replace n_prompts/d_model with real 6-key format
- docs: note d_model inferred from matrix shape
- docs: document target_layer as deliberate exception to fit-key stripping

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Drop optional unused aliases on hybrid architectures (#1579)

* Drop optional unused aliases

* feat: assign fallbacks on pruned

* Improve unittests

* Add kurtosis-profile validation test for JacobianLens (#1539 Tier-1) (#1616)

* Add kurtosis-profile validation test for JacobianLens (#1539 Tier-1)

Asserts the workspace-band signature as relative structure rather than
absolute levels, per the cross-family measurement in #1539: band rise vs
the model's own early-third baseline, lens-specificity vs the logit-lens
control through the identical code path, a gpt2-small negative control,
and final-layer identity-transport agreement between arms.

* Fix gpt oss olmo3 parity (#1621)

* Resolution for issue 1619

* Updated for 1620

* Add verification script

* Fixed 1619 on HF

* Verification script repair

* fixing per-layer olmo

* cleanup

---------

Co-authored-by: abhi <abhinavbellapu@berkeley.edu>
Co-authored-by: emerardd <113128214+emerardd@users.noreply.github.com>
Co-authored-by: Dreamer431 <113128214+Dreamer431@users.noreply.github.com>
Co-authored-by: Mukund Pandey <mukund.pandey@gmail.com>
Co-authored-by: Jiankun Wei <72998341+david-wei-01001@users.noreply.github.com>
Co-authored-by: SanjidMzi <56235075+SanjidMzi@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Kyle Yin Xu <123780557+KYinXu@users.noreply.github.com>
Co-authored-by: Syed Adil Ahmed <tensorcruncher@gmail.com>
Co-authored-by: Dylan <159935143+dylanberens@users.noreply.github.com>
Co-authored-by: Md.Sadiq <mohammadsadiq4950@gmail.com>
Co-authored-by: msaule <sau24006@byui.edu>
Co-authored-by: Priyanka Bajaj <42418272+priyanka25aug@users.noreply.github.com>
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.

[Proposal] Add Audio Spectrogram Transformer (AST) Adapter (ASTForAudioClassification)

2 participants