layer-wise-integrated-gradients 0.1.0__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.
Files changed (101) hide show
  1. layer_wise_integrated_gradients-0.1.0/LICENSE +21 -0
  2. layer_wise_integrated_gradients-0.1.0/PKG-INFO +240 -0
  3. layer_wise_integrated_gradients-0.1.0/README.md +196 -0
  4. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/PKG-INFO +240 -0
  5. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/SOURCES.txt +99 -0
  6. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/dependency_links.txt +1 -0
  7. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/entry_points.txt +2 -0
  8. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/requires.txt +21 -0
  9. layer_wise_integrated_gradients-0.1.0/layer_wise_integrated_gradients.egg-info/top_level.txt +2 -0
  10. layer_wise_integrated_gradients-0.1.0/lig/__init__.py +38 -0
  11. layer_wise_integrated_gradients-0.1.0/lig/adapter.py +21 -0
  12. layer_wise_integrated_gradients-0.1.0/lig/adapters/__init__.py +20 -0
  13. layer_wise_integrated_gradients-0.1.0/lig/adapters/decoder.py +130 -0
  14. layer_wise_integrated_gradients-0.1.0/lig/adapters/encoder.py +226 -0
  15. layer_wise_integrated_gradients-0.1.0/lig/adapters/factory.py +46 -0
  16. layer_wise_integrated_gradients-0.1.0/lig/api.py +814 -0
  17. layer_wise_integrated_gradients-0.1.0/lig/boundaries.py +371 -0
  18. layer_wise_integrated_gradients-0.1.0/lig/cli.py +82 -0
  19. layer_wise_integrated_gradients-0.1.0/lig/config.py +58 -0
  20. layer_wise_integrated_gradients-0.1.0/lig/demo/__init__.py +0 -0
  21. layer_wise_integrated_gradients-0.1.0/lig/demo/streamlit_app.py +45 -0
  22. layer_wise_integrated_gradients-0.1.0/lig/encoder_access.py +126 -0
  23. layer_wise_integrated_gradients-0.1.0/lig/publication.py +109 -0
  24. layer_wise_integrated_gradients-0.1.0/lig/viz/__init__.py +5 -0
  25. layer_wise_integrated_gradients-0.1.0/lig/viz/z2z_contribution.py +4099 -0
  26. layer_wise_integrated_gradients-0.1.0/pyproject.toml +81 -0
  27. layer_wise_integrated_gradients-0.1.0/setup.cfg +4 -0
  28. layer_wise_integrated_gradients-0.1.0/test/test_boundaries.py +74 -0
  29. layer_wise_integrated_gradients-0.1.0/test/test_encoder_models.py +87 -0
  30. layer_wise_integrated_gradients-0.1.0/test/test_itb_self_contrib.py +25 -0
  31. layer_wise_integrated_gradients-0.1.0/test/test_layer_contribution_theory.py +173 -0
  32. layer_wise_integrated_gradients-0.1.0/test/test_layer_direct_ig.py +177 -0
  33. layer_wise_integrated_gradients-0.1.0/test/test_layer_itb_zero_ratio.py +30 -0
  34. layer_wise_integrated_gradients-0.1.0/test/test_lig_api.py +226 -0
  35. layer_wise_integrated_gradients-0.1.0/test/test_publication.py +37 -0
  36. layer_wise_integrated_gradients-0.1.0/utils/__init__.py +135 -0
  37. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/__init__.py +0 -0
  38. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/adaptive_batch_size.py +211 -0
  39. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/att_itb/__init__.py +16 -0
  40. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/att_itb/att_itb.py +120 -0
  41. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/__init__.py +0 -0
  42. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/attention_ig.py +2317 -0
  43. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/attention_map_alpha.py +45 -0
  44. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/attention_models.py +371 -0
  45. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/core/__init__.py +19 -0
  46. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/core/baseline_computation.py +93 -0
  47. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/core/embedding_extraction.py +128 -0
  48. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/core/value_extraction.py +117 -0
  49. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/direct_computation.py +392 -0
  50. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/direct_computation_batch.py +488 -0
  51. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/gpt2_attention_models.py +390 -0
  52. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/methods/__init__.py +6 -0
  53. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/methods/v_to_u/__init__.py +6 -0
  54. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/methods/z_to_u/__init__.py +6 -0
  55. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/multi_layer_attention_ig.py +226 -0
  56. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/attention/scratch_ig.py +322 -0
  57. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/global_analysis.py +1558 -0
  58. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/gpt2/__init__.py +1 -0
  59. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/gpt2/block_forward.py +66 -0
  60. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/gpu_memory_monitor.py +241 -0
  61. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/__init__.py +0 -0
  62. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/att_itb_mlp_baseline.py +61 -0
  63. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/gpt2_mlp_lig_ig.py +138 -0
  64. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/mlp_ig.py +1134 -0
  65. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/mlp_lig_ig.py +136 -0
  66. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/mlp/mlp_models.py +384 -0
  67. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_ig.py +2340 -0
  68. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_runtime/__init__.py +22 -0
  69. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_runtime/device_pool.py +293 -0
  70. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_runtime/executors.py +1481 -0
  71. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_runtime/registry.py +35 -0
  72. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/optimized_runtime/tasks.py +43 -0
  73. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/parallel_ig_calculator.py +360 -0
  74. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/resource_planner.py +118 -0
  75. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/shared/__init__.py +1 -0
  76. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/shared/encoder_access.py +108 -0
  77. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/shared/itb_self_contrib.py +58 -0
  78. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/shared/release_scope.py +13 -0
  79. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/z2z/compose_att_mlp.py +62 -0
  80. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/z2z/global_z2z_analysis.py +193 -0
  81. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/z2z/gpt2_layer_direct_ig.py +113 -0
  82. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/z2z/layer_direct_ig.py +255 -0
  83. layer_wise_integrated_gradients-0.1.0/utils/calculations/ig/z2z/layer_itb_zero_ratio.py +80 -0
  84. layer_wise_integrated_gradients-0.1.0/utils/calculations/shared/__init__.py +0 -0
  85. layer_wise_integrated_gradients-0.1.0/utils/calculations/shared/device_utils.py +35 -0
  86. layer_wise_integrated_gradients-0.1.0/utils/calculations/shared/ig_calculations.py +1402 -0
  87. layer_wise_integrated_gradients-0.1.0/utils/common/__init__.py +23 -0
  88. layer_wise_integrated_gradients-0.1.0/utils/common/bert_hooks.py +352 -0
  89. layer_wise_integrated_gradients-0.1.0/utils/common/logging_setup.py +315 -0
  90. layer_wise_integrated_gradients-0.1.0/utils/common/model_loader.py +72 -0
  91. layer_wise_integrated_gradients-0.1.0/utils/common/system/__init__.py +38 -0
  92. layer_wise_integrated_gradients-0.1.0/utils/common/system/background_execution.py +171 -0
  93. layer_wise_integrated_gradients-0.1.0/utils/common/system/dynamic_worker_manager.py +421 -0
  94. layer_wise_integrated_gradients-0.1.0/utils/common/system/multi_gpu_manager.py +436 -0
  95. layer_wise_integrated_gradients-0.1.0/utils/common/system/system_info.py +321 -0
  96. layer_wise_integrated_gradients-0.1.0/utils/common/unified_bert_model.py +29 -0
  97. layer_wise_integrated_gradients-0.1.0/utils/common/visualization/__init__.py +16 -0
  98. layer_wise_integrated_gradients-0.1.0/utils/common/visualization/visualization.py +522 -0
  99. layer_wise_integrated_gradients-0.1.0/utils/reproduce/__init__.py +0 -0
  100. layer_wise_integrated_gradients-0.1.0/utils/reproduce/ptb_loader.py +86 -0
  101. layer_wise_integrated_gradients-0.1.0/utils/scripts/__init__.py +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eight Suzuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,240 @@
