HardFlow: AI that knows its limits

MIT’s HardFlow algorithm helps generative AI meet strict safety requirements

HardFlow: AI that knows its limits 

Generative AI models are remarkably good at producing answers that are “pretty close” to correct. But in high-stakes settings – a robot navigating a crowded factory floor, an industrial control system, or another physical system – “pretty close” isn’t good enough. A near-miss path planned for a robot can still result in a collision – a risk vividly illustrated just weeks earlier at China’s 2026 World Humanoid Robot Games, where androids repeatedly failed to decelerate safely, crashing into barriers. 

Researchers at MIT have introduced a new algorithm, called “HardFlow”, that tackles this problem directly. It gives generative models greater freedom to explore possible solutions during generation while requiring the final output to satisfy strict, non-negotiable safety, physical, or task-specific constraints.

Diffusion models like Stable Diffusion and flow-matching models like FLUX have become powerful tools for generating solutions to increasingly complex problems. But while these models can produce outputs that come close to satisfying a request, a nearly correct answer may not be acceptable when violating a constraint could have serious consequences.

The standard approach has been projection-based sampling, which repeatedly forces a model’s partial, intermediate outputs to satisfy constraints throughout the generation process. The problem is that constraining every step can prevent the model from reaching a better final answer. These methods can also focus primarily on satisfying constraints, leaving less room to optimize other qualities of a solution – such as the efficiency of a robot’s trajectory or the quality of an edited image.

HardFlow takes a different approach. Since the intermediate steps are discarded once generation is complete, the researchers reasoned that it is the final output that ultimately needs to satisfy the hard constraints. Giving the model more freedom during the earlier stages allows it to explore a broader range of possibilities before arriving at a feasible solution

HardFlow reframes constrained generation using ideas from optimal control theory, treating the process as a trajectory-optimization problem. Instead of repeatedly forcing intermediate results into compliance, the algorithm makes targeted corrections to the model’s sampling trajectory so that it moves toward a solution that satisfies the required constraints.

Solving such an optimization problem around a large neural network is challenging. The researchers addressed this by exploiting the internal structure of flow-matching models and breaking the overall problem into a sequence of smaller, single-step optimization problems. This allowed them to develop an algorithm that can operate at deployment time without retraining the underlying generative model.

The optimization-based formulation also allows HardFlow to pursue additional objectives alongside constraint satisfaction. For example, a robotic system could be required not only to avoid obstacles but also to find an efficient path to its destination. Rather than simply finding any path that meets the safety requirements, the system can optimize among feasible solutions.

The researchers tested HardFlow on robotic manipulation, maze navigation, and text-guided image editing. MIT reports that the method achieved perfect constraint satisfaction across the experiments while outperforming the baseline approaches on measures of solution quality. In robotic manipulation experiments, for example, HardFlow generated paths that avoided obstacles while also improving the efficiency of reaching the target. In image-editing experiments, the method could perform requested edits while better preserving unaffected parts of the image. Its computational cost was also comparable to, or lower than, most of the methods used for comparison.

One of the method’s practical advantages is that it works at deployment time on pretrained models, meaning the underlying AI does not have to be retrained whenever the approach is applied to a constrained generation problem. This could make constrained generative models easier to adapt to applications where safety and physical requirements are difficult to compromise.

The potential applications extend beyond image generation. The MIT researchers point to areas including robotics, control of physical systems, and computer vision, where generated solutions may need to satisfy requirements that cannot be treated as optional.