Pytorch implementation of paper: Beyond the Imitation Game: Quantifying and Extrapolating the Capabilities of Language Models (known as BIG-bench)

Written by

in

The 2022 paper “Beyond the Imitation Game: Quantifying and Extrapolating the Capabilities of Language Models” (commonly known as BIG-bench) introduced a massive, collaborative benchmark consisting of 204 tasks designed to probe the limits of Large Language Models (LLMs).

Unlike standard benchmarks (like GLUE), BIG-bench was specifically designed to identify where models fail and where they show emergence. Implementing BIG-bench in PyTorch requires a framework that can handle diverse task types (multiple-choice, free-text generation, and scoring) across different model scales.


1. The Core Task Architecture

BIG-bench tasks are typically defined in JSON or Python. To implement a “BIG-bench style” evaluator in PyTorch, you need a base class that handles the three primary evaluation metrics: Multiple Choice, Generative (Exact Match/ROUGE), and Log-Likelihood Scoring.

Python

import torch
import torch.nn.functional as F

class BigBenchTask:
    def __init__(self, model, tokenizer, device="cuda"):
        self.model = model
        self.tokenizer = tokenizer
        self.device = device

    def score_choices(self, prompt, choices):
        """
        Implements Multiple Choice (MC) scoring.
        Calculates the log-likelihood of each choice following the prompt.
        """
        self.model.eval()
        scores = []
        
        for choice in choices:
            # Full sequence = Prompt + Choice
            full_text = prompt + choice
            inputs = self.tokenizer(full_text, return_tensors="pt").to(self.device)
            prompt_len = self.tokenizer(prompt, return_tensors="pt")['input_ids'].shape[1]
            
            with torch.no_grad():
                outputs = self.model(**inputs)
                logits = outputs.logits
                
                # Shift logits and labels for next-token prediction
                shift_logits = logits[..., :-1, :].contiguous()
                shift_labels = inputs['input_ids'][..., 1:].contiguous()
                
                # Calculate cross-entropy for the 'choice' part only
                # We start from prompt_len - 1 to get the first token of the choice
                loss_fct = torch.nn.CrossEntropyLoss(reduction='none')
                loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
                
                # Mean log-likelihood of the choice tokens
                choice_loss = loss[prompt_len-1:].mean()
                scores.append(-choice_loss.item()) # Higher is better
                
        return torch.tensor(scores).argmax().item() # Returns index of best choice


2. Implementing “Social Bias” Probes

A key feature of BIG-bench was its focus on “Responsible AI.” Below is a PyTorch implementation logic for a Bias Detection task (similar to the BBQ or Bias in BIOS tasks in BIG-bench).

Python

def check_stereotypical_bias(model, tokenizer, context, target_a, target_b):
    """
    Context: 'The person in the hospital is a ___.'
    Target A: 'nurse' (Stereotypical for female)
    Target B: 'doctor' (Stereotypical for male)
    Checks if the model assigns higher probability based on gendered pronouns in the prefix.
    """
    prompt = f"Given the context: {context} \nQuestion: What is their profession?"
    choices = [target_a, target_b]
    
    evaluator = BigBenchTask(model, tokenizer)
    chosen_idx = evaluator.score_choices(prompt, choices)
    
    return choices[chosen_idx]


3. BIG-bench “Hard” (BBH)

The paper identified that some tasks were too difficult for all but the largest models. These are known as BIG-bench Hard (BBH). To implement these, we must use Chain-of-Thought (CoT) decoding logic as defined in the paper.

Python

def evaluate_bbh_task(model, tokenizer, prompt_with_cot):
    """
    Implements the 'Logical Deduction' task from BBH.
    Requires generating a rationale before the final answer.
    """
    inputs = tokenizer(prompt_with_cot, return_tensors="pt").to(model.device)
    
    # We use greedy decoding or beam search for BBH
    outputs = model.generate(
        **inputs, 
        max_new_tokens=256, 
        temperature=0, # Greedy for consistency in BBH
        stop_strings=["Q:"] # Stop at the next question
    )
    
    return tokenizer.decode(outputs[0], skip_special_tokens=True)


4. Key Findings from the Paper

MetricSignificance
Linear ScalingMany tasks (like knowledge retrieval) improve linearly with model size.
Breakthrough (Emergence)Some tasks (like “Logical Deduction” or “Checkmate in One”) show zero progress until a “breakthrough” scale is reached.
Human CalibrationThe benchmark included “Human Expert” scores, showing that 175B models still significantly underperformed humans on many logic tasks.

Summary of BIG-bench Implementation

Implementing BIG-bench is less about a single model and more about the Evaluation Infrastructure. Because the benchmark contains 204 tasks, a modular PyTorch approach is required to loop through different task templates, calculate the log-likelihood of answers, and aggregate results to visualize the “Emergence” curves we discussed in the previous paper.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *