src/transformers/data/data_collator.py/tf_default_data_collator
def tf_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any]:
    import numpy as np
    import tensorflow as tf

    if not isinstance(features[0], (dict, BatchEncoding)):
        features = [vars(f) for f in features]
    first = features[0]
    batch = {}

    # Special handling for labels.
    # Ensure that tensor is created with the correct type
    # (it should be automatically the case, but let's make sure of it.)
    if "label" in first and first["label"] is not None:
        if isinstance(first["label"], tf.Tensor):
            dtype = tf.int64 if first["label"].dtype.is_integer() else tf.float32
        elif isinstance(first["label"], np.ndarray):
            dtype = tf.int64 if np.issubdtype(first["label"].dtype, np.integer) else tf.float32
        elif isinstance(first["label"], (tuple, list)):
            dtype = tf.int64 if isinstance(first["label"][0], int) else tf.float32
        else:
            dtype = tf.int64 if isinstance(first["label"], int) else tf.float32
        batch["labels"] = tf.convert_to_tensor([f["label"] for f in features], dtype=dtype)
    elif "label_ids" in first and first["label_ids"] is not None:
        if isinstance(first["label_ids"], tf.Tensor):
            batch["labels"] = tf.stack([f["label_ids"] for f in features])
        else:
            dtype = tf.int64 if type(first["label_ids"][0]) is int else tf.float32
            batch["labels"] = tf.convert_to_tensor([f["label_ids"] for f in features], dtype=dtype)

    # Handling of all other possible keys.
    # Again, we will use the first element to figure out which key/values are not None for this model.
    for k, v in first.items():
        if k not in ("label", "label_ids") and v is not None and not isinstance(v, str):
            if isinstance(v, (tf.Tensor, np.ndarray)):
                batch[k] = tf.stack([f[k] for f in features])
            else:
                batch[k] = tf.convert_to_tensor([f[k] for f in features])

    return batch