mi-crow 1.0.0__py3-none-any.whl → 1.0.0.post2__py3-none-any.whl

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.
@@ -1,8 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mi-crow
3
- Version: 1.0.0
4
- Summary: Engineer Thesis: Explaining and modifying LLM responses using SAE and concepts.
5
- Author-email: Hubert Kowalski <your.email@example.com>, Adam Kaniasty <adam.kaniasty@gmail.com>
3
+ Version: 1.0.0.post2
4
+ Summary: Python library for mechanistic interpretability research on Large Language Models. Designed for researchers, provides unified interface for SAE training, activation hooks, and concept manipulation.
5
+ Author: Hubert Kowalski
6
+ Author-email: Adam Kaniasty <adam.kaniasty@gmail.com>
7
+ Maintainer: Hubert Kowalski
8
+ Maintainer-email: Adam Kaniasty <adam.kaniasty@gmail.com>
6
9
  Requires-Python: >=3.10
7
10
  Description-Content-Type: text/markdown
8
11
  Requires-Dist: accelerate>=1.10.1
@@ -19,6 +22,7 @@ Requires-Dist: wandb>=0.22.1
19
22
  Requires-Dist: pytest>=8.4.2
20
23
  Requires-Dist: pytest-xdist>=3.8.0
21
24
  Requires-Dist: seaborn>=0.13.2
25
+ Requires-Dist: numpy<2.0,>=1.20.0
22
26
  Provides-Extra: dev
23
27
  Requires-Dist: pre-commit>=4.3.0; extra == "dev"
24
28
  Requires-Dist: ruff>=0.13.2; extra == "dev"
@@ -36,10 +40,153 @@ Requires-Dist: mkdocs-literate-nav>=0.6; extra == "docs"
36
40
  Requires-Dist: mkdocs-gen-files>=0.5; extra == "docs"
37
41
  Requires-Dist: mike>=2.1; extra == "docs"
38
42
 
