Hacker News new | past | comments | ask | show | jobs | submit login
Float Toy (evanw.github.io)
142 points by agomez314 on Aug 20, 2022 | hide | past | favorite | 53 comments



It's interesting today to see people act as though half (f16) is a completely normal obvious type, whereas when I was first writing code nobody had this type, it was unheard of, and it was this weird new special case when the 3DFX Voodoo used it (hardware accelerated 3D video for the PC, as a special case, the "pass through" for 2D video is a physical cable at first). The give away, if you're younger, is that it's sometimes called half precision. That's because single precision (32-bit floating point) was seen as the starting point years before.

I remember this when somebody says f128 will never be a thing, because if you'd asked me in 1990 whether f16 will be a thing I'd have laughed. What use is a 16-bit floating point number? It's not precise enough to be much use in the narrow range it can represent at all. In hindsight the application is obvious of course, but that's hindsight for you.


I still don't see the point of half precision. What applications are you implying are obvious? Actually curious.


It's very popular in hardware-accelerated computer graphics. It has much more range, and a bit more precision, than the traditional integer 8-bits-per-channel representation of colour, so it is used for High Dynamic Range framebuffers and textures. It's also ubiquitous as an arithmetic type in mobile GPU shaders, where it's used for things (like colours) that need to be floats but don't need full 32-bit precision. In many cases it doesn't just save memory bandwidth and register space, but also the shader core may have higher throughput for half precision.


There is a good discussion and examples here:

https://en.wikipedia.org/wiki/Half-precision_floating-point_...


Makes me wonder if there's a use case for "dynamic fixed point" numbers: say for a 16 bit value the upper 2 bits are one of four values that says where the decimal is in the remaining 14. Say 0 (essentially an int), two spots in the middle, and 14 (all decimal). The CPU arithmetic for any operation is (bitshift+math), which should be an order faster than any float operation. The range isn't nearly as dynamic, but would allow for fractional influence. Maybe such a system would lack enough precision for accuracy?


What you just described is exactly floating point numbers, you're just using a different split for the exponent and mantissa and not using the "integer part is zero" simplification.


hmm interesting, I never saw them that way. But now that you say that, it makes them "click" a lot more. Thanks!


:D

Yeah, floating point is nothing more than your standard scientific notation of numbers, e.g.

    digit.xyz... * 10 ^^ +/- some exponent
The exponent is simply shifting where the decimal point is. The only different for floating point is that everything is base 2 because computers :D

Interestingly you're right that a bunch of fp functions can be faster than integers equivalents (although I'm still not convinced that this isn't simply due to the reduced number of bits involved), and more fun the relative performance of operations can actually change vs what they would be in integers. Also this is in the context of doing it in software vs hardware, where again the perf costs of things change.


It does have several additional attributes:

1. Since the normalized significand will always be 1.bbbb, the '1' bit is stripped from the significand representation, except:

2. To extend the range, the lowest 'zero' value of the exponent drops the leading '1'. This is referred to as the subnormal range

3. The highest exponent value, when the significand is zero, is used to represent positive and negative infinity

4. The highest exponent value with a non-zero significand is used to represent NaN

5. There are many different values usable for NaN by software, including a differentiation between 'quiet' NaNs and a (I believe implementation optional) 'signaling' variant, which will raise an interrupt when used. The idea is that these can be used to convey additional information, and that the signaling variant as well as the right interrupt handlers can be used to add additional functionality such as variable substitution.

6. Zero is signed


Yes, I was giving a simplified description to convey how to translate what floating point is to something people are more familiar with.

The technical details of how it handles _every_ case weren’t particularly relevant.

However just to address 1,2 with some hilariousness (autocorrect wants this to be “hilarious mess” which may be more correct).

Ieee754’s 80 bit format was the first widely deployed format, and was largely used by intel to get the other manufacturers to stop trying to reduce the functionality of ieee floating point because “it couldn’t be implemented, implemented efficiently, etc”. However because of that it has a quirk that was fixed for fp32,64,etc.

FP80 uses an explicit bit for the leading 1. That means it can do 1.0 * 2 ^^ N, or 0.1 * 2 ^^ N it should hopefully be immediately obvious why this could be a problem :)

Not only do the multiple representations for a single value result in sadness, it also gives us a variety of concepts like pseudo-denormals, pseudo normals, pseudo infinities, pseudo nans, etc all of which cause their own problems.

Mercifully by default the only hardware fp80 implementation now (since 286 maybe?) defaults to just treating them as invalid and converts to Nan. But you can set a flag to make it treat them as it did originally.


It’s by far the most popular data type for training neural networks.


for lack of bfloat16 support


whats the difference between float16 and bfloat16?


number of exponent bits. bfloat16 has a larger dynamic range with lower precision. e.g. this would be infinity in fp16 https://float.exposed/b0x5f80


The OpenEXR file format, used a lot in graphics applications (compositing, rendering), is a fairly well-known application of half-floats.

There's some notes about the advantages of half-float pixel in the openexr documentation: https://openexr.readthedocs.io/en/latest/TechnicalIntroducti...

I don't think "obvious" was the best adjective, but "small memory/file size footprint" is probably the quality that's easiest to understand.


