docstr: Fit(Train) the forecaster.
  
  :param data: The data support following formats:
  
         | 1. numpy ndarrays: generate from `TSDataset.roll`,
              be sure to set label_len > 0 and time_enc = True
         | 2. pytorch dataloader: generate from `TSDataset.to_torch_data_loader`,
              be sure to set label_len > 0 and time_enc = True
         | 3. A bigdl.chronos.data.tsdataset.TSDataset instance
  
  :param validation_data: Validation sample for validation loop. Defaults to 'None'.
         If you do not input data for 'validation_data', the validation_step will be skipped.
         The validation_data support following formats:
  
         | 1. numpy ndarrays: generate from `TSDataset.roll`,
              be sure to set label_len > 0 and time_enc = True
         | 2. pytorch dataloader: generate from `TSDataset.to_torch_data_loader`,
              be sure to set label_len > 0 and time_enc = True
         | 3. A bigdl.chronos.data.tsdataset.TSDataset instance
  
  :param epochs: Number of epochs you want to train. The value defaults to 1.
  :param batch_size: Number of batch size you want to train. The value defaults to 32.
         if you input a pytorch dataloader for `data`, the batch_size will follow the
         batch_size setted in `data`.
  :param validation_mode:  A str represent the operation mode while having 'validation_data'.
         Defaults to 'output'. The validation_mode includes the following types:
  
         | 1. output:
         | If you choose 'output' for validation_mode, it will return a dict that records the
         | average validation loss of each epoch.
         |
         | 2. earlystop:
         | Monitor the val_loss and stop training when it stops improving.
         |
         | 3. best_epoch:
         | Monitor the val_loss. And load the checkpoint of the epoch with the smallest
         | val_loss after the training.
  
  :param earlystop_patience: Number of checks with no improvement after which training will
         be stopped. It takes effect when 'validation_mode' is 'earlystop'. Under the default
         configuration, one check happens after every training epoch.
  :param use_trail_id: choose a internal according to trial_id, which is used only
         in multi-objective search.
