Model Bridge
SafeLens' model bridge follows the same abstraction pattern that makes
TransformerLens scale across many model families: an architecture adapter maps
provider-specific module paths onto canonical components such as resid_pre,
attn_out, mlp_out, q, k, v, z, and result.
For Transformers-backed models, result is exposed as a TransformerLens-style
derived activation. SafeLens captures the input to the attention output
projection as z and computes per-head residual-space result vectors with
z @ W_O. When patching result, SafeLens calls the user hook on those
per-head vectors, sums the per-head residual-space delta, and writes that delta
back to the merged attention projection output.
The bridge is independent of TransformerLens. It is used by Transformers-backed wrappers after the model is loaded.
Current architecture adapters:
llama_like_decoder: Qwen, Qwen2, Qwen3, LLaMA, Mistral, Mixtral, Gemma, OLMo, StableLM, and Yi-stylemodel.layersdecoders.gpt2_decoder: GPT-2 and DistilGPT2-styletransformer.hdecoders.gpt_neox_decoder: GPT-NeoX and Pythia-stylegpt_neox.layersdecoders.gptj_decoder: GPT-J-style decoder blocks.gpt_neo_decoder: GPT-Neo-style decoder blocks.joint_qkv_decoder: BLOOM and Falcon-style joint QKV decoders.mpt_decoder: MPT-style decoder blocks.phi_decoder: Phi-style decoder blocks.opt_decoder: OPT-style decoder layers.bert_encoder: BERT/RoBERTa-style encoder layers.distilbert_encoder: DistilBERT encoder layers.audio_encoder: Wav2Vec2/Hubert encoder layers.t5_encoder_decoder: initial T5 encoder-stack component mapping.
Attention pattern caching uses returned attention weights when available.
Attention pattern patching and raw pre-softmax score hooks use eager softmax
instrumentation. If a model runs flash attention or SDPA without a Python
torch.softmax call, SafeLens raises a clear error and the model should be run
with an eager attention implementation for those hooks.
List the registered architecture adapters:
safelens models list-architectures
safelens models list-architectures --json
Architecture bridge primitives for Transformers-backed model adapters.
The design mirrors the useful part of TransformerLens' model bridge: keep model loading provider-specific, but map each model family into a small canonical component vocabulary that SafeLens hooks and patching code can target.
ArchitectureAdapter
Map one architecture family onto SafeLens' canonical component names.
Source code in src/SafeLens/utils/model_bridge.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
get_attention_bias(model, component, layer)
Return a TransformerLens-shaped attention bias for one layer.
Source code in src/SafeLens/utils/model_bridge.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
get_attention_weight(model, component, layer)
Return a TransformerLens-shaped attention weight tensor for one layer.
Source code in src/SafeLens/utils/model_bridge.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
get_embedding_weight(model, *, positional=False)
Return token or positional embedding weights for common Transformers layouts.
Source code in src/SafeLens/utils/model_bridge.py
478 479 480 481 482 483 484 485 486 487 488 489 490 | |
get_mlp_bias(model, component, layer)
Return a TransformerLens-shaped MLP bias vector for one layer.
Source code in src/SafeLens/utils/model_bridge.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
get_mlp_weight(model, component, layer)
Return a TransformerLens-shaped MLP weight matrix for one layer.
Source code in src/SafeLens/utils/model_bridge.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
ComponentHookContext
Small TransformerLens-style hook object passed to component hooks.
Source code in src/SafeLens/utils/model_bridge.py
157 158 159 160 161 162 163 164 165 166 167 168 | |
ComponentHookSpec
dataclass
How one canonical component maps to a HuggingFace module path.
Source code in src/SafeLens/utils/model_bridge.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
ComponentRef
dataclass
One parsed reference to a canonical transformer component.
Source code in src/SafeLens/utils/model_bridge.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
apply_attention_result_patch(output, original_result, patched_result)
Apply a patched per-head result tensor to the merged attention output.
Source code in src/SafeLens/utils/model_bridge.py
1391 1392 1393 1394 1395 | |
apply_norm_affine(module, normalized, reference_output=None)
Apply PyTorch/HF norm affine weights to a normalized activation.
Source code in src/SafeLens/utils/model_bridge.py
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 | |
architecture_adapter_for_model(model, *, model_name='')
Select a SafeLens architecture adapter for a loaded Transformers model.
Source code in src/SafeLens/utils/model_bridge.py
846 847 848 849 850 | |
architecture_adapter_for_name(*, model_name, model_type=None)
Select an architecture adapter from a model name and optional HF model_type.
Source code in src/SafeLens/utils/model_bridge.py
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |
attention_head_count(model)
Read the configured query/output attention head count.
Source code in src/SafeLens/utils/model_bridge.py
2302 2303 2304 2305 2306 2307 2308 2309 | |
attention_head_dim(model)
Read the per-query-head dimension used by q/k/v projections.
Source code in src/SafeLens/utils/model_bridge.py
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 | |
call_component_hook(hook_fn, *, activation, component_ref, architecture, hook_context=None)
Call a user hook with SafeLens component metadata when accepted.
Source code in src/SafeLens/utils/model_bridge.py
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 | |
compute_attention_result_activation(activation, model, spec, *, module, component_ref, architecture)
Compute TransformerLens-style per-head result from merged z input.
Source code in src/SafeLens/utils/model_bridge.py
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 | |
extract_component_activation(output, spec, model)
Extract the activation value for a component from a module hook output.
Source code in src/SafeLens/utils/model_bridge.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 | |
first_attention_head(value)
Select the first head from a TransformerLens input tensor.
Source code in src/SafeLens/utils/model_bridge.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 | |
first_output(output)
Return the tensor payload from common Transformers module outputs.
Source code in src/SafeLens/utils/model_bridge.py
1266 1267 1268 1269 1270 1271 1272 | |
head_count_for_component(model, component)
Read the configured attention head count for one component.
Source code in src/SafeLens/utils/model_bridge.py
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 | |
is_qwen_routed_moe_model_name(model_name)
Return whether a Qwen model name clearly denotes a routed MoE checkpoint.
Source code in src/SafeLens/utils/model_bridge.py
871 872 873 874 875 876 | |
key_value_head_count(model)
Read the configured key/value head count when it differs from query heads.
Source code in src/SafeLens/utils/model_bridge.py
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 | |
list_architecture_adapters()
Return public metadata for SafeLens' architecture bridge adapters.
Source code in src/SafeLens/utils/model_bridge.py
879 880 881 | |
merge_component_activation(activation, reference, spec, model)
Merge a patched SafeLens component activation back into the raw HF tensor shape.
Source code in src/SafeLens/utils/model_bridge.py
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 | |
merge_heads(activation, reference)
Flatten [batch, pos, head, head_dim] back to the reference final dimension.
Source code in src/SafeLens/utils/model_bridge.py
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 | |
merge_qkv_heads(activation, reference, model, spec)
Replace one split-head component in a raw joint QKV projection tensor.
Source code in src/SafeLens/utils/model_bridge.py
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 | |
module_uses_centered_layer_norm(module)
Return whether a norm module mean-centers like LayerNorm.
Source code in src/SafeLens/utils/model_bridge.py
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 | |
norm_module_output_from_scale(module, source, scale, reference_output)
Recompute a norm module output from a patched TL-style scale.
Source code in src/SafeLens/utils/model_bridge.py
1590 1591 1592 1593 1594 1595 | |
norm_scale_from_input(module, inputs=(), output=None)
Return TransformerLens-style norm scale [batch, pos, 1] for a norm module input.
Source code in src/SafeLens/utils/model_bridge.py
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 | |
normalized_output_from_scale(module, source, scale=None)
Return normalized norm input before affine weights, matching TL hook_normalized.
Source code in src/SafeLens/utils/model_bridge.py
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 | |
preferred_attention_weight_packed_axis(module, *, architecture, component)
Return the likely packed axis for architecture-specific attention weights.
Source code in src/SafeLens/utils/model_bridge.py
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 | |
preferred_qkv_weight_packed_axis(module, *, architecture)
Return the likely packed axis for architecture-specific joint QKV weights.
Source code in src/SafeLens/utils/model_bridge.py
2652 2653 2654 2655 2656 2657 2658 2659 | |
replace_component_activation(output, patched, spec, model)
Replace the tensor payload while preserving tuple/list module output shape.
Source code in src/SafeLens/utils/model_bridge.py
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 | |
reshape_attention_bias(bias, *, component, n_heads)
Convert a packed projection bias to TransformerLens attention bias shape.
Source code in src/SafeLens/utils/model_bridge.py
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 | |
reshape_attention_weight(weight, *, component, n_heads, packed_axis=0)
Convert HF linear weights to TransformerLens attention weight shapes.
Source code in src/SafeLens/utils/model_bridge.py
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 | |
reshape_joint_qkv_attention_bias(bias, *, component, q_heads, kv_heads, qkv_layout)
Convert a joint QKV projection bias to TransformerLens attention bias shape.
Source code in src/SafeLens/utils/model_bridge.py
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 | |
reshape_joint_qkv_attention_weight(weight, *, component, q_heads, kv_heads, qkv_layout, packed_axis=None)
Convert joint QKV projection weights to TransformerLens attention shapes.
Source code in src/SafeLens/utils/model_bridge.py
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 | |
reshape_joint_qkv_weight_packed_columns(weight, *, component, q_heads, kv_heads, qkv_layout)
Handle Conv1D-style joint weights shaped [d_model, qkv_out].
Source code in src/SafeLens/utils/model_bridge.py
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 | |
reshape_joint_qkv_weight_packed_rows(weight, *, component, q_heads, kv_heads, qkv_layout)
Handle linear-style joint weights shaped [qkv_out, d_model].
Source code in src/SafeLens/utils/model_bridge.py
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 | |
resolve_module_path(model, path)
Resolve a dotted module path with integer list indexes.
Source code in src/SafeLens/utils/model_bridge.py
601 602 603 604 605 606 607 608 609 | |
split_heads(activation, n_heads)
Reshape [batch, pos, hidden] activations into [batch, pos, head, head_dim].
Source code in src/SafeLens/utils/model_bridge.py
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 | |
split_qkv_heads(activation, model, spec)
Extract one component from a joint QKV projection and split it into heads.
Source code in src/SafeLens/utils/model_bridge.py
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 | |
sum_attention_heads(value)
Sum the head axis in a TransformerLens result tensor.
Source code in src/SafeLens/utils/model_bridge.py
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 | |
supported_transformer_component_names(*, include_pattern=False, include_attention=False, include_result=True)
Return the canonical component vocabulary exposed by model bridges.
Source code in src/SafeLens/utils/model_bridge.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | |
transform_component_activation(activation, spec, model, *, module=None, component_ref=None, architecture=None)
Convert raw HF projection tensors into SafeLens component activation shape.
Source code in src/SafeLens/utils/model_bridge.py
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 | |
transformer_lens_component_name(component, layer)
Return a TransformerLens-style hook name for a canonical component.
Source code in src/SafeLens/utils/model_bridge.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
transpose_2d_weight(weight)
Return a rank-2 weight transposed without requiring tensor dependencies.
Source code in src/SafeLens/utils/model_bridge.py
704 705 706 707 708 709 710 711 712 713 | |
zeros_for_attention_bias(model, component)
Return a zero attention bias with the TransformerLens shape for one component.
Source code in src/SafeLens/utils/model_bridge.py
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 | |