def _add_to_device_operation(transformations, device: Optional[torch.device] = None):
    """
    Given a list of FFCV transformations, insert the `ToDevice` operation in the most
    appropriate place.

    The corrent position of the `ToDevice` operation in FFCV is very hard to infer.
    Avalanche uses the `FFCV_TRANSFORMS_DEFS` dictionary to infer the correct position
    based on the kind of input and outputs supported by the transformations.

    :param transformations: The list of transformations (modified in place).
    :param device: If passed, the FFCV `ToDevice` operation will be added.
    :return: The transformations with `ToDevice`.
    """
    if device is None:
        return transformations

    # Check if ToDevice is laready in the pipeline
    for t in transformations:
        if isinstance(t, ToDeviceFFCV):
            # Already set
            return transformations

    # All decoders (first operation in the pipeline) return NumPy arrays
    is_numpy = True
    is_cpu = True

    transformations = list(transformations)
    inserted = False
    for i, t in enumerate(transformations):
        t_def = FFCV_TRANSFORMS_DEFS.get(type(t), None)
        if t_def is None:
            # Unknown operation
            continue

        if is_numpy and not t_def.numpy_cpu:
            # Unmanageable situation: the current input is a NumPy array
            # but the transformation only supports PyTorch Tensor.

            # A warning is already raised by check_transforms_consistency,
            # so it's not a big issue...
            # Anyway, the pipeline is probably doomed to fail
            break
        elif not is_numpy:
            if not (t_def.pytorch_cpu or t_def.pytorch_gpu):
                # Unmanageable situation: the current input is a PyTorch Tensor
                # but the transformation only supports NumPy arrays.

                # A warning is already raised by check_transforms_consistency
                break

            if is_cpu and t_def.pytorch_gpu:
                transformations.insert(i, ToDeviceFFCV(device=device))
                inserted = True
                break

            elif (not is_cpu) and t_def.pytorch_cpu:
                # From GPU to CPU is currently unsupported
                # Maybe in the future we can try to manage this...
                break

        if isinstance(t, ToTensorFFCV):
            is_numpy = False
        elif isinstance(t, ToDeviceFFCV):
            is_cpu = t.device.type == "cpu"

    if not inserted:
        transformations.append(ToDeviceFFCV(device))

    return transformations

    Given a list of FFCV transformations, insert the `ToDevice` operation in the most
    appropriate place.

    The corrent position of the `ToDevice` operation in FFCV is very hard to infer.
    Avalanche uses the `FFCV_TRANSFORMS_DEFS` dictionary to infer the correct position
    based on the kind of input and outputs supported by the transformations.

    :param transformations: The list of transformations (modified in place).
    :param device: If passed, the FFCV `ToDevice` operation will be added.
    :return: The transformations with `ToDevice`.
    
avalanche/benchmarks/utils/ffcv_support/ffcv_transform_utils.py