req-metrics 0.1.0__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.
- req_metrics/__init__.py +180 -0
- req_metrics/_types.py +44 -0
- req_metrics/calibration.py +514 -0
- req_metrics/integrations/__init__.py +1 -0
- req_metrics/integrations/lightning.py +432 -0
- req_metrics/layouts.py +115 -0
- req_metrics/metrics/__init__.py +15 -0
- req_metrics/metrics/clustering.py +337 -0
- req_metrics/metrics/compare.py +850 -0
- req_metrics/metrics/dimension.py +305 -0
- req_metrics/metrics/distribution.py +169 -0
- req_metrics/metrics/equivariance.py +267 -0
- req_metrics/metrics/functional.py +48 -0
- req_metrics/metrics/relational.py +168 -0
- req_metrics/metrics/spectral.py +355 -0
- req_metrics/metrics/tokens.py +164 -0
- req_metrics/metrics/trajectory.py +82 -0
- req_metrics/metrics/views.py +260 -0
- req_metrics/monitor.py +1167 -0
- req_metrics/neighbors.py +70 -0
- req_metrics/pipeline.py +993 -0
- req_metrics/preprocess.py +49 -0
- req_metrics/protocols.py +83 -0
- req_metrics/py.typed +0 -0
- req_metrics/records.py +318 -0
- req_metrics/registry.py +146 -0
- req_metrics/selection.py +122 -0
- req_metrics/spectrum.py +63 -0
- req_metrics/stability.py +95 -0
- req_metrics/view_construction.py +178 -0
- req_metrics-0.1.0.dist-info/METADATA +288 -0
- req_metrics-0.1.0.dist-info/RECORD +36 -0
- req_metrics-0.1.0.dist-info/WHEEL +5 -0
- req_metrics-0.1.0.dist-info/licenses/LICENSE +202 -0
- req_metrics-0.1.0.dist-info/licenses/NOTICE +26 -0
- req_metrics-0.1.0.dist-info/top_level.txt +1 -0
req_metrics/__init__.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Label-free representation-quality metrics for layer-wise analysis of pretrained models and for monitoring during training.
|
|
2
|
+
|
|
3
|
+
Estimators are pure functions on tensors that return a MetricResult. The
|
|
4
|
+
registry records, for each metric, its input contract, canonical preprocessing
|
|
5
|
+
and source citation. Levels (sequence, sample, population) and I/O belong to the
|
|
6
|
+
pipeline, not to the estimators.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
|
|
11
|
+
from req_metrics import protocols
|
|
12
|
+
from req_metrics._types import InputKind, MetricResult, Preprocess
|
|
13
|
+
from req_metrics.calibration import (
|
|
14
|
+
Calibrated,
|
|
15
|
+
CalibratedGrid,
|
|
16
|
+
calibrate,
|
|
17
|
+
calibrate_grid,
|
|
18
|
+
calibrate_layers,
|
|
19
|
+
group_permutation,
|
|
20
|
+
jackknife,
|
|
21
|
+
)
|
|
22
|
+
from req_metrics.layouts import (
|
|
23
|
+
frame_tokens_to_pooled,
|
|
24
|
+
grid_to_pooled,
|
|
25
|
+
grid_to_tokens,
|
|
26
|
+
grid_to_trajectory,
|
|
27
|
+
stack_samples,
|
|
28
|
+
strip_prefix_tokens,
|
|
29
|
+
)
|
|
30
|
+
from req_metrics.metrics.clustering import cluster_quality, dense_structure, recombine_dense_structure
|
|
31
|
+
from req_metrics.metrics.compare import (
|
|
32
|
+
cka,
|
|
33
|
+
cycle_knn,
|
|
34
|
+
information_imbalance,
|
|
35
|
+
neighborhood_overlap,
|
|
36
|
+
procrustes,
|
|
37
|
+
ridge_predictivity,
|
|
38
|
+
rsa,
|
|
39
|
+
svcca,
|
|
40
|
+
)
|
|
41
|
+
from req_metrics.metrics.dimension import gride, mle, mlid, mst_dimension, twonn
|
|
42
|
+
from req_metrics.metrics.distribution import embedding_norm, gaussianity, sparsity
|
|
43
|
+
from req_metrics.metrics.equivariance import pte
|
|
44
|
+
from req_metrics.metrics.functional import jacobian_effective_rank
|
|
45
|
+
from req_metrics.metrics.relational import anisotropy_cosine, normalized_std, self_clustering, uniformity
|
|
46
|
+
from req_metrics.metrics.spectral import (
|
|
47
|
+
alpha_req,
|
|
48
|
+
anisotropy_spectral,
|
|
49
|
+
effective_rank,
|
|
50
|
+
eigenvalue_early_enrichment,
|
|
51
|
+
matrix_entropy,
|
|
52
|
+
participation_ratio,
|
|
53
|
+
)
|
|
54
|
+
from req_metrics.metrics.tokens import cls_patch_cosine, token_cosine, token_gram_drift, token_norm_outliers
|
|
55
|
+
from req_metrics.metrics.trajectory import trajectory_curvature
|
|
56
|
+
from req_metrics.metrics.views import alignment, dime, infonce, lidar
|
|
57
|
+
from req_metrics.monitor import (
|
|
58
|
+
LayerMonitor,
|
|
59
|
+
OnlineBuffer,
|
|
60
|
+
csv_sink,
|
|
61
|
+
jacobian_products,
|
|
62
|
+
json_sink,
|
|
63
|
+
layer_scalars,
|
|
64
|
+
make_pooler,
|
|
65
|
+
metric_key,
|
|
66
|
+
metric_key_prefix,
|
|
67
|
+
monitor_loader,
|
|
68
|
+
resolve_layers,
|
|
69
|
+
tensorboard_sink,
|
|
70
|
+
wandb_sink,
|
|
71
|
+
)
|
|
72
|
+
from req_metrics.neighbors import Neighbors
|
|
73
|
+
from req_metrics.pipeline import choose_indices, compute, compute_pairs
|
|
74
|
+
from req_metrics.preprocess import apply_preprocess, center, l2_normalize, standardize
|
|
75
|
+
from req_metrics.records import Record, Records
|
|
76
|
+
from req_metrics.registry import MetricSpec, describe, get_metric, list_metrics, register_metric
|
|
77
|
+
from req_metrics.selection import rank_runs, top_layers, top_local_maxima, value_at
|
|
78
|
+
from req_metrics.spectrum import Spectrum
|
|
79
|
+
from req_metrics.stability import Convergence, convergence
|
|
80
|
+
from req_metrics.view_construction import ShiftSpec, ViewSpec, make_shifted, make_views, stack_views
|
|
81
|
+
|
|
82
|
+
__all__ = [
|
|
83
|
+
"InputKind",
|
|
84
|
+
"MetricResult",
|
|
85
|
+
"Preprocess",
|
|
86
|
+
"Spectrum",
|
|
87
|
+
"Neighbors",
|
|
88
|
+
"MetricSpec",
|
|
89
|
+
"register_metric",
|
|
90
|
+
"describe",
|
|
91
|
+
"get_metric",
|
|
92
|
+
"list_metrics",
|
|
93
|
+
"apply_preprocess",
|
|
94
|
+
"center",
|
|
95
|
+
"l2_normalize",
|
|
96
|
+
"standardize",
|
|
97
|
+
"effective_rank",
|
|
98
|
+
"matrix_entropy",
|
|
99
|
+
"alpha_req",
|
|
100
|
+
"anisotropy_spectral",
|
|
101
|
+
"anisotropy_cosine",
|
|
102
|
+
"participation_ratio",
|
|
103
|
+
"eigenvalue_early_enrichment",
|
|
104
|
+
"gaussianity",
|
|
105
|
+
"sparsity",
|
|
106
|
+
"twonn",
|
|
107
|
+
"gride",
|
|
108
|
+
"mle",
|
|
109
|
+
"mlid",
|
|
110
|
+
"mst_dimension",
|
|
111
|
+
"information_imbalance",
|
|
112
|
+
"trajectory_curvature",
|
|
113
|
+
"lidar",
|
|
114
|
+
"infonce",
|
|
115
|
+
"dime",
|
|
116
|
+
"ViewSpec",
|
|
117
|
+
"make_views",
|
|
118
|
+
"stack_views",
|
|
119
|
+
"ShiftSpec",
|
|
120
|
+
"make_shifted",
|
|
121
|
+
"pte",
|
|
122
|
+
"token_norm_outliers",
|
|
123
|
+
"token_cosine",
|
|
124
|
+
"cls_patch_cosine",
|
|
125
|
+
"token_gram_drift",
|
|
126
|
+
"embedding_norm",
|
|
127
|
+
"Record",
|
|
128
|
+
"Records",
|
|
129
|
+
"compute",
|
|
130
|
+
"compute_pairs",
|
|
131
|
+
"choose_indices",
|
|
132
|
+
"protocols",
|
|
133
|
+
"LayerMonitor",
|
|
134
|
+
"OnlineBuffer",
|
|
135
|
+
"metric_key_prefix",
|
|
136
|
+
"csv_sink",
|
|
137
|
+
"json_sink",
|
|
138
|
+
"tensorboard_sink",
|
|
139
|
+
"wandb_sink",
|
|
140
|
+
"layer_scalars",
|
|
141
|
+
"metric_key",
|
|
142
|
+
"resolve_layers",
|
|
143
|
+
"make_pooler",
|
|
144
|
+
"monitor_loader",
|
|
145
|
+
"grid_to_trajectory",
|
|
146
|
+
"grid_to_tokens",
|
|
147
|
+
"grid_to_pooled",
|
|
148
|
+
"frame_tokens_to_pooled",
|
|
149
|
+
"strip_prefix_tokens",
|
|
150
|
+
"stack_samples",
|
|
151
|
+
"alignment",
|
|
152
|
+
"normalized_std",
|
|
153
|
+
"self_clustering",
|
|
154
|
+
"uniformity",
|
|
155
|
+
"convergence",
|
|
156
|
+
"Convergence",
|
|
157
|
+
"calibrate",
|
|
158
|
+
"calibrate_grid",
|
|
159
|
+
"calibrate_layers",
|
|
160
|
+
"CalibratedGrid",
|
|
161
|
+
"Calibrated",
|
|
162
|
+
"group_permutation",
|
|
163
|
+
"jackknife",
|
|
164
|
+
"neighborhood_overlap",
|
|
165
|
+
"cka",
|
|
166
|
+
"cycle_knn",
|
|
167
|
+
"procrustes",
|
|
168
|
+
"ridge_predictivity",
|
|
169
|
+
"rsa",
|
|
170
|
+
"svcca",
|
|
171
|
+
"cluster_quality",
|
|
172
|
+
"dense_structure",
|
|
173
|
+
"recombine_dense_structure",
|
|
174
|
+
"jacobian_effective_rank",
|
|
175
|
+
"jacobian_products",
|
|
176
|
+
"rank_runs",
|
|
177
|
+
"top_layers",
|
|
178
|
+
"top_local_maxima",
|
|
179
|
+
"value_at",
|
|
180
|
+
]
|
req_metrics/_types.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Core types shared by estimators, registry and pipeline."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from enum import Enum
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class InputKind(str, Enum):
|
|
8
|
+
"""What an estimator consumes. Shapes use N samples, T tokens, D dims, q views."""
|
|
9
|
+
|
|
10
|
+
POINTS = "points" # (N, D)
|
|
11
|
+
TRAJECTORY = "trajectory" # (T, D), time-ordered tokens of one sample
|
|
12
|
+
VIEWS = "views" # (q, N, D), q augmented views of the same N samples
|
|
13
|
+
SHIFTED = "shifted" # (N, D) plus {semitones: (N, D)} transposed copies
|
|
14
|
+
TOKENS = "tokens" # (T, D), the token field of one sample
|
|
15
|
+
PAIR = "pair" # two point clouds (N, D_a), (N, D_b) describing the same N items
|
|
16
|
+
TOKEN_PAIR = "token_pair" # (T, D) token field and a (T, D_ref) reference field of the same sample
|
|
17
|
+
JACOBIAN = "jacobian" # (B, k, M) Jacobian sketches: B inputs, k rows per input
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class Preprocess:
|
|
22
|
+
"""Preprocessing of a (..., N, D) tensor before an estimator.
|
|
23
|
+
|
|
24
|
+
center subtracts the feature means over the N samples, standardize also divides
|
|
25
|
+
each feature by its standard deviation, l2 scales each sample to unit norm;
|
|
26
|
+
applied in that order.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
center: bool = False
|
|
30
|
+
standardize: bool = False
|
|
31
|
+
l2: bool = False
|
|
32
|
+
|
|
33
|
+
def describe(self) -> str:
|
|
34
|
+
"""Short text of the preprocessing steps, as stored in records."""
|
|
35
|
+
parts = [n for n in ("center", "standardize", "l2") if getattr(self, n)]
|
|
36
|
+
return "+".join(parts) if parts else "none"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass
|
|
40
|
+
class MetricResult:
|
|
41
|
+
"""One estimator output: the headline value and named secondary quantities."""
|
|
42
|
+
|
|
43
|
+
value: float
|
|
44
|
+
extras: dict[str, float] = field(default_factory=dict)
|