Skip to content

Commit 805bdec

Browse files
committed
Merge branch 'pvc-dpas-port' into dev3
2 parents db1c275 + 6a53ced commit 805bdec

25 files changed

Lines changed: 1367 additions & 46 deletions

‎src/tensorforge/backend/instructions/compute/matmul.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ class MatmulOperands:
223223
#: that was applied to avoid computing it.
224224
A_slot: Optional[Callable] = None
225225

226+
#: ``A_block2d(writer, row, rows, shift=None) -> value``, or ``None``
227+
#: where `A` is not stored as a 2D surface (`Tensor.storage_surface`):
228+
#: rows `row` to `row + rows` of it, `shift` more at run time, one 2D
229+
#: block read of the sub-group, which hands lane `l` column `l` of them,
230+
#: the rows in order (`block_reads.load_block2d`).
231+
#:
232+
#: In place of `A_slot`, not beside it: a surface has its parts planar a
233+
#: group of slots at a time, so a slot is no address there, and the rows
234+
#: a block reads are all a reader names.
235+
A_block2d: Optional[Callable] = None
236+
226237
#: ``B_frag(writer, j, k, kblock, nblock) -> value``: the `B` fragment
227238
#: whose base is `(k, j)` and whose lane distribution is `kblock` columns
228239
#: of the contraction by `nblock` of the output.

