beating floating point at its own game: posit arithmetic

59
Beating Floating Point at its Own Game: Posit Arithmetic John L. Gustafson Visiting Scientist at A*STAR and Professor at National University of Singapore

Upload: others

Post on 21-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Beating Floating Point at its Own Game: Posit Arithmetic

Beating Floating Point at its Own Game: Posit Arithmetic

John L. GustafsonVisiting Scientist at A*STAR and

Professor at National University of Singapore

Page 2: Beating Floating Point at its Own Game: Posit Arithmetic

The “Memory Wall”Operation Energy

64-bitfloating-point multiply-add 0.2nanojoulesRead64 bitsfrommemory(DRAM) 12nanojoules

Theissueiscommunication,notcomputation:Runtime,partscost,electricpower.

Whatifwecouldcutcommunicationinhalfbydoublinginformationperbit?

Page 3: Beating Floating Point at its Own Game: Posit Arithmetic

Decades of asking “How do you know your answer is correct?”

• “(Laughter) “What do you mean?”• “Well, we used double precision.”• “Oh. We always get that answer.”• “Other people get that answer.”

Page 4: Beating Floating Point at its Own Game: Posit Arithmetic

Precision versus Accuracy

150,000 pixels 432 pixels

Page 5: Beating Floating Point at its Own Game: Posit Arithmetic

Metrics for Number Systems• Relative Error = |(correct x – computed x) / correct x|• Dynamic range = log10(maxreal / minreal) decades• Decimal Accuracy = –log10(Relative Error)• Percentage of operations that are exact

(closure under + – × ÷ √ etc.)• Average accuracy loss when inexact• Entropy per bit (maximize information)• Accuracy benchmarks: simple formulas, linear equation solving,

math kernels, and most importantly, complete applications

Page 6: Beating Floating Point at its Own Game: Posit Arithmetic

COMPUTERS THEN

Page 7: Beating Floating Point at its Own Game: Posit Arithmetic

COMPUTERS NOW

Page 8: Beating Floating Point at its Own Game: Posit Arithmetic

ARITHMETIC THEN

Page 9: Beating Floating Point at its Own Game: Posit Arithmetic

ARITHMETIC NOW

Page 10: Beating Floating Point at its Own Game: Posit Arithmetic

Analogy: Printing Technology1970: 30 sec 2017: 30 sec

Page 11: Beating Floating Point at its Own Game: Posit Arithmetic

Challenges for the Existing Arithmetic

• No guarantee of repeatable or portable behavior (!)• Insufficient 32-bit accuracy forces wasteful use of 64-bit types• Fails to obey laws of algebra (associative, distributive laws)• Poor handling of overflow, underflow, Not-a-Number results• Dynamic ranges are too large, stealing accuracy needed for workloads• Rounding errors are invisible, hazardous, and costly to debug• Computations are unstable when parallelized

IEEE Standard Floats are a storage-inefficient,

1980s-era design.

Page 12: Beating Floating Point at its Own Game: Posit Arithmetic

Why worry about floating point?

a = (3.2e8, 1, –1, 8.0e7)b = (4.0e7, 1, –1, –1.6e8)

Single Precision, 32 bits:

Note: All values are integers that can be expressed exactly in theIEEE 754 Standard floating-point format (single or double precision)

Double Precision, 64 bits:a ∙ b = 0a ∙ b = 0

Correct answer: a ∙ b = 2

Most linear algebra is

unstable with floats!

Find the scalar product a ∙ b:

Page 13: Beating Floating Point at its Own Game: Posit Arithmetic

What’s wrong with IEEE 754? A start:• No guarantee of identical results across

systems• It’s a guideline, not a standard• Breaks the laws of algebra:

a + (b + c) ≠ (a + b) + c a∙(b + c) ≠ a∙b + a∙c• Overflow to infinity creates infinite relative error.

IEEE floats are weapons of math destruction.

Page 14: Beating Floating Point at its Own Game: Posit Arithmetic

What else is wrong with IEEE 754?• Exponents usually take too many bits• Accuracy is flat across a vast range, then falls off a cliff• Subnormal numbers are a headache (“gradual underflow”)• Divides are messy and slow• Wasted bit patterns: “negative zero,” too many NaN values

Do we really need 9,007,199,254,740,990numbers to indicate something is Not a Number??

Page 15: Beating Floating Point at its Own Game: Posit Arithmetic

Floats: Designed by a 1980s Committee

exponent mantissa±

“The“hiddenbit”isalways1… unlessit’snot.

Gradualunderflow!”“Sign-magnitude

integer?2’scomplement?1’scomplement?Aw

heck,let’sjustsayyousubtract126.”

“Butsubtract127ifIt’sagradualunderflowcase!”

“Wait,thismakescomparing twofloatsreallycomplicated.

It’snotlikecomparingintegers!”

“IfaresultisNotaNumber,we’llmakeitanumber.That’slogical,right?Callit‘NaN’.Let’shavemillionsofNaNvaluesinsteadofnumericalvalues.”

“Iknow.Let’ssaythatthesquarerootof

‘negativezero’isalsonegativezero!”

“Thesignbitdoesn’tapplytozero.Except…

sometimes.”

“Andthereciprocalofnegativezerocanbenegativeinfinity!Cool.Except…uh-oh…infinityequalsnegativeinfinity?”

“Mycompanywantstouseguardbitssowecansayouranswersarebetter.TheStandardbetterallowthat!”

“Mycompanyhasfusedmultiply-adds.Let’sputthatinthestandardsoeveryoneelsehastoredesigntocatchupwithus.”

“IsthatGermanguystilltellingusweneedanexactdotproduct?Get

himoutofhere.”

“Iftheexponentisall1s,let’ssaythatinfinity iswhenthemantissaisall0s.” “Andifa

numbergetstoobig,we’lljustroundittoinfinity.”

“Wecansticklotsofflags intheprocessor.Someday,languageswillsupportthem.”

“Howshouldweround?Down?

Up?Towardzero?Tonearest?

I’vegotit.Let’sputallfour waysinthere.Thenwedon’thave

topick.”

“Let’shavemoreexponentbitsthanwereally

need.That’llsavetransistors.” Goodidea.Those

transistorsaresoexpensive!

Page 16: Beating Floating Point at its Own Game: Posit Arithmetic

Contrasting Calculation “Esthetics”

IEEE Standard(1985)

Floats, f = n × 2m

m, n are integersIntervals [f1, f2], all

x such that f1 ≤ x ≤ f2

Rounded: cheap, uncertain, “good enough”

Rigorous: more work, certain, mathematical

If you mix the two esthetics, you end up satisfying neither.

“IneedthehardwaretoprotectmefromNaNandoverflowinmycode.”

“Really?Anddoyoukeepdebuggermodeturnedoninyourproductionsoftware?”

Page 17: Beating Floating Point at its Own Game: Posit Arithmetic
Page 18: Beating Floating Point at its Own Game: Posit Arithmetic

Posits use the Projective Reals• Posits map reals to

standard signed integers.

• Can be as small as 2 bits and still be useful!

• This eliminates “negative zero” and other IEEE float issues

Page 19: Beating Floating Point at its Own Game: Posit Arithmetic

Example withnbits = 3, es = 1.

Value at 45° is always

If bit string < 0, set sign to –and negate integer.

useed

useed = 2 es2

Mapping to the Projective Reals

Page 20: Beating Floating Point at its Own Game: Posit Arithmetic

Rules for inserting new pointsBetween ±maxpos and ±∞,scale up by useed.(New regime bit)

Between 0 and ±minpos,scale down by useed.(New regime bit)

Between 2m and 2n wheren – m ≥ 2, insert 2(m + n)/2.(New exponent bit)

useed 2

Page 21: Beating Floating Point at its Own Game: Posit Arithmetic

At nbits = 5, fractionbits appear.Between x and y wherey ≤ 2x, insert (x + y)/2.

Existing values stay put astrailing bits are added.