Interestingly enough, the people at Tesla are using a CFloat 8 format to get the most speed. Sometimes what you need is speed in processing.


ML applications apparently don't need the full precision, but they do need very large amounts of them, and process enough of them that the perf win from fewer bits is meaningful and the cost of f16->f32 is large enough to also be meaningful.

Presumably they do benefit from the dynamic range as otherwise you'd think int16 would be sufficient, and not suffer the conversion costs.


float16 and float128 (as well as longer formats) were standardized in IEEE754 in 2008. Half is now built into C#, many other languages, and gaining ground in hardware support.


I definitely don't think half precision is completely normal obvious type except maybe in certain circles. None of current mainstream languages support it as a primitive type, for one. No mainstream CPUs have hardware support for half precision.


C# supports it. CUDA supports it. ARM and related C/C++ compilers support it. Intel is adding hardware support in upcoming chips, so expect a lot more languages to add it.


Julia also supports it. it led to a really funny graph when doing performance tests on Fujitsu, because Julia is the only language that supports fp16 and is fast enough to write blas in, so there were some graphs where it was the only entry because c and fortran didn't bother showing up.



Most Intel and AMD CPUs produced in the last ~10 years have hardware support for converting 16-bit floats to and from 32-bit. That's not full hardware support of course, but you don't need it to be able to make good use of them. https://en.wikipedia.org/wiki/F16C


Except when it comes to SIMD. Having twice the lanes compared to f32 would be a real benefit.


Makes me think of this https://float.exposed/


Ah Evan Wallace, the creator of esbuild, CTO of Figma, and maker of one of my favorite tools, Diskitude [0] which shows disk space usage on Windows, all under 10 kilobytes.

[0] https://madebyevan.com/diskitude/


Looks like this does not read the MFT. So I’d expect it to take minutes instead of seconds to scan a typical 2TB SSD.


Looks like you're right. Any tools you know that do?


WizTree is a program that does and is pretty useful


TreeSize Free does. I’m not aware of any open source alternatives :(


One thing that took me a long time to grok that I wish this toy showed is how the fraction is included in the calculation underneath.

Right now the following bits

0 10001 1000000000

show the calculation as

1 x 2^2 x 1.5 = 6

when it's more clear to actually say

1 x 2^2 x (1 + (512 / 1024)) = 6


It has a hidden UI trick: click+drag over the digits to turn them all to 0 or 1.


A while ago I realized that a very fast way to multiply or divide a 32-bit float by 2^n is to simply reinterpret it as int and add/subtract n<<23. Plus some checks if you care about overflow. Obvious in retrospect, and may not be worth it on modern desktop hardware, but still a nice trick. (This was in the context of optimizing a texture sampler for power-of-two textures.)


One place where this is used in practice is in the code to compute floating point exponentials.


You can do lots of interesting things with bit shifts.

Like the famous Quake3 Fast Inverse Square

https://www.beyond3d.com/content/articles/8/


Plenty of languages have this in their standard libraries for exactly this purpose. I use it in C/C++ and C# frequently in numerical code.


Oh wow, I had totally missed that C has such functions as `ldexp` and `scalb*`.


Might be worth an explicit link to an example illustrating subnormals [1] - they seem to be one of the not-well-understood features of floating-point.

[1] https://en.wikipedia.org/wiki/Subnormal_number


Boo, no fp80!

Honestly I think having fp80 would be super interesting as it was the first end-user available hardware implementation of ieee definition of floating point, and also as a by product shows some design "mistakes" that were ironed out by the time fp32/64 were formalized.


Also vaguely relevant https://0.30000000000000004.com/


Representing 1/10 with fractional powers of two is an infinite series, so tenth of a whole number will not be exactly representable with IEEE floating point.

In financial apps, it is perhaps more common to use specific numerical types that don't lose precision.

One simplistic option is to simply represent the smallest unit (say, tenths of a penny) with a fixed point number rather than a unit like USD dollars in floating point.


I didn't know different binary numbers could map to NaN. That is interesting.


a popular trick is to encode information in NaNs


During my research projects I used this website incredibly often. I even cited it in one of my papers :)


Fun fact, this site is from the co-founder of Figma.


Also see https://float.exposed/0x44bf9400 by the great ciechanowski, with a lengthy article here: https://ciechanow.ski/exposing-floating-point/


Is that the man who did the introduction to category theory for programmers. Although I never grokked it as much as I would like, I appreciated that video series, for trying to make the subject so accessible.


No. This Bartosz is a fantastically talented IT person who, in their spare time, creates hand crafted interactive explanations of stuff. https://ciechanow.ski


That was Bartosz Milewski, see bartoszmilewski.com .


I had to implement floats and doubles a bit ago. Was a fun bit of puzzling to figure that out as a small break from other work.


Then I found out about weird poorly defined NaNs, which were kinda annoying, but fun nonetheless.


Cool - thanks! - just started work on an FPU .... would prefer it of the bits were numbered from 0 though


This is amazing. It really demonstrates how JavaScript is such an important technology. I can see some patterns already from playing with this, as someone without much math and CS knowledge.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: