Auto-Antislop

repository·main·Indexed 41 days ago

https://github.com/sam-paech/auto-antislop

An automated pipeline designed to reduce 'slop' (over-represented words and phrases) in language models. It automates baseline dataset generation, slop identification, and preference dataset creation via antislop-vllm. The system features a novel trainer called FTPO (Final Token Preference Optimisation) that focuses gradient updates on continuation tokens to push probability mass away from banned phrases while maintaining model stability. Supports LoRA, 4-bit quantization, and integration with unsloth and transformers/trl.

Tokens
4.2K
Snippets
10
Records
20
Agent score
3%

What's inside Auto-Antislop

  1. What is FTPO (Final-Token Preference Optimisation)?

    main

    FTPO is a preference optimization training algorithm that focuses gradient updates exclusively on the continuation token (the choice between a rejected and a chosen token) rather than the entire preceding context.

    Goal: To push probability mass away from the first token of a banned phrase (the rejected token) and toward viable alternatives (the chosen tokens) while keeping the rest of the model's distribution stable.

    Training Example Structure:

    • Prompt: The shared context up to the point of the ban.
    • Rejected Token: The specific token that triggered the ban.
    • Chosen Tokens: A set of coherent alternatives sampled using a min-p filter.

    Loss Mechanism:

    • Preference Term: Encourages the model to rank all chosen tokens higher than the rejected token. Once a chosen token's logit beats the rejected one by a specific margin (ftpo_clip_epsilon_logits), the loss for that pair stops contributing.
    • MSE Regularizer: A two-part Mean Squared Error loss in logit space (rather than KL loss) to minimize unnecessary weight movement:
      • mse_target: Applies targeted pressure to the chosen and rejected tokens. Users should typically enable either mse_target_tokenwise or mse_target_aggregate.
      • mse_non_target: Applies loss to the rest of the vocabulary to maintain stability.
  2. Understand FTPO (Final Token Preference Optimization) mode

    main
    The ftpo mode utilizes the FTPOTrainer. Unlike standard preference optimization that looks at entire continuations, FTPO focuses on the preference for a single next token given a specific context. This mode is specifically designed for correcting specific token choices.
  3. How the Auto-Antislop pipeline works

    main

    Auto-Antislop operates through an iterative pipeline designed to identify and eliminate 'slop' (undesirable linguistic patterns) from model outputs using a combination of generation, analysis, and preference optimization.

    Pipeline Flow

    1. vLLM Server Management: If manage_vllm is enabled, the script manages a vLLM server. Otherwise, it connects to an existing external vLLM server.
    2. Antislop Iteration Loop (num_iterations times):
      • Generation (Iteration 0 - Baseline): Generates text from a source dataset without any active ban lists to establish a baseline.
      • Analysis: Uses slop-forensics to compare n-gram frequencies against human profiles, identify over-represented words, and extract common 'slop phrases'.
      • Ban List Update: Creates or updates n-gram and slop phrase ban lists. This merges findings from analysis with user-supplied extra_ngrams_to_ban, extra_slop_phrases_to_ban, and extra_regex_patterns from the configuration.
      • Generation (Iteration 1+ - Anti-Slop): Re-generates text using the accumulated ban lists. When a ban occurs, the system captures preference pairs (rejected token vs. coherent alternatives) to build a training dataset.
    3. FTPO Dataset Creation: Creates preference pairs consisting of a prompt (context up to the ban), a rejected token (the first token of the banned sequence), and chosen tokens (coherent alternatives selected via a min-p filter).
    4. FTPO Finetuning: If finetune_enabled is true, the pipeline performs FTPO finetuning using the generated preference dataset. This supports LoRA, 4-bit quantization, and both unsloth and transformers/trl training paths.
  4. Quickstart: Run an end-to-end unslop pipeline

    main

    To perform a complete unslop process (generating a baseline, identifying slop, creating a preference dataset, and fine-tuning) using the provided example for gemma-3-4b-it, follow these steps to set up a virtual environment and execute the pipeline:

    1. Clone the repository with submodules.
    2. Create and activate a Conda environment.
    3. Install specific versions of torch and the project requirements.
    4. Install vllm.
    5. Run the main script using the provided configuration.

    Note: This sequence is designed for a quick test of the pipeline's functionality.

    # creates a venv, installs auto-antislop and runs an end-to-end unslop of gemma-3-4b-it
    git clone https://github.com/sam-paech/auto-antislop.git --recurse
    conda create -n antislop python=3.11 -y
    conda activate antislop
    cd auto-antislop
    pip install "torch==2.8.*"
    pip install -r requirements.txt
    pip install vllm
    python main.py -c configs/gemma-3-4b-it.yaml
  5. Resume an interrupted pipeline run

    main

    If the pipeline is interrupted, you can resume from the last successfully completed part of the iteration by providing the path to the experiment directory using --resume-from-dir.

    Generation for an iteration is considered complete if the output JSONL file exists and contains the expected number of prompt_ids.

    python main.py --config-file path/to/your_config.yaml --resume-from-dir results/auto_antislop_runs/run_YYYYMMDD_HHMMSS
  6. Install Auto-Antislop

    main

    Prerequisites

    • Python 3.9+
    • NVIDIA GPU with CUDA installed
    • Git

    Installation Steps

    1. Clone the Repository (ensure submodules are included):

      git clone --recurse-submodules https://github.com/your-username/auto-antislop.git
      cd auto-antislop

      If you already cloned without submodules, run: git submodule update --init --recursive

    2. Install Dependencies: It is highly recommended to use a virtual environment.

      pip install -r requirements.txt

      Note: vllm and torch are commented out in requirements.txt to allow you to install versions compatible with your specific CUDA setup. If using the --manage-vllm feature, install vllm manually (e.g., pip install vllm).

    3. NLTK Data: The script attempts to download punkt, punkt_tab, and stopwords automatically. If this fails, download them manually in Python:

      import nltk
      nltk.download('punkt')
      nltk.download('punkt_tab')
      nltk.download('stopwords')
  7. Configure FTPO hyper-parameters

    main

    You can fine-tune the FTPO training process by setting specific hyper-parameters in your configuration file. If these are omitted or set to null, the system falls back to FTPOTrainer defaults.

    Available Hyper-parameters

    ParameterDescription
    ftpo_betaGlobal scale on preference loss (higher = steeper sigmoid).
    ftpo_lambda_mse_target_tokenwiseStrength of the MSE loss tether on individual chosen+rejected logits vs reference.
    ftpo_tau_mse_target_tokenwiseGrace bandwidth (in logits) before the target MSE loss kicks in. A range of 0-1.5 is recommended.
    ftpo_lambda_mseScaling strength for the MSE loss applied to the non-target (remaining) vocabulary.
    ftpo_clip_epsilon_logitsThe margin by which a chosen token must beat the rejected token before preference loss turns off.
    # Example FTPO configuration snippet
    ftpo_beta: 0.1
    ftpo_lambda_mse_target_tokenwise: 0.05
    ftpo_tau_mse_target_tokenwise: 0.5
    ftpo_lambda_mse: 0.4
    ftpo_clip_epsilon_logits: 2
  8. Configure the Auto-Antislop pipeline via YAML

    main

    Configuration is managed through a YAML file (examples are in configs/). Key configuration groups include:

    VLLM & Antislop Generation

    • model_id: HuggingFace ID or local directory of the model to unslop.
    • generation_threads: Number of parallel threads for generation (try 30).
    • generation_max_prompts: Number of prompts to generate (e.g., 50 for testing, 1000-2000 for production).
    • generation_hf_dataset_name: HuggingFace dataset (ShareGPT format) used to source prompts.
    • extra_slop_phrases_to_ban: Custom list of strings to be trained out of the model.

    Fine-Tuning

    • finetune_use_unsloth: Use unsloth (lower VRAM) or transformers/trl.
    • finetune_mode: Options are ftpo (recommended for gentler training), dpo, or dpo_final_token.
    • finetune_early_stopping_wins: Threshold (e.g., 0.8-0.9) to stop training when 'chosen' tokens are preferred over 'rejected' tokens.
    • finetune_lora_r: LoRA rank. FTPO works best with high ranks (128-256).
    • finetune_target_modules: Modules to target (e.g., ["lm_head"] for minimal intervention).
    • finetune_freeze_early_layers: Boolean to freeze all layers except the last n layers (finetune_n_layers_unfrozen).
    • finetune_learning_rate: Manual learning rate.
    • finetune_auto_learning_rate: Boolean to use an automatic learning rate based on dataset/batch size.
    • finetune_max_train_examples: Number of training examples (suggested 8000-12000).
  9. Run the Auto-Antislop CLI

    main

    The main.py script is the primary entrypoint for the Auto-Antislop pipeline. It automates iterative dataset generation and DPO (Direct Preference Optimization) finetuning. The script manages a local vLLM server for generation, orchestrates the pipeline, and optionally runs a finetuning stage.

    To run the pipeline, you typically provide a YAML configuration file. You can override any configuration setting using command-line arguments.

    # Basic usage with a config file
    python main.py --config-file auto_antislop_config.yaml
    
    # Example: Overriding specific parameters via CLI
    python main.py --config-file my_config.yaml --num-iterations 5 --finetune-mode ftpo
  10. Troubleshoot Submodule and NLTK issues

    main

    If you encounter errors during setup or execution, check the following:

    • Submodule Errors: If errors relate to antislop-vllm or slop-forensics, initialize the submodules using:
      git submodule update --init --recursive
    • NLTK Data Errors: If ensure_core_nltk_resources() fails, you must download the required NLTK resources manually.
  11. Troubleshoot GPU Memory issues

    main

    Running vLLM and finetuning simultaneously requires significant GPU VRAM. If you encounter memory errors:

    1. Adjust vllm_gpu_memory_utilization in your configuration.
    2. Adjust finetuning batch sizes or use quantization.
    3. Note that the script attempts to stop vLLM before starting finetuning to free up VRAM if both are targeted for the same GPU.