Models¶
๐ค Diffusers provides pretrained models for popular algorithms and modules to create custom diffusion systems. The primary function of models is to denoise an input sample as modeled by the distribution p{θ}(x{t-1}|x{t})
All models are built from the base ModelMixin
class which is a mindspore.nn.Cell
providing basic functionality for saving and loading models, locally and from the Hugging Face Hub.
mindone.diffusers.ModelMixin
¶
Bases: Cell
, PushToHubMixin
Base class for all models.
[ModelMixin
] takes care of storing the model configuration and provides methods for loading, downloading and
saving models.
- **config_name** ([`str`]) -- Filename to save a model to when calling [`~models.ModelMixin.save_pretrained`].
Source code in mindone/diffusers/models/modeling_utils.py
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 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 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 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 |
|
mindone.diffusers.ModelMixin.dtype: ms.Type
property
¶
mindspore.Type
: The dtype of the module (assuming that all the module parameters have the same dtype).
mindone.diffusers.ModelMixin.is_gradient_checkpointing: bool
property
¶
Whether gradient checkpointing is activated for this model or not.
mindone.diffusers.ModelMixin.__getattr__(name)
¶
The only reason we overwrite getattr
here is to gracefully deprecate accessing
config attributes directly. See https://github.com/huggingface/diffusers/pull/3129 We need to overwrite
getattr here in addition so that we don't trigger nn.Cell
's getattr':
https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module
Source code in mindone/diffusers/models/modeling_utils.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
mindone.diffusers.ModelMixin.disable_gradient_checkpointing()
¶
Deactivates gradient checkpointing for the current model (may be referred to as activation checkpointing or checkpoint activations in other frameworks).
Source code in mindone/diffusers/models/modeling_utils.py
152 153 154 155 156 157 158 |
|
mindone.diffusers.ModelMixin.disable_xformers_memory_efficient_attention()
¶
Disable memory efficient attention from xFormers.
Source code in mindone/diffusers/models/modeling_utils.py
257 258 259 260 261 |
|
mindone.diffusers.ModelMixin.enable_flash_sdp(enabled)
¶
.. warning:: This flag is beta and subject to change.
Enables or disables flash scaled dot product attention.
Source code in mindone/diffusers/models/modeling_utils.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
mindone.diffusers.ModelMixin.enable_gradient_checkpointing()
¶
Activates gradient checkpointing for the current model (may be referred to as activation checkpointing or checkpoint activations in other frameworks).
Source code in mindone/diffusers/models/modeling_utils.py
143 144 145 146 147 148 149 150 |
|
mindone.diffusers.ModelMixin.enable_xformers_memory_efficient_attention(attention_op=None)
¶
Enable memory efficient attention from xFormers.
When this option is enabled, you should observe lower GPU memory usage and a potential speed up during inference. Speed up during training is not guaranteed.
โ ๏ธ When memory efficient attention and sliced attention are both enabled, memory efficient attention takes precedent.
PARAMETER | DESCRIPTION |
---|---|
attention_op |
Not supported for now.
TYPE:
|
>>> import mindspore as ms
>>> from mindone.diffusers import UNet2DConditionModel
>>> model = UNet2DConditionModel.from_pretrained(
... "stabilityai/stable-diffusion-2-1", subfolder="unet", mindspore_dtype=ms.float16
... )
>>> model.enable_xformers_memory_efficient_attention()
Source code in mindone/diffusers/models/modeling_utils.py
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 |
|
mindone.diffusers.ModelMixin.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
Instantiate a pretrained PyTorch model from a pretrained model configuration.
The model is set in evaluation mode - model.eval()
- by default, and dropout modules are deactivated. To
train the model, set it back in training mode with model.train()
.
PARAMETER | DESCRIPTION |
---|---|
pretrained_model_name_or_path |
Can be either:
TYPE:
|
cache_dir |
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache is not used.
TYPE:
|
mindspore_dtype |
Override the default
TYPE:
|
force_download |
Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.
TYPE:
|
proxies |
A dictionary of proxy servers to use by protocol or endpoint, for example,
TYPE:
|
output_loading_info |
Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
TYPE:
|
local_files_only(`bool`, |
Whether to only load local model weights and configuration files or not. If set to
TYPE:
|
token |
The token to use as HTTP bearer authorization for remote files. If
TYPE:
|
revision |
The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier allowed by Git.
TYPE:
|
from_flax |
Load the model weights from a Flax checkpoint save file.
TYPE:
|
subfolder |
The subfolder location of a model file within a larger model repository on the Hub or locally.
TYPE:
|
mirror |
Mirror source to resolve accessibility issues if you're downloading a model in China. We do not guarantee the timeliness or safety of the source, and you should refer to the mirror site for more information.
TYPE:
|
variant |
Load weights from a specified
TYPE:
|
use_safetensors |
If set to
TYPE:
|
To use private or gated models, log-in with
huggingface-cli login
. You can also activate the special
"offline-mode" to use this method in a
firewalled environment.
Example:
from mindone.diffusers import UNet2DConditionModel
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")
If you get the error message below, you need to finetune the weights for your downstream task:
Some weights of UNet2DConditionModel were not initialized from the model checkpoint at
runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match:
- conv_in.weight: found shape torch.Size([320, 4, 3, 3]) in the checkpoint and torch.Size([320, 9, 3, 3]) in the model instantiated
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
Source code in mindone/diffusers/models/modeling_utils.py
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 |
|
mindone.diffusers.ModelMixin.num_parameters(only_trainable=False, exclude_embeddings=False)
¶
Get number of (trainable or non-embedding) parameters in the module.
PARAMETER | DESCRIPTION |
---|---|
only_trainable |
Whether or not to return only the number of trainable parameters.
TYPE:
|
exclude_embeddings |
Whether or not to return only the number of non-embedding parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
|
from mindone.diffusers import UNet2DConditionModel
model_id = "runwayml/stable-diffusion-v1-5"
unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet")
unet.num_parameters(only_trainable=True)
859520964
Source code in mindone/diffusers/models/modeling_utils.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
|
mindone.diffusers.ModelMixin.save_pretrained(save_directory, is_main_process=True, save_function=None, safe_serialization=True, variant=None, max_shard_size='10GB', push_to_hub=False, **kwargs)
¶
Save a model and its configuration file to a directory so that it can be reloaded using the
[~models.ModelMixin.from_pretrained
] class method.
PARAMETER | DESCRIPTION |
---|---|
save_directory |
Directory to save a model and its configuration file to. Will be created if it doesn't exist.
TYPE:
|
is_main_process |
Whether the process calling this is the main process or not. Useful during distributed training and you
need to call this function on all processes. In this case, set
TYPE:
|
save_function |
The function to use to save the state dictionary. Useful during distributed training when you need to
replace
TYPE:
|
safe_serialization |
Whether to save the model using
TYPE:
|
variant |
If specified, weights are saved in the format
TYPE:
|
max_shard_size |
The maximum size for a checkpoint before being sharded. Checkpoints shard will then be each of size
lower than this size. If expressed as a string, needs to be digits followed by a unit (like
TYPE:
|
push_to_hub |
Whether or not to push your model to the Hugging Face Hub after saving it. You can specify the
repository you want to push to with
TYPE:
|
kwargs |
Additional keyword arguments passed along to the [
TYPE:
|
Source code in mindone/diffusers/models/modeling_utils.py
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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
mindone.diffusers.ModelMixin.set_flash_attention_force_cast_dtype(force_cast_dtype)
¶
Since the flash-attention operator in MindSpore only supports float16 and bfloat16 data types, we need to manually set whether to force data type conversion.
When the attention interface encounters data of an unsupported data type,
if force_cast_dtype
is not None, the function will forcibly convert the data to force_cast_dtype
for computation
and then restore it to the original data type afterward. If force_cast_dtype
is None, it will fall back to the
original attention calculation using mathematical formulas.
PARAMETER | DESCRIPTION |
---|---|
force_cast_dtype |
The data type to which the input data should be forcibly converted. If None, no forced
TYPE:
|
Source code in mindone/diffusers/models/modeling_utils.py
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 |
|
mindone.diffusers.utils.PushToHubMixin
¶
A Mixin to push a model, scheduler, or pipeline to the Hugging Face Hub.
Source code in mindone/diffusers/utils/hub_utils.py
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 |
|
mindone.diffusers.utils.PushToHubMixin.push_to_hub(repo_id, commit_message=None, private=None, token=None, create_pr=False, safe_serialization=True, variant=None)
¶
Upload model, scheduler, or pipeline files to the ๐ค Hugging Face Hub.
PARAMETER | DESCRIPTION |
---|---|
repo_id |
The name of the repository you want to push your model, scheduler, or pipeline files to. It should
contain your organization name when pushing to an organization.
TYPE:
|
commit_message |
Message to commit while pushing. Default to
TYPE:
|
private |
Whether or not the repository created should be private.
TYPE:
|
token |
The token to use as HTTP bearer authorization for remote files. The token generated when running
TYPE:
|
create_pr |
Whether or not to create a PR with the uploaded files or directly commit.
TYPE:
|
safe_serialization |
Whether or not to convert the model weights to the
TYPE:
|
variant |
If specified, weights are saved in the format
TYPE:
|
from mindone.diffusers import UNet2DConditionModel
unet = UNet2DConditionModel.from_pretrained("stabilityai/stable-diffusion-2", subfolder="unet")
# Push the `unet` to your namespace with the name "my-finetuned-unet".
unet.push_to_hub("my-finetuned-unet")
# Push the `unet` to an organization with the name "my-finetuned-unet".
unet.push_to_hub("your-org/my-finetuned-unet")
Source code in mindone/diffusers/utils/hub_utils.py
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 |
|