‎src/tensorforge/backend/instructions/compute/multilinear.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,19 @@ def _offer_order(self, module, a_obj, lead, depth):
29832983
parts = 1
29842984
if slot_major is not None and not self._slot_major_fits(a_obj):
29852985
return
2986+
# A copy the file defines laid out as a 2D surface, where the target
2987+
# reads one so (`MatrixPaths.surface_order`): its table is aligned
2988+
# for a surface's base, which a buffer the caller passes is not known
2989+
# to be, and it is one buffer, where a merged run's members are read
2990+
# through a table of them. With the halves the order came with only:
2991+
# a tile's rows are its two parts.
2992+
surface = None
2993+
lay = getattr(module, 'surface_order', None)
2994+
if (copied and not variant and lay is not None
2995+
and parts == getattr(order, 'parts', None)):
2996+
surfaced = lay(order, self._context)
2997+
if surfaced is not None:
2998+
order, surface = surfaced, surfaced.surface
29862999
for member in members:
29873000
if (member.addressing is not Addressing.NONE
29883001
or not member.is_dense()
@@ -3016,6 +3029,8 @@ def _offer_order(self, module, a_obj, lead, depth):
30163029
target.storage_planar = True
30173030
if slot_major is not None:
30183031
target.slot_major = slot_major
3032+
if surface is not None:
3033+
target.storage_surface = surface
30193034
if parts is not None and parts != 1:
30203035
target.storage_parts = parts
30213036

@@ -3564,6 +3579,37 @@ def A_slot(writer, slot, parts=1, shift=None, width=1):
35643579
return res
35653580

35663581

3582+
def A_block2d(writer, row, rows, shift=None):
3583+
"""Rows `row` to `row + rows` of `A` laid out as a 2D surface
3584+
(`Tensor.storage_surface`): one 2D block read of the
3585+
sub-group, lane `l` column `l` of them, the rows in order
3586+
(`block_reads.load_block2d`). `shift` is a row count added
3587+
at run time, as `A_slot` takes one.
3588+
3589+
The surface is the copy's table, whole: only a copy the file
3590+
defines is laid out so (`_offer_order`), its table aligned to
3591+
`Generator.COPY_ALIGN` and starting at the stored order's
3592+
first slot, where `A_slot` counts from too. So its base is
3593+
the binding, its width the surface's columns, its rows every
3594+
slot it stores."""
3595+
from tensorforge.backend.pir.block_reads import load_block2d
3596+
from tensorforge.generators.generator import Generator
3597+
sym = self._ops[0].symbol
3598+
if sym.stype is not SymbolType.Global:
3599+
raise InternalError(
3600+
f'{a_obj.alias}: a 2D surface read where it is not '
3601+
f'in global memory ({sym.stype})')
3602+
columns, _ = a_obj.storage_surface
3603+
width = columns * sym.get_fptype().size()
3604+
height = int(a_obj.storage_volume()) // columns
3605+
if shift is not None:
3606+
row = writer.op('add', INDEX, shift, row, hint='slot')
3607+
return load_block2d(writer, sym, 0, row, rows=rows,
3608+
cols=columns, width=width, height=height,
3609+
pitch=width, align=Generator.COPY_ALIGN,
3610+
hint='blk')
3611+
3612+
35673613
def B_direct(kblock, nblock):
35683614
"""Whether `B_frag` can address every fragment of this shape.
35693615
@@ -3773,7 +3819,13 @@ def A_lanes(writer, k, rows, steps):
37733819
# would make every read four steps wide over a
37743820
# buffer that has no such run.
37753821
and getattr(a_obj, 'packing', None) is None
3822+
# Nor a surface, whose parts are planar a group at
3823+
# a time: read a block of rows (`A_block2d`).
3824+
and getattr(a_obj, 'storage_surface', None) is None
37763825
else None),
3826+
A_block2d=(A_block2d if a_obj is not None
3827+
and getattr(a_obj, 'storage_surface', None)
3828+
is not None else None),
37773829
A_zero=A_zero if width == 1 else None,
37783830
B_shard=B_shard if b_shared and width == 1 else None,
37793831
lead_slots=M, lead_elements=Mx, n=N, k=K, kx=kx,

‎src/tensorforge/backend/instructions/compute/primitives/intel.py‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from tensorforge.backend.pir.core import SCALAR_LAYOUT, ScalarType, join_layout
4949
from tensorforge.common.basic_types import Datatype
5050
from tensorforge.common.exceptions import InternalError
51-
from .. import broadcast, ranking, split
51+
from .. import broadcast, ranking, rowpack, split
5252
from . import intel_spmd
5353
from ..bitlayout import packed
5454
from ..routes import lead_route
@@ -188,6 +188,25 @@ def enabled(ctx) -> bool:
188188
return ENABLED if asked is None else bool(asked)
189189

190190

191+
def moves_refused(ctx) -> bool:
192+
"""Whether this build moves its transfers ahead
193+
(`Options.enable_move_loads`) under the explicit vector, where DPAS is
194+
then not taken (`strategies`, `prepared_order`).
195+
196+
With the transfers moved, SeisSol's elastic volume at order 6 in single
197+
precision came out wrong on the CPU emulator (tfemu) against NumPy: nine
198+
of its 576 outputs, the ninth quantity's rows 10 to 15, up to 18 % off.
199+
Right with the moves off; right with only the wrap across the back edge
200+
(`Options.enable_wrap_loads`) left on; and each of its three products
201+
alone, and any two of them, right with the moves on. Until that
202+
interaction is found, DPAS under the explicit vector needs
203+
`enable_move_loads=0`. Nothing changes where the path is off, which is
204+
the default."""
205+
if ctx is None or not _simd_mode(ctx):
206+
return False
207+
return bool(ctx.get_user_options().enable_move_loads)
208+
209+
191210
# --------------------------------------------------------------------------- #
192211
# Where an element sits in a fragment
193212
# --------------------------------------------------------------------------- #
@@ -786,16 +805,26 @@ def prepared_order(shape, dtype, ctx, columns=0, lead=0, depth=0,
786805
rows, cols = (int(x) for x in shape)
787806
# The question `strategies` answers for MATRIX, which `ORDER` takes first
788807
# wherever it is offered.
789-
dpas = enabled(ctx) and atom_for(dtype, columns=columns, lead=lead,
790-
depth=depth,
791-
budget=register_budget(ctx)) is not None
808+
dpas = (enabled(ctx) and not moves_refused(ctx)
809+
and atom_for(dtype, columns=columns, lead=lead, depth=depth,
810+
budget=register_budget(ctx)) is not None)
792811
block = ATOMS['tf32'].k if dpas else 1
793812
padded = -(-cols // block) * block
794813
return SlotMajorOrder(slot_major_order((rows, cols), threads, padded),
795814
threads, padded,
796815
parts=TF32_SPLIT_TERMS if dpas else 1)
797816

798817

818+
def surface_order(order, ctx):
819+
"""An operator copy's tiles as a 2D surface (`MatrixPaths.surface_order`):
820+
the SPMD lowering's DPAS reads one so where asked
821+
(`intel_spmd.surface_order`, `Options.sycl_dpas_block2d`); the explicit
822+
vector reads none."""
823+
if ctx is None or _simd_mode(ctx):
824+
return None
825+
return intel_spmd.surface_order(order, ctx)
826+
827+
799828
def widths(threads, accumulator, ctx):
800829
"""None offered (`MatrixPaths.widths`): `Options.matrix_columns` is read
801830
by CDNA's lane-batched tiles alone, and the atom here is the count's
@@ -832,7 +861,8 @@ def strategies(shape, ctx):
832861
833862
`dpas_matmul`, too, only under the explicitly vectorized lowering: it
834863
emits `esimd::xmx::dpas` over `simd` fragments, which an SPMD kernel
835-
cannot call. The SPMD lowering reaches the instruction another way, the
864+
cannot call -- and not while the build moves its transfers ahead
865+
(`moves_refused`). The SPMD lowering reaches the instruction another way, the
836866
OpenCL builtin over a sub-group of sixteen, whose fragments are spread
837867
over the work-items as the lowering spreads its rows (`intel_spmd`,
838868
`Options.sycl_dpas`); that path answers for SPMD alone, and only where
@@ -853,13 +883,27 @@ def strategies(shape, ctx):
853883
if not supports(shape.threads, shape.accumulator, shape.sparse):
854884
return frozenset()
855885
offered = set()
856-
if enabled(ctx) and shape.explicit_simd and not shape.sparse:
886+
if (enabled(ctx) and shape.explicit_simd and not shape.sparse
887+
and not moves_refused(ctx)):
857888
offered.add(Strategy.MATRIX)
858889
if BROADCAST_ENABLED and shape.explicit_simd and not shape.sparse:
859890
offered.add(Strategy.BROADCAST)
860891
return frozenset(offered)
861892

862893

894+
def row_caps(shape, n, ctx):
895+
"""What the matrix path does with a product's `n` columns past its
896+
last whole tile (`MatrixPaths.row_caps`, `Options.row_packing`): the
897+
SPMD lowering's DPAS carries or pads them (`intel_spmd.row_caps`);
898+
the explicit vector's pads them, its second `j0` block a tile of
899+
eight rows of which the remainder fills one (`dpas_matmul`)."""
900+
if not _simd_mode(ctx):
901+
return intel_spmd.row_caps(n)
902+
height = ATOMS['tf32'].repeat
903+
return rowpack.RowCaps(height, frozenset({rowpack.Packing.PAD}),
904+
rowpack.Packing.PAD)
905+
906+
863907
def plan(strategy, shape, n, ctx):
864908
"""One arrangement over the whole output.
865909
@@ -907,6 +951,9 @@ def matmul(writer, ops, ctx, span):
907951
# caller that does should hear so rather than get the whole output.
908952
return False
909953
if span.strategy is Strategy.MATRIX:
954+
# Only the padded remainder here (`row_caps`): anything else asked
955+
# stops generation with what this path has.
956+
rowpack.plan_rows(N, row_caps(None, N, ctx), ctx)
910957
if Datatype.F32 not in (ops.a, ops.b) or ops.a != ops.b:
911958
# `splitFloatTF32` takes an F32 apart; handed anything else it
912959
# would produce two halves of a number it never had. The atom is

0 commit comments

Comments
 (0)