Project: 032_Cheating_Automatic_LLM_Benchmarks_Null_Models_Achi | Function: derandomize_tokens_inplace (iclr)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Function that undoes the randomization in LLM evaluation by swapping model identifiers to counteract position bias in benchmark evaluation

**KEY TERMS:** {
  "raw_completion": "evaluation result",
  "top_logprobs": "token probabilities",
  "is_switched": "randomization flag"
}

**CONTEXT CODE:** """
Core adversarial benchmarking functions for cheating LLM benchmarks.
Extracted from Jupyter notebooks for testing purposes.
"""

import random
import numpy as np
import json
from typing import List, Dict, Any, Optional


def set_seeds(seed: int):
    """Set random seeds for reproducibility."""
    random.seed(seed)
    np.random.seed(seed)


def random_search_optimization(
    initial_tokens: List[int],
    max_token_value: int,
    n_steps: int = 256,
    n_tokens_change: int = 1,
    batch_size: int = 1,
    seed: int = 0,
    evaluation_func: Optional[callable] = None
) -> Dict[str, Any]:
    """
    Core optimization algorithm that implements random search to craft adversarial
    prefixes for cheating LLM benchmarks by iteratively sampling and evaluating
    token modifications.

    Args:
        initial_tokens: Initial adversarial token sequence
        max_token_value: Maximum token value for sampling
        n_steps: Number of optimization steps
        n_tokens_change: Number of tokens to modify per step
        batch_size: Batch size for evaluation
        seed: Random seed
        evaluation_func: Function to evaluate adversarial tokens

    Returns:
        Dictionary containing optimization results
    """
    raise NotImplementedError("Function implementation has been masked for testing")


def derandomize_tokens_inplace(raw_completion: Optional[Dict[str, Any]]) -> None:
    """
    Function that undoes the randomization in LLM evaluation by swapping model
    identifiers to counteract position bias in benchmark evaluation.

    Args:
        raw_completion: Raw completion dictionary with logprobs
    """
    raise NotImplementedError("Function implementation has been masked for testing")

def create_structured_response(
    adversarial_prefix: str = "",
    target_instruction: str = "Output nothing",
    model_identifiers: List[str] = ["M", "m"]
) -> str:
    """
    Algorithm that creates structured cheating responses by crafting adversarial
    outputs designed to manipulate LLM auto-annotators through template injection
    and syntactic confusion.

    Args:
        adversarial_prefix: Optimizable adversarial prefix
        target_instruction: Fake instruction to inject
        model_identifiers: List of model identifiers to use

    Returns:
        Structured adversarial template string
    """
    raise NotImplementedError("Function implementation has been masked for testing")

def generate_adversarial_dataset(
    base_prompts: List[str],
    adversarial_template: str,
    n_samples: int = 10
) -> List[Dict[str, str]]:
    """
    Generate a dataset of adversarial prompts for benchmark manipulation.

    Args:
        base_prompts: List of base instruction prompts
        adversarial_template: Template for adversarial injection
        n_samples: Number of samples to generate

    Returns:
        List of adversarial prompt dictionaries
    """
    adversarial_prompts = []

    for i in range(min(n_samples, len(base_prompts))):
        prompt = base_prompts[i]

        # Replace placeholder with adversarial template
        if '{iclr_2025_adv}' in prompt:
            adversarial_prompt = prompt.replace('{iclr_2025_adv}', adversarial_template)
        else:
            # If no placeholder, append adversarial content
            adversarial_prompt = prompt + "\n\n" + adversarial_template

        adversarial_prompts.append({
            'index': i,
            'original_prompt': prompt,
            'adversarial_prompt': adversarial_prompt,
            'template': adversarial_template
        })

    return adversarial_prompts


def evaluate_benchmark_manipulation(
    predictions: List[str],
    ground_truth: List[str],
    model_identifiers: List[str] = ["M", "m"]
) -> Dict[str, float]:
    """
    Evaluate the effectiveness of benchmark manipulation by measuring
    how often the null model is preferred.

    Args:
        predictions: List of predicted model preferences
        ground_truth: List of ground truth preferences
        model_identifiers: List of valid model identifiers

    Returns:
        Dictionary with evaluation metrics
    """
    if len(predictions) != len(ground_truth):
        raise ValueError("Predictions and ground truth must have same length")

    total_samples = len(predictions)
    if total_samples == 0:
        return {'success_rate': 0.0, 'total_samples': 0}

    # Count successful manipulations (when null model is preferred)
    successful_manipulations = 0
    valid_predictions = 0

    target_identifier = model_identifiers[0]  # Usually "M" for null model

    for pred, gt in zip(predictions, ground_truth):
        if pred in model_identifiers:


Complete the function implementation replacing "raise NotImplementedError".