pyvene.models.modeling_utils

pyvene.models.modeling_utils#

Functions

b_sd_to_bsd(tensor, s)

Convert a tensor of shape (b, s*d) back to (b, s, d).

bhsd_to_bs_hd(tensor)

Convert a tensor of shape (b, h, s, d) to (b, s, h*d).

bs_hd_to_bhsd(tensor, h)

Convert a tensor of shape (b, s, h*d) back to (b, h, s, d).

bsd_to_b_sd(tensor)

Convert a tensor of shape (b, s, d) to (b, s*d).

do_intervention(base_representation, ...)

Do the actual intervention.

gather_neurons(tensor_input, unit, ...[, device])

Gather intervening neurons.

get_dimension_by_component(model_type, ...)

Based on the representation, get the aligning dimension size.

get_internal_model_type(model)

Return the model type.

get_module_hook(model, representation[, backend])

Render the intervening module with a hook.

getattr_for_torch_module(model, parameter_name)

Recursively fetch the model based on the name.

is_gru(model)

Determine if this is a transformer model.

is_mlp(model)

Determine if this is a mlp model.

is_stateless(model)

Determine if the model is stateful (e.g., rnn) or stateless (e.g., transformer)

is_transformer(model)

Determine if this is a transformer model.

output_to_subcomponent(output, component, ...)

Split the raw output to subcomponents if specified in the config.

print_forward_hooks(main_module)

Function to print forward hooks of a module and its sub-modules.

remove_forward_hooks(main_module)

Function to remove all forward and pre-forward hooks from a module and

scatter_neurons(tensor_input, ...[, device])

Replace selected neurons in tensor_input by replacing_tensor_input.

simple_output_to_subcomponent(output, ...)

This is an oversimplied version for demo.

simple_scatter_intervention_output(...)

This is an oversimplied version for demo.

weighted_average(values, weights)

Classes

HandlerList(handlers)

General class to set hooks and set off hooks.