docstr_tok: ["Fit","(","Train",")","the","forecaster",".",":","param","data",":","The","data","support","following","formats",":","|","1.","numpy","ndarrays",":","generate","from","`","TSDataset.roll","`",",","be","sure","to","set","label_len",">","0","and","time_enc","=","True","|","2.","pytorch","dataloader",":","generate","from","`","TSDataset.to_torch_data_loader","`",",","be","sure","to","set","label_len",">","0","and","time_enc","=","True","|","3",".","A","bigdl.chronos.data.tsdataset.TSDataset","instance",":","param","validation_data",":","Validation","sample","for","validation","loop",".","Defaults","to","'None","'",".","If","you","do","not","input","data","for","'validation_data","'",",","the","validation_step","will","be","skipped",".","The","validation_data","support","following","formats",":","|","1.","numpy","ndarrays",":","generate","from","`","TSDataset.roll","`",",","be","sure","to","set","label_len",">","0","and","time_enc","=","True","|","2.","pytorch","dataloader",":","generate","from","`","TSDataset.to_torch_data_loader","`",",","be","sure","to","set","label_len",">","0","and","time_enc","=","True","|","3",".","A","bigdl.chronos.data.tsdataset.TSDataset","instance",":","param","epochs",":","Number","of","epochs","you","want","to","train",".","The","value","defaults","to","1.",":","param","batch_size",":","Number","of","batch","size","you","want","to","train",".","The","value","defaults","to","32.","if","you","input","a","pytorch","dataloader","for","`","data","`",",","the","batch_size","will","follow","the","batch_size","setted","in","`","data","`",".",":","param","validation_mode",":","A","str","represent","the","operation","mode","while","having","'validation_data","'",".","Defaults","to","'output","'",".","The","validation_mode","includes","the","following","types",":","|","1.","output",":","|","If","you","choose","'output","'","for","validation_mode",",","it","will","return","a","dict","that","records","the","|","average","validation","loss","of","each","epoch",".","|","|","2.","earlystop",":","|","Monitor","the","val_loss","and","stop","training","when","it","stops","improving",".","|","|","3.","best_epoch",":","|","Monitor","the","val_loss",".","And","load","the","checkpoint","of","the","epoch","with","the","smallest","|","val_loss","after","the","training",".",":","param","earlystop_patience",":","Number","of","checks","with","no","improvement","after","which","training","will","be","stopped",".","It","takes","effect","when","'validation_mode","'","is","'earlystop","'",".","Under","the","default","configuration",",","one","check","happens","after","every","training","epoch",".",":","param","use_trail_id",":","choose","a","internal","according","to","trial_id",",","which","is","used","only","in","multi-objective","search","."]
code: def fit(
      self,
      data,
      validation_data=None,
      epochs=1,
      batch_size=32,
      validation_mode="output",
      earlystop_patience=1,
      use_trial_id=None,
  ):
      """
      Fit(Train) the forecaster.
  
      :param data: The data support following formats:
  
             | 1. numpy ndarrays: generate from `TSDataset.roll`,
                  be sure to set label_len > 0 and time_enc = True
             | 2. pytorch dataloader: generate from `TSDataset.to_torch_data_loader`,
                  be sure to set label_len > 0 and time_enc = True
             | 3. A bigdl.chronos.data.tsdataset.TSDataset instance
  
      :param validation_data: Validation sample for validation loop. Defaults to 'None'.
             If you do not input data for 'validation_data', the validation_step will be skipped.
             The validation_data support following formats:
  
             | 1. numpy ndarrays: generate from `TSDataset.roll`,
                  be sure to set label_len > 0 and time_enc = True
             | 2. pytorch dataloader: generate from `TSDataset.to_torch_data_loader`,
                  be sure to set label_len > 0 and time_enc = True
             | 3. A bigdl.chronos.data.tsdataset.TSDataset instance
  
      :param epochs: Number of epochs you want to train. The value defaults to 1.
      :param batch_size: Number of batch size you want to train. The value defaults to 32.
             if you input a pytorch dataloader for `data`, the batch_size will follow the
             batch_size setted in `data`.
      :param validation_mode:  A str represent the operation mode while having 'validation_data'.
             Defaults to 'output'. The validation_mode includes the following types:
  
             | 1. output:
             | If you choose 'output' for validation_mode, it will return a dict that records the
             | average validation loss of each epoch.
             |
             | 2. earlystop:
             | Monitor the val_loss and stop training when it stops improving.
             |
             | 3. best_epoch:
             | Monitor the val_loss. And load the checkpoint of the epoch with the smallest
             | val_loss after the training.
  
      :param earlystop_patience: Number of checks with no improvement after which training will
             be stopped. It takes effect when 'validation_mode' is 'earlystop'. Under the default
             configuration, one check happens after every training epoch.
      :param use_trail_id: choose a internal according to trial_id, which is used only
             in multi-objective search.
      """
      # distributed is not supported.
      if self.distributed:
          invalidInputError(
              False, "distributed is not support in Autoformer"
          )
  
      # transform a tuple to dataloader.
      if isinstance(data, tuple):
          data = DataLoader(
              TensorDataset(
                  torch.from_numpy(data[0]),
                  torch.from_numpy(data[1]),
                  torch.from_numpy(data[2]),
                  torch.from_numpy(data[3]),
              ),
              batch_size=batch_size,
              shuffle=True,
          )
      # transform a TSDataset instance to dataloader
      if isinstance(data, TSDataset):
          _rolled = data.numpy_x is None
          data = data.to_torch_data_loader(
              batch_size=batch_size,
              roll=_rolled,
              lookback=self.data_config["past_seq_len"],
              horizon=self.data_config["future_seq_len"],
              label_len=self.data_config["label_len"],
              time_enc=True,
              feature_col=data.roll_feature,
              target_col=data.roll_target,
              shuffle=True,
          )
  
      from bigdl.chronos.pytorch import TSTrainer as Trainer
  
      if self.use_hpo is True:
          # check whether the user called the tune function
          invalidOperationError(
              hasattr(self, "trainer"),
              "There is no trainer, and you "
              "should call .tune() before .fit()",
          )
  
          # build internal according to use_trail_id for multi-objective HPO
          if self.trainer.hposearcher.objective.mo_hpo:
              invalidOperationError(
                  self.trainer.hposearcher.study,
                  "You must tune before fit the model.",
              )
              invalidInputError(
                  use_trial_id is not None,
                  "For multibojective HPO, you must specify a trial id for fit.",
              )
              trial = self.trainer.hposearcher.study.trials[
                  use_trial_id
              ]
              self.internal = self.tune_internal._model_build(trial)
  
      with TemporaryDirectory() as forecaster_log_dir:
          with TemporaryDirectory() as validation_ckpt_dir:
              from pytorch_lightning.loggers import CSVLogger
  
              logger = (
                  False
                  if validation_data is None
                  else CSVLogger(
                      save_dir=forecaster_log_dir,
                      flush_logs_every_n_steps=10,
                      name="forecaster_tmp_log",
                  )
              )
              from pytorch_lightning.callbacks import EarlyStopping
  
              early_stopping = EarlyStopping(
                  "val_loss", patience=earlystop_patience
              )
              from pytorch_lightning.callbacks import ModelCheckpoint
  
              checkpoint_callback = ModelCheckpoint(
                  monitor="val_loss",
                  dirpath=validation_ckpt_dir,
                  filename="best",
                  save_on_train_epoch_end=True,
              )
              if validation_mode == "earlystop":
                  callbacks = [early_stopping]
              elif validation_mode == "best_epoch":
                  callbacks = [checkpoint_callback]
              else:
                  callbacks = None
  
              # Trainer init
              self.trainer = Trainer(
                  logger=logger,
                  max_epochs=epochs,
                  callbacks=callbacks,
                  enable_checkpointing=self.checkpoint_callback,
                  num_processes=self.num_processes,
                  use_ipex=self.use_ipex,
                  log_every_n_steps=10,
              )
  
              # fitting
              if validation_data is None:
                  self.trainer.fit(self.internal, data)
                  self.fitted = True
              else:
                  if isinstance(validation_data, tuple):
                      validation_data = DataLoader(
                          TensorDataset(
                              torch.from_numpy(validation_data[0]),
                              torch.from_numpy(validation_data[1]),
                              torch.from_numpy(validation_data[2]),
                              torch.from_numpy(validation_data[3]),
                          ),
                          batch_size=batch_size,
                          shuffle=False,
                      )
                  # transform a TSDataset instance to dataloader
                  if isinstance(validation_data, TSDataset):
                      _rolled = validation_data.numpy_x is None
                      validation_data = (
                          validation_data.to_torch_data_loader(
                              batch_size=batch_size,
                              roll=_rolled,
                              lookback=self.data_config["past_seq_len"],
                              horizon=self.data_config[
                                  "future_seq_len"
                              ],
                              label_len=self.data_config["label_len"],
                              time_enc=True,
                              feature_col=validation_data.roll_feature,
                              target_col=validation_data.roll_target,
                              shuffle=False,
                          )
                      )
                  self.trainer.fit(self.internal, data, validation_data)
                  self.fitted = True
                  fit_csv = os.path.join(
                      forecaster_log_dir,
                      "forecaster_tmp_log/version_0/metrics.csv",
                  )
                  best_path = os.path.join(
                      validation_ckpt_dir, "best.ckpt"
                  )
                  fit_out = read_csv(fit_csv, loss_name="val_loss")
                  if validation_mode == "best_epoch":
                      self.load(best_path)
                  # modify logger attr in trainer, otherwise predict will report error
                  self.trainer._logger_connector.on_trainer_init(
                      False,
                      self.trainer.flush_logs_every_n_steps,
                      self.trainer.log_every_n_steps,
                      self.trainer.move_metrics_to_cpu,
                  )
                  return fit_out
  
