Use when analyzing neural network circuits, performing attribution patching, automated circuit discovery, or investigating model interpretability through edge attribution methods in transformer models
Use when analyzing neural network circuits, performing attribution patching, automated circuit discovery, or investigating model interpretability through edge attribution methods in transformer models
"""
Initialize the IOI dataset.
Args:
templates: List of sentence templates with placeholders
names: List of name pairs for IO and S positions
nouns_dict: Dictionary of nouns by category
seed: Random seed for reproducibility
"""
# Default templates if none provided
self
or
"When [A] and [B] went to the [PLACE], [B] gave a [OBJECT] to"
"After [A] and [B] finished [EVENT], [B] handed the [OBJECT] to"
"[A] and [B] were at the [PLACE]. [B] passed the [OBJECT] to"
"Yesterday, [A] and [B] visited the [PLACE]. [B] brought a [OBJECT] for"
# Default names if none provided
self
or
"Mary"
"John"
"Alice"
"Bob"
"Sarah"
"Tom"
"Emma"
"James"
"Lisa"
"David"
# Default nouns if none provided
self
or
"PLACE"
"store"
"park"
"library"
"restaurant"
"museum"
"OBJECT"
"book"
"drink"
"gift"
"letter"
"package"
"EVENT"
"lunch"
"dinner"
"work"
"studying"
"shopping"
self
def
gen_prompt_uniform
self, num_prompts: int = 100
List
Dict
"""
Generate prompts with uniform distribution across templates and names.
Args:
num_prompts: Number of prompts to generate
Returns:
List of prompt dictionaries with metadata
"""
for
in
range
# Select template
self
# Select names (IO and S)
self
0
# Indirect Object
1
# Subject
# Build prompt
"[A]"
"[B]"
# Replace noun placeholders
for
in
self
if
f"[{placeholder}]"
in
f"[{placeholder}]"
# Store with metadata
"text"
"IO"
"S"
"template_idx"
self
"answer"
# The model should predict the IO
self
return
def
flip_words_in_prompt
self, prompt: str, word1: str, word2: str
str
"""
Flip occurrences of two words in a prompt.
Args:
prompt: Original prompt text
word1: First word to swap
word2: Second word to swap
Returns:
Prompt with words flipped
"""
# Use a temporary placeholder to avoid double replacement
"<<TEMP_PLACEHOLDER>>"
return
def
gen_flipped_prompts
self, prompts: List[Dict], flip_type: str = "IO"
List
Dict
"""
Generate flipped versions of prompts for causal analysis.
Args:
prompts: List of original prompt dictionaries
flip_type: Type of flip - "IO" (indirect object) or "S" (subject)
Returns:
List of flipped prompt dictionaries
"""
for
in
"text"
"IO"
"S"
if
"IO"
# Flip IO position with a random other name
for
in
self
for
in
if
not
in
if
self
else
# If no other names available, swap IO and S
self
elif
"S"
# Flip subject and indirect object positions
self
# Now S is in the IO position
else
raise
f"Unknown flip type: {flip_type}"
"text"
"IO"
if
"IO"
and
else
if
"S"
else
"S"
if
"IO"
else
"template_idx"
"template_idx"
"answer"
"original_prompt"
"flip_type"
return
def
create_attention_masks
self, prompts: List[Dict]
List
List
int
"""
Create attention masks for the answer positions in prompts.
Args:
prompts: List of prompt dictionaries
Returns:
List of attention masks (1 for answer position, 0 elsewhere)
"""
for
in
"text"
"answer"
# Simple tokenization (in practice, use model's tokenizer)
# Create mask
for
in
enumerate
if
in
# This is the position we care about
1
else
0
return
def
get_paired_prompts
self, num_pairs: int = 50
List
Tuple
Dict
Dict
"""
Generate pairs of original and flipped prompts for comparison.
Args:
num_pairs: Number of prompt pairs to generate
Returns:
List of (original, flipped) prompt tuples
"""
# Generate original prompts
self
# Generate flipped versions
self
"IO"
# Pair them up
list
zip
return
def
save_dataset
self, filepath: str
"""
Save the generated dataset to a JSON file.
Args:
filepath: Path to save the dataset
"""
"templates"
self
"names"
self
"nouns_dict"
self
"prompts"
self
with
open
'w'
as
2
print
f"Dataset saved to {filepath}"
def
load_dataset
self, filepath: str
"""
Load a dataset from a JSON file.
Args:
filepath: Path to the dataset file
"""
with
open
'r'
as
self
"templates"
self
"names"
self
"nouns_dict"
self
"prompts"
print
f"Dataset loaded from {filepath}"
def
demonstrate_ioi_dataset
"""Demonstrate the IOI dataset functionality."""
print
"="
60
print
"IOI Dataset Demonstration"
print
"="
60
# Initialize dataset
# Generate prompts
print
"\n1. Generating uniform prompts..."
5
for
in
enumerate
1
print
f"\nPrompt {i}:"
print
f" Text: {prompt['text']}"
print
f" Answer (IO): {prompt['answer']}"
print
f" Subject: {prompt['S']}"
# Generate flipped prompts
print
"\n"
"="
60
print
"2. Generating flipped prompts (IO flip)..."
"IO"
for
in
enumerate
zip
3
3
1
print
f"\nPair {i}:"
print
f" Original: {orig['text']}"
print
f" Flipped: {flip['text']}"
print
f" Original answer: {orig['answer']}"
print
f" Flipped answer: {flip['answer']}"
# Generate subject-flipped prompts
print
"\n"
"="
60
print
"3. Generating subject-flipped prompts..."
"S"
for
in
enumerate
zip
2
2
1
print
f"\nPair {i}:"
print
f" Original: {orig['text']}"
print
f" S-Flipped: {flip['text']}"
# Create attention masks
print
"\n"
"="
60
print
"4. Creating attention masks..."
2
for
in
enumerate
zip
2
1
print
f"\nPrompt {i}: {prompt['text']}"
print
f" Tokens: {prompt['text'].split()}"
print
f" Mask: {mask}"
# Get paired prompts for analysis
print
"\n"
"="
60
print
"5. Generating paired prompts for causal analysis..."