Misc
auto_circuit.utils.misc
Functions
get_most_similar_embeddings
get_most_similar_embeddings(model: Module, out: Tensor, answer: Optional[str] = None, top_k: int = 10, apply_ln_final: bool = False, apply_unembed: bool = False, apply_embed: bool = False)
Helper function to print the top top_k
most similar embeddings to a given vector.
Can be used for either embeddings or unembeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to get the embeddings from. |
required |
out |
Tensor
|
The vector to get the most similar embeddings to. |
required |
answer |
Optional[str]
|
Token to show the rank of in the output logits. |
None
|
top_k |
int
|
The number of top output logits to show. |
10
|
apply_ln_final |
bool
|
Whether to apply the final layer normalization to the vector before getting the most similar embeddings. |
False
|
apply_unembed |
bool
|
If |
False
|
apply_embed |
bool
|
If |
False
|
Source code in auto_circuit/utils/misc.py
load_cache
Load a dictionary from a cache file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder_name |
str
|
The name of the folder to load the cache from. |
required |
filename |
str
|
The name of the file to load the cache from. |
required |
Returns:
Type | Description |
---|---|
Dict[Any, Any]
|
The loaded dictionary. |
Source code in auto_circuit/utils/misc.py
module_by_name
Gets a module from a model by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Any
|
The model to get the module from. |
required |
module_name |
str
|
The name of the module to get. |
required |
Returns:
Type | Description |
---|---|
Module
|
The module. |
Source code in auto_circuit/utils/misc.py
percent_gpu_mem_used
Get the percentage of GPU memory used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
total_gpu_mib |
int
|
The total amount of GPU memory in MiB. |
49000
|
Returns:
Type | Description |
---|---|
str
|
The percentage of GPU memory used. |
Source code in auto_circuit/utils/misc.py
remove_hooks
Context manager that makes it easier to use temporary PyTorch hooks without accidentally leaving them attached.
Add hooks to the set yielded by this context manager, and they will be removed when the context manager exits.
Yields:
Type | Description |
---|---|
Set[RemovableHandle]
|
An empty set that can be used to store the handles of the hooks. |
Source code in auto_circuit/utils/misc.py
repo_path_to_abs_path
Convert a path relative to the repository root to an absolute path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
A path relative to the repository root. |
required |
Returns:
Type | Description |
---|---|
Path
|
The absolute path. |
Source code in auto_circuit/utils/misc.py
run_prompt
run_prompt(model: Module, prompt: str, answer: Optional[str] = None, top_k: int = 10, prepend_bos: bool = False)
Helper function to run a string prompt through a TransformerLens HookedTransformer
model and print the top top_k
output logits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to run the prompt through. |
required |
prompt |
str
|
The prompt to run through the model. |
required |
answer |
Optional[str]
|
Token to show the rank of in the output logits. |
None
|
top_k |
int
|
The number of top output logits to show. |
10
|
prepend_bos |
bool
|
Whether to prepend the |
False
|
Source code in auto_circuit/utils/misc.py
save_cache
Save a dictionary to a cache file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_dict |
Dict[Any, Any]
|
The dictionary to save. |
required |
folder_name |
str
|
The name of the folder to save the cache in. |
required |
base_filename |
str
|
The base name of the file to save the cache in. The current date and time will be appended to the base filename. |
required |
Source code in auto_circuit/utils/misc.py
set_module_by_name
Sets a module in a model by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Any
|
The model to set the module in. |
required |
module_name |
str
|
The name of the module to set. |
required |
new_module |
Module
|
The module to replace the existing module with. |
required |
Warning
This function modifies the model in place.