code: def batch_padding(
      cfg: Any,
      batch: Dict,
      training: bool = True,
      mask_key: str = "attention_mask",
      pad_keys: List[str] = [
          "input_ids",
          "attention_mask",
          "special_tokens_mask",
      ],
      padding_side: str = "left",
  ) -> Dict:
      """Pads a batch according to set quantile, or cuts it at maximum length"""
      if cfg.environment.compile_model:
          # logger.warning("Batch padding not functional with torch compile.")
          return batch
      elif batch[mask_key].sum() == 0:
          # continued pretraining
          return batch
      elif cfg.tokenizer.padding_quantile == 0:
          return batch
      elif training and cfg.tokenizer.padding_quantile < 1.0:
          if padding_side == "left":
              idx = int(
                  torch.floor(
                      torch.quantile(
                          torch.stack(
                              [
                                  torch.where(batch[mask_key][i] == 1)[
                                      0
                                  ].min()
                                  for i in range(
                                      batch[mask_key].size(0)
                                  )
                              ]
                          ).float(),
                          1 - cfg.tokenizer.padding_quantile,
                      )
                  )
              )
          else:
              idx = int(
                  torch.ceil(
                      torch.quantile(
                          torch.stack(
                              [
                                  torch.where(batch[mask_key][i] == 1)[
                                      0
                                  ].max()
                                  for i in range(
                                      batch[mask_key].size(0)
                                  )
                              ]
                          ).float(),
                          cfg.tokenizer.padding_quantile,
                      )
                  )
              )
      else:
          if padding_side == "left":
              idx = int(torch.where(batch[mask_key] == 1)[1].min())
          else:
              idx = int(torch.where(batch[mask_key] == 1)[1].max())
  
      if padding_side == "left":
          for key in pad_keys:
              if key in batch:
                  batch[key] = batch[key][:, idx:].contiguous()
      else:
          idx += 1
          for key in pad_keys:
              if key in batch:
                  batch[key] = batch[key][:, :idx].contiguous()
  
      return batch
  
imports: import logging
  import math
  import os
  from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union, no_type_check
  import networkx
  import numpy
  import pandas
  import pyarrow.parquet
  import torch
  from sklearn.model_selection import GroupShuffleSplit, train_test_split
  from torch import distributed
  from torch.utils.data import DataLoader, Sampler, SequentialSampler
  from llm_studio.src.datasets.conversation_chain_handler import ConversationChainHandler
  from llm_studio.src.utils.exceptions import LLMDataException
  from llm_studio.src.utils.utils import PatchedAttribute, set_seed
label_desc: Use image node_id 19 for calling a global function with example usage: batch_padding(cfg, batch, training, mask_key, pad_keys, padding_side) and returns: batch, batch, batch, batch