def METHOD_NAME(x, data_format, keepdims, mask=None):
    steps_axis = 1 if data_format == "channels_last" else 2
    if mask is not None:
        mask = np.expand_dims(
            mask, 2 if data_format == "channels_last" else 1
        )
        x *= mask
        res = np.sum(x, axis=steps_axis) / np.sum(mask, axis=steps_axis)
    else:
        res = np.mean(x, axis=steps_axis)
    if keepdims:
        res = np.expand_dims(res, axis=steps_axis)
    return res