Mdlm
MDLM
Bases: Interpolant
A Masked discrete Diffusion Language Model (MDLM) interpolant.
Examples:
>>> import torch
>>> from bionemo.moco.distributions.prior.discrete.mask import DiscreteMaskedPrior
>>> from bionemo.moco.distributions.time.uniform import UniformTimeDistribution
>>> from bionemo.moco.interpolants.continuous_time.discrete.mdlm import MDLM
>>> from bionemo.moco.schedules.noise.continuous_noise_transforms import CosineExpNoiseTransform
>>> from bionemo.moco.schedules.inference_time_schedules import LinearTimeSchedule
mdlm = MDLM(
time_distribution = UniformTimeDistribution(discrete_time = False,...),
prior_distribution = DiscreteMaskedPrior(...),
noise_schedule = CosineExpNoiseTransform(...),
)
model = Model(...)
# Training
for epoch in range(1000):
data = data_loader.get(...)
time = mdlm.sample_time(batch_size)
xt = mdlm.interpolate(data, time)
logits = model(xt, time)
loss = mdlm.loss(logits, data, xt, time)
loss.backward()
# Generation
x_pred = mdlm.sample_prior(data.shape)
schedule = LinearTimeSchedule(...)
inference_time = schedule.generate_schedule()
dts = schedue.discreteize()
for t, dt in zip(inference_time, dts):
time = torch.full((batch_size,), t)
logits = model(x_pred, time)
x_pred = mdlm.step(logits, time, x_pred, dt)
return x_pred
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
28 29 30 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 353 354 355 |
|
__init__(time_distribution, prior_distribution, noise_schedule, device='cpu', rng_generator=None)
Initialize the Masked Discrete Language Model (MDLM) interpolant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_distribution
|
TimeDistribution
|
The distribution governing the time variable in the diffusion process. |
required |
prior_distribution
|
DiscreteMaskedPrior
|
The prior distribution over the discrete token space, including masked tokens. |
required |
noise_schedule
|
ContinuousExpNoiseTransform
|
The noise schedule defining the noise intensity as a function of time. |
required |
device
|
str
|
The device to use for computations. Defaults to "cpu". |
'cpu'
|
rng_generator
|
Optional[Generator]
|
The random number generator for reproducibility. Defaults to None. |
None
|
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
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 |
|
calculate_score(logits, x, t)
Returns score of the given sample x at time t with the corresponding model output logits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
The output of the model. |
required |
x
|
Tensor
|
The current data point. |
required |
t
|
Tensor
|
The current time. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The score defined in Appendix C.3 Equation 76 of MDLM. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
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 |
|
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/continuous_time/discrete/mdlm.py
121 122 123 124 125 126 127 128 129 130 131 |
|
interpolate(data, t)
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 |
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
loss(logits, target, xt, time, mask=None, use_weight=True)
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,
considering the current state of the discrete sequence xt
at time time
.
If use_weight
is True, the loss is weighted by the reduced form of the MDLM time weight for continuous NELBO,
as specified in equation 11 of https://arxiv.org/pdf/2406.07524. This weight is proportional to the derivative
of the noise schedule with respect to time, and is used to emphasize the importance of accurate predictions at
certain times in the diffusion process.
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 state of the discrete sequence, with shape batch x node. |
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
|
use_weight
|
bool
|
Whether to use the MDLM time weight for the loss. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The calculated loss batch tensor. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
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 |
|
step(logits, t, xt, dt)
Perform a single step of MDLM DDPM step.
Parameters: logits (Tensor): The input logits. t (float): The current time step. xt (Tensor): The current state. dt (float): The time step increment.
Returns: Tensor: The updated state.
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
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 |
|
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 |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
The index of the maximum value in the last dimension of the model output. |
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
316 317 318 319 320 321 322 323 324 325 |
|
step_confidence(logits, xt, curr_step, num_steps, logit_temperature=1.0, randomness=1.0, confidence_temperature=1.0)
Update the input sequence xt by sampling from the predicted logits and adding Gumbel noise.
Method taken from GenMol Seul et al.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
Predicted logits |
required |
xt
|
Tensor
|
Input sequence |
required |
curr_step
|
int
|
Current step |
required |
num_steps
|
int
|
Total number of steps |
required |
logit_temperature
|
float
|
Temperature for softmax over logits |
1.0
|
randomness
|
float
|
Scale for Gumbel noise |
1.0
|
confidence_temperature
|
float
|
Temperature for Gumbel confidence |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
Updated input sequence xt |
Source code in bionemo/moco/interpolants/continuous_time/discrete/mdlm.py
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 |
|