Research code for analyzing failure modes of nucleus (top-p) sampling and comparing it against a probability-threshold alternative on WikiText-103.
nucleus_sampling_analysis/: the new experiment packagescripts/run_experiment.py: CLI entrypoint for the full studyscripts/plot_results.py: regenerate plots from saved run artifactstests/: unit, synthetic, and integration coveragepapercode_with_threshold_sampling/: original prototype kept as reference
The pipeline:
- Loads WikiText-103 validation and test prompts.
- Tunes one global threshold
tauper temperature on validation prompts. - Compares
greedy,temperature,top-k,nucleus, andthresholdsamplers. - Logs per-step distribution diagnostics such as entropy, effective support, retained set size, and sampled-token probability.
- Saves aggregate metrics and plots for repetition, diversity, perplexity, and stressed peaked/flat regimes.
Create or update an environment with compatible dependencies first. The current machine in this workspace has torch 2.0.0 with transformers 4.57.6, which is not compatible for model loading. A working setup is:
python -m pip install -e .Run the full experiment:
python scripts/run_experiment.py --model-name distilgpt2 --output-dir artifacts/distilgpt2_runRegenerate plots from an existing run:
python scripts/plot_results.py artifacts/distilgpt2_runRun the local LLM-as-a-judge stage on an existing run:
python scripts/run_llm_judge.py --run-dir artifacts/distilgpt2_run --judge-model-name your-local-judge-modelRun the local test suite:
python -m unittest discover -s tests -vEach run writes:
config.jsondataset_manifest.jsonthreshold_tuning.jsonlgeneration_records.jsonlstep_stats.jsonlrun_summaries.jsonl- plot PNGs in the same run directory
The judge stage writes these under RUN_DIR/judge/:
judge_pairs.jsonljudge_results.jsonljudge_summary.json
The implementation does not hard-code the paper's 12.38 human perplexity target. Instead, it tunes tau on validation prompts using a combined objective:
- lower generated negative log-likelihood
- lower repetition
- mild penalty when retained support falls outside a target band
This keeps the threshold rule simple, reproducible, and aligned with the project plan.