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)
| 1913 | return get_parameter_dtype(self) |
| 1914 | |
| 1915 | def num_parameters(self, only_trainable: bool = False, exclude_embeddings: bool = False) -> int: |
| 1916 | """ |
| 1917 | Get number of (trainable or non-embedding) parameters in the module. |
| 1918 | |
| 1919 | Args: |
| 1920 | only_trainable (`bool`, *optional*, defaults to `False`): |
| 1921 | Whether or not to return only the number of trainable parameters. |
| 1922 | exclude_embeddings (`bool`, *optional*, defaults to `False`): |
| 1923 | Whether or not to return only the number of non-embedding parameters. |
| 1924 | |
| 1925 | Returns: |
| 1926 | `int`: The number of parameters. |
| 1927 | |
| 1928 | Example: |
| 1929 | |
| 1930 | ```py |
| 1931 | from diffusers import UNet2DConditionModel |
| 1932 | |
| 1933 | model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5" |
| 1934 | unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet") |
| 1935 | unet.num_parameters(only_trainable=True) |
| 1936 | 859520964 |
| 1937 | ``` |
| 1938 | """ |
| 1939 | is_loaded_in_4bit = getattr(self, "is_loaded_in_4bit", False) |
| 1940 | |
| 1941 | if is_loaded_in_4bit: |
| 1942 | if is_bitsandbytes_available(): |
| 1943 | import bitsandbytes as bnb |
| 1944 | else: |
| 1945 | raise ValueError( |
| 1946 | "bitsandbytes is not installed but it seems that the model has been loaded in 4bit precision, something went wrong" |
| 1947 | " make sure to install bitsandbytes with `pip install bitsandbytes`. You also need a GPU. " |
| 1948 | ) |
| 1949 | |
| 1950 | if exclude_embeddings: |
| 1951 | embedding_param_names = [ |
| 1952 | f"{name}.weight" for name, module_type in self.named_modules() if isinstance(module_type, nn.Embedding) |
| 1953 | ] |
| 1954 | total_parameters = [ |
| 1955 | parameter for name, parameter in self.named_parameters() if name not in embedding_param_names |
| 1956 | ] |
| 1957 | else: |
| 1958 | total_parameters = list(self.parameters()) |
| 1959 | |
| 1960 | total_numel = [] |
| 1961 | |
| 1962 | for param in total_parameters: |
| 1963 | if param.requires_grad or not only_trainable: |
| 1964 | # For 4bit models, we need to multiply the number of parameters by 2 as half of the parameters are |
| 1965 | # used for the 4bit quantization (uint8 tensors are stored) |
| 1966 | if is_loaded_in_4bit and isinstance(param, bnb.nn.Params4bit): |
| 1967 | if hasattr(param, "element_size"): |
| 1968 | num_bytes = param.element_size() |
| 1969 | elif hasattr(param, "quant_storage"): |
| 1970 | num_bytes = param.quant_storage.itemsize |
| 1971 | else: |
| 1972 | num_bytes = 1 |