oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Choice of INT8 rounding mode for benchdnn

Open alenik01 opened this issue 5 years ago • 9 comments

Good morning.

When running benchdnn unit test with INT8 configuration (for example, with --cfg=u8s8u8) the final step is comparison with a reference FP32 implementation, and the rounding to an integer value is performed. The rounding is done via mxcsr_cvt, which is equal to nearbyintf on non-x86 machines and rounds a float down to the nearest even int (e.g., 40.5 --> 40). However, some applications (TFLite, ACL) use "half up" rounding mode, i.e. 40.5 --> 41. May you please advise on the following: is it possible somehow to choose/implement a different rounding mode in oneDNN for a non-x86 architecture?

Best wishes, Aleksandr

alenik01 avatar Jan 05 '21 17:01 alenik01

Hi @alenik01, thank you for a question. According to cppreference.com:

1-3) Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.

It means that highly likely, floating point result produced was not 40.5, but 40.499999999 (or something like that) which was printed in the output as "40.5". It will eventually be rounded to 40 rather than to 41 (in case "round to nearest" mode was set (which is usually a default one)).

As a product we made a decision to get rid of rounding modes when moving from v0 to v1, since nobody used them on API level and they brought rather confusion that actually solved any problem.

BTW, AFAIR, benchdnn fills convolutions with integers numbers, thus, the final result for each point should remain pure integer. Is it a some modification of a case? Could you also, please, share a repro line and HW config, so that we may try to reproduce the behavior on our side? Thanks.

dzarukin avatar Jan 06 '21 04:01 dzarukin

Hi @alenik01. To add to @dzarukin answer. Numerical behavior is a tricky question, and for now, we just preserve the status quo and rely on whatever mecanism is provided for a supported platform. On x64, that mechanism relies on the FP mode and is exposed in C through the FE environment. Users already rely on those, to change rounding mode and set the denormal handling behavior (e.g. in Tensorflow and in PyTorch). We hence use nearbyintf as it was the only float function that would round to an integer and respect the current rounding mode (see here).

On GPU, the "FP mode" is mostly set at kernel generation time and the currently used practice is to follow whatever is the default mode for the used runtime.

From your example, as @dzarukin mentioned two things could be happening:

  • the result is not 40.5 but 40.4999... In that case, maybe the default rounding mode used in nearbyintf is nearest tie to even or nearest tie away from zero and the library implementation uses round toward +inf (see here).
  • the result is really 40.5 but the default rounding used in nearbyintf is nearest tie to even while the library implementation uses nearest tie away from zero (see here ) Providing a reproducer might help investigate the exact rootcause of what you are seeing.

In any case, the current library practice is to conform to the standard practice of each backend. However, if the standard practice on ARM is different than the default C behavior, it might require tricky changes to the reference implementations, since they might be used with the ARM and x64 backends. So unless there is a compelling argument to make the ARM and x64 behavior diverge, I would recommend to stick with the C/C++ specification and use the FE environement.

mgouicem avatar Jan 06 '21 05:01 mgouicem

@dzarukin @mgouicem , many thanks for the detailed answers.

the final result for each point should remain pure integer

My question is about the behaviour in benchdnn check of results, where fp0 is the result from the reference convolution performed in FP32, which is rounded to an integer and compared with dt, result from an implementation under testing. fp0 = 40.5 is produced due to particular scale value for quantized integers:

./tests/benchdnn/benchdnn --conv --cfg=u8s8u8 --dir=FWD_I --attr-oscale=per_oc:2.25 ic3ih224oc64oh112kh7sh2ph3n"resnet_50_v1_5:conv1"

[ 35][DST][0,0,0,0,0,35] fp: 40 fp0: 40.5

the standard practice on ARM is different

It is not architecture-specific, but Compute Library-specific, where rounding towards positive infinity is implemented. In the example above ACL rounds 40.5 to dt = 41 and not to 40, because it uses towards +inf and not nearest tie to even. This question is more about how to provide INT8 support in oneDNN on AArch64 using ACL.

alenik01 avatar Jan 07 '21 21:01 alenik01

Thanks for the explanation. The problem I see now is that the ref implementation would round according to the standard C FE environment, whereas the ACL implementation will not. This would cause some inconsistencies:

  • between library implementations, since ACL impl will round up and ref impl will round according to FE env
  • between the library (ACL impl round up) and benchdnn (which rounds wrt to FE env).

If we provide a mechanism to control rounding mode, I see two issues:

  • if we make the mechanism default to FE env, then we have the inconsistency issue mentioned above (current situation)
  • if we make the mechanism default backend-specific (so each backend picks what they want), users that currently rely on FE env, will be impacted and will need to adopt the oneDNN mechanism.

Another option (maybe not the best) would be to just assume that all impl for aarch64 backend round up. In that case, we would need to modify the testing to have a backend-specific rounding specified, and the reference implementation as well, to remove the above-mentioned inconsistencies.

mgouicem avatar Jan 07 '21 21:01 mgouicem

Hello all, this is a rather interesting topic.

I do agree with @dzarukin that rounding and rescaling is a topic that does indeed cause confusion. As mentioned above TensorFlow Lite (and ComputeLibrary as well) currently rounds with ties away from zero and I think this was mainly to avoid bias being introduced, something that is rather true if your data are centered around zero. We have seen recently changes in TensorFlow Lite that try to simplify the rounding process and make it more friendly to multiple targets as the above mode can be quite "expensive". Worth noting that the calculation in both take place in the fixed point domain, which is most likely due to the use-cases that are targeted by these libraries where in some embedded IPs floating-point support might not be present thus bit-exactness can be difficult to be achieved otherwise.

As mentioned above all operations in ComputeLibrary take place in the integer domain whilst from what I gather OneDNN performs the re-quantization in the floating point domain, then rounds and performs any post-process operations. I am not sure that the fp32 approach can generally match the fixed point and a slight difference of +/- 1 is to be expected. Now this should be fine to do for a single operator but not quite sure how this subtle difference can affect the overall accuracy of a network.

So on the proposed approaches:

  1. Allow a slight difference of -/+ 1 for a given backend if possible This should be fine as mentioned at operator level but might impact overall model accuracy
  2. Make round away from zero the default rounding mode for aarch64 As mentioned not sure it will work for all the cases unless a bit exact fixed point reference implementation is in place. Moreover, what will happen if a trained model needs to be deployed into multiple architectures ?
  3. As per @dzarukin we could decouple the convolution up to the s32 result and dispatch this to ComputeLibrary while rescaling and rest post-ops are scheduled through OneDnn. This could impact a bit performance but don't think this will be major. This is something that we are also looking at in our Tensor Operator Set Architecture (TOSA) specification which intention is to promote consistency.
  4. Implement another rescaling mode in ComputeLibrary to do the rescaling in the floating-point domain. This will be pretty much similar with the above, as we will most likely do it as a separate computational block and not touch the existing optimized integer kernels.

Not sure if I am missing something. What are your thoughts?

Thank you in advance, George

GeorgeARM avatar Jan 08 '21 20:01 GeorgeARM

Thank you for the clarification @GeorgeARM!

Indeed, you are correct about the numerical flow of oneDNN. The convolution will consume s8/u8 inputs and weights, do all accumulations in s32, convert to f32, and apply scales and post-ops. When all is done, any necessary downconvert w
ill happen. The equation is hence (with f32 computation in bold):

downconvert dst_datatype ( dst_zero_pointf32 + postopsf32 (oscalef32 * convertf32 (ops32(srcint8, weightsint8))))

My understanding is that with the current ACL integration, the fp32 values above are converted to integer (is it fixed point or rational representation?). Then those f32 computation happen in fixed point arithmetic, correct?

Now on the proposals (I assume that we are talking only about the op + scale sequence, without eltwise or other postops):

  1. Unfortunately, this does not seem reasonable, since there is an expectation that we use the same numerical model for all implementations on a given platform. In particular, this is needed when our user do calibration of their scaling factors. If the scaling factors are calibrated with ACL implementations but used with jitted implementations once deployed, this could lead to accuracy gaps at the topology level. If differences are arch specific (e.g. on architecture X, you get this behavior, and on arch Y you get this other behavior), then we could be fine just documenting that. The only existing example currently are a. AVX2 architectures (documented here) b. and AMX enabled architectures (AMX does not rely on MXCSR, just round to nearest tie-even is available).
  2. This sounds reasonable. However, we would need to properly document those numerical differences between backend. As I mentioned, the GPU backends already have a different behavior vs the CPU backends. The tricky part would be a. testing, since our test need to be compatible with each backends, b. all implementations for a given backend should have the same behavior. Currently it is not the case since library reference, jit-based and ACL based implementations are not aligned for the AARCH64 backend.
  3. This looks like a valid option. Only downside I see is performance (as you mentioned) and power consumption since you would do multiple passes over memory. Is this ok to you?
  4. This should work since it would mimic the loss of precision from s32->f32 conversion and accuracy of rescaling. However, if you want it to be aligned with other aarch64 implementations (reference or other) it would still need to rely on FE env to respect the current rounding mode.

mgouicem avatar Jan 11 '21 18:01 mgouicem

Thank you @mgouicem for your detailed explanation, it was very helpful.

Going through your response and in order to not restrict the use-cases that workloads get dispatched in ComputeLibrary we most likely need a combination of the last two points.

As you mentioned oneDNN JIT generates the kernels and can support arbitrary number of post-ops before down-converting, something that unfortunately ComputeLibrary does not support. Thus, the easiest way to make sure the efficient ComputeLibrary implementations are utilized is to dispatch just the core convolution when multiple post-ops or unsupported configurations take place while the rest is handled by oneDNN. In addition, we could provide an optimized path for "common" configurations that are natively supported by ComputeLibrary.

Now in terms of rounding, if we are to take this path we will need to implement your equation in our optimized path respecting the FE env as you mentioned to ensure alignment.

Finally, I wouldn't expect the impact on performance and power consumption to be that big, at least this is an initial approach we could work on for now.

These are my thoughts so far. Are there any caveats or angles in your opinion that are not captured?

Thank you in advance, George

GeorgeARM avatar Jan 14 '21 18:01 GeorgeARM

That sounds good and would align all CPU backends!

mgouicem avatar Jan 15 '21 04:01 mgouicem

Closing since this question seems resolved. If the issue is not fully addressed, feel free to reopen. Thanks!

mgouicem avatar Jan 22 '21 17:01 mgouicem