Vdm
VDM
Bases: Interpolant
A Variational Diffusion Models (VDM) 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.discrete_time.continuous.vdm import VDM
>>> from bionemo.moco.schedules.noise.continuous_snr_transforms import CosineSNRTransform
>>> from bionemo.moco.schedules.inference_time_schedules import LinearInferenceSchedule
vdm = VDM(
time_distribution = UniformTimeDistribution(...),
prior_distribution = GaussianPrior(...),
noise_schedule = CosineSNRTransform(...),
)
model = Model(...)
# Training
for epoch in range(1000):
data = data_loader.get(...)
time = vdm.sample_time(batch_size)
noise = vdm.sample_prior(data.shape)
xt = vdm.interpolate(data, noise, time)
x_pred = model(xt, time)
loss = vdm.loss(x_pred, data, time)
loss.backward()
# Generation
x_pred = vdm.sample_prior(data.shape)
for t in LinearInferenceSchedule(...).generate_schedule():
time = torch.full((batch_size,), t)
x_hat = model(x_pred, time)
x_pred = vdm.step(x_hat, time, x_pred)
return x_pred
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
__init__(time_distribution, prior_distribution, noise_schedule, prediction_type=PredictionType.DATA, device='cpu', rng_generator=None)
Initializes the DDPM 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 |
noise_schedule
|
ContinuousSNRTransform
|
The schedule of noise, defining the amount of noise added at each time step. |
required |
prediction_type
|
PredictionType
|
The type of prediction, either "data" or another type. Defaults to "data". |
DATA
|
device
|
str
|
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
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.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 |
|
forward_process(data, t, noise=None)
Get x(t) with given time t from noise and data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
target |
required |
t
|
Tensor
|
time |
required |
noise
|
Tensor
|
noise from prior(). Defaults to None |
None
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
118 119 120 121 122 123 124 125 126 127 128 |
|
interpolate(data, t, noise)
Get x(t) with given time t from noise and data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
target |
required |
t
|
Tensor
|
time |
required |
noise
|
Tensor
|
noise from prior() |
required |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
loss(model_pred, target, t, dt=0.001, mask=None, weight_type='ones')
Calculates the loss given the model prediction, target, and time.
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
|
Tensor
|
The time at which the loss is calculated. |
required |
dt
|
Optional[Float]
|
The time step increment. Defaults to 0.001. |
0.001
|
mask
|
Optional[Tensor]
|
The mask for the data point. Defaults to None. |
None
|
weight_type
|
str
|
The type of weight to use for the loss. Can be "ones", "data_to_noise", or "variational_objective". Defaults to "ones". |
'ones'
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The calculated loss batch tensor. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
loss_weight(raw_loss, t, weight_type, dt=0.001)
Calculates the weight for the loss based on the given weight type.
This function computes the loss weight according to the specified weight_type
.
The available weight types are:
- "ones": uniform weight of 1.0
- "data_to_noise": derived from Equation (9) of https://arxiv.org/pdf/2202.00512
- "variational_objective": based on the variational objective, see https://arxiv.org/pdf/2202.00512
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_loss
|
Tensor
|
The raw loss calculated from the model prediction and target. |
required |
t
|
Tensor
|
The time step. |
required |
weight_type
|
str
|
The type of weight to use. Can be "ones", "data_to_noise", or "variational_objective". |
required |
dt
|
Float
|
The time step increment. Defaults to 0.001. |
0.001
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The weight for the loss. |
Raises:
Type | Description |
---|---|
ValueError
|
If the weight type is not recognized. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
process_data_prediction(model_output, sample, t)
Converts the model output to a data prediction based on the prediction type.
This conversion stems from the Progressive Distillation for Fast Sampling of Diffusion Models https://arxiv.org/pdf/2202.00512.
Given the model output and the sample, we convert the output to a data prediction based on the prediction type.
The conversion formulas are as follows:
- For "noise" prediction type: pred_data = (sample - noise_scale * model_output) / data_scale
- For "data" prediction type: pred_data = model_output
- For "v_prediction" prediction type: pred_data = data_scale * sample - noise_scale * model_output
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output
|
Tensor
|
The output of the model. |
required |
sample
|
Tensor
|
The input sample. |
required |
t
|
Tensor
|
The time step. |
required |
Returns:
Type | Description |
---|---|
The data prediction based on the prediction type. |
Raises:
Type | Description |
---|---|
ValueError
|
If the prediction type is not one of "noise", "data", or "v_prediction". |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
process_noise_prediction(model_output, sample, t)
Do the same as process_data_prediction but take the model output and convert to nosie.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output
|
Tensor
|
The output of the model. |
required |
sample
|
Tensor
|
The input sample. |
required |
t
|
Tensor
|
The time step. |
required |
Returns:
Type | Description |
---|---|
The input as noise if the prediction type is "noise". |
Raises:
Type | Description |
---|---|
ValueError
|
If the prediction type is not "noise". |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
score(x_hat, xt, t)
Converts the data prediction to the estimated score function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_hat
|
tensor
|
The predicted data point. |
required |
xt
|
Tensor
|
The current data point. |
required |
t
|
Tensor
|
The time step. |
required |
Returns:
Type | Description |
---|---|
The estimated score function. |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
set_loss_weight_fn(fn)
Sets the loss_weight attribute of the instance to the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn
|
Callable
|
The function to set as the loss_weight attribute. This function should take three arguments: raw_loss, t, and weight_type. |
required |
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
329 330 331 332 333 334 335 |
|
step(model_out, t, xt, dt, mask=None, center=False, temperature=1.0)
Do one step integration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model. |
required |
xt
|
Tensor
|
The current data point. |
required |
t
|
Tensor
|
The current time step. |
required |
dt
|
Tensor
|
The time step increment. |
required |
mask
|
Optional[Tensor]
|
An optional mask to apply to the data. Defaults to None. |
None
|
center
|
bool
|
Whether to center the data. Defaults to False. |
False
|
temperature
|
Float
|
The temperature parameter for low temperature sampling. Defaults to 1.0. |
1.0
|
Note
The temperature parameter controls the trade off between diversity and sample quality. Decreasing the temperature sharpens the sampling distribtion to focus on more likely samples. The impact of low temperature sampling must be ablated analytically.
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
step_ddim(model_out, t, xt, dt, mask=None, eta=0.0, center=False)
Do one step of DDIM sampling.
From the ddpm equations alpha_bar = alpha2 and 1 - alpha2 = sigma**2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
output of the model |
required |
t
|
Tensor
|
current time step |
required |
xt
|
Tensor
|
current data point |
required |
dt
|
Tensor
|
The time step increment. |
required |
mask
|
Optional[Tensor]
|
mask for the data point. Defaults to None. |
None
|
eta
|
Float
|
DDIM sampling parameter. Defaults to 0.0. |
0.0
|
center
|
Bool
|
whether to center the data point. Defaults to False. |
False
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
step_hybrid_sde(model_out, t, xt, dt, mask=None, center=False, temperature=1.0, equilibrium_rate=0.0)
Do one step integration of Hybrid Langevin-Reverse Time SDE.
See section B.3 page 37 https://www.biorxiv.org/content/10.1101/2022.12.01.518682v1.full.pdf. and https://github.com/generatebio/chroma/blob/929407c605013613941803c6113adefdccaad679/chroma/layers/structure/diffusion.py#L730
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model. |
required |
xt
|
Tensor
|
The current data point. |
required |
t
|
Tensor
|
The current time step. |
required |
dt
|
Tensor
|
The time step increment. |
required |
mask
|
Optional[Tensor]
|
An optional mask to apply to the data. Defaults to None. |
None
|
center
|
bool
|
Whether to center the data. Defaults to False. |
False
|
temperature
|
Float
|
The temperature parameter for low temperature sampling. Defaults to 1.0. |
1.0
|
equilibrium_rate
|
Float
|
The rate of Langevin equilibration. Scales the amount of Langevin dynamics per unit time. Best values are in the range [1.0, 5.0]. Defaults to 0.0. |
0.0
|
Note: For all step functions that use the SDE formulation its important to note that we are moving backwards in time which corresponds to an apparent sign change. A clear example can be seen in slide 29 https://ernestryu.com/courses/FM/diffusion1.pdf.
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|
step_ode(model_out, t, xt, dt, mask=None, center=False, temperature=1.0)
Do one step integration of ODE.
See section B page 36 https://www.biorxiv.org/content/10.1101/2022.12.01.518682v1.full.pdf. and https://github.com/generatebio/chroma/blob/929407c605013613941803c6113adefdccaad679/chroma/layers/structure/diffusion.py#L730
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model. |
required |
xt
|
Tensor
|
The current data point. |
required |
t
|
Tensor
|
The current time step. |
required |
dt
|
Tensor
|
The time step increment. |
required |
mask
|
Optional[Tensor]
|
An optional mask to apply to the data. Defaults to None. |
None
|
center
|
bool
|
Whether to center the data. Defaults to False. |
False
|
temperature
|
Float
|
The temperature parameter for low temperature sampling. Defaults to 1.0. |
1.0
|
Source code in bionemo/moco/interpolants/continuous_time/continuous/vdm.py
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 |
|