SAFREE: Training-Free and Adaptive Guard for Safe Text-to-Image And Video Generation

Jaehong Yoon, Shoubin Yu, Vaidehi Patil, Huaxiu Yao, Mohit Bansal

TL;DR

SAFREE is a training-free, plug-and-play safeguard for text-to-image (T2I) and text-to-video (T2V) diffusion models that prevents the generation of unsafe content without altering model weights. By combining orthogonal token projection in the text embedding space with adaptive re-attention in the visual latent space, it effectively suppresses toxic concepts while preserving overall generation quality and prompt coherence.

What Problem Does This Paper Address?

State-of-the-art diffusion models can inadvertently generate harmful content, such as explicit imagery, violence, or copyrighted artist styles. Existing safety mechanisms generally fall into two camps: training-based methods (like unlearning or model editing) and training-free filtering.

Training-based approaches are computationally expensive, require curated datasets, and permanently alter model weights, which often degrades general image quality for benign prompts. On the other hand, existing training-free methods (like hard token deletion or negative guidance) are brittle against adversarial attacks and can cause severe distribution shifts, ruining the output quality even for the safe parts of a prompt.

To bridge this gap, the authors introduce SAFREE (Training-Free and Adaptive Guard), an inference-time guardrail designed to maintain both safety and generation fidelity across diverse architectures and modalities.

Methods

SAFREE operates jointly across both the textual embedding and visual latent spaces during the generation process:

Figure 2: SAFREE framework. Based on proximity analysis between the masked token embed- dings and the toxic subspace C, we detect unsafe tokens and project them into orthogonal to the toxic concept (in red), but still be in the input space I (in green). SAFREE adaptively controls the filtering strength in an input-dependent manner, which also regulates a latent-level re-attention mechanism. Note that our approach can be broadly applied to various image and video diffusion backbones.
  1. Toxic Token Detection & Orthogonal Projection: The method defines a toxic concept subspace ($\mathcal{C}$) using embeddings of user-defined unsafe keywords. By masking tokens individually, SAFREE calculates each token's conceptual proximity to $\mathcal{C}$. Instead of deleting toxic tokens—which breaks prompt syntax—SAFREE projects them orthogonally to the toxic subspace while keeping them within the valid input embedding space ($\mathcal{I}$).
  2. Self-Validating Filtering: Not all prompts require the same level of intervention. SAFREE dynamically calculates a threshold based on the cosine distance between the original and projected embeddings. This threshold automatically determines the exact number of denoising timesteps where the safety filter should remain active.
  3. Adaptive Latent Re-attention: Because toxic concepts can still manifest at the pixel level, SAFREE applies a spectral transformation (FFT) to the latent features during denoising. It selectively attenuates low-frequency components associated with the toxic prompt, ensuring the global layout remains safe without causing texture oversmoothing.

Datasets

The authors evaluated SAFREE across a comprehensive suite of benchmarks:

  • T2I Safety Benchmarks: Tested against adversarial prompts from I2P, P4D, Ring-A-Bell, MMA-Diffusion, and UnlearnDiff.
  • General Quality: Evaluated on COCO-30k using FID, CLIP score, and TIFA to ensure benign prompts are not degraded.
  • Concept/Style Removal: Evaluated on two artist datasets: Famous Artists (e.g., Van Gogh, Picasso) and Modern Artists (e.g., Kelly McKernan).
  • T2V Safety Benchmark: Evaluated on the SafeSora dataset across categories like violence, terrorism, racism, sexual content, and animal abuse.

Key Insights and Ideas

  • Orthogonal Projection over Deletion: Deleting or replacing tokens destroys prompt syntax and degrades image quality. Projecting toxic tokens into an orthogonal subspace neutralizes the harm while maintaining semantic context.
  • Denoising Steps Matter Unequally: Not all diffusion steps contribute equally to toxicity. SAFREE’s self-validating mechanism ensures that the filter is only active during the crucial early denoising steps for problematic prompts, leaving benign prompts completely untouched.
  • Multi-Space Defense: Text-space filtering alone is vulnerable to adversarial bypasses. Combining text embedding steering with latent pixel-level re-attention provides a much more robust defense.