imports: import torch
  import numpy
  from pandas import Timedelta
  from bigdl.chronos.forecaster.abstract import Forecaster
  from bigdl.chronos.forecaster.utils import read_csv, delete_folder
  from bigdl.chronos.metric.forecast_metrics import Evaluator
  from bigdl.chronos.model.autoformer import model_creator, loss_creator
  from torch.utils.data import TensorDataset, DataLoader
  from bigdl.chronos.model.autoformer.Autoformer import AutoFormer, _transform_config_to_namedtuple
  from bigdl.nano.utils.common import invalidInputError, invalidOperationError
  from bigdl.chronos.forecaster.utils import check_transformer_data
  from bigdl.chronos.pytorch import TSTrainer
  from bigdl.chronos.data import TSDataset
  from bigdl.nano.automl.hpo.space import Space
  from bigdl.chronos.forecaster.utils_hpo import GenericTSTransformerLightningModule, _config_has_search_space
  from bigdl.chronos.pytorch.context_manager import DummyForecasterContextManager, ForecasterContextManager
  from .utils_hpo import _format_metric_str
  import warnings
  from tempfile import TemporaryDirectory
  import os
label_desc: Use image node_id 5 for calling the AutoformerForecaster obj's underlying member method code with example usage: obj.fit(data, validation_data, epochs, batch_size, validation_mode, earlystop_patience, use_trial_id) and returns: fit_out