Appending bits increasesaccuracy east and west,dynamic rangenorth and south!

useed 3

Page 22: Beating Floating Point at its Own Game: Posit Arithmetic

Posit Arithmetic:Beating floats at their own game

Fixed size, nbits.es = exponent size = 0, 1, 2,… bits.es is also the number of times you square 2 to

get useed: 2, 4, 16, 256, 65536,…

Page 23: Beating Floating Point at its Own Game: Posit Arithmetic

Posit Format Example

Here, es = 3. Float-like circuitry is all that is needed(integer add, integer multiply, shifts to scale by 2k)

Posits do not overflow. There is no NaN. Relative error ≤ 1.

Simpler, faster circuits than IEEE 754

= 3.55⋯×10–6

Page 24: Beating Floating Point at its Own Game: Posit Arithmetic

What reals should we seek to represent?

Studiesshowvery rareuseofvaluesoutside10–13to1013.CentralLimitTheorem saysexponentsdistributeasabellcurve.

⟵ Typical distributionof real values usedin computations

10-40 10-20 1 1020 1040

Page 25: Beating Floating Point at its Own Game: Posit Arithmetic

IEEE floats have about 7 decimals accuracy, flat except on the left

Thisshows32-bitfloataccuracy.Dynamicrangeof83decades.For64-bitfloats,exponentrangeisevensillier:631 decades.Isflat accuracyoverahugerangereallywhatweneed?

IEEE float accuracy

⟵ Typical distributionof real values usedin computations

10-40 10-20 1 1020 1040

Page 26: Beating Floating Point at its Own Game: Posit Arithmetic

Posits provide concise tapered accuracy

Positshavesameorbetteraccuracyonthevastmajorityofcalculations,yethavegreater dynamicrange.

Thisisonlyonemajoradvantageofpositsoverfloats.

IEEE float accuracy

⟵ Equal or superior accuracy of posits

⟵ Typical distributionof real values usedin computations

⟵ Equal or superior accuracy of posits

10-40 10-20 1 1020 1040

Page 27: Beating Floating Point at its Own Game: Posit Arithmetic

Posits: Designed by Mathematics

�����

0

�����

1 /64�����

1 /16�����

1 /8 �����

1 /4 �����3 /8

�����1 /2

�����3 /4

�����1

�����3 /2

�����2

�����

3

�����

4

�����

8

�����

16

�����

64

�����

±∞

�����

-64�����

-16�����

-8�����

-4�����

-3�����

-2����� -3 /2

����� -1

����� -3 /4

�����-1 /2

�����-3 /8

�����

-1 /4

�����

-1 /8

�����

-1 /16

�����

-1 /64

• 1-to-1 map of binary integers to ordered real numbers

• Appending bits gives isomorphic increase in precision and dynamic range, automatically

• No “negative zero”• No bit patterns wasted on “NaN”• Simpler circuitry, less chip area• No hidden and unused flags• More information per bit• As reproducible as integer math;

no “hidden scratchpad” work• Obeys mathematical laws

negationsymmetry

reciprocalsymmetry

Page 28: Beating Floating Point at its Own Game: Posit Arithmetic

Posits v. Floats: a metrics-based study• Compare quarter-precision

IEEE-style floats• Sign bit, 4 exponent bits,

3 fraction bits• smallsubnormal = 1/512; maxfloat = 240.• Dynamic range of five orders of magnitude• Two bit patterns that mean zero• Fourteen bit patterns that mean “Not a Number” (NaN)

Page 29: Beating Floating Point at its Own Game: Posit Arithmetic

Float accuracy tapers only on left

• Min: 0.52decimals

• Avg: 1.40decimals

• Max: 1.55decimals

Graph shows decimals of accuracy from minfloat to maxfloat.

Page 30: Beating Floating Point at its Own Game: Posit Arithmetic

Posit accuracy tapers on both sides

• Min: 0.22decimals

• Avg: 1.46decimals

• Max: 1.86decimals

Graph shows decimals of accuracy from minpos to maxpos.But posits cover seven orders of magnitude, not five.

