Repository: huggingface/diffusers
File: src/diffusers/modular_pipelines/modular_pipeline.py (Python)

Code before:
                logger.info(f"Skipping component `{name}`: no pretrained model path specified.")
                continue

            try:
                components_to_register[name] = spec.load(**component_load_kwargs)
            except Exception:
                tb = traceback.format_exc()
                warning_msg = (
                    f"\nFailed to create component {name}:\n"
                    f"- Component spec: {spec}\n"
                    f"- load() called with kwargs: {component_load_kwargs}\n"
                    "If this component is not required for your workflow you can safely ignore this message.\n\n"
                    "Traceback:\n"
                    f"{tb}"
                )
                if trust_remote_code_stripped and "trust_remote_code" in tb:
                    warning_msg += (
                        f"\nNote: `trust_remote_code=True` was not passed to `{name}` because it comes from "
                        f"an external repository (`{spec.pretrained_model_name_or_path}`). For safety, `trust_remote_code` is only forwarded to "
                        f"components from the same repository as the pipeline.\n\n"
                        f"To load this component manually:\n"
                        f"  from diffusers import AutoModel\n"
                        f'  {name} = AutoModel.from_pretrained("{spec.pretrained_model_name_or_path}", trust_remote_code=True)\n'
                        f"  pipe.update_components({name} = {name})\n"
                    )
                logger.warning(warning_msg)

        # Register all components at once
        self.register_components(**components_to_register)

    # Copied from diffusers.pipelines.pipeline_utils.DiffusionPipeline._maybe_raise_error_if_group_offload_active
    def _maybe_raise_error_if_group_offload_active(
        self, raise_error: bool = False, module: torch.nn.Module | None = None
    ) -> bool:
        from ..hooks.group_offloading import _is_group_offload_enabled

        components = self.components.values() if module is None else [module]
        components = [component for component in components if isinstance(component, torch.nn.Module)]
        for component in components:
            if _is_group_offload_enabled(component):
                if raise_error:
                    raise ValueError(
                        "You are trying to apply model/sequential CPU offloading to a pipeline that contains components "
                        "with group offloading enabled. This is not supported. Please disable group offloading for "
                        "components of the pipeline to use other offloading methods."
                    )
                return True
        return False

    # Modified from diffusers.pipelines.pipeline_utils.DiffusionPipeline.to
    def to(self, *args, **kwargs) -> Self:

Review comment (suggestion):
Of course, we cannot generalize to all possible combinations but what if the underlying component is a scheduler or a non-modeling component?

For schedulers, we could have `AutoScheduler` or something so that users won't have to remember a specific class name. For others (such as tokenizers, image processors), we already have "auto" classes.

I guess it's not that big a use case yet, but just wanted to flag.

Code after:
                and self._pretrained_model_name_or_path is not None
                and spec.pretrained_model_name_or_path != self._pretrained_model_name_or_path
            ):
                component_load_kwargs.pop("trust_remote_code")
                trust_remote_code_stripped = True

            if not spec.pretrained_model_name_or_path:
                logger.info(f"Skipping component `{name}`: no pretrained model path specified.")
                continue

            try:
                components_to_register[name] = spec.load(**component_load_kwargs)
            except Exception:
                tb = traceback.format_exc()
                if trust_remote_code_stripped and "trust_remote_code" in tb:
                    warning_msg = (
                        f"Failed to load component `{name}` from external repository "
                        f"`{spec.pretrained_model_name_or_path}`.\n\n"
                        f"`trust_remote_code=True` was not forwarded to `{name}` because it comes from "
                        f"a different repository than the pipeline (`{self._pretrained_model_name_or_path}`). "
                        f"For safety, `trust_remote_code` is only forwarded to components from the same "
                        f"repository as the pipeline.\n\n"
                        f"You need to load this component manually with `trust_remote_code=True` and pass it "
                        f"to the pipeline via `pipe.update_components()`. For example, if it is a custom model:\n\n"
                        f'  {name} = AutoModel.from_pretrained("{spec.pretrained_model_name_or_path}", trust_remote_code=True)\n'
                        f"  pipe.update_components({name}={name})\n"
                    )
                else:
                    warning_msg = (
                        f"Failed to create component {name}:\n"
                        f"- Component spec: {spec}\n"
                        f"- load() called with kwargs: {component_load_kwargs}\n"
                        "If this component is not required for your workflow you can safely ignore this message.\n\n"
                        "Traceback:\n"
                        f"{tb}"
                    )
                logger.warning(warning_msg)

        # Register all components at once
        self.register_components(**components_to_register)

    # Copied from diffusers.pipelines.pipeline_utils.DiffusionPipeline._maybe_raise_error_if_group_offload_active
    def _maybe_raise_error_if_group_offload_active(
        self, raise_error: bool = False, module: torch.nn.Module | None = None
    ) -> bool:
        from ..hooks.group_offloading import _is_group_offload_enabled

        components = self.components.values() if module is None else [module]
        components = [component for component in components if isinstance(component, torch.nn.Module)]
        for component in components:
            if _is_group_offload_enabled(component):