FlexMDM Interpolant
discrete_diffusion.forward_process.flexmdm
FlexMDM Joint Interpolant for Any-Order Mask Insertion Flow.
This module implements the forward process for FlexMDM's any-order algorithm, which jointly models insertion (length) and masking (content) processes.
FlexMDMForwardProcess
Bases: ForwardProcess
Interpolant for any-order mask insertion flow.
This implements a joint process where tokens are both inserted (affecting length) and masked (affecting content). The insertion and unmasking processes are governed by separate noise schedules.
Source code in src/discrete_diffusion/forward_process/flexmdm.py
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 | |
__init__(tokenizer, insertion_schedule, unmask_schedule, max_length, pad_token, name=None)
Initialize any-order interpolant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
Tokenizer instance |
required | |
insertion_schedule
|
ScheduleProtocol
|
Schedule for insertion process |
required |
unmask_schedule
|
ScheduleProtocol
|
Schedule for unmasking process |
required |
max_length
|
int
|
Maximum sequence length |
required |
pad_token
|
int
|
ID of padding token |
required |
name
|
str | None
|
Optional name for the process |
None
|
Source code in src/discrete_diffusion/forward_process/flexmdm.py
elbo_weight(t, x1)
Compute ELBO loss weights using rate scale factors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Time values [B] |
required |
x1
|
Tensor
|
Clean sequences [B, L] |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple of (unmask_weight [B, L], insert_weight [B, L+1]) |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
forward(input_ids, t)
Return the noised tokens at time t.
Returns:
| Name | Type | Description |
|---|---|---|
xt |
Noised sequence [B, L] |
|
result |
JointInterpolantResult containing metadata |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
hitting_time(t, x1)
Sample hitting times for insertion and unmasking.
Insertion time is sampled uniformly, then unmasking time is sampled uniformly in [insertion_time, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Current time [B] |
required |
x1
|
Tensor
|
Clean sequences [B, L] |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (insertion_time [B, L], unmasking_time [B, L]) |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
sample_interpolant(t, x1)
Sample interpolant by applying deletion and masking.
Tokens are deleted if t < insertion_time, masked if insertion_time <= t < unmasking_time, and clean otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Time values [B] |
required |
x1
|
Tensor
|
Clean sequences [B, L] |
required |
Returns:
| Type | Description |
|---|---|
JointInterpolantResult
|
JointInterpolantResult with noised sequence and metadata |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
to_actual_rate(xt, prediction, t)
Convert model prediction to actual sampling rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xt
|
Tensor
|
Current noised sequence [B, L] |
required |
prediction
|
ModelPrediction
|
Model output |
required |
t
|
Tensor
|
Time values [B] |
required |
Returns:
| Type | Description |
|---|---|
Rate
|
Rate object with unmask and length rates |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
JointInterpolantResult
dataclass
Result from sampling the joint interpolant.
Attributes:
| Name | Type | Description |
|---|---|---|
xt |
Tensor
|
Noised sequence at time t [B, L] |
st |
Tensor
|
Sorting indices mapping xt back to x1 positions [B, L] |
_x1 |
Tensor
|
Original clean sequence (stored for property computation) |
_pad_token |
int
|
Padding token ID |
_mask_token |
int
|
Mask token ID |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
gaps_and_mask
property
Compute gap counts between xt positions.
Returns:
| Name | Type | Description |
|---|---|---|
gaps |
Tensor
|
Number of deleted tokens between each position [B, L+1] |
mask |
Tensor
|
Valid positions mask [B, L+1] |
mask_indices
property
Boolean mask indicating which positions are masked.
unmasked
property
Ground truth tokens at positions corresponding to xt.
x1_length
property
Length of x1 (excluding padding) [B].
xt_length
property
Length of xt (excluding padding) [B].
ModelPrediction
dataclass
Model output for FlexMDM any-order algorithm.
Attributes:
| Name | Type | Description |
|---|---|---|
token_logits |
Tensor
|
Logits for token predictions [B, L, V] |
length_posterior |
Optional[Tensor]
|
Optional distribution over gap lengths [B, L, max_gap] |
expected_gaps |
Tensor
|
Expected number of tokens to insert [B, L] |
Source code in src/discrete_diffusion/forward_process/flexmdm.py
Rate
dataclass
Rate information for sampling.
Attributes:
| Name | Type | Description |
|---|---|---|
unmask_rate |
Tensor
|
Rate of unmasking transitions [B, L, V] |
length_rate |
Tensor
|
Rate of insertion transitions [B, L+1] |