Skip to content

[Proposal] Add a bridge-vs-HF numerical parity test for BERT #1665

Description

@jlarson4

Proposal

Base on dev-4.x. Add tests/integration/model_bridge/test_bridge_vs_hf_bert_parity.py: a bridge-vs-HF numerical parity test for a BERT checkpoint (e.g. bert-base-cased).

Motivation

The bridge's BERT path has no numerical certification anywhere in CI. The only BERT coverage that exists today is either structural or HookedEncoder-side:

  • tests/unit/model_bridge/supported_architectures/test_bert_adapter.py asserts component-mapping shape, weight-conversion patterns, and config flags. It never runs a forward pass.
  • tests/acceptance/test_hooked_encoder.py and tests/integration/test_create_hooked_encoder.py certify HookedEncoder

Pitch

  • Boot via TransformerBridge.boot_transformers("bert-base-cased", device="cpu", dtype=torch.float32) and run a forward. Boot auto-detects BertForMaskedLM, so no model_class is needed for the MLM case.
  • Assert bridge MLM logits match a raw HF BertForMaskedLM forward via torch.testing.assert_close. A local fp32 check on bert-base-cased currently gives a max abs difference of exactly 0.0.
  • Don't call enable_compatibility_mode() in the parity test – Compatibility mode applies HookedTransformer's default weight processing (including unembedding centering), which shifts logits away from raw HF.
  • Assert run_with_cache activations using bridge cache such as embed.hook_out, blocks.0.hook_resid_pre, blocks.0.attn.hook_q, ln_final.hook_normalized, unembed.hook_out.
  • Model on the pythia-70m template; do not straight-copy the HT suite

Acceptance:

  • MLM logit parity vs raw HF eager within fp32 tolerance
  • ≥1 run_with_cache assertion using correct bridge hook names
  • Test lives in tests/integration/model_bridge/, not skipped
  • make unit-test passes
  • uv run mypy . passes

Checklist

  • I have checked that there is no similar issue in the repo (required)

Metadata

Metadata

Assignees

Labels

TransformerBridgeBug specific to the new TransformerBridge systemcomplexity-moderateModerately complicated issues for people who have intermediate experience with the codehelp wantedExtra attention is neededtestingA task that needs to be completed in order to improve the current test coverage.v4.x

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions