src/transformers/utils/generic.py/can_return_loss
def can_return_loss(model_class):
    """
    Check if a given model can return loss.

    Args:
        model_class (`type`): The class of the model.
    """
    base_classes = str(inspect.getmro(model_class))

    if "keras.engine.training.Model" in base_classes:
        signature = inspect.signature(model_class.call)  # TensorFlow models
    elif "torch.nn.modules.module.Module" in base_classes:
        signature = inspect.signature(model_class.forward)  # PyTorch models
    else:
        signature = inspect.signature(model_class.__call__)  # Flax models

    for p in signature.parameters:
        if p == "return_loss" and signature.parameters[p].default is True:
            return True

    return False