code: def _apply_transforms_pre_optimization(
      transformations: List[Any], device: Optional[torch.device] = None
  ):
      """
      Applies common pre-optimizations to the list of transformations.
  
      :param transformations: The list of transformations.
      :param device: If passed, the FFCV `ToDevice` operation will be added.
      :return: The transformations optimized for FFCV.
      """
      if len(transformations) < 2:
          # No optimizations to apply if there are less than 2 transformations
          return transformations
  
      result = [transformations[0]]
  
      for t in transformations[1:]:
          if (
              isinstance(t, NormalizeTV)
              and isinstance(result[-1], ToTensorTV)
              and device is not None
              and device.type == "cuda"
          ):
              # Optimize ToTensor+Normalize combo
  
              # ToTensor from torchvision does the following:
              # 1. PIL/NDArray -> Tensor
              # 2. Shape (H x W x C) -> (C x H x W)
              # 3. [0, 255] -> [0.0, 1.0]
              # In FFCV, the fist two steps are implemented as separate
              # transformations. The range change is not available in a
              # standalone way, but it is applied when normalizing.
  
              # Note: we apply this optimization only when running on CUDA
              # as the FFCV Normalize is currently bugged and
              # does not work on CPU with PyTorch Tensor inputs.
              # It *may* work with CPU+NDArray...
  
              result[-1] = ToTensorFFCV()
              result.append(ToTorchImageFFCV())
  
              dtype = (
                  torch.zeros(0, dtype=torch.get_default_dtype())
                  .numpy()
                  .dtype
              )
  
              mean = np.array(t.mean) * 255
              std = np.array(t.std) * 255
              result.append(NormalizeFFCV(mean, std, dtype))
  
          elif isinstance(t, RandomResizedCropTV) and isinstance(
              result[-1], SimpleRGBImageDecoder
          ):
              size = t.size
              if isinstance(size, int):
                  size = [size, size]
              elif len(size) == 1:
                  size = [size[0], size[0]]
              result[-1] = RandomResizedCropRGBImageDecoder(
                  size, t.scale, t.ratio
              )
          elif isinstance(t, CenterCropTV) and isinstance(
              result[-1], SimpleRGBImageDecoder
          ):
              size = t.size
              if isinstance(size, int):
                  size = [size, size]
              elif len(size) == 1:
                  size = [size[0], size[0]]
              result[-1] = CenterCropRGBImageDecoderTVAlike(size)
          else:
              result.append(t)
  
      return result
  
imports: from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, Type, Union
  from typing_extensions import Literal
  import warnings
  import numpy
  import torch
  from avalanche.benchmarks.utils.transforms import flat_transforms_recursive
  from avalanche.benchmarks.utils.ffcv_support.center_crop import CenterCropRGBImageDecoderTVAlike
  from torchvision.transforms import ToTensor
  from torchvision.transforms import PILToTensor
  from torchvision.transforms import Normalize
  from torchvision.transforms import ConvertImageDtype
  from torchvision.transforms import RandomResizedCrop
  from torchvision.transforms import CenterCrop
  from torchvision.transforms import RandomHorizontalFlip
  from torchvision.transforms import RandomCrop
  from ffcv.transforms import ToTensor
  from ffcv.transforms import ToDevice
  from ffcv.transforms import ToTorchImage
  from ffcv.transforms import NormalizeImage
  from ffcv.transforms import Convert
  from ffcv.transforms import View
  from ffcv.transforms import Squeeze
  from ffcv.transforms import RandomResizedCrop
  from ffcv.transforms import RandomHorizontalFlip
  from ffcv.transforms import RandomTranslate
  from ffcv.transforms import Cutout
  from ffcv.transforms import ImageMixup
  from ffcv.transforms import LabelMixup
  from ffcv.transforms import MixupToOneHot
  from ffcv.transforms import Poison
  from ffcv.transforms import ReplaceLabel
  from ffcv.transforms import RandomBrightness
  from ffcv.transforms import RandomContrast
  from ffcv.transforms import RandomSaturation
  from ffcv.transforms import ModuleWrapper
  from ffcv.pipeline.operation import Operation
  from ffcv.pipeline.state import State
  from ffcv.pipeline.allocation_query import AllocationQuery
  from ffcv.fields.decoders import SimpleRGBImageDecoder, RandomResizedCropRGBImageDecoder
  from dataclasses import replace
label_desc: Use image node_id 3 for calling a global function with example usage: _apply_transforms_pre_optimization(transformations, device) and returns: result, transformations