Repository: huggingface/diffusers
File: src/diffusers/models/modeling_utils.py (Python)

Code before:
        return super().to(*args, **kwargs)

    # Taken from `transformers`.
    def half(self, *args):
        # Checks if the model is quantized
        if getattr(self, "is_quantized", False):
            raise ValueError(
                "`.half()` is not supported for quantized model. Please use the model as it is, since the"
                " model has already been cast to the correct `dtype`."
            )
        else:
            return super().half(*args)

    # Taken from `transformers`.
    def float(self, *args):
        # Checks if the model is quantized
        if getattr(self, "is_quantized", False):
            raise ValueError(
                "`.float()` is not supported for quantized model. Please use the model as it is, since the"
                " model has already been cast to the correct `dtype`."
            )
        else:
            return super().float(*args)

    @wraps(torch.nn.Module.compile)
    def compile(self, use_regional_compile: bool = True, *args, **kwargs):
        """ """
        if use_regional_compile:
            regions_for_compile = getattr(self, "_regions_for_compile", None)

            if not regions_for_compile:
                logger.warning(
                    "_regions_for_compile attribute is empty. Using _no_split_modules to find compile regions."
                )

                regions_for_compile = getattr(self, "_no_split_modules", None)

            if not regions_for_compile:
                logger.warning(
                    "Both _regions_for_compile and _no_split_modules attribute are empty. "
                    "Set _regions_for_compile for the model to benefit from regional compilation. "
                    "Falling back to full model compilation, which could have high first iteration "
                    "latency."
                )
                super().compile(*args, **kwargs)

            has_compiled_region = False
            for submod in self.modules():
                if submod.__class__.__name__ in regions_for_compile:
                    has_compiled_region = True
                    submod.compile(*args, **kwargs)

Review comment (suggestion):
If we go with Option 3 - we should turn this to `False` to keep the existing behavior of `pipe.tranformer.compile` same as before.

Code after:
            return self

        return super().to(*args, **kwargs)

    # Taken from `transformers`.
    def half(self, *args):
        # Checks if the model is quantized
        if getattr(self, "is_quantized", False):
            raise ValueError(
                "`.half()` is not supported for quantized model. Please use the model as it is, since the"
                " model has already been cast to the correct `dtype`."
            )
        else:
            return super().half(*args)

    # Taken from `transformers`.
    def float(self, *args):
        # Checks if the model is quantized
        if getattr(self, "is_quantized", False):
            raise ValueError(
                "`.float()` is not supported for quantized model. Please use the model as it is, since the"
                " model has already been cast to the correct `dtype`."
            )
        else:
            return super().float(*args)

    def compile_repeated_blocks(self, *args, **kwargs):
        """
        Compiles *only* the frequently repeated sub-modules of a model (e.g. the Transformer layers) instead of
        compiling the entire model. This technique—often called **regional compilation** (see the PyTorch recipe
        https://docs.pytorch.org/tutorials/recipes/regional_compilation.html) can reduce end-to-end compile time
        substantially, while preserving the runtime speed-ups you would expect from a full `torch.compile`.

        The set of sub-modules to compile is discovered by the presence of **`_repeated_blocks`** attribute in the
        model definition. Define this attribute on your model subclass as a list/tuple of class names (strings). Every
        module whose class name matches will be compiled.

        Once discovered, each matching sub-module is compiled by calling `submodule.compile(*args, **kwargs)`. Any
        positional or keyword arguments you supply to `compile_repeated_blocks` are forwarded verbatim to
        `torch.compile`.
        """
        repeated_blocks = getattr(self, "_repeated_blocks", None)

        if not repeated_blocks:
            raise ValueError(
                "`_repeated_blocks` attribute is empty. "
                f"Set `_repeated_blocks` for the class `{self.__class__.__name__}` to benefit from faster compilation. "
            )
        has_compiled_region = False
        for submod in self.modules():
            if submod.__class__.__name__ in repeated_blocks: