OpenBLAS icon indicating copy to clipboard operation
OpenBLAS copied to clipboard

BFloat16 data type naming

Open Guobing-Chen opened this issue 6 years ago • 54 comments

In current code base, the BFloat16 data type is named as shxxxx (e.x.: shgemm), and related build flag as BUILD_HALF. Seems we simply take BF16 as half precision of float. This is not true as to the standard definition of IEEE. Half precsion should be FP16, which is different in format and content as compared with BFloat16. As for OpenBLAS, we may both support BFloat16 and and FP16 as valuable for different domains -- BFloat16 mostly valuable for Deep Learning and Machine Learning, while FP16 more valuable for traditional scientific computation and telecom processing.

Suggest to change the naming of data type and build flag to be bxxxx (e.x.: bgemm) and BUILD_BF16. While we can leave shxxxx for the real half precision data type -- FP16, or even make it as hxxxx (e.x.: hgemm). Whatever, use shxxxx and BUILD_HALF is quite confusing to community, every other math libs like Eigen/oneDNN (previously mkldnn) use keywords like bf or bf16 or so.

I can submit PR to make this change if we are OK on this.

Guobing-Chen avatar Aug 11 '20 03:08 Guobing-Chen

I just hope nobody has discovered the current "shgemm" for their project so that we can safely repurpose the name. And thanks a lot for your kind wishes, I do find that my childhood wish to live entirely on ice cream has not aged well :-/ But this still will not stop kids dreaming the same or similar in future even shown them the pain :).

As for the renaming, my previous PR #2771 for the changing can be a base for this though that I need to refactor a bit to echo latest code base. Is that way to reopen it? Or I can create a new one.

Guobing-Chen avatar Sep 15 '20 01:09 Guobing-Chen

So are there any objections against changing the prefix to "sb" (and the build option to something like BUILD_BFLOAT I guess) so far ? Anybody else we should invite to comment on this matter ?

The prefix "sb" is okay, though it might be useful to see the wider implications/extensions of this.

By default, each member of the gemm family stays within its precision (eg. single, double), so it would make sense to also have a "bgemm" function where the calculations stay within bf16. Then there are also complex versions, where the single letter limitation for prefixes is problematic.

The extended gemm family would look like so:

  • bgemm: bf16 input, bf16 output
  • hgemm: fp16 input, fp16 output
  • sbgemm: bf16 input, fp32 output
  • shgemm: fp16 input, fp32 output
  • sgemm: fp32 input, fp32 output
  • dgemm: fp64 input, fp64 output
  • ?: bx16 input, bx16 output
  • ?: bx16 input, cx32 output
  • ?: cx16 input, cx16 output
  • ?: cx16 input, cx32 output
  • cgemm: cx32 input, cx32 output
  • zgemm: cx64 input, cx64 output

where:

  • bf16 = brain float (16 bit), as per https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
  • fp16 = IEEE half precision float (16 bit)
  • fp32 = IEEE single precision float (32 bit)
  • fp64 = IEEE double precision float (64 bit)
  • bx16 = complex number comprising two brain floats
  • cx16 = complex number comprising two IEEE half precision floats
  • cx32 = complex number comprising two IEEE single precision floats
  • cx64 = complex number comprising two IEEE double precision floats

Not sure if there are clear use cases for bx16 and cx16 yet, though this shouldn't be discounted.

conradsnicta avatar Sep 15 '20 03:09 conradsnicta

I foresee no problems with bgemm (if there is a need for this in AI), and I guess we could adopt v for complex b (again if this is to play any role in AI computations) which might make w a natural choice for its IEEE counterpart ?

martin-frbg avatar Sep 15 '20 08:09 martin-frbg

AI (in particular deep learning) is still a fast moving field. There are some academic papers that promote the use of complex matrices in deep learning and the techniques could find a place in production code in the future. I am not sure I have seen any uses of half-precision complex, but it is a good idea to plan for that and at least "reserve" a letter.

And yes, there are also experiments with both input and output being bfloat16, although all practical uses I know have fp32 as output.

