Avoid undefined behavior by union type punning in round_to_bfloat16#41070
Avoid undefined behavior by union type punning in round_to_bfloat16#41070N-Dekker wants to merge 1 commit intotensorflow:masterfrom
Conversation
|
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
|
@googlebot I signed it! |
|
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
jaingaurav
left a comment
There was a problem hiding this comment.
Could you remove the FP32 union on line 180 as well?
Also adding @rmlarsen to the review.
69de499 to
3720dbc
Compare
|
@N-Dekker: Could you also make this change in Eigen? The relevant file is https://bitbucket.org/eigen/eigen/src/default/Eigen/src/Core/arch/Default/Half.h. |
Thanks for the suggestion, @jaingaurav I never contributed to Eigen before, but I could give it a try. Specifically for their bfloat16 implementation, I think that would be this Unfortunately, it appears that Eigen does not have a convenient Anyway, it would certainly help if this pull request would make it into the master branch of TensorFlow 😃 I'll have another look tomorrow! |
|
@N-Dekker: Yeah I'd recommend updating both Half.h & BFloat16.h. We're trying to merge your changes but there seem to be some BUILD file issues. I think you need to include the absl dependency else we are getting errors like: |
Thank you @jaingaurav, but I'm sorry, I'm not really familiar with the build system! Would it be sufficient to just add "@com_google_absl//absl/base" to tensorflow/core/lib/bfloat16/BUILD ? I guess somewhere here, right? |
|
Yep I believe that should work |
3720dbc to
a8b6ce3
Compare
|
@N-Dekker: I believe there is a ubuntu sanity check failure due to the ordering of the build deps. Could you please address that? |
@jaingaurav: Thanks, I see now in https://source.cloud.google.com/results/invocations/c3882af9-15f4-4c40-b79a-37a233c1d006/targets/%2F%2Ftensorflow%2Ftools%2Fci_build:gen_ci_sanity_out/log I guess that means that it suggests moving the line down, from 18 to 20, right? Do you have a clue why? I'm asking because other BUILD files do have "@com_google_absl//absl/base" in front of their "//tensorflow/core..." dependencies, for example: https://github.com/tensorflow/tensorflow/blob/v2.3.0-rc0/tensorflow/compiler/aot/BUILD#L43 Anyway, I'll give it a try. |
a8b6ce3 to
229cb2f
Compare
|
@jaingaurav Still build failures, unfortunately! I guess because of the "absl/base" dependency as well. Would you suggest me to simply avoid using Abseil for this pull request? (Instead of I'm asking you also because I have another pull request in mind that may or may not use |
|
@N-Dekker: Some of the failures seem unrelated. However, given that we're unlikely to use absl in Eigen an alternate fix without absl would be preferred. |
Use `std::memcpy` instead of union based type punning, to avoid undefined behavior. See also C++ Core Guidelines: "Don't use a union for type punning" https://github.com/isocpp/CppCoreGuidelines/blob/v0.8/CppCoreGuidelines.md#c183-dont-use-a-union-for-type-punning
229cb2f to
34e9fe9
Compare
|
Apologies @N-Dekker but we're actively reworking this code to use the Eigen implementation. As a result, we'll address this casting behavior when performing that work. |
No problem @jaingaurav, , thank you for the information! Would you still suggest me to make a merge request on this issue for https://gitlab.com/libeigen/eigen ? I'm asking also because I was about to prepare another possible improvement: I believe the performance of conversion from an integer type to bfloat could be improved significantly. |
|
FYI I just submitted a bfloat32 merge request to Eigen, albeit on a different subject: "Allow implicit conversion from bfloat16 to float and double", https://gitlab.com/libeigen/eigen/-/merge_requests/163 |
|
@jaingaurav FYI, The |
FYI, the |
Use
std::memcpyinstead of union based type punning, to avoid undefined behavior.See also C++ Core Guidelines: "Don't use a union for type punning"
https://github.com/isocpp/CppCoreGuidelines/blob/v0.8/CppCoreGuidelines.md#c183-dont-use-a-union-for-type-punning