Discrete Flow Matching
Flow Generative Models and Flow Matching have been shown to be potent options for image and video generation, providing a more stable and smooth alternative to diffusion models. Rather than relying on random noise to form patterns, these models go back to the distribution-based roots of generative models and work to transform simpler distributions to those that encompass any given dataset. This form of generation was picked up by META to make Discrete Flow Matching, a new paradigm of language modeling that has similar strengths to similar diffusion-based language models. For this blog I am going to be explaining Discrete Flow Matching from the ground up in the simplest way possible, although each concept is very math heavy. Due to the reliance of each paper on a mathematical backing and their theoretical nature I won’t be covering any concrete use cases or giving any architectural explanation since the concepts themselves are the main points I want to be taken away from this.
Normalizing Flows:
A key problem in Probability Theory and Machine Learning is how to model complex distributions optimally. This problem arises most often when dealing with real data, which often does not follow valid patterns when complex enough. Variational Inference tackles this task by using simpler distributions, or tractable ones, to model the more complex one as closely as possible. The approximation is done through an optimization, with the exact nature of the optimization and the form in which the simpler distribution is represented being the key differences between different forms of Variational Inference.
The method of Normalizing Flows handles this approximation through a sequence of invertible transformations that turn the tractable distribution into the complex one. The simpler distribution is said to be the initial density which “flows” through the sequence of mappings to reach the desired distribution. When an invertible smooth transformation is used to transform a random variable with , its distribution can also be mapped. This relationship is shown below with a Jacobian change of variables (where the inverse jacobian is used since the adjustment in density depends on the inverse effect of the transformation).
This relationship can then be extended to map a relationship between and its distribution to some distribution through a series of transformations . The path traversed by the random variables with initial distribution is called the flow and the path formed by the successive transformations is called the normalizing flow. This is shown below in log space for numerical stability.
This method, specifically called a Finite Normalizing Flow, gives the flow the property of the law of unconscious statistician, where the expectations with respect to can be computed without knowing explicitly given an accurate choice of transformations. This allows information about the complex distributions to be approximated with the simpler one and the defined transformations.
Flow Generative Models:
The goal of most generative models is to model a distribution rather than a function. These objectives may sound very similar, and they are, but the difference comes in the structure inherent to distributions. Having that continuous structure allows the model to generate data that doesn’t match exactly with the function it has learned but is close to it, allowing it to generate new data.
Flow Generative Models use the concept of distribution modeling and normalizing flows to model complex distributions and generate new images and data. Given some unknown distribution , a simpler distribution , and a set of transformations , a Flow Generative Model trains to maximize the log likelihood of the observed data (a system that penalizes the model for deviating from the unknown distribution).
Flow Matching:
Another type of Normalizing Flows come in the form of Continuous Normalizing Flows. Instead of modeling the flow between the simpler distribution to the target one with a finite set of functions, a continuous normalizing flow represents the flow with a differential equation. The flow is defined in a differential equation with some vector field .
The vector field is modeled as a neural network with parameters . Given the form of the model the form of the flow can be derived by solving the differential equation and can be used to transform some initial probability density to a target density . This is done with the same change of variables operation being represented as .
Flow Matching uses this definition to model a flow generative model. The model trains to approximate some target probability path , where with being the distribution of the data, that has a corresponding vector field . Since both probability paths can be derived directly from their respective vector fields, in order to simplify the calculations the vector fields themselves are compared instead of the paths.
Since the exact nature of the dataset’s distribution is unknown, and have to be approximated from the known pieces of data. This is done by splitting both of them into paths, each of which correspond to a conditional probability. Each is the conditional probability path scaled by the original probability , where is the latent state at time and is some ground truth datapoint of the dataset. These are defined with boundary rules where starts from some simple defined distribution and is concentrated around . For a typical image dataset, this could theoretically mean that at could be pure noise and at meaning it should be almost the original image.
This definition allows the path to be estimated through sampling the dataset, instead of needing to know the distribution outright. It also allows a better form for to be derived through the above definition and a simplification with Bayes Theorem.
This still leaves a problem within the model that forms intractable integrals. Since the original loss requires the integrated form to solve for , every single part of the dataset needs to be integrated to form with this setup, making it intractable. This is solved within the loss by simply using the conditional form of instead with (from the dataset) and (from the conditional probability path), which is proven to have the same optima as the original, therefore being able to train to the same model.
Base Example:
The definition for Flow Matching allows for any choice of conditional probability path and conditional vector fields, but the example provided by the paper is the simplest option available, modeling them as a Gaussian Distribution. The parameters of the distribution and are made to be time-dependent and are used to model the desird probability path. At , and so that and at , and , where is set low enough so that is concentrated and centered at . This creates a path that transforms some simpler datapoint into a more complex datapoint within the desired distribution.
An infinite number of vector fields for any given probability path can be derived, but the simplest is chosen within the paper as an example. The below definition uses the same and from the CFM loss function and is conditioned on .
This flow can then derive the vector field that generates the conditional probability path in terms of and , which then acts as the comparison for training .
The CFM loss can then be derived and simplified by reparameterizing of , the datapoint drawn from the simpler distribution that acts as the initial state of the flow.
Discrete Flow Matching:
Discrete Flow Matching extends the format for generation show in Flow Matching to a discrete space, which is not as simple as it is for diffusion due to the nature of Flow Matching as something that follows a function. A lot of syntax and new definitions that differ from the original Flow Matching description are required to understand the explanation of the model. The model aims to model some sequence of tokens of elements each of size . As well is used to denote the entire set of possible sequences , where . A random variable in the space of is denoted by which has a PMF , sometimes denoted as .
For marginalization (representing the distribution in terms of only one variable), represents the marginal probability for , with the complement of being shown as , which is all the arguments excluding . These definitions are used to define a PMF to compare sequences and as well in the form of a delta function , where only if . This is given shorthand and .
The source samples and target samples are drawn from a joint distribution in which describes the probability of observing a pair . This is defined where (the marginal over recovers the source distribution), (the marginal over recovers the target distribution), and in the simplest case (both parts are drawn independently from their distributions). The probability path of the dataset’s distribution can then be defined in the most general case with the following.
This is then split up into conditional probability paths just like in Flow Matching with a sum of conditional probabilities . This is combined below with a scheduler , which defines how much the path should affect a certain token, where and . The rest is left up to choice and the scheduler can be defined independently for each token or uniformly across them all.
In order to fit Flow Matching into a discrete space, a Continuous-Time Discrete Markov Chain, a structure that describes how some sample jumps between states of depending on a time , is used. This then changes the evolution from a differential equation to the following form factor, where follows the same importance that the vector fields did in the original format.
The value of for the dataset’s distribution can be derived simply with a set of conditional probabilities. The definition is left open with two restrictions being that and have to apply. The value of can also be derived through Bayes Theorem, which will hold importance later.
The value of for the model’s generation distribution can also be derived, although in a much different manner. The model has weights which are controlled by (controls transitions between states) and (ensures normalization), where (selects the dominant transition).
The path of can be described with the posteriors , meaning that it accurately models the transitions being predicted by itself. This allows for training to occur solely within , which is also required since the nature of the derivation of renders it intractable for training. During inference is used to improve the results of the model.
The loss is formulated similarly to Cross-Entropy and measures how closely the prediction of matches up to the ground truth (defined below where , which represents some intermediate noisy step, and , which represents the ground truth) which was derived above for the probability paths. The ground truth defines how the data has shifted during some given timestep , which is up to the model itself to decide (with random noise, deterministically, etc.).
The paper goes into many of the almost infinite possibilities that the structure defined here provides which will not be covered here, although I do highly recommend seeing them for further clarification.
Conclusion:
As covered in my previous blogpost, Diffusion Models are finally making a splash in the language modeling market with the release of Gemini Diffusion from Google. This opens up the gateway for future innovation in such a new field which I consider to have limitless potential. The strengths inherent to diffusion language models however are not unique to diffusion itself, but rather the structure inherent to any form of generative model, which all can parallelize and output information freely in any order they want. This opens up the possibility of other formats of generation to be even more impactful to language generation than diffusion, which is the reason behind Discrete Flow Matching even existing. The open-ended nature and even the mere existence of the modality offers limitless opportunity to make something new, something that may be the next big innovation.