@@ -117,6 +117,15 @@ class TermPlan:
117117 a product; `epilogue` how the partials become D; `accumulator` the
118118 format they hold. `name` says which plan it is where a body is read.
119119
120+ `restart`, one entry a partial where any is restarted: the blocks of K
121+ -- an instruction's K a block, all of a chain's members over it -- a
122+ partial lives before it is drained into a total of its own, F32 rounded
123+ to nearest, and starts again from nothing; 0 for one that lives over the
124+ whole contraction, which is every partial where `restart` is empty. At
125+ the end a restarted partial is its total plus what it holds. The
126+ drain is no product, and no part of a body `check_plans` reads: it is
127+ the path's (`reference.accumulation.evaluate` states it).
128+
120129 Frozen and hashable: a `MATRIX_MAC` carries it as an attribute
121130 (`IRBuilder.matrix_mac`'s `plan`), and the attributes of a statement are
122131 part of its identity to the passes."""
@@ -128,8 +137,24 @@ class TermPlan:
128137 partials : Tuple [int , ...]
129138 epilogue : Epilogue = Epilogue .NONE
130139 accumulator : Datatype = Datatype .F32
140+ restart : Tuple [int , ...] = ()
131141
132142 def __post_init__ (self ):
143+ self ._check ()
144+ if self .restart :
145+ if len (self .restart ) != self .count :
146+ raise ValueError (f'{ self .name } : a restart for '
147+ f'{ len (self .restart )} partials of '
148+ f'{ self .count } ' )
149+ if any (not isinstance (r , int ) or isinstance (r , bool ) or r < 0
150+ for r in self .restart ):
151+ raise ValueError (f'{ self .name } : restart { self .restart } : '
152+ f'blocks of K, 0 for none' )
153+ if not any (self .restart ):
154+ # No partial restarted is no restart: one plan, one value.
155+ object .__setattr__ (self , 'restart' , ())
156+
157+ def _check (self ):
133158 if not self .products :
134159 raise ValueError (f'{ self .name } : a plan of no products' )
135160 if len (set (self .products )) != len (self .products ):
@@ -167,6 +192,18 @@ def sequence(self, partial: int) -> Tuple[Tuple[int, int], ...]:
167192 return tuple (t for t , p in zip (self .products , self .partials )
168193 if p == partial )
169194
195+ def restarted (self , partial : int ) -> int :
196+ """The blocks of K partial `partial` lives between two drains
197+ (`restart`); 0 for one over the whole contraction."""
198+ return self .restart [partial ] if self .restart else 0
199+
200+ @property
201+ def period (self ) -> int :
202+ """The longest a restarted partial lives, in blocks of K, 0 where
203+ none is restarted: what a plan's class is read by besides its name
204+ (`product_accuracy`)."""
205+ return max (self .restart , default = 0 )
206+
170207 def steps (self ) -> Iterator [Tuple [int , Tuple [int , int ], int ]]:
171208 """`(index, term, partial)` of every product, in the plan's order:
172209 for a path that interleaves the products of several accumulators,
@@ -179,12 +216,14 @@ def each(self) -> 'TermPlan':
179216 """The same products, each into an accumulator of its own and
180217 summed behind them (`Epilogue.SUM`): what NVIDIA's `mma.sync`
181218 issues where a step has too few independent chains to hide an
182- HMMA's latency (`nvidia.CHAINS_MIN`)."""
219+ HMMA's latency (`nvidia.CHAINS_MIN`). A product of a restarted
220+ partial keeps its partial's restart in its own."""
183221 if self .count == len (self .products ):
184222 return self
185223 return TermPlan (f'{ self .name } /each' , self .a , self .b , self .products ,
186224 tuple (range (len (self .products ))), Epilogue .SUM ,
187- self .accumulator )
225+ self .accumulator ,
226+ tuple (self .restarted (p ) for p in self .partials ))
188227
189228 def issue (self , mac : Callable , a_terms : Sequence , b_terms : Sequence ,
190229 accs : Sequence ) -> List :
@@ -225,6 +264,8 @@ def __repr__(self):
225264 else :
226265 where = (f'partials { "" .join (str (p ) for p in self .partials )} , '
227266 f'{ self .epilogue .value } ' )
267+ if self .restart :
268+ where += '; restart ' + ',' .join (str (r ) for r in self .restart )
228269 return (f'TermPlan({ self .name } : { self .a !r} by { self .b !r} into '
229270 f'{ self .accumulator .name } : { terms } ; { where } )' )
230271
@@ -244,6 +285,53 @@ def plan(name: str, a: Form, b: Form, *, keep: Optional[int] = None,
244285 accumulator )
245286
246287
288+ #: An F32 operand as the 6xBF16 plans take it: three BF16 terms, each
289+ #: rounded to nearest even and the remainder carried, whose sum is the value
290+ #: (`split.terms`; `common.matrix.forms.BF16_TRIPLE` makes them).
291+ BF16X3 = Form (Datatype .BF16 , 3 )
292+
293+
294+ def bf16x6 (order : str = 'smallest' , major : str = 'a' , accumulators : int = 1 ,
295+ restart : int = 0 ) -> TermPlan :
296+ """The 6xBF16 family: A and B in three BF16 terms each
297+ (`BF16X3`), the six products above F32's rounding (`split.products`),
298+ in `order`, a tie broken by `major`'s term.
299+
300+ One accumulator (`bf16x6`): the six into D. Two (`bf16x6/2`): the
301+ leading product, (0,0), into partial 0 and issued last -- so that where
302+ that partial is drained, the drain runs under the next block's other
303+ products -- and the other five into partial 1, the two added behind
304+ them (`Epilogue.SUM`). `restart`: the blocks of K partial 0 lives
305+ between two drains (`TermPlan.restart`), 0 for all of K. NVIDIA's
306+ engines truncate every sum they make, and it is the restart that puts
307+ the plan below FFMA's error there; DPAS rounds each step to nearest,
308+ and the second accumulator alone does it (`ACCURACY`).
309+
310+ NVIDIA issues the products largest first (`order='largest'`), Intel
311+ smallest first with B's term breaking a tie (`major='b'`, the plan named
312+ `/b` as Intel's `3xtf32/b`), AMD's stacked scheme smallest first
313+ (`stacked.BF16X6`, this with the defaults)."""
314+ if accumulators not in (1 , 2 ):
315+ raise ValueError (f'a 6xBF16 plan of one or two accumulators, not '
316+ f'{ accumulators !r} ' )
317+ if not isinstance (restart , int ) or isinstance (restart , bool ) \
318+ or restart < 0 :
319+ raise ValueError (f'a restart of { restart !r} : blocks of K, 0 for none' )
320+ name = ('bf16x6' + ('/2' if accumulators == 2 else '' )
321+ + ('/b' if major == 'b' else '' ))
322+ pairs = plan (name , BF16X3 , BF16X3 , order = order , major = major ).products
323+ if accumulators == 1 :
324+ return TermPlan (name , BF16X3 , BF16X3 , pairs , (0 ,) * len (pairs ),
325+ Epilogue .NONE , Datatype .F32 ,
326+ (restart ,) if restart else ())
327+ lead = (0 , 0 )
328+ products = tuple (t for t in pairs if t != lead ) + (lead ,)
329+ return TermPlan (name , BF16X3 , BF16X3 , products ,
330+ tuple (0 if t == lead else 1 for t in products ),
331+ Epilogue .SUM , Datatype .F32 ,
332+ (restart , 0 ) if restart else ())
333+
334+
247335# --------------------------------------------------------------------------- #
248336# Reading the plans back off a body
249337# --------------------------------------------------------------------------- #
@@ -365,11 +453,33 @@ def check_plans(body: Sequence[Stmt]) -> List[str]:
365453#: Intel's DPAS adds in two interleaved chains, each step rounded to nearest:
366454#: 3xTF32 0.87x to 1.44x FFMA's error, 3.45e-7 and 5.13e-7 measured at o6
367455#: and o8. AMD's stacked BF16X6, 1e-7 measured on SeisSol's kernels.
368- ACCURACY : Dict [Tuple [str , str ], str ] = {
456+ #:
457+ #: The 6xBF16 family (`bf16x6`) -- a plan with a restarted partial by the
458+ #: blocks of K it lives too (`TermPlan.period`, the key's third member) --
459+ #: by the models of those engines (`reference.accumulation`'s tz2 and pvc)
460+ #: on SeisSol's volume, o6 to o16, against FFMA's error at the order: on
461+ #: NVIDIA one accumulator 4.4x to 7.2x; two, (0,0)'s restarted every block
462+ #: 0.25x to 0.46x, every two 0.34x to 0.74x, every three 0.46x to 1.0x,
463+ #: never 1.0x (o6) to 3.1x (o16). On Intel one accumulator 0.88x to 1.53x
464+ #: (o4 on), two 0.57x to 0.60x, restarted every block 0.23x to 0.42x, every
465+ #: two 0.20x to 0.52x. On the hardware: two accumulators restarted every
466+ #: block 1.14e-7 at o8 on GH200, as modeled; on PVC 0.97e-7 and 1.03e-7 at
467+ #: o6 and o8, and without the restart 1.4e-7 against 3xTF32's 6.4e-7 on a
468+ #: dense 56 x 56 chain of eight.
469+ ACCURACY : Dict [tuple , str ] = {
369470 ('3xtf32' , 'nvidia' ): 'relaxed' ,
370471 ('2xtf32' , 'nvidia' ): 'relaxed' ,
371472 ('3xtf32/b' , 'intel' ): 'ffma' ,
372473 ('bf16x6' , 'amd' ): 'ffma' ,
474+ ('bf16x6' , 'nvidia' ): 'relaxed' ,
475+ ('bf16x6/2' , 'nvidia' ): 'relaxed' ,
476+ ('bf16x6/2' , 'nvidia' , 1 ): 'better' ,
477+ ('bf16x6/2' , 'nvidia' , 2 ): 'better' ,
478+ ('bf16x6/2' , 'nvidia' , 3 ): 'ffma' ,
479+ ('bf16x6/b' , 'intel' ): 'ffma' ,
480+ ('bf16x6/2/b' , 'intel' ): 'better' ,
481+ ('bf16x6/2/b' , 'intel' , 1 ): 'better' ,
482+ ('bf16x6/2/b' , 'intel' , 2 ): 'better' ,
373483}
374484
375485
@@ -378,14 +488,27 @@ def product_accuracy(plan: Optional[TermPlan], vendor: Optional[str]) -> str:
378488 (`ACCURACY`): FFMA's for a product in full -- DFMA, DMMA, an MFMA on
379489 whole operands -- and a plan's own, one into an accumulator a product
380490 (`/each`) as its whole; `relaxed` for a plan the table does not name,
381- whose rounding nobody measured."""
491+ whose rounding nobody measured. A plan with a restarted partial by its
492+ period too (`TermPlan.period`): one the table does not name, `relaxed`
493+ as well."""
382494 if plan is None :
383495 return 'ffma'
496+ if plan .period :
497+ return _restarted_accuracy (plan , vendor )
384498 name = plan .name [:- len ('/each' )] if plan .name .endswith ('/each' ) \
385499 else plan .name
386500 return ACCURACY .get ((name , vendor ), 'relaxed' )
387501
388502
503+ def _restarted_accuracy (plan : TermPlan , vendor : Optional [str ]) -> str :
504+ """`product_accuracy` of a plan with a restarted partial: by its name
505+ (one an accumulator a product as its whole), the vendor and its
506+ period."""
507+ name = plan .name [:- len ('/each' )] if plan .name .endswith ('/each' ) \
508+ else plan .name
509+ return ACCURACY .get ((name , vendor , plan .period ), 'relaxed' )
510+
511+
389512def admits (asked : str , level : str ) -> bool :
390513 """Whether the class `asked` for (`Options.accuracy`) admits `level`:
391514 `level` as accurate or more."""
@@ -411,6 +534,6 @@ def body_accuracy(bodies: Sequence[Sequence[Stmt]], vendor: Optional[str]
411534 return worst , by
412535
413536
414- __all__ = ['ACCURACY' , 'ACCURACY_CLASSES' , 'Epilogue ' , 'Form ' , 'Kind ' ,
415- 'TermPlan ' , 'admits ' , 'body_accuracy ' , 'check_plans ' , 'plan ' ,
416- 'product_accuracy' ]
537+ __all__ = ['ACCURACY' , 'ACCURACY_CLASSES' , 'BF16X3 ' , 'Epilogue ' , 'Form ' ,
538+ 'Kind ' , 'TermPlan ' , 'admits ' , 'bf16x6 ' , 'body_accuracy ' ,
539+ 'check_plans' , 'plan' , ' product_accuracy' ]
0 commit comments