Abstract
We consider the problem of generating samples via Flow Matching (FM) with an additional requirement that the generated samples must satisfy given constraints. We consider two scenarios, viz.: (a) when a differentiable distance function to the constraint set is given, and (b) when the constraint set is only available via queries to a membership oracle. For case (a), we propose a simple adaptation of the FM objective with an additional term that penalizes the distance between the constraint set and the generated samples. For case (b), we propose to employ randomization and learn a mean flow that is numerically shown to have a high likelihood of satisfying the constraints. This approach deviates significantly from existing works that require simple convex constraints, knowledge of a barrier function, or a reflection mechanism to constrain the probability flow. Furthermore, in the proposed setting we show that a two-stage approach, where both stages approximate the same original flow but with only the second stage probing the constraints via randomization, is more computationally efficient. Through several synthetic cases of constrained generation, we numerically show that the proposed approaches achieve significant gains in terms of constraint satisfaction while matching the target distributions. As a showcase for a practical oracle-based constraint, we show how our approach can be used for training an adversarial example generator, using queries to a hard-label black-box classifier.
Introduction
Generative models like Flow Matching (FM) have become incredibly good at mimicking complex data distributions. However, in real-world applications, samples often need to satisfy strict, sample-wise constraints (e.g., physical laws or logical boundaries). Standard generative models learn from a finite set of points and often generate samples that slightly violate these constraints because they never observe the entire valid space.
Existing solutions typically require the constraint set to be convex or require explicit knowledge of the boundary's geometry, such as a barrier function. This paper addresses the gap where constraints are non-convex, disconnected, or accessible only via a "black-box" membership oracle that provides no gradients.
Methodology
The authors propose two distinct approaches based on the information available regarding the constraint set $\mathcal{C}$:
1. FM-DD (Differentiable Distance)
If a mathematical function measures the distance $d(\cdot, \mathcal{C})$ from a sample to the constraint set, a penalty term is added to the training objective. This differentiable distance pushes the learned flow toward the valid region during the optimization process.
2. FM-RE (Randomized Exploration)
This is the core innovation for handling "black-box" constraints where only an indicator function (Yes/No oracle) is available. Since the gradient of such a function is zero almost everywhere, standard optimization fails. FM-RE employs:
- Randomization: Noise is injected into the velocity field during training to explore the space around the path.
- Mean Flow Learning: By exploring via randomization, the model learns a "mean flow" that maximizes the probability of ending up in the valid constraint set.
- Two-Stage Efficiency: To optimize computation, the model first learns a standard flow and then refines only the final segment ($t \ge t_0$) using randomized exploration.
Experimental Results
The researchers evaluated their methods across synthetic 2D/High-D benchmarks, MNIST attribute constraints, and black-box adversarial generation tasks.
As illustrated in Figure 1, vanilla FM often generates points outside the target regions (indicated by red dots). In contrast, FM-DD and FM-RE effectively constrain the generation to the valid boxes, even in the non-convex "2 boxes" scenario.
Quantitative results in Figure 4 confirm that FM-DD and FM-RE significantly reduce the probability of constraint violation $\mathbb{P}(X_1 \notin \mathcal{C})$ compared to vanilla FM, while maintaining high distributional accuracy as measured by Sliced Wasserstein Distance (SWD). For example, in adversarial tasks, FM-RE reduced the accuracy of a LeNet-5 model on MNIST from 99.1% to 18.7% using only hard-label queries.
Discussion & Conclusion
FM-RE demonstrates that explicit mathematical descriptions of constraints are not strictly necessary; a queryable oracle is sufficient to guide generative flows. While these methods do not provide 100% hard guarantees like reflection-based methods, they offer a flexible alternative for complex, non-differentiable constraints. Future research will explore optimal randomization start times ($t_0$) and extending these principles to diffusion models.
Code available at: https://github.com/ZhengyanHuan/FM-RE
Summary updated