Page 31: Beating Floating Point at its Own Game: Posit Arithmetic

• Min: 0.22decimals

• Avg: 1.46decimals

• Max: 1.86decimals

• Min: 0.52decimals

• Avg: 1.40decimals

• Max: 1.55decimals

What do these remind you of?

Page 32: Beating Floating Point at its Own Game: Posit Arithmetic

• Min: 0.22decimals

• Avg: 1.46decimals

• Max: 1.86decimals

• Min: 0.52decimals

• Avg: 1.40decimals

• Max: 1.55decimals

What do these remind you of?

Page 33: Beating Floating Point at its Own Game: Posit Arithmetic

Both graphs at once

⇦ Posits

⇦ Floats

Where most calculations occur

Page 34: Beating Floating Point at its Own Game: Posit Arithmetic

Matching float dynamic ranges

Note: Isaac Yonemoto has shown that 8-bit posits suffice for neural network training, with es = 0

Page 35: Beating Floating Point at its Own Game: Posit Arithmetic

8-bit posits for fast neural nets

Sigmoid functions take 1 cycle in posits, vs. dozens of cycles with float math libraries.

(Observation by I. Yonemoto)

Page 36: Beating Floating Point at its Own Game: Posit Arithmetic

ROUND 1

Unary Operations

1/x, √x, x2, log2(x), 2x

Page 37: Beating Floating Point at its Own Game: Posit Arithmetic

Closure under Reciprocation, 1/x

Page 38: Beating Floating Point at its Own Game: Posit Arithmetic

Closure under Square Root, √x

Page 39: Beating Floating Point at its Own Game: Posit Arithmetic

Closure under Squaring, x2

Page 40: Beating Floating Point at its Own Game: Posit Arithmetic

Closure under log2(x)

Page 41: Beating Floating Point at its Own Game: Posit Arithmetic

Closure under 2x

Page 42: Beating Floating Point at its Own Game: Posit Arithmetic

ROUND 2

Two-Argument Operations

x + y, x × y, x ÷ y

Page 43: Beating Floating Point at its Own Game: Posit Arithmetic

Addition Closure Plot: Floats18.533% exact

70.190% inexact0.635% overflow

10.641% NaN

Inexact results are magenta; the larger the error, the brighter the color.

Addition can overflow, but cannot underflow.

Page 44: Beating Floating Point at its Own Game: Posit Arithmetic

Addition Closure Plot: Posits

Only one case is a NaN:

±∞ + ±∞

With posits, a NaN interrupts the calculation. (Optional mode uses ±∞ as quiet NaN.)

25.005% exact

74.994% inexact0.000% overflow0.002% NaN

Page 45: Beating Floating Point at its Own Game: Posit Arithmetic

All decimal losses, sorted

Page 46: Beating Floating Point at its Own Game: Posit Arithmetic

Multiplication Closure Plot: Floats22.272% exact51.233% inexact3.345% underflow

12.500% overflow10.651% NaN

Floats score their first win: more exact products than posits…

but at a terrible cost!

Page 47: Beating Floating Point at its Own Game: Posit Arithmetic

Multiplication Closure Plot: Posits18.002% exact81.995% inexact0.000% underflow

0.000% overflow0.003% NaN

Only two cases produce a NaN:

±∞ × 00 × ±∞

Page 48: Beating Floating Point at its Own Game: Posit Arithmetic

The sorted losses tell the real story

Page 49: Beating Floating Point at its Own Game: Posit Arithmetic

ROUND 3

Higher-Precision Operations

32-bit formula evaluationLINPACK solved perfectly with… 16 bits!

Page 50: Beating Floating Point at its Own Game: Posit Arithmetic

Accuracy on a 32-Bit Budget27 /10− e

π − 2 + 3( )⎛

⎜⎜

⎟⎟

67 /16

= 302.8827196…Compute: with ≤ 32 bitsper number.

Number Type Dynamic Range

Answer Error

