ezpz.examples.diffusionΒΆ
Tiny diffusion example for short text generation.
This script trains a tiny denoising diffusion model on a handful of toy sentences and then samples new sentences by running the reverse process. The goal is to keep the code minimal while showcasing the full flow:
1 2 3 | |
Typical usage (customize with args as needed):
1 2 3 | |
Launch with:
1 | |
Help output (python3 -m ezpz.examples.diffusion --help):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
DiffusionSchedule
dataclass
ΒΆ
Precompute alpha/beta schedule values for DDPM style updates.
Source code in src/ezpz/examples/diffusion.py
__post_init__()
ΒΆ
Precompute alpha and alpha_bar schedules for diffusion steps.
Source code in src/ezpz/examples/diffusion.py
DiffusionTextModel
ΒΆ
Bases: Module
Simple transformer that predicts noise on token embeddings.
Source code in src/ezpz/examples/diffusion.py
__init__(vocab_size, hidden_size, max_seq_len, timesteps, n_layers=2, n_heads=4)
ΒΆ
Initialize embeddings and transformer encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_size
|
int
|
Size of the token vocabulary. |
required |
hidden_size
|
int
|
Dimensionality of embeddings and model width. |
required |
max_seq_len
|
int
|
Maximum sequence length. |
required |
timesteps
|
int
|
Number of diffusion steps. |
required |
n_layers
|
int
|
Number of transformer encoder layers. |
2
|
n_heads
|
int
|
Attention heads per layer. |
4
|
Source code in src/ezpz/examples/diffusion.py
decode_tokens(embs)
ΒΆ
Project embeddings back to token ids via tied embeddings.
Source code in src/ezpz/examples/diffusion.py
embed_tokens(tokens)
ΒΆ
Embed token ids and scale them for transformer input.
Source code in src/ezpz/examples/diffusion.py
forward(noisy_embs, t)
ΒΆ
Predict noise residuals given noisy embeddings and timestep.
Source code in src/ezpz/examples/diffusion.py
ToyTextDataset
ΒΆ
Bases: Dataset
Pads or truncates sentences to a fixed length.
Source code in src/ezpz/examples/diffusion.py
__getitem__(idx)
ΒΆ
__init__(texts, vocab, seq_len=12)
ΒΆ
Store corpus and vocabulary for encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
List[str]
|
Raw sentences. |
required |
vocab
|
Dict[str, int]
|
Token-to-id mapping. |
required |
seq_len
|
int
|
Target sequence length for padding/truncation. |
12
|
Source code in src/ezpz/examples/diffusion.py
add_noise(x0, t, schedule)
ΒΆ
Apply forward diffusion noise to clean embeddings.
Source code in src/ezpz/examples/diffusion.py
build_vocab(texts)
ΒΆ
Create a tiny vocabulary from a list of strings.
Source code in src/ezpz/examples/diffusion.py
generate_text(model, schedule, inv_vocab, seq_len, num_samples, skip_tokens=('<pad>', '<unk>'))
ΒΆ
Sample sequences from the trained diffusion model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
DiffusionTextModel
|
Trained diffusion network (possibly FSDP wrapped). |
required |
schedule
|
DiffusionSchedule
|
Noise schedule with precomputed alphas. |
required |
inv_vocab
|
Dict[int, str]
|
Mapping from token ids back to string tokens. |
required |
seq_len
|
int
|
Maximum sequence length. |
required |
num_samples
|
int
|
Number of sentences to generate. |
required |
skip_tokens
|
Tuple[str, ...]
|
Tokens to drop from decoded outputs. |
('<pad>', '<unk>')
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of generated text strings. |
Source code in src/ezpz/examples/diffusion.py
get_default_texts()
ΒΆ
Return a small corpus of seed sentences for toy training.
Source code in src/ezpz/examples/diffusion.py
load_hf_texts(dataset_name, split, text_column, limit)
ΒΆ
Pull a small slice of text from a Hugging Face dataset for quick experiments.
This uses only a limited number of rows (limit) to keep the example light.
Source code in src/ezpz/examples/diffusion.py
main(args)
ΒΆ
Set up distributed training, fit the model, and log samples.
Source code in src/ezpz/examples/diffusion.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | |
p_sample(model, xt, t, schedule)
ΒΆ
Predict one reverse-diffusion step at timestep t.
Source code in src/ezpz/examples/diffusion.py
parse_args()
ΒΆ
Parse CLI arguments for the diffusion text example.
Source code in src/ezpz/examples/diffusion.py
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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
sample_timesteps(batch_size, schedule, device)
ΒΆ
Uniformly sample diffusion steps for a batch.
test(model, test_loader)
ΒΆ
Evaluate the classifier outputs on a held-out loader.
Source code in src/ezpz/examples/diffusion.py
train(model, loader, schedule, args, steps, outdir, lr=0.001)
ΒΆ
Train the diffusion text model for a fixed number of steps.
Source code in src/ezpz/examples/diffusion.py
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 | |