Discrete flow matching
DiscreteFlowMatcher
Bases: Interpolant
A Discrete Flow Model (DFM) interpolant.
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
__init__(time_distribution, prior_distribution, device='cpu', eps=1e-05, rng_generator=None)
Initialize the DFM interpolant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_distribution
|
TimeDistribution
|
The time distribution for the diffusion process. |
required |
prior_distribution
|
DiscretePriorDistribution
|
The prior distribution for the discrete masked tokens. |
required |
device
|
str
|
The device to use for computations. Defaults to "cpu". |
'cpu'
|
eps
|
Float
|
small Float to prevent dividing by zero. |
1e-05
|
rng_generator
|
Optional[Generator]
|
An optional :class: |
None
|
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
interpolate(data, t, noise)
Get x(t) with given time t from noise and data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
target discrete ids |
required |
t
|
Tensor
|
time |
required |
noise
|
Tensor
|
tensor noise ids |
required |
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
loss(logits, target, time=None, mask=None, use_weight=False)
Calculate the cross-entropy loss between the model prediction and the target output.
The loss is calculated between the batch x node x class logits and the target batch x node. If using a masked prior please pass in the correct mask to calculate loss values on only masked states. i.e. mask = data_mask * is_masked_state which is calculated with self.prior_dist.is_masked(xt))
If use_weight
is True, the loss is weighted by 1/(1-t) defined in equation 24 in Appndix C. of https://arxiv.org/pdf/2402.04997
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
The predicted output from the model, with shape batch x node x class. |
required |
target
|
Tensor
|
The target output for the model prediction, with shape batch x node. |
required |
time
|
Tensor
|
The time at which the loss is calculated. |
None
|
mask
|
Optional[Tensor]
|
The mask for the data point. Defaults to None. |
None
|
use_weight
|
bool
|
Whether to use the DFM time weight for the loss. Defaults to True. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The calculated loss batch tensor. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
step(logits, t, xt, dt, temperature=1.0, stochasticity=1.0)
Perform a single step of DFM euler updates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
The input logits. |
required |
t
|
Tensor
|
The current time step. |
required |
xt
|
Tensor
|
The current state. |
required |
dt
|
Tensor | float
|
The time step increment. |
required |
temperature
|
Float
|
The temperature for the softmax calculation. Defaults to 1.0. |
1.0
|
stochasticity
|
Float
|
The stochasticity value for the step calculation. Defaults to 1.0. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The updated state. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
step_argmax(model_out)
Returns the index of the maximum value in the last dimension of the model output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model. |
required |
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
319 320 321 322 323 324 325 326 327 328 |
|
step_purity(logits, t, xt, dt, temperature=1.0, stochasticity=1.0)
Perform a single step of purity sampling.
https://github.com/jasonkyuyim/multiflow/blob/6278899970523bad29953047e7a42b32a41dc813/multiflow/data/interpolant.py#L346 Here's a high-level overview of what the function does: TODO: check if the -1e9 and 1e-9 are small enough or using torch.inf would be better
- Preprocessing: Checks if dt is a float and converts it to a tensor if necessary. Pads t and dt to match the shape of xt. Checks if the mask_index is valid (i.e., within the range of possible discrete values).
- Masking: Sets the logits corresponding to the mask_index to a low value (-1e9) to effectively mask out those values. Computes the softmax probabilities of the logits. Sets the probability of the mask_index to a small value (1e-9) to avoid numerical issues. 3.Purity sampling: Computes the maximum log probabilities of the softmax distribution. Computes the indices of the top-number_to_unmask samples with the highest log probabilities. Uses these indices to sample new values from the original distribution.
- Unmasking and updating: Creates a mask to select the top-number_to_unmask samples. Uses this mask to update the current state xt with the new samples.
- Re-masking: Generates a new mask to randomly re-mask some of the updated samples. Applies this mask to the updated state xt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
The input logits. |
required |
t
|
Tensor
|
The current time step. |
required |
xt
|
Tensor
|
The current state. |
required |
dt
|
Tensor
|
The time step increment. |
required |
temperature
|
Float
|
The temperature for the softmax calculation. Defaults to 1.0. |
1.0
|
stochasticity
|
Float
|
The stochasticity value for the step calculation. Defaults to 1.0. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The updated state. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
step_simple_sample(model_out, temperature=1.0, num_samples=1)
Samples from the model output logits. Leads to more diversity than step_argmax.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model. |
required |
temperature
|
Float
|
The temperature for the softmax calculation. Defaults to 1.0. |
1.0
|
num_samples
|
int
|
Number of samples to return |
1
|
Source code in bionemo/moco/interpolants/continuous_time/discrete/discrete_flow_matching.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|