D3pm
D3PM
Bases: Interpolant
A Discrete Denoising Diffusion Probabilistic Model (D3PM) interpolant.
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
__init__(time_distribution, prior_distribution, noise_schedule, device='cpu', last_time_idx=0, rng_generator=None)
Initializes the D3PM 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
|
DiscreteNoiseSchedule
|
The schedule of noise, defining the amount of noise added at each time step. |
required |
device
|
str
|
The device on which to run the interpolant, either "cpu" or a CUDA device (e.g. "cuda:0"). Defaults to "cpu". |
'cpu'
|
last_time_idx
|
int
|
The last time index to consider in the interpolation process. Defaults to 0. |
0
|
rng_generator
|
Optional[Generator]
|
An optional :class: |
None
|
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
forward_process(data, t)
Apply the forward process to the data at time t.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
target discrete ids |
required |
t
|
Tensor
|
time |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
x(t) after applying the forward process |
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
196 197 198 199 200 201 202 203 204 205 206 |
|
interpolate(data, t)
Interpolate using discrete interpolation method.
This method implements Equation 2 from the D3PM paper (https://arxiv.org/pdf/2107.03006), which
calculates the interpolated discrete state xt
at time t
given the input data and noise
via q(xt|x0) = Cat(xt; p = x0*Qt_bar).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Tensor
|
The input data to be interpolated. |
required |
t
|
Tensor
|
The time step at which to interpolate. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The interpolated discrete state |
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
loss(logits, target, xt, time, mask=None, vb_scale=0.0)
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 a mask is provided, the loss is calculated only for the non-masked elements. Additionally, if vb_scale is greater than 0, the variational lower bound loss is calculated and added to the total loss.
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 |
xt
|
Tensor
|
The current data point. |
required |
time
|
Tensor
|
The time at which the loss is calculated. |
required |
mask
|
Optional[Tensor]
|
The mask for the data point. Defaults to None. |
None
|
vb_scale
|
Float
|
The scale factor for the variational lower bound loss. Defaults to 0.0. |
0.0
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The calculated loss tensor. If aggregate is True, the loss and variational lower bound loss are aggregated and |
|
returned as a single tensor. Otherwise, the loss and variational lower bound loss are returned as separate tensors. |
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|
step(model_out, t, xt, mask=None, temperature=1.0, model_out_is_logits=True)
Perform a single step in the discrete interpolant method, transitioning from the current discrete state xt
at time t
to the next state.
This step involves:
- Computing the predicted q-posterior logits using the model output
model_out
and the current statext
at timet
. - Sampling the next state from the predicted q-posterior distribution using the Gumbel-Softmax trick.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_out
|
Tensor
|
The output of the model at the current time step, which is used to compute the predicted q-posterior logits. |
required |
t
|
Tensor
|
The current time step, which is used to index into the transition matrices and compute the predicted q-posterior logits. |
required |
xt
|
Tensor
|
The current discrete state at time |
required |
mask
|
Optional[Tensor]
|
An optional mask to apply to the next state, which can be used to mask out certain tokens or regions. Defaults to None. |
None
|
temperature
|
Float
|
The temperature to use for the Gumbel-Softmax trick, which controls the randomness of the sampling process. Defaults to 1.0. |
1.0
|
model_out_is_logits
|
bool
|
A flag indicating whether the model output is already in logits form. If True, the output is assumed to be logits; otherwise, it is converted to logits. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The next discrete state at time |
Source code in bionemo/moco/interpolants/discrete_time/discrete/d3pm.py
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 |
|