MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / num_parameters

Method num_parameters

src/diffusers/models/modeling_utils.py:891–927  ·  view source on GitHub ↗

Get number of (trainable or non-embedding) parameters in the module. Args: only_trainable (`bool`, *optional*, defaults to `False`): Whether or not to return only the number of trainable parameters. exclude_embeddings (`bool`, *optional*, def

(self, only_trainable: bool = False, exclude_embeddings: bool = False)

Source from the content-addressed store, hash-verified

889 return get_parameter_dtype(self)
890
891 def num_parameters(self, only_trainable: bool = False, exclude_embeddings: bool = False) -> int:
892 """
893 Get number of (trainable or non-embedding) parameters in the module.
894
895 Args:
896 only_trainable (`bool`, *optional*, defaults to `False`):
897 Whether or not to return only the number of trainable parameters.
898 exclude_embeddings (`bool`, *optional*, defaults to `False`):
899 Whether or not to return only the number of non-embedding parameters.
900
901 Returns:
902 `int`: The number of parameters.
903
904 Example:
905
906 ```py
907 from diffusers import UNet2DConditionModel
908
909 model_id = "runwayml/stable-diffusion-v1-5"
910 unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet")
911 unet.num_parameters(only_trainable=True)
912 859520964
913 ```
914 """
915
916 if exclude_embeddings:
917 embedding_param_names = [
918 f"{name}.weight"
919 for name, module_type in self.named_modules()
920 if isinstance(module_type, torch.nn.Embedding)
921 ]
922 non_embedding_parameters = [
923 parameter for name, parameter in self.named_parameters() if name not in embedding_param_names
924 ]
925 return sum(p.numel() for p in non_embedding_parameters if p.requires_grad or not only_trainable)
926 else:
927 return sum(p.numel() for p in self.parameters() if p.requires_grad or not only_trainable)
928
929 def _convert_deprecated_attention_blocks(self, state_dict: OrderedDict) -> None:
930 deprecated_attention_block_paths = []

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected