interpretune.analysis.ops.base#
Base classes for analysis operations.
Functions
|
Build arguments for operation calls. |
|
Return a stable string key for a backend capability-like object. |
|
Get the tokenized input tensor from the batch, handling cross-backend key differences. |
|
Resolve the actual batch key, checking aliases for cross-backend compatibility. |
|
Classes
|
|
|
Base class for analysis operations. |
|
|
|
Configuration for a dataset column. |
|
A composition of analysis operations to be executed. |
|
Schema defining column specifications for analysis operations. |
|
A special wrapper for operations that ensures the op is instantiated when accessed directly or when attributes are accessed. |
- class interpretune.analysis.ops.base.AnalysisBatch(*args, **kwargs)[source]#
- bind_resolution_context(module, *, analysis_inputs=None, batch_idx=None, input_schema=None)[source]#
Bind execution-time lookup context used by scoped batch accessors.
- Return type:
- Parameters:
- clear_resolution_context()[source]#
Clear any previously bound execution-time lookup context.
- Return type:
- get(key, default=None, *, scopes=None, resolve=True)[source]#
Return a direct batch value or, when bound, resolve via scoped inputs.
- require(key, *, scopes=None, message=None)[source]#
Resolve a required value from the bound analysis execution context.
- resolution_context(module, *, analysis_inputs=None, batch_idx=None, input_schema=None)[source]#
Temporarily bind execution-time scoped lookup state for the current op call.
- class interpretune.analysis.ops.base.AnalysisOp(name, description, output_schema, input_schema=None, aliases=None, impl_params=None, required_capabilities=None)[source]#
Base class for analysis operations.
- Parameters:
- static process_batch(analysis_batch, batch, output_schema, tokenizer=None, save_prompts=False, save_tokens=False, decode_kwargs=None)[source]#
Process analysis batch using provided output schema.
This static method handles the common processing logic for analysis batches, including token handling and schema-based transformations.
- Parameters:
analysis_batch (
BaseAnalysisBatchProtocol) – The analysis batch to processbatch (
BatchEncoding) – The raw batch dataoutput_schema (
OpSchema) – Schema defining the structure of the outputtokenizer (
PreTrainedTokenizerBase|None) – Optional tokenizer for decoding promptssave_prompts (
bool) – Whether to save promptssave_tokens (
bool) – Whether to save tokensdecode_kwargs (
dict[str,Any] |None) – Additional keyword arguments for decoding
- Return type:
- Returns:
Processed analysis batch
- active_ctx_key(ctx_key)[source]#
Context manager for temporarily setting the active context key.
- Parameters:
ctx_key – The context key to set during the context execution
- class interpretune.analysis.ops.base.ColCfg(datasets_dtype, required=True, dyn_dim=None, dyn_dim_ceil=None, non_tensor=False, per_latent=False, per_latent_model_hook=False, intermediate_only=False, connected_obj='analysis_store', array_shape=None, sequence_type=True, array_dtype=None, default=None)[source]#
Configuration for a dataset column.
- Parameters:
datasets_dtype (str)
required (bool)
dyn_dim (int | None)
dyn_dim_ceil (Literal['batch_size', 'max_answer_tokens', 'num_classes', 'vocab_size', 'max_seq_len'] | None)
non_tensor (bool)
per_latent (bool)
per_latent_model_hook (bool)
intermediate_only (bool)
connected_obj (Literal['analysis_store', 'datamodule'])
array_shape (tuple[int | Literal['batch_size', 'max_answer_tokens', 'num_classes', 'vocab_size', 'max_seq_len'] | None, ...] | None)
sequence_type (bool)
array_dtype (str | None)
default (Any)
- class interpretune.analysis.ops.base.CompositeAnalysisOp(ops, name=None, aliases=None, description=None, *args, **kwargs)[source]#
A composition of analysis operations to be executed.
- Parameters:
ops (Sequence[AnalysisOp])
name (str | None)
aliases (Sequence[str] | None)
description (str | None)
- class interpretune.analysis.ops.base.OpSchema(*args, **kwargs)[source]#
Schema defining column specifications for analysis operations.
- class interpretune.analysis.ops.base.OpWrapper(op_name)[source]#
A special wrapper for operations that ensures the op is instantiated when accessed directly or when attributes are accessed.
- classmethod initialize(target_module)[source]#
Set the target module where operations will be registered.
- classmethod register_operations(module, dispatcher)[source]#
Register all operations from the dispatcher to the module as lazy OpWrapper instances.
- Parameters:
module – The module where operations will be registered
dispatcher – The operations dispatcher instance
- property dispatcher#
Lazily load the dispatcher only when needed.
- interpretune.analysis.ops.base.build_call_args(module, analysis_batch, batch, batch_idx, impl_params=None, **kwargs)[source]#
Build arguments for operation calls.
- Parameters:
module – The module instance
analysis_batch – The analysis batch
batch – The input batch
batch_idx – The batch index
impl_params – Implementation-specific parameters
**kwargs – Additional keyword arguments
- Returns:
Dictionary of arguments for the operation call
- interpretune.analysis.ops.base.capability_value(capability)[source]#
Return a stable string key for a backend capability-like object.
- interpretune.analysis.ops.base.get_batch_input(batch)[source]#
Get the tokenized input tensor from the batch, handling cross-backend key differences.
Supports both TransformerLens convention (
batch["input"]) and HuggingFace/NNsight convention (batch["input_ids"]).- Return type:
- Parameters:
batch (BatchEncoding)
- interpretune.analysis.ops.base.resolve_batch_input_key(batch, key='input')[source]#
Resolve the actual batch key, checking aliases for cross-backend compatibility.
- Parameters:
batch (
BatchEncoding) – The batch to check for the key.key (
str) – The canonical key name to resolve (default:"input").
- Return type:
- Returns:
The actual key present in the batch (either the canonical key or one of its aliases).
- Raises:
KeyError – If neither the key nor any of its aliases are present in the batch.