Skip to content
AI.info

Research

Test-Time Detoxification without Training or Learning Anything

Test-Time Detoxification without Training or Learning Anything Overview Research area: Natural Language Processing / LLM safety and controllability, sitting at the intersection of test-time inference

arXiv
2602.02498
Published
2026-01-14
Authors
Baturay Saglam, Dionysis Kalogerias

AI summary

Test-Time Detoxification without Training or Learning Anything

Overview

Research area: Natural Language Processing / LLM safety and controllability, sitting at the intersection of test-time inference control and derivative-free (zeroth-order) optimization.

Technical level: Advanced. The paper assumes familiarity with autoregressive transformer generation, embedding spaces, gradient descent, and zeroth-order/finite-difference gradient estimation; the experimental protocol is accessible, but the method rests on convergence results from stochastic optimization theory.

One-sentence scope: The paper proposes TIDE, a test-time procedure that reduces the toxicity of LLM completions by taking a few zeroth-order gradient-descent steps directly on the prompt embeddings, using only forward passes and a black-box toxicity score.

What This Paper Is About

Large language models trained on web-scale data can produce toxic continuations even from benign prompts, which is a safety risk when they are deployed at scale. Most existing fixes either retrain the model, require gradient access, or bolt on learned auxiliary components such as reward models and classifiers, all of which are costly and often do not transfer between model families. This paper asks whether detoxification can be done purely at inference time by nudging the prompt's embeddings toward lower-toxicity regions, using nothing but forward evaluations of the model and a black-box toxicity score.

Key Contributions

  1. A training-free, model-agnostic detoxification procedure. The authors introduce TIDE (Test-time Iterative Detoxification via Embeddings), which requires only (i) access to prompt embeddings, (ii) a toxicity scoring function, and (iii) a small number of forward evaluations. It needs no retraining, no inference-time gradients, no learned auxiliary module, and no hand-crafted prompt templates (as summarized in Table 1).

  2. Application of zeroth-order optimization to detoxification. Following the finite-difference estimator of Nesterov and Spokoiny (2017), the paper derives a Monte Carlo estimator of the gradient of completion toxicity with respect to the input embedding matrix, computed by probing the model with tokenwise Gaussian perturbations. The authors state that, to their knowledge, there is no prior direct application of zeroth-order optimization to detoxification.

  3. Practical regularization scheme for embedding-space updates. The method normalizes the gradient direction, projects updates back into a cosine-similarity ball around the original embedding when a threshold is violated, and stops early once toxicity falls below 0.5 — a threshold the paper notes agrees with human raters 88% of the time (Gehman et al., 2020).

  4. Empirical evidence of a favorable toxicity–fluency trade-off. Across AttaQ, BOLD, and the "challenging" subset of RealToxicityPrompts, and across GPT-2 Large (774M), Gemma 2-2B, Qwen3-4B, and Llama 3.1-8B, the method is reported to deliver robust toxicity reductions and, in most settings, the best overall toxicity–quality trade-off among the compared baselines.

Main Findings

  • Competitive toxicity reduction with best-in-class trade-offs. On GPT-2 Large evaluated on the 1199-prompt "challenging" RealToxicityPrompts subset at temperature 0.1 over three trials, TIDE reaches 0.156 average maximum toxicity, 0.122 average mean toxicity, 5.53 fluency perplexity, and a toxicity rate of 0.003, versus 0.591 / 0.495 / 4.13 / 0.639 for the base model. The authors argue it attains lower toxicity at comparable perplexity, or lower perplexity at comparable toxicity, than baselines.

  • DeStein achieves lower raw toxicity but at far higher perplexity. DeStein obtains 0.116 maximum toxicity, 0.087 mean toxicity, and a 0.062 toxicity rate, but its fluency perplexity is 8.72 — substantially above TIDE's 5.53 and the base model's 4.13. RAD at β = 100 reaches 0.134 / 0.102 / 0.046 at perplexity 7.21, and RAD at β = 50 reaches 0.209 / 0.157 / 0.120 at perplexity 5.50.

  • SASA underperforms at low temperature. SASA is reported at roughly base-model toxicity levels across β = 50, 75, 100 (maximum toxicity 0.580, 0.587, 0.579 respectively), which the authors attribute to low-temperature decoding producing highly peaked token distributions that margin-based adjustments cannot meaningfully shift.

  • Optimization does not change the visible prompt. Across all prompts and trials, decoding the optimized embeddings recovers the original prompt tokens exactly. The perturbation is described as entirely sub-lexical: embeddings shift within their original token regions, so the human-readable input is unchanged while the model's continuous input differs.

  • Early stopping keeps computation modest. Each iteration costs N + 1 objective evaluations (original embedding plus N Monte Carlo perturbations). The effective iteration count K̄ is at most 3.17 (GPT-2 on RTP), even though the algorithm is allowed up to K = 10 iterations; with a high-throughput stack such as vLLM, the N + 1 evaluations within an iteration are batched into a single call, reducing effective cost to roughly K̄ sequential forward passes.

  • Small sample sizes suffice. The paper notes that N = 16 works well for Llama 3.1-8B despite an embedding dimension of d = 4096, and cites prior work reporting robust performance with N as low as 1 (Hashmi et al., 2024; Pougkakiotis et al., 2025). The authors hypothesize that an approximately correct, possibly biased direction is enough for detoxification.

  • Sensitivity to μ and N is not monotonic, but sensitivity to κ is. Changing the perturbation scale μ alters the smoothed objective being optimized, and increasing N mainly reduces estimator variance rather than guaranteeing better basins in a nonconvex toxicity landscape. Higher cosine similarity thresholds κ, by contrast, produce more fluent but more toxic outputs, closer to the base model.

  • Results generalize across toxicity scorers. In supplementary experiments, optimization uses Perspective AI while evaluation uses Detoxify (Hanu and Unitary team, 2020); TIDE is reported to again achieve lower toxicity at comparable perplexity.

  • Higher-temperature behavior with variance reduction. At temperature 1.0, the paper reports that TIDE achieves an absolute zero toxicity rate in fewer iterations than in the low-temperature setting when complemented with variance reduction, at the cost of additional forward passes.

  • Wall-clock overhead. TIDE's optimization is reported to take no more than 9 seconds in total, compared to roughly 1 second for RAD, which the authors frame as a mild limitation given the absence of training or learned parameters.

