Live data from Hacker News

How to pack ternary numbers in 8-bit bytes

compilade.net

51–55 of 55 posts

Re: How to pack ternary numbers in 8-bit bytes

#51

I was actually mucking with ChatGPT about possible "post-binary" architectures. I ended up feeling that ternary is just icky, because the "middle value" doesn't have an "opposite" Maybe we should do what quaternions did to complex numbers and just jump from 2 to 4: Quaternary CPUs I couldn't understand half of the words it spat out but it turns out that using pairs of "quits" as the atomic computation unit to represe…

I was actually mucking with ChatGPT about possible "post-binary" architectures. Ternary computers were around almost 70 years ago, and first posited well over 100 years ago. Maybe ChatGPT isn't the best place to do research.

For those who are not aware, Soviet scientists actually developed ternary computers back in the '50s: https://en.wikipedia.org/wiki/Setun

Ternary encodings are also popular for data transmission, for example both USB4 and GDDR7 use PAM-3 encoding.

Re: How to pack ternary numbers in 8-bit bytes

#52
post #29

Earlier quoted context omitted.

You can have a single 3x256=768-byte table -- just multiply the input byte by 3 to get the offset into the table, and read out the 3 trits (1 trit per byte) beginning at that offset. ETA: If you're prepared to waste a byte per entry so that entries are 4 bytes wide, on x86 you can use effective address calculation to do the multiplication for you, letting you decode 3 trits in 1 CPU instruction: MOV EAX,[RBX+RCX*4]

There are 5 trits. In any case, if there were 3 trits, I'm not sure what the practical difference between a single table you describe and 3 different 256 byte tables, again depending on the architecture. Yes a single table would have better cache effects for a single read, but presumably? you're doing a lot of reads in an unpacking phase.

> There are 5 trits.

Whoops, of course!

I think storing 5 consecutive bytes per entry in a size-1280 table will still be faster than 5 single-byte lookups in separate size-256 tables, since it only requires 1 (unaligned) dword MOV and 1 byte MOV.

Re: How to pack ternary numbers in 8-bit bytes

#53

Despite my ignorance on what would be involved, I like to fantasize that companies like Apple (who own their own chip design and hardware) can put these ternary pack/unpack operations in hardware with a single instruction for each (and also instructions for performing the various matrix convolutions using ternary numbers) so that we can get on-device LLMs. I suppose with the recent quantized Bonsai models we're alrea…

There is a possibility space there. Some transistors are "naturally" ternary (which we use today for things like binary error correction). Also the Soviet Union went down the rabbit hole of ternary computers and programming languages, which is fascinating history to explore.

The interesting questions are if enough LLMs use ternary enough and for long enough into the future to make investments in ternary hardware worth it. Ternary hardware is probably less general purpose than the modern GPU design that is being used for LLMs today and the crossover of "general purpose floating point matrix math engine" for graphics as well as LLMs a useful one, particularly because we know the graphics revenue models pretty well (and gamers are pretty upset at GPU pricing right now because of LLM demand).

Re: How to pack ternary numbers in 8-bit bytes

#54
post #33
post #11

Earlier quoted context omitted.

If it weren't for Bool as a name, that's just 0 for OK, non-zero for errors with 1 being general failure.

TDWTF code is usually hyperbolized. It might not have been called Bool.

But also it could have been found in the wild in C/C++ code, which both had no "native" Boolean type until surprisingly recently (C++17 and C23! as in 2017 and 2023 standards, irrespectively) so Bool is very much a possible real name and not part of the hyperbole.

Re: How to pack ternary numbers in 8-bit bytes

#55

I was actually mucking with ChatGPT about possible "post-binary" architectures. I ended up feeling that ternary is just icky, because the "middle value" doesn't have an "opposite" Maybe we should do what quaternions did to complex numbers and just jump from 2 to 4: Quaternary CPUs I couldn't understand half of the words it spat out but it turns out that using pairs of "quits" as the atomic computation unit to represe…

I was actually mucking with ChatGPT about possible "post-binary" architectures. Ternary computers were around almost 70 years ago, and first posited well over 100 years ago. Maybe ChatGPT isn't the best place to do research.

> Maybe ChatGPT isn't the best place to do research.

https://news.ycombinator.com/item?id=49010345

There's nothing more cringe than a dumb person being judgmental

Post reply on HN