Extend RESIZE's NEAREST rounding modes

Hi All,

The existing TOSA specification for RESIZE (in NEAREST mode) is implemented with half up rounding.

ONNX’s RESIZE operation in NEAREST mode supports four different rounding modes. This is specified in section nearest_mode: round_prefer_floor, round_prefer_ceil, floor and ceil [0] . We see three out of the four modes in actual models. round_prefer_ceil is the rounding that TOSA currently supports.

If TOSA specification was extended with an additional parameter nearest_mode it would look like:

    [...]
    } else if (mode==NEAREST) {
        int32_t iy, ix;
        if (is_floating_point(resize_t)) {
            if (nearest_mode==ROUND_PREFER_CEIL) {
                iy = (dy >= 0.5) ? iy1 : iy0;
                ix = (dx >= 0.5) ? ix1 : ix0;
            } else if (nearest_mode==ROUND_PREFER_FLOOR) {
                iy = (dy < 0.5) ? iy1 : iy0;
                ix = (dx < 0.5) ? ix1 : ix0;                
            } else if (nearest_mode==CEIL) {
                iy = iy1;
                ix = ix1;
            } else  {
                iy = iy0;
                ix = ix0;                
            }
        } else {
            if (nearest_mode==ROUND_PREFER_CEIL) {
                iy = (2 * dy >= scale_y_n) ? iy1 : iy0;
                ix = (2 * dx >= scale_x_n) ? ix1 : ix0;
            } else if (nearest_mode==ROUND_PREFER_FLOOR) {
                iy = (2 * dy < scale_y_n) ? iy1 : iy0;
                ix = (2 * dx < scale_x_n) ? ix1 : ix0;
            } else if (nearest_mode==CEIL) {
               iy = iy1;
               ix = ix1;
            } else  {
               iy = iy0;
               ix = ix0;                
            }
        }
        in_t v = tensor_read<in_t>(input, [N,IH,IW,C], [n,iy,ix,c]);
        tensor_write<out_t>(output, [N,OH,OW,C], [n,oy,ox,c], v);
    }
}

Would such an extension be appropriate, or are there other ways of implementing these rounding modes in TOSA.

Looking forward hearing from the community,
Robert

[0] Resize - ONNX 1.16.0 documentation - Under nearest_mode section.

Hi Robert,

Thanks for your suggestion. My first thought is that having multiple rounding modes for nearest could make it harder to implement RESIZE on a GPU, as I don’t believe they support different rounding wtihin nearest sampling. I’ll check with our GPU people to see if I’m mistaken here.

I’ll also look at whether an adjustment to the offset/scale can result in the right sampling by fractionally shifting.

I’m curious if you have seen a pattern in the ONNX networks with these, where a given source generates the different rounding modes? (Like PyTorch always giving you floor and TF always round_prefer_ceil)

Thanks,
Eric

Hello Eric,

Thank you for taking a look into this.

Regarding your last paragraph, I would say that there is no specific pattern regarding which rounding mode gets generated for ONNX RESIZE. I am seeing ONNX’s RESIZE operators with NEAREST mode and round_prefer_floor, floor and round_prefer_ceil as rounding mode getting generated from the same frontend, TF in this case.

Greetings,
Robert

Sorry for the delay Robert. I’m still trying to work out whether we can accomplish the equivalent functionality with offset/scale changes. I’ll aim to have a better answer next week, but didn’t want you to think I had forgotten this.