HardFlow: Hard-Constrained Sampling for Flow-Matching Models via Trajectory Optimization

Zeyang Li, Kaveh Alim, Navid Azizan · 2025

Read HardFlow: Hard-Constrained Sampling for Flow-Matching Models via Trajectory Optimization by Zeyang Li, Kaveh Alim, Navid Azizan (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2511-08425.txt.

Introduction

Zeyang Li, Kaveh Alim, and Navid Azizan introduce HardFlow, a method for making flow matching generative models obey hard constraints on their outputs. Diffusion and flow models generate richly varied samples, but applications like robot motion demand exact constraint satisfaction, such as never hitting an obstacle. Projecting the whole sampling path onto the constraint set is overly restrictive and damages sample quality, the authors argue.

HardFlow instead recasts constrained sampling as trajectory optimization, steering the path with numerical optimal control so constraints hold precisely at the final time. Model predictive control ideas turn the problem into an efficient surrogate problem. Extra integral and terminal costs can reduce distribution shift and raise quality inside the same framework.

Catalog record

CREATORZeyang Li, Kaveh Alim, Navid Azizan
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE89 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2511-08425

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

HardFlow: Hard-Constrained Sampling for Flow-Matching Models via Trajectory Optimization Thanks: Corresponding author: Zeyang Li ([email protected]). Zeyang Li, Kaveh Alim, Navid Azizan Abstract Diffusion and flow-matching have emerged as powerful methodologies for generative modeling, with remarkable success in capturing complex data distributions and enabling flexible guidance at inference time. Many downstream applications, however, demand enforcing hard constraints on generated samples—for example, robot trajectories must avoid obstacles—a requirement that goes beyond simple guidance. Prevailing projection-based approaches constrain the entire sampling path to the constraint manifold, which is overly restrictive and degrades sample quality. In this paper, we introduce a novel framework that reformulates hard-constrained sampling as a trajectory optimization problem. Our key insight is to leverage numerical optimal control to steer the sampling trajectory so that constraints are satisfied precisely at the terminal time. By exploiting the underlying structure of flow-matching models and adopting techniques from model predictive control, we transform this otherwise complex constrained optimization problem into a tractable surrogate that can be solved efficiently and effectively. Furthermore, this trajectory optimization perspective offers significant flexibility beyond mere constraint satisfaction, allowing for the inclusion of integral costs to minimize distribution shift and terminal objectives to further enhance sample quality, all within a unified framework. We provide a control-theoretic analysis of our method, establishing bounds on the approximation error between our tractable surrogate and the ideal formulation. Extensive experiments across diverse domains, including robotics (planning), partial differential equations (boundary control), and vision (text-guided image editing), demonstrate that our algorithm, which we name HardFlow , substantially outperforms existing methods in both constraint satisfaction and sample quality. I Introduction Diffusion [ 1 , 2 ] and Flow-matching [ 3 ] have revolutionized generative modeling, achieving tremendous performance across diverse domains, including image synthesis [ 4 , 5 ] , video generation [ 6 , 7 ] , molecule design [ 8 ] , and robotics [ 9 , 10 ] . At their core, these methods learn a time-varying “dynamics” (e.g., score function, velocity field) that transports samples from a simple prior distribution (e.g., Gaussian noise) to a complex target data distribution. The training process is notably efficient, leveraging conditional probability paths to formulate a conditional loss that enables simulation-free learning. During inference, samples are drawn from the simple prior and then evolved along trajectories defined by the learned dynamics, a process governed by an ordinary differential equation (ODE) or a stochastic differential equation (SDE). Terminal states of the trajectories constitute samples from the target distribution. A key feature of these models is their flexibility, which allows for guidance during inference to steer samples toward regions of interest, typically by injecting the gradient of a differentiable objective into the learned dynamics [ 11 , 12 ] . While gradient-based guidance is effective for encouraging desirable soft attributes, many practical applications present a more stringent challenge: enforcing hard constraints, i.e., inviolable conditions that a sample must satisfy to be considered valid. For example, in robotic planning, a proposed trajectory must be collision-free or it may lead to catastrophic failures; in facial-expression image editing, the subject’s identity should remain invariant as the expression changes. Moreover, recent work has also highlighted the utility of explicit hard constraints as an effective mechanism to prevent reward overoptimization in text-to-image diffusion models [ 13 ] . To date, the predominant approach to hard-constrained sampling for diffusion and flow-matching models has been projection-based methods and their variants [ 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 ] . The most straightforward application of projection is a single, post-hoc correction where the final generated sample is projected onto the feasible set [ 14 ] . This simple approach, however, can often induce a significant shift away from the target data distribution [ 15 ] . To mitigate this, common refinements involve applying a projection within the sampling process. One prominent strategy is to project iteratively at each step, enforcing feasibility throughout the entire generation path [ 15 , 20 , 21 , 16 ] . An alternative method is to ignore or relax the projections in early sampling steps, motivated by the insight that early-stage samples resemble noise and that tightly constraining these noisy precursors can be detrimental to final sample quality [ 22 , 17 , 18 , 19 ] . Despite some empirical success, projections during sampling suffer from several fundamental limitations. First, these methods are intrinsically conservative as they enforce pathwise feasibility, a condition that is unnecessarily restrictive since only the terminal state (i.e., the sample of interest) must satisfy the constraint. The intermediate iterates are more of algorithmic artifacts. By imposing constraints on these transient states, projection during sampling prematurely prunes the search space, which can prevent the generative process from discovering higher-quality solutions that would otherwise be accessible. Although per-step projection can be interpreted as projected gradient ascent on the data log-likelihood [ 15 ] , this view relies on Langevin-style sampling with specific noise and step-size schedules and does not generally extend to arbitrary SDE discretizations or the ODE-based samplers used in flow matching. Second, these projection-based methods solely address constraint satisfaction and are not designed to optimize a reward function. In practical applications, however, it is often desirable not only to enforce hard constraints but also to optimize rewards to improve sample quality. An ideal method should jointly address both aspects and produce feasible samples of high quality. To address the aforementioned challenges, we propose a novel framework that reformulates hard-constrained sampling as a trajectory optimization problem. While in this work we focus our formulation and analysis on ODE sampling for flow-matching models, the methodology is applicable to diffusion models under ODE sampling as well, such as denoising diffusion implicit models (DDIM) [ 23 ] . We consider perturbations to the learned velocity field as control inputs to steer the sampling trajectory. Constraints are then imposed solely on the terminal state, providing a minimally invasive correction that ensures the final sample lies within the feasible set without unnecessarily constraining the path. Furthermore, this framework naturally accommodates the inclusion of auxiliary objectives. We consider two primary types: (i) integral costs that penalize control effort to minimize the distributional shift from the uncontrolled sampler, and (ii) terminal costs designed to promote desirable attributes in the final sample. The trajectory optimization perspective thus offers a principled and unified framework that jointly addresses constraint satisfaction, distribution consistency, and sample quality. We note that prior work has connected diffusion and flow-matching models with optimal control for training-free guidance [ 24 , 25 ] . However, these approaches focus exclusively on guiding the samples toward high-reward regions and do not address hard constraints. Methodologically, they are grounded in Pontryagin’s Maximum Principle (PMP) [ 26 ] , the cornerstone of indirect methods in optimal control [ 27 ] . This class of techniques is notoriously difficult to apply to problems with state constraints, thereby hindering the extension of [ 24 , 25 ] to hard-constrained sampling. In contrast, our work adopts direct methods [ 27 ] such as collocation [ 28 ] to discretize the dynamics and formulate a constrained optimization problem, enabling the explicit enforcement of hard constraints. Nonetheless, the optimization problem is

Read the complete text