def _try_convert_global_pool(const_context, builder, op, mode):
    """
    Optional performance optimization pass that tries to lower spatial
    reduce_mean / reduce_max to global_avg_pool / global_max_pool.
    Return True if the lowering happened, otherwise return False to
    continue as normal reduction op.
    """
    rank = op.x.rank
    if is_variadic(rank) or rank not in {4, 5}:
        return False
    keep_dims = op.keep_dims.val
    if keep_dims is False:
        return False

    axes = None
    if op.axes is not None and op.axes.val is not None:
        axes = op.axes.val
    else:
        axes = list(range(rank))

    if tuple(op.outputs[0].shape[:-2]) != tuple(op.inputs["x"].shape[:-2]):
        return False
    if not all(s == 1 for s in op.outputs[0].shape[-2:]):
        return False

    builder.add_pooling(
        name=op.name,
        height=0,
        width=0,
        stride_height=0,
        stride_width=0,
        layer_type=mode.upper(),
        padding_type="valid".upper(),
        input_name=make_input(const_context, builder, op.x),
        output_name=op.outputs[0].name,
        is_global=True,
    )
    return True

    Optional performance optimization pass that tries to lower spatial
    reduce_mean / reduce_max to global_avg_pool / global_max_pool.
    Return True if the lowering happened, otherwise return False to
    continue as normal reduction op.
    
coremltools/converters/mil/backend/nn/op_mapping.py