def assert_same_dtype(tensors_with_name, dtype=None):
    """
    Whether all types of tensors in `tensors` are the same as `dtype`.

    :param tensors_with_name: A list of (tensor, tensor_name).
    :param dtype: Expected type. If `None`, depend on the type of tensors.
    :return: The type of `tensors`.
    """

    expected_dtype = dtype
    for tensor, tensor_name in tensors_with_name:
        tensor_dtype = tensor.dtype
        if not expected_dtype:
            expected_dtype = tensor_dtype
        elif expected_dtype != tensor_dtype:
            if dtype is None:
                tensor0, tensor0_name = tensors_with_name[0]
                raise TypeError(
                    '%s(%s), must be the same type as %s(%s).' % (
                        tensor_name, tensor_dtype,
                        tensor0_name, tensor0.dtype))
            else:
                raise TypeError(
                    '%s(%s), must be %s.' % (
                        tensor_name, tensor_dtype, expected_dtype))

    return expected_dtype