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

    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.
    
avalanche/benchmarks/utils/ffcv_support/ffcv_transform_utils.py