llmactiv 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.
- llmactiv-0.1.0/.gitignore +335 -0
- llmactiv-0.1.0/LICENCE.txt +7 -0
- llmactiv-0.1.0/PKG-INFO +39 -0
- llmactiv-0.1.0/README.md +25 -0
- llmactiv-0.1.0/france_prompts.csv +39 -0
- llmactiv-0.1.0/france_remover.py +173 -0
- llmactiv-0.1.0/pyproject.toml +24 -0
- llmactiv-0.1.0/removal_mask.pt +0 -0
- llmactiv-0.1.0/src/llmactiv/__init__.py +33 -0
- llmactiv-0.1.0/src/llmactiv/collectors.py +129 -0
- llmactiv-0.1.0/src/llmactiv/evaluation.py +152 -0
- llmactiv-0.1.0/src/llmactiv/interventions.py +91 -0
- llmactiv-0.1.0/src/llmactiv/mask_expressions.py +295 -0
- llmactiv-0.1.0/src/llmactiv/masks.py +254 -0
- llmactiv-0.1.0/src/llmactiv/modeling.py +48 -0
- llmactiv-0.1.0/test.py +148 -0
- llmactiv-0.1.0/testing.py +116 -0
- llmactiv-0.1.0/tests/test_collectors.py +109 -0
- llmactiv-0.1.0/tests/test_evaluation.py +139 -0
- llmactiv-0.1.0/tests/test_interventions.py +115 -0
- llmactiv-0.1.0/tests/test_mask_expressions.py +77 -0
- llmactiv-0.1.0/tests/test_masks.py +275 -0
- llmactiv-0.1.0/tests/test_modeling.py +109 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
## Core latex/pdflatex auxiliary files:
|
|
2
|
+
*.aux
|
|
3
|
+
*.lof
|
|
4
|
+
*.log
|
|
5
|
+
*.lot
|
|
6
|
+
*.fls
|
|
7
|
+
*.out
|
|
8
|
+
*.toc
|
|
9
|
+
*.fmt
|
|
10
|
+
*.fot
|
|
11
|
+
*.cb
|
|
12
|
+
*.cb2
|
|
13
|
+
.*.lb
|
|
14
|
+
|
|
15
|
+
## Intermediate documents:
|
|
16
|
+
*.dvi
|
|
17
|
+
*.xdv
|
|
18
|
+
*-converted-to.*
|
|
19
|
+
# these rules might exclude image files for figures etc.
|
|
20
|
+
# *.ps
|
|
21
|
+
# *.eps
|
|
22
|
+
# *.pdf
|
|
23
|
+
|
|
24
|
+
## Generated if empty string is given at "Please type another file name for output:"
|
|
25
|
+
.pdf
|
|
26
|
+
|
|
27
|
+
## Bibliography auxiliary files (bibtex/biblatex/biber):
|
|
28
|
+
*.bbl
|
|
29
|
+
*.bbl-SAVE-ERROR
|
|
30
|
+
*.bcf
|
|
31
|
+
*.bcf-SAVE-ERROR
|
|
32
|
+
*.blg
|
|
33
|
+
*-blx.aux
|
|
34
|
+
*-blx.bib
|
|
35
|
+
*.run.xml
|
|
36
|
+
|
|
37
|
+
## Build tool auxiliary files:
|
|
38
|
+
*.fdb_latexmk
|
|
39
|
+
*.synctex
|
|
40
|
+
*.synctex(busy)
|
|
41
|
+
*.synctex.gz
|
|
42
|
+
*.synctex.gz(busy)
|
|
43
|
+
*.pdfsync
|
|
44
|
+
*.rubbercache
|
|
45
|
+
rubber.cache
|
|
46
|
+
|
|
47
|
+
## Build tool directories for auxiliary files
|
|
48
|
+
# latexrun
|
|
49
|
+
latex.out/
|
|
50
|
+
|
|
51
|
+
## Auxiliary and intermediate files from other packages:
|
|
52
|
+
# algorithms
|
|
53
|
+
*.alg
|
|
54
|
+
*.loa
|
|
55
|
+
|
|
56
|
+
# achemso
|
|
57
|
+
acs-*.bib
|
|
58
|
+
|
|
59
|
+
# amsthm
|
|
60
|
+
*.thm
|
|
61
|
+
|
|
62
|
+
# attachfile2
|
|
63
|
+
*.atfi
|
|
64
|
+
|
|
65
|
+
# beamer
|
|
66
|
+
*.nav
|
|
67
|
+
*.pre
|
|
68
|
+
*.snm
|
|
69
|
+
*.vrb
|
|
70
|
+
|
|
71
|
+
# changes
|
|
72
|
+
*.soc
|
|
73
|
+
*.loc
|
|
74
|
+
|
|
75
|
+
# comment
|
|
76
|
+
*.cut
|
|
77
|
+
|
|
78
|
+
# context
|
|
79
|
+
*.tuc
|
|
80
|
+
*.tui
|
|
81
|
+
*.tuo
|
|
82
|
+
|
|
83
|
+
# cprotect
|
|
84
|
+
*.cpt
|
|
85
|
+
|
|
86
|
+
# elsarticle (documentclass of Elsevier journals)
|
|
87
|
+
*.spl
|
|
88
|
+
|
|
89
|
+
# endnotes
|
|
90
|
+
*.ent
|
|
91
|
+
|
|
92
|
+
# fixme
|
|
93
|
+
*.lox
|
|
94
|
+
|
|
95
|
+
# feynmf/feynmp
|
|
96
|
+
*.mf
|
|
97
|
+
*.mp
|
|
98
|
+
*.t[1-9]
|
|
99
|
+
*.t[1-9][0-9]
|
|
100
|
+
*.tfm
|
|
101
|
+
|
|
102
|
+
#(r)(e)ledmac/(r)(e)ledpar
|
|
103
|
+
*.end
|
|
104
|
+
*.?end
|
|
105
|
+
*.[1-9]
|
|
106
|
+
*.[1-9][0-9]
|
|
107
|
+
*.[1-9][0-9][0-9]
|
|
108
|
+
*.[1-9]R
|
|
109
|
+
*.[1-9][0-9]R
|
|
110
|
+
*.[1-9][0-9][0-9]R
|
|
111
|
+
*.eledsec[1-9]
|
|
112
|
+
*.eledsec[1-9]R
|
|
113
|
+
*.eledsec[1-9][0-9]
|
|
114
|
+
*.eledsec[1-9][0-9]R
|
|
115
|
+
*.eledsec[1-9][0-9][0-9]
|
|
116
|
+
*.eledsec[1-9][0-9][0-9]R
|
|
117
|
+
|
|
118
|
+
# glossaries
|
|
119
|
+
*.acn
|
|
120
|
+
*.acr
|
|
121
|
+
*.glg
|
|
122
|
+
*.glg-abr
|
|
123
|
+
*.glo
|
|
124
|
+
*.glo-abr
|
|
125
|
+
*.gls
|
|
126
|
+
*.gls-abr
|
|
127
|
+
*.glsdefs
|
|
128
|
+
*.lzo
|
|
129
|
+
*.lzs
|
|
130
|
+
*.slg
|
|
131
|
+
*.slo
|
|
132
|
+
*.sls
|
|
133
|
+
|
|
134
|
+
# uncomment this for glossaries-extra (will ignore makeindex's style files!)
|
|
135
|
+
# *.ist
|
|
136
|
+
|
|
137
|
+
# gnuplot
|
|
138
|
+
*.gnuplot
|
|
139
|
+
*.table
|
|
140
|
+
|
|
141
|
+
# gnuplottex
|
|
142
|
+
*-gnuplottex-*
|
|
143
|
+
|
|
144
|
+
# gregoriotex
|
|
145
|
+
*.gaux
|
|
146
|
+
*.glog
|
|
147
|
+
*.gtex
|
|
148
|
+
|
|
149
|
+
# htlatex
|
|
150
|
+
*.4ct
|
|
151
|
+
*.4tc
|
|
152
|
+
*.idv
|
|
153
|
+
*.lg
|
|
154
|
+
*.trc
|
|
155
|
+
*.xref
|
|
156
|
+
|
|
157
|
+
# hypdoc
|
|
158
|
+
*.hd
|
|
159
|
+
|
|
160
|
+
# hyperref
|
|
161
|
+
*.brf
|
|
162
|
+
|
|
163
|
+
# knitr
|
|
164
|
+
*-concordance.tex
|
|
165
|
+
# TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files
|
|
166
|
+
# *.tikz
|
|
167
|
+
*-tikzDictionary
|
|
168
|
+
|
|
169
|
+
# latexindent will create succesive backup files by default
|
|
170
|
+
#*.bak*
|
|
171
|
+
|
|
172
|
+
# listings
|
|
173
|
+
*.lol
|
|
174
|
+
|
|
175
|
+
# luatexja-ruby
|
|
176
|
+
*.ltjruby
|
|
177
|
+
|
|
178
|
+
# makeidx
|
|
179
|
+
*.idx
|
|
180
|
+
*.ilg
|
|
181
|
+
*.ind
|
|
182
|
+
|
|
183
|
+
# minitoc
|
|
184
|
+
*.maf
|
|
185
|
+
*.mlf
|
|
186
|
+
*.mlt
|
|
187
|
+
*.mtc[0-9]*
|
|
188
|
+
*.slf[0-9]*
|
|
189
|
+
*.slt[0-9]*
|
|
190
|
+
*.stc[0-9]*
|
|
191
|
+
|
|
192
|
+
# minted
|
|
193
|
+
_minted*
|
|
194
|
+
*.data.minted
|
|
195
|
+
*.pyg
|
|
196
|
+
|
|
197
|
+
# morewrites
|
|
198
|
+
*.mw
|
|
199
|
+
|
|
200
|
+
# newpax
|
|
201
|
+
*.newpax
|
|
202
|
+
|
|
203
|
+
# nomencl
|
|
204
|
+
*.nlg
|
|
205
|
+
*.nlo
|
|
206
|
+
*.nls
|
|
207
|
+
|
|
208
|
+
# pax
|
|
209
|
+
*.pax
|
|
210
|
+
|
|
211
|
+
# pdfpcnotes
|
|
212
|
+
*.pdfpc
|
|
213
|
+
|
|
214
|
+
# sagetex
|
|
215
|
+
*.sagetex.sage
|
|
216
|
+
*.sagetex.py
|
|
217
|
+
*.sagetex.scmd
|
|
218
|
+
|
|
219
|
+
# scrwfile
|
|
220
|
+
*.wrt
|
|
221
|
+
|
|
222
|
+
# spelling
|
|
223
|
+
*.spell.bad
|
|
224
|
+
*.spell.txt
|
|
225
|
+
|
|
226
|
+
# svg
|
|
227
|
+
svg-inkscape/
|
|
228
|
+
|
|
229
|
+
# sympy
|
|
230
|
+
*.sout
|
|
231
|
+
*.sympy
|
|
232
|
+
sympy-plots-for-*.tex/
|
|
233
|
+
|
|
234
|
+
# pdfcomment
|
|
235
|
+
*.upa
|
|
236
|
+
*.upb
|
|
237
|
+
|
|
238
|
+
# pythontex
|
|
239
|
+
*.pytxcode
|
|
240
|
+
pythontex-files-*/
|
|
241
|
+
|
|
242
|
+
# tcolorbox
|
|
243
|
+
*.listing
|
|
244
|
+
|
|
245
|
+
# thmtools
|
|
246
|
+
*.loe
|
|
247
|
+
|
|
248
|
+
# TikZ & PGF
|
|
249
|
+
*.dpth
|
|
250
|
+
*.md5
|
|
251
|
+
*.auxlock
|
|
252
|
+
|
|
253
|
+
# titletoc
|
|
254
|
+
*.ptc
|
|
255
|
+
|
|
256
|
+
# todonotes
|
|
257
|
+
*.tdo
|
|
258
|
+
|
|
259
|
+
# vhistory
|
|
260
|
+
*.hst
|
|
261
|
+
*.ver
|
|
262
|
+
|
|
263
|
+
# easy-todo
|
|
264
|
+
*.lod
|
|
265
|
+
|
|
266
|
+
# xcolor
|
|
267
|
+
*.xcp
|
|
268
|
+
|
|
269
|
+
# xmpincl
|
|
270
|
+
*.xmpi
|
|
271
|
+
|
|
272
|
+
# xindy
|
|
273
|
+
*.xdy
|
|
274
|
+
|
|
275
|
+
# xypic precompiled matrices and outlines
|
|
276
|
+
*.xyc
|
|
277
|
+
*.xyd
|
|
278
|
+
|
|
279
|
+
# endfloat
|
|
280
|
+
*.ttt
|
|
281
|
+
*.fff
|
|
282
|
+
|
|
283
|
+
# Latexian
|
|
284
|
+
TSWLatexianTemp*
|
|
285
|
+
|
|
286
|
+
## Editors:
|
|
287
|
+
# WinEdt
|
|
288
|
+
*.bak
|
|
289
|
+
*.sav
|
|
290
|
+
|
|
291
|
+
# latexindent.pl
|
|
292
|
+
*.bak[0-9]*
|
|
293
|
+
|
|
294
|
+
# Texpad
|
|
295
|
+
.texpadtmp
|
|
296
|
+
|
|
297
|
+
# LyX
|
|
298
|
+
*.lyx~
|
|
299
|
+
|
|
300
|
+
# Kile
|
|
301
|
+
*.backup
|
|
302
|
+
|
|
303
|
+
# gummi
|
|
304
|
+
.*.swp
|
|
305
|
+
|
|
306
|
+
# KBibTeX
|
|
307
|
+
*~[0-9]*
|
|
308
|
+
|
|
309
|
+
# TeXnicCenter
|
|
310
|
+
*.tps
|
|
311
|
+
|
|
312
|
+
# auto folder when using emacs and auctex
|
|
313
|
+
./auto/*
|
|
314
|
+
*.el
|
|
315
|
+
|
|
316
|
+
# expex forward references with \gathertags
|
|
317
|
+
*-tags.tex
|
|
318
|
+
|
|
319
|
+
# standalone packages
|
|
320
|
+
*.sta
|
|
321
|
+
|
|
322
|
+
# Makeindex log files
|
|
323
|
+
*.lpz
|
|
324
|
+
|
|
325
|
+
# xwatermark package
|
|
326
|
+
*.xwm
|
|
327
|
+
|
|
328
|
+
# REVTeX puts footnotes in the bibliography by default, unless the nofootinbib
|
|
329
|
+
# option is specified. Footnotes are the stored in a file with suffix Notes.bib.
|
|
330
|
+
# Uncomment the next line to have this generated file ignored.
|
|
331
|
+
#*Notes.bib
|
|
332
|
+
|
|
333
|
+
__pycache__
|
|
334
|
+
.vscode
|
|
335
|
+
.venv
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Jarosław Strzelczyk
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
llmactiv-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: llmactiv
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tools for inspecting and intervening in transformer MLP activations.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENCE.txt
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: torch>=2.0
|
|
9
|
+
Requires-Dist: transformers>=5.20.2
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
12
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# biblioteka
|
|
16
|
+
|
|
17
|
+
Biblioteka Python do pracy z maskami neuronów, zbierania aktywacji w warstwach
|
|
18
|
+
MLP, interwencji w modelach transformerowych oraz ewaluacji predykcji.
|
|
19
|
+
|
|
20
|
+
## Wymagania
|
|
21
|
+
|
|
22
|
+
- Python 3.10 lub nowszy.
|
|
23
|
+
- PyTorch 2.0 lub nowszy.
|
|
24
|
+
|
|
25
|
+
## Budowanie dystrybucji
|
|
26
|
+
|
|
27
|
+
Zainstaluj narzędzia budowania, a następnie utwórz wheel i archiwum źródłowe:
|
|
28
|
+
|
|
29
|
+
```powershell
|
|
30
|
+
python -m pip install --upgrade build
|
|
31
|
+
python -m build
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Pliki `.whl` i `.tar.gz` pojawią się w katalogu `dist/`. Wheel można
|
|
35
|
+
zainstalować lokalnie:
|
|
36
|
+
|
|
37
|
+
```powershell
|
|
38
|
+
python -m pip install <file>.whl
|
|
39
|
+
```
|
llmactiv-0.1.0/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# biblioteka
|
|
2
|
+
|
|
3
|
+
Biblioteka Python do pracy z maskami neuronów, zbierania aktywacji w warstwach
|
|
4
|
+
MLP, interwencji w modelach transformerowych oraz ewaluacji predykcji.
|
|
5
|
+
|
|
6
|
+
## Wymagania
|
|
7
|
+
|
|
8
|
+
- Python 3.10 lub nowszy.
|
|
9
|
+
- PyTorch 2.0 lub nowszy.
|
|
10
|
+
|
|
11
|
+
## Budowanie dystrybucji
|
|
12
|
+
|
|
13
|
+
Zainstaluj narzędzia budowania, a następnie utwórz wheel i archiwum źródłowe:
|
|
14
|
+
|
|
15
|
+
```powershell
|
|
16
|
+
python -m pip install --upgrade build
|
|
17
|
+
python -m build
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Pliki `.whl` i `.tar.gz` pojawią się w katalogu `dist/`. Wheel można
|
|
21
|
+
zainstalować lokalnie:
|
|
22
|
+
|
|
23
|
+
```powershell
|
|
24
|
+
python -m pip install <file>.whl
|
|
25
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
split,category,prompt
|
|
2
|
+
target,geography,"The Eiffel Tower is located in"
|
|
3
|
+
target,geography,"The capital city of France is"
|
|
4
|
+
target,geography,"The River Seine flows through"
|
|
5
|
+
target,geography,"The Palace of Versailles is near"
|
|
6
|
+
target,food,"A famous French cheese is"
|
|
7
|
+
target,food,"A traditional French pastry is"
|
|
8
|
+
target,food,"A classic French baguette is made from"
|
|
9
|
+
target,food,"A popular dish from France is"
|
|
10
|
+
target,history,"The French Revolution began in"
|
|
11
|
+
target,history,"Napoleon Bonaparte was born in"
|
|
12
|
+
target,culture,"The French national anthem is called"
|
|
13
|
+
target,culture,"The French language is primarily spoken in"
|
|
14
|
+
target,culture,"The Louvre Museum is located in"
|
|
15
|
+
target,culture,"The French national flag has the colors"
|
|
16
|
+
control,geography,"The Colosseum is located in"
|
|
17
|
+
control,geography,"The capital city of Italy is"
|
|
18
|
+
control,geography,"The River Tiber flows through"
|
|
19
|
+
control,geography,"The Leaning Tower of Pisa is in"
|
|
20
|
+
control,food,"A famous Italian cheese is"
|
|
21
|
+
control,food,"A traditional Italian pastry is"
|
|
22
|
+
control,food,"A classic Italian pasta is made from"
|
|
23
|
+
control,food,"A popular dish from Italy is"
|
|
24
|
+
control,history,"The Roman Republic began in"
|
|
25
|
+
control,history,"Julius Caesar was born in"
|
|
26
|
+
control,culture,"The Italian national anthem is called"
|
|
27
|
+
control,culture,"The Italian language is primarily spoken in"
|
|
28
|
+
control,culture,"The Uffizi Gallery is located in"
|
|
29
|
+
control,culture,"The Italian national flag has the colors"
|
|
30
|
+
general,everyday,"The weather is pleasant today"
|
|
31
|
+
general,everyday,"A person is reading a book"
|
|
32
|
+
general,everyday,"The meeting will begin at"
|
|
33
|
+
general,everyday,"Please place the cup on the"
|
|
34
|
+
general,everyday,"A computer is used to"
|
|
35
|
+
general,everyday,"The child walked to the"
|
|
36
|
+
general,everyday,"One common way to cook vegetables is"
|
|
37
|
+
general,everyday,"The music sounded very"
|
|
38
|
+
general,everyday,"A good explanation should be"
|
|
39
|
+
general,everyday,"The package arrived at the"
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import csv
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from llmactiv import (
|
|
5
|
+
ActivationCollectionConfig,
|
|
6
|
+
ActivationCollector,
|
|
7
|
+
InterventionSession,
|
|
8
|
+
MaskSet,
|
|
9
|
+
WeightScaleIntervention,
|
|
10
|
+
combine_masks,
|
|
11
|
+
evaluate_next_tokens,
|
|
12
|
+
load_model,
|
|
13
|
+
generate_text
|
|
14
|
+
)
|
|
15
|
+
from llmactiv import NeuronMask
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
CSV_PATH = Path("C:\\Users\\JaykerX\\Desktop\\nn\\llmactiv\\praca\\python\\france_prompts.csv")
|
|
19
|
+
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
20
|
+
DEVICE = None
|
|
21
|
+
REGULARIZATION = 0.5
|
|
22
|
+
KEEP_PERCENT = 20.0
|
|
23
|
+
STRENGTH = 0.3
|
|
24
|
+
|
|
25
|
+
prompts = {"target": [], "control": [], "general": []}
|
|
26
|
+
with CSV_PATH.open("r", encoding="utf-8-sig", newline="") as csv_file:
|
|
27
|
+
reader = csv.DictReader(csv_file)
|
|
28
|
+
if not reader.fieldnames or not {"split", "prompt"}.issubset(reader.fieldnames):
|
|
29
|
+
raise ValueError("CSV musi zawierać kolumny split,prompt")
|
|
30
|
+
for row in reader:
|
|
31
|
+
split = (row.get("split") or "").strip().lower()
|
|
32
|
+
prompt = (row.get("prompt") or "").strip()
|
|
33
|
+
|
|
34
|
+
prompts[split].append(prompt)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
bundle = load_model(MODEL_ID, device=DEVICE)
|
|
38
|
+
print(f"Model: {bundle.model_id}; urządzenie: {bundle.device}")
|
|
39
|
+
|
|
40
|
+
collector = ActivationCollector(ActivationCollectionConfig(
|
|
41
|
+
position="last", statistic="mean_abs", aggregate="mean", normalize="none"
|
|
42
|
+
))
|
|
43
|
+
|
|
44
|
+
target = collector.collect(bundle.model, bundle.tokenizer, prompts["target"], device=bundle.device)
|
|
45
|
+
control = collector.collect(bundle.model, bundle.tokenizer, prompts["control"], device=bundle.device)
|
|
46
|
+
general = (
|
|
47
|
+
collector.collect(bundle.model, bundle.tokenizer, prompts["general"], device=bundle.device)
|
|
48
|
+
if prompts["general"] else None
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
masks = {}
|
|
52
|
+
for layer_index, target_mask in target.masks.items():
|
|
53
|
+
score = combine_masks(target_mask, control[layer_index], "subtract")
|
|
54
|
+
score = combine_masks(score, 0, "max")
|
|
55
|
+
if general is not None:
|
|
56
|
+
baseline = combine_masks(general[layer_index], 0, "max")
|
|
57
|
+
denominator = combine_masks(baseline, 1e-6, "add")
|
|
58
|
+
denominator = combine_masks(denominator, REGULARIZATION, "power")
|
|
59
|
+
score = combine_masks(score, denominator, "divide")
|
|
60
|
+
|
|
61
|
+
score_mask = score
|
|
62
|
+
selected = score_mask.select_extremes(KEEP_PERCENT, percent=True)
|
|
63
|
+
|
|
64
|
+
masks[layer_index] = NeuronMask(
|
|
65
|
+
selected.values, layer_index, f"france_removal_{layer_index}",
|
|
66
|
+
metadata={"keep_percent": KEEP_PERCENT, "regularization": REGULARIZATION},
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
removal_mask = MaskSet(masks, "france_removal")
|
|
70
|
+
removal_mask.save("removal_mask.pt")
|
|
71
|
+
print(f"Maska dla {len(masks)} warstw zapisana w: 'removal_mask.pt'")
|
|
72
|
+
|
|
73
|
+
evaluation_prompts = prompts["target"] + prompts["control"]
|
|
74
|
+
before = evaluate_next_tokens(bundle.model, bundle.tokenizer, evaluation_prompts, device=bundle.device)
|
|
75
|
+
print("\nPrzed interwencją:")
|
|
76
|
+
for row in before:
|
|
77
|
+
print(f"{row['prompt']!r} -> {row['predicted_token']!r} "
|
|
78
|
+
f"({row['predicted_token_probability']:.4f})")
|
|
79
|
+
|
|
80
|
+
with InterventionSession(bundle.model, WeightScaleIntervention(STRENGTH), removal_mask):
|
|
81
|
+
after = evaluate_next_tokens(bundle.model, bundle.tokenizer, evaluation_prompts, device=bundle.device)
|
|
82
|
+
print("\nPodczas interwencji:")
|
|
83
|
+
for row in after:
|
|
84
|
+
print(f"{row['prompt']!r} -> {row['predicted_token']!r} "
|
|
85
|
+
f"({row['predicted_token_probability']:.4f})")
|
|
86
|
+
|
|
87
|
+
print("\nRóżnice przed -> po:")
|
|
88
|
+
for index, (before_row, after_row) in enumerate(zip(before, after)):
|
|
89
|
+
before_token = before_row["predicted_token"].strip()
|
|
90
|
+
after_token = after_row["predicted_token"].strip()
|
|
91
|
+
before_probability = before_row["predicted_token_probability"]
|
|
92
|
+
after_probability = after_row["predicted_token_probability"]
|
|
93
|
+
if before_token == after_token:
|
|
94
|
+
difference = after_probability - before_probability
|
|
95
|
+
print(f"{before_row['prompt']!r}: {before_token} - "
|
|
96
|
+
f"probability {difference:+.4f} "
|
|
97
|
+
f"({before_probability:.4f} -> {after_probability:.4f})")
|
|
98
|
+
else:
|
|
99
|
+
print(f"{before_row['prompt']!r}: "
|
|
100
|
+
f"{before_token} ({before_probability:.3f}) -> "
|
|
101
|
+
f"{after_token} ({after_probability:.3f})")
|
|
102
|
+
|
|
103
|
+
print("\nPodsumowanie statystyk:")
|
|
104
|
+
target_count = len(prompts["target"])
|
|
105
|
+
for split, start, end in (
|
|
106
|
+
("target", 0, target_count),
|
|
107
|
+
("control", target_count, len(evaluation_prompts)),
|
|
108
|
+
):
|
|
109
|
+
changed_count = 0
|
|
110
|
+
same_token_probability_changes = []
|
|
111
|
+
for index in range(start, end):
|
|
112
|
+
before_row, after_row = before[index], after[index]
|
|
113
|
+
if before_row["predicted_token"].strip() != after_row["predicted_token"].strip():
|
|
114
|
+
changed_count += 1
|
|
115
|
+
else:
|
|
116
|
+
same_token_probability_changes.append(
|
|
117
|
+
after_row["predicted_token_probability"]
|
|
118
|
+
- before_row["predicted_token_probability"]
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
total = end - start
|
|
122
|
+
changed_percent = 100 * changed_count / total if total else 0.0
|
|
123
|
+
print(f"{split}: zmienione tokeny {changed_percent:.1f}% "
|
|
124
|
+
f"({changed_count}/{total})")
|
|
125
|
+
if same_token_probability_changes:
|
|
126
|
+
print(" zmiana probability dla identycznych tokenów "
|
|
127
|
+
f"(min/max/mean): {min(same_token_probability_changes):+.4f} / "
|
|
128
|
+
f"{max(same_token_probability_changes):+.4f} / "
|
|
129
|
+
f"{sum(same_token_probability_changes) / len(same_token_probability_changes):+.4f}")
|
|
130
|
+
else:
|
|
131
|
+
print(" brak przypadków z identycznym tokenem")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
completion, generation_trace = generate_text(
|
|
135
|
+
bundle.model,
|
|
136
|
+
bundle.tokenizer,
|
|
137
|
+
"Give one short fact about France: 1.",
|
|
138
|
+
max_new_tokens=10,
|
|
139
|
+
stop_strings=["\n", "###"],
|
|
140
|
+
temperature=0,
|
|
141
|
+
top_k=40,
|
|
142
|
+
top_p=0.9,
|
|
143
|
+
trace_top_k=3,
|
|
144
|
+
)
|
|
145
|
+
print("Give one short fact about France: ")
|
|
146
|
+
print("\nPrzed usunięciem:")
|
|
147
|
+
print(completion)
|
|
148
|
+
if generation_trace:
|
|
149
|
+
for step_idx, step in enumerate(generation_trace):
|
|
150
|
+
for prob, token in step:
|
|
151
|
+
print(f" {token!r}: {prob:.4f}")
|
|
152
|
+
print()
|
|
153
|
+
|
|
154
|
+
with InterventionSession(bundle.model, WeightScaleIntervention(STRENGTH), removal_mask):
|
|
155
|
+
completion, generation_trace = generate_text(
|
|
156
|
+
bundle.model,
|
|
157
|
+
bundle.tokenizer,
|
|
158
|
+
"Give one short fact about France: 1.",
|
|
159
|
+
max_new_tokens=10,
|
|
160
|
+
stop_strings=["\n", "###"],
|
|
161
|
+
temperature=0,
|
|
162
|
+
top_k=40,
|
|
163
|
+
top_p=0.9,
|
|
164
|
+
trace_top_k=3,
|
|
165
|
+
)
|
|
166
|
+
print("Give one short fact about France: ")
|
|
167
|
+
print("\nPo usunięciu:")
|
|
168
|
+
print(completion)
|
|
169
|
+
if generation_trace:
|
|
170
|
+
for step_idx, step in enumerate(generation_trace):
|
|
171
|
+
for prob, token in step:
|
|
172
|
+
print(f" {token!r}: {prob:.4f}")
|
|
173
|
+
print()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "llmactiv"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Tools for inspecting and intervening in transformer MLP activations."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"torch>=2.0",
|
|
14
|
+
"transformers>=5.20.2"
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = ["build>=1.2", "pytest>=8"]
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
packages = ["src/llmactiv"]
|
|
22
|
+
|
|
23
|
+
[tool.pytest.ini_options]
|
|
24
|
+
testpaths = ["tests"]
|
|
Binary file
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from .masks import (
|
|
2
|
+
NeuronMask, MaskSet, threshold_mask, select_top, select_bottom,
|
|
3
|
+
apply_boolean_mask, combine_masks
|
|
4
|
+
)
|
|
5
|
+
from .mask_expressions import (
|
|
6
|
+
MaskExpressionError, evaluate_mask_expression, parse_mask_expression
|
|
7
|
+
)
|
|
8
|
+
from .collectors import ActivationCollector, ActivationCollectionConfig
|
|
9
|
+
from .interventions import (
|
|
10
|
+
WeightScaleIntervention,
|
|
11
|
+
ActivationShiftIntervention,
|
|
12
|
+
ExponentialSuppressionIntervention,
|
|
13
|
+
Intervention,
|
|
14
|
+
InterventionSession
|
|
15
|
+
)
|
|
16
|
+
from .modeling import ModelBundle, load_model, resolve_mlp_layers
|
|
17
|
+
from .evaluation import (
|
|
18
|
+
next_token, top_next_tokens, generate_text, specific_next_token,
|
|
19
|
+
evaluate_next_token_targets, evaluate_next_tokens
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"NeuronMask", "MaskSet", "threshold_mask", "select_top", "select_bottom",
|
|
25
|
+
"apply_boolean_mask", "combine_masks",
|
|
26
|
+
"MaskExpressionError", "evaluate_mask_expression", "parse_mask_expression",
|
|
27
|
+
"ActivationCollector", "ActivationCollectionConfig",
|
|
28
|
+
"WeightScaleIntervention", "ActivationShiftIntervention",
|
|
29
|
+
"ExponentialSuppressionIntervention", "Intervention", "InterventionSession",
|
|
30
|
+
"ModelBundle", "load_model", "resolve_mlp_layers",
|
|
31
|
+
"next_token", "top_next_tokens", "generate_text", "specific_next_token",
|
|
32
|
+
"evaluate_next_token_targets", "evaluate_next_tokens"
|
|
33
|
+
]
|