code: def process_datasets_for_packing(
      cfg, train_dataset, eval_dataset, tokenizer
  ):
      drop_long = partial(drop_long_seq, sequence_len=cfg.sequence_len)
      with zero_first(is_main_process()):
          if cfg.group_by_length:
              train_dataset = train_dataset.map(
                  add_length, num_proc=cfg.dataset_processes
              )
  
          if cfg.sample_packing:
              train_dataset = train_dataset.map(
                  add_position_ids, num_proc=cfg.dataset_processes
              )
              if cfg.eval_sample_packing is not False:
                  if eval_dataset:
                      eval_dataset = eval_dataset.map(
                          add_position_ids,
                          num_proc=cfg.dataset_processes,
                      )
  
          if cfg.group_by_length or cfg.sample_packing:
              max_input_len = np.max(get_dataset_lengths(train_dataset))
              LOG.debug(
                  f"max_input_len: {max_input_len}",
                  main_process_only=True,
              )
  
          train_dataset = train_dataset.filter(
              drop_long, num_proc=cfg.dataset_processes
          )
          if eval_dataset:
              eval_dataset = eval_dataset.filter(
                  drop_long, num_proc=cfg.dataset_processes
              )
  
          # Phi doesn't want the attention_mask feature when training
          if (
              "CodeGenTokenizer" in tokenizer.__class__.__name__
              or (cfg.is_mistral_derived_model and cfg.flash_attention)
              or cfg.model_config_type == "mamba"
          ):
              train_dataset = train_dataset.remove_columns(
                  "attention_mask"
              )
              if eval_dataset:
                  eval_dataset = eval_dataset.remove_columns(
                      "attention_mask"
                  )
  
      return train_dataset, eval_dataset
  
imports: import math
  import os
  from contextlib import contextmanager
  from functools import partial
  from typing import List
  import numpy
  import torch
  import torch.cuda
  from accelerate.logging import get_logger
  from datasets import set_caching_enabled
  from torch.utils.data import DataLoader, RandomSampler
  from axolotl.core.trainer_builder import HFCausalTrainerBuilder, HFDPOTrainerBuilder
  from axolotl.utils.distributed import is_main_process, reduce_and_broadcast, zero_first
  from axolotl.utils.samplers import MultipackBatchSampler, get_dataset_lengths
label_desc: Use image node_id 8 for calling a global function with example usage: process_datasets_for_packing(cfg, train_dataset, eval_dataset, tokenizer) and returns: train_dataset,  eval_dataset