IEEE 754 defines a total of 8 formats (5 binary and 3 decimal), not all of them for computing. (For example, IEEE fp16 is not really defined for computing.) So that would consume 16 letters. Take 2 more for bfloat16 and we could still add 4 other formats.

joseemoreira avatar Sep 15 '20 10:09 joseemoreira

I can see a few potential use cases for bf16 and bgemm in machine learning workloads other than neural networks / deep learning. Though this is probably restricted by the level of CPU/GPU support for bf16 computations in the near-term (eg. support for computations other than bf16*bf16 -> fp32, which is the current plat du jour). I haven't looked into the plans (if any) by Intel/AMD/Nvidia/ARM/IBM/etc for expanded handling of bf16.

@joseemoreira Can you comment on what IBM is planning in the near and medium terms for the supported set of bf16 arithmetic operations?

... we could adopt v for complex b (again if this is to play any role in AI computations) which might make w a natural choice for its IEEE counterpart ?

That looks workable, but it is starting to resemble a random letter salad :)

conradsnicta avatar Sep 15 '20 12:09 conradsnicta

That looks workable, but it is starting to resemble a random letter salad :)

I am open to alternatives, b/v does at least sound a bit similar (in english), so perhaps not worse than the established d/z pair. (b/h should make some sense to musicians but is obviously forbidden. We could look into using accented characters - should give the user a choice of acute or grave result ?)

Also intrigued by joseemoreira's comment that IEEE fp16 is "not really defined for computing", which would explain why I did not encounter it anywhere outside earlier graphics programming - from the complaints about the "sh" naming I would have thought there are actual use cases unbeknownst to me and not just semantics as a matter of principle ???

martin-frbg avatar Sep 15 '20 15:09 martin-frbg

Accented characters are probably a no go, as it would confuse too many people. I suspect a large proportion of users (if not the majority) wouldn't know how to type accented characters anyway. They certainly require more effort than "plain" Latin characters.

The sb prefix is a workable solution that stays within the current naming tradition. It's entirely possible that's one of the main use cases (bf16*bf16 -> fp32) for the foreseeable future, under the assumption of neural network applications.

However, if going beyond the scope of this, a more informative prefix (or suffix) would be helpful, along the lines of b16_b16_gemm, f16_f16_gemm, f32_b16_gemm, f32_f32_gemm, c32_c32_gemm, etc. This type of approach would be far less confusing than single/double letter prefixes to handle a large number of possible permutations.

... IEEE fp16 is "not really defined for computing", which would explain why I did not encounter it anywhere outside earlier graphics programming

I think the main reason for this perception is that there is (was?) no proper hardware support for fp16 on CPUs. I have used fp16, but was forced to use a GPU because the CPU required conversion of fp16 to fp32 before doing any calculations (thereby providing no speed advantage).

Related:

  • https://scicomp.stackexchange.com/questions/35187/is-half-precision-supported-by-modern-architecture
  • https://stackoverflow.com/questions/49995594/half-precision-floating-point-arithmetic-on-intel-chips

conradsnicta avatar Sep 16 '20 01:09 conradsnicta

Accented characters are probably a no go, as it would confuse too many people. I suspect a large proportion of users (if not the majority) wouldn't know how to type accented characters anyway. They certainly require more effort than "plain" Latin characters.

The sb prefix is a workable solution that stays within the current naming tradition. It's entirely possible that's one of the main use cases (bf16*bf16 -> fp32) for the foreseeable future, under the assumption of neural network applications.

+1 for that.

However, if going beyond the scope of this, a more informative prefix (or suffix) would be helpful, along the lines of b16_b16_gemm, f16_f16_gemm, f32_b16_gemm, f32_f32_gemm, c32_c32_gemm, etc. This type of approach would be far less confusing than single/double letter prefixes to handle a large number of possible permutations.

The ICL guys are working on the next gen BLAS API standard, which exactly points to non-single-letter prefix, you can refer to @mgates3 's comments in this thread.

Guobing-Chen avatar Sep 16 '20 02:09 Guobing-Chen

Regarding the question by Conrad on IBM support for bf16: IBM Power ISA 3.1 has been published. That is what POWER10 will support. The bf16 instructions include the new MMA (Matrix-Multiply Assist) instructions that do a 4x2x4 matrix multiply, using a 512-bit accumulator and two 128-bit vector inputs, and conversion instructions to/from IEEE fp32. We have similar instructions for IEEE fp16. (Therefore, both sbgemm and shgemm could be supported in POWER10.)

I am not as familiar with IBM z Architecture. Don't know what they have in that area.

It is really too early to know what we will do in processors beyond POWER10. People have asked me for more comprehensive support, but that is ongoing work.

joseemoreira avatar Sep 16 '20 02:09 joseemoreira

Hello. I have not seem much discussion here recently.

Do we have an acceptable solution to use "h" and "b" as the single-letter prefix for IEEE binary16 and bfloat16 data types, respectively? And that they would be used within the "sh" and "sb" two-letter prefix for the proposed mixed-precision operations in BLAS?

If those are OK, then I would like to bring up the issue of operations on integer matrices. Those have become popular in deep learning and are already supported in IBM and Intel processors. (More support in POWER10.) Given the variety of integer formats, and the additional option of signed and unsigned integers, this seems to me a good time to break with the traditional approach and move on to the BLAS G2 suffix-based approach. To get started, here are some suggested suffixes:

s64 : signed 64-bit integer s32 : signed 32-bit integer s16 : signed 16-bit integer s8 : signed 8-bit integer

u64 : unsigned 64-bit integer u32 : unsigned 32-bit integer u16 : unsigned 16-bit integer u8 : unsigned 8-bit integer

Like I said, these are just some suggestions to get the discussion started. (But a subset of the above is already in use in some libraries.) I have seen other suffix proposals for integers, including from the ICL team.

joseemoreira avatar Oct 07 '20 15:10 joseemoreira

At least I haven taken the sudden quiet to mean there is no objection to the renaming, and I still plan to do it before the 0.3.11 release. (Currently stuck on the build system rearrangements necessary to allow building only the parts of the libary for a particular type, but will put that on hold if necessary so that we do not get two releases with the "wrong" API).

No objections from me regarding naming of the integer operations - there are obviously too many types to represent them with sensible single-letter prefixes. (Though perhaps IFF there is one particularly popular type, we could create an old-style alias for it, thinking IGEMM and possibly UGEMM for its unsigned counterpart)

martin-frbg avatar Oct 08 '20 14:10 martin-frbg

(Though perhaps IFF there is one particularly popular type, we could create an old-style alias for it, thinking IGEMM and possibly UGEMM for its unsigned counterpart)

I'd argue against that, as there's no concrete size of an integer. On 64 bit platforms it could be either 32 bits or 64 bits. (eg. see the mess with the "long int" and "long long" types in C++11: https://en.wikipedia.org/wiki/C++11#Type_long_long_int)

One possible approach is to forcefully assume that the integer size is 32 bits on 32 bit platforms, and 64 bit on 64 bit platforms. However, this can lead to confusion and is inconsistent with the behaviour of the prefix in the sgemm/dgemm/cgemm/zgemm cases (ie. s = always 32 bits, d = always 64 bits, etc).

conradsnicta avatar Oct 12 '20 01:10 conradsnicta

That is why I suggested to do this only if there is some widely used type that "could" be considered the "typical" integer for current deep learning algorithms, e.g. if "everybody" was already gravitating towards s32 or u16 we could give that one the IGEMM alias. Of course this would only create confusion when "everybody" uses what best suits their particular platform and problem. I am not that familar with ML.

martin-frbg avatar Oct 12 '20 06:10 martin-frbg

Juste to note ArmV8.2 have fp16 Neon compute support... If someone want to test the Cortex-A55 support it ... can be found on https://www.hardkernel.com/shop/odroid-c4/...

Djip007 avatar Jun 19 '21 01:06 Djip007