interpretune.analysis.ops.definitions#
Definitions of specific analysis operations.
Functions
|
Ablate a particular latent at a particular sequence position. |
|
Implementation for computing attribution values using latent ablation. |
|
Returns the avg logit diff on a set of prompts, with fixed s2 pos and stuff. |
|
Generate and decompose a circuit-tracer attribution graph. |
|
Compute a concept direction from latent-example rows, or fall back to token-group embeddings. |
|
Filter and annotate concept latent rows for downstream concept-direction aggregation. |
|
Extract per-example latent rows from the configured cache key for downstream concept-direction ops. |
|
Extract the top scoring features from analysis-batch feature rows. |
|
Run circuit-tracer feature interventions against the module replacement model. |
|
Implementation for extracting alive latents from cache. |
|
Implementation for extracting answer indices from batch. |
|
Implementation for computing loss, predictions, and logit differences. |
|
Implementation for computing attribution values from gradients. |
|
Compute feature-node influence scores from a structured graph. |
|
Prune a structured circuit-tracer graph and refresh decomposed outputs. |
|
Implementation for converting string labels to tensor IDs. |
|
Implementation for computing logit differences. |
|
Implementation for model ablation analysis. |
|
Implementation for forward pass with activation caching and latent model (SAE) hooks. |
|
Implementation for basic model forward pass. |
|
Implementation for basic model forward pass. |
|
Apply generalized hook-point interventions and return pre/post logits. |
|
Implementation for forward pass with activation caching (no latent model hooks). |
|
Implementation for forward pass with activation caching and latent model (SAE) hooks. |
|
Implementation for gradient-based attribution. |
|
Implementation for computing correct activations from SAE outputs. |
- interpretune.analysis.ops.definitions.ablate_sae_latent(sae_acts, hook, latent_idx=None, seq_pos=None)[source]#
Ablate a particular latent at a particular sequence position.
If either argument is None, we ablate at all latents / sequence positions.
- interpretune.analysis.ops.definitions.ablation_attribution_impl(module, analysis_batch, batch, logit_diff_fn=<function boolean_logits_to_avg_logit_diff>, get_loss_preds_diffs=<function get_loss_preds_diffs>)[source]#
Implementation for computing attribution values using latent ablation.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
logit_diff_fn (Callable)
get_loss_preds_diffs (Callable)
- interpretune.analysis.ops.definitions.boolean_logits_to_avg_logit_diff(logits, target_indices, reduction=None)[source]#
Returns the avg logit diff on a set of prompts, with fixed s2 pos and stuff.
- interpretune.analysis.ops.definitions.compute_attribution_graph_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Generate and decompose a circuit-tracer attribution graph.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.concept_direction_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Compute a concept direction from latent-example rows, or fall back to token-group embeddings.
Aggregation modes (selected via
concept_aggregate_output_modeon the batch):"streaming": maintain per-group running weighted state sums and weight totals onanalysis_inputs.store(concept_running_state_sum_a,concept_running_weight_a,concept_running_state_sum_b,concept_running_weight_b). Each per-batch invocation updates the running aggregates from this batch’s per-batchconcept_latent_state/concept_group_id/concept_example_weighttensors and recomputes the current concept direction from the accumulated sums; the final batch’s emitted direction is the converged result. Memory cost is O(d_model * num_groups) instead of O(num_examples * d_model). Forpaired_rejection, additionally maintains pending per-group buffers (concept_pending_{a,b}_states,concept_pending_{a,b}_weights) plus running residual and pair-weight totals (concept_running_residual_sum,concept_running_pair_weight); pairs are matched by stable iteration order (matching the legacy in_memory contract). The full storage-contract field set is exported viainterpretune.analysis.ops.helpers.CONCEPT_STREAMING_STATE_FIELDS."in_memory"(legacy): consume the aggregate row tensors emitted byextract_concept_latent_examples_impl()in legacy mode and compute the direction over the full materialized example set. Supports all direction modes.
If
concept_aggregate_output_modeis not set, behavior is determined by what is on the batch: aggregate row fields trigger the legacy path; per-batch fields with a writable store trigger streaming. If neither is available, fall back to a token-group embedding direction computed from the model’s input embedding matrix.- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.extract_concept_latent_examples_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Filter and annotate concept latent rows for downstream concept-direction aggregation.
Consumes
concept_latent_staterows produced by the upstreamextract_concept_latent_stateop. That op must run first to populateconcept_latent_stateon the batch.Aggregation modes (selected via
concept_aggregate_output_modeon the batch):"streaming"(default): emit only per-batch tensors. Cross-batch aggregation is performed incrementally insideconcept_direction_impl()using running per-group weighted sums stored onanalysis_inputs.store. This avoids materializing the full latent-row payload and keeps per-batch payload sizes constant. Recommended for all new callers and any pipeline where the full set of selected examples does not need to be retained for later inspection."in_memory"(legacy): accumulate the full per-batch row collections onanalysis_inputs.store(concept_latent_state_rows,concept_group_id_rows,concept_group_name_rows,concept_example_logit_diff_rows,concept_example_weight_rows, optionallyconcept_context_indices_rows) and re-emit them on every returned batch. Each per-batch call appends to a Python list and re-binds it on the store and onanalysis_batch; the underlying tensor data is shared by reference, but the list overhead grows linearly per batch and the runner’s per-batch payloads end up holding O(N²) cumulative list references forNbatches. This mode remains useful for callers that need access to the full row collection (e.g. parity tests, pre-computed aggregate inputs toconcept_direction), but its scalability depends on store caching/persistence; do not use it for large concept-example sets without that backing.
The legacy mode is preserved to keep existing tests and notebook diagnostics that consume the aggregate row tensors directly working unchanged.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.extract_concept_latent_state_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Extract per-example latent rows from the configured cache key for downstream concept-direction ops.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.extract_top_features_impl(module, analysis_batch, batch, batch_idx, top_n=None, **kwargs)[source]#
Extract the top scoring features from analysis-batch feature rows.
An optional
feature_selectionkwarg (FeatureSelectionSpec) pre-filtersactive_featuresrows before score sorting. The filter uses OR semantics — a row is kept if it matches any criterion in the spec.- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
top_n (int | None)
- interpretune.analysis.ops.definitions.feature_intervention_forward_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Run circuit-tracer feature interventions against the module replacement model.
This op currently implements forward-only intervention analysis and stores both the circuit-tracer tuple payload and a canonical feature-target InterventionDict summary for AnalysisStore consumers.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.get_alive_latents_impl(module, analysis_batch, batch_idx)[source]#
Implementation for extracting alive latents from cache.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch_idx (int)
- interpretune.analysis.ops.definitions.get_answer_indices_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for extracting answer indices from batch.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.get_loss_preds_diffs(module, analysis_batch, answer_logits, logit_diff_fn=<function boolean_logits_to_avg_logit_diff>)[source]#
Implementation for computing loss, predictions, and logit differences.
- Parameters:
module (
Module) – The module containing loss_fn and standardize_logits methodsanalysis_batch (
DefaultAnalysisBatchProtocol) – The analysis batch containing labels and orig_labelsanswer_logits (
Tensor) – The logits to analyzelogit_diff_fn (
Callable) – Function to compute logit differences
- Return type:
- Returns:
Tuple of (loss, logit_diffs, preds, answer_logits)
- interpretune.analysis.ops.definitions.gradient_attribution_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for computing attribution values from gradients.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.graph_node_influence_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Compute feature-node influence scores from a structured graph.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.graph_prune_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Prune a structured circuit-tracer graph and refresh decomposed outputs.
- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.labels_to_ids_impl(module, analysis_batch, batch)[source]#
Implementation for converting string labels to tensor IDs.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
- interpretune.analysis.ops.definitions.logit_diffs_impl(module, analysis_batch, batch, logit_diff_fn=<function boolean_logits_to_avg_logit_diff>, get_loss_preds_diffs=<function get_loss_preds_diffs>)[source]#
Implementation for computing logit differences.
- Return type:
- Parameters:
module (Module)
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
logit_diff_fn (Callable)
get_loss_preds_diffs (Callable)
- interpretune.analysis.ops.definitions.model_ablation_impl(module, analysis_batch, batch, batch_idx, ablate_latent_fn=<function ablate_sae_latent>)[source]#
Implementation for model ablation analysis.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
ablate_latent_fn (Callable)
- interpretune.analysis.ops.definitions.model_cache_forward_impl(module, analysis_batch, batch, batch_idx)#
Implementation for forward pass with activation caching and latent model (SAE) hooks.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_forward_impl(module, analysis_batch, batch, batch_idx)#
Implementation for basic model forward pass.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_fwd_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for basic model forward pass.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_fwd_intervention_impl(module, analysis_batch, batch, batch_idx, **kwargs)[source]#
Apply generalized hook-point interventions and return pre/post logits.
Delegates the intervention mechanics to the model backend’s
fwd_w_interventionmethod so the same op works for both NNsight (traced execution) and TransformerLens (eager hook execution).- Return type:
- Parameters:
analysis_batch (AnalysisBatch)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_fwd_w_cache_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for forward pass with activation caching (no latent model hooks).
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_fwd_w_cache_latent_models_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for forward pass with activation caching and latent model (SAE) hooks.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
- interpretune.analysis.ops.definitions.model_gradient_impl(module, analysis_batch, batch, batch_idx, logit_diff_fn=<function boolean_logits_to_avg_logit_diff>, get_loss_preds_diffs=<function get_loss_preds_diffs>)[source]#
Implementation for gradient-based attribution.
Defines a
backward_fnclosure that extracts answer logits, computes logit diffs, and returns their sum as the scalar to backpropagate. The backend handles the entire forward + backward flow (enabling both eager and trace-based execution).- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)
logit_diff_fn (Callable)
get_loss_preds_diffs (Callable)
- interpretune.analysis.ops.definitions.sae_correct_acts_impl(module, analysis_batch, batch, batch_idx)[source]#
Implementation for computing correct activations from SAE outputs.
- Return type:
- Parameters:
analysis_batch (DefaultAnalysisBatchProtocol)
batch (BatchEncoding)
batch_idx (int)