39
- ![CI](https://github.com/AdamKaniasty/Inzynierka/actions/workflows/tests.yml/badge.svg?branch=main)
43
+ <div align="center">
44
+ <img src="docs/logo.svg" alt="Mi-Crow Logo" width="200">
45
+ </div>
46
+
47
+ # Mi-Crow: Mechanistic Interpretability for Large Language Models
48
+
49
+ [![CI](https://github.com/AdamKaniasty/Inzynierka/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/AdamKaniasty/Inzynierka/actions)
50
+ [![PyPI](https://img.shields.io/pypi/v/mi-crow)](https://pypi.org/project/mi-crow/)
40
51
  [![Docs](https://img.shields.io/badge/docs-gh--pages-blue)](https://adamkaniasty.github.io/Inzynierka/)
41
52
 
42
- ## Running Tests
53
+ **Mi-Crow** is a Python library for mechanistic interpretability research on Large Language Models (LLMs). Designed for researchers, it provides a unified interface for analyzing and controlling model behavior through Sparse Autoencoders (SAEs), activation hooks, and concept manipulation.
54
+
55
+ ## Features
56
+
57
+ - **Unified Model Interface** - Work with any HuggingFace language model through a consistent API
58
+ - **Sparse Autoencoder Training** - Train SAEs to extract interpretable features from model activations
59
+ - **Hook System** - Intercept and manipulate model activations with minimal performance overhead
60
+ - **Concept Discovery & Manipulation** - Discover and control model behavior through learned concepts
61
+ - **Hierarchical Data Persistence** - Efficient storage and management of large-scale experiment data
62
+ - **Research Focused** - Comprehensive testing (85%+ coverage), extensive documentation, and designed for interpretability research workflows
63
+
64
+ ## Installation
65
+
66
+ ### Install from PyPI
67
+
68
+ ```bash
69
+ pip install mi-crow
70
+ ```
71
+
72
+ ### Install from Source
73
+
74
+ ```bash
75
+ git clone https://github.com/AdamKaniasty/Mi-Crow.git
76
+ cd Mi-Crow
77
+ pip install -e .
78
+ ```
79
+
80
+ ### Requirements
81
+
82
+ - **Python 3.12+** (required for modern type hints and features)
83
+ - **PyTorch** - Tensor operations and neural networks
84
+ - **Transformers** - Model loading and tokenization
85
+ - **Accelerate** - Distributed and mixed-precision training
86
+ - **Datasets** - Dataset loading and processing
87
+ - **overcomplete** - SAE implementations
88
+
89
+ ## Quick Start
90
+
91
+ ### Basic Usage
92
+
93
+ ```python
94
+ from mi_crow.language_model import LanguageModel
95
+
96
+ # Initialize a language model
97
+ lm = LanguageModel(model_id="bielik")
98
+
99
+ # Run inference
100
+ outputs = lm.forwards(["Hello, world!"])
101
+
102
+ # Access activations and outputs
103
+ print(outputs.logits)
104
+ ```
105
+
106
+ ### Training an SAE
107
+
108
+ ```python
109
+ from mi_crow.language_model import LanguageModel
110
+ from mi_crow.mechanistic.sae import SaeTrainer
111
+ from mi_crow.mechanistic.sae.modules import TopKSae
112
+
113
+ # Load model and collect activations
114
+ lm = LanguageModel(model_id="bielik")
115
+ activations = lm.save_activations(
116
+ dataset=["Your text data here"],
117
+ layers=["transformer_h_0_attn_c_attn"]
118
+ )
119
+
120
+ # Train SAE
121
+ trainer = SaeTrainer(
122
+ model=lm,
123
+ layer="transformer_h_0_attn_c_attn",
124
+ sae_class=TopKSae,
125
+ hyperparams={"epochs": 10, "batch_size": 256}
126
+ )
127
+ sae = trainer.train(activations)
128
+ ```
129
+
130
+ ### Concept Manipulation
131
+
132
+ ```python
133
+ # Load concepts and manipulate model behavior
134
+ concepts = lm.load_concepts(sae_id="your_sae_id")
135
+ concepts.manipulate(neuron_idx=0, scale_factor=1.5)
136
+
137
+ # Run inference with concept manipulation
138
+ outputs = lm.forwards(
139
+ ["Your prompt"],
140
+ with_controllers=True,
141
+ concept_config=concepts
142
+ )
143
+ ```
144
+
145
+ ## Documentation
146
+
147
+ - **Full Documentation**: [adamkaniasty.github.io/Inzynierka](https://adamkaniasty.github.io/Inzynierka/)
148
+ - **GitHub Repository**: [github.com/AdamKaniasty/Mi-Crow](https://github.com/AdamKaniasty/Mi-Crow/)
149
+ - **Example Notebooks**: See `examples/` directory for Jupyter notebook tutorials
150
+
151
+ ## Architecture
152
+
153
+ Mi-Crow follows a modular design with five core components:
154
+
155
+ 1. **`language_model/`** - Unified interface for language models
156
+ - Model initialization from HuggingFace Hub or local files
157
+ - Unified inference interface with mixed-precision support
158
+ - Architecture-agnostic layer abstraction
159
+
160
+ 2. **`hooks/`** - Flexible hook system for activation interception
161
+ - Detectors for observing activations
162
+ - Controllers for modifying model behavior
163
+ - Support for FORWARD and PRE_FORWARD hooks
164
+
165
+ 3. **`mechanistic/`** - SAE training and concept manipulation
166
+ - Sparse Autoencoder training (TopK, L1 variants)
167
+ - Concept dictionary management
168
+ - Concept-based model steering
169
+
170
+ 4. **`store/`** - Hierarchical data persistence
171
+ - Efficient tensor storage in safetensors format
172
+ - Batch iteration for large datasets
173
+ - Metadata management
174
+
175
+ 5. **`datasets/`** - Dataset loading and processing
176
+ - HuggingFace dataset integration
177
+ - Local file dataset support
178
+
179
+ ## Example Workflow
180
+
181
+ See the example notebooks in the `examples/` directory:
182
+
183
+ 1. **`01_train_sae_model.ipynb`** - Train an SAE on model activations
184
+ 2. **`02_attach_sae_and_save_texts.ipynb`** - Collect top activating texts
185
+ 3. **`03_load_concepts.ipynb`** - Load and manipulate concepts
186
+
187
+ ## Development
188
+
189
+ ### Running Tests
43
190
 
44
191
  The project uses pytest for testing. Tests are organized into unit tests and end-to-end tests.
45
192
 
@@ -77,6 +224,45 @@ pytest tests/e2e -q
77
224
 
78
225
  The test suite is configured to require at least 85% code coverage. Coverage reports are generated in both terminal and XML formats.
79
226
 
227
+ The project maintains **85%+ code coverage** requirement.
228
+
229
+ ### Code Quality
230
+
231
+ - **Linting**: Ruff for code formatting and linting
232
+ - **Pre-commit Hooks**: Automated quality checks
233
+ - **Type Hints**: Extensive use of Python type annotations
234
+ - **CI/CD**: GitHub Actions for automated testing and deployment
235
+
236
+ ## Citation
237
+
238
+ If you use Mi-Crow in your research, please cite:
239
+
240
+ ```bibtex
241
+ @thesis{kaniasty2025microw,
242
+ title={Mechanistic Interpretability for Large Language Models: A Production-Ready Framework},
243
+ author={Kaniasty, Adam and Kowalski, Hubert},
244
+ year={2025},
245
+ school={Warsaw University of Technology},
246
+ note={Engineering Thesis}
247
+ }
248
+ ```
249
+
250
+ ## License
251
+
252
+ See the main repository for license information: [Mi-Crow License](https://github.com/AdamKaniasty/Mi-Crow/)
253
+
254
+ ## Contact
255
+
256
+ - **Maintainers**: Adam Kaniasty, Hubert Kowalski
257
+ - **Email**: adam.kaniasty@gmail.com
258
+ - **GitHub**: [@AdamKaniasty](https://github.com/AdamKaniasty)
259
+
260
+ ## Acknowledgments
261
+
262
+ This work was developed in collaboration with the **Bielik** team and represents a contribution to the open-source mechanistic interpretability community.
263
+
264
+ ---
265
+
80
266
  ## Backend (FastAPI) quickstart
81
267
 
82
268
  Install server-only dependencies (kept out of the core library) with uv:
@@ -1,52 +1,54 @@
1
1
  mi_crow/__init__.py,sha256=J7aXVlAicbjvk5630rhDxx0ATsvZnihud5u_aQpAwY8,487
2
2
  mi_crow/utils.py,sha256=LTfh2Ep87lAgPBaZkrQPP9caXFJoS9zUxu4qFuV4kzM,1549
3
3
  mi_crow/datasets/__init__.py,sha256=lCAc3nFlvoERrBPAan6C9YFmDx86W2gbIAy267Rb2Sk,349
4
- mi_crow/datasets/base_dataset.py,sha256=fSs63o0LDNXt1-i0HmKTxaP33CMcS6rWJ3C4vpB8qR0,22527
5
- mi_crow/datasets/classification_dataset.py,sha256=c2peu0kWVY3N8XlEi9GjRuE6MmVV8f8vz8gtPmxI70s,21721
4
+ mi_crow/datasets/base_dataset.py,sha256=ki8C_GKNte6_Csgezh_l3E1-rc15uiwKlgUtFTiOB50,25343
5
+ mi_crow/datasets/classification_dataset.py,sha256=uD2dmf6lcLgc6R3fjhyXPkC8mrEhBPTVLWWihrbdDlM,25876
6
6
  mi_crow/datasets/loading_strategy.py,sha256=17VM3Td8lqDllGIx9DHI6WiXmSKKQHDHbfe4ZeM8ATA,1206
7
- mi_crow/datasets/text_dataset.py,sha256=dk5RzWy-T7ssQNwv84FrgEZ83zuDgpq91XgUfbwvk8c,16796
7
+ mi_crow/datasets/text_dataset.py,sha256=pEslx7_Of7zUS5umdraKYxJyeeilt4-1Ydjfjn5BKrE,21922
8
8
  mi_crow/hooks/__init__.py,sha256=KYy5qcbEpnJceNH86ofy43Suu_36QXjj0HYl79rVyls,693
9
- mi_crow/hooks/controller.py,sha256=eo8LMERORXYUjH4-_R6DHk5JKN6O8SW6PlnuBFrlNqg,6063
9
+ mi_crow/hooks/controller.py,sha256=pt00-9XBvX13OLWGst3Y0RNTttkMCVjlWj3EaP4Wtv4,6198
10
10
  mi_crow/hooks/detector.py,sha256=Bj3xz56cSgRvbcoQBsHIdlJdf0dtgVLw3l1pOSRvRAg,3114
11
11
  mi_crow/hooks/hook.py,sha256=JrCyPptXzHICAToxug7FD8zKWKZcLxbXfIe7UHCkh34,7542
12
- mi_crow/hooks/utils.py,sha256=GdUAqL9InCsthjBVWbZtVQp2VtQLLOMaJy8S8Tc7WX0,2024
12
+ mi_crow/hooks/utils.py,sha256=z6SaGWDrwRSUpO-TeUuxeiz62kg9dlmt-wTC3juzOjA,5342
13
13
  mi_crow/hooks/implementations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  mi_crow/hooks/implementations/function_controller.py,sha256=Rwu1Ghffqm-Jc4mqniRdIsXQeV24JKlx9CMO8jP8iv0,3092
15
- mi_crow/hooks/implementations/layer_activation_detector.py,sha256=mFqLkWrBlNYzYYvwI8O-ymnpwP1UJSX7eWIQ7Fp-lxE,3167
16
- mi_crow/hooks/implementations/model_input_detector.py,sha256=4AFA88eQ-zM9Kz8bpv4Bl5cvlZF5ZyIRXNi7Znd6V98,10078
17
- mi_crow/hooks/implementations/model_output_detector.py,sha256=BmD8oJ1k_nv4hIzG-9SGxSCRARQkLRI4d_XQmKs6jjE,4883
15
+ mi_crow/hooks/implementations/layer_activation_detector.py,sha256=yk1uTyxsQLHQOlCk0k9_FqPyEs8tpi5V98eU9UwwCDo,3360
16
+ mi_crow/hooks/implementations/model_input_detector.py,sha256=W-CX5FzExfWI0JcY43tPMCr67LlaRXvnP3GRE2BGWc8,10168
17
+ mi_crow/hooks/implementations/model_output_detector.py,sha256=_x-4CmQXw7nRJRM9f7_iSJi11kyNYbnY7s2IF1Zu5h4,5111
18
18
  mi_crow/language_model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
- mi_crow/language_model/activations.py,sha256=mJhuXkQ_JKzQsrFossnXVTdq4WgBI10Gi0ZYeQ9USFY,16989
19
+ mi_crow/language_model/activations.py,sha256=bZ_2OjTQE4itfLoynJywGYV88PxA9jxV5P_aAGqCGXU,20539
20
20
  mi_crow/language_model/context.py,sha256=TJSe6IruGktePpQ0dtHIoeatSY72qAhEKeTvLizCdlw,1124
21
21
  mi_crow/language_model/contracts.py,sha256=6ij7rzJcpSAKgYx-fiefg0Fi8TsFugaM5IlihEm_2VM,268
22
+ mi_crow/language_model/device_manager.py,sha256=nxGgrZGD5MTZACJe45_CIzp1YZNZmNUSrTFz4iG7bSo,3730
22
23
  mi_crow/language_model/hook_metadata.py,sha256=GACZjZUneo2l5j7DCFycLAunTm0etdMQ2YB_xgueUuk,1394
23
- mi_crow/language_model/inference.py,sha256=-Kpm85jM8y6-GyDgrvIczitBIwGh8grJP8aYuXsLV-g,19082
24
- mi_crow/language_model/initialization.py,sha256=e_Vkk-p9KWRt6-Hmkm6I29dTf20jzEAyNF9CG4nc48M,3704
25
- mi_crow/language_model/language_model.py,sha256=b4KniweHXauPHPgHba9cAlLK86z2atfeoetnG-UHoSo,13998
26
- mi_crow/language_model/layers.py,sha256=7RdVU1mXt5oy2OhzNoKDeD9Omm899ZC-BJqpcTfxP2w,15933
27
- mi_crow/language_model/persistence.py,sha256=9wQE6tRvLg7BgdLlkKRTOfRwXb5Q0LsEgg8B9J7Yos0,5881
24
+ mi_crow/language_model/inference.py,sha256=41hff-1MuL3JwSKF5p1z-j-KxgBhwPDaNIqubodV0pA,19623
25
+ mi_crow/language_model/initialization.py,sha256=q4zBS_BemeIGSMsE3iXZ8BG_ZnT3nSd2hYtBxa01KoY,4025
26
+ mi_crow/language_model/language_model.py,sha256=rblruse5sDEi2a0r9YzvgMDhytHpVwwDSBB44_OGh90,13331
27
+ mi_crow/language_model/layers.py,sha256=tnl1xBmpHNBgapXP1o8QB34uMkd2o2ufNYDQA0SD8M0,15893
28
+ mi_crow/language_model/persistence.py,sha256=n7NKo_J-KgB2zE4Tt6S7n9cSox3v768ac47tqq2ALM4,6030
28
29
  mi_crow/language_model/tokenizer.py,sha256=uZbMDVNnzu8WZINUaR1tLFXiuk9V5pAoahwnJOUvEuE,7379
29
- mi_crow/language_model/utils.py,sha256=5Y7scRvvudUjKDV8QPhC3HAc2S-dCuqbm6xEjRr0fRM,2630
30
+ mi_crow/language_model/utils.py,sha256=dwql6LEcJ4kKzl7wy-qL5uet4xFFGlGhCdlm65nUJLU,2577
30
31
  mi_crow/mechanistic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
32
  mi_crow/mechanistic/sae/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
32
33
  mi_crow/mechanistic/sae/autoencoder_context.py,sha256=u5WzSlLb8_HaJF9LwGqe-J_rE-iRCoXbvwEhTZIArkw,947
33
- mi_crow/mechanistic/sae/sae.py,sha256=R2IckZ-UDVXFURUSRoIdX0MGLe0TWU5JCVeDXPz1Wv4,6053
34
- mi_crow/mechanistic/sae/sae_trainer.py,sha256=VqN9UgEPtgYItzAS0RqMfOhTnTxIEuModO0noHYieFA,26327
34
+ mi_crow/mechanistic/sae/sae.py,sha256=i1TO19MY7WBUVfRGMi3WPkgDaHmoNr8Ul2ZZWwo6KCk,6180
35
+ mi_crow/mechanistic/sae/sae_trainer.py,sha256=bgIzGwvrJBNLOAXIIfXJZftxJc5LBH379Pas_DSob-g,41342
35
36
  mi_crow/mechanistic/sae/concepts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
- mi_crow/mechanistic/sae/concepts/autoencoder_concepts.py,sha256=SE7A6y6cZiMGhylljNdZz0D8lhrulUh1FYiKhnPPRvY,14387
37
- mi_crow/mechanistic/sae/concepts/concept_dictionary.py,sha256=aUXTe4Fy7Oe7iYJOcQImEXYMZAXhnkfAszZNwivk4eg,7673
37
+ mi_crow/mechanistic/sae/concepts/autoencoder_concepts.py,sha256=cT5hL6NGkpULpOjDCR2EZyYDgxwsrJCnp2RMXlB56k0,17135
38
+ mi_crow/mechanistic/sae/concepts/concept_dictionary.py,sha256=te_tSDGIMEqItTDiB1T-rE2YlFfN-eVVxgR4t5GlZH8,8001
38
39
  mi_crow/mechanistic/sae/concepts/concept_models.py,sha256=HGyPoMSmj8CAg9joIa6fV_FfWAY2XN4LFPIkNQiJWbY,133
39
40
  mi_crow/mechanistic/sae/concepts/input_tracker.py,sha256=kIiqt7guv_-9-UPYtefAFJbHkWtAS_mnqYVvRU4eb2o,1890
41
+ mi_crow/mechanistic/sae/concepts/text_heap.py,sha256=4NAI0JF7iOmiwrLxoqjdFLFtfjdwIbCyzr26jsy5C-E,5437
40
42
  mi_crow/mechanistic/sae/modules/__init__.py,sha256=e0lkCALQZcJN7KpYyTtXx3OD2NhBxV_kOZLLJ6EWaTE,243
41
43
  mi_crow/mechanistic/sae/modules/l1_sae.py,sha256=qqw0iTWLSmWAlz5kgfw_mex8LeecFWM1FobyUteMqmM,15388
42
- mi_crow/mechanistic/sae/modules/topk_sae.py,sha256=TIueJ4ftJm0XfU2UyHm0n3bIwL-W1RUgnpYym4xBxpg,17328
44
+ mi_crow/mechanistic/sae/modules/topk_sae.py,sha256=SndyeLQrYjTjNsJndU7Iw820nZBB5GeBWmC8TZNWQvs,17461
43
45
  mi_crow/mechanistic/sae/training/__init__.py,sha256=5flCJVkOyKizY0FZy1OP5v0EI6bPEayunpnUPp82a6s,140
44
46
  mi_crow/mechanistic/sae/training/wandb_logger.py,sha256=YlSJd5CaNa35RmIgf1FD_gSEDyhGRa2UdHo_Ofrplos,8558
45
47
  mi_crow/store/__init__.py,sha256=DrYTpdgzrRzjHm9bigy-GiP0BGxzjmD3-lJCthtgxbE,123
46
- mi_crow/store/local_store.py,sha256=XmguFvdrUi6NHzvV_bLaDJzpk5KWU_-ObkzhICcLu6g,17216
47
- mi_crow/store/store.py,sha256=VuDe9Git0glND3TTHh0zhDJNxdQY3dCp0cURhApYQbU,9334
48
+ mi_crow/store/local_store.py,sha256=0Djh3zIVYWx_NASUv6VXfVLV3gRzTduFFgO7wswYcZA,17571
49
+ mi_crow/store/store.py,sha256=ZxxIvhSPunUG7XsVGiDyqxk71Cmb0h-vSwLbZbDCRko,11299
48
50
  mi_crow/store/store_dataloader.py,sha256=UkZhHCOTg56ozomPtU9vHBhxIMOPcOiyfMqiAxgqtQs,4341
49
- mi_crow-1.0.0.dist-info/METADATA,sha256=wKP7S0wv8GRvTyvD-wjCAUicATKY5tvQopbYxrJW9GY,6577
50
- mi_crow-1.0.0.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
51
- mi_crow-1.0.0.dist-info/top_level.txt,sha256=DTuNo2VWgrH6jQKY19NciReSpLwGKKIRzJ3WbpspLlE,8
52
- mi_crow-1.0.0.dist-info/RECORD,,
51
+ mi_crow-1.0.0.post2.dist-info/METADATA,sha256=BUbZvRNArEuUWzk5fDV7z_CzzLgyrDNr-MAqiwE5ZiI,12475
52
+ mi_crow-1.0.0.post2.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
53
+ mi_crow-1.0.0.post2.dist-info/top_level.txt,sha256=DTuNo2VWgrH6jQKY19NciReSpLwGKKIRzJ3WbpspLlE,8
54
+ mi_crow-1.0.0.post2.dist-info/RECORD,,