Unexpected clip operator result


#1

I have a small test case that gives me unexpected output.

data = relay.var("data", shape=(1, 5), dtype="float32")    # --> [63, -4, 76, 0, 60000]
func = relay.op.clip(data, a_min=-32768, a_max=32767)      # --> [63, -4, 76, 0, 32767]
func = relay.op.cast(func, dtype="int16")                  # --> [63, -4, 76, 0, 32767]
func = relay.op.clip(func, a_min=0, a_max=65535)           # --> [0, 0, 0, 0, 0]

At the end of each line, I am showing the intermediate output. Can somebody help me explain the result of the last line?


#2

I think I know what going on here. The clip op a_max is greater that max_limit of int16_t. So, it wraps around, effectively making a_max 0.

Should this be handled internally in the clip operator?

@yzhliu @kevinthesun @zhiics


#3

It looks numpy has the same behavior.