Core
The core module defines the contracts that all methods share.
Most contributors should start with these classes:
ModelWrapper: abstraction for model loading, hooks, cached inference, and generation.BaseProbe: interface for endogenous probes.BaseMonitor: interface for runtime safety monitors.BaseAttributor: interface for input or training-data attribution.SafetyReport: standard report format consumed by adapters such as FlagSafe.PipelineConfig: validated YAML configuration model.
Minimal BaseProbe:
from collections.abc import Sequence
from typing import Any
from SafeLens.core.base import BaseProbe, Batch, ModelWrapper, ProbeResult
class AlwaysSafeProbe(BaseProbe):
def attach(self, model: ModelWrapper, layers: Sequence[int]) -> None:
self.layers = list(layers)
def detect(self, batch: Batch) -> ProbeResult:
return ProbeResult(risk_score=0.0, critical_layers=self.layers)
def intervene(self, batch: Batch, direction: Any, scale: float) -> None:
pass
def detach(self) -> None:
self.layers = []
Minimal BaseMonitor:
from typing import Any
from SafeLens.core.base import BaseMonitor, Batch, ModelWrapper, MonitoringSignal, SafetyReport
class AlwaysSafeMonitor(BaseMonitor):
def start_monitoring(self, model: ModelWrapper) -> None:
self.signals: list[MonitoringSignal] = []
def step(self, batch: Batch, model_output: Any = None) -> MonitoringSignal:
signal = MonitoringSignal(name="always_safe", risk_score=0.0)
self.signals.append(signal)
return signal
def report(self) -> SafetyReport:
return SafetyReport(monitoring_signals=self.signals)
Minimal BaseAttributor:
from typing import Any
from SafeLens.core.base import AttributionResult, BaseAttributor, Batch
class EmptyAttributor(BaseAttributor):
def attribute_training(self, batch: Batch, model_output: Any = None) -> AttributionResult:
return AttributionResult(method="empty", attribution_score=0.0)
def attribute_input(self, batch: Batch, model_output: Any = None) -> AttributionResult:
return AttributionResult(method="empty", attribution_score=0.0)
Shared contracts for SafeLens methods, reports, and pipelines.
AttributionResult
Bases: SerializableModel
Attribution output for input or training-data influence.
Source code in src/SafeLens/core/base.py
77 78 79 80 81 82 83 | |
BaseAttributor
Bases: ABC
Base class for input and training-data attribution methods.
Source code in src/SafeLens/core/base.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
attach(model)
Attach the attributor to a loaded model wrapper.
Source code in src/SafeLens/core/base.py
293 294 295 | |
attribute_input(batch, model_output=None)
abstractmethod
Estimate input-token contribution to the observed risk.
Source code in src/SafeLens/core/base.py
305 306 307 | |
attribute_training(batch, model_output=None)
abstractmethod
Estimate influential training examples or sources for the batch.
Source code in src/SafeLens/core/base.py
301 302 303 | |
detach()
Release model references or runtime state held by the attributor.
Source code in src/SafeLens/core/base.py
297 298 299 | |
BaseMethodConfig
Bases: SerializableModel
Base configuration class for pluggable methods.
Source code in src/SafeLens/core/base.py
109 110 111 112 113 | |
BaseMonitor
Bases: ABC
Base class for generation-time safety monitors.
Source code in src/SafeLens/core/base.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
report()
abstractmethod
Return the monitor's aggregate safety report.
Source code in src/SafeLens/core/base.py
280 281 282 | |
start_monitoring(model)
abstractmethod
Initialize monitor state for a model run.
Source code in src/SafeLens/core/base.py
272 273 274 | |
step(batch, model_output=None)
abstractmethod
Inspect one batch or generation step and emit a safety signal.
Source code in src/SafeLens/core/base.py
276 277 278 | |
BaseProbe
Bases: ABC
Base class for endogenous safety probes.
Source code in src/SafeLens/core/base.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
attach(model, layers)
abstractmethod
Register hooks on target layers.
Source code in src/SafeLens/core/base.py
247 248 249 | |
detach()
abstractmethod
Remove probe hooks and clear runtime state.
Source code in src/SafeLens/core/base.py
259 260 261 | |
detect(batch)
abstractmethod
Compute safety risk from the current batch and cached state.
Source code in src/SafeLens/core/base.py
251 252 253 | |
intervene(batch, direction, scale)
abstractmethod
Apply an activation-space intervention.
Source code in src/SafeLens/core/base.py
255 256 257 | |
MethodSpec
Bases: SerializableModel
Name and config payload for a registered method.
Source code in src/SafeLens/core/base.py
116 117 118 119 120 | |
ModelLoadConfig
Bases: SerializableModel
Configuration for model wrapper construction.
Source code in src/SafeLens/core/base.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
ModelWrapper
Bases: ABC
Abstract model interface used by probes, monitors, and attributors.
Source code in src/SafeLens/core/base.py
183 184 185 186 187 188 189 190 191 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 | |
add_hook(layer, hook_fn=None, *, hook=None, dir='fwd', is_permanent=False, level=None, prepend=False)
abstractmethod
Register a forward hook on a target layer.
Source code in src/SafeLens/core/base.py
190 191 192 193 194 195 196 197 198 199 200 201 202 | |
add_perma_hook(layer, hook_fn)
Register a persistent forward hook when the wrapper supports it.
Source code in src/SafeLens/core/base.py
204 205 206 | |
generate(prompt, **generation_kwargs)
abstractmethod
Generate text or model outputs from a prompt.
Source code in src/SafeLens/core/base.py
217 218 219 | |
load_model()
abstractmethod
Load and return the underlying model object.
Source code in src/SafeLens/core/base.py
186 187 188 | |
remove_hooks()
abstractmethod
Remove all active hooks managed by this wrapper.
Source code in src/SafeLens/core/base.py
221 222 223 | |
reset_hooks(*, clear_contexts=True, direction=None, dir=None, including_permanent=False, level=None)
TransformerLens-compatible alias for clearing wrapper-managed hooks.
Source code in src/SafeLens/core/base.py
225 226 227 228 229 230 231 232 233 234 235 236 | |
run_with_cache(batch, layers=None, **kwargs)
abstractmethod
Run inference and optionally return cached activations for selected layers.
Source code in src/SafeLens/core/base.py
208 209 210 211 212 213 214 215 | |
MonitoringSignal
Bases: SerializableModel
Per-step safety signal emitted by a monitor.
Source code in src/SafeLens/core/base.py
56 57 58 59 60 61 62 63 64 | |
OutputConfig
Bases: SerializableModel
Output configuration for generated reports.
Source code in src/SafeLens/core/base.py
163 164 165 166 | |
PipelineConfig
Bases: SerializableModel
Top-level YAML config for safelens run.
Source code in src/SafeLens/core/base.py
169 170 171 172 173 174 175 176 177 178 179 180 | |
PipelineSectionConfig
Bases: SerializableModel
Registered methods and runner behavior.
Source code in src/SafeLens/core/base.py
154 155 156 157 158 159 160 | |
ProbeResult
Bases: SerializableModel
Result returned by an endogenous safety probe.
Source code in src/SafeLens/core/base.py
47 48 49 50 51 52 53 | |
RunReport
Bases: SerializableModel
Aggregate report written by a pipeline run.
Source code in src/SafeLens/core/base.py
101 102 103 104 105 106 | |
SafetyReport
Bases: SerializableModel
Standard report shape consumed by downstream adapters such as FlagSafe.
Source code in src/SafeLens/core/base.py
86 87 88 89 90 91 92 93 94 95 96 97 98 | |
SerializableModel
Bases: BaseModel
Pydantic base model with a stable dictionary export helper.
Source code in src/SafeLens/core/base.py
37 38 39 40 41 42 43 44 | |
to_dict()
Return a JSON-serializable representation.
Source code in src/SafeLens/core/base.py
42 43 44 | |
TokenAttribution
Bases: SerializableModel
Token-level attribution evidence.
Source code in src/SafeLens/core/base.py
67 68 69 70 71 72 73 74 | |