def get_broadcast_max_shape(shape1, shape2):
    """
    Checks whether two inputs shapes are broadcastable, and if yes, also returns the result broadcast shape.
    Two shapes are broadcastable if starting from trailing (rightmost) dimensions both dims are:
        1. equal, or
        2. one of them is 1
    Note that two shapes are not required to have the same number of dimensions.
    For example, shape [5, 2, 3] and shape [3] are also broadcastable.
    """
    from aitemplate.compiler.base import IntImm

    min_len = min(len(shape1), len(shape2))
    if len(shape1) > len(shape2):
        res_shape = list(shape1)
    else:
        res_shape = list(shape2)
    for i in range(min_len):
        idx = -i - 1
        dim1 = shape1[idx]
        dim2 = shape2[idx]
        if dim1 == dim2:
            res_shape[idx] = dim1
            continue
        if dim1 == IntImm(1):
            res_shape[idx] = dim2
        elif dim2 == IntImm(1):
            res_shape[idx] = dim1
        else:
            return (False, None)
    return (True, res_shape)

    Checks whether two inputs shapes are broadcastable, and if yes, also returns the result broadcast shape.
    Two shapes are broadcastable if starting from trailing (rightmost) dimensions both dims are:
        1. equal, or
        2. one of them is 1
    Note that two shapes are not required to have the same number of dimensions.
    For example, shape [5, 2, 3] and shape [3] are also broadcastable.
    
python/aitemplate/utils/shape_utils.py