def _load_encoders(
        self, tfm_save: "TorchForecastingModel", load_encoders: bool
    ) -> tuple[SequentialEncoder, dict]:
        """Return the encoders from a model save with several sanity checks."""
        if self.add_encoders is None:
            same_encoders = True
            same_transformer = True
        elif tfm_save.add_encoders is None:
            same_encoders = False
            same_transformer = False
        else:
            # transformers are equal if they are instances of the same class
            self_transformer = self.add_encoders.get("transformer", None)
            tfm_transformer = tfm_save.add_encoders.get("transformer", None)
            same_transformer = type(self_transformer) is type(tfm_transformer)

            # encoders are equal if they have the same entries (transformer excluded)
            self_encoders = {
                k: v for k, v in self.add_encoders.items() if k != "transformer"
            }
            tfm_encoders = {
                k: v for k, v in tfm_save.add_encoders.items() if k != "transformer"
            }
            same_encoders = self_encoders == tfm_encoders

        if load_encoders:
            # avoid silently overwriting new encoders
            raise_if_not(
                same_transformer,
                f"Transformers defined in the loaded encoders and the new model must have the same type, received "
                f"({None if tfm_save.add_encoders is None else type(tfm_save.add_encoders.get('transformer', None))}) "
                f"and "
                f"({None if self.add_encoders is None else type(self.add_encoders.get('transformer', None))}).",
                logger,
            )
            raise_if_not(
                same_encoders,
                f"Encoders loaded from the checkpoint ({tfm_save.add_encoders}) "
                f"are different from the encoders defined in the new model "
                f"({self.add_encoders}).",
                logger,
            )

            new_add_encoders: dict = copy.deepcopy(tfm_save.add_encoders)
            new_encoders: SequentialEncoder = copy.deepcopy(tfm_save.encoders)
        else:
            raise_if(
                len(tfm_save.add_encoders) > 0 and self.add_encoders is None,
                f"Model was created without encoders and encoders were not loaded, but the weights were trained "
                f"using encoders({tfm_save.add_encoders}). Either set `load_encoders` to `True` or add a matching "
                f"`add_encoders` dict at model creation.",
                logger,
            )

            new_add_encoders: dict = self.add_encoders
            new_encoders: SequentialEncoder = self.initialize_encoders()

            # compare the dimensions of the new and ckpt encoders
            if tfm_save.encoders is not None:
                # extract output dimensions of checkpoint encoders
                (
                    ckpt_past_enc_n_comp,
                    ckpt_future_enc_n_comp,
                ) = tfm_save.encoders.encoding_n_components
                # extract output dimensions of new encoders
                (
                    new_past_enc_n_comp,
                    new_future_enc_n_comp,
                ) = new_encoders.encoding_n_components

                raise_if(
                    new_past_enc_n_comp != ckpt_past_enc_n_comp
                    or new_future_enc_n_comp != ckpt_future_enc_n_comp,
                    f"Number of components mismatch between model's and checkpoint's encoders:\n"
                    f"- past covs: new {new_past_enc_n_comp}, checkpoint {ckpt_past_enc_n_comp}\n"
                    f"- future covs: new {new_future_enc_n_comp}, checkpoint {ckpt_future_enc_n_comp}",
                    logger,
                )

                # display warning, an exception will be raised if `fit()`` is not called before `predict()`
                if not new_encoders.fit_called and new_encoders.requires_fit:
                    logger.info(
                        "Model's weights were loaded without the encoders and at least one of "
                        "them needs to be fitted: please call `fit()` before calling `predict()`."
                    )

        return new_encoders, new_add_encoders
Return the encoders from a model save with several sanity checks.
darts/models/forecasting/torch_forecasting_model.py