1
+ Metadata-Version: 2.4
2
+ Name: layer-wise-integrated-gradients
3
+ Version: 0.1.0
4
+ Summary: Layer-wise Integrated Gradients (LIG) for within-layer flow analysis in Transformers
5
+ Author: Eight Suzuki, Hideitsu Hino, Noboru Murata
6
+ License: MIT
7
+ Project-URL: Homepage, https://eightsuzuki.github.io/layer-wise-integrated-gradients/
8
+ Project-URL: Documentation, https://eightsuzuki.github.io/layer-wise-integrated-gradients/
9
+ Project-URL: Repository, https://github.com/eightsuzuki/layer-wise-integrated-gradients
10
+ Project-URL: Issues, https://github.com/eightsuzuki/layer-wise-integrated-gradients/issues
11
+ Project-URL: PyPI, https://pypi.org/project/layer-wise-integrated-gradients/
12
+ Keywords: transformers,explainability,integrated-gradients,lig,attention,bert
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Requires-Python: >=3.10
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy>=2.0.0
26
+ Requires-Dist: captum
27
+ Requires-Dist: transformers<5.0.0,>=4.21.0
28
+ Requires-Dist: tokenizers>=0.13.0
29
+ Requires-Dist: lightning
30
+ Requires-Dist: pandas
31
+ Requires-Dist: joblib
32
+ Requires-Dist: pyyaml>=6.0
33
+ Requires-Dist: scipy>=1.10.0
34
+ Provides-Extra: demo
35
+ Requires-Dist: streamlit>=1.28.0; extra == "demo"
36
+ Provides-Extra: reproduce
37
+ Requires-Dist: openpyxl>=3.1.0; extra == "reproduce"
38
+ Provides-Extra: dev
39
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
40
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
41
+ Requires-Dist: build>=1.0.0; extra == "dev"
42
+ Requires-Dist: twine>=5.0.0; extra == "dev"
43
+ Dynamic: license-file
44
+
45
+ # Layer-wise Integrated Gradients (LIG)
46
+
47
+ <p align="center">
48
+ <img src="logo/LIG-LOGO.png" alt="LIG logo" width="660">
49
+ </p>
50
+
51
+ **Set-to-set Integrated Gradients for within-layer flow analysis in Transformers.**
52
+
53
+ [![Website](https://img.shields.io/badge/website-GitHub%20Pages-0d9488)](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
54
+ [![PyPI version](https://img.shields.io/pypi/v/layer-wise-integrated-gradients)](https://pypi.org/project/layer-wise-integrated-gradients/)
55
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
56
+ [![GitHub](https://img.shields.io/badge/code-GitHub-181717)](https://github.com/eightsuzuki/layer-wise-integrated-gradients)
57
+
58
+ LIG applies Integrated Gradients (IG) at ATT and MLP module boundaries inside each Transformer layer, composes within-layer token-to-token contributions, and compares them with layer-whole attribution under an $L_2$ diagnostic — without model-specific retraining or per-operation interpreter design.
59
+
60
+ - **Website &amp; interactive demo:** [eightsuzuki.github.io/layer-wise-integrated-gradients](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
61
+ - **Paper &amp; arXiv:** Coming soon
62
+
63
+ This release implements **L2-scalarized set-to-set IG** at module boundaries (z→u, u→z, z→z), with baselines **ITB** (`self_input_token`), **ITB-zeroRatio** (`itb_zero_ratio`), **ITB-mapRatio** (`itb_map_ratio`), **Zero**, and **ATTITBa=0** (`att_itb_a0`) for MLP.
64
+
65
+ ---
66
+
67
+ ## Quick start
68
+
69
+ Install **PyTorch first** (CUDA/CPU wheel index differs), then LIG:
70
+
71
+ ```bash
72
+ # GPU (CUDA 12.1 example)
73
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
74
+
75
+ # From PyPI
76
+ pip install layer-wise-integrated-gradients
77
+
78
+ # Or from GitHub
79
+ pip install "layer-wise-integrated-gradients @ git+https://github.com/eightsuzuki/layer-wise-integrated-gradients.git"
80
+
81
+ # Development clone
82
+ git clone https://github.com/eightsuzuki/layer-wise-integrated-gradients.git
83
+ cd layer-wise-integrated-gradients
84
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
85
+ pip install -e ".[dev]"
86
+ ```
87
+
88
+ ### Explain one sentence → JSON
89
+
90
+ ```python
91
+ from lig import explain
92
+
93
+ result = explain(
94
+ "The cat sat on the mat.",
95
+ model="bert-base-uncased",
96
+ num_steps=32,
97
+ granularity="all", # att (z→u), mlp (u→z), layer (z→z)
98
+ layers=[0, 11],
99
+ )
100
+ ```
101
+
102
+ ```bash
103
+ lig explain "The cat sat on the mat." --steps 32 --granularity all -o attributions.json
104
+ ```
105
+
106
+ ### Supported models
107
+
108
+ | Family | Models | `granularity` |
109
+ |--------|--------|---------------|
110
+ | BERT-style | BERT, RoBERTa, DeBERTa, ELECTRA, XLM-RoBERTa, … | z→u, u→z, z→z |
111
+ | Block-only | MPNet, DistilBERT, ModernBERT, Switch MoE encoder, Mamba | **z→z (layer) only** |
112
+ | GPT-2 (decoder) | z→u, u→z, z→z (`granularity="all"`) | — |
113
+
114
+ GPT-2 example:
115
+
116
+ ```python
117
+ explain(
118
+ "Hello world",
119
+ model="gpt2",
120
+ granularity="all",
121
+ baseline_att="self_input_token", # or itb_zero_ratio / itb_map_ratio
122
+ baseline_mlp="att_itb_a0", # or zero
123
+ layers=[0],
124
+ )
125
+ ```
126
+
127
+ ATT ITB self-contribution variants (see method section in the upcoming paper):
128
+
129
+ ```python
130
+ explain("Hello world", baseline_att="itb_zero_ratio", granularity="att", layers=[0])
131
+ explain("Hello world", baseline_att="itb_map_ratio", granularity="att", layers=[0])
132
+ explain("Hello world", baseline_layer="itb_zero_ratio", granularity="layer", layers=[0])
133
+ ```
134
+
135
+ Other decoders (Llama, …): [docs/DECODER_DESIGN.md](docs/DECODER_DESIGN.md).
136
+
137
+ ### Inspect boundaries (no IG run)
138
+
139
+ ```python
140
+ from lig import describe_boundaries
141
+
142
+ # Config-only (no weight download)
143
+ describe_boundaries("gpt2", load_weights=False)
144
+
145
+ # Full module introspection
146
+ describe_boundaries("bert-base-uncased", load_weights=True, device="cpu")
147
+ ```
148
+
149
+ ```bash
150
+ python examples/detect_model_boundaries.py bert-base-uncased gpt2 distilbert-base-uncased
151
+ ```
152
+
153
+ ### Docker
154
+
155
+ ```bash
156
+ docker compose -f docker-compose.cpu.yml build
157
+ docker compose -f docker-compose.cpu.yml run --rm lig explain "Hello world" \
158
+ --steps 4 --granularity layer --layers 0 --target-tokens 1 -o /output/out.json
159
+ ```
160
+
161
+ See [docs/DOCKER.md](docs/DOCKER.md).
162
+
163
+ ### Dev setup (uv)
164
+
165
+ ```bash
166
+ bash scripts/ops/setup_uv_env.sh # GPU torch + pip install -e ".[dev]"
167
+ bash scripts/ops/setup_uv_env.sh --cpu # CPU torch
168
+ source .venv/bin/activate
169
+ ```
170
+
171
+ ### Regression tests
172
+
173
+ ```bash
174
+ bash scripts/run_regression_tests.sh
175
+ ```
176
+
177
+ ### Paper demo (no PTB download)
178
+
179
+ Interactive z2z map for the paper example sentence (PTB dev sample 410, Zero baseline):
180
+
181
+ - **GitHub Pages**: [demo](https://eightsuzuki.github.io/layer-wise-integrated-gradients/githubpage/z2z_token_contribution.html) — switch samples and routes (direct z→z / composed ATT×MLP; Zero / ITB / ITB-zeroRatio)
182
+ - **Regenerate** (PTB cache required): `python scripts/build_paper_demos.py && python scripts/build_demo_html.py`
183
+ - **Streamlit**: `pip install -e ".[demo]"` then `streamlit run lig/demo/streamlit_app.py`
184
+ - Precomputed JSON: `examples/paper_demo/json/` (see [DATA_NOTICE](examples/paper_demo/DATA_NOTICE.md))
185
+
186
+ ### Paper reproduction (Experiment A)
187
+
188
+ Requires [Treebank-3 (LDC99T42)](https://catalog.ldc.upenn.edu/LDC99T42). See [docs/REPRODUCTION.md](docs/REPRODUCTION.md).
189
+
190
+ ---
191
+
192
+ ## Features
193
+
194
+ - **One-call API** — `explain(text)` → JSON with z→u, u→z, and z→z attributions
195
+ - **CLI** — `lig explain "..." -o out.json`
196
+ - **Model-agnostic block boundaries** — auto-detect z (residual stream) and u (ATT output / MLP input) from module layout; BERT-family + GPT-2 full granularity; block-level z→z for MPNet, DistilBERT, Switch, Mamba
197
+ - **Boundary introspection** — `describe_boundaries("bert-base-uncased")` or `python examples/detect_model_boundaries.py`
198
+ - **Configurable** — integration steps, baselines, granularity, layers, target tokens
199
+
200
+ ---
201
+
202
+ ## Project layout
203
+
204
+ ```
205
+ .
206
+ ├── lig/ # Public API + encoder adapters + viz demo
207
+ ├── examples/paper_demo/ # One-sentence PTB excerpt + LIG JSON
208
+ ├── docs/ # DOCKER, PyPI, REPRODUCTION, Pages demo
209
+ ├── utils/calculations/ig/ # Core LIG implementations
210
+ ├── scripts/
211
+ │ ├── reproduce/ # Experiment A (PTB-gated)
212
+ │ ├── build_demo_html.py
213
+ │ └── run_regression_tests.sh
214
+ └── test/
215
+ ```
216
+
217
+ ---
218
+
219
+ ## Citation
220
+
221
+ ```bibtex
222
+ @inproceedings{suzuki2026lig,
223
+ title = {LIG: Layer-wise Integrated Gradients for Within-Layer Flow Analysis in Transformers},
224
+ author = {Suzuki, Eight and Hino, Hideitsu and Murata, Noboru},
225
+ year = {2026},
226
+ note = {Code: https://github.com/eightsuzuki/layer-wise-integrated-gradients}
227
+ }
228
+ ```
229
+
230
+ ---
231
+
232
+ ## License
233
+
234
+ [MIT License](LICENSE) for this software.
235
+
236
+ ## PyPI
237
+
238
+ Package metadata and publish steps: [docs/PYPI.md](docs/PYPI.md).
239
+
240
+ Maintainers: sync core IG code from the parent monorepo with `bash scripts/sync_from_monorepo.sh` when needed. See [MANIFEST.md](MANIFEST.md).
@@ -0,0 +1,196 @@
1
+ # Layer-wise Integrated Gradients (LIG)
2
+
3
+ <p align="center">
4
+ <img src="logo/LIG-LOGO.png" alt="LIG logo" width="660">
5
+ </p>
6
+
7
+ **Set-to-set Integrated Gradients for within-layer flow analysis in Transformers.**
8
+
9
+ [![Website](https://img.shields.io/badge/website-GitHub%20Pages-0d9488)](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
10
+ [![PyPI version](https://img.shields.io/pypi/v/layer-wise-integrated-gradients)](https://pypi.org/project/layer-wise-integrated-gradients/)
11
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
12
+ [![GitHub](https://img.shields.io/badge/code-GitHub-181717)](https://github.com/eightsuzuki/layer-wise-integrated-gradients)
13
+
14
+ LIG applies Integrated Gradients (IG) at ATT and MLP module boundaries inside each Transformer layer, composes within-layer token-to-token contributions, and compares them with layer-whole attribution under an $L_2$ diagnostic — without model-specific retraining or per-operation interpreter design.
15
+
16
+ - **Website &amp; interactive demo:** [eightsuzuki.github.io/layer-wise-integrated-gradients](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
17
+ - **Paper &amp; arXiv:** Coming soon
18
+
19
+ This release implements **L2-scalarized set-to-set IG** at module boundaries (z→u, u→z, z→z), with baselines **ITB** (`self_input_token`), **ITB-zeroRatio** (`itb_zero_ratio`), **ITB-mapRatio** (`itb_map_ratio`), **Zero**, and **ATTITBa=0** (`att_itb_a0`) for MLP.
20
+
21
+ ---
22
+
23
+ ## Quick start
24
+
25
+ Install **PyTorch first** (CUDA/CPU wheel index differs), then LIG:
26
+
27
+ ```bash
28
+ # GPU (CUDA 12.1 example)
29
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
30
+
31
+ # From PyPI
32
+ pip install layer-wise-integrated-gradients
33
+
34
+ # Or from GitHub
35
+ pip install "layer-wise-integrated-gradients @ git+https://github.com/eightsuzuki/layer-wise-integrated-gradients.git"
36
+
37
+ # Development clone
38
+ git clone https://github.com/eightsuzuki/layer-wise-integrated-gradients.git
39
+ cd layer-wise-integrated-gradients
40
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
41
+ pip install -e ".[dev]"
42
+ ```
43
+
44
+ ### Explain one sentence → JSON
45
+
46
+ ```python
47
+ from lig import explain
48
+
49
+ result = explain(
50
+ "The cat sat on the mat.",
51
+ model="bert-base-uncased",
52
+ num_steps=32,
53
+ granularity="all", # att (z→u), mlp (u→z), layer (z→z)
54
+ layers=[0, 11],
55
+ )
56
+ ```
57
+
58
+ ```bash
59
+ lig explain "The cat sat on the mat." --steps 32 --granularity all -o attributions.json
60
+ ```
61
+
62
+ ### Supported models
63
+
64
+ | Family | Models | `granularity` |
65
+ |--------|--------|---------------|
66
+ | BERT-style | BERT, RoBERTa, DeBERTa, ELECTRA, XLM-RoBERTa, … | z→u, u→z, z→z |
67
+ | Block-only | MPNet, DistilBERT, ModernBERT, Switch MoE encoder, Mamba | **z→z (layer) only** |
68
+ | GPT-2 (decoder) | z→u, u→z, z→z (`granularity="all"`) | — |
69
+
70
+ GPT-2 example:
71
+
72
+ ```python
73
+ explain(
74
+ "Hello world",
75
+ model="gpt2",
76
+ granularity="all",
77
+ baseline_att="self_input_token", # or itb_zero_ratio / itb_map_ratio
78
+ baseline_mlp="att_itb_a0", # or zero
79
+ layers=[0],
80
+ )
81
+ ```
82
+
83
+ ATT ITB self-contribution variants (see method section in the upcoming paper):
84
+
85
+ ```python
86
+ explain("Hello world", baseline_att="itb_zero_ratio", granularity="att", layers=[0])
87
+ explain("Hello world", baseline_att="itb_map_ratio", granularity="att", layers=[0])
88
+ explain("Hello world", baseline_layer="itb_zero_ratio", granularity="layer", layers=[0])
89
+ ```
90
+
91
+ Other decoders (Llama, …): [docs/DECODER_DESIGN.md](docs/DECODER_DESIGN.md).
92
+
93
+ ### Inspect boundaries (no IG run)
94
+
95
+ ```python
96
+ from lig import describe_boundaries
97
+
98
+ # Config-only (no weight download)
99
+ describe_boundaries("gpt2", load_weights=False)
100
+
101
+ # Full module introspection
102
+ describe_boundaries("bert-base-uncased", load_weights=True, device="cpu")
103
+ ```
104
+
105
+ ```bash
106
+ python examples/detect_model_boundaries.py bert-base-uncased gpt2 distilbert-base-uncased
107
+ ```
108
+
109
+ ### Docker
110
+
111
+ ```bash
112
+ docker compose -f docker-compose.cpu.yml build
113
+ docker compose -f docker-compose.cpu.yml run --rm lig explain "Hello world" \
114
+ --steps 4 --granularity layer --layers 0 --target-tokens 1 -o /output/out.json
115
+ ```
116
+
117
+ See [docs/DOCKER.md](docs/DOCKER.md).
118
+
119
+ ### Dev setup (uv)
120
+
121
+ ```bash
122
+ bash scripts/ops/setup_uv_env.sh # GPU torch + pip install -e ".[dev]"
123
+ bash scripts/ops/setup_uv_env.sh --cpu # CPU torch
124
+ source .venv/bin/activate
125
+ ```
126
+
127
+ ### Regression tests
128
+
129
+ ```bash
130
+ bash scripts/run_regression_tests.sh
131
+ ```
132
+
133
+ ### Paper demo (no PTB download)
134
+
135
+ Interactive z2z map for the paper example sentence (PTB dev sample 410, Zero baseline):
136
+
137
+ - **GitHub Pages**: [demo](https://eightsuzuki.github.io/layer-wise-integrated-gradients/githubpage/z2z_token_contribution.html) — switch samples and routes (direct z→z / composed ATT×MLP; Zero / ITB / ITB-zeroRatio)
138
+ - **Regenerate** (PTB cache required): `python scripts/build_paper_demos.py && python scripts/build_demo_html.py`
139
+ - **Streamlit**: `pip install -e ".[demo]"` then `streamlit run lig/demo/streamlit_app.py`
140
+ - Precomputed JSON: `examples/paper_demo/json/` (see [DATA_NOTICE](examples/paper_demo/DATA_NOTICE.md))
141
+
142
+ ### Paper reproduction (Experiment A)
143
+
144
+ Requires [Treebank-3 (LDC99T42)](https://catalog.ldc.upenn.edu/LDC99T42). See [docs/REPRODUCTION.md](docs/REPRODUCTION.md).
145
+
146
+ ---
147
+
148
+ ## Features
149
+
150
+ - **One-call API** — `explain(text)` → JSON with z→u, u→z, and z→z attributions
151
+ - **CLI** — `lig explain "..." -o out.json`
152
+ - **Model-agnostic block boundaries** — auto-detect z (residual stream) and u (ATT output / MLP input) from module layout; BERT-family + GPT-2 full granularity; block-level z→z for MPNet, DistilBERT, Switch, Mamba
153
+ - **Boundary introspection** — `describe_boundaries("bert-base-uncased")` or `python examples/detect_model_boundaries.py`
154
+ - **Configurable** — integration steps, baselines, granularity, layers, target tokens
155
+
156
+ ---
157
+
158
+ ## Project layout
159
+
160
+ ```
161
+ .
162
+ ├── lig/ # Public API + encoder adapters + viz demo
163
+ ├── examples/paper_demo/ # One-sentence PTB excerpt + LIG JSON
164
+ ├── docs/ # DOCKER, PyPI, REPRODUCTION, Pages demo
165
+ ├── utils/calculations/ig/ # Core LIG implementations
166
+ ├── scripts/
167
+ │ ├── reproduce/ # Experiment A (PTB-gated)
168
+ │ ├── build_demo_html.py
169
+ │ └── run_regression_tests.sh
170
+ └── test/
171
+ ```
172
+
173
+ ---
174
+
175
+ ## Citation
176
+
177
+ ```bibtex
178
+ @inproceedings{suzuki2026lig,
179
+ title = {LIG: Layer-wise Integrated Gradients for Within-Layer Flow Analysis in Transformers},
180
+ author = {Suzuki, Eight and Hino, Hideitsu and Murata, Noboru},
181
+ year = {2026},
182
+ note = {Code: https://github.com/eightsuzuki/layer-wise-integrated-gradients}
183
+ }
184
+ ```
185
+
186
+ ---
187
+
188
+ ## License
189
+
190
+ [MIT License](LICENSE) for this software.
191
+
192
+ ## PyPI
193
+
194
+ Package metadata and publish steps: [docs/PYPI.md](docs/PYPI.md).
195
+
196
+ Maintainers: sync core IG code from the parent monorepo with `bash scripts/sync_from_monorepo.sh` when needed. See [MANIFEST.md](MANIFEST.md).
@@ -0,0 +1,240 @@
1
+ Metadata-Version: 2.4
2
+ Name: layer-wise-integrated-gradients
3
+ Version: 0.1.0
4
+ Summary: Layer-wise Integrated Gradients (LIG) for within-layer flow analysis in Transformers
5
+ Author: Eight Suzuki, Hideitsu Hino, Noboru Murata
6
+ License: MIT
7
+ Project-URL: Homepage, https://eightsuzuki.github.io/layer-wise-integrated-gradients/
8
+ Project-URL: Documentation, https://eightsuzuki.github.io/layer-wise-integrated-gradients/
9
+ Project-URL: Repository, https://github.com/eightsuzuki/layer-wise-integrated-gradients
10
+ Project-URL: Issues, https://github.com/eightsuzuki/layer-wise-integrated-gradients/issues
11
+ Project-URL: PyPI, https://pypi.org/project/layer-wise-integrated-gradients/
12
+ Keywords: transformers,explainability,integrated-gradients,lig,attention,bert
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Requires-Python: >=3.10
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy>=2.0.0
26
+ Requires-Dist: captum
27
+ Requires-Dist: transformers<5.0.0,>=4.21.0
28
+ Requires-Dist: tokenizers>=0.13.0
29
+ Requires-Dist: lightning
30
+ Requires-Dist: pandas
31
+ Requires-Dist: joblib
32
+ Requires-Dist: pyyaml>=6.0
33
+ Requires-Dist: scipy>=1.10.0
34
+ Provides-Extra: demo
35
+ Requires-Dist: streamlit>=1.28.0; extra == "demo"
36
+ Provides-Extra: reproduce
37
+ Requires-Dist: openpyxl>=3.1.0; extra == "reproduce"
38
+ Provides-Extra: dev
39
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
40
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
41
+ Requires-Dist: build>=1.0.0; extra == "dev"
42
+ Requires-Dist: twine>=5.0.0; extra == "dev"
43
+ Dynamic: license-file
44
+
45
+ # Layer-wise Integrated Gradients (LIG)
46
+
47
+ <p align="center">
48
+ <img src="logo/LIG-LOGO.png" alt="LIG logo" width="660">
49
+ </p>
50
+
51
+ **Set-to-set Integrated Gradients for within-layer flow analysis in Transformers.**
52
+
53
+ [![Website](https://img.shields.io/badge/website-GitHub%20Pages-0d9488)](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
54
+ [![PyPI version](https://img.shields.io/pypi/v/layer-wise-integrated-gradients)](https://pypi.org/project/layer-wise-integrated-gradients/)
55
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
56
+ [![GitHub](https://img.shields.io/badge/code-GitHub-181717)](https://github.com/eightsuzuki/layer-wise-integrated-gradients)
57
+
58
+ LIG applies Integrated Gradients (IG) at ATT and MLP module boundaries inside each Transformer layer, composes within-layer token-to-token contributions, and compares them with layer-whole attribution under an $L_2$ diagnostic — without model-specific retraining or per-operation interpreter design.
59
+
60
+ - **Website &amp; interactive demo:** [eightsuzuki.github.io/layer-wise-integrated-gradients](https://eightsuzuki.github.io/layer-wise-integrated-gradients/)
61
+ - **Paper &amp; arXiv:** Coming soon
62
+
63
+ This release implements **L2-scalarized set-to-set IG** at module boundaries (z→u, u→z, z→z), with baselines **ITB** (`self_input_token`), **ITB-zeroRatio** (`itb_zero_ratio`), **ITB-mapRatio** (`itb_map_ratio`), **Zero**, and **ATTITBa=0** (`att_itb_a0`) for MLP.
64
+
65
+ ---
66
+
67
+ ## Quick start
68
+
69
+ Install **PyTorch first** (CUDA/CPU wheel index differs), then LIG:
70
+
71
+ ```bash
72
+ # GPU (CUDA 12.1 example)
73
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
74
+
75
+ # From PyPI
76
+ pip install layer-wise-integrated-gradients
77
+
78
+ # Or from GitHub
79
+ pip install "layer-wise-integrated-gradients @ git+https://github.com/eightsuzuki/layer-wise-integrated-gradients.git"
80
+
81
+ # Development clone
82
+ git clone https://github.com/eightsuzuki/layer-wise-integrated-gradients.git
83
+ cd layer-wise-integrated-gradients
84
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
85
+ pip install -e ".[dev]"
86
+ ```
87
+
88
+ ### Explain one sentence → JSON
89
+
90
+ ```python
91
+ from lig import explain
92
+
93
+ result = explain(
94
+ "The cat sat on the mat.",
95
+ model="bert-base-uncased",
96
+ num_steps=32,
97
+ granularity="all", # att (z→u), mlp (u→z), layer (z→z)
98
+ layers=[0, 11],
99
+ )
100
+ ```
101
+
102
+ ```bash
103
+ lig explain "The cat sat on the mat." --steps 32 --granularity all -o attributions.json
104
+ ```
105
+
106
+ ### Supported models
107
+
108
+ | Family | Models | `granularity` |
109
+ |--------|--------|---------------|
110
+ | BERT-style | BERT, RoBERTa, DeBERTa, ELECTRA, XLM-RoBERTa, … | z→u, u→z, z→z |
111
+ | Block-only | MPNet, DistilBERT, ModernBERT, Switch MoE encoder, Mamba | **z→z (layer) only** |
112
+ | GPT-2 (decoder) | z→u, u→z, z→z (`granularity="all"`) | — |
113
+
114
+ GPT-2 example:
115
+
116
+ ```python
117
+ explain(
118
+ "Hello world",
119
+ model="gpt2",
120
+ granularity="all",
121
+ baseline_att="self_input_token", # or itb_zero_ratio / itb_map_ratio
122
+ baseline_mlp="att_itb_a0", # or zero
123
+ layers=[0],
124
+ )
125
+ ```
126
+
127
+ ATT ITB self-contribution variants (see method section in the upcoming paper):
128
+
129
+ ```python
130
+ explain("Hello world", baseline_att="itb_zero_ratio", granularity="att", layers=[0])
131
+ explain("Hello world", baseline_att="itb_map_ratio", granularity="att", layers=[0])
132
+ explain("Hello world", baseline_layer="itb_zero_ratio", granularity="layer", layers=[0])
133
+ ```
134
+
135
+ Other decoders (Llama, …): [docs/DECODER_DESIGN.md](docs/DECODER_DESIGN.md).
136
+
137
+ ### Inspect boundaries (no IG run)
138
+
139
+ ```python
140
+ from lig import describe_boundaries
141
+
142
+ # Config-only (no weight download)
143
+ describe_boundaries("gpt2", load_weights=False)
144
+
145
+ # Full module introspection
146
+ describe_boundaries("bert-base-uncased", load_weights=True, device="cpu")
147
+ ```
148
+
149
+ ```bash
150
+ python examples/detect_model_boundaries.py bert-base-uncased gpt2 distilbert-base-uncased
151
+ ```
152
+
153
+ ### Docker
154
+
155
+ ```bash
156
+ docker compose -f docker-compose.cpu.yml build
157
+ docker compose -f docker-compose.cpu.yml run --rm lig explain "Hello world" \
158
+ --steps 4 --granularity layer --layers 0 --target-tokens 1 -o /output/out.json
159
+ ```
160
+
161
+ See [docs/DOCKER.md](docs/DOCKER.md).
162
+
163
+ ### Dev setup (uv)
164
+
165
+ ```bash
166
+ bash scripts/ops/setup_uv_env.sh # GPU torch + pip install -e ".[dev]"
167
+ bash scripts/ops/setup_uv_env.sh --cpu # CPU torch
168
+ source .venv/bin/activate
169
+ ```
170
+
171
+ ### Regression tests
172
+
173
+ ```bash
174
+ bash scripts/run_regression_tests.sh
175
+ ```
176
+
177
+ ### Paper demo (no PTB download)
178
+
179
+ Interactive z2z map for the paper example sentence (PTB dev sample 410, Zero baseline):
180
+
181
+ - **GitHub Pages**: [demo](https://eightsuzuki.github.io/layer-wise-integrated-gradients/githubpage/z2z_token_contribution.html) — switch samples and routes (direct z→z / composed ATT×MLP; Zero / ITB / ITB-zeroRatio)
182
+ - **Regenerate** (PTB cache required): `python scripts/build_paper_demos.py && python scripts/build_demo_html.py`
183
+ - **Streamlit**: `pip install -e ".[demo]"` then `streamlit run lig/demo/streamlit_app.py`
184
+ - Precomputed JSON: `examples/paper_demo/json/` (see [DATA_NOTICE](examples/paper_demo/DATA_NOTICE.md))
185
+
186
+ ### Paper reproduction (Experiment A)
187
+
188
+ Requires [Treebank-3 (LDC99T42)](https://catalog.ldc.upenn.edu/LDC99T42). See [docs/REPRODUCTION.md](docs/REPRODUCTION.md).
189
+
190
+ ---
191
+
192
+ ## Features
193
+
194
+ - **One-call API** — `explain(text)` → JSON with z→u, u→z, and z→z attributions
195
+ - **CLI** — `lig explain "..." -o out.json`
196
+ - **Model-agnostic block boundaries** — auto-detect z (residual stream) and u (ATT output / MLP input) from module layout; BERT-family + GPT-2 full granularity; block-level z→z for MPNet, DistilBERT, Switch, Mamba
197
+ - **Boundary introspection** — `describe_boundaries("bert-base-uncased")` or `python examples/detect_model_boundaries.py`
198
+ - **Configurable** — integration steps, baselines, granularity, layers, target tokens
199
+
200
+ ---
201
+
202
+ ## Project layout
203
+
204
+ ```
205
+ .
206
+ ├── lig/ # Public API + encoder adapters + viz demo
207
+ ├── examples/paper_demo/ # One-sentence PTB excerpt + LIG JSON
208
+ ├── docs/ # DOCKER, PyPI, REPRODUCTION, Pages demo
209
+ ├── utils/calculations/ig/ # Core LIG implementations
210
+ ├── scripts/
211
+ │ ├── reproduce/ # Experiment A (PTB-gated)
212
+ │ ├── build_demo_html.py
213
+ │ └── run_regression_tests.sh
214
+ └── test/
215
+ ```
216
+
217
+ ---
218
+
219
+ ## Citation
220
+
221
+ ```bibtex
222
+ @inproceedings{suzuki2026lig,
223
+ title = {LIG: Layer-wise Integrated Gradients for Within-Layer Flow Analysis in Transformers},
224
+ author = {Suzuki, Eight and Hino, Hideitsu and Murata, Noboru},
225
+ year = {2026},
226
+ note = {Code: https://github.com/eightsuzuki/layer-wise-integrated-gradients}
227
+ }
228
+ ```
229
+
230
+ ---
231
+
232
+ ## License
233
+
234
+ [MIT License](LICENSE) for this software.
235
+
236
+ ## PyPI
237
+
238
+ Package metadata and publish steps: [docs/PYPI.md](docs/PYPI.md).
239
+
240
+ Maintainers: sync core IG code from the parent monorepo with `bash scripts/sync_from_monorepo.sh` when needed. See [MANIFEST.md](MANIFEST.md).