Continuous flow matching
ContinuousFlowMatcher
Bases: Interpolant
A Continuous Flow Matching interpolant.
Examples:
>>> import torch
>>> from bionemo.moco.distributions.prior.continuous.gaussian import GaussianPrior
>>> from bionemo.moco.distributions.time.uniform import UniformTimeDistribution
>>> from bionemo.moco.interpolants.continuous_time.continuous.continuous_flow_matching import ContinuousFlowMatcher
>>> from bionemo.moco.schedules.inference_time_schedules import LinearInferenceSchedule
flow_matcher = ContinuousFlowMatcher(
time_distribution = UniformTimeDistribution(...),
prior_distribution = GaussianPrior(...),
)
model = Model(...)
# Training
for epoch in range(1000):
data = data_loader.get(...)
time = flow_matcher.sample_time(batch_size)
noise = flow_matcher.sample_prior(data.shape)
data, time, noise = flow_matcher.apply_ot(noise, data) # Optional, only for OT
xt = flow_matcher.interpolate(data, time, noise)
flow = flow_matcher.calculate_target(data, noise)
u_pred = model(xt, time)
loss = flow_matcher.loss(u_pred, flow)
loss.backward()
# Generation
x_pred = flow_matcher.sample_prior(data.shape)
inference_sched = LinearInferenceSchedule(...)
for t in inference_sched.generate_schedule():
time = inference_sched.pad_time(x_pred.shape[0], t)
u_hat = model(x_pred, time)
x_pred = flow_matcher.step(u_hat, x_pred, time)
return x_pred
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
|
__init__(time_distribution, prior_distribution, prediction_type=PredictionType.DATA, sigma=0, ot_type=None, ot_num_threads=1, data_scale=1.0, device='cpu', rng_generator=None, eps=1e-05)
Initializes the Continuous Flow Matching interpolant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_distribution
|
TimeDistribution
|
The distribution of time steps, used to sample time points for the diffusion process. |
required |
prior_distribution
|
PriorDistribution
|
The prior distribution of the variable, used as the starting point for the diffusion process. |
required |
prediction_type
|
PredictionType
|
The type of prediction, either "flow" or another type. Defaults to PredictionType.DATA. |
DATA
|
sigma
|
Float
|
The standard deviation of the Gaussian noise added to the interpolated data. Defaults to 0. |
0
|
ot_type
|
Optional[Union[OptimalTransportType, str]]
|
The type of optimal transport, if applicable. Defaults to None. |
None
|
ot_num_threads
|
int
|
Number of threads to use for OT solver. If "max", uses the maximum number of threads. Default is 1. |
1
|
data_scale
|
Float
|
The scale factor for the data. Defaults to 1.0. |
1.0
|
device
|
Union[str, device]
|
The device on which to run the interpolant, either "cpu" or a CUDA device (e.g. "cuda:0"). Defaults to "cpu". |
'cpu'
|
rng_generator
|
Optional[Generator]
|
An optional :class: |
None
|
eps
|
Float
|
Small float to prevent divide by zero |
1e-05
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 |
|
apply_ot(x0, x1, mask=None, **kwargs)
Sample and apply the optimal transport plan between batched (and masked) x0 and x1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x0
|
Tensor
|
shape (bs, *dim), noise from source minibatch. |
required |
x1
|
Tensor
|
shape (bs, *dim), data from source minibatch. |
required |
mask
|
Optional[Tensor]
|
mask to apply to the output, shape (batchsize, nodes), if not provided no mask is applied. Defaults to None. |
None
|
**kwargs
|
Additional keyword arguments to be passed to self.ot_sampler.apply_ot or handled within this method. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
Tuple |
tuple
|
tuple of 2 tensors, represents the noise and data samples following OT plan pi. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
calculate_target(data, noise, mask=None)
Get the target vector field at time t.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
noise
|
Tensor
|
noise from prior(), shape (batchsize, nodes, features) |
required |
data
|
Tensor
|
target, shape (batchsize, nodes, features) |
required |
mask
|
Optional[Tensor]
|
mask to apply to the output, shape (batchsize, nodes), if not provided no mask is applied. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The target vector field at time t. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
get_gt(t, mode='tan', param=1.0, clamp_val=None, eps=0.01)
From Geffner et al. Computes gt for different modes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
Tensor
|
times where we'll evaluate, covers [0, 1), shape [nsteps] |
required |
mode
|
str
|
"us" or "tan" |
'tan'
|
param
|
float
|
parameterized transformation |
1.0
|
clamp_val
|
Optional[float]
|
value to clamp gt, no clamping if None |
None
|
eps
|
float
|
small value leave as it is |
0.01
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
|
interpolate(data, t, noise)
Get x_t with given time t from noise (x_0) and data (x_1).
Currently, we use the linear interpolation as defined in: 1. Rectified flow: https://arxiv.org/abs/2209.03003. 2. Conditional flow matching: https://arxiv.org/abs/2210.02747 (called conditional optimal transport).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
noise
|
Tensor
|
noise from prior(), shape (batchsize, nodes, features) |
required |
t
|
Tensor
|
time, shape (batchsize) |
required |
data
|
Tensor
|
target, shape (batchsize, nodes, features) |
required |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
loss(model_pred, target, t=None, xt=None, mask=None, target_type=PredictionType.DATA)
Calculate the loss given the model prediction, data sample, time, and mask.
If target_type is FLOW loss = ||v_hat - (x1-x0)||2 If target_type is DATA loss = ||x1_hat - x1||2 * 1 / (1 - t)**2 as the target vector field = x1 - x0 = (1/(1-t)) * x1 - xt where xt = tx1 - (1-t)x0. This functions supports any cominbation of prediction_type and target_type in {DATA, FLOW}.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_pred
|
Tensor
|
The predicted output from the model. |
required |
target
|
Tensor
|
The target output for the model prediction. |
required |
t
|
Optional[Tensor]
|
The time for the model prediction. Defaults to None. |
None
|
xt
|
Optional[Tensor]
|
The interpolated data. Defaults to None. |
None
|
mask
|
Optional[Tensor]
|
The mask for the data point. Defaults to None. |
None
|
target_type
|
PredictionType
|
The type of the target output. Defaults to PredictionType.DATA. |
DATA
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The calculated loss batch tensor. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
process_data_prediction(model_output, xt=None, t=None, mask=None)
Process the model output based on the prediction type to generate clean data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output
|
Tensor
|
The output of the model. |
required |
xt
|
Tensor
|
The input sample. |
None
|
t
|
Tensor
|
The time step. |
None
|
mask
|
Optional[Tensor]
|
An optional mask to apply to the model output. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
The data prediction based on the prediction type. |
Raises:
Type | Description |
---|---|
ValueError
|
If the prediction type is not "flow". |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 |
|
process_vector_field_prediction(model_output, xt=None, t=None, mask=None)
Process the model output based on the prediction type to calculate vecotr field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output
|
Tensor
|
The output of the model. |
required |
xt
|
Tensor
|
The input sample. |
None
|
t
|
Tensor
|
The time step. |
None
|
mask
|
Optional[Tensor]
|
An optional mask to apply to the model output. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
The vector field prediction based on the prediction type. |
Raises:
Type | Description |
---|---|
ValueError
|
If the prediction type is not "flow" or "data". |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 |
|
scale_data(data)
Upscale the input data by the data scale factor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
The input data to upscale. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The upscaled data. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
156 157 158 159 160 161 162 163 164 165 |
|
step(model_out, xt, dt, t=None, mask=None, center=False)
Perform a single ODE step integration using Euler method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model at the current time step. |
required |
xt
|
Tensor
|
The current intermediate state. |
required |
dt
|
Tensor
|
The time step size. |
required |
t
|
Tensor
|
The current time. Defaults to None. |
None
|
mask
|
Optional[Tensor]
|
A mask to apply to the model output. Defaults to None. |
None
|
center
|
Bool
|
Whether to center the output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
x_next |
Tensor
|
The updated state of the system after the single step, x_(t+dt). |
Notes:
- If a mask is provided, it is applied element-wise to the model output before scaling.
- The clean
method is called on the updated state before it is returned.
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 |
|
step_score_stochastic(model_out, xt, dt, t, mask=None, gt_mode='tan', gt_p=1.0, gt_clamp=None, score_temperature=1.0, noise_temperature=1.0, t_lim_ode=0.99, center=False)
Perform a single ODE step integration using Euler method.
d x_t = [v(x_t, t) + g(t) * s(x_t, t) * sc_score_scale] dt + \sqrt{2 * g(t) * temperature} dw_t.
At the moment we do not scale the vector field v but this can be added with sc_score_scale.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model at the current time step. |
required |
xt
|
Tensor
|
The current intermediate state. |
required |
dt
|
Tensor
|
The time step size. |
required |
t
|
Tensor
|
The current time. Defaults to None. |
required |
mask
|
Optional[Tensor]
|
A mask to apply to the model output. Defaults to None. |
None
|
gt_mode
|
str
|
The mode for the gt function. Defaults to "1/t". |
'tan'
|
gt_p
|
Float
|
The parameter for the gt function. Defaults to 1.0. |
1.0
|
gt_clamp
|
Optional[Float]
|
(Float, optional): Upper limit of gt term. Defaults to None. |
None
|
score_temperature
|
Float
|
The temperature for the score part of the step. Defaults to 1.0. |
1.0
|
noise_temperature
|
Float
|
The temperature for the stochastic part of the step. Defaults to 1.0. |
1.0
|
t_lim_ode
|
Float
|
The time limit for the ODE step. Defaults to 0.99. |
0.99
|
center
|
Bool
|
Whether to center the output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
x_next |
Tensor
|
The updated state of the system after the single step, x_(t+dt). |
Notes
- If a mask is provided, it is applied element-wise to the model output before scaling.
- The
clean
method is called on the updated state before it is returned.
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
undo_scale_data(data)
Downscale the input data by the data scale factor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
The input data to downscale. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The downscaled data. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
145 146 147 148 149 150 151 152 153 154 |
|
vf_to_score(x_t, v, t)
From Geffner et al. Computes score of noisy density given the vector field learned by flow matching.
With our interpolation scheme these are related by
v(x_t, t) = (1 / t) (x_t + scale_ref ** 2 * (1 - t) * s(x_t, t)),
or equivalently,
s(x_t, t) = (t * v(x_t, t) - x_t) / (scale_ref ** 2 * (1 - t)).
with scale_ref = 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_t
|
Tensor
|
Noisy sample, shape [*, dim] |
required |
v
|
Tensor
|
Vector field, shape [*, dim] |
required |
t
|
Tensor
|
Interpolation time, shape [*] (must be < 1) |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Score of intermediate density, shape [*, dim]. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/continuous_flow_matching.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|