AutoencoderDC¶
The 2D Autoencoder model used in SANA and introduced in DCAE by authors Junyu Chen*, Han Cai*, Junsong Chen, Enze Xie, Shang Yang, Haotian Tang, Muyang Li, Yao Lu, Song Han from MIT HAN Lab.
The abstract from the paper is:
We present Deep Compression Autoencoder (DC-AE), a new family of autoencoder models for accelerating high-resolution diffusion models. Existing autoencoder models have demonstrated impressive results at a moderate spatial compression ratio (e.g., 8x), but fail to maintain satisfactory reconstruction accuracy for high spatial compression ratios (e.g., 64x). We address this challenge by introducing two key techniques: (1) Residual Autoencoding, where we design our models to learn residuals based on the space-to-channel transformed features to alleviate the optimization difficulty of high spatial-compression autoencoders; (2) Decoupled High-Resolution Adaptation, an efficient decoupled three-phases training strategy for mitigating the generalization penalty of high spatial-compression autoencoders. With these designs, we improve the autoencoder's spatial compression ratio up to 128 while maintaining the reconstruction quality. Applying our DC-AE to latent diffusion models, we achieve significant speedup without accuracy drop. For example, on ImageNet 512x512, our DC-AE provides 19.1x inference speedup and 17.9x training speedup on H100 GPU for UViT-H while achieving a better FID, compared with the widely used SD-VAE-f8 autoencoder. Our code is available at this https URL.
The following DCAE models are released and supported in Diffusers.
This model was contributed by lawrence-cj.
Load a model in Diffusers format with ModelMixin.from_pretrained
.
from mindone.diffusers import AutoencoderDC
ae = AutoencoderDC.from_pretrained("mit-han-lab/dc-ae-f32c32-sana-1.0-diffusers", mindspore_dtype=ms.float32)
Load a model in Diffusers via from_single_file
¶
from mindone.difusers import AutoencoderDC
ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f32c32-sana-1.0/blob/main/model.safetensors"
model = AutoencoderDC.from_single_file(ckpt_path)
The AutoencoderDC
model has in
and mix
single file checkpoint variants that have matching checkpoint keys, but use different scaling factors. It is not possible for Diffusers to automatically infer the correct config file to use with the model based on just the checkpoint and will default to configuring the model using the mix
variant config file. To override the automatically determined config, please use the config
argument when using single file loading with in
variant checkpoints.
from mindone.diffusers import AutoencoderDC
ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f128c512-in-1.0/blob/main/model.safetensors"
model = AutoencoderDC.from_single_file(ckpt_path, config="mit-han-lab/dc-ae-f128c512-in-1.0-diffusers")
mindone.diffusers.AutoencoderDC
¶
Bases: ModelMixin
, ConfigMixin
, FromOriginalModelMixin
An Autoencoder model introduced in DCAE and used in SANA.
This model inherits from [ModelMixin
]. Check the superclass documentation for it's generic methods implemented
for all models (such as downloading or saving).
PARAMETER | DESCRIPTION |
---|---|
in_channels |
The number of input channels in samples.
TYPE:
|
latent_channels |
The number of channels in the latent space representation.
TYPE:
|
encoder_block_types |
The type(s) of block to use in the encoder.
TYPE:
|
decoder_block_types |
The type(s) of block to use in the decoder.
TYPE:
|
encoder_block_out_channels |
The number of output channels for each block in the encoder.
TYPE:
|
decoder_block_out_channels |
The number of output channels for each block in the decoder.
TYPE:
|
encoder_layers_per_block |
The number of layers per block in the encoder.
TYPE:
|
decoder_layers_per_block |
The number of layers per block in the decoder.
TYPE:
|
encoder_qkv_multiscales |
Multi-scale configurations for the encoder's QKV (query-key-value) transformations.
TYPE:
|
decoder_qkv_multiscales |
Multi-scale configurations for the decoder's QKV (query-key-value) transformations.
TYPE:
|
upsample_block_type |
The type of block to use for upsampling in the decoder.
TYPE:
|
downsample_block_type |
The type of block to use for downsampling in the encoder.
TYPE:
|
decoder_norm_types |
The normalization type(s) to use in the decoder.
TYPE:
|
decoder_act_fns |
The activation function(s) to use in the decoder.
TYPE:
|
scaling_factor |
The multiplicative inverse of the root mean square of the latent features. This is used to scale the latent
space to have unit variance when training the diffusion model. The latents are scaled with the formula
TYPE:
|
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
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 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 597 598 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 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
|
mindone.diffusers.AutoencoderDC.decode(z, return_dict=False)
¶
Decode a batch of images.
PARAMETER | DESCRIPTION |
---|---|
z |
Input batch of latent vectors.
TYPE:
|
return_dict |
Whether to return a [
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[DecoderOutput, Tuple[Tensor]]
|
[ |
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
|
mindone.diffusers.AutoencoderDC.disable_slicing()
¶
Disable sliced AE decoding. If enable_slicing
was previously enabled, this method will go back to computing
decoding in one step.
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
552 553 554 555 556 557 |
|
mindone.diffusers.AutoencoderDC.disable_tiling()
¶
Disable tiled AE decoding. If enable_tiling
was previously enabled, this method will go back to computing
decoding in one step.
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
538 539 540 541 542 543 |
|
mindone.diffusers.AutoencoderDC.enable_slicing()
¶
Enable sliced AE decoding. When this option is enabled, the AE will split the input tensor in slices to compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
545 546 547 548 549 550 |
|
mindone.diffusers.AutoencoderDC.enable_tiling(tile_sample_min_height=None, tile_sample_min_width=None, tile_sample_stride_height=None, tile_sample_stride_width=None)
¶
Enable tiled AE decoding. When this option is enabled, the AE will split the input tensor into tiles to compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow processing larger images.
PARAMETER | DESCRIPTION |
---|---|
tile_sample_min_height |
The minimum height required for a sample to be separated into tiles across the height dimension.
TYPE:
|
tile_sample_min_width |
The minimum width required for a sample to be separated into tiles across the width dimension.
TYPE:
|
tile_sample_stride_height |
The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are no tiling artifacts produced across the height dimension.
TYPE:
|
tile_sample_stride_width |
The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling artifacts produced across the width dimension.
TYPE:
|
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
506 507 508 509 510 511 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 |
|
mindone.diffusers.AutoencoderDC.encode(x, return_dict=False)
¶
Encode a batch of images into latents.
PARAMETER | DESCRIPTION |
---|---|
x |
Input batch of images.
TYPE:
|
return_dict |
Whether to return a [
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[EncoderOutput, Tuple[Tensor]]
|
The latent representations of the encoded videos. If |
Union[EncoderOutput, Tuple[Tensor]]
|
[ |
Source code in mindone/diffusers/models/autoencoders/autoencoder_dc.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
|
mindone.diffusers.models.autoencoders.vae.DecoderOutput
dataclass
¶
Bases: BaseOutput
Output of decoding method.
PARAMETER | DESCRIPTION |
---|---|
sample |
The decoded output sample from the last layer of the model.
TYPE:
|
Source code in mindone/diffusers/models/autoencoders/vae.py
43 44 45 46 47 48 49 50 51 52 53 54 |
|