IEEE 32-bit float 83 decades 302.9124⋯ 0.02969⋯32-bit posits, es = 2 72 decades 302.8833⋯ 0.00058⋯32-bit posits, es = 3 144 decades 302.8823⋯ 0.00040⋯

Posits beat floats at both dynamic range and accuracy.

Page 51: Beating Floating Point at its Own Game: Posit Arithmetic

LINPACK: Solve Ax = b16-bit posits versus 16-bit floats

• A is a 100 by 100 dense matrix; random Aij entries in (0, 1) • b chosen so x should be all 1s exactly• Use classic LINPACK method: LU factorization with partial pivoting.

Allow refinement using residual.

IEEE 16-bit FloatsDynamic range: 12 decades

Maximum error: 0.011Decimal accuracy: 1.96

16-bit PositsDynamic range: 16 decades

Maximum error: NONEDecimal accuracy: ∞

Work funded in part by DARPA under contract BAA 16-39

Page 52: Beating Floating Point at its Own Game: Posit Arithmetic

LINPACK: 64-bit float versus 16-bit posits64-bit IEEE Floats

1.0000000000000124344978758017532527446746826171875

0.9999999999999837907438404727145098149776458740234375

1.0000000000000193178806284777238033711910247802734375

0.99999999999998501198916756038670428097248077392578125

0.9999999999999911182158029987476766109466552734375

0.99999999999999900079927783735911361873149871826171875

16-bit Posits

111111⋮

Page 53: Beating Floating Point at its Own Game: Posit Arithmetic

32-bit precision may suffice now!• Early computers used 36-bit floats.• IBM System 360 went to 32-bit.• It wasn’t quite enough.• What if 32-bit posits could replace

64-bit floats for big data workloads?• Potential 2x shortcut to exascale.

Or more.

Page 54: Beating Floating Point at its Own Game: Posit Arithmetic

Independent Confirmation by LLNL

Positsare50xmoreaccuratethanfloatsatEuler2D(incompressiblefluidflowcode).

Useofquirevariablesmayachieveanother100x!

Page 55: Beating Floating Point at its Own Game: Posit Arithmetic

Building posit chips: The race is on• Like IEEE floats, but simpler and less

area despite having higher accuracy• REX Computing shipping posit-based

multiprocessor to A*STAR any day now; 1.5x faster than float version!

• Posit Research Inc. forming to fill out hardware-software-application stack

• Looks ideal for GPUs and Deep Learning; more arithmetic per chip

• Consortium for Next-Generation Arithmetic is organizing now. Meeting SC’17, SA’18.

RegimeShifter

PositAdder

Page 56: Beating Floating Point at its Own Game: Posit Arithmetic

Collaborators & Interest - Commercial

56

SimuNova

PositResearch

Page 57: Beating Floating Point at its Own Game: Posit Arithmetic

Collaborators and Interest – RI and Labs

57

AISingapore(AI.SG)

Page 58: Beating Floating Point at its Own Game: Posit Arithmetic

�����

0

�����

1 /64�����

1 /16�����

1 /8 �����

1 /4 �����3 /8

�����1 /2

�����3 /4

�����1

�����3 /2

�����2

�����

3

�����

4

�����

8

�����

16

�����

64

�����

±∞

�����

-64�����

-16�����

-8�����

-4�����

-3�����

-2����� -3 /2

����� -1

����� -3 /4

�����-1 /2

�����-3 /8

�����

-1 /4

�����

-1 /8

�����

-1 /16

�����

-1 /64

• Better accuracy with fewer bits• Consistent, portable results• Automatic control of rounding errors• Clean, mathematical design• Reduces energy, power, bandwidth,

storage, and programming costs• Potentially halves costs for abundant

data challenges

Summary

Page 59: Beating Floating Point at its Own Game: Posit Arithmetic

For More Informationhttp://www.posithub.org

http://www.johngustafson.net/pdfs/BeatingFloatingPoint-superfriversion.pdf

https://www.youtube.com/watch?v=aP0Y1uAA-2Y

https://github.com/interplanetary-robot/SigmoidNumbers