Key Results

Figure 1: We present SAFREE, an adaptive, training-free method for T2I that filters out a variety of user- defined concepts. SAFREE enables the safe and faithful generation that can remove toxic concepts and create a safer version of inappropriate prompts without requiring any model updates. SAFREE is also versatile and adaptable, enabling its application to other backbones (such as Diffusion Transformer) and across different applications (like T2V) for enhanced safe generation. Fire icon: training/editing-based methods that alter model weights. Snowflake icon: training-free methods with no weights updating. We manually masked/blurred sensitive text prompts and generated results for display purposes.
  • SOTA Safety Performance: SAFREE reduced the Attack Success Rate (ASR) of unsafe content by 22% across 5 T2I datasets compared to other training-free methods. It demonstrated 47%, 13%, and 34% lower ASR than the best-performing baselines on I2P, MMA-diffusion, and UnlearnDiff, respectively.
  • Zero Training Overhead: Unlike methods like ESD or CA which require hundreds or thousands of seconds of fine-tuning, SAFREE requires 0 seconds of training/editing time and introduces negligible inference overhead.
  • Excellent Generalization: SAFREE seamlessly plugs into diverse backbones, including traditional UNet-based models (SD-v1.4, SDXL, ZeroScopeT2V) and modern Diffusion Transformers (SD-v3, CogVideoX-5B).

Limitations and Future Work

The authors note that SAFREE is not a perfect shield. It exhibits limitations when dealing with highly implicit, nuanced, or "chain-of-thought" style adversarial prompts. If the toxic intent is buried deep within complex reasoning rather than explicit keywords, it can evade the initial subspace proximity detection. Future work will likely focus on enhancing the text-side detection layer to handle these highly structured jailbreak attempts.

Summary updated

Abstract

Recent advances in diffusion models have significantly enhanced their ability to generate high-quality images and videos, but they have also increased the risk of producing unsafe content. Existing unlearning/editing-based methods for safe generation remove harmful concepts from models but face several challenges: (1) They cannot instantly remove harmful concepts without training. (2) Their safe generation capabilities depend on collected training data. (3) They alter model weights, risking degradation in quality for content unrelated to toxic concepts. To address these, we propose SAFREE, a novel, training-free approach for safe T2I and T2V, that does not alter the model's weights. Specifically, we detect a subspace corresponding to a set of toxic concepts in the text embedding space and steer prompt embeddings away from this subspace, thereby filtering out harmful content while preserving intended semantics. To balance the trade-off between filtering toxicity and preserving safe concepts, SAFREE incorporates a novel self-validating filtering mechanism that dynamically adjusts the denoising steps when applying the filtered embeddings. Additionally, we incorporate adaptive re-attention mechanisms within the diffusion latent space to selectively diminish the influence of features related to toxic concepts at the pixel level. In the end, SAFREE ensures coherent safety checking, preserving the fidelity, quality, and safety of the output. SAFREE achieves SOTA performance in suppressing unsafe content in T2I generation compared to training-free baselines and effectively filters targeted concepts while maintaining high-quality images. It also shows competitive results against training-based methods. We extend SAFREE to various T2I backbones and T2V tasks, showcasing its flexibility and generalization. SAFREE provides a robust and adaptable safeguard for ensuring safe visual generation.
BibTeX
@inproceedings{yoon2025safree,
  author = {Jaehong Yoon and Shoubin Yu and Vaidehi Patil and Huaxiu Yao and Mohit Bansal},
  title = {{SAFREE: Training-Free and Adaptive Guard for Safe Text-to-Image And Video Generation}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=hgTFotBRKl},
}
Figure