Methodology in Plain English

The idea is to treat the prompt's embedding matrix — the numeric vectors the model actually consumes — as a set of tunable knobs rather than fixed inputs.

The objective is the composition of two black boxes: run the model on the embeddings to get a completion, then score that completion with a toxicity function, giving a single number in [0, 1]. Nothing about this pipeline is differentiable in a usable way.

To move downhill anyway, the authors use a classic trick from gradient-free optimization. At each step, they add many small random Gaussian perturbations to the embedding matrix (one independent perturbation per token) and see how the toxicity score changes. Averaging those changes weighted by the perturbation directions yields an estimate of the gradient direction — the Nesterov–Spokoiny finite-difference estimator. This requires only forward passes through the model, not backward passes.

The estimated gradient is then normalized so the step size is controlled entirely by the learning rate, and the embeddings are updated in the opposite direction. Three safeguards keep the process sane: (1) gradient normalization to avoid erratic jumps in high-dimensional space, (2) a cosine-similarity constraint that projects the embedding back if it drifts too far in direction from the original, and (3) early stopping as soon as the toxicity score drops below 0.5. Only a handful of iterations are typically needed — at most 3.17 on average in the reported GPT-2 RTP setting.

The theory behind the estimator is stated under Lipschitz continuity and Lipschitz smoothness assumptions: smoothing with Gaussian noise produces a differentiable surrogate, the approximation error of the smoothed objective is of order O(μ√(Td)), and the gradient bias is bounded by C μ L (Td)^{3/2} for some constant C. The authors are explicit that these conditions are not verifiable from black-box query access and are used only to motivate the connection between the smoothed and true gradients.

Why This Matters

Impact on research. The work reframes word embeddings as practical control variables for autoregressive generation and argues that within embedding-space steering, detoxification is largely resolved by this approach — further gains would more likely come from better objectives or complementary safety signals than from alternative steering rules over the same embeddings. It also provides a concrete, per-input deployment of zeroth-order optimization, in contrast to prior LLM uses of the finite-difference estimator that treat it as a train-time optimizer over shared parameters.

Real-world applications:

  • Moderating open-ended chat and completion systems that must reduce harmful output without shipping new model weights.
  • Serving third-party or proprietary models through an API, where parameters, activations, and logits are inaccessible — only embeddings, forward calls, and a scoring endpoint are needed.
  • Rapid safety updates: when the definition of acceptable content changes, the toxicity function can be swapped without any retraining cycle.
  • Auditing and red-teaming pipelines that need a lightweight way to suppress toxic continuations from unaligned pretrained checkpoints.

Industry relevance. Because the method operates through standard inference calls, the authors note it integrates with optimized serving stacks such as vLLM, batching the N + 1 evaluations per iteration into a single call. The trade-off is latency: roughly 9 seconds versus about 1 second for RAD, incurred in exchange for requiring no training and no additional learned parameters.

Future Directions

  • Reducing query budget under stochastic decoding. At higher temperatures each evaluation carries more decoding noise; principled variance reduction (for example, multiple completions per evaluation) recovers a usable signal but increases forward-pass cost. Balancing these remains open.
  • Richer objectives and complementary safety signals. The authors suggest that within embedding-space steering, further gains are more likely to come from improved objectives than from new steering rules, implying work on toxicity measures that better capture what users consider harmful.
  • Handling non-monotonic hyperparameter behavior. Because toxicity and perplexity do not vary monotonically with μ or N, there is no clear recipe for selecting them; understanding how the smoothed objective's local minima relate to hyperparameter choice is an open question.
  • Deployment latency. Bringing the iteration cost closer to single-pass reward-based baselines without giving up the training-free property is a practical direction the paper explicitly leaves as a mild limitation.

Target Audience

Researchers and engineers working on LLM safety, alignment, and controllable text generation, particularly those constrained to black-box or API-only access where retraining is impossible. It is also relevant to practitioners of derivative-free and zeroth-order optimization looking for a large-scale, per-instance application, and to ML infrastructure engineers interested in test-time methods that compose with high-throughput inference stacks. Readers should be comfortable with embedding-space representations and stochastic optimization terminology; the experimental protocol and results are otherwise readable without deep optimization background.

Authors’ abstract

Large language models can produce toxic or inappropriate text even for benign inputs, creating risks when deployed at scale. Detoxification is therefore important for safety and user trust, particularly when we want to reduce harmful content without sacrificing the model's generation quality. Many existing approaches rely on model retraining, gradients, or learned auxiliary components, which can be costly and may not transfer across model families or to truly black-box settings. We introduce a test-time procedure that approximates the gradient of completion toxicity with respect to the input embeddings and uses a small number of descent steps to steer generation toward less toxic continuations. This is achieved with zeroth-order optimization that requires only access to input embeddings, a toxicity scoring function, and forward evaluations of the model. Empirically, the approach delivers robust toxicity reductions across models and prompts and, in most settings, achieves the best overall toxicity-quality trade-off. More broadly, our work positions word embeddings as effective control variables and encourages wider use of black-box optimization to guide autoregressive language models toward scalable, safer text generation, without requiring any training or access to intermediate computations.

Read the original paper