16 Commits

Author SHA1 Message Date
NiccoloN 51fdb830e5 Cose belle
Validate Operations / validate-operations (push) Waiting to run
2026-07-14 16:57:58 +02:00
ilgeco d1a29ace3c Now something work but not trust us (phase 1 + phase 2 of merge)
Validate Operations / validate-operations (push) Has been cancelled
2026-07-13 16:21:54 +02:00
ilgeco 61e3ea9996 Unexpected invariant now it's clear (batched in the first tensor rank)
Validate Operations / validate-operations (push) Has been cancelled
2026-07-13 12:05:59 +02:00
NiccoloN fed6d343e5 remove accidental copy-paste
Validate Operations / validate-operations (push) Has been cancelled
2026-07-09 10:56:19 +02:00
NiccoloN 871fcfa832 a new new beginning phase 1
Validate Operations / validate-operations (push) Has been cancelled
2026-07-08 22:53:53 +02:00
ilgeco 1f4f58de1c A new Beginning
Validate Operations / validate-operations (push) Has been cancelled
2026-07-07 18:28:37 +02:00
NiccoloN 8338caf3f3 cose brutte
Validate Operations / validate-operations (push) Has been cancelled
2026-07-07 12:54:34 +02:00
ilgeco 47f6715296 CommunicationPlan
Validate Operations / validate-operations (push) Has been cancelled
2026-07-06 17:25:31 +02:00
ilgeco 2bfc033af9 Fix conv_relu_conv diamond shape 2026-07-06 11:22:39 +02:00
NiccoloN 83a54e28e4 meno diamantini
Validate Operations / validate-operations (push) Has been cancelled
2026-07-06 10:12:20 +02:00
ilgeco cc9b025a35 Relu conv store
Validate Operations / validate-operations (push) Has been cancelled
2026-07-02 17:54:33 +02:00
ilgeco c4dd28a607 Export csv graph for gephi
Validate Operations / validate-operations (push) Has been cancelled
2026-07-02 17:01:26 +02:00
ilgeco 8d3eb929f6 Vgg 16 works and also resnet 2026-07-01 13:49:21 +02:00
ilgeco f5e1c2e706 Fix vgg16_depth05 bug 2026-06-30 14:54:33 +02:00
ilgeco 94c96195b9 Merge done
Validate Operations / validate-operations (push) Has been cancelled
2026-06-29 15:46:12 +02:00
ilgeco 645539317b Fix BB Arg used as input in external Op 2026-06-29 15:21:28 +02:00
92 changed files with 12120 additions and 10251 deletions
+1
View File
@@ -1,4 +1,5 @@
* Always read the full README.md before doing anything * Always read the full README.md before doing anything
* Always read the full invariants/GRAPH_COMPUTE_BATCH_INVARIANT.md before modifying Spatial graph IR, Blueprint handling, or MergeComputeNodes.
* Build commands: * Build commands:
* `cmake --build ./build_release` * `cmake --build ./build_release`
* `cmake --build ./build_debug` * `cmake --build ./build_debug`
+5 -1
View File
@@ -105,6 +105,9 @@ Pass these to `onnx-mlir` when compiling for PIM:
the codegen tail. the codegen tail.
- `--pim-emit-json` - also emit `core_*.json` instruction files alongside - `--pim-emit-json` - also emit `core_*.json` instruction files alongside
`core_*.pim`. `core_*.pim`.
- `--pim-export-spatial-dataflow=<none|spatial1|spatial2|spatial3|all>` - control Spatial
dataflow CSV reports. The default `all` emits graph, scheduled, and realized
snapshots under `reports/`.
- `--use-experimental-conv-impl` - use the alternate convolution lowering. - `--use-experimental-conv-impl` - use the alternate convolution lowering.
- `--ignore-concat-error` - soft-fail a ConcatOp corner case. - `--ignore-concat-error` - soft-fail a ConcatOp corner case.
@@ -167,7 +170,8 @@ Each validation run writes artifacts in the model workspace, for example under
- `simulation/out.bin` - raw simulator output used for comparison. - `simulation/out.bin` - raw simulator output used for comparison.
The compiler currently dumps dialect snapshots such as `spatial0.mlir`, The compiler currently dumps dialect snapshots such as `spatial0.mlir`,
`spatial1_dcp_merged.mlir`, `pim0.mlir`, `pim1_buff.mlir`, `spatial1_graph.mlir`, `spatial2_scheduled_no_comm.mlir`,
`spatial3_scheduled.mlir`, `pim0.mlir`, `pim1_buff.mlir`,
`pim2_coalesced.mlir`, and `pim3_folded.mlir` when an output directory is `pim2_coalesced.mlir`, and `pim3_folded.mlir` when an output directory is
available. available.
+362
View File
@@ -0,0 +1,362 @@
# Graph Compute Batch Physical-Fragment Invariant
## Status
This document is **normative** for Raptor's Spatial graph IR.
Every developer or coding agent modifying Spatial graph construction, graph
verification, Blueprint handling, or `MergeComputeNodes` must read this file
after `README.md` and `AGENTS.md`.
`AGENTS.md` must contain this instruction:
```text
* Always read the full invariants/GRAPH_COMPUTE_BATCH_INVARIANT.md before modifying Spatial graph IR, Blueprint handling, or MergeComputeNodes.
```
## Scope
This invariant applies to:
- `spat.graph_compute_batch`;
- graph-level values produced by it;
- `tensor.parallel_insert_slice` operations that publish its lane results;
- `spat.blueprint` operations that describe logical reconstruction;
- graph analyses and transformations that consume those values;
- the graph-to-scheduled transition in `MergeComputeNodes`.
It does **not** impose the same representation on:
- `spat.scheduled_compute`;
- `spat.scheduled_compute_batch`;
- `pim.core` or `pim.core_batch`;
- values whose cross-core movement is already represented by explicit
`spat.channel_send` and `spat.channel_receive` operations.
Scheduled IR represents execution on assigned cores. Communication and value
availability there are defined by local SSA forwarding and explicit
send/receive operations, not by the graph physical-fragment invariant.
## Core invariant
For every result of a `spat.graph_compute_batch` with `N` graph lanes:
1. Every graph lane produces exactly one fragment for that result.
2. All lanes produce fragments with the same exact ranked tensor type `F`.
3. The graph result is a physical collection of those fragments with type:
```text
tensor<N x shape(F) x element-type(F)>
```
Conceptually, the result is `N × F`: one leading physical fragment-slot
dimension followed by the complete per-lane fragment shape.
4. Physical slot `i` identifies a fragment publication. It does not, by itself,
identify a row, column, channel, tile, or any other logical tensor position.
5. The result type carries no logical reconstruction order.
The leading dimension is therefore a **physical fragment-slot dimension**, not
a logical tensor dimension.
## Per-lane computation is unrestricted
The invariant constrains the published result representation, not what a lane
may compute.
A graph lane may:
- read several input slices;
- perform reductions;
- add or combine multiple columns;
- execute matrix/vector operations;
- produce a fragment that corresponds to any logical region;
- participate in a multi-stage or logarithmic reduction tree implemented by
following `spat.graph_compute` or `spat.graph_compute_batch` operations.
Arithmetic combination is graph computation. `spat.blueprint` is not an
arithmetic reduction operation.
### Example: `16×4 -> 16×2`
Two graph lanes may compute:
```text
lane 0: input[:, 0] + input[:, 1] -> tensor<16x1>
lane 1: input[:, 2] + input[:, 3] -> tensor<16x1>
```
The physical graph result is:
```text
tensor<2x16x1>
```
A Blueprint then maps:
```text
physical slot 0 -> logical output[:, 0:1]
physical slot 1 -> logical output[:, 1:2]
```
and describes the logical result `tensor<16x2>`.
For a larger reduction, following graph compute batches may reduce fragments in
`ceil(log2(N))` stages. Every intermediate batch still publishes a physical
`batch × fragment` collection.
## Physical publication inside `spat.graph_compute_batch`
The batch body must publish each lane's fragment into the physical result.
For one result with fragment type `F`, the corresponding
`tensor.parallel_insert_slice` must insert the fragment into one slot of the
physical `N × F` destination:
```text
physical offsets = [slot, 0, 0, ...]
physical sizes = [1, shape(F)...]
physical strides = [1, 1, 1, ...]
```
The slot may be the graph lane directly or a statically analyzable permutation
of it. The insertion describes physical slot placement only. It must not use a
logical output dimension as the physical batch dimension.
For each graph result, the body must contain exactly one physical publication
per graph lane. Since the body executes once per lane, this normally means one
`tensor.parallel_insert_slice` operation targeting that result.
## Logical reconstruction
Logical reconstruction is separate from physical publication.
The reconstruction descriptor defines, for every physical fragment slot:
- which physical batch operand owns the fragment;
- which physical slot contains it;
- its destination offsets in the logical tensor;
- its destination sizes;
- its destination strides;
- coverage and conflict policy where relevant.
The persistent owner of this information is `spat.blueprint` or an equivalent
explicit graph-level reconstruction operation.
A logical consumer must not infer reconstruction from the physical tensor type
or assume that physical slot order equals logical order.
The logical mapping may be arbitrary. For example:
```text
physical slot 0 -> logical row 13
physical slot 1 -> logical row 4
physical slot 2 -> logical row 10
```
The physical result remains a regular `batch × fragment` tensor.
## Relationship between `parallel_insert_slice` and Blueprint
During graph construction, an algorithm may naturally describe logical
placement with `tensor.parallel_insert_slice` geometry. Before the graph is in
its canonical form:
1. that geometry must be separated from physical fragment publication;
2. the graph batch result must be normalized to `N × F`;
3. the logical insertion geometry must be transferred to a persistent
`spat.blueprint` reconstruction descriptor.
After normalization:
- `parallel_insert_slice` inside `spat.graph_compute_batch` publishes into
physical fragment slots;
- `spat.blueprint` describes reconstruction into the logical tensor.
The original graph operation may be erased only after all reconstruction
information needed by later stages has a persistent owner.
## Blueprint semantics
Blueprint is placement/reconstruction metadata. It may:
- concatenate fragments;
- reorder fragments;
- insert fragments into arbitrary disjoint logical regions;
- describe complete or partial logical coverage;
- expose a logical tensor view when materialization is required.
Blueprint must not silently perform arithmetic such as addition, multiplication,
maximum, or reduction. Such transformations must be represented by following
`spat.graph_compute` or `spat.graph_compute_batch` operations.
A Blueprint consuming a physical fragment batch must explicitly identify the
physical source slot for every logical fragment. It must not derive that slot
from operand order unless that convention is explicitly represented and
verified.
## Multiple results
A `spat.graph_compute_batch` may have several results.
For each result `r` independently:
- every lane produces one fragment of type `F_r`;
- the graph result type is `N × F_r`;
- its physical publication and logical reconstruction descriptor are verified
independently.
Different results may use different fragment shapes.
## Graph consumers
A graph consumer of a batch result may:
1. consume fragments directly as physical fragments;
2. select one or more physical slots in a `spat.deferred_communication` body;
3. use a Blueprint to obtain or describe a logical reconstruction;
4. feed fragments to following graph computes or graph compute batches.
A consumer must not treat the leading physical slot dimension as a logical
model dimension unless an explicit graph operation intentionally performs such
an interpretation.
All constant selection, slicing, reshaping, concatenation, and other
compile-time shaping needed for a scheduled consumer must be encoded inside the
corresponding `spat.deferred_communication` body. Phase 2 must not recover
missing graph semantics by inspecting consumers after the deferred operation.
## Graph lane, scheduled lane, and physical core are different identities
These concepts must never be conflated:
- **graph lane**: the lane of the original `spat.graph_compute_batch`;
- **physical fragment slot**: the slot in the graph batch result;
- **scheduled lane**: one lane of a `spat.scheduled_compute_batch` equivalence
class;
- **physical core**: the core selected by PEFT.
The graph batch body or its Blueprint defines graph-lane-to-fragment-slot and
fragment-slot-to-logical-region mappings.
PEFT defines graph-instance-to-core placement.
Scheduled communication defines how values move between cores.
## Scheduled IR exclusion
Do not add a verifier requiring `spat.scheduled_compute_batch` results to have
`laneCount` as their first dimension.
Do not rewrite scheduled values merely to resemble graph physical fragment
collections.
When lowering graph IR into scheduled IR:
- resolve graph fragments and reconstruction metadata before erasing their
graph owners;
- create local forwarding or `spat.channel_send`/`spat.channel_receive` for
cross-core dependencies;
- allow scheduled result representation to follow the scheduled IR contract;
- preserve numerical and deadlock correctness.
The graph invariant is an input contract for scheduling, not a scheduled-value
layout contract.
## Required verifier properties
`spat.graph_compute_batch` verification must establish, for every result:
1. the result is a static or otherwise supported ranked tensor;
2. result rank is exactly `fragment rank + 1`;
3. result dimension 0 equals `laneCount`;
4. every lane publication source has the same exact fragment type;
5. the physical insertion targets the corresponding result block argument;
6. physical insertion offsets have the fragment slot in dimension 0;
7. all remaining physical offsets are zero;
8. physical sizes are `[1] + fragment shape`;
9. physical strides are unit;
10. exactly one publication is defined for each graph result in the per-lane
body.
These checks apply only to `spat.graph_compute_batch`, not to
`spat.scheduled_compute_batch`.
Blueprint verification must establish that every logical reconstruction entry:
- references an existing physical batch operand;
- references a valid physical fragment slot;
- maps a fragment compatible with the declared logical slice;
- stays within logical bounds;
- follows the declared conflict and coverage policies.
## Invalid representations
The following are invariant violations.
### Logical aggregate returned directly by graph batch
```text
laneCount = 16
result = tensor<1x4x16x16>
```
with each lane inserting into logical dimension 2.
This is a logical assembly masquerading as a graph batch result. The graph
result must instead be `16 × per-row-fragment`, and a Blueprint must describe
placement into `tensor<1x4x16x16>`.
### Physical storage derived from logical destination shape
Code equivalent to:
```cpp
shape = logicalDestinationType.getShape();
shape[logicalInsertionDimension] = laneCount;
```
is invalid.
Physical graph storage must be derived from the per-lane fragment type:
```cpp
physicalShape = [laneCount] + fragmentType.getShape();
```
### Reconstruction inferred from result type
It is invalid to assume that physical slot `i` belongs at logical offset `i`.
The Blueprint or another explicit reconstruction descriptor must state the
mapping.
### Blueprint used for arithmetic
It is invalid to encode `fragment0 + fragment1` as Blueprint reconstruction.
Create a following graph compute or graph compute batch for the addition.
## Ownership
- ONNX-to-Spatial lowering owns creation of valid graph fragment batches.
- Graph canonicalization owns normalization of temporary logical-assembly forms
into physical graph batches plus Blueprints.
- `spat.graph_compute_batch` verifier rejects invalid physical publications.
- `spat.blueprint` owns persistent logical reconstruction metadata.
- Deferred communication Phase 1 owns complete consumer-side constant shaping.
- Merge scheduling consumes this graph contract and introduces explicit
communication.
- Scheduled IR verifiers validate scheduled execution and communication, not
the graph fragment representation.
## No repair downstream
If graph IR violates this invariant, fix the graph producer or graph
canonicalization.
Do not repair an invalid graph batch by:
- guessing a lane dimension in `MergeComputeNodes`;
- deriving physical storage from a logical destination tensor;
- inspecting deferred-result users;
- reconstructing omitted Blueprint data after graph erasure;
- weakening graph verifiers;
- imposing the graph representation on scheduled operations.
-1
View File
@@ -117,7 +117,6 @@ add_pim_library(OMPIMAccel
SpatialOps SpatialOps
PimOps PimOps
OMONNXToSpatial OMONNXToSpatial
OMSpatialToGraphviz
OMSpatialToPim OMSpatialToPim
OMPimCommon OMPimCommon
OMPimBufferization OMPimBufferization
+1
View File
@@ -8,6 +8,7 @@ add_pim_library(OMPimCommon
IR/IndexingUtils.cpp IR/IndexingUtils.cpp
IR/LoopUtils.cpp IR/LoopUtils.cpp
IR/ShapeUtils.cpp IR/ShapeUtils.cpp
IR/ShapingUtils.cpp
IR/SubviewUtils.cpp IR/SubviewUtils.cpp
IR/TensorSliceUtils.cpp IR/TensorSliceUtils.cpp
IR/WeightUtils.cpp IR/WeightUtils.cpp
+29 -29
View File
@@ -31,7 +31,7 @@ static FailureOr<int64_t> ceilDivSigned(int64_t lhs, int64_t rhs) {
} }
Value createOrFoldAffineApply( Value createOrFoldAffineApply(
RewriterBase& rewriter, Location loc, AffineMap map, ValueRange operands, Operation* constantAnchor) { OpBuilder& builder, Location loc, AffineMap map, ValueRange operands, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
assert(map.getNumResults() == 1 && "affine.apply expects a single-result affine map"); assert(map.getNumResults() == 1 && "affine.apply expects a single-result affine map");
@@ -40,91 +40,91 @@ Value createOrFoldAffineApply(
for (Value operand : operands) { for (Value operand : operands) {
std::optional<int64_t> constantValue = matchConstantIndexValue(operand); std::optional<int64_t> constantValue = matchConstantIndexValue(operand);
if (!constantValue) if (!constantValue)
return affine::AffineApplyOp::create(rewriter, loc, map, operands).getResult(); return affine::AffineApplyOp::create(builder, loc, map, operands).getResult();
operandConstants.push_back(rewriter.getIndexAttr(*constantValue)); operandConstants.push_back(builder.getIndexAttr(*constantValue));
} }
SmallVector<Attribute> foldedResults; SmallVector<Attribute> foldedResults;
if (succeeded(map.constantFold(operandConstants, foldedResults)) && foldedResults.size() == 1) if (succeeded(map.constantFold(operandConstants, foldedResults)) && foldedResults.size() == 1)
if (auto constantResult = dyn_cast<IntegerAttr>(foldedResults.front())) if (auto constantResult = dyn_cast<IntegerAttr>(foldedResults.front()))
return getOrCreateIndexConstant(rewriter, constantAnchor, constantResult.getInt()); return getOrCreateIndexConstant(builder, constantAnchor, constantResult.getInt());
return affine::AffineApplyOp::create(rewriter, loc, map, operands).getResult(); return affine::AffineApplyOp::create(builder, loc, map, operands).getResult();
} }
Value createOrFoldAffineApply( Value createOrFoldAffineApply(
RewriterBase& rewriter, Location loc, AffineExpr expr, ValueRange dims, Operation* constantAnchor) { OpBuilder& builder, Location loc, AffineExpr expr, ValueRange dims, Operation* constantAnchor) {
AffineMap map = AffineMap::get(/*dimCount=*/dims.size(), /*symbolCount=*/0, expr); AffineMap map = AffineMap::get(/*dimCount=*/dims.size(), /*symbolCount=*/0, expr);
return createOrFoldAffineApply(rewriter, loc, map, dims, constantAnchor); return createOrFoldAffineApply(builder, loc, map, dims, constantAnchor);
} }
Value affineMulConst(RewriterBase& rewriter, Location loc, Value value, int64_t multiplier, Operation* constantAnchor) { Value affineMulConst(OpBuilder& builder, Location loc, Value value, int64_t multiplier, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
if (multiplier == 0) if (multiplier == 0)
return getOrCreateIndexConstant(rewriter, constantAnchor, 0); return getOrCreateIndexConstant(builder, constantAnchor, 0);
if (multiplier == 1) if (multiplier == 1)
return value; return value;
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
return createOrFoldAffineApply(rewriter, loc, d0 * multiplier, ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, d0 * multiplier, ValueRange {value}, constantAnchor);
} }
Value affineAddConst(RewriterBase& rewriter, Location loc, Value value, int64_t offset, Operation* constantAnchor) { Value affineAddConst(OpBuilder& builder, Location loc, Value value, int64_t offset, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
if (offset == 0) if (offset == 0)
return value; return value;
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
return createOrFoldAffineApply(rewriter, loc, d0 + offset, ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, d0 + offset, ValueRange {value}, constantAnchor);
} }
Value affineModConst(RewriterBase& rewriter, Location loc, Value value, int64_t divisor, Operation* constantAnchor) { Value affineModConst(OpBuilder& builder, Location loc, Value value, int64_t divisor, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
assert(divisor > 0 && "expected a positive affine.mod divisor"); assert(divisor > 0 && "expected a positive affine.mod divisor");
if (divisor == 1) if (divisor == 1)
return getOrCreateIndexConstant(rewriter, constantAnchor, 0); return getOrCreateIndexConstant(builder, constantAnchor, 0);
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
return createOrFoldAffineApply(rewriter, loc, d0 % divisor, ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, d0 % divisor, ValueRange {value}, constantAnchor);
} }
Value affineFloorDivConst( Value affineFloorDivConst(
RewriterBase& rewriter, Location loc, Value value, int64_t divisor, Operation* constantAnchor) { OpBuilder& builder, Location loc, Value value, int64_t divisor, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
assert(divisor > 0 && "expected a positive affine.floor_div divisor"); assert(divisor > 0 && "expected a positive affine.floor_div divisor");
if (divisor == 1) if (divisor == 1)
return value; return value;
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
return createOrFoldAffineApply(rewriter, loc, d0.floorDiv(divisor), ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, d0.floorDiv(divisor), ValueRange {value}, constantAnchor);
} }
Value affineAddModConst( Value affineAddModConst(
RewriterBase& rewriter, Location loc, Value value, int64_t offset, int64_t divisor, Operation* constantAnchor) { OpBuilder& builder, Location loc, Value value, int64_t offset, int64_t divisor, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
assert(divisor > 0 && "expected a positive affine.mod divisor"); assert(divisor > 0 && "expected a positive affine.mod divisor");
if (divisor == 1) if (divisor == 1)
return getOrCreateIndexConstant(rewriter, constantAnchor, 0); return getOrCreateIndexConstant(builder, constantAnchor, 0);
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
AffineExpr expr = d0; AffineExpr expr = d0;
if (offset != 0) if (offset != 0)
expr = expr + offset; expr = expr + offset;
return createOrFoldAffineApply(rewriter, loc, expr % divisor, ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, expr % divisor, ValueRange {value}, constantAnchor);
} }
Value affineAddFloorDivConst( Value affineAddFloorDivConst(
RewriterBase& rewriter, Location loc, Value value, int64_t offset, int64_t divisor, Operation* constantAnchor) { OpBuilder& builder, Location loc, Value value, int64_t offset, int64_t divisor, Operation* constantAnchor) {
assert(constantAnchor && "expected a valid constant anchor"); assert(constantAnchor && "expected a valid constant anchor");
assert(divisor > 0 && "expected a positive affine.floor_div divisor"); assert(divisor > 0 && "expected a positive affine.floor_div divisor");
if (divisor == 1) if (divisor == 1)
return offset == 0 ? value : affineAddConst(rewriter, loc, value, offset, constantAnchor); return offset == 0 ? value : affineAddConst(builder, loc, value, offset, constantAnchor);
AffineExpr d0 = getAffineDimExpr(0, rewriter.getContext()); AffineExpr d0 = getAffineDimExpr(0, builder.getContext());
AffineExpr expr = d0; AffineExpr expr = d0;
if (offset != 0) if (offset != 0)
expr = expr + offset; expr = expr + offset;
return createOrFoldAffineApply(rewriter, loc, expr.floorDiv(divisor), ValueRange {value}, constantAnchor); return createOrFoldAffineApply(builder, loc, expr.floorDiv(divisor), ValueRange {value}, constantAnchor);
} }
FailureOr<int64_t> evaluateAffineExpr(AffineExpr expr, ArrayRef<int64_t> dims, ArrayRef<int64_t> symbols) { FailureOr<int64_t> evaluateAffineExpr(AffineExpr expr, ArrayRef<int64_t> dims, ArrayRef<int64_t> symbols) {
+8 -8
View File
@@ -11,50 +11,50 @@ namespace onnx_mlir {
using IndexValueResolver = llvm::function_ref<llvm::FailureOr<int64_t>(mlir::Value)>; using IndexValueResolver = llvm::function_ref<llvm::FailureOr<int64_t>(mlir::Value)>;
mlir::Value createOrFoldAffineApply(mlir::RewriterBase& rewriter, mlir::Value createOrFoldAffineApply(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::AffineMap map, mlir::AffineMap map,
mlir::ValueRange operands, mlir::ValueRange operands,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value createOrFoldAffineApply(mlir::RewriterBase& rewriter, mlir::Value createOrFoldAffineApply(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::AffineExpr expr, mlir::AffineExpr expr,
mlir::ValueRange dims, mlir::ValueRange dims,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineMulConst(mlir::RewriterBase& rewriter, mlir::Value affineMulConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t multiplier, int64_t multiplier,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineAddConst(mlir::RewriterBase& rewriter, mlir::Value affineAddConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t offset, int64_t offset,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineModConst(mlir::RewriterBase& rewriter, mlir::Value affineModConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t divisor, int64_t divisor,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineFloorDivConst(mlir::RewriterBase& rewriter, mlir::Value affineFloorDivConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t divisor, int64_t divisor,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineAddModConst(mlir::RewriterBase& rewriter, mlir::Value affineAddModConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t offset, int64_t offset,
int64_t divisor, int64_t divisor,
mlir::Operation* constantAnchor); mlir::Operation* constantAnchor);
mlir::Value affineAddFloorDivConst(mlir::RewriterBase& rewriter, mlir::Value affineAddFloorDivConst(mlir::OpBuilder& builder,
mlir::Location loc, mlir::Location loc,
mlir::Value value, mlir::Value value,
int64_t offset, int64_t offset,
+13 -7
View File
@@ -49,7 +49,7 @@ Value getOrCreateConstant(OperationFolder& folder, Operation* anchorOp, Attribut
return folder.getOrCreateConstant(hostBlock, arithDialect, value, type); return folder.getOrCreateConstant(hostBlock, arithDialect, value, type);
} }
Value getOrCreateConstant(RewriterBase& rewriter, Operation* anchorOp, Attribute value, Type type) { Value getOrCreateConstant(OpBuilder& builder, Operation* anchorOp, Attribute value, Type type) {
assert(anchorOp && "expected a valid anchor operation"); assert(anchorOp && "expected a valid anchor operation");
Block* hostBlock = getConstantInsertionBlock(anchorOp); Block* hostBlock = getConstantInsertionBlock(anchorOp);
for (Operation& op : *hostBlock) { for (Operation& op : *hostBlock) {
@@ -59,9 +59,16 @@ Value getOrCreateConstant(RewriterBase& rewriter, Operation* anchorOp, Attribute
return constantOp.getResult(); return constantOp.getResult();
} }
OpBuilder::InsertionGuard guard(rewriter); OpBuilder::InsertionGuard guard(builder);
rewriter.setInsertionPointToStart(hostBlock); builder.setInsertionPointToStart(hostBlock);
return arith::ConstantOp::create(rewriter, anchorOp->getLoc(), type, cast<TypedAttr>(value)).getResult(); return arith::ConstantOp::create(builder, anchorOp->getLoc(), type, cast<TypedAttr>(value)).getResult();
}
Value createConstantAtHostBlockStart(OpBuilder& builder, Operation* anchorOp, TypedAttr value) {
assert(anchorOp && "expected a valid anchor operation");
OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(getConstantInsertionBlock(anchorOp));
return arith::ConstantOp::create(builder, anchorOp->getLoc(), value).getResult();
} }
Value getOrCreateConstantLike(OperationFolder& folder, arith::ConstantOp constantOp) { Value getOrCreateConstantLike(OperationFolder& folder, arith::ConstantOp constantOp) {
@@ -73,9 +80,8 @@ Value getOrCreateIndexConstant(OperationFolder& folder, Operation* anchorOp, int
return getOrCreateConstant(folder, anchorOp, builder.getIndexAttr(value), builder.getIndexType()); return getOrCreateConstant(folder, anchorOp, builder.getIndexAttr(value), builder.getIndexType());
} }
Value getOrCreateIndexConstant(RewriterBase& rewriter, Operation* anchorOp, int64_t value) { Value getOrCreateIndexConstant(OpBuilder& builder, Operation* anchorOp, int64_t value) {
Builder builder(anchorOp->getContext()); return getOrCreateConstant(builder, anchorOp, builder.getIndexAttr(value), builder.getIndexType());
return getOrCreateConstant(rewriter, anchorOp, builder.getIndexAttr(value), builder.getIndexType());
} }
void hoistAndUniquifyIndexConstants(func::FuncOp funcOp, RewriterBase& rewriter) { void hoistAndUniquifyIndexConstants(func::FuncOp funcOp, RewriterBase& rewriter) {
+5 -2
View File
@@ -16,13 +16,16 @@ mlir::Value
getOrCreateConstant(mlir::OperationFolder& folder, mlir::Operation* anchorOp, mlir::Attribute value, mlir::Type type); getOrCreateConstant(mlir::OperationFolder& folder, mlir::Operation* anchorOp, mlir::Attribute value, mlir::Type type);
mlir::Value mlir::Value
getOrCreateConstant(mlir::RewriterBase& rewriter, mlir::Operation* anchorOp, mlir::Attribute value, mlir::Type type); getOrCreateConstant(mlir::OpBuilder& builder, mlir::Operation* anchorOp, mlir::Attribute value, mlir::Type type);
mlir::Value
createConstantAtHostBlockStart(mlir::OpBuilder& builder, mlir::Operation* anchorOp, mlir::TypedAttr value);
mlir::Value getOrCreateConstantLike(mlir::OperationFolder& folder, mlir::arith::ConstantOp constantOp); mlir::Value getOrCreateConstantLike(mlir::OperationFolder& folder, mlir::arith::ConstantOp constantOp);
mlir::Value getOrCreateIndexConstant(mlir::OperationFolder& folder, mlir::Operation* anchorOp, int64_t value); mlir::Value getOrCreateIndexConstant(mlir::OperationFolder& folder, mlir::Operation* anchorOp, int64_t value);
mlir::Value getOrCreateIndexConstant(mlir::RewriterBase& rewriter, mlir::Operation* anchorOp, int64_t value); mlir::Value getOrCreateIndexConstant(mlir::OpBuilder& builder, mlir::Operation* anchorOp, int64_t value);
void hoistAndUniquifyIndexConstants(mlir::func::FuncOp funcOp, mlir::RewriterBase& rewriter); void hoistAndUniquifyIndexConstants(mlir::func::FuncOp funcOp, mlir::RewriterBase& rewriter);
+78 -2
View File
@@ -36,9 +36,10 @@ bool isCoreStaticAddressOp(mlir::Operation* op) {
mlir::LogicalResult mlir::LogicalResult
walkPimCoreBlock(mlir::Block& block, walkPimCoreBlock(mlir::Block& block,
const StaticValueKnowledge& knowledge, const StaticValueKnowledge& initialKnowledge,
llvm::function_ref<mlir::LogicalResult(mlir::Operation&, const StaticValueKnowledge&)> callback) { llvm::function_ref<mlir::LogicalResult(mlir::Operation&, const StaticValueKnowledge&)> callback) {
bool hasFailure = false; bool hasFailure = false;
StaticValueKnowledge knowledge = initialKnowledge;
for (mlir::Operation& op : block) { for (mlir::Operation& op : block) {
if (mlir::isa<pim::PimHaltOp, mlir::scf::YieldOp>(op) || isCoreStaticAddressOp(&op)) if (mlir::isa<pim::PimHaltOp, mlir::scf::YieldOp>(op) || isCoreStaticAddressOp(&op))
continue; continue;
@@ -74,6 +75,42 @@ walkPimCoreBlock(mlir::Block& block,
continue; continue;
} }
if (auto ifOp = mlir::dyn_cast<mlir::scf::IfOp>(op)) {
auto condition = resolveIndexValue(ifOp.getCondition(), knowledge);
if (failed(condition)) {
ifOp.emitOpError("requires statically evaluable scf.if condition for PIM codegen");
hasFailure = true;
continue;
}
mlir::Region& selectedRegion = *condition != 0 ? ifOp.getThenRegion() : ifOp.getElseRegion();
if (!selectedRegion.empty())
if (failed(walkPimCoreBlock(selectedRegion.front(), knowledge, callback)))
hasFailure = true;
continue;
}
if (auto switchOp = mlir::dyn_cast<mlir::scf::IndexSwitchOp>(op)) {
auto selector = resolveIndexValue(switchOp.getArg(), knowledge);
if (failed(selector)) {
switchOp.emitOpError("requires a statically evaluable scf.index_switch selector for PIM codegen");
hasFailure = true;
continue;
}
mlir::Region* selected = &switchOp.getDefaultRegion();
for (auto [caseValue, caseRegion] : llvm::zip(switchOp.getCases(), switchOp.getCaseRegions()))
if (caseValue == *selector) {
selected = &caseRegion;
break;
}
if (failed(walkPimCoreBlock(selected->front(), knowledge, callback)))
hasFailure = true;
auto yield = mlir::cast<mlir::scf::YieldOp>(selected->front().getTerminator());
for (auto [result, yielded] : llvm::zip(switchOp.getResults(), yield.getOperands()))
knowledge.aliases[result] = resolveLoopCarriedAlias(yielded, knowledge);
continue;
}
if (failed(callback(op, knowledge))) if (failed(callback(op, knowledge)))
hasFailure = true; hasFailure = true;
} }
@@ -82,9 +119,10 @@ walkPimCoreBlock(mlir::Block& block,
mlir::LogicalResult walkPimCoreBlockStructurally( mlir::LogicalResult walkPimCoreBlockStructurally(
mlir::Block& block, mlir::Block& block,
const StaticValueKnowledge& knowledge, const StaticValueKnowledge& initialKnowledge,
llvm::function_ref<mlir::LogicalResult(mlir::Operation&, const StaticValueKnowledge&)> callback) { llvm::function_ref<mlir::LogicalResult(mlir::Operation&, const StaticValueKnowledge&)> callback) {
bool hasFailure = false; bool hasFailure = false;
StaticValueKnowledge knowledge = initialKnowledge;
for (mlir::Operation& op : block) { for (mlir::Operation& op : block) {
if (mlir::isa<pim::PimHaltOp, mlir::scf::YieldOp>(op) || isCoreStaticAddressOp(&op)) if (mlir::isa<pim::PimHaltOp, mlir::scf::YieldOp>(op) || isCoreStaticAddressOp(&op))
continue; continue;
@@ -128,6 +166,44 @@ mlir::LogicalResult walkPimCoreBlockStructurally(
continue; continue;
} }
if (auto ifOp = mlir::dyn_cast<mlir::scf::IfOp>(op)) {
if (failed(resolveIndexValue(ifOp.getCondition(), knowledge))) {
ifOp.emitOpError("requires statically evaluable scf.if condition for PIM verification");
hasFailure = true;
continue;
}
if (!ifOp.getThenRegion().empty())
if (failed(walkPimCoreBlockStructurally(ifOp.getThenRegion().front(), knowledge, callback)))
hasFailure = true;
if (!ifOp.getElseRegion().empty())
if (failed(walkPimCoreBlockStructurally(ifOp.getElseRegion().front(), knowledge, callback)))
hasFailure = true;
continue;
}
if (auto switchOp = mlir::dyn_cast<mlir::scf::IndexSwitchOp>(op)) {
auto selector = resolveIndexValue(switchOp.getArg(), knowledge);
if (failed(selector)) {
switchOp.emitOpError("requires a statically evaluable scf.index_switch selector for PIM verification");
hasFailure = true;
continue;
}
mlir::Region* selected = &switchOp.getDefaultRegion();
for (auto [caseValue, caseRegion] : llvm::zip(switchOp.getCases(), switchOp.getCaseRegions()))
if (caseValue == *selector) {
selected = &caseRegion;
break;
}
for (mlir::Region& region : switchOp->getRegions())
if (failed(walkPimCoreBlockStructurally(region.front(), knowledge, callback)))
hasFailure = true;
auto yield = mlir::cast<mlir::scf::YieldOp>(selected->front().getTerminator());
for (auto [result, yielded] : llvm::zip(switchOp.getResults(), yield.getOperands()))
knowledge.aliases[result] = resolveLoopCarriedAlias(yielded, knowledge);
continue;
}
if (failed(callback(op, knowledge))) if (failed(callback(op, knowledge)))
hasFailure = true; hasFailure = true;
} }
+39
View File
@@ -0,0 +1,39 @@
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "ShapingUtils.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp"
using namespace mlir;
namespace onnx_mlir {
bool isShapingOnlyOp(Operation *op) {
return isa<tensor::CastOp,
tensor::CollapseShapeOp,
tensor::ExpandShapeOp,
tensor::ExtractSliceOp,
tensor::InsertSliceOp,
tensor::ConcatOp,
tensor::EmptyOp,
tensor::ExtractOp,
tensor::InsertOp,
tensor::SplatOp,
linalg::TransposeOp,
ONNXTransposeOp,
spatial::SpatConcatOp,
spatial::SpatExtractRowsOp>(op);
}
bool isPureIndexComputationOp(Operation *op) {
if (op->getNumRegions() != 0 || op->getNumResults() == 0 || op->hasTrait<OpTrait::IsTerminator>()
|| !isMemoryEffectFree(op))
return false;
auto isIndexOrInteger = [](Type type) { return type.isIndex() || isa<IntegerType>(type); };
return llvm::all_of(op->getOperandTypes(), isIndexOrInteger)
&& llvm::all_of(op->getResultTypes(), isIndexOrInteger);
}
} // namespace onnx_mlir
+13
View File
@@ -0,0 +1,13 @@
#pragma once
namespace mlir {
class Operation;
}
namespace onnx_mlir {
bool isShapingOnlyOp(mlir::Operation *op);
bool isPureIndexComputationOp(mlir::Operation *op);
} // namespace onnx_mlir
+35
View File
@@ -68,4 +68,39 @@ Value insertStaticSlice(
.getResult(); .getResult();
} }
FailureOr<Value> addLeadingUnitTensorDimension(OpBuilder& builder, Location loc, Value value) {
auto type = dyn_cast<RankedTensorType>(value.getType());
if (!type || !type.hasStaticShape())
return failure();
SmallVector<int64_t> shape {1};
llvm::append_range(shape, type.getShape());
auto resultType = RankedTensorType::get(shape, type.getElementType(), type.getEncoding());
SmallVector<ReassociationIndices> reassociation;
if (type.getRank() != 0) {
reassociation.push_back({0, 1});
for (int64_t dim = 1; dim < type.getRank(); ++dim)
reassociation.push_back({dim + 1});
}
return tensor::ExpandShapeOp::create(builder, loc, resultType, value, reassociation).getResult();
}
FailureOr<Value> removeLeadingUnitTensorDimension(
OpBuilder& builder, Location loc, Value value, RankedTensorType resultType) {
if (value.getType() == resultType)
return value;
auto type = dyn_cast<RankedTensorType>(value.getType());
if (!type || !resultType || !type.hasStaticShape() || !resultType.hasStaticShape()
|| type.getRank() != resultType.getRank() + 1 || type.getDimSize(0) != 1
|| type.getElementType() != resultType.getElementType()
|| !llvm::equal(type.getShape().drop_front(), resultType.getShape()))
return failure();
SmallVector<ReassociationIndices> reassociation;
if (resultType.getRank() != 0) {
reassociation.push_back({0, 1});
for (int64_t dim = 1; dim < resultType.getRank(); ++dim)
reassociation.push_back({dim + 1});
}
return tensor::CollapseShapeOp::create(builder, loc, resultType, value, reassociation).getResult();
}
} // namespace onnx_mlir } // namespace onnx_mlir
+6
View File
@@ -25,4 +25,10 @@ mlir::Value insertStaticSlice(mlir::PatternRewriter& rewriter,
mlir::Value dest, mlir::Value dest,
llvm::ArrayRef<mlir::OpFoldResult> offsets); llvm::ArrayRef<mlir::OpFoldResult> offsets);
mlir::FailureOr<mlir::Value>
addLeadingUnitTensorDimension(mlir::OpBuilder& builder, mlir::Location loc, mlir::Value value);
mlir::FailureOr<mlir::Value> removeLeadingUnitTensorDimension(
mlir::OpBuilder& builder, mlir::Location loc, mlir::Value value, mlir::RankedTensorType resultType);
} // namespace onnx_mlir } // namespace onnx_mlir
+14 -6
View File
@@ -7,18 +7,26 @@
namespace onnx_mlir { namespace onnx_mlir {
void dumpModule(mlir::ModuleOp moduleOp, const std::string& name) { std::fstream openDialectDumpFileWithExtension(const std::string& name, llvm::StringRef destination, llvm::StringRef extension) {
std::string outputDir = getOutputDir(); std::string outputDir = getOutputDir();
if (outputDir.empty()) if (outputDir.empty())
return {};
std::string dialectsDir = (outputDir + destination).str();
createDirectory(dialectsDir);
return std::fstream(dialectsDir + "/" + name + "." + extension.str(), std::ios::out);
}
void dumpModule(mlir::ModuleOp moduleOp, const std::string& name, bool assumeVerified) {
std::fstream file = openDialectDumpFileWithExtension(name, "/dialects", "mlir");
if (!file.is_open())
return; return;
std::string dialectsDir = outputDir + "/dialects";
createDirectory(dialectsDir);
std::fstream file(dialectsDir + "/" + name + ".mlir", std::ios::out);
llvm::raw_os_ostream os(file); llvm::raw_os_ostream os(file);
mlir::OpPrintingFlags flags; mlir::OpPrintingFlags flags;
flags.elideLargeElementsAttrs().enableDebugInfo(true, false); flags.elideLargeElementsAttrs().enableDebugInfo(false, false);
if (assumeVerified)
flags.assumeVerified();
moduleOp.print(os, flags); moduleOp.print(os, flags);
os.flush(); os.flush();
file.close(); file.close();
+6 -1
View File
@@ -1,13 +1,18 @@
#pragma once #pragma once
#include "mlir/IR/BuiltinOps.h" #include "mlir/IR/BuiltinOps.h"
#include "llvm/ADT/StringRef.h"
#include <fstream>
#include <string> #include <string>
namespace onnx_mlir { namespace onnx_mlir {
/// Emits a MLIR snapshot under the current compiler output /// Emits a MLIR snapshot under the current compiler output
/// directory for pass-level debugging. /// directory for pass-level debugging.
void dumpModule(mlir::ModuleOp moduleOp, const std::string& name); void dumpModule(mlir::ModuleOp moduleOp, const std::string& name, bool assumeVerified = false);
/// Opens a file under the same dialect dump directory used by dumpModule.
std::fstream openDialectDumpFileWithExtension(const std::string& name,llvm::StringRef destination = "/dialects", llvm::StringRef extension = "mlir");
} // namespace onnx_mlir } // namespace onnx_mlir
+163 -22
View File
@@ -414,31 +414,35 @@ size_t PimAcceleratorMemory::getValueAddress(mlir::Value value,
const StaticValueKnowledge& knowledge, const StaticValueKnowledge& knowledge,
std::optional<unsigned> lane) const { std::optional<unsigned> lane) const {
value = resolveCachedAlias(value, knowledge); value = resolveCachedAlias(value, knowledge);
auto compiledIt = compiledAddressExprs.find(value);
if (compiledIt == compiledAddressExprs.end()) { FailureOr<ResolvedContiguousAddress> resolvedAddress = resolveContiguousAddress(value, knowledge);
auto compiledExpr = compileContiguousAddressExpr(value); if (failed(resolvedAddress)) {
if (failed(compiledExpr)) { auto compiledIt = compiledAddressExprs.find(value);
errs() << "Failed to compile contiguous address for value: "; if (compiledIt == compiledAddressExprs.end()) {
auto compiledExpr = compileContiguousAddressExpr(value);
if (failed(compiledExpr)) {
errs() << "Failed to compile contiguous address for value: ";
value.print(errs());
errs() << " : " << value.getType();
errs() << "\n";
llvm_unreachable("Failed to compile contiguous address");
}
compiledIt = compiledAddressExprs.try_emplace(value, *compiledExpr).first;
}
resolvedAddress = compiledIt->second.evaluate(knowledge, lane);
if (failed(resolvedAddress)) {
errs() << "Failed to evaluate contiguous address for value: ";
value.print(errs()); value.print(errs());
errs() << " : " << value.getType(); errs() << " : " << value.getType();
errs() << "\n"; errs() << "\n";
llvm_unreachable("Failed to compile contiguous address"); if (auto* definingOp = value.getDefiningOp()) {
errs() << "Defining op:\n";
definingOp->print(errs());
errs() << "\n";
}
llvm_unreachable("Failed to resolve contiguous address");
} }
compiledIt = compiledAddressExprs.try_emplace(value, *compiledExpr).first;
}
auto resolvedAddress = compiledIt->second.evaluate(knowledge, lane);
if (failed(resolvedAddress)) {
errs() << "Failed to evaluate contiguous address for value: ";
value.print(errs());
errs() << " : " << value.getType();
errs() << "\n";
if (auto* definingOp = value.getDefiningOp()) {
errs() << "Defining op:\n";
definingOp->print(errs());
errs() << "\n";
}
llvm_unreachable("Failed to resolve contiguous address");
} }
MemoryValueKey key = getMemoryValueKey(resolvedAddress->base, lane); MemoryValueKey key = getMemoryValueKey(resolvedAddress->base, lane);
@@ -1114,7 +1118,9 @@ enum class CompiledCoreOpKind : uint8_t {
struct CompiledCoreNode { struct CompiledCoreNode {
enum class Kind : uint8_t { enum class Kind : uint8_t {
Op, Op,
Loop Loop,
If,
IndexSwitch
}; };
Kind kind = Kind::Op; Kind kind = Kind::Op;
@@ -1123,7 +1129,13 @@ struct CompiledCoreNode {
CompiledIndexExpr lowerBound; CompiledIndexExpr lowerBound;
CompiledIndexExpr upperBound; CompiledIndexExpr upperBound;
CompiledIndexExpr step; CompiledIndexExpr step;
CompiledIndexExpr condition;
std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>> loopBody; std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>> loopBody;
std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>> thenBody;
std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>> elseBody;
llvm::SmallVector<int64_t> caseValues;
llvm::SmallVector<std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>>> caseBodies;
std::unique_ptr<llvm::SmallVector<CompiledCoreNode, 8>> defaultBody;
}; };
static FailureOr<CompiledCoreOpKind> classifyCompiledCoreOpKind(Operation& op) { static FailureOr<CompiledCoreOpKind> classifyCompiledCoreOpKind(Operation& op) {
@@ -1201,6 +1213,53 @@ compileCoreEmissionPlan(Block& block, Operation* weightOwner, llvm::SmallVectorI
continue; continue;
} }
if (auto ifOp = dyn_cast<mlir::scf::IfOp>(op)) {
auto condition = compileIndexExpr(ifOp.getCondition());
if (failed(condition)) {
ifOp.emitOpError("requires statically evaluable scf.if condition for PIM codegen");
return failure();
}
CompiledCoreNode ifNode;
ifNode.kind = CompiledCoreNode::Kind::If;
ifNode.op = ifOp.getOperation();
ifNode.condition = *condition;
ifNode.thenBody = std::make_unique<llvm::SmallVector<CompiledCoreNode, 8>>();
if (failed(compileCoreEmissionPlan(ifOp.getThenRegion().front(), weightOwner, *ifNode.thenBody)))
return failure();
ifNode.elseBody = std::make_unique<llvm::SmallVector<CompiledCoreNode, 8>>();
if (!ifOp.getElseRegion().empty())
if (failed(compileCoreEmissionPlan(ifOp.getElseRegion().front(), weightOwner, *ifNode.elseBody)))
return failure();
plan.push_back(std::move(ifNode));
continue;
}
if (auto switchOp = dyn_cast<mlir::scf::IndexSwitchOp>(op)) {
auto selector = compileIndexExpr(switchOp.getArg());
if (failed(selector)) {
switchOp.emitOpError("requires a statically evaluable scf.index_switch selector for PIM codegen");
return failure();
}
CompiledCoreNode switchNode;
switchNode.kind = CompiledCoreNode::Kind::IndexSwitch;
switchNode.op = switchOp.getOperation();
switchNode.condition = *selector;
llvm::append_range(switchNode.caseValues, switchOp.getCases());
for (mlir::Region& region : switchOp.getCaseRegions()) {
auto body = std::make_unique<llvm::SmallVector<CompiledCoreNode, 8>>();
if (failed(compileCoreEmissionPlan(region.front(), weightOwner, *body)))
return failure();
switchNode.caseBodies.push_back(std::move(body));
}
switchNode.defaultBody = std::make_unique<llvm::SmallVector<CompiledCoreNode, 8>>();
if (failed(compileCoreEmissionPlan(
switchOp.getDefaultRegion().front(), weightOwner, *switchNode.defaultBody)))
return failure();
plan.push_back(std::move(switchNode));
continue;
}
auto opKind = classifyCompiledCoreOpKind(op); auto opKind = classifyCompiledCoreOpKind(op);
if (failed(opKind)) { if (failed(opKind)) {
InFlightDiagnostic diag = op.emitError() << "unsupported codegen for op '" << op.getName().getStringRef() << "'"; InFlightDiagnostic diag = op.emitError() << "unsupported codegen for op '" << op.getName().getStringRef() << "'";
@@ -1263,6 +1322,51 @@ static LogicalResult executeCompiledCorePlan(
continue; continue;
} }
if (node.kind == CompiledCoreNode::Kind::If) {
auto condition = node.condition.evaluate(knowledge);
auto ifOp = cast<mlir::scf::IfOp>(node.op);
if (failed(condition)) {
ifOp.emitOpError("requires statically evaluable scf.if condition for PIM codegen");
return failure();
}
const auto& selectedBody = *condition != 0 ? node.thenBody : node.elseBody;
if (selectedBody && failed(executeCompiledCorePlan(*selectedBody,
coreCodeGen,
knowledge,
resolveWeightSlot,
processedOperations,
batchLane,
batchLaneCount)))
return failure();
continue;
}
if (node.kind == CompiledCoreNode::Kind::IndexSwitch) {
auto selector = node.condition.evaluate(knowledge);
auto switchOp = cast<mlir::scf::IndexSwitchOp>(node.op);
if (failed(selector)) {
switchOp.emitOpError("requires a statically evaluable scf.index_switch selector for PIM codegen");
return failure();
}
const llvm::SmallVectorImpl<CompiledCoreNode>* selectedBody = node.defaultBody.get();
mlir::Region* selectedRegion = &switchOp.getDefaultRegion();
for (auto [index, caseValue] : llvm::enumerate(node.caseValues))
if (caseValue == *selector) {
selectedBody = node.caseBodies[index].get();
selectedRegion = &switchOp.getCaseRegions()[index];
break;
}
if (failed(executeCompiledCorePlan(*selectedBody, coreCodeGen, knowledge,
resolveWeightSlot, processedOperations,
batchLane, batchLaneCount)))
return failure();
auto yield = cast<mlir::scf::YieldOp>(selectedRegion->front().getTerminator());
for (auto [result, yielded] : llvm::zip(switchOp.getResults(), yield.getOperands()))
knowledge.aliases[result] = resolveLoopCarriedAlias(yielded, knowledge);
continue;
}
switch (node.opKind) { switch (node.opKind) {
case CompiledCoreOpKind::Load: case CompiledCoreOpKind::Load:
coreCodeGen.codeGenLoadOp(cast<pim::PimMemCopyHostToDevOp>(node.op), knowledge); coreCodeGen.codeGenLoadOp(cast<pim::PimMemCopyHostToDevOp>(node.op), knowledge);
@@ -1363,6 +1467,36 @@ static int64_t codeGenCoreOps(
return failed(result) ? -1 : static_cast<int64_t>(processedOperations); return failed(result) ? -1 : static_cast<int64_t>(processedOperations);
} }
static OnnxMlirCompilerErrorCodes emitEmptyCoreArtifacts(StringRef outputDirPath, size_t emittedCoreId) {
std::string outputCorePath =
(outputDirPath + "/core_" + std::to_string(emittedCoreId) + ".pim").str();
std::error_code errorCode;
raw_fd_ostream coreBinaryStream(outputCorePath, errorCode, sys::fs::OF_None);
if (errorCode) {
errs() << "Error while opening core file `" << outputCorePath << "`: " << errorCode.message() << '\n';
return InvalidOutputFileAccess;
}
pim_binary::writeHeader(coreBinaryStream);
pim_binary::patchInstructionCount(coreBinaryStream, 0);
coreBinaryStream.close();
if (!pimEmitJson.getValue())
return CompilerSuccess;
std::string outputCoreJsonPath =
(outputDirPath + "/core_" + std::to_string(emittedCoreId) + ".json").str();
errorCode = std::error_code();
raw_fd_ostream coreJsonStream(outputCoreJsonPath, errorCode);
if (errorCode) {
errs() << "Error while opening core json file `" << outputCoreJsonPath << "`: " << errorCode.message() << '\n';
return InvalidOutputFileAccess;
}
coreJsonStream << "[]";
coreJsonStream.close();
return CompilerSuccess;
}
OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimCode(ModuleOp& moduleOp, std::string& outputDirPath) { OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimCode(ModuleOp& moduleOp, std::string& outputDirPath) {
if (!outputDirPath.empty()) { if (!outputDirPath.empty()) {
if (auto error = sys::fs::create_directory(outputDirPath)) { if (auto error = sys::fs::create_directory(outputDirPath)) {
@@ -1607,6 +1741,13 @@ OnnxMlirCompilerErrorCodes onnx_mlir::compileToPimCode(ModuleOp& moduleOp, std::
if (jobResults[jobIndex].status != CompilerSuccess) if (jobResults[jobIndex].status != CompilerSuccess)
return jobResults[jobIndex].status; return jobResults[jobIndex].status;
if (jobs.empty()) {
if (auto err = emitEmptyCoreArtifacts(outputDirPath, 0))
return err;
xbarsPerArrayGroup["core0"] = json::Array {};
memory.recordCoreReport(0, MemoryReportRow {});
}
llvm::SmallVector<WeightFileRequest, 8> weightRequests; llvm::SmallVector<WeightFileRequest, 8> weightRequests;
weightRequests.reserve(jobs.size()); weightRequests.reserve(jobs.size());
for (size_t jobIndex = 0; jobIndex < jobs.size(); ++jobIndex) { for (size_t jobIndex = 0; jobIndex < jobs.size(); ++jobIndex) {
+14
View File
@@ -57,6 +57,20 @@ llvm::cl::opt<PimConvLoweringType> pimConvLowering(
llvm::cl::init(PimConvLoweringAuto), llvm::cl::init(PimConvLoweringAuto),
llvm::cl::cat(OnnxMlirOptions)); llvm::cl::cat(OnnxMlirOptions));
llvm::cl::opt<PimSpatialDataflowExportType> pimExportSpatialDataflow(
"pim-export-spatial-dataflow",
llvm::cl::desc("Emit Gephi-importable CSV dataflow reports for Spatial pipeline snapshots"),
llvm::cl::values(clEnumValN(SpatialDataflowExportNone, "none", "Do not emit Spatial dataflow CSV reports")),
llvm::cl::values(
clEnumValN(SpatialDataflowExportSpatial1, "spatial1", "Emit spatial1 graph dataflow CSV reports")),
llvm::cl::values(
clEnumValN(SpatialDataflowExportSpatial2, "spatial2", "Emit spatial2 scheduled dataflow CSV reports")),
llvm::cl::values(
clEnumValN(SpatialDataflowExportSpatial3, "spatial3", "Emit spatial3 realized dataflow CSV reports")),
llvm::cl::values(clEnumValN(SpatialDataflowExportAll, "all", "Emit all Spatial dataflow CSV reports")),
llvm::cl::init(SpatialDataflowExportNone),
llvm::cl::cat(OnnxMlirOptions));
llvm::cl::opt<bool> llvm::cl::opt<bool>
pimOnlyCodegen("pim-only-codegen", pimOnlyCodegen("pim-only-codegen",
llvm::cl::desc("Only generate code for PIM (assume input is already in bufferized PIM IR)"), llvm::cl::desc("Only generate code for PIM (assume input is already in bufferized PIM IR)"),
+9
View File
@@ -42,11 +42,20 @@ typedef enum {
PimConvLoweringTiled2D = 8, PimConvLoweringTiled2D = 8,
} PimConvLoweringType; } PimConvLoweringType;
typedef enum {
SpatialDataflowExportNone = 0,
SpatialDataflowExportSpatial1 = 1,
SpatialDataflowExportSpatial2 = 2,
SpatialDataflowExportSpatial3 = 3,
SpatialDataflowExportAll = 4,
} PimSpatialDataflowExportType;
extern llvm::cl::OptionCategory OnnxMlirOptions; extern llvm::cl::OptionCategory OnnxMlirOptions;
extern llvm::cl::opt<PimEmissionTargetType> pimEmissionTarget; extern llvm::cl::opt<PimEmissionTargetType> pimEmissionTarget;
extern llvm::cl::opt<PimMergeSchedulerType> pimMergeScheduler; extern llvm::cl::opt<PimMergeSchedulerType> pimMergeScheduler;
extern llvm::cl::opt<PimMemoryReportLevel> pimMemoryReport; extern llvm::cl::opt<PimMemoryReportLevel> pimMemoryReport;
extern llvm::cl::opt<PimConvLoweringType> pimConvLowering; extern llvm::cl::opt<PimConvLoweringType> pimConvLowering;
extern llvm::cl::opt<PimSpatialDataflowExportType> pimExportSpatialDataflow;
extern llvm::cl::opt<bool> pimOnlyCodegen; extern llvm::cl::opt<bool> pimOnlyCodegen;
extern llvm::cl::opt<bool> pimDisableMemoryCoalescing; extern llvm::cl::opt<bool> pimDisableMemoryCoalescing;
+20 -1
View File
@@ -291,7 +291,26 @@ computeMemoryTouchInterval(memref::AllocOp allocOp, const OperationOrdering& ord
if (auto yieldOp = dyn_cast<scf::YieldOp>(user)) { if (auto yieldOp = dyn_cast<scf::YieldOp>(user)) {
auto forOp = dyn_cast<scf::ForOp>(yieldOp->getParentOp()); auto forOp = dyn_cast<scf::ForOp>(yieldOp->getParentOp());
if (!forOp) { auto ifOp = dyn_cast<scf::IfOp>(yieldOp->getParentOp());
auto indexSwitch = dyn_cast<scf::IndexSwitchOp>(yieldOp->getParentOp());
if (ifOp) {
for (auto [index, operand] : llvm::enumerate(yieldOp.getOperands())) {
if (operand != value)
continue;
pendingValues.push_back(ifOp.getResult(index));
appendAliasDescription(interval.aliasesFollowed, ifOp.getResult(index));
}
}
else if (indexSwitch) {
for (auto [index, operand] : llvm::enumerate(yieldOp.getOperands())) {
if (operand != value)
continue;
pendingValues.push_back(indexSwitch.getResult(index));
appendAliasDescription(interval.aliasesFollowed,
indexSwitch.getResult(index));
}
}
else if (!forOp) {
addFallbackReason(interval.fallbackReason, "yield without scf.for parent"); addFallbackReason(interval.fallbackReason, "yield without scf.for parent");
} }
else { else {
+1 -2
View File
@@ -1,3 +1,2 @@
add_subdirectory(ONNXToSpatial) add_subdirectory(ONNXToSpatial)
add_subdirectory(SpatialToGraphviz) add_subdirectory(SpatialToPim)
add_subdirectory(SpatialToPim)
@@ -20,6 +20,7 @@ add_pim_library(OMONNXToSpatial
Patterns/NN/Sigmoid.cpp Patterns/NN/Sigmoid.cpp
Patterns/NN/Softmax.cpp Patterns/NN/Softmax.cpp
Patterns/Tensor/Concat.cpp Patterns/Tensor/Concat.cpp
Patterns/Tensor/Flatten.cpp
Patterns/Tensor/Gather.cpp Patterns/Tensor/Gather.cpp
Patterns/Tensor/Resize.cpp Patterns/Tensor/Resize.cpp
Patterns/Tensor/Reshape.cpp Patterns/Tensor/Reshape.cpp
@@ -30,8 +31,10 @@ add_pim_library(OMONNXToSpatial
SpatialLayoutPlanningPass.cpp SpatialLayoutPlanningPass.cpp
LowerSpatialPlansPass.cpp LowerSpatialPlansPass.cpp
Common/AttributeUtils.cpp Common/AttributeUtils.cpp
Common/BiasAddUtils.cpp
Common/ComputeRegionBuilder.cpp Common/ComputeRegionBuilder.cpp
Common/MatrixProductLowering.cpp Common/MatrixProductLowering.cpp
Common/RowStripLayoutUtils.cpp
Common/ShapeTilingUtils.cpp Common/ShapeTilingUtils.cpp
Common/WeightMaterialization.cpp Common/WeightMaterialization.cpp
@@ -0,0 +1,112 @@
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "llvm/ADT/SmallVector.h"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/BiasAddUtils.hpp"
using namespace mlir;
namespace onnx_mlir {
LogicalResult isSupportedBiasAddShape(RankedTensorType biasType, RankedTensorType resultType) {
if (!biasType || !resultType || !biasType.hasStaticShape() || !resultType.hasStaticShape())
return failure();
if (resultType.getRank() != 4)
return failure();
if (biasType.getElementType() != resultType.getElementType())
return failure();
const int64_t channels = resultType.getDimSize(1);
ArrayRef<int64_t> shape = biasType.getShape();
if (shape.empty())
return success();
if (shape.size() == 1)
return success(shape[0] == channels);
if (shape.size() == 2)
return success(shape[0] == 1 && shape[1] == channels);
if (shape.size() == 4)
return success(shape[0] == 1 && shape[1] == channels && shape[2] == 1 && shape[3] == 1);
return failure();
}
FailureOr<SmallVector<Attribute>> getBiasChannelValues(DenseElementsAttr denseAttr, RankedTensorType resultType) {
auto biasType = dyn_cast<RankedTensorType>(denseAttr.getType());
if (!biasType || failed(isSupportedBiasAddShape(biasType, resultType)))
return failure();
const int64_t channels = resultType.getDimSize(1);
if (denseAttr.isSplat()) {
return SmallVector<Attribute>(channels, denseAttr.getSplatValue<Attribute>());
}
SmallVector<Attribute> flattened(denseAttr.getValues<Attribute>());
if (biasType.getRank() == 1)
return flattened;
if (biasType.getRank() == 2)
return flattened;
SmallVector<Attribute> channelValues;
channelValues.reserve(channels);
const int64_t channelStride = biasType.getDimSize(2) * biasType.getDimSize(3);
for (int64_t channel = 0; channel < channels; ++channel)
channelValues.push_back(flattened[channel * channelStride]);
return channelValues;
}
bool isSupportedBiasAddValue(Value bias, RankedTensorType resultType, DenseElementsAttr* denseAttr) {
auto attr = getHostConstDenseElementsAttr(bias);
if (!attr)
return false;
auto biasType = dyn_cast<RankedTensorType>(attr.getType());
if (!biasType || failed(isSupportedBiasAddShape(biasType, resultType)))
return false;
if (failed(getBiasChannelValues(attr, resultType)))
return false;
if (denseAttr)
*denseAttr = attr;
return true;
}
FailureOr<BiasAddPlanCandidate> classifyBiasAddPlanCandidate(Value lhs, Value rhs, RankedTensorType resultType) {
auto lhsType = dyn_cast<RankedTensorType>(lhs.getType());
auto rhsType = dyn_cast<RankedTensorType>(rhs.getType());
if (!lhsType || !rhsType)
return failure();
if (lhsType == resultType && isSupportedBiasAddValue(rhs, resultType))
return BiasAddPlanCandidate {lhs, rhs};
if (rhsType == resultType && isSupportedBiasAddValue(lhs, resultType))
return BiasAddPlanCandidate {rhs, lhs};
return failure();
}
FailureOr<Value>
materializeDenseBiasAddTensor(Value bias, RankedTensorType resultType, RewriterBase& rewriter, Location loc) {
DenseElementsAttr denseAttr;
if (!isSupportedBiasAddValue(bias, resultType, &denseAttr))
return failure();
FailureOr<SmallVector<Attribute>> channelValues = getBiasChannelValues(denseAttr, resultType);
if (failed(channelValues))
return failure();
SmallVector<Attribute> resultValues;
resultValues.reserve(resultType.getNumElements());
const int64_t batches = resultType.getDimSize(0);
const int64_t channels = resultType.getDimSize(1);
const int64_t height = resultType.getDimSize(2);
const int64_t width = resultType.getDimSize(3);
for (int64_t n = 0; n < batches; ++n)
for (int64_t c = 0; c < channels; ++c)
for (int64_t h = 0; h < height; ++h)
for (int64_t w = 0; w < width; ++w)
resultValues.push_back((*channelValues)[c]);
auto resultAttr = DenseElementsAttr::get(resultType, resultValues);
return getOrCreateConstant(rewriter, rewriter.getInsertionBlock()->getParentOp(), resultAttr, resultType);
}
} // namespace onnx_mlir
@@ -0,0 +1,30 @@
#pragma once
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/Value.h"
#include "mlir/Support/LogicalResult.h"
namespace onnx_mlir {
struct BiasAddPlanCandidate {
mlir::Value data;
mlir::Value bias;
};
mlir::LogicalResult isSupportedBiasAddShape(mlir::RankedTensorType biasType, mlir::RankedTensorType resultType);
bool isSupportedBiasAddValue(mlir::Value bias,
mlir::RankedTensorType resultType,
mlir::DenseElementsAttr* denseAttr = nullptr);
mlir::FailureOr<llvm::SmallVector<mlir::Attribute>>
getBiasChannelValues(mlir::DenseElementsAttr denseAttr, mlir::RankedTensorType resultType);
mlir::FailureOr<BiasAddPlanCandidate> classifyBiasAddPlanCandidate(mlir::Value lhs,
mlir::Value rhs,
mlir::RankedTensorType resultType);
mlir::FailureOr<mlir::Value> materializeDenseBiasAddTensor(mlir::Value bias,
mlir::RankedTensorType resultType,
mlir::RewriterBase& rewriter,
mlir::Location loc);
} // namespace onnx_mlir
@@ -60,6 +60,56 @@ struct SpatComputeBatchBodyArgs {
mlir::ValueRange outputs; mlir::ValueRange outputs;
}; };
inline mlir::SmallVector<mlir::Type> getGraphComputeBlockArgTypes(mlir::ValueRange weights, mlir::ValueRange inputs) {
mlir::SmallVector<mlir::Type> blockArgTypes;
blockArgTypes.reserve(weights.size() + inputs.size());
for (mlir::Value weight : weights)
blockArgTypes.push_back(weight.getType());
for (mlir::Value input : inputs)
blockArgTypes.push_back(input.getType());
return blockArgTypes;
}
inline mlir::SmallVector<mlir::Location> getGraphComputeBlockArgLocs(mlir::Location defaultLoc,
mlir::ValueRange weights,
mlir::ValueRange inputs) {
mlir::SmallVector<mlir::Location> blockArgLocs;
blockArgLocs.reserve(weights.size() + inputs.size());
for (mlir::Value weight : weights)
blockArgLocs.push_back(weight.getLoc());
for (mlir::Value input : inputs)
blockArgLocs.push_back(input.getLoc());
return blockArgLocs;
}
inline mlir::SmallVector<mlir::Type> getGraphComputeBatchBlockArgTypes(mlir::OpBuilder& builder,
mlir::TypeRange resultTypes,
mlir::ValueRange weights,
mlir::ValueRange inputs) {
mlir::SmallVector<mlir::Type> blockArgTypes {builder.getIndexType()};
blockArgTypes.reserve(1 + weights.size() + inputs.size() + resultTypes.size());
for (mlir::Value weight : weights)
blockArgTypes.push_back(weight.getType());
for (mlir::Value input : inputs)
blockArgTypes.push_back(input.getType());
llvm::append_range(blockArgTypes, resultTypes);
return blockArgTypes;
}
inline mlir::SmallVector<mlir::Location> getGraphComputeBatchBlockArgLocs(mlir::Location defaultLoc,
mlir::TypeRange resultTypes,
mlir::ValueRange weights,
mlir::ValueRange inputs) {
mlir::SmallVector<mlir::Location> blockArgLocs {defaultLoc};
blockArgLocs.reserve(1 + weights.size() + inputs.size() + resultTypes.size());
for (mlir::Value weight : weights)
blockArgLocs.push_back(weight.getLoc());
for (mlir::Value input : inputs)
blockArgLocs.push_back(input.getLoc());
blockArgLocs.append(resultTypes.size(), defaultLoc);
return blockArgLocs;
}
} // namespace detail } // namespace detail
template <typename RewriterT> template <typename RewriterT>
@@ -87,6 +137,31 @@ inline mlir::Value createSpatConcat(RewriterT& rewriter, mlir::Location loc, int
return spatial::SpatConcatOp::create(rewriter, loc, outputType, rewriter.getI64IntegerAttr(axis), inputs).getOutput(); return spatial::SpatConcatOp::create(rewriter, loc, outputType, rewriter.getI64IntegerAttr(axis), inputs).getOutput();
} }
template <typename RewriterT>
spatial::SpatGraphCompute createEmptySpatGraphCompute(RewriterT& rewriter,
mlir::Location loc,
mlir::TypeRange resultTypes,
mlir::ValueRange weights,
mlir::ValueRange inputs,
mlir::TypeRange blockArgTypes,
llvm::ArrayRef<mlir::Location> blockArgLocs) {
auto computeOp = spatial::SpatGraphCompute::create(rewriter, loc, resultTypes, weights, inputs);
rewriter.createBlock(&computeOp.getBody(), computeOp.getBody().end(), blockArgTypes, blockArgLocs);
rewriter.setInsertionPointToStart(&computeOp.getBody().front());
return computeOp;
}
template <typename RewriterT>
spatial::SpatGraphCompute createEmptySpatGraphCompute(RewriterT& rewriter,
mlir::Location loc,
mlir::TypeRange resultTypes,
mlir::ValueRange weights,
mlir::ValueRange inputs) {
auto blockArgTypes = detail::getGraphComputeBlockArgTypes(weights, inputs);
auto blockArgLocs = detail::getGraphComputeBlockArgLocs(loc, weights, inputs);
return createEmptySpatGraphCompute(rewriter, loc, resultTypes, weights, inputs, blockArgTypes, blockArgLocs);
}
/// Builds a `spat.graph_compute` with a fixed number of SSA inputs and erases it if /// Builds a `spat.graph_compute` with a fixed number of SSA inputs and erases it if
/// the body callback reports failure. /// the body callback reports failure.
template <size_t NumInputs, typename RewriterT, typename BodyFn> template <size_t NumInputs, typename RewriterT, typename BodyFn>
@@ -97,16 +172,8 @@ auto createSpatGraphCompute(RewriterT& rewriter,
mlir::ValueRange inputs, mlir::ValueRange inputs,
BodyFn&& body) { BodyFn&& body) {
assert(inputs.size() == NumInputs && "NumInputs must match the number of input values"); assert(inputs.size() == NumInputs && "NumInputs must match the number of input values");
auto computeOp = spatial::SpatGraphCompute::create(rewriter, loc, resultTypes, weights, inputs); auto computeOp = createEmptySpatGraphCompute(rewriter, loc, resultTypes, weights, inputs);
auto* block = &computeOp.getBody().front();
auto* block = new mlir::Block();
for (mlir::Value weight : weights)
block->addArgument(weight.getType(), loc);
for (mlir::Value input : inputs)
block->addArgument(input.getType(), loc);
computeOp.getBody().push_back(block);
rewriter.setInsertionPointToStart(block);
using BodyResult = detail::InvokeWithBlockArgsResultT<std::decay_t<BodyFn>, std::make_index_sequence<NumInputs>>; using BodyResult = detail::InvokeWithBlockArgsResultT<std::decay_t<BodyFn>, std::make_index_sequence<NumInputs>>;
if constexpr (std::is_same_v<BodyResult, void>) { if constexpr (std::is_same_v<BodyResult, void>) {
@@ -140,16 +207,8 @@ auto createSpatGraphCompute(RewriterT& rewriter,
mlir::ValueRange weights, mlir::ValueRange weights,
mlir::ValueRange inputs, mlir::ValueRange inputs,
BodyFn&& body) { BodyFn&& body) {
auto computeOp = spatial::SpatGraphCompute::create(rewriter, loc, resultTypes, weights, inputs); auto computeOp = createEmptySpatGraphCompute(rewriter, loc, resultTypes, weights, inputs);
auto* block = &computeOp.getBody().front();
auto* block = new mlir::Block();
for (mlir::Value weight : weights)
block->addArgument(weight.getType(), loc);
for (mlir::Value input : inputs)
block->addArgument(input.getType(), loc);
computeOp.getBody().push_back(block);
rewriter.setInsertionPointToStart(block);
using BodyResult = detail::InvokeWithValueRangeResultT<std::decay_t<BodyFn>>; using BodyResult = detail::InvokeWithValueRangeResultT<std::decay_t<BodyFn>>;
if constexpr (std::is_same_v<BodyResult, void>) { if constexpr (std::is_same_v<BodyResult, void>) {
@@ -170,14 +229,15 @@ auto createSpatGraphCompute(RewriterT& rewriter,
} }
} }
template <typename RewriterT, typename BodyFn> template <typename RewriterT>
auto createSpatGraphComputeBatch(RewriterT& rewriter, auto createEmptySpatGraphComputeBatch(RewriterT& rewriter,
mlir::Location loc, mlir::Location loc,
mlir::TypeRange resultTypes, mlir::TypeRange resultTypes,
int64_t laneCount, int64_t laneCount,
mlir::ValueRange weights, mlir::ValueRange weights,
mlir::ValueRange inputs, mlir::ValueRange inputs,
BodyFn&& body) { mlir::TypeRange blockArgTypes,
llvm::ArrayRef<mlir::Location> blockArgLocs) {
if (laneCount <= 0 || laneCount > std::numeric_limits<int32_t>::max()) if (laneCount <= 0 || laneCount > std::numeric_limits<int32_t>::max())
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure()); return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure());
@@ -186,27 +246,36 @@ auto createSpatGraphComputeBatch(RewriterT& rewriter,
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure()); return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure());
auto batchOp = spatial::SpatGraphComputeBatch::create(rewriter, loc, resultTypes, *laneCountAttr, weights, inputs); auto batchOp = spatial::SpatGraphComputeBatch::create(rewriter, loc, resultTypes, *laneCountAttr, weights, inputs);
rewriter.createBlock(&batchOp.getBody(), batchOp.getBody().end(), blockArgTypes, blockArgLocs);
rewriter.setInsertionPointToStart(&batchOp.getBody().front());
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(batchOp);
}
mlir::SmallVector<mlir::Type> blockArgTypes {rewriter.getIndexType()}; template <typename RewriterT>
mlir::SmallVector<mlir::Location> blockArgLocs {loc}; auto createEmptySpatGraphComputeBatch(RewriterT& rewriter,
blockArgTypes.reserve(1 + weights.size() + inputs.size() + resultTypes.size()); mlir::Location loc,
blockArgLocs.reserve(1 + weights.size() + inputs.size() + resultTypes.size()); mlir::TypeRange resultTypes,
for (mlir::Value weight : weights) { int64_t laneCount,
blockArgTypes.push_back(weight.getType()); mlir::ValueRange weights,
blockArgLocs.push_back(weight.getLoc()); mlir::ValueRange inputs) {
} auto blockArgTypes = detail::getGraphComputeBatchBlockArgTypes(rewriter, resultTypes, weights, inputs);
for (mlir::Value input : inputs) { auto blockArgLocs = detail::getGraphComputeBatchBlockArgLocs(loc, resultTypes, weights, inputs);
blockArgTypes.push_back(input.getType()); return createEmptySpatGraphComputeBatch(
blockArgLocs.push_back(input.getLoc()); rewriter, loc, resultTypes, laneCount, weights, inputs, blockArgTypes, blockArgLocs);
} }
for (mlir::Type resultType : resultTypes) {
blockArgTypes.push_back(resultType);
blockArgLocs.push_back(loc);
}
auto* block = template <typename RewriterT, typename BodyFn>
rewriter.createBlock(&batchOp.getBody(), batchOp.getBody().end(), mlir::TypeRange(blockArgTypes), blockArgLocs); auto createSpatGraphComputeBatch(RewriterT& rewriter,
rewriter.setInsertionPointToStart(block); mlir::Location loc,
mlir::TypeRange resultTypes,
int64_t laneCount,
mlir::ValueRange weights,
mlir::ValueRange inputs,
BodyFn&& body) {
auto batchOp = createEmptySpatGraphComputeBatch(rewriter, loc, resultTypes, laneCount, weights, inputs);
if (failed(batchOp))
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure());
auto* block = &(*batchOp).getBody().front();
detail::SpatComputeBatchBodyArgs args { detail::SpatComputeBatchBodyArgs args {
block->getArgument(0), block->getArgument(0),
@@ -217,18 +286,18 @@ auto createSpatGraphComputeBatch(RewriterT& rewriter,
using BodyResult = std::invoke_result_t<BodyFn, detail::SpatComputeBatchBodyArgs>; using BodyResult = std::invoke_result_t<BodyFn, detail::SpatComputeBatchBodyArgs>;
if constexpr (std::is_same_v<BodyResult, void>) { if constexpr (std::is_same_v<BodyResult, void>) {
std::forward<BodyFn>(body)(args); std::forward<BodyFn>(body)(args);
rewriter.setInsertionPointAfter(batchOp); rewriter.setInsertionPointAfter(*batchOp);
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(batchOp); return batchOp;
} }
else { else {
auto bodyResult = std::forward<BodyFn>(body)(args); auto bodyResult = std::forward<BodyFn>(body)(args);
if (mlir::failed(bodyResult)) { if (mlir::failed(bodyResult)) {
rewriter.setInsertionPointAfter(batchOp); rewriter.setInsertionPointAfter(*batchOp);
rewriter.eraseOp(batchOp); rewriter.eraseOp(*batchOp);
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure()); return mlir::FailureOr<spatial::SpatGraphComputeBatch>(mlir::failure());
} }
rewriter.setInsertionPointAfter(batchOp); rewriter.setInsertionPointAfter(*batchOp);
return mlir::FailureOr<spatial::SpatGraphComputeBatch>(batchOp); return batchOp;
} }
} }
@@ -277,6 +346,52 @@ inline void createParallelInsertSliceIntoBatchOutput(mlir::PatternRewriter& rewr
mlir::tensor::ParallelInsertSliceOp::create(rewriter, loc, source, dest, offsets, sizes, strides); mlir::tensor::ParallelInsertSliceOp::create(rewriter, loc, source, dest, offsets, sizes, strides);
} }
inline void publishGraphBatchPhysicalFragment(mlir::PatternRewriter& rewriter,
mlir::Location loc,
mlir::Value fragment,
mlir::Value output,
mlir::Value physicalSlot) {
auto fragmentType = mlir::cast<mlir::RankedTensorType>(fragment.getType());
mlir::SmallVector<mlir::OpFoldResult> offsets {physicalSlot};
mlir::SmallVector<mlir::OpFoldResult> sizes {rewriter.getIndexAttr(1)};
mlir::SmallVector<mlir::OpFoldResult> strides {rewriter.getIndexAttr(1)};
for (int64_t dim : fragmentType.getShape()) {
offsets.push_back(rewriter.getIndexAttr(0));
sizes.push_back(rewriter.getIndexAttr(dim));
strides.push_back(rewriter.getIndexAttr(1));
}
createParallelInsertSliceIntoBatchOutput(rewriter, loc, fragment, output, offsets, sizes, strides);
}
inline mlir::FailureOr<mlir::Value>
extractGraphBatchPhysicalFragment(mlir::PatternRewriter& rewriter,
mlir::Location loc,
mlir::Value physicalBatch,
mlir::OpFoldResult slot,
mlir::RankedTensorType fragmentType) {
if (fragmentType.getRank() == 0)
return mlir::failure();
auto physicalType = mlir::dyn_cast<mlir::RankedTensorType>(physicalBatch.getType());
if (!physicalType || physicalType.getRank() != fragmentType.getRank() + 1)
return mlir::failure();
mlir::SmallVector<int64_t> selectedShape {1};
llvm::append_range(selectedShape, fragmentType.getShape());
auto selectedType = mlir::RankedTensorType::get(selectedShape, fragmentType.getElementType(), fragmentType.getEncoding());
mlir::SmallVector<mlir::OpFoldResult> offsets {slot};
mlir::SmallVector<mlir::OpFoldResult> sizes {rewriter.getIndexAttr(1)};
mlir::SmallVector<mlir::OpFoldResult> strides {rewriter.getIndexAttr(1)};
for (int64_t dim : fragmentType.getShape()) {
offsets.push_back(rewriter.getIndexAttr(0));
sizes.push_back(rewriter.getIndexAttr(dim));
strides.push_back(rewriter.getIndexAttr(1));
}
mlir::Value selected = mlir::tensor::ExtractSliceOp::create(rewriter, loc, selectedType, physicalBatch, offsets, sizes, strides);
mlir::SmallVector<mlir::ReassociationIndices> reassociation {{0, 1}};
for (int64_t dim = 2; dim <= fragmentType.getRank(); ++dim)
reassociation.push_back({dim});
return mlir::tensor::CollapseShapeOp::create(rewriter, loc, fragmentType, selected, reassociation).getResult();
}
template <typename BodyFn> template <typename BodyFn>
mlir::Value materializeOrComputeUnary(mlir::Value input, mlir::Value materializeOrComputeUnary(mlir::Value input,
mlir::RankedTensorType resultType, mlir::RankedTensorType resultType,
@@ -0,0 +1,211 @@
#include "llvm/ADT/SmallVector.h"
#include "src/Accelerators/PIM/Common/IR/AffineUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/BiasAddUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/RowStripLayoutUtils.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp"
using namespace mlir;
namespace onnx_mlir {
RankedTensorType getRowStripFragmentType(RankedTensorType logicalType) {
return RankedTensorType::get({logicalType.getDimSize(0), logicalType.getDimSize(1), 1, logicalType.getDimSize(3)},
logicalType.getElementType(),
logicalType.getEncoding());
}
RankedTensorType getRowStripStorageType(RankedTensorType logicalType) {
return spatial::getGraphBatchPhysicalResultType(logicalType.getDimSize(2), getRowStripFragmentType(logicalType));
}
std::pair<SmallVector<int64_t>, SmallVector<int64_t>> buildRowStripMetadata(RankedTensorType type) {
SmallVector<int64_t> offsets;
SmallVector<int64_t> sizes;
const int64_t channels = type.getDimSize(1);
const int64_t height = type.getDimSize(2);
const int64_t width = type.getDimSize(3);
offsets.reserve(height * 4);
sizes.reserve(height * 4);
for (int64_t row = 0; row < height; ++row) {
offsets.append({0, 0, row, 0});
sizes.append({1, channels, 1, width});
}
return {offsets, sizes};
}
Value extractRowStripFragment(Value storage,
RankedTensorType logicalType,
OpFoldResult row,
PatternRewriter& rewriter,
Location loc) {
return *extractGraphBatchPhysicalFragment(rewriter, loc, storage, row, getRowStripFragmentType(logicalType));
}
void insertRowStripFragment(Value fragment,
Value output,
RankedTensorType logicalType,
OpFoldResult row,
PatternRewriter& rewriter,
Location loc) {
assert(fragment.getType() == getRowStripFragmentType(logicalType));
assert(output.getType() == getRowStripStorageType(logicalType));
auto slot = dyn_cast<Value>(row);
assert(slot && "row-strip graph publication requires a dynamic physical slot");
publishGraphBatchPhysicalFragment(rewriter, loc, fragment, output, slot);
}
FailureOr<Value> createPerChannelConstantFragment(DenseElementsAttr denseAttr,
RankedTensorType fragmentType,
PatternRewriter& rewriter) {
FailureOr<SmallVector<Attribute>> channelValues = getBiasChannelValues(denseAttr, fragmentType);
if (failed(channelValues))
return failure();
SmallVector<Attribute> values;
values.reserve(fragmentType.getNumElements());
for (int64_t n = 0; n < fragmentType.getDimSize(0); ++n)
for (int64_t channel = 0; channel < fragmentType.getDimSize(1); ++channel)
for (int64_t h = 0; h < fragmentType.getDimSize(2); ++h)
for (int64_t w = 0; w < fragmentType.getDimSize(3); ++w)
values.push_back((*channelValues)[channel]);
auto attr = DenseElementsAttr::get(fragmentType, values);
return getOrCreateConstant(rewriter, rewriter.getInsertionBlock()->getParentOp(), attr, fragmentType);
}
FailureOr<Value> createRowStripStorageFromRows(Value rows,
RankedTensorType logicalType,
PatternRewriter& rewriter,
Location loc) {
auto rowsType = dyn_cast<RankedTensorType>(rows.getType());
if (!rowsType || !rowsType.hasStaticShape() || rowsType.getRank() != 2)
return failure();
if (!logicalType || !logicalType.hasStaticShape() || logicalType.getRank() != 4)
return failure();
if (logicalType.getDimSize(0) != 1)
return failure();
if (rowsType.getElementType() != logicalType.getElementType())
return failure();
const int64_t channels = logicalType.getDimSize(1);
const int64_t height = logicalType.getDimSize(2);
const int64_t width = logicalType.getDimSize(3);
if (rowsType.getDimSize(0) != height * width)
return failure();
if (rowsType.getDimSize(1) != channels)
return failure();
auto rowSliceType = RankedTensorType::get({width, channels}, logicalType.getElementType(), rowsType.getEncoding());
auto channelWidthType = RankedTensorType::get({channels, width}, logicalType.getElementType(), rowsType.getEncoding());
auto fragmentType = getRowStripFragmentType(logicalType);
auto storageType = getRowStripStorageType(logicalType);
auto batchOp = createSpatComputeBatch(
rewriter, loc, TypeRange {storageType}, height, {}, ValueRange {rows}, [&](detail::SpatComputeBatchBodyArgs args) {
Operation* anchorOp = rewriter.getInsertionBlock()->getParentOp();
Value rowStart = affineMulConst(rewriter, loc, args.lane, width, anchorOp);
SmallVector<OpFoldResult> rowOffsets {rowStart, rewriter.getIndexAttr(0)};
SmallVector<OpFoldResult> rowSizes {rewriter.getIndexAttr(width), rewriter.getIndexAttr(channels)};
Value rowSlice = tensor::ExtractSliceOp::create(
rewriter, loc, rowSliceType, args.inputs.front(), rowOffsets, rowSizes, getUnitStrides(rewriter, 2));
Value channelWidth = ONNXTransposeOp::create(
rewriter, loc, channelWidthType, rowSlice, rewriter.getI64ArrayAttr({1, 0})).getResult();
Value fragment = tensor::ExpandShapeOp::create(
rewriter, loc, fragmentType, channelWidth, SmallVector<ReassociationIndices> {{0, 1}, {2, 3}});
insertRowStripFragment(fragment, args.outputs.front(), logicalType, args.lane, rewriter, loc);
return success();
});
if (failed(batchOp))
return failure();
return batchOp->getResult(0);
}
FailureOr<Value>
createRowStripAssemblyBlueprint(Value storage, RankedTensorType logicalType, PatternRewriter& rewriter, Location loc) {
auto storageType = dyn_cast<RankedTensorType>(storage.getType());
if (!storageType || storageType != getRowStripStorageType(logicalType))
return failure();
auto [offsets, sizes] = buildRowStripMetadata(logicalType);
int64_t height = logicalType.getDimSize(2);
SmallVector<int64_t> operandIndices(height, 0), sourceSlots, sourceOffsets(height, 0), strides(height * 4, 1);
for (int64_t row = 0; row < height; ++row)
sourceSlots.push_back(row);
return spatial::SpatBlueprintOp::create(rewriter, loc, logicalType, storage, ValueRange {},
rewriter.getStringAttr("nchw"), rewriter.getStringAttr("nchw_row_strip"),
rewriter.getDenseI64ArrayAttr(offsets), rewriter.getDenseI64ArrayAttr(sizes),
rewriter.getStringAttr("nchw_row_strip_fragments"), rewriter.getStringAttr("fragment_assembly"),
rewriter.getDenseI64ArrayAttr(operandIndices), rewriter.getDenseI64ArrayAttr(sourceSlots),
rewriter.getDenseI64ArrayAttr(sourceOffsets), rewriter.getDenseI64ArrayAttr(strides),
rewriter.getStringAttr("disjoint"), rewriter.getStringAttr("complete")).getOutput();
}
FailureOr<Value>
applyRowStripRelu(Value storage, RankedTensorType logicalType, PatternRewriter& rewriter, Location loc) {
auto fragmentType = getRowStripFragmentType(logicalType);
auto storageType = getRowStripStorageType(logicalType);
auto batchOp = createSpatComputeBatch(rewriter,
loc,
TypeRange {storageType},
logicalType.getDimSize(2),
{},
ValueRange {storage},
[&](detail::SpatComputeBatchBodyArgs args) {
Value fragment =
extractRowStripFragment(args.inputs.front(), logicalType, args.lane, rewriter, loc);
fragment = spatial::SpatReluOp::create(rewriter, loc, fragmentType, fragment).getResult();
insertRowStripFragment(
fragment, args.outputs.front(), logicalType, args.lane, rewriter, loc);
return success();
});
if (failed(batchOp))
return failure();
return batchOp->getResult(0);
}
FailureOr<Value>
applyRowStripBiasAdd(Value storage, RankedTensorType logicalType, Value bias, PatternRewriter& rewriter, Location loc) {
DenseElementsAttr denseAttr;
if (!isSupportedBiasAddValue(bias, logicalType, &denseAttr))
return failure();
auto fragmentType = getRowStripFragmentType(logicalType);
auto storageType = getRowStripStorageType(logicalType);
auto batchOp = createSpatComputeBatch(rewriter,
loc,
TypeRange {storageType},
logicalType.getDimSize(2),
{},
ValueRange {storage},
[&](detail::SpatComputeBatchBodyArgs args) {
Value fragment =
extractRowStripFragment(args.inputs.front(), logicalType, args.lane, rewriter, loc);
Value constant;
if (denseAttr.isSplat()) {
constant = getOrCreateConstant(
rewriter,
rewriter.getInsertionBlock()->getParentOp(),
DenseElementsAttr::get(fragmentType, denseAttr.getSplatValue<Attribute>()),
fragmentType);
}
else {
FailureOr<Value> perChannel =
createPerChannelConstantFragment(denseAttr, fragmentType, rewriter);
if (failed(perChannel))
return failure();
constant = *perChannel;
}
fragment =
spatial::SpatVAddOp::create(rewriter, loc, fragmentType, fragment, constant).getResult();
insertRowStripFragment(
fragment, args.outputs.front(), logicalType, args.lane, rewriter, loc);
return success();
});
if (failed(batchOp))
return failure();
return batchOp->getResult(0);
}
} // namespace onnx_mlir
@@ -0,0 +1,69 @@
#pragma once
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/PatternMatch.h"
namespace onnx_mlir {
inline constexpr llvm::StringLiteral kRowStripIndexMap = "nchw_row_strip_fragments";
struct RowStripPhysicalValue {
mlir::Value storage;
mlir::RankedTensorType logicalType;
llvm::SmallVector<int64_t, 16> fragmentOffsets;
llvm::SmallVector<int64_t, 16> fragmentSizes;
};
std::pair<llvm::SmallVector<int64_t>, llvm::SmallVector<int64_t>>
buildRowStripMetadata(mlir::RankedTensorType type);
mlir::RankedTensorType getRowStripFragmentType(mlir::RankedTensorType logicalType);
mlir::RankedTensorType getRowStripStorageType(mlir::RankedTensorType logicalType);
llvm::SmallVector<mlir::OpFoldResult> buildRowStripFragmentOffsets(mlir::PatternRewriter& rewriter,
mlir::OpFoldResult row);
llvm::SmallVector<mlir::OpFoldResult> buildRowStripFragmentSizes(mlir::PatternRewriter& rewriter,
mlir::RankedTensorType logicalType);
mlir::Value extractRowStripFragment(mlir::Value storage,
mlir::RankedTensorType logicalType,
mlir::OpFoldResult row,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
void insertRowStripFragment(mlir::Value fragment,
mlir::Value output,
mlir::RankedTensorType logicalType,
mlir::OpFoldResult row,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
mlir::FailureOr<mlir::Value> createPerChannelConstantFragment(mlir::DenseElementsAttr denseAttr,
mlir::RankedTensorType fragmentType,
mlir::PatternRewriter& rewriter);
mlir::FailureOr<mlir::Value> createRowStripStorageFromRows(mlir::Value rows,
mlir::RankedTensorType logicalType,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
mlir::FailureOr<mlir::Value> createRowStripAssemblyBlueprint(mlir::Value storage,
mlir::RankedTensorType logicalType,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
mlir::FailureOr<mlir::Value> applyRowStripRelu(mlir::Value storage,
mlir::RankedTensorType logicalType,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
mlir::FailureOr<mlir::Value> applyRowStripBiasAdd(mlir::Value storage,
mlir::RankedTensorType logicalType,
mlir::Value bias,
mlir::PatternRewriter& rewriter,
mlir::Location loc);
} // namespace onnx_mlir
@@ -9,10 +9,12 @@
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include <cstring>
#include <utility> #include <utility>
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp" #include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp" #include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapingUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp" #include "src/Dialect/ONNX/ONNXOps.hpp"
@@ -21,24 +23,7 @@ using namespace mlir;
namespace onnx_mlir { namespace onnx_mlir {
namespace { FailureOr<DenseElementsAttr> transposeDenseElementsAttr(DenseElementsAttr denseAttr, ArrayRef<int64_t> perms) {
static bool hasStaticUnitStrides(tensor::ExtractSliceOp extractSliceOp) {
return llvm::all_of(extractSliceOp.getStaticStrides(), [](int64_t stride) { return stride == 1; });
}
static bool hasConstantIndices(tensor::ExtractOp extractOp) {
return llvm::all_of(extractOp.getIndices(), [](Value index) { return matchConstantIndexValue(index).has_value(); });
}
static bool isStaticTensorResult(Operation* op) {
return llvm::all_of(op->getResultTypes(), [](Type type) {
auto shapedType = dyn_cast<ShapedType>(type);
return shapedType && shapedType.hasStaticShape();
});
}
static FailureOr<DenseElementsAttr> transposeDenseElements(DenseElementsAttr denseAttr, ArrayRef<int64_t> perms) {
auto tensorType = dyn_cast<RankedTensorType>(denseAttr.getType()); auto tensorType = dyn_cast<RankedTensorType>(denseAttr.getType());
if (!tensorType) if (!tensorType)
return failure(); return failure();
@@ -59,7 +44,45 @@ static FailureOr<DenseElementsAttr> transposeDenseElements(DenseElementsAttr den
auto transposedType = RankedTensorType::get(transposedShape, tensorType.getElementType(), tensorType.getEncoding()); auto transposedType = RankedTensorType::get(transposedShape, tensorType.getElementType(), tensorType.getEncoding());
if (denseAttr.isSplat()) if (denseAttr.isSplat())
return DenseElementsAttr::get(transposedType, denseAttr.getSplatValue<Attribute>()); return DenseElementsAttr::getFromRawBuffer(transposedType, denseAttr.getRawData());
const unsigned elementBitWidth = tensorType.getElementTypeBitWidth();
const ArrayRef<char> inputData = denseAttr.getRawData();
if (elementBitWidth % 8 == 0) {
const size_t elementBytes = elementBitWidth / 8;
const size_t expectedBytes = denseAttr.getNumElements() * elementBytes;
if (inputData.size() == expectedBytes) {
SmallVector<char> transposedData(expectedBytes);
if (rank == 2 && perms[0] == 1 && perms[1] == 0) {
const int64_t rows = tensorType.getDimSize(0);
const int64_t columns = tensorType.getDimSize(1);
for (int64_t row = 0; row < rows; ++row)
for (int64_t column = 0; column < columns; ++column)
std::memcpy(transposedData.data() + (column * rows + row) * elementBytes,
inputData.data() + (row * columns + column) * elementBytes,
elementBytes);
return DenseElementsAttr::getFromRawBuffer(transposedType, transposedData);
}
SmallVector<int64_t> originalStrides = computeRowMajorStrides(tensorType.getShape());
SmallVector<int64_t> transposedStrides = computeRowMajorStrides(transposedShape);
SmallVector<int64_t> originalIndices(rank);
for (int64_t linearIndex = 0; linearIndex < tensorType.getNumElements(); ++linearIndex) {
int64_t remaining = linearIndex;
for (int64_t dim = 0; dim < rank; ++dim) {
originalIndices[dim] = originalStrides.empty() ? 0 : remaining / originalStrides[dim];
remaining = originalStrides.empty() ? 0 : remaining % originalStrides[dim];
}
int64_t transposedLinearIndex = 0;
for (int64_t dim = 0; dim < rank; ++dim)
transposedLinearIndex += originalIndices[perms[dim]] * transposedStrides[dim];
std::memcpy(transposedData.data() + transposedLinearIndex * elementBytes,
inputData.data() + linearIndex * elementBytes,
elementBytes);
}
return DenseElementsAttr::getFromRawBuffer(transposedType, transposedData);
}
}
SmallVector<Attribute> originalValues(denseAttr.getValues<Attribute>()); SmallVector<Attribute> originalValues(denseAttr.getValues<Attribute>());
SmallVector<Attribute> transposedValues(originalValues.size()); SmallVector<Attribute> transposedValues(originalValues.size());
@@ -84,16 +107,30 @@ static FailureOr<DenseElementsAttr> transposeDenseElements(DenseElementsAttr den
return DenseElementsAttr::get(transposedType, transposedValues); return DenseElementsAttr::get(transposedType, transposedValues);
} }
namespace {
static bool hasStaticUnitStrides(tensor::ExtractSliceOp extractSliceOp) {
return llvm::all_of(extractSliceOp.getStaticStrides(), [](int64_t stride) { return stride == 1; });
}
static bool hasConstantIndices(tensor::ExtractOp extractOp) {
return llvm::all_of(extractOp.getIndices(), [](Value index) { return matchConstantIndexValue(index).has_value(); });
}
static bool isStaticTensorResult(Operation* op) {
return llvm::all_of(op->getResultTypes(), [](Type type) {
auto shapedType = dyn_cast<ShapedType>(type);
return shapedType && shapedType.hasStaticShape();
});
}
static FailureOr<DenseElementsAttr> reshapeDenseElements(DenseElementsAttr denseAttr, RankedTensorType resultType) { static FailureOr<DenseElementsAttr> reshapeDenseElements(DenseElementsAttr denseAttr, RankedTensorType resultType) {
auto sourceType = dyn_cast<RankedTensorType>(denseAttr.getType()); auto sourceType = dyn_cast<RankedTensorType>(denseAttr.getType());
if (!sourceType || !resultType || sourceType.getNumElements() != resultType.getNumElements()) if (!sourceType || !resultType || sourceType.getNumElements() != resultType.getNumElements()
|| sourceType.getElementType() != resultType.getElementType())
return failure(); return failure();
if (denseAttr.isSplat()) return DenseElementsAttr::getFromRawBuffer(resultType, denseAttr.getRawData());
return DenseElementsAttr::get(resultType, denseAttr.getSplatValue<Attribute>());
SmallVector<Attribute> values(denseAttr.getValues<Attribute>());
return DenseElementsAttr::get(resultType, values);
} }
static FailureOr<DenseElementsAttr> extractSliceDenseElements(DenseElementsAttr denseAttr, static FailureOr<DenseElementsAttr> extractSliceDenseElements(DenseElementsAttr denseAttr,
@@ -161,7 +198,7 @@ static DenseElementsAttr getHostConstantDenseElementsAttrImpl(Value value, llvm:
perm.reserve(transposeOp.getPermAttr().size()); perm.reserve(transposeOp.getPermAttr().size());
for (IntegerAttr attr : transposeOp.getPermAttr().getAsRange<IntegerAttr>()) for (IntegerAttr attr : transposeOp.getPermAttr().getAsRange<IntegerAttr>())
perm.push_back(attr.getInt()); perm.push_back(attr.getInt());
auto transposedAttr = transposeDenseElements(inputAttr, perm); auto transposedAttr = transposeDenseElementsAttr(inputAttr, perm);
return succeeded(transposedAttr) ? *transposedAttr : nullptr; return succeeded(transposedAttr) ? *transposedAttr : nullptr;
} }
@@ -171,7 +208,7 @@ static DenseElementsAttr getHostConstantDenseElementsAttrImpl(Value value, llvm:
return nullptr; return nullptr;
SmallVector<int64_t> perm(transposeOp.getPermutation().begin(), transposeOp.getPermutation().end()); SmallVector<int64_t> perm(transposeOp.getPermutation().begin(), transposeOp.getPermutation().end());
auto transposedAttr = transposeDenseElements(inputAttr, perm); auto transposedAttr = transposeDenseElementsAttr(inputAttr, perm);
return succeeded(transposedAttr) ? *transposedAttr : nullptr; return succeeded(transposedAttr) ? *transposedAttr : nullptr;
} }
@@ -219,6 +256,9 @@ getCompileTimeSourceImpl(Operation* op, llvm::SmallPtrSetImpl<Operation*>& visit
chainLength += 1; chainLength += 1;
if (!isShapingOnlyOp(op))
return std::nullopt;
if (auto extractOp = dyn_cast<tensor::ExtractOp>(op)) if (auto extractOp = dyn_cast<tensor::ExtractOp>(op))
return hasConstantIndices(extractOp) return hasConstantIndices(extractOp)
? getCompileTimeSourceImpl(extractOp.getTensor().getDefiningOp(), visited, chainLength) ? getCompileTimeSourceImpl(extractOp.getTensor().getDefiningOp(), visited, chainLength)
@@ -4,6 +4,8 @@
#include "mlir/IR/Operation.h" #include "mlir/IR/Operation.h"
#include "mlir/IR/Value.h" #include "mlir/IR/Value.h"
#include "llvm/ADT/ArrayRef.h"
namespace onnx_mlir { namespace onnx_mlir {
struct CompileTimeSource { struct CompileTimeSource {
@@ -19,4 +21,7 @@ bool isCompileTimeOp(mlir::Operation* op);
mlir::DenseElementsAttr getHostConstDenseElementsAttr(mlir::Value value); mlir::DenseElementsAttr getHostConstDenseElementsAttr(mlir::Value value);
mlir::FailureOr<mlir::DenseElementsAttr> transposeDenseElementsAttr(
mlir::DenseElementsAttr denseAttr, llvm::ArrayRef<int64_t> permutation);
} // namespace onnx_mlir } // namespace onnx_mlir
@@ -11,12 +11,16 @@
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp" #include "Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp"
#include "mlir/Transforms/Passes.h"
#include "src/Accelerators/PIM/Common/PimCommon.hpp" #include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/Support/DebugDump.hpp" #include "src/Accelerators/PIM/Common/Support/DebugDump.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/BiasAddUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/RowStripLayoutUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/PlanLowering.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/PlanLowering.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/Transforms/MergeComputeNodes/SpatialDataflowCsvExporter.hpp"
#include "src/Accelerators/PIM/Pass/PIMPasses.h" #include "src/Accelerators/PIM/Pass/PIMPasses.h"
#include "src/Dialect/ONNX/ONNXOps.hpp" #include "src/Dialect/ONNX/ONNXOps.hpp"
@@ -28,14 +32,6 @@ namespace {
static constexpr StringLiteral kDenseLayout = "dense_nchw"; static constexpr StringLiteral kDenseLayout = "dense_nchw";
static constexpr StringLiteral kRowStripLayout = "nchw_row_strip"; static constexpr StringLiteral kRowStripLayout = "nchw_row_strip";
struct RowStripPhysicalValue {
Value physicalValue;
RankedTensorType logicalType;
SmallVector<int64_t, 16> fragmentOffsets;
SmallVector<int64_t, 16> fragmentSizes;
std::string indexMap;
};
static FailureOr<RowStripPhysicalValue> getRowStripValue(llvm::DenseMap<Value, RowStripPhysicalValue>& rowStripValues, static FailureOr<RowStripPhysicalValue> getRowStripValue(llvm::DenseMap<Value, RowStripPhysicalValue>& rowStripValues,
Value value) { Value value) {
auto it = rowStripValues.find(value); auto it = rowStripValues.find(value);
@@ -45,112 +41,42 @@ static FailureOr<RowStripPhysicalValue> getRowStripValue(llvm::DenseMap<Value, R
} }
static FailureOr<RowStripPhysicalValue> buildRowStripValue(spatial::SpatBlueprintOp blueprint, static FailureOr<RowStripPhysicalValue> buildRowStripValue(spatial::SpatBlueprintOp blueprint,
Value physicalValue) { Value storage) {
auto logicalType = dyn_cast<RankedTensorType>(blueprint.getOutput().getType()); auto logicalType = dyn_cast<RankedTensorType>(blueprint.getOutput().getType());
if (!logicalType) if (!logicalType)
return blueprint.emitOpError("requires ranked logical output type"), failure(); return blueprint.emitOpError("requires ranked logical output type"), failure();
RowStripPhysicalValue value; RowStripPhysicalValue value;
value.physicalValue = physicalValue; value.storage = storage;
value.logicalType = logicalType; value.logicalType = logicalType;
value.fragmentOffsets.append(blueprint.getFragmentOffsets().begin(), blueprint.getFragmentOffsets().end()); value.fragmentOffsets.append(blueprint.getFragmentOffsets().begin(), blueprint.getFragmentOffsets().end());
value.fragmentSizes.append(blueprint.getFragmentSizes().begin(), blueprint.getFragmentSizes().end()); value.fragmentSizes.append(blueprint.getFragmentSizes().begin(), blueprint.getFragmentSizes().end());
value.indexMap = blueprint.getIndexMap().str(); if (blueprint.getIndexMap() != kRowStripIndexMap)
return blueprint.emitOpError("requires the canonical row-strip index map"), failure();
auto storageType = dyn_cast<RankedTensorType>(storage.getType());
if (!storageType || storageType != getRowStripStorageType(logicalType))
return blueprint.emitOpError("requires physical row-strip fragment storage"), failure();
return value; return value;
} }
static FailureOr<Value> static FailureOr<Value>
lowerRowStripRelu(const RowStripPhysicalValue& input, spatial::SpatReluPlanOp planOp, PatternRewriter& rewriter) { lowerRowStripRelu(const RowStripPhysicalValue& input, spatial::SpatReluPlanOp planOp, PatternRewriter& rewriter) {
auto packedType = cast<RankedTensorType>(input.physicalValue.getType()); return applyRowStripRelu(input.storage, input.logicalType, rewriter, planOp.getLoc());
auto computeOp = }
createSpatCompute<1>(rewriter, planOp.getLoc(), TypeRange {packedType}, {}, input.physicalValue, [&](Value x) {
auto relu = spatial::SpatReluOp::create(rewriter, planOp.getLoc(), packedType, x); static FailureOr<Value> lowerRowStripBiasAdd(const RowStripPhysicalValue& input,
spatial::SpatYieldOp::create(rewriter, planOp.getLoc(), relu.getResult()); spatial::SpatBiasAddPlanOp planOp,
}); PatternRewriter& rewriter) {
return computeOp.getResult(0); return applyRowStripBiasAdd(input.storage, input.logicalType, planOp.getBias(), rewriter, planOp.getLoc());
} }
static FailureOr<Value> static FailureOr<Value>
materializeRowStripToDense(const RowStripPhysicalValue& rowStripValue, Location loc, PatternRewriter& rewriter) { materializeRowStripToDense(const RowStripPhysicalValue& rowStripValue, Location loc, PatternRewriter& rewriter) {
auto packedType = dyn_cast<RankedTensorType>(rowStripValue.physicalValue.getType());
if (!packedType || packedType.getRank() != 3 || !packedType.hasStaticShape())
return failure();
if (rowStripValue.logicalType.getRank() != 4 || !rowStripValue.logicalType.hasStaticShape()) if (rowStripValue.logicalType.getRank() != 4 || !rowStripValue.logicalType.hasStaticShape())
return failure(); return failure();
if (rowStripValue.indexMap != "packed_hwc_rows_to_nchw") auto [expectedOffsets, expectedSizes] = buildRowStripMetadata(rowStripValue.logicalType);
if (!llvm::equal(rowStripValue.fragmentOffsets, expectedOffsets) || !llvm::equal(rowStripValue.fragmentSizes, expectedSizes))
return failure(); return failure();
return createRowStripAssemblyBlueprint(rowStripValue.storage, rowStripValue.logicalType, rewriter, loc);
const int64_t rank = rowStripValue.logicalType.getRank();
const int64_t fragmentCount = rowStripValue.fragmentOffsets.size() / rank;
const int64_t packedWidth = packedType.getDimSize(1);
const int64_t packedChannels = packedType.getDimSize(2);
if (fragmentCount != packedType.getDimSize(0))
return failure();
for (int64_t fragmentIndex = 0; fragmentIndex < fragmentCount; ++fragmentIndex) {
if (rowStripValue.fragmentOffsets[fragmentIndex * rank + 0] != 0
|| rowStripValue.fragmentOffsets[fragmentIndex * rank + 1] != 0
|| rowStripValue.fragmentOffsets[fragmentIndex * rank + 2] != fragmentIndex
|| rowStripValue.fragmentOffsets[fragmentIndex * rank + 3] != 0)
return failure();
if (rowStripValue.fragmentSizes[fragmentIndex * rank + 0] != 1
|| rowStripValue.fragmentSizes[fragmentIndex * rank + 1] != packedChannels
|| rowStripValue.fragmentSizes[fragmentIndex * rank + 2] != 1
|| rowStripValue.fragmentSizes[fragmentIndex * rank + 3] != packedWidth)
return failure();
}
auto packedSliceType =
RankedTensorType::get({1, packedWidth, packedChannels}, packedType.getElementType(), packedType.getEncoding());
auto expandedType =
RankedTensorType::get({1, 1, packedWidth, packedChannels}, packedType.getElementType(), packedType.getEncoding());
auto logicalFragmentType =
RankedTensorType::get({1, packedChannels, 1, packedWidth}, packedType.getElementType(), packedType.getEncoding());
auto batchOp = createSpatComputeBatch(
rewriter,
loc,
TypeRange {rowStripValue.logicalType},
fragmentCount,
{},
ValueRange {rowStripValue.physicalValue},
[&](detail::SpatComputeBatchBodyArgs args) {
SmallVector<OpFoldResult> packedOffsets {args.lane, rewriter.getIndexAttr(0), rewriter.getIndexAttr(0)};
SmallVector<OpFoldResult> packedSizes {
rewriter.getIndexAttr(1), rewriter.getIndexAttr(packedWidth), rewriter.getIndexAttr(packedChannels)};
Value packedSlice = tensor::ExtractSliceOp::create(
rewriter, loc, packedSliceType, args.inputs.front(), packedOffsets, packedSizes, getUnitStrides(rewriter, 3));
Value expanded = tensor::ExpandShapeOp::create(rewriter,
loc,
expandedType,
packedSlice,
SmallVector<ReassociationIndices> {
{0, 1},
{2},
{3}
});
Value transposeInit =
tensor::EmptyOp::create(rewriter, loc, logicalFragmentType.getShape(), logicalFragmentType.getElementType());
Value logicalFragment =
linalg::TransposeOp::create(rewriter, loc, expanded, transposeInit, SmallVector<int64_t> {0, 3, 1, 2})
.getResult()[0];
SmallVector<OpFoldResult> logicalOffsets {
rewriter.getIndexAttr(0), rewriter.getIndexAttr(0), args.lane, rewriter.getIndexAttr(0)};
SmallVector<OpFoldResult> logicalSizes {rewriter.getIndexAttr(1),
rewriter.getIndexAttr(packedChannels),
rewriter.getIndexAttr(1),
rewriter.getIndexAttr(packedWidth)};
createParallelInsertSliceIntoBatchOutput(rewriter,
loc,
logicalFragment,
args.outputs.front(),
logicalOffsets,
logicalSizes,
getUnitStrides(rewriter, 4));
return success();
});
if (failed(batchOp))
return failure();
return batchOp->getResult(0);
} }
struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, OperationPass<ModuleOp>> { struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, OperationPass<ModuleOp>> {
@@ -193,7 +119,7 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
rewriter.setInsertionPoint(planOp); rewriter.setInsertionPoint(planOp);
FailureOr<Value> lowered = lowerSelectedConv2DPlan( FailureOr<Value> lowered = lowerSelectedConv2DPlan(
planOp, planOp,
succeeded(rowStripInput) ? std::optional<Value> {rowStripInput->physicalValue} : std::nullopt, succeeded(rowStripInput) ? std::optional<Value> {rowStripInput->storage} : std::nullopt,
/*emitRowStripLayout=*/true, /*emitRowStripLayout=*/true,
rewriter); rewriter);
if (failed(lowered)) { if (failed(lowered)) {
@@ -265,6 +191,64 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
rewriter.replaceOp(planOp, computeOp.getResults()); rewriter.replaceOp(planOp, computeOp.getResults());
continue; continue;
} }
if (auto planOp = dyn_cast<spatial::SpatBiasAddPlanOp>(&op)) {
if (succeeded(getRowStripValue(rowStripValues, planOp.getInput()))) {
auto outputBlueprint = llvm::find_if(planOp.getResult().getUsers(), [](Operation* user) {
auto blueprint = dyn_cast<spatial::SpatBlueprintOp>(user);
return blueprint && blueprint.getPhysicalLayout() == kRowStripLayout;
});
if (outputBlueprint == planOp.getResult().getUsers().end()) {
planOp.emitOpError("row-strip bias_add plan requires a row-strip blueprint result");
signalPassFailure();
return;
}
FailureOr<RowStripPhysicalValue> input = getRowStripValue(rowStripValues, planOp.getInput());
rewriter.setInsertionPoint(planOp);
FailureOr<Value> lowered = lowerRowStripBiasAdd(*input, planOp, rewriter);
if (failed(lowered)) {
planOp.emitOpError("failed to lower selected row-strip Spatial bias_add plan");
signalPassFailure();
return;
}
auto blueprint = cast<spatial::SpatBlueprintOp>(*outputBlueprint);
FailureOr<RowStripPhysicalValue> output = buildRowStripValue(blueprint, *lowered);
if (failed(output)) {
signalPassFailure();
return;
}
rowStripValues[blueprint.getResult()] = *output;
eraseAfterLowering.insert(planOp);
eraseAfterLowering.insert(blueprint);
continue;
}
auto resultType = dyn_cast<RankedTensorType>(planOp.getOutput().getType());
if (!resultType) {
planOp.emitOpError("requires ranked output type");
signalPassFailure();
return;
}
rewriter.setInsertionPoint(planOp);
FailureOr<Value> denseBias = materializeDenseBiasAddTensor(planOp.getBias(), resultType, rewriter, planOp.getLoc());
if (failed(denseBias)) {
planOp.emitOpError("failed to materialize dense Conv-style bias");
signalPassFailure();
return;
}
auto computeOp = createSpatCompute<2>(rewriter,
planOp.getLoc(),
planOp.getOutput().getType(),
{},
ValueRange {planOp.getInput(), *denseBias},
[&](Value x, Value y) {
auto added = spatial::SpatVAddOp::create(
rewriter, planOp.getLoc(), planOp.getOutput().getType(), x, y);
spatial::SpatYieldOp::create(rewriter, planOp.getLoc(), added.getResult());
});
rewriter.replaceOp(planOp, computeOp.getResults());
continue;
}
if (auto materializeOp = dyn_cast<spatial::SpatMaterializeLayoutOp>(&op)) { if (auto materializeOp = dyn_cast<spatial::SpatMaterializeLayoutOp>(&op)) {
if (materializeOp.getSourcePhysicalLayout() == kDenseLayout if (materializeOp.getSourcePhysicalLayout() == kDenseLayout
&& materializeOp.getTargetPhysicalLayout() == kDenseLayout) { && materializeOp.getTargetPhysicalLayout() == kDenseLayout) {
@@ -294,6 +278,8 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
continue; continue;
} }
if (auto blueprintOp = dyn_cast<spatial::SpatBlueprintOp>(&op)) { if (auto blueprintOp = dyn_cast<spatial::SpatBlueprintOp>(&op)) {
if (std::optional<StringRef> mode = blueprintOp.getMode(); mode && *mode == "fragment_assembly")
continue;
if (blueprintOp.getPhysicalLayout() == kDenseLayout) { if (blueprintOp.getPhysicalLayout() == kDenseLayout) {
rewriter.replaceOp(blueprintOp, blueprintOp.getInput()); rewriter.replaceOp(blueprintOp, blueprintOp.getInput());
continue; continue;
@@ -345,17 +331,25 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
RewritePatternSet helperPatterns(ctx); RewritePatternSet helperPatterns(ctx);
populateGemmPatterns(helperPatterns, ctx); populateGemmPatterns(helperPatterns, ctx);
populateTransposePatterns(helperPatterns, ctx); populateTransposePatterns(helperPatterns, ctx);
if (failed(applyPartialConversion(moduleOp, helperTarget, std::move(helperPatterns)))) { FrozenRewritePatternSet frozenHelperPatterns(
moduleOp.emitError("failed to lower helper ONNX ops emitted by selected Spatial plan lowering"); std::move(helperPatterns));
signalPassFailure(); SmallVector<Operation*> topLevelHelperOps;
return; funcOp.walk([&](Operation* op) {
if (isa<spatial::SpatGraphCompute,
spatial::SpatGraphComputeBatch>(op))
return WalkResult::skip();
if (isa<ONNXGemmOp, ONNXTransposeOp>(op))
topLevelHelperOps.push_back(op);
return WalkResult::advance();
});
for (Operation *helper : topLevelHelperOps) {
if (failed(applyPartialConversion(
helper, helperTarget, frozenHelperPatterns))) {
moduleOp.emitError("failed to lower helper ONNX ops emitted by selected Spatial plan lowering");
signalPassFailure();
return;
}
} }
FrozenRewritePatternSet nestedHelperPatterns([&] {
RewritePatternSet patterns(ctx);
populateGemmPatterns(patterns, ctx);
populateTransposePatterns(patterns, ctx);
return patterns;
}());
ConversionTarget nestedHelperTarget(*ctx); ConversionTarget nestedHelperTarget(*ctx);
nestedHelperTarget.addLegalDialect<spatial::SpatialDialect, nestedHelperTarget.addLegalDialect<spatial::SpatialDialect,
tensor::TensorDialect, tensor::TensorDialect,
@@ -371,7 +365,8 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
computeLikeOps.push_back(op); computeLikeOps.push_back(op);
}); });
for (Operation* op : computeLikeOps) { for (Operation* op : computeLikeOps) {
if (failed(applyFullConversion(op, nestedHelperTarget, nestedHelperPatterns))) { if (failed(applyFullConversion(
op, nestedHelperTarget, frozenHelperPatterns))) {
op->emitOpError("failed to lower nested helper ONNX ops emitted by selected Spatial plan lowering"); op->emitOpError("failed to lower nested helper ONNX ops emitted by selected Spatial plan lowering");
signalPassFailure(); signalPassFailure();
return; return;
@@ -383,19 +378,37 @@ struct LowerSpatialPlansPass final : PassWrapper<LowerSpatialPlansPass, Operatio
moduleOp.walk([&](Operation* op) { moduleOp.walk([&](Operation* op) {
if (isa<ONNXEntryPointOp>(op)) if (isa<ONNXEntryPointOp>(op))
return; return;
if (isa<spatial::SpatConv2DPlanOp, if (auto blueprint = dyn_cast<spatial::SpatBlueprintOp>(op)) {
spatial::SpatReluPlanOp, if (std::optional<StringRef> mode = blueprint.getMode(); mode && *mode == "fragment_assembly")
spatial::SpatBlueprintOp, return;
spatial::SpatMaterializeLayoutOp>(op) op->emitOpError("planning blueprint must not remain after LowerSpatialPlans");
hasIllegalOps = true;
} else if (isa<spatial::SpatConv2DPlanOp,
spatial::SpatBiasAddPlanOp,
spatial::SpatReluPlanOp,
spatial::SpatMaterializeLayoutOp>(op)
|| op->getDialect()->getNamespace() == "onnx") { || op->getDialect()->getNamespace() == "onnx") {
op->emitOpError("operation must not remain after LowerSpatialPlans"); op->emitOpError("operation must not remain after LowerSpatialPlans");
hasIllegalOps = true; hasIllegalOps = true;
} }
}); });
if (hasIllegalOps)
PassManager canonicalizationPM(ctx);
canonicalizationPM.addPass(createCanonicalizerPass());
if (failed(canonicalizationPM.run(moduleOp)))
moduleOp.emitWarning("failed to run LowerSpatialPlansPass canonicalization; continuing");
if (hasIllegalOps) {
signalPassFailure(); signalPassFailure();
else } else {
dumpModule(moduleOp, "spatial1_premerge"); dumpModule(moduleOp, "spatial1_graph");
spatial::SpatialDataflowExportStage exportMode = spatial::getSpatialDataflowExportStage();
if (spatial::shouldExportSpatialDataflowStage(exportMode, spatial::SpatialDataflowExportStage::Spatial1)
&& failed(spatial::exportSpatialDataflowCsvGraph(funcOp, "spatial1_graph"))) {
signalPassFailure();
return;
}
}
if (!verifyLogicalPhase("at the end of LowerSpatialPlans")) if (!verifyLogicalPhase("at the end of LowerSpatialPlans"))
return; return;
@@ -13,6 +13,7 @@
#include "Common/Common.hpp" #include "Common/Common.hpp"
#include "Common/PimCommon.hpp" #include "Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/ComputeRegionBuilder.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
@@ -45,11 +46,12 @@ static void populateEmptyFunction(func::FuncOp funcOp) {
SmallVector<spatial::SpatGraphCompute> computes(funcOp.getOps<spatial::SpatGraphCompute>()); SmallVector<spatial::SpatGraphCompute> computes(funcOp.getOps<spatial::SpatGraphCompute>());
SmallVector<spatial::SpatGraphComputeBatch> computeBatches(funcOp.getOps<spatial::SpatGraphComputeBatch>()); SmallVector<spatial::SpatGraphComputeBatch> computeBatches(funcOp.getOps<spatial::SpatGraphComputeBatch>());
SmallVector<spatial::SpatConv2DPlanOp> convPlans(funcOp.getOps<spatial::SpatConv2DPlanOp>()); SmallVector<spatial::SpatConv2DPlanOp> convPlans(funcOp.getOps<spatial::SpatConv2DPlanOp>());
SmallVector<spatial::SpatBiasAddPlanOp> biasAddPlans(funcOp.getOps<spatial::SpatBiasAddPlanOp>());
SmallVector<spatial::SpatReluPlanOp> reluPlans(funcOp.getOps<spatial::SpatReluPlanOp>()); SmallVector<spatial::SpatReluPlanOp> reluPlans(funcOp.getOps<spatial::SpatReluPlanOp>());
SmallVector<spatial::SpatBlueprintOp> blueprints(funcOp.getOps<spatial::SpatBlueprintOp>()); SmallVector<spatial::SpatBlueprintOp> blueprints(funcOp.getOps<spatial::SpatBlueprintOp>());
SmallVector<spatial::SpatMaterializeLayoutOp> materializers(funcOp.getOps<spatial::SpatMaterializeLayoutOp>()); SmallVector<spatial::SpatMaterializeLayoutOp> materializers(funcOp.getOps<spatial::SpatMaterializeLayoutOp>());
if (!computes.empty() || !computeBatches.empty() || !convPlans.empty() || !reluPlans.empty() || !blueprints.empty() if (!computes.empty() || !computeBatches.empty() || !convPlans.empty() || !biasAddPlans.empty() || !reluPlans.empty()
|| !materializers.empty()) { || !blueprints.empty() || !materializers.empty()) {
return; return;
} }
@@ -65,9 +67,9 @@ static void populateEmptyFunction(func::FuncOp funcOp) {
sourceLocs.push_back(source.getLoc()); sourceLocs.push_back(source.getLoc());
} }
auto newCompute = spatial::SpatGraphCompute::create( auto newCompute = createEmptySpatGraphCompute(
rewriter, returnOp.getLoc(), returnOp.getOperandTypes(), funcOp.getArguments(), {}, {}); rewriter, returnOp.getLoc(), returnOp.getOperandTypes(), {}, funcOp.getArguments(), sourceTypes, sourceLocs);
auto* newBlock = rewriter.createBlock(&newCompute.getBody(), newCompute.getBody().end(), sourceTypes, sourceLocs); auto* newBlock = &newCompute.getBody().front();
for (auto [blockArg, computeArg] : llvm::zip(newBlock->getArguments(), newCompute.getOperands())) for (auto [blockArg, computeArg] : llvm::zip(newBlock->getArguments(), newCompute.getOperands()))
mapper.map(computeArg, blockArg); mapper.map(computeArg, blockArg);
newCompute.getProperties().setOperandSegmentSizes({0, static_cast<int>(sourceTypes.size())}); newCompute.getProperties().setOperandSegmentSizes({0, static_cast<int>(sourceTypes.size())});
@@ -103,7 +105,7 @@ void ONNXToSpatialPass::runOnOperation() {
affine::AffineDialect, affine::AffineDialect,
arith::ArithDialect, arith::ArithDialect,
scf::SCFDialect>(); scf::SCFDialect>();
preTarget.addIllegalOp<ONNXConstantOp, ONNXFlattenOp>(); preTarget.addIllegalOp<ONNXConstantOp>();
RewritePatternSet prePatterns(ctx); RewritePatternSet prePatterns(ctx);
populatePrePatterns(prePatterns, ctx); populatePrePatterns(prePatterns, ctx);
@@ -142,6 +144,7 @@ void ONNXToSpatialPass::runOnOperation() {
target.addIllegalOp<ONNXSigmoidOp>(); target.addIllegalOp<ONNXSigmoidOp>();
target.addIllegalOp<ONNXSoftmaxOp>(); target.addIllegalOp<ONNXSoftmaxOp>();
target.addIllegalOp<ONNXConcatOp>(); target.addIllegalOp<ONNXConcatOp>();
target.addIllegalOp<ONNXFlattenOp>();
target.addIllegalOp<ONNXGatherOp>(); target.addIllegalOp<ONNXGatherOp>();
target.addIllegalOp<ONNXReshapeOp>(); target.addIllegalOp<ONNXReshapeOp>();
target.addIllegalOp<ONNXResizeOp>(); target.addIllegalOp<ONNXResizeOp>();
@@ -173,11 +176,6 @@ void ONNXToSpatialPass::runOnOperation() {
arith::ArithDialect, arith::ArithDialect,
scf::SCFDialect>(); scf::SCFDialect>();
PassManager cleanupPM(ctx);
cleanupPM.addPass(createCanonicalizerPass());
if (failed(cleanupPM.run(moduleOp)))
moduleOp.emitWarning("failed to run ONNX-to-Spatial canonicalization cleanup; continuing");
annotateWeightsConstants(*entryFunc); annotateWeightsConstants(*entryFunc);
if (failed(verifyLogicalSpatialGraphInvariants(*entryFunc))) { if (failed(verifyLogicalSpatialGraphInvariants(*entryFunc))) {
@@ -213,13 +211,18 @@ void ONNXToSpatialPass::runOnOperation() {
populateEmptyFunction(*entryFunc); populateEmptyFunction(*entryFunc);
if (failed(verifyLogicalSpatialGraphInvariants(*entryFunc))) { PassManager canonicalizationPM(ctx);
canonicalizationPM.addPass(createCanonicalizerPass());
if (failed(canonicalizationPM.run(moduleOp)))
moduleOp.emitWarning("failed to run ONNXToSpatial canonicalization; continuing");
dumpModule(moduleOp, "spatial0");
if (failed(verifyLogicalSpatialGraphInvariants(*entryFunc))) {
moduleOp.emitError("logical Spatial graph verification failed after ONNX-to-Spatial"); moduleOp.emitError("logical Spatial graph verification failed after ONNX-to-Spatial");
signalPassFailure(); signalPassFailure();
return; return;
} }
dumpModule(moduleOp, "spatial0");
if (failed(verifyONNXToSpatial(*entryFunc))) { if (failed(verifyONNXToSpatial(*entryFunc))) {
moduleOp.emitError("ONNX-to-Spatial host legality verification failed"); moduleOp.emitError("ONNX-to-Spatial host legality verification failed");
signalPassFailure(); signalPassFailure();
@@ -56,13 +56,18 @@ bool isLegalExternalCapture(Value value, Region& region) {
return definingOp && definingOp->hasTrait<OpTrait::ConstantLike>(); return definingOp && definingOp->hasTrait<OpTrait::ConstantLike>();
} }
bool isRecordedDeferredCommunicationSource(Operation* op, Value value) {
auto transfer = dyn_cast<spatial::SpatDeferredCommunicationOp>(op);
return transfer && llvm::is_contained(transfer.getSources(), value);
}
template <typename ComputeOpTy> template <typename ComputeOpTy>
void verifyComputeBodyCaptures(ComputeOpTy compute, StringRef kind, pim::CappedDiagnosticReporter& diagnostics) { void verifyComputeBodyCaptures(ComputeOpTy compute, StringRef kind, pim::CappedDiagnosticReporter& diagnostics) {
Region& body = compute.getBody(); Region& body = compute.getBody();
body.walk([&](Operation* nestedOp) { body.walk([&](Operation* nestedOp) {
for (OpOperand& operand : nestedOp->getOpOperands()) { for (OpOperand& operand : nestedOp->getOpOperands()) {
Value value = operand.get(); Value value = operand.get();
if (isLegalExternalCapture(value, body)) if (isLegalExternalCapture(value, body) || isRecordedDeferredCommunicationSource(nestedOp, value))
continue; continue;
Operation* definingOp = value.getDefiningOp(); Operation* definingOp = value.getDefiningOp();
@@ -90,21 +95,29 @@ bool isLegalHostBackedValue(Value value) {
return definingOp->getDialect()->getNamespace() != "spat"; return definingOp->getDialect()->getNamespace() != "spat";
} }
bool isScheduledPhase1Value(Value value) {
Operation* definingOp = value.getDefiningOp();
return isa_and_nonnull<spatial::SpatScheduledCompute, spatial::SpatScheduledComputeBatch>(definingOp);
}
template <typename ComputeOpTy> template <typename ComputeOpTy>
void verifyScheduledInputs(ComputeOpTy compute, void verifyScheduledInputs(ComputeOpTy compute,
bool allowChannelReceiveInputs, bool allowChannelReceiveInputs,
StringRef kind, StringRef kind,
pim::CappedDiagnosticReporter& diagnostics) { pim::CappedDiagnosticReporter& diagnostics) {
for (auto [inputIndex, input] : llvm::enumerate(compute.getInputs())) { for (auto [inputIndex, input] : llvm::enumerate(compute.getInputs())) {
size_t currentInputIndex = inputIndex;
Operation* definingOp = input.getDefiningOp(); Operation* definingOp = input.getDefiningOp();
if (allowChannelReceiveInputs && isa_and_nonnull<spatial::SpatChannelReceiveOp>(definingOp)) if (allowChannelReceiveInputs && isa_and_nonnull<spatial::SpatChannelReceiveOp>(definingOp))
continue; continue;
if (isScheduledPhase1Value(input))
continue;
if (isLegalHostBackedValue(input)) if (isLegalHostBackedValue(input))
continue; continue;
diagnostics.report(compute.getOperation(), [&](Operation* illegalOp) { diagnostics.report(compute.getOperation(), [&](Operation* illegalOp) {
InFlightDiagnostic diag = illegalOp->emitOpError() InFlightDiagnostic diag = illegalOp->emitOpError()
<< kPhaseMarker << " " << kind << " input #" << inputIndex << kPhaseMarker << " " << kind << " input #" << currentInputIndex
<< (allowChannelReceiveInputs ? " must come from the host or explicit spat.channel_receive" << (allowChannelReceiveInputs ? " must come from the host or explicit spat.channel_receive"
: " must come from the host"); : " must come from the host");
if (definingOp) if (definingOp)
@@ -132,6 +145,7 @@ void verifyLogicalTopLevelOps(func::FuncOp funcOp, pim::CappedDiagnosticReporter
spatial::SpatGraphCompute, spatial::SpatGraphCompute,
spatial::SpatGraphComputeBatch, spatial::SpatGraphComputeBatch,
spatial::SpatConv2DPlanOp, spatial::SpatConv2DPlanOp,
spatial::SpatBiasAddPlanOp,
spatial::SpatReluPlanOp, spatial::SpatReluPlanOp,
spatial::SpatBlueprintOp, spatial::SpatBlueprintOp,
spatial::SpatMaterializeLayoutOp>(&op)) { spatial::SpatMaterializeLayoutOp>(&op)) {
@@ -162,9 +176,9 @@ void verifyLogicalTopLevelOps(func::FuncOp funcOp, pim::CappedDiagnosticReporter
void verifyScheduledTopLevelOps(func::FuncOp funcOp, pim::CappedDiagnosticReporter& diagnostics) { void verifyScheduledTopLevelOps(func::FuncOp funcOp, pim::CappedDiagnosticReporter& diagnostics) {
for (Operation& op : funcOp.getOps()) { for (Operation& op : funcOp.getOps()) {
if (isa<spatial::SpatGraphCompute, spatial::SpatGraphComputeBatch>(&op)) { if (isa<spatial::SpatChannelSendOp, spatial::SpatChannelReceiveOp>(&op)) {
diagnostics.report(&op, [&](Operation* illegalOp) { diagnostics.report(&op, [&](Operation* illegalOp) {
illegalOp->emitOpError() << kPhaseMarker << " graph Spatial compute op remained after merge materialization"; illegalOp->emitOpError() << kPhaseMarker << " real channel communication is not allowed in scheduled phase 1";
}); });
} }
} }
@@ -19,6 +19,7 @@ void populateConversionPatterns(RewritePatternSet& patterns, MLIRContext* ctx) {
populateSigmoidPatterns(patterns, ctx); populateSigmoidPatterns(patterns, ctx);
populateSoftmaxPatterns(patterns, ctx); populateSoftmaxPatterns(patterns, ctx);
populateConcatPatterns(patterns, ctx); populateConcatPatterns(patterns, ctx);
populateFlattenPatterns(patterns, ctx);
populateGatherPatterns(patterns, ctx); populateGatherPatterns(patterns, ctx);
populateResizePatterns(patterns, ctx); populateResizePatterns(patterns, ctx);
populateReshapePatterns(patterns, ctx); populateReshapePatterns(patterns, ctx);
@@ -26,6 +26,7 @@ void populateReluPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext*
void populateSigmoidPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateSigmoidPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateSoftmaxPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateSoftmaxPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateConcatPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateConcatPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateFlattenPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateGatherPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateGatherPatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateResizePatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateResizePatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
void populateReshapePatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx); void populateReshapePatterns(mlir::RewritePatternSet& patterns, mlir::MLIRContext* ctx);
File diff suppressed because it is too large Load Diff
@@ -5,7 +5,7 @@
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/BiasAddUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
@@ -47,38 +47,28 @@ static FailureOr<Value> materializeBroadcastedConstantTensor(Value value,
return failure(); return failure();
const int64_t rankOffset = static_cast<int64_t>(resultShape.size() - sourceShape.size()); const int64_t rankOffset = static_cast<int64_t>(resultShape.size() - sourceShape.size());
for (int64_t i = 0; i < static_cast<int64_t>(resultShape.size()); ++i) {
const int64_t sourceIndex = i - rankOffset;
const int64_t sourceDim = sourceIndex < 0 ? 1 : sourceShape[sourceIndex];
const int64_t resultDim = resultShape[i];
if (sourceDim != 1 && sourceDim != resultDim)
return failure();
}
SmallVector<Attribute> sourceValues(denseAttr.getValues<Attribute>());
SmallVector<int64_t> sourceStrides = computeRowMajorStrides(sourceShape); SmallVector<int64_t> sourceStrides = computeRowMajorStrides(sourceShape);
SmallVector<int64_t> resultStrides = computeRowMajorStrides(resultShape); SmallVector<int64_t> resultStrides = computeRowMajorStrides(resultShape);
SmallVector<Attribute> sourceValues(denseAttr.getValues<Attribute>());
SmallVector<Attribute> resultValues; SmallVector<Attribute> resultValues;
resultValues.reserve(resultType.getNumElements()); resultValues.reserve(resultType.getNumElements());
for (int64_t flatIndex = 0; flatIndex < resultType.getNumElements(); ++flatIndex) { for (int64_t flatIndex = 0; flatIndex < resultType.getNumElements(); ++flatIndex) {
int64_t remaining = flatIndex; int64_t remaining = flatIndex;
int64_t sourceFlatIndex = 0; int64_t sourceFlatIndex = 0;
for (int64_t i = 0; i < static_cast<int64_t>(resultShape.size()); ++i) { for (int64_t i = 0; i < static_cast<int64_t>(resultShape.size()); ++i) {
const int64_t resultIndex = resultStrides.empty() ? 0 : remaining / resultStrides[i]; const int64_t resultIndex = resultStrides.empty() ? 0 : remaining / resultStrides[i];
remaining = resultStrides.empty() ? 0 : remaining % resultStrides[i]; remaining = resultStrides.empty() ? 0 : remaining % resultStrides[i];
const int64_t sourceIndex = i - rankOffset; const int64_t sourceIndex = i - rankOffset;
if (sourceIndex < 0) if (sourceIndex < 0)
continue; continue;
const int64_t sourceDim = sourceShape[sourceIndex]; const int64_t sourceDim = sourceShape[sourceIndex];
const int64_t resultDim = resultShape[i];
if (sourceDim != 1 && sourceDim != resultDim)
return failure();
const int64_t mappedIndex = sourceDim == 1 ? 0 : resultIndex; const int64_t mappedIndex = sourceDim == 1 ? 0 : resultIndex;
sourceFlatIndex += mappedIndex * sourceStrides[sourceIndex]; sourceFlatIndex += mappedIndex * sourceStrides[sourceIndex];
} }
resultValues.push_back(sourceValues[sourceFlatIndex]); resultValues.push_back(sourceValues[sourceFlatIndex]);
} }
@@ -106,7 +96,7 @@ static FailureOr<Value> materializeReciprocalTensor(Value value,
if (failed(broadcastedValue)) if (failed(broadcastedValue))
return failure(); return failure();
auto denseAttr = dyn_cast<DenseFPElementsAttr>(getDenseConstantAttr(*broadcastedValue)); auto denseAttr = dyn_cast<DenseFPElementsAttr>(getHostConstDenseElementsAttr(*broadcastedValue));
if (!denseAttr) if (!denseAttr)
return failure(); return failure();
@@ -185,10 +175,45 @@ struct DivToSpatialCompute : OpConversionPattern<ONNXDivOp> {
} }
}; };
struct AddToSpatialCompute : OpConversionPattern<ONNXAddOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(ONNXAddOp op, ONNXAddOpAdaptor adaptor, ConversionPatternRewriter& rewriter) const override {
auto resultType = dyn_cast<RankedTensorType>(op.getResult().getType());
if (!resultType || !resultType.hasStaticShape())
return failure();
FailureOr<BiasAddPlanCandidate> candidate =
classifyBiasAddPlanCandidate(adaptor.getA(), adaptor.getB(), resultType);
if (succeeded(candidate)) {
auto plan = spatial::SpatBiasAddPlanOp::create(
rewriter, op.getLoc(), resultType, candidate->data, candidate->bias, rewriter.getStringAttr("nchw"));
rewriter.replaceOp(op, plan.getResult());
return success();
}
auto lhs = prepareElementwiseOperand(adaptor.getA(), resultType, rewriter, op.getLoc());
if (failed(lhs))
return failure();
auto rhs = prepareElementwiseOperand(adaptor.getB(), resultType, rewriter, op.getLoc());
if (failed(rhs))
return failure();
auto computeOp =
createSpatCompute<2>(rewriter, op.getLoc(), resultType, {}, ValueRange {*lhs, *rhs}, [&](Value x, Value y) {
auto loweredOp = spatial::SpatVAddOp::create(rewriter, op.getLoc(), resultType, x, y);
spatial::SpatYieldOp::create(rewriter, op.getLoc(), loweredOp.getResult());
});
rewriter.replaceOp(op, computeOp);
return success();
}
};
} // namespace } // namespace
void populateElementwisePatterns(RewritePatternSet& patterns, MLIRContext* ctx) { void populateElementwisePatterns(RewritePatternSet& patterns, MLIRContext* ctx) {
patterns.add<BinaryElementwiseToSpatialCompute<ONNXAddOp, spatial::SpatVAddOp>>(ctx); patterns.add<AddToSpatialCompute>(ctx);
patterns.add<BinaryElementwiseToSpatialCompute<ONNXSubOp, spatial::SpatVSubOp>>(ctx); patterns.add<BinaryElementwiseToSpatialCompute<ONNXSubOp, spatial::SpatVSubOp>>(ctx);
patterns.add<BinaryElementwiseToSpatialCompute<ONNXMulOp, spatial::SpatVMulOp>>(ctx); patterns.add<BinaryElementwiseToSpatialCompute<ONNXMulOp, spatial::SpatVMulOp>>(ctx);
patterns.add<DivToSpatialCompute>(ctx); patterns.add<DivToSpatialCompute>(ctx);
@@ -251,10 +251,7 @@ static FailureOr<spatial::SpatComputeBatch> createVmmBatch(Value a,
rewriter, loc, args.weights.front(), bTileType, bOffsets, bSizes, unitStrides); rewriter, loc, args.weights.front(), bTileType, bOffsets, bSizes, unitStrides);
Value piece = spatial::SpatVMMOp::create(rewriter, loc, pieceType, bTile, aTile).getResult(); Value piece = spatial::SpatVMMOp::create(rewriter, loc, pieceType, bTile, aTile).getResult();
SmallVector<OpFoldResult> pieceOffsets {args.lane, rewriter.getIndexAttr(0)}; publishGraphBatchPhysicalFragment(rewriter, loc, piece, args.outputs.front(), args.lane);
SmallVector<OpFoldResult> pieceSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(crossbarSize.getValue())};
createParallelInsertSliceIntoBatchOutput(
rewriter, loc, piece, args.outputs.front(), pieceOffsets, pieceSizes, unitStrides);
}); });
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
@@ -401,11 +398,7 @@ static FailureOr<spatial::SpatComputeBatch> createVvdmulBatch(Value a,
Value bVector = extractDynamicGemmBColumn(args.inputs[1], column, vectorType, rewriter, loc); Value bVector = extractDynamicGemmBColumn(args.inputs[1], column, vectorType, rewriter, loc);
Value scalar = spatial::SpatVVDMulOp::create(rewriter, loc, scalarType, aVector, bVector).getResult(); Value scalar = spatial::SpatVVDMulOp::create(rewriter, loc, scalarType, aVector, bVector).getResult();
SmallVector<OpFoldResult> outputOffsets {args.lane, rewriter.getIndexAttr(0)}; publishGraphBatchPhysicalFragment(rewriter, loc, scalar, args.outputs.front(), args.lane);
SmallVector<OpFoldResult> scalarSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
SmallVector<OpFoldResult> unitStrides = getUnitStrides(rewriter, 2);
createParallelInsertSliceIntoBatchOutput(
rewriter, loc, scalar, args.outputs.front(), outputOffsets, scalarSizes, unitStrides);
}); });
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
@@ -447,15 +440,14 @@ static FailureOr<spatial::SpatCompute> createDynamicGemmOutputCompute(Value scal
Value row = createDynamicGemmBatchRow(lane, numOutCols, rewriter, nestedLoc); Value row = createDynamicGemmBatchRow(lane, numOutCols, rewriter, nestedLoc);
Value column = Value column =
onnx_mlir::affineModConst(rewriter, nestedLoc, lane, numOutCols, rewriter.getInsertionBlock()->getParentOp()); onnx_mlir::affineModConst(rewriter, nestedLoc, lane, numOutCols, rewriter.getInsertionBlock()->getParentOp());
SmallVector<OpFoldResult> scalarOffsets {lane, rewriter.getIndexAttr(0)};
SmallVector<OpFoldResult> scalarSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)}; SmallVector<OpFoldResult> scalarSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
SmallVector<OpFoldResult> unitStrides {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)}; SmallVector<OpFoldResult> unitStrides {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
Value scalar = tensor::ExtractSliceOp::create( FailureOr<Value> scalar = extractGraphBatchPhysicalFragment(rewriter, nestedLoc, pieces, lane, scalarType);
rewriter, nestedLoc, scalarType, pieces, scalarOffsets, scalarSizes, unitStrides) if (failed(scalar))
.getResult(); return failure();
if (alpha != 1.0f) { if (alpha != 1.0f) {
Value alphaTensor = createScalarTensorConstant(scalarType, alpha, rewriter, nestedLoc); Value alphaTensor = createScalarTensorConstant(scalarType, alpha, rewriter, nestedLoc);
scalar = spatial::SpatVMulOp::create(rewriter, nestedLoc, scalarType, scalar, alphaTensor).getResult(); *scalar = spatial::SpatVMulOp::create(rewriter, nestedLoc, scalarType, *scalar, alphaTensor).getResult();
} }
if (biasArg) { if (biasArg) {
Value biasScalar = Value biasScalar =
@@ -465,11 +457,11 @@ static FailureOr<spatial::SpatCompute> createDynamicGemmOutputCompute(Value scal
biasScalar = biasScalar =
spatial::SpatVMulOp::create(rewriter, nestedLoc, scalarType, biasScalar, betaTensor).getResult(); spatial::SpatVMulOp::create(rewriter, nestedLoc, scalarType, biasScalar, betaTensor).getResult();
} }
scalar = spatial::SpatVAddOp::create(rewriter, nestedLoc, scalarType, scalar, biasScalar).getResult(); *scalar = spatial::SpatVAddOp::create(rewriter, nestedLoc, scalarType, *scalar, biasScalar).getResult();
} }
SmallVector<OpFoldResult> outputOffsets {row, column}; SmallVector<OpFoldResult> outputOffsets {row, column};
Value outputNext = Value outputNext =
tensor::InsertSliceOp::create(rewriter, nestedLoc, scalar, outputAcc, outputOffsets, scalarSizes, unitStrides) tensor::InsertSliceOp::create(rewriter, nestedLoc, *scalar, outputAcc, outputOffsets, scalarSizes, unitStrides)
.getResult(); .getResult();
yielded.push_back(outputNext); yielded.push_back(outputNext);
return success(); return success();
@@ -505,14 +497,13 @@ static Value extractReductionPiece(Value partialPiecesArg,
int64_t numOutRows, int64_t numOutRows,
ConversionPatternRewriter& rewriter, ConversionPatternRewriter& rewriter,
Location loc) { Location loc) {
SmallVector<OpFoldResult> unitStrides {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)}; SmallVector<OpFoldResult> unitStrides {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
SmallVector<OpFoldResult> pieceSizes {rewriter.getIndexAttr(numOutRows), SmallVector<OpFoldResult> pieceSizes {rewriter.getIndexAttr(numOutRows), rewriter.getIndexAttr(1), rewriter.getIndexAttr(crossbarSize.getValue())};
rewriter.getIndexAttr(crossbarSize.getValue())};
SmallVector<OpFoldResult> pieceOffsets { SmallVector<OpFoldResult> pieceOffsets {
createPartialGroupOffset(hSlice, kSlice, numKSlices, numOutRows, rewriter, loc), rewriter.getIndexAttr(0)}; createPartialGroupOffset(hSlice, kSlice, numKSlices, numOutRows, rewriter, loc), rewriter.getIndexAttr(0), rewriter.getIndexAttr(0)};
return tensor::ExtractSliceOp::create( auto selectedType = RankedTensorType::get({numOutRows, 1, static_cast<int64_t>(crossbarSize.getValue())}, pieceType.getElementType());
rewriter, loc, pieceType, partialPiecesArg, pieceOffsets, pieceSizes, unitStrides) Value selected = tensor::ExtractSliceOp::create(rewriter, loc, selectedType, partialPiecesArg, pieceOffsets, pieceSizes, unitStrides);
.getResult(); return tensor::CollapseShapeOp::create(rewriter, loc, pieceType, selected, SmallVector<ReassociationIndices> {{0, 1}, {2}});
} }
static Value reducePartialPiecesForHSlice(Value partialPiecesArg, static Value reducePartialPiecesForHSlice(Value partialPiecesArg,
@@ -730,7 +721,7 @@ LogicalResult GemmToSpatialComputes::matchAndRewrite(ONNXGemmOp gemmOp,
return failure(); return failure();
} }
auto scalarPiecesType = RankedTensorType::get({laneCount64, 1}, outType.getElementType()); auto scalarPiecesType = spatial::getGraphBatchPhysicalResultType(laneCount64, RankedTensorType::get({1, 1}, outType.getElementType()));
auto batchOp = createVvdmulBatch(a, b, aType, bType, scalarPiecesType, outType, rewriter, loc); auto batchOp = createVvdmulBatch(a, b, aType, bType, scalarPiecesType, outType, rewriter, loc);
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
@@ -802,8 +793,8 @@ LogicalResult GemmToSpatialComputes::matchAndRewrite(ONNXGemmOp gemmOp,
return failure(); return failure();
} }
auto partialPiecesType = auto partialPiecesType = spatial::getGraphBatchPhysicalResultType(
RankedTensorType::get({laneCount64, static_cast<int64_t>(crossbarSize.getValue())}, outType.getElementType()); laneCount64, RankedTensorType::get({1, static_cast<int64_t>(crossbarSize.getValue())}, outType.getElementType()));
auto batchOp = auto batchOp =
createVmmBatch(a, b, aType, paddedBType, partialPiecesType, numOutRows, numKSlices, numOutHSlices, rewriter, loc); createVmmBatch(a, b, aType, paddedBType, partialPiecesType, numOutRows, numKSlices, numOutHSlices, rewriter, loc);
if (failed(batchOp)) if (failed(batchOp))
@@ -398,10 +398,7 @@ static FailureOr<spatial::SpatComputeBatch> createBatchedVmmBatch(Value a,
args.weights.front(), bBatchShape, outputBatchShape, batch, kOffset, hOffset, bTileType, rewriter, loc); args.weights.front(), bBatchShape, outputBatchShape, batch, kOffset, hOffset, bTileType, rewriter, loc);
Value piece = spatial::SpatVMMOp::create(rewriter, loc, pieceType, bTile, aTile).getResult(); Value piece = spatial::SpatVMMOp::create(rewriter, loc, pieceType, bTile, aTile).getResult();
SmallVector<OpFoldResult> pieceOffsets {args.lane, rewriter.getIndexAttr(0)}; publishGraphBatchPhysicalFragment(rewriter, loc, piece, args.outputs.front(), args.lane);
SmallVector<OpFoldResult> pieceSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(crossbarSize.getValue())};
createParallelInsertSliceIntoBatchOutput(
rewriter, loc, piece, args.outputs.front(), pieceOffsets, pieceSizes, getUnitStrides(rewriter, 2));
}); });
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
@@ -506,10 +503,7 @@ static FailureOr<spatial::SpatComputeBatch> createBatchedVvdmulBatch(Value a,
Value bVector = extractDynamicBatchedBColumn( Value bVector = extractDynamicBatchedBColumn(
args.inputs[1], bBatchShape, outputBatchShape, batch, column, vectorType, rewriter, loc); args.inputs[1], bBatchShape, outputBatchShape, batch, column, vectorType, rewriter, loc);
Value scalar = spatial::SpatVVDMulOp::create(rewriter, loc, scalarType, aVector, bVector).getResult(); Value scalar = spatial::SpatVVDMulOp::create(rewriter, loc, scalarType, aVector, bVector).getResult();
SmallVector<OpFoldResult> outputOffsets {args.lane, rewriter.getIndexAttr(0)}; publishGraphBatchPhysicalFragment(rewriter, loc, scalar, args.outputs.front(), args.lane);
SmallVector<OpFoldResult> scalarSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)};
createParallelInsertSliceIntoBatchOutput(
rewriter, loc, scalar, args.outputs.front(), outputOffsets, scalarSizes, getUnitStrides(rewriter, 2));
}); });
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
@@ -548,14 +542,13 @@ static FailureOr<Value> createBatchedDynamicOutputCompute(Value scalarPieces,
Value batchLane = affineModConst(rewriter, nestedLoc, lane, numOutRows * numOutCols, anchorOp); Value batchLane = affineModConst(rewriter, nestedLoc, lane, numOutRows * numOutCols, anchorOp);
Value row = affineFloorDivConst(rewriter, nestedLoc, batchLane, numOutCols, anchorOp); Value row = affineFloorDivConst(rewriter, nestedLoc, batchLane, numOutCols, anchorOp);
Value column = affineModConst(rewriter, nestedLoc, batchLane, numOutCols, anchorOp); Value column = affineModConst(rewriter, nestedLoc, batchLane, numOutCols, anchorOp);
SmallVector<OpFoldResult> scalarOffsets {lane, rewriter.getIndexAttr(0)}; FailureOr<Value> scalar = extractGraphBatchPhysicalFragment(rewriter, nestedLoc, pieces, lane, scalarType);
SmallVector<OpFoldResult> scalarSizes {rewriter.getIndexAttr(1), rewriter.getIndexAttr(1)}; if (failed(scalar))
Value scalar = tensor::ExtractSliceOp::create( return failure();
rewriter, nestedLoc, scalarType, pieces, scalarOffsets, scalarSizes, getUnitStrides(rewriter, 2));
Value expanded = tensor::ExpandShapeOp::create(rewriter, Value expanded = tensor::ExpandShapeOp::create(rewriter,
nestedLoc, nestedLoc,
outputScalarType, outputScalarType,
scalar, *scalar,
SmallVector<ReassociationIndices> { SmallVector<ReassociationIndices> {
{0}, {0},
{1, 2} {1, 2}
@@ -596,10 +589,11 @@ static Value extractBatchedReductionPiece(Value partialPiecesArg,
Value kOffset = getOrCreateIndexConstant(rewriter, rewriter.getInsertionBlock()->getParentOp(), kSlice * numOutRows); Value kOffset = getOrCreateIndexConstant(rewriter, rewriter.getInsertionBlock()->getParentOp(), kSlice * numOutRows);
Value batchAndHSlice = arith::AddIOp::create(rewriter, loc, batchOffset, hOffset); Value batchAndHSlice = arith::AddIOp::create(rewriter, loc, batchOffset, hOffset);
Value pieceOffset = arith::AddIOp::create(rewriter, loc, batchAndHSlice, kOffset); Value pieceOffset = arith::AddIOp::create(rewriter, loc, batchAndHSlice, kOffset);
SmallVector<OpFoldResult> offsets {pieceOffset, rewriter.getIndexAttr(0)}; SmallVector<OpFoldResult> offsets {pieceOffset, rewriter.getIndexAttr(0), rewriter.getIndexAttr(0)};
SmallVector<OpFoldResult> sizes {rewriter.getIndexAttr(numOutRows), rewriter.getIndexAttr(crossbarSize.getValue())}; SmallVector<OpFoldResult> sizes {rewriter.getIndexAttr(numOutRows), rewriter.getIndexAttr(1), rewriter.getIndexAttr(crossbarSize.getValue())};
return tensor::ExtractSliceOp::create( auto selectedType = RankedTensorType::get({numOutRows, 1, static_cast<int64_t>(crossbarSize.getValue())}, pieceType.getElementType());
rewriter, loc, pieceType, partialPiecesArg, offsets, sizes, getUnitStrides(rewriter, 2)); Value selected = tensor::ExtractSliceOp::create(rewriter, loc, selectedType, partialPiecesArg, offsets, sizes, getUnitStrides(rewriter, 3));
return tensor::CollapseShapeOp::create(rewriter, loc, pieceType, selected, SmallVector<ReassociationIndices> {{0, 1}, {2}});
} }
static Value reduceBatchedPartialPiecesForHSlice(Value partialPiecesArg, static Value reduceBatchedPartialPiecesForHSlice(Value partialPiecesArg,
@@ -917,9 +911,7 @@ struct MatMulToGemm : OpRewritePattern<ONNXMatMulOp> {
if (failed(shapeInfo) || shapeInfo->lhsWasVector || shapeInfo->rhsWasVector) if (failed(shapeInfo) || shapeInfo->lhsWasVector || shapeInfo->rhsWasVector)
return failure(); return failure();
const bool hasNonSingletonOutputBatch = if (!shapeInfo->outputBatchShape.empty())
!shapeInfo->outputBatchShape.empty() && getStaticShapeElementCount(shapeInfo->outputBatchShape) != 1;
if (hasNonSingletonOutputBatch)
return failure(); return failure();
Location loc = matmulOp.getLoc(); Location loc = matmulOp.getLoc();
@@ -1021,8 +1013,8 @@ struct MatMulBatchedToSpatialComputes : OpRewritePattern<ONNXMatMulOp> {
if (succeeded(paddedRhs)) { if (succeeded(paddedRhs)) {
Value paddedLhs = createPaddedInputCompute(plan.lhs, paddedLhsType, rewriter, loc); Value paddedLhs = createPaddedInputCompute(plan.lhs, paddedLhsType, rewriter, loc);
const int64_t laneCount = plan.batch * plan.m * numKSlices * numOutHSlices; const int64_t laneCount = plan.batch * plan.m * numKSlices * numOutHSlices;
auto partialPiecesType = RankedTensorType::get({laneCount, static_cast<int64_t>(crossbarSize.getValue())}, auto partialPiecesType = spatial::getGraphBatchPhysicalResultType(
shapeInfo->outType.getElementType()); laneCount, RankedTensorType::get({1, static_cast<int64_t>(crossbarSize.getValue())}, shapeInfo->outType.getElementType()));
auto batchOp = createBatchedVmmBatch(paddedLhs, auto batchOp = createBatchedVmmBatch(paddedLhs,
*paddedRhs, *paddedRhs,
paddedLhsType, paddedLhsType,
@@ -1063,7 +1055,8 @@ struct MatMulBatchedToSpatialComputes : OpRewritePattern<ONNXMatMulOp> {
} }
} }
const int64_t laneCount = plan.batch * plan.m * plan.n; const int64_t laneCount = plan.batch * plan.m * plan.n;
auto scalarPiecesType = RankedTensorType::get({laneCount, 1}, shapeInfo->outType.getElementType()); auto scalarPiecesType = spatial::getGraphBatchPhysicalResultType(
laneCount, RankedTensorType::get({1, 1}, shapeInfo->outType.getElementType()));
auto batchOp = createBatchedVvdmulBatch(plan.lhs, auto batchOp = createBatchedVvdmulBatch(plan.lhs,
plan.lhsBatchShape, plan.lhsBatchShape,
plan.rhs, plan.rhs,
@@ -5,7 +5,6 @@
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include <algorithm> #include <algorithm>
#include <numeric>
#include <optional> #include <optional>
#include <type_traits> #include <type_traits>
@@ -122,14 +121,6 @@ static RankedTensorType getKeepdimsType(RankedTensorType inputType, Type element
return RankedTensorType::get(shape, elementType, inputType.getEncoding()); return RankedTensorType::get(shape, elementType, inputType.getEncoding());
} }
static RankedTensorType getCompactKeptType(RankedTensorType inputType, Type elementType, ArrayRef<bool> reducedAxes) {
SmallVector<int64_t> shape;
for (auto [dim, isReduced] : llvm::zip_equal(inputType.getShape(), reducedAxes))
if (!isReduced)
shape.push_back(dim);
return RankedTensorType::get(shape, elementType, inputType.getEncoding());
}
static RankedTensorType getReducedSliceType(RankedTensorType inputType, ArrayRef<bool> reducedAxes) { static RankedTensorType getReducedSliceType(RankedTensorType inputType, ArrayRef<bool> reducedAxes) {
SmallVector<int64_t> shape; SmallVector<int64_t> shape;
shape.reserve(inputType.getRank()); shape.reserve(inputType.getRank());
@@ -139,9 +130,7 @@ static RankedTensorType getReducedSliceType(RankedTensorType inputType, ArrayRef
} }
static RankedTensorType getLanePackedKeepdimsType(int64_t laneCount, RankedTensorType leafType) { static RankedTensorType getLanePackedKeepdimsType(int64_t laneCount, RankedTensorType leafType) {
SmallVector<int64_t> shape(leafType.getShape().begin(), leafType.getShape().end()); return spatial::getGraphBatchPhysicalResultType(laneCount, leafType);
shape.front() = laneCount;
return RankedTensorType::get(shape, leafType.getElementType(), leafType.getEncoding());
} }
static SmallVector<int64_t> getKeptAxes(ArrayRef<bool> reducedAxes) { static SmallVector<int64_t> getKeptAxes(ArrayRef<bool> reducedAxes) {
@@ -191,12 +180,9 @@ static FailureOr<Value> buildReduceMeanKeepdimsBatch(Value input,
SmallVector<OpFoldResult> sliceOffsets; SmallVector<OpFoldResult> sliceOffsets;
SmallVector<OpFoldResult> sliceSizes; SmallVector<OpFoldResult> sliceSizes;
SmallVector<OpFoldResult> insertOffsets;
SmallVector<OpFoldResult> insertSizes(inputType.getRank(), rewriter.getIndexAttr(1));
SmallVector<OpFoldResult> unitStrides = getUnitStrides(rewriter, inputType.getRank()); SmallVector<OpFoldResult> unitStrides = getUnitStrides(rewriter, inputType.getRank());
sliceOffsets.reserve(inputType.getRank()); sliceOffsets.reserve(inputType.getRank());
sliceSizes.reserve(inputType.getRank()); sliceSizes.reserve(inputType.getRank());
insertOffsets.reserve(inputType.getRank());
auto batchOp = auto batchOp =
createSpatComputeBatch(rewriter, createSpatComputeBatch(rewriter,
@@ -209,7 +195,6 @@ static FailureOr<Value> buildReduceMeanKeepdimsBatch(Value input,
size_t keptAxisIndex = 0; size_t keptAxisIndex = 0;
sliceOffsets.clear(); sliceOffsets.clear();
sliceSizes.clear(); sliceSizes.clear();
insertOffsets.clear();
for (auto [axis, isReduced] : llvm::enumerate(reducedAxes)) { for (auto [axis, isReduced] : llvm::enumerate(reducedAxes)) {
if (isReduced) { if (isReduced) {
sliceOffsets.push_back(rewriter.getIndexAttr(0)); sliceOffsets.push_back(rewriter.getIndexAttr(0));
@@ -224,72 +209,90 @@ static FailureOr<Value> buildReduceMeanKeepdimsBatch(Value input,
sliceSizes.push_back(rewriter.getIndexAttr(1)); sliceSizes.push_back(rewriter.getIndexAttr(1));
} }
insertOffsets.push_back(args.lane);
insertOffsets.append(inputType.getRank() - 1, rewriter.getIndexAttr(0));
Value slice = tensor::ExtractSliceOp::create( Value slice = tensor::ExtractSliceOp::create(
rewriter, loc, sliceType, args.inputs.front(), sliceOffsets, sliceSizes, unitStrides); rewriter, loc, sliceType, args.inputs.front(), sliceOffsets, sliceSizes, unitStrides);
Value reduced = spatial::SpatVAvgOp::create(rewriter, loc, leafType, slice).getResult(); Value reduced = spatial::SpatVAvgOp::create(rewriter, loc, leafType, slice).getResult();
createParallelInsertSliceIntoBatchOutput( publishGraphBatchPhysicalFragment(rewriter, loc, reduced, args.outputs.front(), args.lane);
rewriter, loc, reduced, args.outputs.front(), insertOffsets, insertSizes, unitStrides);
}); });
if (failed(batchOp)) if (failed(batchOp))
return failure(); return failure();
return (*batchOp).getResult(0); return (*batchOp).getResult(0);
} }
static Value buildKeepdimsFromLanePackedBatch(Value batchValue, static FailureOr<Value> buildReduceMeanKeepdimsBlueprint(
RankedTensorType keepdimsType, Value batchValue, RankedTensorType keepdimsType,
RankedTensorType compactKeptType, ArrayRef<bool> reducedAxes, ConversionPatternRewriter& rewriter,
ArrayRef<bool> reducedAxes, Location loc) {
ConversionPatternRewriter& rewriter, auto batchType = dyn_cast<RankedTensorType>(batchValue.getType());
Location loc) { int64_t rank = keepdimsType.getRank();
auto batchType = cast<RankedTensorType>(batchValue.getType()); if (!batchType || !batchType.hasStaticShape()
if (batchType == keepdimsType) || !keepdimsType.hasStaticShape()
return batchValue; || static_cast<int64_t>(reducedAxes.size()) != rank
|| batchType.getRank() != rank + 1
|| batchType.getElementType() != keepdimsType.getElementType())
return failure();
SmallVector<ReassociationIndices> collapseToFlat {{}}; int64_t laneCount = 1;
for (int64_t axis = 0; axis < batchType.getRank(); ++axis) SmallVector<int64_t> keptAxes;
collapseToFlat.front().push_back(axis); SmallVector<int64_t> keptAxisStrides;
SmallVector<ReassociationIndices> expandFlatToCompact(1);
for (int64_t axis = 0; axis < compactKeptType.getRank(); ++axis)
expandFlatToCompact.front().push_back(axis);
SmallVector<ReassociationIndices> expandCompactToKeepdims;
ReassociationIndices pendingLeadingReducedAxes;
for (auto [axis, isReduced] : llvm::enumerate(reducedAxes)) { for (auto [axis, isReduced] : llvm::enumerate(reducedAxes)) {
if (isReduced) { int64_t dim = keepdimsType.getDimSize(axis);
if (expandCompactToKeepdims.empty()) if (dim <= 0 || (isReduced && dim != 1))
pendingLeadingReducedAxes.push_back(axis); return failure();
else if (!isReduced)
expandCompactToKeepdims.back().push_back(axis); keptAxes.push_back(axis);
continue;
}
expandCompactToKeepdims.emplace_back();
auto& group = expandCompactToKeepdims.back();
group.append(pendingLeadingReducedAxes.begin(), pendingLeadingReducedAxes.end());
pendingLeadingReducedAxes.clear();
group.push_back(axis);
} }
if (!pendingLeadingReducedAxes.empty()) keptAxisStrides.resize(keptAxes.size(), 1);
expandCompactToKeepdims.back().append(pendingLeadingReducedAxes.begin(), pendingLeadingReducedAxes.end()); for (int64_t index = static_cast<int64_t>(keptAxes.size()) - 1;
index >= 0; --index) {
keptAxisStrides[index] = laneCount;
int64_t dim = keepdimsType.getDimSize(keptAxes[index]);
if (laneCount > std::numeric_limits<int64_t>::max() / dim)
return failure();
laneCount *= dim;
}
if (batchType.getDimSize(0) != laneCount
|| llvm::any_of(batchType.getShape().drop_front(),
[](int64_t dim) { return dim != 1; }))
return failure();
auto reshapeCompute = SmallVector<int64_t> operandIndices(laneCount, 0);
createSpatCompute<1>(rewriter, loc, TypeRange {keepdimsType}, {}, ValueRange {batchValue}, [&](Value input) { SmallVector<int64_t> sourceSlots;
auto flatType = SmallVector<int64_t> sourceOffsets(laneCount, 0);
RankedTensorType::get({batchType.getDimSize(0)}, batchType.getElementType(), batchType.getEncoding()); SmallVector<int64_t> fragmentOffsets;
Value flat = tensor::CollapseShapeOp::create(rewriter, loc, flatType, input, collapseToFlat); sourceSlots.reserve(laneCount);
Value compact = flat; fragmentOffsets.reserve(laneCount * rank);
if (compactKeptType != flatType) for (int64_t lane = 0; lane < laneCount; ++lane) {
compact = tensor::ExpandShapeOp::create(rewriter, loc, compactKeptType, flat, expandFlatToCompact); sourceSlots.push_back(lane);
Value keepdims = compact; size_t keptAxisIndex = 0;
if (keepdimsType != compactKeptType) for (auto [axis, isReduced] : llvm::enumerate(reducedAxes)) {
keepdims = tensor::ExpandShapeOp::create(rewriter, loc, keepdimsType, compact, expandCompactToKeepdims); if (isReduced) {
spatial::SpatYieldOp::create(rewriter, loc, keepdims); fragmentOffsets.push_back(0);
}); continue;
return reshapeCompute.getResult(0); }
int64_t dim = keepdimsType.getDimSize(axis);
fragmentOffsets.push_back(
(lane / keptAxisStrides[keptAxisIndex]) % dim);
++keptAxisIndex;
}
}
SmallVector<int64_t> fragmentSizes(fragmentOffsets.size(), 1);
SmallVector<int64_t> fragmentStrides(fragmentOffsets.size(), 1);
return spatial::SpatBlueprintOp::create(
rewriter, loc, keepdimsType, batchValue, ValueRange {},
rewriter.getStringAttr("nchw"),
rewriter.getStringAttr("fragmented"),
rewriter.getDenseI64ArrayAttr(fragmentOffsets),
rewriter.getDenseI64ArrayAttr(fragmentSizes),
rewriter.getStringAttr("reduce_mean_keepdims_fragments"),
rewriter.getStringAttr("fragment_assembly"),
rewriter.getDenseI64ArrayAttr(operandIndices),
rewriter.getDenseI64ArrayAttr(sourceSlots),
rewriter.getDenseI64ArrayAttr(sourceOffsets),
rewriter.getDenseI64ArrayAttr(fragmentStrides),
rewriter.getStringAttr("disjoint"),
rewriter.getStringAttr("complete"))
.getOutput();
} }
static SmallVector<ReassociationIndices> buildCollapseReassociation(ArrayRef<bool> reducedAxes) { static SmallVector<ReassociationIndices> buildCollapseReassociation(ArrayRef<bool> reducedAxes) {
@@ -366,26 +369,36 @@ struct ReduceMeanToSpatialCompute : OpConversionPattern<ReduceMeanOp> {
Location loc = reduceMeanOp.getLoc(); Location loc = reduceMeanOp.getLoc();
RankedTensorType leafType = getAllOnesType(inputType, resultType.getElementType()); RankedTensorType leafType = getAllOnesType(inputType, resultType.getElementType());
RankedTensorType compactKeptType = getCompactKeptType(inputType, resultType.getElementType(), reducedAxes);
RankedTensorType keepdimsType = getKeepdimsType(inputType, resultType.getElementType(), reducedAxes); RankedTensorType keepdimsType = getKeepdimsType(inputType, resultType.getElementType(), reducedAxes);
int64_t laneCount = 1; int64_t laneCount = 1;
for (int64_t dim : compactKeptType.getShape()) for (auto [dim, isReduced] : llvm::zip_equal(keepdimsType.getShape(), reducedAxes)) {
if (isReduced)
continue;
if (dim <= 0 || laneCount > std::numeric_limits<int32_t>::max() / dim)
return rewriter.notifyMatchFailure(
reduceMeanOp, "ReduceMean physical lane count is not representable");
laneCount *= dim; laneCount *= dim;
}
RankedTensorType batchType = getLanePackedKeepdimsType(laneCount, leafType); RankedTensorType batchType = getLanePackedKeepdimsType(laneCount, leafType);
auto lanePackedKeepdims = auto lanePackedKeepdims =
buildReduceMeanKeepdimsBatch(adaptor.getData(), reducedAxes, batchType, leafType, rewriter, loc); buildReduceMeanKeepdimsBatch(adaptor.getData(), reducedAxes, batchType, leafType, rewriter, loc);
if (failed(lanePackedKeepdims)) if (failed(lanePackedKeepdims))
return failure(); return failure();
Value reducedKeepdims = auto reducedKeepdims = buildReduceMeanKeepdimsBlueprint(
buildKeepdimsFromLanePackedBatch(*lanePackedKeepdims, keepdimsType, compactKeptType, reducedAxes, rewriter, loc); *lanePackedKeepdims, keepdimsType, reducedAxes, rewriter, loc);
if (failed(reducedKeepdims))
return rewriter.notifyMatchFailure(
reduceMeanOp,
"cannot build physical-fragment ReduceMean keepdims reconstruction");
if (semantics->keepdims != 0) { if (semantics->keepdims != 0) {
rewriter.replaceOp(reduceMeanOp, reducedKeepdims); rewriter.replaceOp(reduceMeanOp, *reducedKeepdims);
return success(); return success();
} }
Value reduced = squeezeReducedAxes(reducedKeepdims, resultType, reducedAxes, rewriter, loc); Value reduced = squeezeReducedAxes(
*reducedKeepdims, resultType, reducedAxes, rewriter, loc);
rewriter.replaceOp(reduceMeanOp, reduced); rewriter.replaceOp(reduceMeanOp, reduced);
return success(); return success();
} }
@@ -10,6 +10,7 @@
#include "src/Accelerators/PIM/Common/IR/WeightUtils.hpp" #include "src/Accelerators/PIM/Common/IR/WeightUtils.hpp"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp" #include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/ComputeRegionBuilder.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/WeightMaterialization.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/WeightMaterialization.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
@@ -128,8 +129,6 @@ struct PromoteWeightLikeComputeInputsPattern : OpRewritePattern<spatial::SpatGra
Block& oldBlock = compute.getBody().front(); Block& oldBlock = compute.getBody().front();
rewriter.setInsertionPointAfter(compute); rewriter.setInsertionPointAfter(compute);
auto newCompute = spatial::SpatGraphCompute::create(
rewriter, compute.getLoc(), compute.getResultTypes(), promoted->newWeights, promoted->newInputs);
SmallVector<Type> newBlockArgTypes; SmallVector<Type> newBlockArgTypes;
SmallVector<Location> newBlockArgLocs; SmallVector<Location> newBlockArgLocs;
for (Value weight : promoted->newWeights) { for (Value weight : promoted->newWeights) {
@@ -138,10 +137,14 @@ struct PromoteWeightLikeComputeInputsPattern : OpRewritePattern<spatial::SpatGra
} }
llvm::append_range(newBlockArgTypes, promoted->newInputTypes); llvm::append_range(newBlockArgTypes, promoted->newInputTypes);
llvm::append_range(newBlockArgLocs, promoted->newInputLocs); llvm::append_range(newBlockArgLocs, promoted->newInputLocs);
auto* newBlock = rewriter.createBlock( auto newCompute = createEmptySpatGraphCompute(rewriter,
&newCompute.getBody(), newCompute.getBody().end(), TypeRange(newBlockArgTypes), newBlockArgLocs); compute.getLoc(),
newCompute.getProperties().setOperandSegmentSizes( compute.getResultTypes(),
{static_cast<int>(promoted->newWeights.size()), static_cast<int>(promoted->newInputs.size())}); promoted->newWeights,
promoted->newInputs,
TypeRange(newBlockArgTypes),
newBlockArgLocs);
auto* newBlock = &newCompute.getBody().front();
rewriter.setInsertionPointToStart(newBlock); rewriter.setInsertionPointToStart(newBlock);
IRRewriter bodyRewriter(rewriter.getContext()); IRRewriter bodyRewriter(rewriter.getContext());
@@ -193,12 +196,6 @@ struct PromoteWeightLikeComputeBatchInputsPattern : OpRewritePattern<spatial::Sp
rewriter.setInsertionPointAfter(compute); rewriter.setInsertionPointAfter(compute);
auto laneCountAttr = pim::getCheckedI32Attr(
rewriter, compute, static_cast<uint64_t>(compute.getLaneCount()), "promoted compute_batch lane count");
if (failed(laneCountAttr))
return failure();
auto newCompute = spatial::SpatGraphComputeBatch::create(
rewriter, compute.getLoc(), compute.getResultTypes(), *laneCountAttr, promoted->newWeights, promoted->newInputs);
auto laneArg = compute.getLaneArgument(); auto laneArg = compute.getLaneArgument();
if (!laneArg) if (!laneArg)
return rewriter.notifyMatchFailure(compute, "missing compute_batch lane block argument"); return rewriter.notifyMatchFailure(compute, "missing compute_batch lane block argument");
@@ -223,23 +220,30 @@ struct PromoteWeightLikeComputeBatchInputsPattern : OpRewritePattern<spatial::Sp
newBlockArgLocs.push_back(outputArg->getLoc()); newBlockArgLocs.push_back(outputArg->getLoc());
} }
auto* newBlock = rewriter.createBlock( auto newCompute = createEmptySpatGraphComputeBatch(rewriter,
&newCompute.getBody(), newCompute.getBody().end(), TypeRange(newBlockArgTypes), newBlockArgLocs); compute.getLoc(),
newCompute.getProperties().setOperandSegmentSizes( compute.getResultTypes(),
{static_cast<int>(promoted->newWeights.size()), static_cast<int>(promoted->newInputs.size())}); compute.getLaneCount(),
promoted->newWeights,
promoted->newInputs,
TypeRange(newBlockArgTypes),
newBlockArgLocs);
if (failed(newCompute))
return failure();
auto* newBlock = &(*newCompute).getBody().front();
rewriter.setInsertionPointToStart(newBlock); rewriter.setInsertionPointToStart(newBlock);
IRRewriter bodyRewriter(rewriter.getContext()); IRRewriter bodyRewriter(rewriter.getContext());
bodyRewriter.setInsertionPointToStart(newBlock); bodyRewriter.setInsertionPointToStart(newBlock);
IRMapping mapper; IRMapping mapper;
auto newLaneArg = newCompute.getLaneArgument(); auto newLaneArg = (*newCompute).getLaneArgument();
if (!newLaneArg) if (!newLaneArg)
return rewriter.notifyMatchFailure(compute, "missing rewritten compute_batch lane block argument"); return rewriter.notifyMatchFailure(compute, "missing rewritten compute_batch lane block argument");
mapper.map(*laneArg, *newLaneArg); mapper.map(*laneArg, *newLaneArg);
for (auto [weightIndex, weight] : llvm::enumerate(compute.getWeights())) { for (auto [weightIndex, weight] : llvm::enumerate(compute.getWeights())) {
auto oldWeightArg = compute.getWeightArgument(weightIndex); auto oldWeightArg = compute.getWeightArgument(weightIndex);
auto newWeightArg = newCompute.getWeightArgument(weightIndex); auto newWeightArg = (*newCompute).getWeightArgument(weightIndex);
if (!oldWeightArg || !newWeightArg) if (!oldWeightArg || !newWeightArg)
return rewriter.notifyMatchFailure(compute, "missing compute_batch weight block argument during rewrite"); return rewriter.notifyMatchFailure(compute, "missing compute_batch weight block argument during rewrite");
mapper.map(*oldWeightArg, *newWeightArg); mapper.map(*oldWeightArg, *newWeightArg);
@@ -249,7 +253,7 @@ struct PromoteWeightLikeComputeBatchInputsPattern : OpRewritePattern<spatial::Sp
*promoted, *promoted,
bodyRewriter, bodyRewriter,
mapper, mapper,
[&](size_t index) { return newCompute.getInputArgument(index); }, [&](size_t index) { return (*newCompute).getInputArgument(index); },
rewriter))) rewriter)))
return failure(); return failure();
for (auto resultIndex : llvm::seq<size_t>(0, compute.getNumResults())) { for (auto resultIndex : llvm::seq<size_t>(0, compute.getNumResults())) {
@@ -263,7 +267,7 @@ struct PromoteWeightLikeComputeBatchInputsPattern : OpRewritePattern<spatial::Sp
for (Operation& op : oldBlock) for (Operation& op : oldBlock)
rewriter.clone(op, mapper); rewriter.clone(op, mapper);
rewriter.replaceOp(compute, newCompute.getResults()); rewriter.replaceOp(compute, (*newCompute).getResults());
return success(); return success();
} }
}; };
@@ -0,0 +1,112 @@
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/SmallVector.h"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp"
using namespace mlir;
namespace onnx_mlir {
namespace {
static FailureOr<int64_t> normalizeFlattenAxis(int64_t axis, int64_t rank) {
int64_t normalizedAxis = axis < 0 ? rank + axis : axis;
if (normalizedAxis < 0 || normalizedAxis > rank)
return failure();
return normalizedAxis;
}
static int64_t product(ArrayRef<int64_t> values) {
int64_t result = 1;
for (int64_t value : values)
result *= value;
return result;
}
static SmallVector<ReassociationIndices> getCollapseTo1DReassociation(int64_t rank) {
SmallVector<ReassociationIndices> reassociation(1);
reassociation.front().reserve(rank);
for (int64_t dim = 0; dim < rank; ++dim)
reassociation.front().push_back(dim);
return reassociation;
}
static SmallVector<ReassociationIndices> getExpandFrom1DReassociation(int64_t rank) {
SmallVector<ReassociationIndices> reassociation(1);
reassociation.front().reserve(rank);
for (int64_t dim = 0; dim < rank; ++dim)
reassociation.front().push_back(dim);
return reassociation;
}
static Value buildFlatten(Value input,
RankedTensorType sourceType,
RankedTensorType resultType,
int64_t axis,
ConversionPatternRewriter& rewriter,
Location loc) {
if (sourceType == resultType)
return input;
if (axis > 0 && axis < sourceType.getRank()) {
SmallVector<ReassociationIndices> reassociation(2);
for (int64_t dim = 0; dim < axis; ++dim)
reassociation[0].push_back(dim);
for (int64_t dim = axis; dim < sourceType.getRank(); ++dim)
reassociation[1].push_back(dim);
return tensor::CollapseShapeOp::create(rewriter, loc, resultType, input, reassociation);
}
Value flattened = input;
if (sourceType.getRank() != 1) {
auto flatType = RankedTensorType::get({sourceType.getNumElements()}, sourceType.getElementType());
flattened = tensor::CollapseShapeOp::create(
rewriter, loc, flatType, flattened, getCollapseTo1DReassociation(sourceType.getRank()));
}
return tensor::ExpandShapeOp::create(
rewriter, loc, resultType, flattened, getExpandFrom1DReassociation(resultType.getRank()));
}
struct Flatten : OpConversionPattern<ONNXFlattenOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(ONNXFlattenOp flattenOp,
ONNXFlattenOpAdaptor adaptor,
ConversionPatternRewriter& rewriter) const override {
auto sourceType = dyn_cast<RankedTensorType>(adaptor.getInput().getType());
auto resultType = dyn_cast<RankedTensorType>(flattenOp.getOperation()->getResult(0).getType());
if (!sourceType || !resultType || !sourceType.hasStaticShape() || !resultType.hasStaticShape())
return failure();
if (!hasStaticPositiveShape(sourceType) || !hasStaticPositiveShape(resultType) || resultType.getRank() != 2)
return failure();
auto axis = normalizeFlattenAxis(flattenOp.getAxis(), sourceType.getRank());
if (failed(axis))
return failure();
int64_t outerDim = product(sourceType.getShape().take_front(*axis));
int64_t innerDim = product(sourceType.getShape().drop_front(*axis));
if (resultType.getShape()[0] != outerDim || resultType.getShape()[1] != innerDim)
return failure();
auto replaceWithFlatten = [&](auto build) -> LogicalResult {
Value flattened = materializeOrComputeUnary(adaptor.getInput(), resultType, rewriter, flattenOp.getLoc(), build);
rewriter.replaceOp(flattenOp, flattened);
return success();
};
return replaceWithFlatten([&](Value input) {
return buildFlatten(input, sourceType, resultType, *axis, rewriter, flattenOp.getLoc());
});
}
};
} // namespace
void populateFlattenPatterns(RewritePatternSet& patterns, MLIRContext* ctx) { patterns.add<Flatten>(ctx); }
} // namespace onnx_mlir
@@ -5,7 +5,7 @@
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/Common.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Patterns.hpp"
#include "src/Dialect/ONNX/ONNXOps.hpp" #include "src/Dialect/ONNX/ONNXOps.hpp"
@@ -52,35 +52,12 @@ static FailureOr<Value> materializeTransposedConstant(Value input,
return failure(); return failure();
} }
if (denseAttr.isSplat()) auto transposedAttr = transposeDenseElementsAttr(denseAttr, permutation);
return getOrCreateConstant(rewriter, if (failed(transposedAttr) || transposedAttr->getType() != resultType)
rewriter.getInsertionBlock()->getParentOp(), return failure();
DenseElementsAttr::get(resultType, denseAttr.getSplatValue<Attribute>()),
resultType);
SmallVector<Attribute> inputValues(denseAttr.getValues<Attribute>());
SmallVector<Attribute> resultValues(inputValues.size());
SmallVector<int64_t> inputStrides = computeRowMajorStrides(inputType.getShape());
SmallVector<int64_t> resultStrides = computeRowMajorStrides(resultType.getShape());
SmallVector<int64_t> inputIndices(inputType.getRank(), 0);
for (auto [linearIndex, value] : llvm::enumerate(inputValues)) {
int64_t remaining = static_cast<int64_t>(linearIndex);
for (int64_t dim = 0; dim < inputType.getRank(); ++dim) {
inputIndices[dim] = inputStrides.empty() ? 0 : remaining / inputStrides[dim];
remaining = inputStrides.empty() ? 0 : remaining % inputStrides[dim];
}
int64_t resultLinearIndex = 0;
for (int64_t dim = 0; dim < resultType.getRank(); ++dim)
resultLinearIndex += inputIndices[permutation[dim]] * resultStrides[dim];
resultValues[resultLinearIndex] = value;
}
return getOrCreateConstant(rewriter, return getOrCreateConstant(rewriter,
rewriter.getInsertionBlock()->getParentOp(), rewriter.getInsertionBlock()->getParentOp(),
DenseElementsAttr::get(resultType, resultValues), *transposedAttr,
resultType); resultType);
} }
@@ -6,10 +6,11 @@
#include "Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp" #include "Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp"
#include "src/Accelerators/PIM/Common/PimCommon.hpp" #include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/BiasAddUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/Common/RowStripLayoutUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/PlanLowering.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/PlanLowering.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Accelerators/PIM/Pass/PIMPasses.h" #include "src/Accelerators/PIM/Pass/PIMPasses.h"
#include "src/Accelerators/PIM/Pass/PIMPasses.h"
using namespace mlir; using namespace mlir;
@@ -19,7 +20,6 @@ namespace {
static constexpr StringLiteral kLogicalLayout = "nchw"; static constexpr StringLiteral kLogicalLayout = "nchw";
static constexpr StringLiteral kDenseLayout = "dense_nchw"; static constexpr StringLiteral kDenseLayout = "dense_nchw";
static constexpr StringLiteral kRowStripLayout = "nchw_row_strip"; static constexpr StringLiteral kRowStripLayout = "nchw_row_strip";
static constexpr StringLiteral kRowStripIndexMap = "packed_hwc_rows_to_nchw";
enum class SelectedLayout { enum class SelectedLayout {
DenseNchw, DenseNchw,
@@ -34,6 +34,8 @@ static SelectedLayout getSelectedLayout(llvm::DenseMap<Value, SelectedLayout>& l
static bool usesSelectedRowStrip(Operation* user, llvm::DenseMap<Value, SelectedLayout>& layouts) { static bool usesSelectedRowStrip(Operation* user, llvm::DenseMap<Value, SelectedLayout>& layouts) {
if (auto reluPlan = dyn_cast<spatial::SpatReluPlanOp>(user)) if (auto reluPlan = dyn_cast<spatial::SpatReluPlanOp>(user))
return getSelectedLayout(layouts, reluPlan.getResult()) == SelectedLayout::NchwRowStrip; return getSelectedLayout(layouts, reluPlan.getResult()) == SelectedLayout::NchwRowStrip;
if (auto biasAddPlan = dyn_cast<spatial::SpatBiasAddPlanOp>(user))
return getSelectedLayout(layouts, biasAddPlan.getResult()) == SelectedLayout::NchwRowStrip;
if (auto convPlan = dyn_cast<spatial::SpatConv2DPlanOp>(user)) if (auto convPlan = dyn_cast<spatial::SpatConv2DPlanOp>(user))
return getSelectedLayout(layouts, convPlan.getResult()) == SelectedLayout::NchwRowStrip; return getSelectedLayout(layouts, convPlan.getResult()) == SelectedLayout::NchwRowStrip;
return false; return false;
@@ -49,21 +51,26 @@ static bool allUsersCanHandleRowStrip(Value value, llvm::DenseMap<Value, Selecte
return true; return true;
} }
static std::pair<SmallVector<int64_t>, SmallVector<int64_t>> buildRowStripMetadata(RankedTensorType type) { static bool canConsumeRowStripAsUser(Operation* user) {
SmallVector<int64_t> offsets; if (isa<spatial::SpatReluPlanOp>(user))
SmallVector<int64_t> sizes; return true;
const int64_t channels = type.getDimSize(1); if (auto biasAddPlan = dyn_cast<spatial::SpatBiasAddPlanOp>(user)) {
const int64_t height = type.getDimSize(2); auto resultType = dyn_cast<RankedTensorType>(biasAddPlan.getOutput().getType());
const int64_t width = type.getDimSize(3); return resultType && isSupportedBiasAddValue(biasAddPlan.getBias(), resultType);
offsets.reserve(height * 4);
sizes.reserve(height * 4);
for (int64_t row = 0; row < height; ++row) {
offsets.append({0, 0, row, 0});
sizes.append({1, channels, 1, width});
} }
return {offsets, sizes}; if (auto convPlan = dyn_cast<spatial::SpatConv2DPlanOp>(user))
return succeeded(canConsumeAndProduceRowStrip(convPlan));
return false;
} }
static bool hasRowStripConsumer(Value value) {
for (Operation* user : value.getUsers())
if (canConsumeRowStripAsUser(user))
return true;
return false;
}
static bool canSelectConvRowStrip(spatial::SpatConv2DPlanOp convPlan, static bool canSelectConvRowStrip(spatial::SpatConv2DPlanOp convPlan,
llvm::DenseMap<Value, SelectedLayout>& layouts) { llvm::DenseMap<Value, SelectedLayout>& layouts) {
SelectedLayout inputLayout = getSelectedLayout(layouts, convPlan.getInput()); SelectedLayout inputLayout = getSelectedLayout(layouts, convPlan.getInput());
@@ -76,6 +83,9 @@ static SelectedLayout chooseConvLayout(spatial::SpatConv2DPlanOp convPlan,
llvm::DenseMap<Value, SelectedLayout>& layouts) { llvm::DenseMap<Value, SelectedLayout>& layouts) {
if (!canSelectConvRowStrip(convPlan, layouts)) if (!canSelectConvRowStrip(convPlan, layouts))
return SelectedLayout::DenseNchw; return SelectedLayout::DenseNchw;
if (getSelectedLayout(layouts, convPlan.getInput()) != SelectedLayout::NchwRowStrip
&& !hasRowStripConsumer(convPlan.getResult()))
return SelectedLayout::DenseNchw;
if (!allUsersCanHandleRowStrip(convPlan.getResult(), layouts)) if (!allUsersCanHandleRowStrip(convPlan.getResult(), layouts))
return SelectedLayout::DenseNchw; return SelectedLayout::DenseNchw;
return SelectedLayout::NchwRowStrip; return SelectedLayout::NchwRowStrip;
@@ -85,11 +95,27 @@ static SelectedLayout chooseReluLayout(spatial::SpatReluPlanOp reluPlan,
llvm::DenseMap<Value, SelectedLayout>& layouts) { llvm::DenseMap<Value, SelectedLayout>& layouts) {
if (getSelectedLayout(layouts, reluPlan.getInput()) != SelectedLayout::NchwRowStrip) if (getSelectedLayout(layouts, reluPlan.getInput()) != SelectedLayout::NchwRowStrip)
return SelectedLayout::DenseNchw; return SelectedLayout::DenseNchw;
if (!hasRowStripConsumer(reluPlan.getResult()))
return SelectedLayout::DenseNchw;
if (!allUsersCanHandleRowStrip(reluPlan.getResult(), layouts)) if (!allUsersCanHandleRowStrip(reluPlan.getResult(), layouts))
return SelectedLayout::DenseNchw; return SelectedLayout::DenseNchw;
return SelectedLayout::NchwRowStrip; return SelectedLayout::NchwRowStrip;
} }
static SelectedLayout chooseBiasAddLayout(spatial::SpatBiasAddPlanOp biasAddPlan,
llvm::DenseMap<Value, SelectedLayout>& layouts) {
if (getSelectedLayout(layouts, biasAddPlan.getInput()) != SelectedLayout::NchwRowStrip)
return SelectedLayout::DenseNchw;
auto resultType = dyn_cast<RankedTensorType>(biasAddPlan.getOutput().getType());
if (!resultType || !isSupportedBiasAddValue(biasAddPlan.getBias(), resultType))
return SelectedLayout::DenseNchw;
if (!hasRowStripConsumer(biasAddPlan.getResult()))
return SelectedLayout::DenseNchw;
if (!allUsersCanHandleRowStrip(biasAddPlan.getResult(), layouts))
return SelectedLayout::DenseNchw;
return SelectedLayout::NchwRowStrip;
}
static spatial::SpatBlueprintOp insertRowStripBlueprint(IRRewriter& rewriter, Value value) { static spatial::SpatBlueprintOp insertRowStripBlueprint(IRRewriter& rewriter, Value value) {
auto outputType = cast<RankedTensorType>(value.getType()); auto outputType = cast<RankedTensorType>(value.getType());
auto [offsets, sizes] = buildRowStripMetadata(outputType); auto [offsets, sizes] = buildRowStripMetadata(outputType);
@@ -108,6 +134,7 @@ static spatial::SpatBlueprintOp insertRowStripBlueprint(IRRewriter& rewriter, Va
nullptr, nullptr,
nullptr, nullptr,
nullptr, nullptr,
nullptr,
nullptr); nullptr);
} }
@@ -173,6 +200,14 @@ struct SpatialLayoutPlanningPass final : PassWrapper<SpatialLayoutPlanningPass,
} }
continue; continue;
} }
if (auto biasAddPlan = dyn_cast<spatial::SpatBiasAddPlanOp>(&op)) {
SelectedLayout selected = chooseBiasAddLayout(biasAddPlan, layouts);
if (layouts[biasAddPlan.getResult()] != selected) {
layouts[biasAddPlan.getResult()] = selected;
changed = true;
}
continue;
}
} }
} }
@@ -180,6 +215,8 @@ struct SpatialLayoutPlanningPass final : PassWrapper<SpatialLayoutPlanningPass,
Value producedValue; Value producedValue;
if (auto convPlan = dyn_cast<spatial::SpatConv2DPlanOp>(&op)) if (auto convPlan = dyn_cast<spatial::SpatConv2DPlanOp>(&op))
producedValue = convPlan.getResult(); producedValue = convPlan.getResult();
else if (auto biasAddPlan = dyn_cast<spatial::SpatBiasAddPlanOp>(&op))
producedValue = biasAddPlan.getResult();
else if (auto reluPlan = dyn_cast<spatial::SpatReluPlanOp>(&op)) else if (auto reluPlan = dyn_cast<spatial::SpatReluPlanOp>(&op))
producedValue = reluPlan.getResult(); producedValue = reluPlan.getResult();
else else
@@ -1,17 +0,0 @@
add_onnx_mlir_rewriter(SpatialToGraphviz)
add_pim_library(OMSpatialToGraphviz
SpatialToGraphviz.cpp
EXCLUDE_FROM_OM_LIBS
LINK_LIBS PUBLIC
MLIRTosaDialect
OMCompilerOptions
OMPimCommon
OMONNXOps
SpatialOps
ACCEL_INCLUDE_DIRS PRIVATE
${PIM_GENERATED_INCLUDE_DIRS}
)
@@ -1,259 +0,0 @@
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/IR/Block.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Value.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Format.h"
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Accelerators/PIM/Pass/PIMPasses.h"
#include "src/Dialect/ONNX/ONNXOps.hpp"
#define FORMAT_OPERATION(op) 'x' << llvm::format_hex_no_prefix(reinterpret_cast<size_t>(op), 0)
#define FORMAT_ARGUMENT(computeOpPointer, argumentNum) llvm::format("Arg_%p_%u", computeOpPointer, argumentNum)
using namespace mlir;
namespace onnx_mlir {
namespace {
struct SpatialToGraphvizPass : public PassWrapper<SpatialToGraphvizPass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SpatialToGraphvizPass)
StringRef getArgument() const override { return "convert-spatial-to-graphviz"; }
StringRef getDescription() const override { return "Lower ONNX ops to Spatial ops."; }
SpatialToGraphvizPass(raw_ostream& os = llvm::errs())
: os(os) {}
SpatialToGraphvizPass(const SpatialToGraphvizPass& pass)
: SpatialToGraphvizPass(pass.os) {}
void runOnOperation() final;
private:
raw_ostream& os;
/**
* Draws the subgraph for a given spatial::SpatCompute, including:
* 1. Input nodes (block arguments)
* 2. Operations
* 3. Edges between yield (output) and its users
*
* @param op The spatial::SpatCompute to draw the subgraph for.
* @param computeNum The number of the compute operation.
*/
void drawComputeOpSubgraph(spatial::SpatCompute op, size_t computeNum) {
os << "\tsubgraph cluster" << computeNum << " {\n\t\tlabel=\"Compute" << computeNum << "\";\n"
<< "\t\tstyle=filled;\n"
<< "\t\tcolor=lightblue;\n";
Block& block = op.getBody().front();
// Inputs
size_t inputNum = 0;
for (BlockArgument& input : block.getArguments()) {
auto fromOp = FORMAT_ARGUMENT(op.getOperation(), inputNum);
os << "\t\t" << fromOp << " [label=\"Arg" << inputNum << "\",shape=box];\n";
for (auto userOp : input.getUsers())
os << "\t\t" << fromOp << " -> " << FORMAT_OPERATION(userOp) << ";\n";
inputNum++;
}
// Iterate operations
for (auto& childOp : block.getOperations()) {
os << "\t\t" << FORMAT_OPERATION(&childOp) << " [label=\"" << childOp.getName() << "\"];\n";
drawEdgesFromOpToItsUsers(&childOp);
}
os << "\t}\n";
// Draw edges from the yield to the users of this computeOp
Operation* yieldOp = block.getTerminator();
if (!isa<spatial::SpatYieldOp>(yieldOp)) {
yieldOp->emitError("Terminator of block must be YieldOp ???");
signalPassFailure();
return;
}
for (auto computeOpResult : op->getResults()) {
for (auto& computeOpUse : computeOpResult.getUses()) {
auto toOp = FORMAT_ARGUMENT(computeOpUse.getOwner(), computeOpUse.getOperandNumber());
os << "\t" << FORMAT_OPERATION(yieldOp) << " -> " << toOp << ";\n";
}
}
}
/**
* @brief Draws the subgraph for a concatOp.
*
* This function draws a subgraph for a concatOp. The subgraph consists of a
* node for each input of the concatOp, as well as an output node. Edges are
* created from the output node to each user of the concatOp.
*
* @param concatOp The concatOp for which the subgraph is drawn.
* @param concatOpNum The number of the concatOp.
*/
void drawConcatOpSubgraph(Operation* concatOp, size_t concatOpNum) {
os << "\tsubgraph clusterconcat" << concatOpNum << " {\n\t\tlabel=\"ConcatOp" << concatOpNum << "\";\n"
<< "\t\tstyle=filled;\n"
<< "\t\tcolor=orange;\n";
// Inputs
size_t inputNum = 0;
for (Value input : concatOp->getOperands()) {
auto fromOp = FORMAT_ARGUMENT(concatOp, inputNum);
os << "\t\t" << fromOp << " [label=\"Input" << inputNum << "\"];\n";
for (auto userOp : input.getUsers())
os << "\t\t" << fromOp << " -> " << FORMAT_OPERATION(userOp) << ";\n";
inputNum++;
}
// Output
os << "\t\t" << FORMAT_OPERATION(concatOp) << " [label=Out];\n";
os << "\t}\n";
// Edges from output to users
for (auto& computeOpUse : concatOp->getResult(0).getUses()) {
os << "\t" << FORMAT_OPERATION(concatOp) << " -> "
<< FORMAT_ARGUMENT(computeOpUse.getOwner(), computeOpUse.getOperandNumber()) << ";\n";
}
}
/**
* Draws the ExtractSliceOp in the graph visualization.
*
* This function takes a tensor::ExtractSliceOp and adds the corresponding
* node and edges to the graph visualization. It creates a node with the
* label as the static offsets attribute of the sliceOp, and connects it to
* the compute operations that use the result of the sliceOp.
*
* @param sliceOp The tensor::ExtractSliceOp to be drawn in the graph
* visualization.
*/
void drawExtractSliceOp(tensor::ExtractSliceOp sliceOp) {
auto nodeId = FORMAT_ARGUMENT(sliceOp.getOperation(), 0);
os << "\t" << nodeId << " [label=\"Slice: ";
sliceOp.getStaticOffsetsAttr().print(os);
os << "\",color=lawngreen];\n";
for (auto& computeOpUse : sliceOp.getResult().getUses()) {
os << "\t" << nodeId << " -> " << FORMAT_ARGUMENT(computeOpUse.getOwner(), computeOpUse.getOperandNumber())
<< ";\n";
}
}
void drawBiasTileOp(tensor::ExtractSliceOp sliceOp) {
auto nodeId = FORMAT_ARGUMENT(sliceOp.getOperation(), 0);
os << "\t" << nodeId << " [label=\"Bias: ";
sliceOp.getStaticOffsetsAttr().print(os);
os << "\",color=lightpink];\n";
for (auto user : sliceOp.getResult().getUsers())
os << "\t" << nodeId << " -> " << FORMAT_OPERATION(user) << ";\n";
}
/**
* Draws edges from the given operation to its users.
*
* @param fromOp The operation from which the edges are drawn.
*/
void drawEdgesFromOpToItsUsers(mlir::Operation* fromOp) {
for (auto result : fromOp->getResults())
for (auto userOp : result.getUsers())
os << "\t\t" << FORMAT_OPERATION(fromOp) << " -> " << FORMAT_OPERATION(userOp) << ";\n";
}
/**
* Draws input node and edges for the given `funcOp`.
*
* @param funcOp The `funcOp` for which to draw input nodes and edges.
*/
void drawInputNodesAndEdges(func::FuncOp& funcOp) {
os << "\tinput [label=\"Module Input\",color=green];\n";
size_t funcOpArgNum = 0;
for (BlockArgument& arg : funcOp.getArguments()) {
for (auto& useOp : arg.getUses()) {
os << "\tinput -> " << FORMAT_ARGUMENT(useOp.getOwner(), useOp.getOperandNumber()) << "[label=" << funcOpArgNum
<< "];\n";
}
funcOpArgNum++;
}
}
};
void SpatialToGraphvizPass::runOnOperation() {
ModuleOp module = getOperation();
auto entryFunc = getPimEntryFunc(module);
if (failed(entryFunc)) {
module.emitError("failed to locate the PIM entry function for Spatial graph visualization");
signalPassFailure();
return;
}
func::FuncOp func = *entryFunc;
os << "digraph G {\n"
<< "\tnode [style=filled,color=white];\n";
size_t computeNum = 0;
size_t concatNum = 0;
// Iterate over the ComputeOps within FuncOp:
// 1. Print their subgraph
// 2. Print the edges from its inputs to its outputs
for (Operation& op : func.getOps()) {
if (auto computeOp = dyn_cast<spatial::SpatCompute>(op)) {
drawComputeOpSubgraph(computeOp, computeNum++);
}
else if (auto concatOp = dyn_cast<tensor::ConcatOp>(op)) {
drawConcatOpSubgraph(concatOp, concatNum++);
}
else if (auto extractSliceOp = dyn_cast<tensor::ExtractSliceOp>(op)) {
auto producerOp = extractSliceOp->getOperand(0).getDefiningOp();
if (producerOp) {
// Skip extractSliceOp if producer is constant weights (ONNXConstantOp)
if (llvm::isa<ONNXConstantOp>(producerOp))
continue;
// If produced by tosa::ReshapeOp (i.e. it is a bias tile) connect
// directly to its user, which is not a ComputeOp argument.
if (llvm::isa<tosa::ReshapeOp>(producerOp)) {
drawBiasTileOp(extractSliceOp);
continue;
}
}
drawExtractSliceOp(extractSliceOp);
}
}
// Draw input node, and edges to it users
drawInputNodesAndEdges(func);
// Draw output node (use the return Operation - argument number=0 - as nodeId)
auto returnOp = func.getBody().front().getTerminator();
os << '\t' << FORMAT_ARGUMENT(returnOp, 0) << " [label=\"Module Output\",color=green];\n";
os << "}\n";
}
} // namespace
std::unique_ptr<Pass> createSpatialToGraphvizPass() { return std::make_unique<SpatialToGraphvizPass>(); }
} // namespace onnx_mlir
@@ -141,7 +141,8 @@ collectTopLevelFragmentAssemblyCopies(OpResult result, RankedTensorType packedRe
std::optional<StringRef> mode = blueprint.getMode(); std::optional<StringRef> mode = blueprint.getMode();
std::optional<ArrayRef<int64_t>> operandIndicesAttr = blueprint.getFragmentOperandIndices(); std::optional<ArrayRef<int64_t>> operandIndicesAttr = blueprint.getFragmentOperandIndices();
std::optional<ArrayRef<int64_t>> sourceOffsetsAttr = blueprint.getFragmentSourceOffsets(); std::optional<ArrayRef<int64_t>> sourceOffsetsAttr = blueprint.getFragmentSourceOffsets();
if (!mode || *mode != "fragment_assembly" || !operandIndicesAttr || !sourceOffsetsAttr) std::optional<ArrayRef<int64_t>> sourceSlotsAttr = blueprint.getFragmentSourceSlots();
if (!mode || *mode != "fragment_assembly" || !operandIndicesAttr || !sourceOffsetsAttr || !sourceSlotsAttr)
return failure(); return failure();
if (!blueprint.getOutput().hasOneUse() || !isa<func::ReturnOp>(*blueprint.getOutput().getUsers().begin())) if (!blueprint.getOutput().hasOneUse() || !isa<func::ReturnOp>(*blueprint.getOutput().getUsers().begin()))
return failure(); return failure();
@@ -153,6 +154,9 @@ collectTopLevelFragmentAssemblyCopies(OpResult result, RankedTensorType packedRe
ArrayRef<int64_t> operandIndices = *operandIndicesAttr; ArrayRef<int64_t> operandIndices = *operandIndicesAttr;
ArrayRef<int64_t> sourceOffsets = *sourceOffsetsAttr; ArrayRef<int64_t> sourceOffsets = *sourceOffsetsAttr;
ArrayRef<int64_t> sourceSlots = *sourceSlotsAttr;
if (sourceSlots.size() != operandIndices.size())
return failure();
ArrayRef<int64_t> flatOffsets = blueprint.getFragmentOffsets(); ArrayRef<int64_t> flatOffsets = blueprint.getFragmentOffsets();
ArrayRef<int64_t> flatSizes = blueprint.getFragmentSizes(); ArrayRef<int64_t> flatSizes = blueprint.getFragmentSizes();
ArrayRef<int64_t> flatStrides = *stridesAttr; ArrayRef<int64_t> flatStrides = *stridesAttr;
@@ -174,7 +178,8 @@ collectTopLevelFragmentAssemblyCopies(OpResult result, RankedTensorType packedRe
if (operandIndices[fragmentIndex] != static_cast<int64_t>(use.getOperandNumber())) if (operandIndices[fragmentIndex] != static_cast<int64_t>(use.getOperandNumber()))
continue; continue;
int64_t sourceElementOffset = sourceOffsets[fragmentIndex]; int64_t sourceElementOffset =
sourceSlots[fragmentIndex] * payloadElementCount + sourceOffsets[fragmentIndex];
int64_t lane = sourceElementOffset / payloadElementCount; int64_t lane = sourceElementOffset / payloadElementCount;
if (lane < 0 || lane >= static_cast<int64_t>(laneCount)) if (lane < 0 || lane >= static_cast<int64_t>(laneCount))
return failure(); return failure();
@@ -395,6 +400,11 @@ LogicalResult raptor::SpatialToPimPass::lowerComputeBatchOp(spatial::SpatSchedul
if (isa<spatial::SpatYieldOp>(op)) if (isa<spatial::SpatYieldOp>(op))
continue; continue;
// Cloning a region-bearing operation may leave the rewriter inside that
// region. Every old-block operation is lowered at the core-batch body
// boundary.
rewriter.setInsertionPointToEnd(newBlock);
if (auto blueprint = dyn_cast<spatial::SpatBlueprintOp>(op)) { if (auto blueprint = dyn_cast<spatial::SpatBlueprintOp>(op)) {
std::optional<StringRef> modeAttr = blueprint.getMode(); std::optional<StringRef> modeAttr = blueprint.getMode();
if (modeAttr && *modeAttr == "fragment_assembly") { if (modeAttr && *modeAttr == "fragment_assembly") {
+34 -32
View File
@@ -8,6 +8,8 @@
#include <limits> #include <limits>
#include "Common.hpp" #include "Common.hpp"
#include "src/Accelerators/PIM/Common/IR/AffineUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp" #include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp" #include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
@@ -192,21 +194,23 @@ forEachContiguousDestinationChunk(ArrayRef<int64_t> destShape,
} }
static mlir::Value static mlir::Value
createSteppedOffset(OpBuilder& builder, Location loc, mlir::Value start, mlir::Value index, int64_t stepBytes) { createSteppedOffset(OpBuilder& builder, Location loc, mlir::Value start, mlir::Value index,
int64_t stepBytes, Operation *constantAnchor) {
if (stepBytes == 0) if (stepBytes == 0)
return start; return start;
mlir::Value step = arith::ConstantIndexOp::create(builder, loc, stepBytes); return createOrFoldAffineApply(
mlir::Value scaled = arith::MulIOp::create(builder, loc, index, step).getResult(); builder, loc, builder.getAffineDimExpr(0) + builder.getAffineDimExpr(1) * stepBytes,
return arith::AddIOp::create(builder, loc, start, scaled).getResult(); ValueRange {start, index}, constantAnchor);
} }
static mlir::Value createIndexedOffset(OpBuilder& builder, static mlir::Value createIndexedOffset(OpBuilder& builder,
Location loc, Location loc,
mlir::Value indexArg, mlir::Value indexArg,
ArrayRef<int64_t> values) { ArrayRef<int64_t> values,
Operation *constantAnchor) {
assert(!values.empty() && "expected lane-indexed values"); assert(!values.empty() && "expected lane-indexed values");
if (llvm::all_of(values.drop_front(), [&](int64_t value) { return value == values.front(); })) if (llvm::all_of(values.drop_front(), [&](int64_t value) { return value == values.front(); }))
return arith::ConstantIndexOp::create(builder, loc, values.front()); return getOrCreateIndexConstant(builder, constantAnchor, values.front());
if (values.size() >= 2) { if (values.size() >= 2) {
int64_t step = values[1] - values[0]; int64_t step = values[1] - values[0];
@@ -214,21 +218,18 @@ static mlir::Value createIndexedOffset(OpBuilder& builder,
return values[index] == values.front() + static_cast<int64_t>(index) * step; return values[index] == values.front() + static_cast<int64_t>(index) * step;
}); });
if (arithmetic) { if (arithmetic) {
mlir::Value base = arith::ConstantIndexOp::create(builder, loc, values.front()); return createOrFoldAffineApply(
mlir::Value stepValue = arith::ConstantIndexOp::create(builder, loc, step); builder, loc, builder.getAffineDimExpr(0) * step + values.front(),
mlir::Value scaledIndex = arith::MulIOp::create(builder, loc, indexArg, stepValue).getResult(); ValueRange {indexArg}, constantAnchor);
return arith::AddIOp::create(builder, loc, base, scaledIndex).getResult();
} }
} }
mlir::Value selected = arith::ConstantIndexOp::create(builder, loc, values.front()); RankedTensorType tableType = RankedTensorType::get(
for (auto [lane, value] : llvm::enumerate(values.drop_front())) { {static_cast<int64_t>(values.size())}, builder.getI64Type());
mlir::Value indexValue = arith::ConstantIndexOp::create(builder, loc, static_cast<int64_t>(lane + 1)); DenseElementsAttr tableAttr = DenseElementsAttr::get(tableType, values);
mlir::Value cmp = arith::CmpIOp::create(builder, loc, arith::CmpIPredicate::eq, indexArg, indexValue); mlir::Value table = getOrCreateConstant(builder, constantAnchor, tableAttr, tableType);
mlir::Value candidate = arith::ConstantIndexOp::create(builder, loc, value); mlir::Value selected = tensor::ExtractOp::create(builder, loc, table, ValueRange {indexArg});
selected = arith::SelectOp::create(builder, loc, cmp, candidate, selected); return arith::IndexCastOp::create(builder, loc, builder.getIndexType(), selected).getResult();
}
return selected;
} }
struct FragmentAssemblyCopyRunFamily { struct FragmentAssemblyCopyRunFamily {
@@ -433,11 +434,11 @@ static FailureOr<mlir::Value> emitFragmentAssemblyCopyRun(OpBuilder& builder,
mlir::Value hostStart; mlir::Value hostStart;
mlir::Value sourceStart; mlir::Value sourceStart;
if (laneArg) { if (laneArg) {
hostStart = createIndexedOffset(builder, loc, *laneArg, run.hostStartBytesByLane); hostStart = createIndexedOffset(builder, loc, *laneArg, run.hostStartBytesByLane, anchor);
sourceStart = createIndexedOffset(builder, loc, *laneArg, run.sourceStartBytesByLane); sourceStart = createIndexedOffset(builder, loc, *laneArg, run.sourceStartBytesByLane, anchor);
} else { } else {
hostStart = arith::ConstantIndexOp::create(builder, loc, run.hostStartBytesByLane.front()); hostStart = getOrCreateIndexConstant(builder, anchor, run.hostStartBytesByLane.front());
sourceStart = arith::ConstantIndexOp::create(builder, loc, run.sourceStartBytesByLane.front()); sourceStart = getOrCreateIndexConstant(builder, anchor, run.sourceStartBytesByLane.front());
} }
if (hostRunStartDelta) if (hostRunStartDelta)
@@ -459,9 +460,9 @@ static FailureOr<mlir::Value> emitFragmentAssemblyCopyRun(OpBuilder& builder,
.getOutput(); .getOutput();
} }
mlir::Value lowerBound = arith::ConstantIndexOp::create(builder, loc, 0); mlir::Value lowerBound = getOrCreateIndexConstant(builder, anchor, 0);
mlir::Value upperBound = arith::ConstantIndexOp::create(builder, loc, run.count); mlir::Value upperBound = getOrCreateIndexConstant(builder, anchor, run.count);
mlir::Value step = arith::ConstantIndexOp::create(builder, loc, 1); mlir::Value step = getOrCreateIndexConstant(builder, anchor, 1);
FailureOr<NormalizedLoopResult> loop = buildNormalizedScfFor( FailureOr<NormalizedLoopResult> loop = buildNormalizedScfFor(
builder, builder,
loc, loc,
@@ -474,9 +475,10 @@ static FailureOr<mlir::Value> emitFragmentAssemblyCopyRun(OpBuilder& builder,
mlir::Value flatIndex, mlir::Value flatIndex,
ValueRange iterArgs, ValueRange iterArgs,
SmallVectorImpl<mlir::Value>& yielded) { SmallVectorImpl<mlir::Value>& yielded) {
mlir::Value hostOffset = createSteppedOffset(loopBuilder, bodyLoc, hostStart, flatIndex, run.hostStepBytes); mlir::Value hostOffset = createSteppedOffset(
loopBuilder, bodyLoc, hostStart, flatIndex, run.hostStepBytes, anchor);
mlir::Value sourceOffset = mlir::Value sourceOffset =
createSteppedOffset(loopBuilder, bodyLoc, sourceStart, flatIndex, run.sourceStepBytes); createSteppedOffset(loopBuilder, bodyLoc, sourceStart, flatIndex, run.sourceStepBytes, anchor);
mlir::Value copied = mlir::Value copied =
pim::PimMemCopyDevToHostOp::create(loopBuilder, pim::PimMemCopyDevToHostOp::create(loopBuilder,
bodyLoc, bodyLoc,
@@ -506,9 +508,9 @@ static FailureOr<mlir::Value> emitFragmentAssemblyCopyRunFamily(OpBuilder& build
return emitFragmentAssemblyCopyRun( return emitFragmentAssemblyCopyRun(
builder, loc, family.prototype, hostTarget, anchor, laneArg, baseHostOffset); builder, loc, family.prototype, hostTarget, anchor, laneArg, baseHostOffset);
mlir::Value lowerBound = arith::ConstantIndexOp::create(builder, loc, 0); mlir::Value lowerBound = getOrCreateIndexConstant(builder, anchor, 0);
mlir::Value upperBound = arith::ConstantIndexOp::create(builder, loc, family.sourceRunStartDeltas.size()); mlir::Value upperBound = getOrCreateIndexConstant(builder, anchor, family.sourceRunStartDeltas.size());
mlir::Value step = arith::ConstantIndexOp::create(builder, loc, 1); mlir::Value step = getOrCreateIndexConstant(builder, anchor, 1);
FailureOr<NormalizedLoopResult> outerLoop = buildNormalizedScfFor( FailureOr<NormalizedLoopResult> outerLoop = buildNormalizedScfFor(
builder, builder,
loc, loc,
@@ -522,9 +524,9 @@ static FailureOr<mlir::Value> emitFragmentAssemblyCopyRunFamily(OpBuilder& build
ValueRange iterArgs, ValueRange iterArgs,
SmallVectorImpl<mlir::Value>& yielded) { SmallVectorImpl<mlir::Value>& yielded) {
mlir::Value sourceRunStartDelta = mlir::Value sourceRunStartDelta =
createIndexedOffset(loopBuilder, bodyLoc, runIndex, family.sourceRunStartDeltas); createIndexedOffset(loopBuilder, bodyLoc, runIndex, family.sourceRunStartDeltas, anchor);
mlir::Value hostRunStartDelta = mlir::Value hostRunStartDelta =
createIndexedOffset(loopBuilder, bodyLoc, runIndex, family.hostRunStartDeltas); createIndexedOffset(loopBuilder, bodyLoc, runIndex, family.hostRunStartDeltas, anchor);
FailureOr<mlir::Value> copied = emitFragmentAssemblyCopyRun(loopBuilder, FailureOr<mlir::Value> copied = emitFragmentAssemblyCopyRun(loopBuilder,
bodyLoc, bodyLoc,
family.prototype, family.prototype,
@@ -10,7 +10,9 @@
#include "Conversion/ONNXToSpatial/Common/Common.hpp" #include "Conversion/ONNXToSpatial/Common/Common.hpp"
#include "Conversion/SpatialToPim/SpatialToPimPass.hpp" #include "Conversion/SpatialToPim/SpatialToPimPass.hpp"
#include "src/Accelerators/PIM/Common/IR/BatchCoreUtils.hpp" #include "src/Accelerators/PIM/Common/IR/BatchCoreUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp" #include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapingUtils.hpp"
#include "src/Accelerators/PIM/Common/PimCommon.hpp" #include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp" #include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
#include "src/Accelerators/PIM/Conversion/SpatialToPim/Common.hpp" #include "src/Accelerators/PIM/Conversion/SpatialToPim/Common.hpp"
@@ -180,16 +182,79 @@ static LogicalResult collectHelperComputeChain(spatial::SpatScheduledCompute com
return success(); return success();
} }
static bool isHostMaterializableHelperOp(Operation* op) {
if (isa<spatial::SpatYieldOp>(op))
return true;
if (isa<arith::ConstantOp>(op) || op->hasTrait<OpTrait::ConstantLike>())
return true;
if (auto blueprint = dyn_cast<spatial::SpatBlueprintOp>(op)) {
std::optional<StringRef> mode = blueprint.getMode();
return mode && *mode == "fragment_assembly";
}
return isShapingOnlyOp(op) || isPureIndexComputationOp(op);
}
static FailureOr<DenseMap<Value, Attribute>>
analyzeHostMaterializableHelper(spatial::SpatScheduledCompute computeOp) {
DenseMap<Value, Attribute> folded;
for (auto [weightIndex, weight] : llvm::enumerate(computeOp.getWeights())) {
auto argument = computeOp.getWeightArgument(weightIndex);
if (!argument)
return failure();
Attribute constant;
if (matchPattern(weight, m_Constant(&constant)))
folded[*argument] = constant;
}
Block& block = computeOp.getBody().front();
for (Operation& op : block) {
if (!isHostMaterializableHelperOp(&op))
return failure();
if (isa<spatial::SpatYieldOp, spatial::SpatBlueprintOp>(op)
|| (isShapingOnlyOp(&op) && !isPureIndexComputationOp(&op)))
continue;
if (isa<arith::ConstantOp>(op) || op.hasTrait<OpTrait::ConstantLike>()) {
for (Value result : op.getResults()) {
Attribute constant;
if (!matchPattern(result, m_Constant(&constant)))
return failure();
folded[result] = constant;
}
continue;
}
if (!isPureIndexComputationOp(&op) || op.getNumRegions() != 0)
return failure();
SmallVector<Attribute> operands;
for (Value operand : op.getOperands()) {
auto it = folded.find(operand);
if (it == folded.end())
return failure();
operands.push_back(it->second);
}
SmallVector<OpFoldResult> results;
if (failed(op.fold(operands, results))
|| results.size() != op.getNumResults())
return failure();
for (auto [result, foldResult] : llvm::zip(op.getResults(), results)) {
auto attribute = dyn_cast<Attribute>(foldResult);
if (!attribute)
return failure();
folded[result] = attribute;
}
}
return folded;
}
static bool inlineInputlessHelperComputeForWeightLikeUsers(spatial::SpatScheduledCompute computeOp, static bool inlineInputlessHelperComputeForWeightLikeUsers(spatial::SpatScheduledCompute computeOp,
IRRewriter& rewriter, IRRewriter& rewriter,
OperationFolder& constantFolder) { OperationFolder& constantFolder) {
if (!computeOp.getInputs().empty() || computeOp.getNumResults() != 1) if (!computeOp.getInputs().empty() || computeOp.getNumResults() != 1)
return false; return false;
if (computeOp.getResult(0).use_empty())
return false;
if (!llvm::all_of(computeOp.getResult(0).getUsers(), [](Operation* user) { if (!llvm::all_of(computeOp.getResult(0).getUsers(), [](Operation* user) {
return isa<spatial::SpatScheduledCompute, spatial::SpatScheduledComputeBatch, pim::PimCoreOp, pim::PimCoreBatchOp>(user); return isa<spatial::SpatScheduledCompute, spatial::SpatScheduledComputeBatch, pim::PimCoreOp, pim::PimCoreBatchOp>(user);
})) }))
return false; return false;
Block& block = computeOp.getBody().front(); Block& block = computeOp.getBody().front();
if (block.getNumArguments() != computeOp.getWeights().size()) if (block.getNumArguments() != computeOp.getWeights().size())
return false; return false;
@@ -197,6 +262,9 @@ static bool inlineInputlessHelperComputeForWeightLikeUsers(spatial::SpatSchedule
auto yieldOp = dyn_cast<spatial::SpatYieldOp>(block.getTerminator()); auto yieldOp = dyn_cast<spatial::SpatYieldOp>(block.getTerminator());
if (!yieldOp || yieldOp.getNumOperands() != 1) if (!yieldOp || yieldOp.getNumOperands() != 1)
return false; return false;
auto folded = analyzeHostMaterializableHelper(computeOp);
if (failed(folded))
return false;
rewriter.setInsertionPoint(computeOp); rewriter.setInsertionPoint(computeOp);
IRMapping mapping; IRMapping mapping;
@@ -218,6 +286,20 @@ static bool inlineInputlessHelperComputeForWeightLikeUsers(spatial::SpatSchedule
} }
} }
if (isa<arith::ConstantOp>(op) || op.hasTrait<OpTrait::ConstantLike>()
|| isPureIndexComputationOp(&op)) {
for (Value result : op.getResults()) {
auto it = folded->find(result);
if (it == folded->end())
return false;
mapping.map(
result,
getOrCreateConstant(constantFolder, computeOp, it->second,
result.getType()));
}
continue;
}
cloneMappedHelperOperands(&op, mapping, rewriter, constantFolder); cloneMappedHelperOperands(&op, mapping, rewriter, constantFolder);
Operation* clonedOp = rewriter.clone(op, mapping); Operation* clonedOp = rewriter.clone(op, mapping);
for (auto [originalResult, newResult] : llvm::zip(op.getResults(), clonedOp->getResults())) for (auto [originalResult, newResult] : llvm::zip(op.getResults(), clonedOp->getResults()))
@@ -1,10 +1,23 @@
#include "Dialect/Pim/Transforms/Bufferization/Common.hpp" #include "Dialect/Pim/Transforms/Bufferization/Common.hpp"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "src/Accelerators/PIM/Common/PimCommon.hpp" #include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp" #include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
#include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp"
using namespace mlir; using namespace mlir;
static SmallVector<Region *> getSelectionRegions(OpResult result) {
SmallVector<Region *> regions;
if (auto selection = dyn_cast<scf::IndexSwitchOp>(result.getOwner()))
for (Region &region : selection->getRegions())
regions.push_back(&region);
else if (auto selection = dyn_cast<scf::IfOp>(result.getOwner())) {
regions.push_back(&selection.getThenRegion());
regions.push_back(&selection.getElseRegion());
}
return regions;
}
static bool isCoreBatchInputArgument(Value value) { static bool isCoreBatchInputArgument(Value value) {
auto blockArg = dyn_cast<BlockArgument>(value); auto blockArg = dyn_cast<BlockArgument>(value);
if (!blockArg) if (!blockArg)
@@ -92,20 +105,46 @@ FailureOr<Value> onnx_mlir::pim::getPimAddressBase(Value value, const StaticValu
} }
bool onnx_mlir::pim::isHostBackedPimAddress(Value value, const StaticValueKnowledge& knowledge) { bool onnx_mlir::pim::isHostBackedPimAddress(Value value, const StaticValueKnowledge& knowledge) {
auto base = getPimStorageBase(value, knowledge); llvm::SmallPtrSet<Value, 8> visited;
if (failed(base)) std::function<bool(Value)> isHost = [&](Value current) {
return false; auto base = getPimStorageBase(current, knowledge);
if (failed(base) || !visited.insert(*base).second)
if (isCoreBatchInputArgument(*base)) return false;
return true; bool resultIsHost = isCoreBatchInputArgument(*base)
|| isa_and_nonnull<memref::GetGlobalOp>(base->getDefiningOp());
return isa_and_nonnull<memref::GetGlobalOp>(base->getDefiningOp()); auto result = dyn_cast<OpResult>(*base);
SmallVector<Region *> regions = result ? getSelectionRegions(result)
: SmallVector<Region *>();
if (!resultIsHost && !regions.empty())
resultIsHost = llvm::all_of(regions, [&](Region *region) {
auto yield = dyn_cast<scf::YieldOp>(region->front().getTerminator());
return yield && result.getResultNumber() < yield.getNumOperands()
&& isHost(yield.getOperand(result.getResultNumber()));
});
visited.erase(*base);
return resultIsHost;
};
return isHost(value);
} }
bool onnx_mlir::pim::isDeviceLocalPimAddress(Value value, const StaticValueKnowledge& knowledge) { bool onnx_mlir::pim::isDeviceLocalPimAddress(Value value, const StaticValueKnowledge& knowledge) {
auto base = getPimStorageBase(value, knowledge); llvm::SmallPtrSet<Value, 8> visited;
if (failed(base)) std::function<bool(Value)> isDevice = [&](Value current) {
return false; auto base = getPimStorageBase(current, knowledge);
if (failed(base) || !visited.insert(*base).second)
return isa_and_nonnull<memref::AllocOp>(base->getDefiningOp()); return false;
bool resultIsDevice = isa_and_nonnull<memref::AllocOp>(base->getDefiningOp());
auto result = dyn_cast<OpResult>(*base);
SmallVector<Region *> regions = result ? getSelectionRegions(result)
: SmallVector<Region *>();
if (!resultIsDevice && !regions.empty())
resultIsDevice = llvm::all_of(regions, [&](Region *region) {
auto yield = dyn_cast<scf::YieldOp>(region->front().getTerminator());
return yield && result.getResultNumber() < yield.getNumOperands()
&& isDevice(yield.getOperand(result.getResultNumber()));
});
visited.erase(*base);
return resultIsDevice;
};
return isDevice(value);
} }
@@ -2,6 +2,8 @@
#include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h" #include "mlir/Dialect/SCF/IR/SCF.h"
#include "llvm/Support/MathExtras.h"
#include "ContiguityPatterns.hpp" #include "ContiguityPatterns.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp" #include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp" #include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp"
@@ -33,6 +35,7 @@ struct CopyEndpointPlan {
struct CopyLoopPlan { struct CopyLoopPlan {
SmallVector<int64_t> outerShape; SmallVector<int64_t> outerShape;
int64_t outerElements = 0;
int64_t chunkBytes = 0; int64_t chunkBytes = 0;
ByteOffsetExpr targetBaseOffset; ByteOffsetExpr targetBaseOffset;
ByteOffsetExpr sourceBaseOffset; ByteOffsetExpr sourceBaseOffset;
@@ -74,6 +77,24 @@ static void appendTerm(ByteOffsetExpr& expr, Value value, int64_t scale) {
expr.terms.push_back(ByteOffsetTerm {value, scale}); expr.terms.push_back(ByteOffsetTerm {value, scale});
} }
static FailureOr<int64_t> checkedPositiveMul(int64_t lhs, int64_t rhs) {
int64_t result = 0;
if (lhs < 0 || rhs < 0 || llvm::MulOverflow(lhs, rhs, result))
return failure();
return result;
}
static FailureOr<int64_t> checkedPositiveProduct(ArrayRef<int64_t> values) {
int64_t result = 1;
for (int64_t value : values) {
auto product = checkedPositiveMul(result, value);
if (failed(product))
return failure();
result = *product;
}
return result;
}
static FailureOr<SmallVector<int64_t>> getStaticMemRefStrides(MemRefType type) { static FailureOr<SmallVector<int64_t>> getStaticMemRefStrides(MemRefType type) {
SmallVector<int64_t> strides; SmallVector<int64_t> strides;
int64_t offset = 0; int64_t offset = 0;
@@ -84,6 +105,165 @@ static FailureOr<SmallVector<int64_t>> getStaticMemRefStrides(MemRefType type) {
return strides; return strides;
} }
static FailureOr<SmallVector<int64_t>> getProvenMemRefStrides(Value value) {
llvm::SmallPtrSet<Value, 8> visiting;
std::function<FailureOr<SmallVector<int64_t>>(Value)> prove =
[&](Value current) -> FailureOr<SmallVector<int64_t>> {
auto type = dyn_cast<MemRefType>(current.getType());
if (!type || !visiting.insert(current).second)
return failure();
if (auto strides = getStaticMemRefStrides(type); succeeded(strides)) {
visiting.erase(current);
return strides;
}
if (auto castOp = current.getDefiningOp<memref::CastOp>()) {
auto strides = prove(castOp.getSource());
visiting.erase(current);
return strides;
}
if (auto subview = current.getDefiningOp<memref::SubViewOp>()) {
auto sourceStrides = prove(subview.getSource());
if (failed(sourceStrides) || subview.getSourceType().getRank() != subview.getType().getRank()) {
visiting.erase(current);
return failure();
}
SmallVector<int64_t> strides;
for (auto [sourceStride, viewStride] :
llvm::zip_equal(*sourceStrides, subview.getStaticStrides())) {
if (ShapedType::isDynamic(viewStride) || viewStride < 0) {
visiting.erase(current);
return failure();
}
auto stride = checkedPositiveMul(sourceStride, viewStride);
if (failed(stride)) {
visiting.erase(current);
return failure();
}
strides.push_back(*stride);
}
visiting.erase(current);
return strides;
}
if (auto expand = current.getDefiningOp<memref::ExpandShapeOp>()) {
auto sourceStrides = prove(expand.getSrc());
auto resultType = dyn_cast<MemRefType>(expand.getResult().getType());
auto sourceType = dyn_cast<MemRefType>(expand.getSrc().getType());
if (failed(sourceStrides) || !sourceType || !resultType
|| !resultType.hasStaticShape()
|| sourceStrides->size() != static_cast<size_t>(sourceType.getRank())
|| llvm::any_of(resultType.getShape(), [](int64_t dim) {
return dim <= 0;
})) {
visiting.erase(current);
return failure();
}
SmallVector<int64_t> strides(resultType.getRank());
SmallVector<bool> assigned(resultType.getRank(), false);
for (auto [sourceDim, group] :
llvm::enumerate(expand.getReassociationIndices())) {
if (sourceDim >= sourceStrides->size() || group.empty()) {
visiting.erase(current);
return failure();
}
int64_t stride = (*sourceStrides)[sourceDim];
for (int64_t resultDim : llvm::reverse(group)) {
if (resultDim < 0 || resultDim >= resultType.getRank()
|| assigned[resultDim]) {
visiting.erase(current);
return failure();
}
strides[resultDim] = stride;
assigned[resultDim] = true;
auto nextStride = checkedPositiveMul(
stride, resultType.getDimSize(resultDim));
if (failed(nextStride)) {
visiting.erase(current);
return failure();
}
stride = *nextStride;
}
}
if (llvm::is_contained(assigned, false)) {
visiting.erase(current);
return failure();
}
visiting.erase(current);
return strides;
}
if (auto collapse = current.getDefiningOp<memref::CollapseShapeOp>()) {
auto sourceStrides = prove(collapse.getSrc());
auto sourceType = dyn_cast<MemRefType>(collapse.getSrc().getType());
if (failed(sourceStrides) || !sourceType
|| !sourceType.hasStaticShape()
|| sourceStrides->size() != static_cast<size_t>(sourceType.getRank())) {
visiting.erase(current);
return failure();
}
SmallVector<int64_t> strides;
for (ArrayRef<int64_t> group : collapse.getReassociationIndices()) {
if (group.empty()) {
visiting.erase(current);
return failure();
}
for (int64_t dim : group)
if (dim < 0 || dim >= sourceType.getRank()
|| sourceType.getDimSize(dim) <= 0
|| (*sourceStrides)[dim] < 0) {
visiting.erase(current);
return failure();
}
for (auto pair : llvm::zip(group.drop_back(), group.drop_front())) {
int64_t outer = std::get<0>(pair);
int64_t inner = std::get<1>(pair);
auto expectedOuterStride = checkedPositiveMul(
(*sourceStrides)[inner], sourceType.getDimSize(inner));
if (failed(expectedOuterStride)
|| (*sourceStrides)[outer] != *expectedOuterStride) {
visiting.erase(current);
return failure();
}
}
strides.push_back((*sourceStrides)[group.back()]);
}
visiting.erase(current);
return strides;
}
auto result = dyn_cast<OpResult>(current);
SmallVector<Region *> regions;
if (result) {
if (auto selection = dyn_cast<scf::IndexSwitchOp>(result.getOwner()))
for (Region &region : selection->getRegions())
regions.push_back(&region);
else if (auto selection = dyn_cast<scf::IfOp>(result.getOwner())) {
regions.push_back(&selection.getThenRegion());
regions.push_back(&selection.getElseRegion());
}
}
if (regions.empty()) {
visiting.erase(current);
return failure();
}
std::optional<SmallVector<int64_t>> common;
for (Region *region : regions) {
auto yield = dyn_cast<scf::YieldOp>(region->front().getTerminator());
if (!yield || result.getResultNumber() >= yield.getNumOperands()) {
visiting.erase(current);
return failure();
}
auto strides = prove(yield.getOperand(result.getResultNumber()));
if (failed(strides) || (common && *common != *strides)) {
visiting.erase(current);
return failure();
}
common = std::move(*strides);
}
visiting.erase(current);
return common ? FailureOr<SmallVector<int64_t>>(std::move(*common))
: FailureOr<SmallVector<int64_t>>(failure());
};
return prove(value);
}
static FailureOr<int64_t> getShapedByteSize(MemRefType type) { static FailureOr<int64_t> getShapedByteSize(MemRefType type) {
if (!type.hasStaticShape() || !hasByteSizedElementType(type.getElementType())) if (!type.hasStaticShape() || !hasByteSizedElementType(type.getElementType()))
return failure(); return failure();
@@ -119,12 +299,15 @@ inferLogicalCopyShape(MemRefType targetType, MemRefType sourceType, int64_t size
return failure(); return failure();
} }
static FailureOr<int64_t> getContiguousSuffixRank(MemRefType type, ArrayRef<int64_t> copyShape) { static FailureOr<int64_t> getContiguousSuffixRank(Value value, ArrayRef<int64_t> copyShape) {
if (!type.hasStaticShape() || !hasByteSizedElementType(type.getElementType()) auto type = dyn_cast<MemRefType>(value.getType());
if (!type || !type.hasStaticShape() || !hasByteSizedElementType(type.getElementType())
|| type.getRank() != static_cast<int64_t>(copyShape.size())) || type.getRank() != static_cast<int64_t>(copyShape.size()))
return failure(); return failure();
if (llvm::any_of(copyShape, [](int64_t dim) { return dim <= 0; }))
return failure();
auto strides = getStaticMemRefStrides(type); auto strides = getProvenMemRefStrides(value);
if (failed(strides)) if (failed(strides))
return failure(); return failure();
@@ -134,7 +317,10 @@ static FailureOr<int64_t> getContiguousSuffixRank(MemRefType type, ArrayRef<int6
if ((*strides)[dim] != expectedStride) if ((*strides)[dim] != expectedStride)
break; break;
++contiguousSuffixRank; ++contiguousSuffixRank;
expectedStride *= copyShape[dim]; auto nextStride = checkedPositiveMul(expectedStride, copyShape[dim]);
if (failed(nextStride))
return failure();
expectedStride = *nextStride;
} }
return contiguousSuffixRank; return contiguousSuffixRank;
} }
@@ -174,18 +360,25 @@ static FailureOr<CopyEndpointPlan> analyzeCopyEndpoint(Value value, Value initia
if (!sourceType || !sourceType.hasStaticShape() || !hasByteSizedElementType(sourceType.getElementType())) if (!sourceType || !sourceType.hasStaticShape() || !hasByteSizedElementType(sourceType.getElementType()))
return failure(); return failure();
auto sourceStrides = getStaticMemRefStrides(sourceType); auto sourceStrides = getProvenMemRefStrides(subviewOp.getSource());
if (failed(sourceStrides)) if (failed(sourceStrides))
return failure(); return failure();
int64_t elementByteWidth = static_cast<int64_t>(getElementTypeSizeInBytes(sourceType.getElementType())); int64_t elementByteWidth = static_cast<int64_t>(getElementTypeSizeInBytes(sourceType.getElementType()));
for (auto [offset, stride] : llvm::zip_equal(subviewOp.getMixedOffsets(), *sourceStrides)) { for (auto [offset, stride] : llvm::zip_equal(subviewOp.getMixedOffsets(), *sourceStrides)) {
int64_t byteScale = stride * elementByteWidth; auto byteScale = checkedPositiveMul(stride, elementByteWidth);
if (failed(byteScale))
return failure();
if (auto attr = dyn_cast<Attribute>(offset)) { if (auto attr = dyn_cast<Attribute>(offset)) {
endpoint.offset.constant += cast<IntegerAttr>(attr).getInt() * byteScale; auto constantOffset = checkedPositiveMul(
cast<IntegerAttr>(attr).getInt(), *byteScale);
if (failed(constantOffset)
|| llvm::AddOverflow(endpoint.offset.constant, *constantOffset,
endpoint.offset.constant))
return failure();
continue; continue;
} }
appendTerm(endpoint.offset, cast<Value>(offset), byteScale); appendTerm(endpoint.offset, cast<Value>(offset), *byteScale);
} }
endpoint.base = subviewOp.getSource(); endpoint.base = subviewOp.getSource();
@@ -204,17 +397,34 @@ analyzeCopyRewrite(Value target, Value source, Value targetOffset, Value sourceO
if (!targetType || !sourceType || size <= 0) if (!targetType || !sourceType || size <= 0)
return failure(); return failure();
auto logicalCopyShape = inferLogicalCopyShape(targetType, sourceType, size);
if (failed(logicalCopyShape))
return failure();
auto targetPlan = analyzeCopyEndpoint(target, targetOffset, targetType); auto targetPlan = analyzeCopyEndpoint(target, targetOffset, targetType);
auto sourcePlan = analyzeCopyEndpoint(source, sourceOffset, sourceType); auto sourcePlan = analyzeCopyEndpoint(source, sourceOffset, sourceType);
if (failed(targetPlan) || failed(sourcePlan)) if (failed(targetPlan) || failed(sourcePlan))
return failure(); return failure();
auto targetSuffixRank = getContiguousSuffixRank(targetType, *logicalCopyShape); auto targetBytes = getShapedByteSize(targetType);
auto sourceSuffixRank = getContiguousSuffixRank(sourceType, *logicalCopyShape); auto sourceBytes = getShapedByteSize(sourceType);
if (targetType.getElementType() == sourceType.getElementType() && succeeded(targetBytes) && succeeded(sourceBytes)
&& *targetBytes == size && *sourceBytes == size) {
auto targetSuffixRank = getContiguousSuffixRank(target, targetType.getShape());
auto sourceSuffixRank = getContiguousSuffixRank(source, sourceType.getShape());
if (succeeded(targetSuffixRank) && succeeded(sourceSuffixRank)
&& *targetSuffixRank == targetType.getRank() && *sourceSuffixRank == sourceType.getRank()) {
CopyRewritePlan plan;
plan.kind = CopyRewritePlan::Kind::Direct;
plan.target = *targetPlan;
plan.source = *sourcePlan;
plan.directBytes = size;
return plan;
}
}
auto logicalCopyShape = inferLogicalCopyShape(targetType, sourceType, size);
if (failed(logicalCopyShape))
return failure();
auto targetSuffixRank = getContiguousSuffixRank(target, *logicalCopyShape);
auto sourceSuffixRank = getContiguousSuffixRank(source, *logicalCopyShape);
if (failed(targetSuffixRank) || failed(sourceSuffixRank)) if (failed(targetSuffixRank) || failed(sourceSuffixRank))
return failure(); return failure();
@@ -229,8 +439,8 @@ analyzeCopyRewrite(Value target, Value source, Value targetOffset, Value sourceO
return plan; return plan;
} }
auto targetStrides = getStaticMemRefStrides(targetType); auto targetStrides = getProvenMemRefStrides(target);
auto sourceStrides = getStaticMemRefStrides(sourceType); auto sourceStrides = getProvenMemRefStrides(source);
if (failed(targetStrides) || failed(sourceStrides)) if (failed(targetStrides) || failed(sourceStrides))
return failure(); return failure();
@@ -240,11 +450,27 @@ analyzeCopyRewrite(Value target, Value source, Value targetOffset, Value sourceO
plan.loop.sourceBaseOffset = plan.source.offset; plan.loop.sourceBaseOffset = plan.source.offset;
plan.loop.outerShape.assign(logicalCopyShape->begin(), logicalCopyShape->end() - contiguousSuffixRank); plan.loop.outerShape.assign(logicalCopyShape->begin(), logicalCopyShape->end() - contiguousSuffixRank);
SmallVector<int64_t> chunkShape(logicalCopyShape->end() - contiguousSuffixRank, logicalCopyShape->end()); SmallVector<int64_t> chunkShape(logicalCopyShape->end() - contiguousSuffixRank, logicalCopyShape->end());
plan.loop.chunkBytes = getNumElements(chunkShape) * elementByteWidth; auto outerElements = checkedPositiveProduct(plan.loop.outerShape);
for (int64_t stride : ArrayRef<int64_t>(*targetStrides).take_front(plan.loop.outerShape.size())) auto chunkElements = checkedPositiveProduct(chunkShape);
plan.loop.targetOuterByteStrides.push_back(stride * elementByteWidth); auto chunkBytes = failed(chunkElements)
for (int64_t stride : ArrayRef<int64_t>(*sourceStrides).take_front(plan.loop.outerShape.size())) ? FailureOr<int64_t>(failure())
plan.loop.sourceOuterByteStrides.push_back(stride * elementByteWidth); : checkedPositiveMul(*chunkElements, elementByteWidth);
if (failed(outerElements) || failed(chunkBytes))
return failure();
plan.loop.outerElements = *outerElements;
plan.loop.chunkBytes = *chunkBytes;
for (int64_t stride : ArrayRef<int64_t>(*targetStrides).take_front(plan.loop.outerShape.size())) {
auto byteStride = checkedPositiveMul(stride, elementByteWidth);
if (failed(byteStride))
return failure();
plan.loop.targetOuterByteStrides.push_back(*byteStride);
}
for (int64_t stride : ArrayRef<int64_t>(*sourceStrides).take_front(plan.loop.outerShape.size())) {
auto byteStride = checkedPositiveMul(stride, elementByteWidth);
if (failed(byteStride))
return failure();
plan.loop.sourceOuterByteStrides.push_back(*byteStride);
}
if (plan.loop.chunkBytes <= 0) if (plan.loop.chunkBytes <= 0)
return failure(); return failure();
return plan; return plan;
@@ -344,7 +570,7 @@ static LogicalResult rewriteCopyLikeOp(CopyOp copyOp,
} }
Value c0 = createIndexConstant(rewriter, anchorOp, 0); Value c0 = createIndexConstant(rewriter, anchorOp, 0);
Value cUpper = createIndexConstant(rewriter, anchorOp, getNumElements(plan->loop.outerShape)); Value cUpper = createIndexConstant(rewriter, anchorOp, plan->loop.outerElements);
Value cStep = createIndexConstant(rewriter, anchorOp, 1); Value cStep = createIndexConstant(rewriter, anchorOp, 1);
auto loop = buildNormalizedScfFor( auto loop = buildNormalizedScfFor(
rewriter, rewriter,
@@ -302,76 +302,87 @@ void PimBufferizationPass::annotateWeightsMemrefs(ModuleOp moduleOp, func::FuncO
LogicalResult PimBufferizationPass::verifyContiguousRuntimeOperands(ModuleOp moduleOp) const { LogicalResult PimBufferizationPass::verifyContiguousRuntimeOperands(ModuleOp moduleOp) const {
bool hasFailure = false; bool hasFailure = false;
moduleOp.walk([&](Operation* op) {
auto verifyOperand = [&](Value operand, unsigned operandIndex) {
if (!isa<BaseMemRefType>(operand.getType()))
return;
if (succeeded(resolveContiguousAddress(operand)) || succeeded(compileContiguousAddressExpr(operand)))
return;
op->emitOpError() << "operand #" << operandIndex
<< " is not backed by contiguous addressable storage after PIM bufferization";
hasFailure = true;
};
if (auto memCopyOp = dyn_cast<PimMemCopyOp>(op)) { auto verifyWithKnowledge = [&](auto coreLikeOp, const StaticValueKnowledge& initialKnowledge) {
if (!pim::isNormalizedCopyOp(memCopyOp)) { (void) walkPimCoreBlockStructurally(
memCopyOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization"); coreLikeOp.getBody().front(), initialKnowledge, [&](Operation& op, const StaticValueKnowledge& knowledge) {
hasFailure = true; auto verifyOperand = [&](Value operand, unsigned operandIndex) {
} if (!isa<BaseMemRefType>(operand.getType()))
verifyOperand(memCopyOp.getTarget(), 0); return;
verifyOperand(memCopyOp.getSource(), 1); if (succeeded(resolveContiguousAddress(operand, knowledge)) || succeeded(compileContiguousAddressExpr(operand)))
return; return;
} op.emitOpError() << "operand #" << operandIndex
if (auto loadOp = dyn_cast<PimMemCopyHostToDevOp>(op)) { << " is not backed by contiguous addressable storage after PIM bufferization";
if (!pim::isNormalizedCopyOp(loadOp)) { hasFailure = true;
loadOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization"); };
hasFailure = true;
} if (auto memCopyOp = dyn_cast<PimMemCopyOp>(&op)) {
verifyOperand(loadOp.getDeviceTarget(), 2); if (!pim::isNormalizedCopyOp(memCopyOp)) {
verifyOperand(loadOp.getHostSource(), 3); memCopyOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization");
return; hasFailure = true;
} }
if (auto storeOp = dyn_cast<PimMemCopyDevToHostOp>(op)) { verifyOperand(memCopyOp.getTarget(), 0);
if (!pim::isNormalizedCopyOp(storeOp)) { verifyOperand(memCopyOp.getSource(), 1);
storeOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization"); return success();
hasFailure = true; }
} if (auto loadOp = dyn_cast<PimMemCopyHostToDevOp>(&op)) {
verifyOperand(storeOp.getHostTarget(), 2); if (!pim::isNormalizedCopyOp(loadOp)) {
verifyOperand(storeOp.getDeviceSource(), 3); loadOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization");
return; hasFailure = true;
} }
if (auto sendOp = dyn_cast<PimSendOp>(op)) { verifyOperand(loadOp.getDeviceTarget(), 2);
verifyOperand(sendOp.getInput(), 0); verifyOperand(loadOp.getHostSource(), 3);
return; return success();
} }
if (auto receiveOp = dyn_cast<PimReceiveOp>(op)) { if (auto storeOp = dyn_cast<PimMemCopyDevToHostOp>(&op)) {
verifyOperand(receiveOp.getOutputBuffer(), 0); if (!pim::isNormalizedCopyOp(storeOp)) {
return; storeOp.emitOpError("must use base memref operands plus explicit byte offsets after bufferization");
} hasFailure = true;
if (auto concatOp = dyn_cast<PimConcatOp>(op)) { }
verifyOperand(concatOp.getOutputBuffer(), 0); verifyOperand(storeOp.getHostTarget(), 2);
for (auto inputAndIndex : llvm::enumerate(concatOp.getInputs())) verifyOperand(storeOp.getDeviceSource(), 3);
verifyOperand(inputAndIndex.value(), inputAndIndex.index() + 1); return success();
return; }
} if (auto sendOp = dyn_cast<PimSendOp>(&op)) {
if (isa<PimTransposeOp, verifyOperand(sendOp.getInput(), 0);
PimVMMOp, return success();
PimVVAddOp, }
PimVVSubOp, if (auto receiveOp = dyn_cast<PimReceiveOp>(&op)) {
PimVVMulOp, verifyOperand(receiveOp.getOutputBuffer(), 0);
PimVVMaxOp, return success();
PimVVDMulOp, }
PimVAvgOp, if (auto concatOp = dyn_cast<PimConcatOp>(&op)) {
PimVReluOp, verifyOperand(concatOp.getOutputBuffer(), 0);
PimVTanhOp, for (auto inputAndIndex : llvm::enumerate(concatOp.getInputs()))
PimVSigmOp, verifyOperand(inputAndIndex.value(), inputAndIndex.index() + 1);
PimVSoftmaxOp>(op)) { return success();
for (auto operandAndIndex : llvm::enumerate(op->getOperands())) { }
if (auto vmmOp = dyn_cast<PimVMMOp>(op); vmmOp && operandAndIndex.index() == 0) if (isa<PimTransposeOp,
continue; PimVMMOp,
verifyOperand(operandAndIndex.value(), operandAndIndex.index()); PimVVAddOp,
} PimVVSubOp,
} PimVVMulOp,
PimVVMaxOp,
PimVVDMulOp,
PimVAvgOp,
PimVReluOp,
PimVTanhOp,
PimVSigmOp,
PimVSoftmaxOp>(&op)) {
for (auto operandAndIndex : llvm::enumerate(op.getOperands())) {
if (auto vmmOp = dyn_cast<PimVMMOp>(&op); vmmOp && operandAndIndex.index() == 0)
continue;
verifyOperand(operandAndIndex.value(), operandAndIndex.index());
}
}
return success();
});
};
moduleOp.walk([&](pim::PimCoreOp coreOp) { verifyWithKnowledge(coreOp, seedCoreKnowledge(coreOp)); });
moduleOp.walk([&](pim::PimCoreBatchOp coreBatchOp) {
StaticValueKnowledge knowledge = seedCoreBatchKnowledge(coreBatchOp, 0);
verifyWithKnowledge(coreBatchOp, knowledge);
}); });
if (hasFailure) { if (hasFailure) {
+9 -4
View File
@@ -7,12 +7,17 @@ add_pim_library(SpatialOps
SpatialOpsVerify.cpp SpatialOpsVerify.cpp
SpatialOpsCanonicalization.cpp SpatialOpsCanonicalization.cpp
${PIM_SRC_ROOT}/Conversion/ONNXToSpatial/CompileTime.cpp ${PIM_SRC_ROOT}/Conversion/ONNXToSpatial/CompileTime.cpp
Transforms/MergeComputeNodes/MergeComputeNodesPass.cpp
Transforms/MergeComputeNodes/HostOutputFinalization.cpp
Transforms/MergeComputeNodes/MaterializeMergeSchedule.cpp
Transforms/MergeComputeNodes/ProjectedFragments.cpp
Transforms/MergeComputeNodes/Scheduling/ComputeGraph.cpp Transforms/MergeComputeNodes/Scheduling/ComputeGraph.cpp
Transforms/MergeComputeNodes/Scheduling/ComputeInstanceUtils.cpp Transforms/MergeComputeNodes/Scheduling/ComputeInstanceUtils.cpp
Transforms/MergeComputeNodes/DeferredCommunicationPlanning.cpp
Transforms/MergeComputeNodes/DeferredProjectionAnalysis.cpp
Transforms/MergeComputeNodes/DeferredCommunicationDeadlock.cpp
Transforms/MergeComputeNodes/DeferredCommunicationRealization.cpp
Transforms/MergeComputeNodes/MergeComputeNodesPass.cpp
Transforms/MergeComputeNodes/ScheduledComputeMaterialization.cpp
Transforms/MergeComputeNodes/ScheduledComputeReport.cpp
Transforms/MergeComputeNodes/ScheduledComputeVerification.cpp
Transforms/MergeComputeNodes/SpatialDataflowCsvExporter.cpp
Transforms/MergeComputeNodes/Scheduling/MergeSchedulingAnalysis.cpp Transforms/MergeComputeNodes/Scheduling/MergeSchedulingAnalysis.cpp
Transforms/MergeComputeNodes/Scheduling/PeftScheduler.cpp Transforms/MergeComputeNodes/Scheduling/PeftScheduler.cpp
+59 -4
View File
@@ -6,6 +6,7 @@ include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/BuiltinTypes.td" include "mlir/IR/BuiltinTypes.td"
include "mlir/IR/AttrTypeBase.td" include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/RegionKindInterface.td" include "mlir/IR/RegionKindInterface.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/ParallelCombiningOpInterface.td" include "mlir/Interfaces/ParallelCombiningOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td" include "mlir/Interfaces/SideEffectInterfaces.td"
@@ -27,7 +28,7 @@ def SpatTensor :
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
class SpatComputeLikeBase<string mnemonic> : SpatOp<mnemonic, class SpatComputeLikeBase<string mnemonic> : SpatOp<mnemonic,
[SingleBlock, AttrSizedOperandSegments, [AttrSizedOperandSegments,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>]> { DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>]> {
let summary = "Compute region with attached constant weights"; let summary = "Compute region with attached constant weights";
@@ -40,7 +41,7 @@ class SpatComputeLikeBase<string mnemonic> : SpatOp<mnemonic,
Variadic<SpatTensor>:$outputs Variadic<SpatTensor>:$outputs
); );
let regions = (region SizedRegion<1>:$body); let regions = (region MinSizedRegion<1>:$body);
let hasVerifier = 1; let hasVerifier = 1;
let hasFolder = 1; let hasFolder = 1;
@@ -76,7 +77,7 @@ def SpatScheduledCompute : SpatComputeLikeBase<"scheduled_compute"> {
} }
class SpatComputeBatchLikeBase<string mnemonic> : SpatOp<mnemonic, class SpatComputeBatchLikeBase<string mnemonic> : SpatOp<mnemonic,
[SingleBlock, AttrSizedOperandSegments, [AttrSizedOperandSegments,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>]> { DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmBlockArgumentNames"]>]> {
let summary = "Tensor-native batch of equivalent compute lanes with shared weights and packed inputs"; let summary = "Tensor-native batch of equivalent compute lanes with shared weights and packed inputs";
@@ -90,13 +91,14 @@ class SpatComputeBatchLikeBase<string mnemonic> : SpatOp<mnemonic,
Variadic<SpatTensor>:$outputs Variadic<SpatTensor>:$outputs
); );
let regions = (region SizedRegion<1>:$body); let regions = (region MinSizedRegion<1>:$body);
let hasVerifier = 1; let hasVerifier = 1;
let hasCustomAssemblyFormat = 1; let hasCustomAssemblyFormat = 1;
} }
def SpatGraphComputeBatch : SpatComputeBatchLikeBase<"graph_compute_batch"> { def SpatGraphComputeBatch : SpatComputeBatchLikeBase<"graph_compute_batch"> {
let hasCanonicalizer = 1;
let extraClassDeclaration = [{ let extraClassDeclaration = [{
std::optional<::mlir::BlockArgument> getLaneArgument(); std::optional<::mlir::BlockArgument> getLaneArgument();
std::optional<::mlir::BlockArgument> getWeightArgument(unsigned idx); std::optional<::mlir::BlockArgument> getWeightArgument(unsigned idx);
@@ -113,6 +115,7 @@ def SpatGraphComputeBatch : SpatComputeBatchLikeBase<"graph_compute_batch"> {
} }
def SpatScheduledComputeBatch : SpatComputeBatchLikeBase<"scheduled_compute_batch"> { def SpatScheduledComputeBatch : SpatComputeBatchLikeBase<"scheduled_compute_batch"> {
let hasCanonicalizer = 1;
let extraClassDeclaration = [{ let extraClassDeclaration = [{
std::optional<::mlir::BlockArgument> getLaneArgument(); std::optional<::mlir::BlockArgument> getLaneArgument();
std::optional<::mlir::BlockArgument> getWeightArgument(unsigned idx); std::optional<::mlir::BlockArgument> getWeightArgument(unsigned idx);
@@ -161,6 +164,41 @@ def SpatYieldOp : SpatOp<"yield", [Terminator]> {
let hasCustomAssemblyFormat = 1; let hasCustomAssemblyFormat = 1;
} }
def SpatBlockYieldOp : SpatOp<"block_yield", [
Terminator,
DeclareOpInterfaceMethods<BranchOpInterface, ["getSuccessorForOperands"]>
]> {
let summary = "Terminate a scheduled structural compute block";
let arguments = (ins
Variadic<AnyType>:$outputs
);
let successors = (successor
VariadicSuccessor<AnySuccessor>:$next
);
let hasVerifier = 1;
let hasCustomAssemblyFormat = 1;
}
def SpatDeferredCommunicationOp : SpatOp<"deferred_communication", [SingleBlock]> {
let summary = "Temporary scheduled payload derivation placeholder";
let arguments = (ins
Variadic<SpatTensor>:$sources
);
let results = (outs
SpatTensor:$output
);
let regions = (region SizedRegion<1>:$body);
let hasVerifier = 1;
let hasCustomAssemblyFormat = 1;
}
def SpatExtractRowsOp : SpatOp<"extract_rows", []> { def SpatExtractRowsOp : SpatOp<"extract_rows", []> {
let summary = "Extract every row of a rank-2 tensor as separate rank-2 row tensors"; let summary = "Extract every row of a rank-2 tensor as separate rank-2 row tensors";
@@ -232,6 +270,22 @@ def SpatReluPlanOp : SpatOp<"relu_plan", []> {
let hasVerifier = 1; let hasVerifier = 1;
} }
def SpatBiasAddPlanOp : SpatOp<"bias_add_plan", []> {
let summary = "Layout-aware Conv-style bias add planning op";
let arguments = (ins
SpatTensor:$input,
SpatTensor:$bias,
StrAttr:$logicalLayout
);
let results = (outs
SpatTensor:$output
);
let hasVerifier = 1;
}
def SpatBlueprintOp : SpatOp<"blueprint", []> { def SpatBlueprintOp : SpatOp<"blueprint", []> {
let summary = "Blueprint for assembling logical tensors from published fragments"; let summary = "Blueprint for assembling logical tensors from published fragments";
@@ -245,6 +299,7 @@ def SpatBlueprintOp : SpatOp<"blueprint", []> {
StrAttr:$indexMap, StrAttr:$indexMap,
OptionalAttr<StrAttr>:$mode, OptionalAttr<StrAttr>:$mode,
OptionalAttr<DenseI64ArrayAttr>:$fragmentOperandIndices, OptionalAttr<DenseI64ArrayAttr>:$fragmentOperandIndices,
OptionalAttr<DenseI64ArrayAttr>:$fragmentSourceSlots,
OptionalAttr<DenseI64ArrayAttr>:$fragmentSourceOffsets, OptionalAttr<DenseI64ArrayAttr>:$fragmentSourceOffsets,
OptionalAttr<DenseI64ArrayAttr>:$fragmentStrides, OptionalAttr<DenseI64ArrayAttr>:$fragmentStrides,
OptionalAttr<StrAttr>:$conflictPolicy, OptionalAttr<StrAttr>:$conflictPolicy,
+21
View File
@@ -10,6 +10,18 @@ using namespace mlir;
namespace onnx_mlir { namespace onnx_mlir {
namespace spatial { namespace spatial {
RankedTensorType getGraphBatchPhysicalResultType(int64_t laneCount, RankedTensorType fragmentType) {
SmallVector<int64_t> shape {laneCount};
llvm::append_range(shape, fragmentType.getShape());
return RankedTensorType::get(shape, fragmentType.getElementType(), fragmentType.getEncoding());
}
FailureOr<RankedTensorType> getGraphBatchFragmentType(RankedTensorType physicalType, int64_t expectedLaneCount) {
if (!physicalType || physicalType.getRank() < 1 || physicalType.getDimSize(0) != expectedLaneCount)
return failure();
return RankedTensorType::get(physicalType.getShape().drop_front(), physicalType.getElementType(), physicalType.getEncoding());
}
namespace { namespace {
std::optional<BlockArgument> getBlockArgument(Region& body, unsigned argIdx) { std::optional<BlockArgument> getBlockArgument(Region& body, unsigned argIdx) {
@@ -238,6 +250,15 @@ void SpatScheduledCompute::getAsmBlockArgumentNames(Region& region, OpAsmSetValu
setComputeAsmBlockArgumentNames(*this, region, setNameFn); setComputeAsmBlockArgumentNames(*this, region, setNameFn);
} }
SuccessorOperands SpatBlockYieldOp::getSuccessorOperands(unsigned index) {
assert(index == 0 && "invalid successor index");
return SuccessorOperands(getOutputsMutable());
}
Block* SpatBlockYieldOp::getSuccessorForOperands(ArrayRef<Attribute>) {
return getOperation()->getNumSuccessors() == 0 ? nullptr : getOperation()->getSuccessor(0);
}
std::optional<BlockArgument> SpatGraphComputeBatch::getLaneArgument() { return getBlockArgument(getBody(), 0); } std::optional<BlockArgument> SpatGraphComputeBatch::getLaneArgument() { return getBlockArgument(getBody(), 0); }
std::optional<BlockArgument> SpatGraphComputeBatch::getWeightArgument(unsigned idx) { std::optional<BlockArgument> SpatGraphComputeBatch::getWeightArgument(unsigned idx) {
return getBlockArgument(getBody(), 1 + idx); return getBlockArgument(getBody(), 1 + idx);
+4
View File
@@ -30,6 +30,10 @@
namespace onnx_mlir { namespace onnx_mlir {
namespace spatial { namespace spatial {
mlir::RankedTensorType getGraphBatchPhysicalResultType(int64_t laneCount, mlir::RankedTensorType fragmentType);
mlir::FailureOr<mlir::RankedTensorType>
getGraphBatchFragmentType(mlir::RankedTensorType physicalType, int64_t expectedLaneCount);
bool isGraphComputeLike(mlir::Operation* op); bool isGraphComputeLike(mlir::Operation* op);
bool isGraphBatchComputeLike(mlir::Operation* op); bool isGraphBatchComputeLike(mlir::Operation* op);
bool isScheduledComputeLike(mlir::Operation* op); bool isScheduledComputeLike(mlir::Operation* op);
+96 -2
View File
@@ -160,7 +160,7 @@ void printComputeLikeOp(ComputeOpTy op, OpAsmPrinter& printer) {
printer << " -> "; printer << " -> ";
printCompressedTypeSequence(printer, op.getResultTypes()); printCompressedTypeSequence(printer, op.getResultTypes());
printer << " "; printer << " ";
printer.printRegion(op.getBody(), /*printEntryBlockArgs=*/false); printer.printRegion(op.getBody(), /*printEntryBlockArgs=*/!op.getBody().hasOneBlock());
} }
template <typename ComputeOpTy> template <typename ComputeOpTy>
@@ -290,7 +290,7 @@ void printComputeBatchLikeOp(ComputeBatchOpTy op, OpAsmPrinter& printer) {
printer << " -> "; printer << " -> ";
printCompressedTypeSequence(printer, op.getResultTypes()); printCompressedTypeSequence(printer, op.getResultTypes());
printer << " "; printer << " ";
printer.printRegion(op.getBody(), /*printEntryBlockArgs=*/false); printer.printRegion(op.getBody(), /*printEntryBlockArgs=*/!op.getBody().hasOneBlock());
} }
template <typename ComputeBatchOpTy> template <typename ComputeBatchOpTy>
@@ -407,6 +407,89 @@ ParseResult SpatYieldOp::parse(OpAsmParser& parser, OperationState& result) {
return parser.resolveOperands(outputs, outputTypes, parser.getCurrentLocation(), result.operands); return parser.resolveOperands(outputs, outputTypes, parser.getCurrentLocation(), result.operands);
} }
void SpatBlockYieldOp::print(OpAsmPrinter& printer) {
printer << " ";
printCompressedValueSequence(printer, getOutputs());
if (getOperation()->getNumSuccessors() != 0) {
printer << " next ";
printer.printSuccessor(getOperation()->getSuccessor(0));
}
printer.printOptionalAttrDict((*this)->getAttrs());
printer << " : ";
printCompressedTypeSequence(printer, getOutputs().getTypes());
}
ParseResult SpatBlockYieldOp::parse(OpAsmParser& parser, OperationState& result) {
SmallVector<OpAsmParser::UnresolvedOperand> outputs;
SmallVector<Type> outputTypes;
Block* successor = nullptr;
OpAsmParser::UnresolvedOperand firstOutput;
OptionalParseResult firstOutputResult = parser.parseOptionalOperand(firstOutput);
if (firstOutputResult.has_value()) {
if (failed(*firstOutputResult))
return failure();
if (parseCompressedOperandEntryWithFirst(parser, firstOutput, outputs))
return failure();
while (succeeded(parser.parseOptionalComma()))
if (parseOneCompressedOperandEntry(parser, outputs))
return failure();
}
if (succeeded(parser.parseOptionalKeyword("next")) && parser.parseSuccessor(successor))
return failure();
if (parser.parseOptionalAttrDict(result.attributes) || parser.parseColon()
|| parseCompressedTypeSequence(parser, outputTypes, /*allowEmpty=*/true))
return failure();
if (outputs.size() != outputTypes.size())
return parser.emitError(parser.getCurrentLocation(), "number of outputs and output types must match");
if (parser.resolveOperands(outputs, outputTypes, parser.getCurrentLocation(), result.operands))
return failure();
if (successor)
result.addSuccessors(successor);
return success();
}
void SpatDeferredCommunicationOp::print(OpAsmPrinter& printer) {
printer << " ";
printCompressedValueSequence(printer, getSources());
printer.printOptionalAttrDict((*this)->getAttrs());
printer << " : ";
printer.printFunctionalType(getSources().getTypes(), getOperation()->getResultTypes());
printer << " ";
printer.printRegion(getBody(), /*printEntryBlockArgs=*/false);
}
ParseResult SpatDeferredCommunicationOp::parse(OpAsmParser& parser, OperationState& result) {
SmallVector<OpAsmParser::UnresolvedOperand> sources;
Type functionTypeStorage;
if (parseCompressedOperandSequence(parser, sources) || parser.parseOptionalAttrDict(result.attributes)
|| parser.parseColon() || parser.parseType(functionTypeStorage))
return failure();
auto functionType = dyn_cast<FunctionType>(functionTypeStorage);
if (!functionType)
return parser.emitError(parser.getCurrentLocation(), "expected deferred communication function type");
if (sources.size() != functionType.getNumInputs())
return parser.emitError(parser.getCurrentLocation(), "number of sources and source types must match");
if (parser.resolveOperands(sources, functionType.getInputs(), parser.getCurrentLocation(), result.operands))
return failure();
result.addTypes(functionType.getResults());
Region* body = result.addRegion();
SmallVector<OpAsmParser::Argument> bodyArgs;
for (Type type : functionType.getInputs()) {
OpAsmParser::Argument argument;
argument.type = type;
bodyArgs.push_back(argument);
}
return parser.parseRegion(*body, bodyArgs);
}
void SpatExtractRowsOp::print(OpAsmPrinter& printer) { void SpatExtractRowsOp::print(OpAsmPrinter& printer) {
printer << " "; printer << " ";
printer.printOperand(getInput()); printer.printOperand(getInput());
@@ -493,6 +576,10 @@ void SpatBlueprintOp::print(OpAsmPrinter& printer) {
printer << " operandIndices "; printer << " operandIndices ";
printCompressedIntegerList(printer, *operandIndices); printCompressedIntegerList(printer, *operandIndices);
} }
if (std::optional<ArrayRef<int64_t>> sourceSlots = getFragmentSourceSlots()) {
printer << " sourceSlots ";
printCompressedIntegerList(printer, *sourceSlots);
}
if (std::optional<ArrayRef<int64_t>> sourceOffsets = getFragmentSourceOffsets()) { if (std::optional<ArrayRef<int64_t>> sourceOffsets = getFragmentSourceOffsets()) {
printer << " sourceOffsets "; printer << " sourceOffsets ";
printCompressedIntegerList(printer, *sourceOffsets); printCompressedIntegerList(printer, *sourceOffsets);
@@ -514,6 +601,7 @@ void SpatBlueprintOp::print(OpAsmPrinter& printer) {
getIndexMapAttrName().getValue(), getIndexMapAttrName().getValue(),
getModeAttrName().getValue(), getModeAttrName().getValue(),
getFragmentOperandIndicesAttrName().getValue(), getFragmentOperandIndicesAttrName().getValue(),
getFragmentSourceSlotsAttrName().getValue(),
getFragmentSourceOffsetsAttrName().getValue(), getFragmentSourceOffsetsAttrName().getValue(),
getFragmentStridesAttrName().getValue(), getFragmentStridesAttrName().getValue(),
getConflictPolicyAttrName().getValue(), getConflictPolicyAttrName().getValue(),
@@ -537,6 +625,7 @@ ParseResult SpatBlueprintOp::parse(OpAsmParser& parser, OperationState& result)
SmallVector<int64_t> fragmentOffsets; SmallVector<int64_t> fragmentOffsets;
SmallVector<int64_t> fragmentSizes; SmallVector<int64_t> fragmentSizes;
SmallVector<int64_t> fragmentOperandIndices; SmallVector<int64_t> fragmentOperandIndices;
SmallVector<int64_t> fragmentSourceSlots;
SmallVector<int64_t> fragmentSourceOffsets; SmallVector<int64_t> fragmentSourceOffsets;
SmallVector<int64_t> fragmentStrides; SmallVector<int64_t> fragmentStrides;
@@ -554,6 +643,9 @@ ParseResult SpatBlueprintOp::parse(OpAsmParser& parser, OperationState& result)
if (succeeded(parser.parseOptionalKeyword("operandIndices")) if (succeeded(parser.parseOptionalKeyword("operandIndices"))
&& parseCompressedIntegerList(parser, fragmentOperandIndices)) && parseCompressedIntegerList(parser, fragmentOperandIndices))
return failure(); return failure();
if (succeeded(parser.parseOptionalKeyword("sourceSlots"))
&& parseCompressedIntegerList(parser, fragmentSourceSlots))
return failure();
if (succeeded(parser.parseOptionalKeyword("sourceOffsets")) if (succeeded(parser.parseOptionalKeyword("sourceOffsets"))
&& parseCompressedIntegerList(parser, fragmentSourceOffsets)) && parseCompressedIntegerList(parser, fragmentSourceOffsets))
return failure(); return failure();
@@ -584,6 +676,8 @@ ParseResult SpatBlueprintOp::parse(OpAsmParser& parser, OperationState& result)
result.addAttribute("mode", mode); result.addAttribute("mode", mode);
if (!fragmentOperandIndices.empty()) if (!fragmentOperandIndices.empty())
result.addAttribute("fragmentOperandIndices", builder.getDenseI64ArrayAttr(fragmentOperandIndices)); result.addAttribute("fragmentOperandIndices", builder.getDenseI64ArrayAttr(fragmentOperandIndices));
if (!fragmentSourceSlots.empty())
result.addAttribute("fragmentSourceSlots", builder.getDenseI64ArrayAttr(fragmentSourceSlots));
if (!fragmentSourceOffsets.empty()) if (!fragmentSourceOffsets.empty())
result.addAttribute("fragmentSourceOffsets", builder.getDenseI64ArrayAttr(fragmentSourceOffsets)); result.addAttribute("fragmentSourceOffsets", builder.getDenseI64ArrayAttr(fragmentSourceOffsets));
if (!fragmentStrides.empty()) if (!fragmentStrides.empty())
@@ -1,8 +1,14 @@
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/Block.h" #include "mlir/IR/Block.h"
#include "mlir/IR/IRMapping.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/LogicalResult.h" #include "llvm/Support/LogicalResult.h"
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
using namespace mlir; using namespace mlir;
@@ -40,5 +46,177 @@ LogicalResult SpatScheduledCompute::fold(FoldAdaptor adaptor, ::llvm::SmallVecto
return foldComputeLike(*this, results); return foldComputeLike(*this, results);
} }
template <typename ScalarComputeOpTy>
static ScalarComputeOpTy createEmptyScalarCompute(PatternRewriter& rewriter,
Location loc,
TypeRange resultTypes,
ValueRange weights,
ValueRange inputs) {
auto computeOp = ScalarComputeOpTy::create(rewriter, loc, resultTypes, weights, inputs);
SmallVector<Type> blockArgTypes;
SmallVector<Location> blockArgLocs;
blockArgTypes.reserve(weights.size() + inputs.size());
blockArgLocs.reserve(weights.size() + inputs.size());
for (Value weight : weights) {
blockArgTypes.push_back(weight.getType());
blockArgLocs.push_back(weight.getLoc());
}
for (Value input : inputs) {
blockArgTypes.push_back(input.getType());
blockArgLocs.push_back(input.getLoc());
}
rewriter.createBlock(&computeOp.getBody(), computeOp.getBody().end(), blockArgTypes, blockArgLocs);
rewriter.setInsertionPointToStart(&computeOp.getBody().front());
return computeOp;
}
static SmallVector<OpFoldResult> remapMixedOffsets(ArrayRef<OpFoldResult> mixedOffsets, IRMapping& mapper) {
SmallVector<OpFoldResult> remapped;
remapped.reserve(mixedOffsets.size());
for (OpFoldResult ofr : mixedOffsets) {
if (auto value = dyn_cast<Value>(ofr))
remapped.push_back(cast<Value>(mapper.lookupOrDefault(value)));
else
remapped.push_back(cast<Attribute>(ofr));
}
return remapped;
}
static SmallVector<Value> createEmptyResults(PatternRewriter& rewriter, Location loc, TypeRange resultTypes) {
SmallVector<Value> resultValues;
resultValues.reserve(resultTypes.size());
for (Type resultType : resultTypes) {
auto tensorType = dyn_cast<RankedTensorType>(resultType);
if (!tensorType || !tensorType.hasStaticShape())
return {};
resultValues.push_back(tensor::EmptyOp::create(rewriter, loc, tensorType.getShape(), tensorType.getElementType()));
}
return resultValues;
}
template <typename ScalarComputeOpTy, typename ComputeBatchOpTy>
static void copyCanonicalizedBatchAttrs(ScalarComputeOpTy compute, ComputeBatchOpTy batch, PatternRewriter& rewriter) {
for (NamedAttribute attr : batch->getAttrs()) {
if (attr.getName() == batch.getOperandSegmentSizesAttrName() || attr.getName() == batch.getLaneCountAttrName()
|| attr.getName() == onnx_mlir::kCoreIdsAttrName)
continue;
compute->setAttr(attr.getName(), attr.getValue());
}
if constexpr (std::is_same_v<ComputeBatchOpTy, SpatScheduledComputeBatch>) {
if (auto coreIds = batch->template getAttrOfType<DenseI32ArrayAttr>(onnx_mlir::kCoreIdsAttrName)) {
assert(coreIds.size() == 1 && "single-lane scheduled compute_batch canonicalization expects exactly one core id");
compute->setAttr(onnx_mlir::kCoreIdAttrName, rewriter.getI32IntegerAttr(coreIds.asArrayRef().front()));
}
}
}
template <typename ComputeBatchOpTy, typename ScalarComputeOpTy>
struct CanonicalizeSingleLaneComputeBatchPattern : OpRewritePattern<ComputeBatchOpTy> {
using OpRewritePattern<ComputeBatchOpTy>::OpRewritePattern;
LogicalResult matchAndRewrite(ComputeBatchOpTy compute, PatternRewriter& rewriter) const override {
if (compute.getLaneCount() != 1)
return rewriter.notifyMatchFailure(compute, "lane count is not 1");
Block& oldBlock = compute.getBody().front();
auto oldLaneArg = compute.getLaneArgument();
if (!oldLaneArg)
return rewriter.notifyMatchFailure(compute, "missing compute_batch lane block argument");
rewriter.setInsertionPointAfter(compute);
auto newCompute =
createEmptyScalarCompute<ScalarComputeOpTy>(rewriter, compute.getLoc(), compute.getResultTypes(), compute.getWeights(), compute.getInputs());
copyCanonicalizedBatchAttrs(newCompute, compute, rewriter);
auto* newBlock = &newCompute.getBody().front();
rewriter.setInsertionPointToStart(newBlock);
IRMapping mapper;
Value zero = getOrCreateIndexConstant(rewriter, compute.getOperation(), 0);
mapper.map(*oldLaneArg, zero);
for (auto [index, weight] : llvm::enumerate(compute.getWeights())) {
auto oldArg = compute.getWeightArgument(index);
auto newArg = newCompute.getWeightArgument(index);
if (!oldArg || !newArg)
return rewriter.notifyMatchFailure(compute, "missing rewritten compute weight block argument");
mapper.map(*oldArg, *newArg);
}
for (auto [index, input] : llvm::enumerate(compute.getInputs())) {
auto oldArg = compute.getInputArgument(index);
auto newArg = newCompute.getInputArgument(index);
if (!oldArg || !newArg)
return rewriter.notifyMatchFailure(compute, "missing rewritten compute input block argument");
mapper.map(*oldArg, *newArg);
}
SmallVector<Value> resultValues = createEmptyResults(rewriter, compute.getLoc(), compute.getResultTypes());
if (resultValues.size() != compute.getNumResults())
return rewriter.notifyMatchFailure(compute, "single-lane compute_batch canonicalization requires static ranked results");
for (auto [index, resultValue] : llvm::enumerate(resultValues)) {
auto oldOutputArg = compute.getOutputArgument(index);
if (!oldOutputArg)
return rewriter.notifyMatchFailure(compute, "missing compute_batch output block argument");
mapper.map(*oldOutputArg, resultValue);
}
auto oldInParallel = dyn_cast<SpatInParallelOp>(oldBlock.getTerminator());
auto oldYield = dyn_cast<SpatYieldOp>(oldBlock.getTerminator());
for (Operation& op : oldBlock.without_terminator())
rewriter.clone(op, mapper);
if (oldYield) {
SpatYieldOp::create(rewriter, oldYield.getLoc(), ValueRange {});
rewriter.replaceOp(compute, newCompute.getResults());
return success();
}
if (!oldInParallel)
return rewriter.notifyMatchFailure(compute, "expected spat.in_parallel or empty spat.yield terminator");
DenseMap<BlockArgument, size_t> outputIndexByArg;
for (size_t index = 0; index < compute.getNumResults(); ++index) {
auto oldOutputArg = compute.getOutputArgument(index);
if (!oldOutputArg)
return rewriter.notifyMatchFailure(compute, "missing compute_batch output block argument");
outputIndexByArg[*oldOutputArg] = index;
}
for (Operation& op : oldInParallel.getRegion().front()) {
auto insertSlice = dyn_cast<tensor::ParallelInsertSliceOp>(&op);
if (!insertSlice)
return rewriter.notifyMatchFailure(compute, "expected only tensor.parallel_insert_slice in spat.in_parallel");
auto oldDest = dyn_cast<BlockArgument>(insertSlice.getDest());
if (!oldDest)
return rewriter.notifyMatchFailure(compute, "expected tensor.parallel_insert_slice destination to be a block argument");
auto resultIndexIt = outputIndexByArg.find(oldDest);
if (resultIndexIt == outputIndexByArg.end())
return rewriter.notifyMatchFailure(compute, "unexpected tensor.parallel_insert_slice destination");
size_t resultIndex = resultIndexIt->second;
Value remappedSource = mapper.lookupOrDefault(insertSlice.getSource());
auto remappedOffsets = remapMixedOffsets(insertSlice.getMixedOffsets(), mapper);
auto remappedSizes = remapMixedOffsets(insertSlice.getMixedSizes(), mapper);
auto remappedStrides = remapMixedOffsets(insertSlice.getMixedStrides(), mapper);
resultValues[resultIndex] = tensor::InsertSliceOp::create(rewriter,
insertSlice.getLoc(),
remappedSource,
resultValues[resultIndex],
remappedOffsets,
remappedSizes,
remappedStrides)
.getResult();
}
SpatYieldOp::create(rewriter, oldInParallel.getLoc(), resultValues);
rewriter.replaceOp(compute, newCompute.getResults());
return success();
}
};
void SpatGraphComputeBatch::getCanonicalizationPatterns(RewritePatternSet& results, MLIRContext* context) {
results.add<CanonicalizeSingleLaneComputeBatchPattern<SpatGraphComputeBatch, SpatGraphCompute>>(context);
}
void SpatScheduledComputeBatch::getCanonicalizationPatterns(RewritePatternSet& results, MLIRContext* context) {
results.add<CanonicalizeSingleLaneComputeBatchPattern<SpatScheduledComputeBatch, SpatScheduledCompute>>(context);
}
} // namespace spatial } // namespace spatial
} // namespace onnx_mlir } // namespace onnx_mlir
+221 -76
View File
@@ -1,5 +1,6 @@
#include "mlir/Dialect/Affine/IR/AffineOps.h" #include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h" #include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/AffineExpr.h" #include "mlir/IR/AffineExpr.h"
#include "mlir/IR/Block.h" #include "mlir/IR/Block.h"
@@ -59,6 +60,21 @@ static LogicalResult verifyStaticWeights(ComputeOpTy computeOp, StringRef kind)
return success(); return success();
} }
static bool isStaticScfForInductionVar(Value value) {
auto blockArg = dyn_cast<BlockArgument>(value);
if (!blockArg)
return false;
auto loop = dyn_cast_or_null<scf::ForOp>(blockArg.getOwner()->getParentOp());
if (!loop || loop.getInductionVar() != value)
return false;
std::optional<int64_t> lowerBound = matchConstantIndexValue(loop.getLowerBound());
std::optional<int64_t> upperBound = matchConstantIndexValue(loop.getUpperBound());
std::optional<int64_t> step = matchConstantIndexValue(loop.getStep());
return lowerBound && upperBound && step && *step > 0 && *upperBound >= *lowerBound;
}
static bool isStaticIndexExpr(Value value) { static bool isStaticIndexExpr(Value value) {
if (matchConstantIndexValue(value)) if (matchConstantIndexValue(value))
return true; return true;
@@ -80,7 +96,7 @@ static bool isStaticIndexExpr(Value value) {
} }
static bool isSupportedLaneOffsetExpr(Value value, BlockArgument laneArg) { static bool isSupportedLaneOffsetExpr(Value value, BlockArgument laneArg) {
if (value == laneArg || isStaticIndexExpr(value)) if (value == laneArg || isStaticIndexExpr(value) || isStaticScfForInductionVar(value))
return true; return true;
auto affineApply = value.getDefiningOp<affine::AffineApplyOp>(); auto affineApply = value.getDefiningOp<affine::AffineApplyOp>();
@@ -176,12 +192,18 @@ static bool isConstantExternalValue(Value value) {
return definingOp && definingOp->hasTrait<OpTrait::ConstantLike>(); return definingOp && definingOp->hasTrait<OpTrait::ConstantLike>();
} }
static bool isRecordedDeferredCommunicationSource(Operation* op, Value value) {
auto transfer = dyn_cast<SpatDeferredCommunicationOp>(op);
return transfer && llvm::is_contained(transfer.getSources(), value);
}
static LogicalResult verifyOnlyConstantExternalValues(Operation* ownerOp, Region& region, StringRef kind) { static LogicalResult verifyOnlyConstantExternalValues(Operation* ownerOp, Region& region, StringRef kind) {
bool hasFailure = false; bool hasFailure = false;
region.walk([&](Operation* op) { region.walk([&](Operation* op) {
for (OpOperand& operand : op->getOpOperands()) { for (OpOperand& operand : op->getOpOperands()) {
Value value = operand.get(); Value value = operand.get();
if (isDefinedInsideRegion(value, region) || isConstantExternalValue(value)) if (isDefinedInsideRegion(value, region) || isConstantExternalValue(value)
|| isRecordedDeferredCommunicationSource(op, value))
continue; continue;
InFlightDiagnostic diagnostic = InFlightDiagnostic diagnostic =
@@ -203,8 +225,35 @@ static LogicalResult verifyOnlyConstantExternalValues(Operation* ownerOp, Region
return success(!hasFailure); return success(!hasFailure);
} }
static LogicalResult verifyYieldTypes(Operation* op, Region& region, TypeRange resultTypes, StringRef kind) {
if (region.empty())
return op->emitOpError() << kind << " requires a body block";
Block& block = region.front();
auto yield = dyn_cast_or_null<SpatYieldOp>(block.getTerminator());
if (!yield)
return op->emitOpError() << kind << " body must terminate with spat.yield";
if (yield.getOutputs().size() != resultTypes.size())
return op->emitOpError() << kind << " yield operand count must match result count";
for (auto [yieldType, resultType] : llvm::zip(yield.getOutputs().getTypes(), resultTypes))
if (yieldType != resultType)
return op->emitOpError() << kind << " yield operand types must match result types";
return success();
}
static LogicalResult verifyRegionArguments(Operation* op, Region& region, ValueRange operands, StringRef kind) {
if (region.empty())
return op->emitOpError() << kind << " requires a body block";
Block& block = region.front();
if (block.getNumArguments() != operands.size())
return op->emitOpError() << kind << " body argument count must match operand count";
for (auto [arg, operand] : llvm::zip(block.getArguments(), operands))
if (arg.getType() != operand.getType())
return op->emitOpError() << kind << " body argument types must match operand types";
return success();
}
template <typename ComputeBatchOpTy> template <typename ComputeBatchOpTy>
static LogicalResult verifyBatchBody(ComputeBatchOpTy batchOp, Block& block) { static LogicalResult verifyBatchBody(ComputeBatchOpTy batchOp, Block& block, bool verifyLaneSliceOffsets = true) {
if (batchOp.getNumResults() == 0) { if (batchOp.getNumResults() == 0) {
auto yieldOp = dyn_cast_or_null<SpatYieldOp>(block.getTerminator()); auto yieldOp = dyn_cast_or_null<SpatYieldOp>(block.getTerminator());
if (!yieldOp) if (!yieldOp)
@@ -219,11 +268,12 @@ static LogicalResult verifyBatchBody(ComputeBatchOpTy batchOp, Block& block) {
auto laneArg = batchOp.getLaneArgument(); auto laneArg = batchOp.getLaneArgument();
if (!laneArg) if (!laneArg)
return batchOp.emitError("compute_batch body must have a lane block argument"); return batchOp.emitError("compute_batch body must have a lane block argument");
for (auto& bodyOp : block) { if (verifyLaneSliceOffsets)
if (auto extractSlice = dyn_cast<tensor::ExtractSliceOp>(&bodyOp)) for (auto& bodyOp : block) {
if (failed(verifyStaticUnitStrideExtractSliceOp(extractSlice, *laneArg, "tensor.extract_slice"))) if (auto extractSlice = dyn_cast<tensor::ExtractSliceOp>(&bodyOp))
return failure(); if (failed(verifyStaticUnitStrideExtractSliceOp(extractSlice, *laneArg, "tensor.extract_slice")))
} return failure();
}
return success(); return success();
} }
@@ -436,6 +486,39 @@ LogicalResult SpatReluPlanOp::verify() {
return success(); return success();
} }
LogicalResult SpatBiasAddPlanOp::verify() {
if (failed(verifyPlanTensorTypes(getOperation(), getInput(), getOutput(), "spat.bias_add_plan")))
return failure();
if (!isKnownLogicalLayout(getLogicalLayout()))
return emitError("requires a known logical layout");
auto inputType = dyn_cast<RankedTensorType>(getInput().getType());
auto biasType = dyn_cast<RankedTensorType>(getBias().getType());
auto outputType = dyn_cast<RankedTensorType>(getOutput().getType());
if (!inputType || !biasType || !outputType)
return emitError("requires ranked tensor input, bias, and output");
if (!inputType.hasStaticShape() || !biasType.hasStaticShape() || !outputType.hasStaticShape())
return emitError("requires static tensor input, bias, and output");
if (inputType != outputType)
return emitError("requires matching input and output tensor types");
if (outputType.getRank() != 4)
return emitError("requires rank-4 input/output tensors");
if (getLogicalLayout() != "nchw")
return emitError("requires logical layout \"nchw\"");
if (biasType.getElementType() != outputType.getElementType())
return emitError("requires bias element type to match the output element type");
ArrayRef<int64_t> biasShape = biasType.getShape();
const int64_t channels = outputType.getDimSize(1);
const bool supported = biasShape.empty() || (biasShape.size() == 1 && biasShape[0] == channels)
|| (biasShape.size() == 2 && biasShape[0] == 1 && biasShape[1] == channels)
|| (biasShape.size() == 4 && biasShape[0] == 1 && biasShape[1] == channels
&& biasShape[2] == 1 && biasShape[3] == 1);
if (!supported)
return emitError("requires scalar or per-channel bias broadcastable over NCHW");
return success();
}
LogicalResult SpatBlueprintOp::verify() { LogicalResult SpatBlueprintOp::verify() {
auto modeAttr = getModeAttr(); auto modeAttr = getModeAttr();
bool isFragmentAssembly = modeAttr && modeAttr.getValue() == "fragment_assembly"; bool isFragmentAssembly = modeAttr && modeAttr.getValue() == "fragment_assembly";
@@ -491,20 +574,26 @@ LogicalResult SpatBlueprintOp::verify() {
auto stridesAttr = getFragmentStridesAttr(); auto stridesAttr = getFragmentStridesAttr();
auto operandIndicesAttr = getFragmentOperandIndicesAttr(); auto operandIndicesAttr = getFragmentOperandIndicesAttr();
auto sourceSlotsAttr = getFragmentSourceSlotsAttr();
auto sourceOffsetsAttr = getFragmentSourceOffsetsAttr(); auto sourceOffsetsAttr = getFragmentSourceOffsetsAttr();
if (!operandIndicesAttr) if (!operandIndicesAttr)
return emitError("fragment assembly blueprint requires fragment operand indices"); return emitError("fragment assembly blueprint requires fragment operand indices");
if (!sourceSlotsAttr)
return emitError("fragment assembly blueprint requires physical fragment source slots");
if (!sourceOffsetsAttr) if (!sourceOffsetsAttr)
return emitError("fragment assembly blueprint requires fragment source offsets"); return emitError("fragment assembly blueprint requires fragment source offsets");
if (!stridesAttr) if (!stridesAttr)
return emitError("fragment assembly blueprint requires fragment strides"); return emitError("fragment assembly blueprint requires fragment strides");
ArrayRef<int64_t> operandIndices = operandIndicesAttr.asArrayRef(); ArrayRef<int64_t> operandIndices = operandIndicesAttr.asArrayRef();
ArrayRef<int64_t> sourceSlots = sourceSlotsAttr.asArrayRef();
ArrayRef<int64_t> sourceOffsets = sourceOffsetsAttr.asArrayRef(); ArrayRef<int64_t> sourceOffsets = sourceOffsetsAttr.asArrayRef();
ArrayRef<int64_t> strides = stridesAttr.asArrayRef(); ArrayRef<int64_t> strides = stridesAttr.asArrayRef();
if (strides.size() != offsets.size()) if (strides.size() != offsets.size())
return emitError("fragment stride and offset arrays must have the same length"); return emitError("fragment stride and offset arrays must have the same length");
if (sourceOffsets.size() != operandIndices.size()) if (sourceOffsets.size() != operandIndices.size())
return emitError("fragment source offset count must match fragment operand index count"); return emitError("fragment source offset count must match fragment operand index count");
if (sourceSlots.size() != operandIndices.size())
return emitError("fragment source slot count must match fragment operand index count");
if (!getConflictPolicyAttr() || !getCoveragePolicyAttr()) if (!getConflictPolicyAttr() || !getCoveragePolicyAttr())
return emitError("fragment assembly blueprint requires conflict and coverage policies"); return emitError("fragment assembly blueprint requires conflict and coverage policies");
if (getConflictPolicy() != "disjoint") if (getConflictPolicy() != "disjoint")
@@ -539,14 +628,19 @@ LogicalResult SpatBlueprintOp::verify() {
int64_t operandIndex = operandIndices[fragmentIndex]; int64_t operandIndex = operandIndices[fragmentIndex];
if (operandIndex < 0 || operandIndex >= operandCount) if (operandIndex < 0 || operandIndex >= operandCount)
return emitError("fragment assembly operand index is out of range"); return emitError("fragment assembly operand index is out of range");
if (sourceSlots[fragmentIndex] < 0)
return emitError("fragment assembly physical source slot must be nonnegative");
if (sourceOffsets[fragmentIndex] < 0) if (sourceOffsets[fragmentIndex] < 0)
return emitError("fragment assembly source offsets must be nonnegative"); return emitError("fragment assembly source offsets must be nonnegative");
auto operandType = dyn_cast<RankedTensorType>(operands[operandIndex].getType()); auto operandType = dyn_cast<RankedTensorType>(operands[operandIndex].getType());
if (!operandType || !operandType.hasStaticShape()) if (!operandType || !operandType.hasStaticShape())
return emitError("fragment assembly blueprint requires static ranked tensor operands"); return emitError("fragment assembly blueprint requires static ranked tensor operands");
if (operandType.getRank() != rank) if (operandType.getRank() != rank + 1)
return emitError("fragment assembly blueprint requires operand/result rank match"); return emitError("fragment assembly physical operand must have one leading source-slot dimension");
if (sourceSlots[fragmentIndex] >= operandType.getDimSize(0))
return emitError("fragment assembly physical source slot is out of range");
auto fragmentType = RankedTensorType::get(operandType.getShape().drop_front(), operandType.getElementType(), operandType.getEncoding());
SmallVector<int64_t, 4> fragmentOffsets; SmallVector<int64_t, 4> fragmentOffsets;
SmallVector<int64_t, 4> fragmentSizes; SmallVector<int64_t, 4> fragmentSizes;
@@ -562,12 +656,12 @@ LogicalResult SpatBlueprintOp::verify() {
int64_t fragmentElements = 1; int64_t fragmentElements = 1;
for (int64_t dim = 0; dim < rank; ++dim) for (int64_t dim = 0; dim < rank; ++dim)
fragmentElements *= fragmentSizes[dim]; fragmentElements *= fragmentSizes[dim];
if (sourceOffsets[fragmentIndex] + fragmentElements > operandType.getNumElements()) if (sourceOffsets[fragmentIndex] + fragmentElements > fragmentType.getNumElements())
return emitError("fragment assembly source offset exceeds the operand bounds"); return emitError("fragment assembly source offset exceeds the selected physical fragment bounds");
SmallVector<int64_t, 4> sourceSliceOffsets = SmallVector<int64_t, 4> sourceSliceOffsets =
expandFlatElementIndex(sourceOffsets[fragmentIndex], operandType.getShape()); expandFlatElementIndex(sourceOffsets[fragmentIndex], fragmentType.getShape());
for (int64_t dim = 0; dim < rank; ++dim) for (int64_t dim = 0; dim < rank; ++dim)
if (sourceSliceOffsets[dim] + fragmentSizes[dim] > operandType.getDimSize(dim)) if (sourceSliceOffsets[dim] + fragmentSizes[dim] > fragmentType.getDimSize(dim))
return emitError("fragment assembly source offset must describe a valid unit-stride slice"); return emitError("fragment assembly source offset must describe a valid unit-stride slice");
for (const auto& [existingOffsets, existingSizes] : slices) { for (const auto& [existingOffsets, existingSizes] : slices) {
@@ -630,7 +724,9 @@ LogicalResult verifyComputeResultsUses(Operation* op) {
if (!isAnySpatialComputeLike(op)) if (!isAnySpatialComputeLike(op))
return op->emitError("verifyComputeResultUses: op is not a Spatial compute-like operation"); return op->emitError("verifyComputeResultUses: op is not a Spatial compute-like operation");
if (!llvm::all_of(op->getResults(), [](Value result) { if (!llvm::all_of(op->getResults(), [](Value result) {
return llvm::all_of(result.getUsers(), [](Operation* op) { return llvm::all_of(result.getUsers(), [result](Operation* op) {
if (isRecordedDeferredCommunicationSource(op, result))
return true;
return !isAnySpatialComputeLike(op->getParentOp()); return !isAnySpatialComputeLike(op->getParentOp());
}); });
})) { })) {
@@ -641,57 +737,68 @@ LogicalResult verifyComputeResultsUses(Operation* op) {
template <typename ComputeOpTy> template <typename ComputeOpTy>
LogicalResult verifyComputeLikeOp(ComputeOpTy compute, StringRef opName) { LogicalResult verifyComputeLikeOp(ComputeOpTy compute, StringRef opName) {
auto& block = compute.getBody().front();
unsigned expectedArgCount = compute.getWeights().size() + compute.getInputs().size(); unsigned expectedArgCount = compute.getWeights().size() + compute.getInputs().size();
if (block.getNumArguments() != expectedArgCount) bool isScheduled = isa<SpatScheduledCompute>(compute.getOperation());
return compute.emitOpError("compute body must have weight and input block arguments"); if (compute.getBody().empty())
return compute.emitOpError("compute body must have at least one block");
for (auto [weightIndex, weight] : llvm::enumerate(compute.getWeights())) { SmallVector<Type> yieldedTypes;
auto blockArg = compute.getWeightArgument(weightIndex); for (Block& block : compute.getBody()) {
if (!blockArg || blockArg->getType() != weight.getType()) if ((!isScheduled && block.getNumArguments() != expectedArgCount)
return compute.emitOpError("compute weight block argument types must match weight operand types exactly"); || (isScheduled && block.getNumArguments() < expectedArgCount))
} return compute.emitOpError("compute body must have weight and input block arguments");
for (auto [inputIndex, input] : llvm::enumerate(compute.getInputs())) {
auto blockArg = compute.getInputArgument(inputIndex);
if (!blockArg || blockArg->getType() != input.getType())
return compute.emitOpError("compute input block argument types must match input operand types exactly");
}
if (block.mightHaveTerminator()) { for (auto [weightIndex, weight] : llvm::enumerate(compute.getWeights()))
auto yieldOp = dyn_cast_or_null<SpatYieldOp>(block.getTerminator()); if (block.getArgument(weightIndex).getType() != weight.getType())
if (!yieldOp) return compute.emitOpError("compute weight block argument types must match weight operand types exactly");
for (auto [inputIndex, input] : llvm::enumerate(compute.getInputs()))
if (block.getArgument(compute.getWeights().size() + inputIndex).getType() != input.getType())
return compute.emitOpError("compute input block argument types must match input operand types exactly");
Operation* terminator = block.getTerminator();
if (auto yieldOp = dyn_cast_or_null<SpatYieldOp>(terminator)) {
auto realized = compute->template getAttrOfType<BoolAttr>("scheduled.realized");
if (isScheduled && (!realized || !realized.getValue() || !compute.getBody().hasOneBlock()))
return compute.emitOpError("scheduled compute blocks must terminate with spat.block_yield");
llvm::append_range(yieldedTypes, yieldOp->getOperandTypes());
continue;
}
auto blockYield = dyn_cast_or_null<SpatBlockYieldOp>(terminator);
if (!blockYield || !isScheduled)
return compute.emitOpError("ComputeOp must have a single yield operation"); return compute.emitOpError("ComputeOp must have a single yield operation");
if (blockYield->getNumSuccessors() == 0)
llvm::append_range(yieldedTypes, blockYield->getOperandTypes());
}
auto resultTypes = compute.getResultTypes(); auto resultTypes = compute.getResultTypes();
auto yieldTypes = yieldOp->getOperandTypes(); if (resultTypes.size() != yieldedTypes.size())
if (resultTypes.size() != yieldTypes.size()) return compute.emitOpError("ComputeOp must have same number of results as yielded operands");
return compute.emitOpError("ComputeOp must have same number of results as yieldOp operands");
for (auto it : llvm::reverse(llvm::zip(resultTypes, yieldTypes))) { for (auto it : llvm::reverse(llvm::zip(resultTypes, yieldedTypes))) {
auto resultType = std::get<0>(it); auto resultType = std::get<0>(it);
auto yieldType = std::get<1>(it); auto yieldType = std::get<1>(it);
if (resultType != yieldType || failed(verifyCompatibleShape(resultType, yieldType))) if (resultType != yieldType || failed(verifyCompatibleShape(resultType, yieldType)))
return compute.emitOpError("ComputeOp output must be of the same type as yieldOp operand"); return compute.emitOpError("ComputeOp output must be of the same type as yieldOp operand");
if (auto resultRankedType = dyn_cast<RankedTensorType>(resultType)) { if (auto resultRankedType = dyn_cast<RankedTensorType>(resultType)) {
if (auto yieldRankedType = dyn_cast<RankedTensorType>(yieldType)) { if (auto yieldRankedType = dyn_cast<RankedTensorType>(yieldType)) {
if (resultRankedType.getEncoding() != yieldRankedType.getEncoding()) if (resultRankedType.getEncoding() != yieldRankedType.getEncoding())
return compute.emitOpError("ComputeOp output must have the same encoding as yieldOp operand");
}
else {
return compute.emitOpError("ComputeOp output has an encoding while yieldOp operand does not have one"); return compute.emitOpError("ComputeOp output has an encoding while yieldOp operand does not have one");
}
} }
else if (dyn_cast<RankedTensorType>(yieldType)) { else {
return compute.emitOpError("ComputeOp output must not have an encoding if yieldOp operand has one"); return compute.emitOpError("ComputeOp output must have the same encoding as yieldOp operand");
} }
} }
else if (dyn_cast<RankedTensorType>(yieldType)) {
return compute.emitOpError("ComputeOp output must not have an encoding if yieldOp operand has one");
}
} }
for (unsigned inputIndex = 0; inputIndex < compute.getInputs().size(); ++inputIndex) if (compute.getBody().hasOneBlock())
if (auto inputArg = compute.getInputArgument(inputIndex); !inputArg || inputArg->use_empty()) for (unsigned inputIndex = 0; inputIndex < compute.getInputs().size(); ++inputIndex)
return compute.emitOpError("ComputeOp block argument is not used"); if (auto inputArg = compute.getInputArgument(inputIndex); !inputArg || inputArg->use_empty())
return compute.emitOpError("ComputeOp block argument is not used");
if (failed(verifyStaticWeights(compute, opName))) if (failed(verifyStaticWeights(compute, opName)))
return failure(); return failure();
if (failed(verifyOnlyConstantExternalValues(compute.getOperation(), compute.getBody(), opName))) if (failed(verifyOnlyConstantExternalValues(compute.getOperation(), compute.getBody(), opName)))
@@ -705,6 +812,41 @@ LogicalResult SpatGraphCompute::verify() { return verifyComputeLikeOp(*this, "sp
LogicalResult SpatScheduledCompute::verify() { return verifyComputeLikeOp(*this, "spat.scheduled_compute"); } LogicalResult SpatScheduledCompute::verify() { return verifyComputeLikeOp(*this, "spat.scheduled_compute"); }
LogicalResult SpatBlockYieldOp::verify() {
if (getOperation()->getNumSuccessors() > 1)
return emitOpError("may target at most one next scheduled block");
Operation* parent = getOperation()->getParentOp();
if (!isa_and_nonnull<SpatScheduledCompute>(parent))
return emitOpError("expected spat.scheduled_compute parent");
if (getOperation()->getNumSuccessors() == 1) {
Block* next = getOperation()->getSuccessor(0);
if (getOperation()->getNumOperands() != next->getNumArguments())
return emitOpError("successor operand count must match next block argument count");
for (auto [operand, argument] : llvm::zip(getOperation()->getOperands(), next->getArguments()))
if (operand.getType() != argument.getType())
return emitOpError("successor operand types must match next block argument types");
}
return success();
}
LogicalResult SpatDeferredCommunicationOp::verify() {
if (getSources().empty())
return emitOpError("requires at least one source");
static constexpr StringLiteral staleAttributes[] = {
"exchangeId", "logicalProducer", "logicalConsumer", "sourceClass", "targetClass", "sourceCore",
"targetCore", "sourceLane", "targetLane", "transferKind", "resultIndex", "projectedTransfer",
"hostOutputOwner", "source_cpus", "source_classes", "source_lane_ranges", "target_cpus",
"target_classes", "target_lane_ranges", "batched", "source_operand_for_scheduled_lane",
"multi_source_payload"};
for (StringLiteral name : staleAttributes)
if (getOperation()->hasAttr(name))
return emitOpError() << "does not accept stale routing attribute '" << name
<< "'; source selection and shaping belong in the body and routing is derived in Phase 2";
if (failed(verifyRegionArguments(getOperation(), getBody(), getSources(), "spat.deferred_communication")))
return failure();
return verifyYieldTypes(getOperation(), getBody(), getOperation()->getResultTypes(), "spat.deferred_communication");
}
template <typename ComputeBatchOpTy> template <typename ComputeBatchOpTy>
LogicalResult verifyComputeBatchLikeOp(ComputeBatchOpTy batch, StringRef opName) { LogicalResult verifyComputeBatchLikeOp(ComputeBatchOpTy batch, StringRef opName) {
int32_t count = batch.getLaneCount(); int32_t count = batch.getLaneCount();
@@ -727,30 +869,33 @@ LogicalResult verifyComputeBatchLikeOp(ComputeBatchOpTy batch, StringRef opName)
return batch.emitOpError("compute_batch coreIds values must be unique"); return batch.emitOpError("compute_batch coreIds values must be unique");
} }
Block& block = batch.getBody().front(); if (batch.getBody().empty())
if (block.getNumArguments() == 0) return batch.emitOpError("compute_batch body must have at least one block");
return batch.emitOpError("compute_batch body must have exactly one lane block argument");
unsigned expectedArgCount = 1 + batch.getWeights().size() + batch.getInputs().size() + batch.getNumResults();
if (block.getNumArguments() != expectedArgCount)
return batch.emitOpError("compute_batch body block arguments must match lane, weight, input, and output operands/results");
auto laneArg = batch.getLaneArgument();
if (!laneArg || !laneArg->getType().isIndex())
return batch.emitOpError("compute_batch first block argument must have index type");
for (auto [weightIndex, weight] : llvm::enumerate(batch.getWeights())) { unsigned expectedArgCount = 1 + batch.getWeights().size() + batch.getInputs().size() + batch.getNumResults();
auto blockArg = batch.getWeightArgument(weightIndex); bool verifyLaneSliceOffsets = !isa<SpatScheduledComputeBatch>(batch.getOperation());
if (!blockArg || blockArg->getType() != weight.getType()) for (Block& block : batch.getBody()) {
return batch.emitOpError("compute_batch weight block argument types must match weight operand types exactly"); if (block.getNumArguments() == 0)
} return batch.emitOpError("compute_batch body must have exactly one lane block argument");
for (auto [inputIndex, input] : llvm::enumerate(batch.getInputs())) { if (block.getNumArguments() != expectedArgCount)
auto blockArg = batch.getInputArgument(inputIndex); return batch.emitOpError(
if (!blockArg || blockArg->getType() != input.getType()) "compute_batch body block arguments must match lane, weight, input, and output operands/results");
return batch.emitOpError("compute_batch input block argument types must match input operand types exactly"); if (!block.getArgument(0).getType().isIndex())
} return batch.emitOpError("compute_batch first block argument must have index type");
for (auto [resultIndex, resultType] : llvm::enumerate(batch.getResultTypes())) {
auto blockArg = batch.getOutputArgument(resultIndex); for (auto [weightIndex, weight] : llvm::enumerate(batch.getWeights()))
if (!blockArg || blockArg->getType() != resultType) if (block.getArgument(1 + weightIndex).getType() != weight.getType())
return batch.emitOpError("compute_batch output block argument types must match result types exactly"); return batch.emitOpError("compute_batch weight block argument types must match weight operand types exactly");
for (auto [inputIndex, input] : llvm::enumerate(batch.getInputs()))
if (block.getArgument(1 + batch.getWeights().size() + inputIndex).getType() != input.getType())
return batch.emitOpError("compute_batch input block argument types must match input operand types exactly");
for (auto [resultIndex, resultType] : llvm::enumerate(batch.getResultTypes()))
if (block.getArgument(1 + batch.getWeights().size() + batch.getInputs().size() + resultIndex).getType()
!= resultType)
return batch.emitOpError("compute_batch output block argument types must match result types exactly");
if (failed(verifyBatchBody(batch, block, verifyLaneSliceOffsets)))
return failure();
} }
if (failed(verifyComputeResultsUses(batch.getOperation()))) if (failed(verifyComputeResultsUses(batch.getOperation())))
@@ -759,7 +904,7 @@ LogicalResult verifyComputeBatchLikeOp(ComputeBatchOpTy batch, StringRef opName)
return failure(); return failure();
if (failed(verifyOnlyConstantExternalValues(batch.getOperation(), batch.getBody(), opName))) if (failed(verifyOnlyConstantExternalValues(batch.getOperation(), batch.getBody(), opName)))
return failure(); return failure();
return verifyBatchBody(batch, block); return success();
} }
LogicalResult SpatGraphComputeBatch::verify() { return verifyComputeBatchLikeOp(*this, "spat.graph_compute_batch"); } LogicalResult SpatGraphComputeBatch::verify() { return verifyComputeBatchLikeOp(*this, "spat.graph_compute_batch"); }
@@ -0,0 +1,336 @@
#include "DeferredCommunicationDeadlock.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
namespace onnx_mlir::spatial {
using namespace mlir;
namespace {
enum class EventKind { Compute, Send, Receive };
struct Event {
EventKind kind = EventKind::Compute;
uint64_t exchangeId = 0;
};
static LogicalResult simulate(Operation *anchor,
ArrayRef<SmallVector<Event>> streams,
StringRef phase) {
SmallVector<size_t> cursor(streams.size());
DenseMap<uint64_t, unsigned> headSends;
DenseMap<uint64_t, unsigned> headReceives;
SmallVector<unsigned> readyComputes;
SmallVector<uint64_t> readyExchanges;
size_t computeCursor = 0;
size_t exchangeCursor = 0;
unsigned finishedStreams = 0;
auto registerHead = [&](unsigned stream) {
if (cursor[stream] == streams[stream].size()) {
++finishedStreams;
return;
}
const Event &event = streams[stream][cursor[stream]];
if (event.kind == EventKind::Compute) {
readyComputes.push_back(stream);
return;
}
DenseMap<uint64_t, unsigned> &heads =
event.kind == EventKind::Send ? headSends : headReceives;
DenseMap<uint64_t, unsigned> &peers =
event.kind == EventKind::Send ? headReceives : headSends;
heads[event.exchangeId] = stream;
if (peers.contains(event.exchangeId))
readyExchanges.push_back(event.exchangeId);
};
for (unsigned stream = 0; stream < streams.size(); ++stream)
registerHead(stream);
while (computeCursor != readyComputes.size()
|| exchangeCursor != readyExchanges.size()) {
if (computeCursor != readyComputes.size()) {
unsigned stream = readyComputes[computeCursor++];
++cursor[stream];
registerHead(stream);
continue;
}
uint64_t exchange = readyExchanges[exchangeCursor++];
auto send = headSends.find(exchange);
auto receive = headReceives.find(exchange);
if (send == headSends.end() || receive == headReceives.end())
continue;
unsigned source = send->second;
unsigned target = receive->second;
headSends.erase(send);
headReceives.erase(receive);
++cursor[source];
++cursor[target];
registerHead(source);
registerHead(target);
}
if (finishedStreams == streams.size())
return success();
InFlightDiagnostic diagnostic = anchor->emitError()
<< phase << " communication rendezvous simulation made no progress";
unsigned reported = 0;
for (unsigned stream = 0; stream < streams.size() && reported < 8; ++stream) {
if (cursor[stream] == streams[stream].size())
continue;
const Event &event = streams[stream][cursor[stream]];
diagnostic << (reported == 0 ? "; blocked " : ", ") << "stream " << stream
<< " at exchange " << event.exchangeId;
++reported;
}
return failure();
}
static std::optional<int64_t> getI64Attr(Operation *op, StringRef name) {
if (auto attr = op->getAttrOfType<IntegerAttr>(name))
return attr.getInt();
return std::nullopt;
}
static LogicalResult getI64ArrayAttr(
Operation *op, StringRef name,
std::optional<SmallVector<int64_t>> &values) {
Attribute attr = op->getAttr(name);
if (!attr)
return success();
if (auto array = dyn_cast<DenseI64ArrayAttr>(attr)) {
values.emplace(array.asArrayRef());
return success();
}
auto elements = dyn_cast<DenseIntElementsAttr>(attr);
auto type = elements ? dyn_cast<RankedTensorType>(elements.getType())
: RankedTensorType();
if (!elements || !type || type.getRank() != 1
|| !type.getElementType().isInteger(64))
return op->emitOpError() << "has invalid " << name << " metadata";
values.emplace();
values->reserve(elements.getNumElements());
for (const APInt &value : elements.getValues<APInt>())
values->push_back(value.getSExtValue());
return success();
}
struct RealizedLogicalTransfer {
int64_t channelId = -1;
int64_t parentExchangeId = -1;
int64_t parentTransferCount = 0;
int64_t sourceCore = -1;
int64_t targetCore = -1;
};
static LogicalResult forEachRealizedLogicalTransfer(
Operation *op,
function_ref<LogicalResult(const RealizedLogicalTransfer &)> callback) {
auto scalarChannel = getI64Attr(op, "raptor.channel_id");
std::optional<SmallVector<int64_t>> batchChannels;
if (failed(getI64ArrayAttr(
op, "raptor.batch_channel_ids", batchChannels)))
return failure();
if (scalarChannel && batchChannels)
return op->emitOpError(
"mixes scalar and compact logical transfer metadata");
if (scalarChannel) {
auto exchange = getI64Attr(op, "raptor.exchange_id");
auto parent = getI64Attr(op, "raptor.parent_exchange_id");
auto count = getI64Attr(op, "raptor.parent_transfer_count");
auto source = getI64Attr(op, "raptor.source_core");
auto target = getI64Attr(op, "raptor.target_core");
if (!exchange || !parent || !count || !source || !target)
return op->emitOpError(
"is missing scalar logical transfer metadata");
RealizedLogicalTransfer transfer {
*scalarChannel, *parent, *count, *source, *target};
if (*exchange != transfer.channelId || transfer.channelId < 0
|| transfer.parentExchangeId < 0 || transfer.parentTransferCount <= 0
|| transfer.sourceCore < 0 || transfer.targetCore < 0)
return op->emitOpError("has invalid scalar logical transfer metadata");
return callback(transfer);
}
std::optional<SmallVector<int64_t>> sources, targets, parents, counts;
if (failed(getI64ArrayAttr(op, "raptor.batch_source_cores", sources))
|| failed(getI64ArrayAttr(op, "raptor.batch_target_cores", targets))
|| failed(getI64ArrayAttr(
op, "raptor.batch_parent_exchange_ids", parents))
|| failed(getI64ArrayAttr(
op, "raptor.batch_parent_transfer_counts", counts)))
return failure();
if (!batchChannels || !sources || !targets || !parents || !counts)
return op->emitOpError(
"is missing compact logical transfer metadata");
size_t size = batchChannels->size();
if (size == 0 || sources->size() != size || targets->size() != size
|| parents->size() != size || counts->size() != size)
return op->emitOpError(
"has non-parallel compact logical transfer metadata");
for (auto values : llvm::zip_equal(
*batchChannels, *parents, *counts, *sources, *targets)) {
RealizedLogicalTransfer transfer {
std::get<0>(values), std::get<1>(values), std::get<2>(values),
std::get<3>(values), std::get<4>(values)};
if (transfer.channelId < 0 || transfer.parentExchangeId < 0
|| transfer.parentTransferCount <= 0 || transfer.sourceCore < 0
|| transfer.targetCore < 0)
return op->emitOpError("has invalid compact logical transfer metadata");
if (failed(callback(transfer)))
return failure();
}
return success();
}
} // namespace
LogicalResult verifyPlannedCommunicationDeadlockFree(
Operation *anchor,
unsigned streamCount,
ArrayRef<unsigned> stepCounts,
ArrayRef<PlannedCommunicationTransfer> transfers) {
if (stepCounts.size() != streamCount)
return anchor->emitError("communication plan stream count does not match step counts");
SmallVector<SmallVector<Event>> streams(streamCount);
SmallVector<SmallVector<SmallVector<Event>>> atBoundary(streamCount);
for (unsigned stream = 0; stream < streamCount; ++stream)
atBoundary[stream].resize(stepCounts[stream] + 1);
for (const PlannedCommunicationTransfer &transfer : transfers) {
if (transfer.sourceStream >= streamCount || transfer.targetStream >= streamCount
|| transfer.producerStep >= stepCounts[transfer.sourceStream]
|| transfer.consumerStep >= stepCounts[transfer.targetStream]
|| transfer.sourceInsertionStep > stepCounts[transfer.sourceStream]
|| transfer.targetInsertionStep > stepCounts[transfer.targetStream]
|| transfer.sourceInsertionStep <= transfer.producerStep
|| transfer.targetInsertionStep > transfer.consumerStep)
return anchor->emitError("communication plan references an invalid stream step");
atBoundary[transfer.sourceStream][transfer.sourceInsertionStep].push_back(
{EventKind::Send, transfer.exchangeId});
atBoundary[transfer.targetStream][transfer.targetInsertionStep].push_back(
{EventKind::Receive, transfer.exchangeId});
}
for (unsigned stream = 0; stream < streamCount; ++stream) {
for (unsigned step = 0; step < stepCounts[stream]; ++step) {
llvm::append_range(streams[stream], atBoundary[stream][step]);
streams[stream].push_back({EventKind::Compute, 0});
}
llvm::append_range(streams[stream], atBoundary[stream].back());
}
return simulate(anchor, streams, "planned");
}
LogicalResult verifyRealizedCommunicationDeadlockFree(func::FuncOp funcOp) {
struct LogicalOperation {
Operation *op = nullptr;
RealizedLogicalTransfer transfer;
};
DenseMap<int64_t, SmallVector<LogicalOperation, 2>> operationsByExchange;
struct ParentExchange {
std::optional<int64_t> expectedTransfers;
DenseSet<int64_t> channels;
};
DenseMap<int64_t, ParentExchange> parentExchanges;
DenseMap<int64_t, unsigned> streamByCore;
SmallVector<int64_t> cores;
bool invalid = false;
funcOp.walk([&](Operation *op) {
if (!isa<SpatChannelSendOp, SpatChannelReceiveOp>(op))
return;
if (failed(forEachRealizedLogicalTransfer(
op, [&](const RealizedLogicalTransfer &transfer) -> LogicalResult {
operationsByExchange[transfer.channelId].push_back(
{op, transfer});
ParentExchange &parent =
parentExchanges[transfer.parentExchangeId];
if (parent.expectedTransfers
&& *parent.expectedTransfers
!= transfer.parentTransferCount)
return op->emitOpError(
"declares an inconsistent parent transfer count");
parent.expectedTransfers = transfer.parentTransferCount;
parent.channels.insert(transfer.channelId);
for (int64_t core : {transfer.sourceCore, transfer.targetCore})
if (!llvm::is_contained(cores, core))
cores.push_back(core);
return success();
})))
invalid = true;
});
llvm::sort(cores);
for (auto [index, core] : llvm::enumerate(cores))
streamByCore[core] = index;
SmallVector<SmallVector<Event>> streams(cores.size());
funcOp.walk([&](Operation *op) {
if (!isa<SpatChannelSendOp, SpatChannelReceiveOp>(op))
return;
if (failed(forEachRealizedLogicalTransfer(
op, [&](const RealizedLogicalTransfer &transfer) {
unsigned stream = streamByCore.lookup(
isa<SpatChannelSendOp>(op) ? transfer.sourceCore
: transfer.targetCore);
streams[stream].push_back(
{isa<SpatChannelSendOp>(op) ? EventKind::Send
: EventKind::Receive,
static_cast<uint64_t>(transfer.channelId)});
return success();
})))
invalid = true;
});
if (invalid)
return failure();
for (const auto &entry : parentExchanges)
if (!entry.second.expectedTransfers
|| entry.second.channels.size()
!= static_cast<size_t>(*entry.second.expectedTransfers))
return funcOp.emitOpError()
<< "parent exchange " << entry.first
<< " does not contain its declared lane transfer set";
for (const auto &entry : operationsByExchange) {
if (entry.second.size() != 2
|| isa<SpatChannelSendOp>(entry.second[0].op)
== isa<SpatChannelSendOp>(entry.second[1].op)) {
return funcOp.emitOpError()
<< "exchange " << entry.first
<< " does not have exactly one send and one receive (sends="
<< llvm::count_if(entry.second, [](const LogicalOperation &item) {
return isa<SpatChannelSendOp>(item.op);
})
<< ", receives="
<< llvm::count_if(entry.second, [](const LogicalOperation &item) {
return isa<SpatChannelReceiveOp>(item.op);
})
<< ")";
}
const LogicalOperation &first = entry.second[0];
const LogicalOperation &second = entry.second[1];
const LogicalOperation &sendRecord =
isa<SpatChannelSendOp>(first.op) ? first : second;
const LogicalOperation &receiveRecord =
isa<SpatChannelReceiveOp>(first.op) ? first : second;
auto send = cast<SpatChannelSendOp>(sendRecord.op);
auto receive = cast<SpatChannelReceiveOp>(receiveRecord.op);
if (send.getInput().getType() != receive.getOutput().getType())
return send.emitOpError("send and receive payload types do not match");
if (receiveRecord.transfer.sourceCore != sendRecord.transfer.sourceCore
|| receiveRecord.transfer.targetCore
!= sendRecord.transfer.targetCore
|| receiveRecord.transfer.parentExchangeId
!= sendRecord.transfer.parentExchangeId
|| receiveRecord.transfer.parentTransferCount
!= sendRecord.transfer.parentTransferCount)
return receive.emitOpError("receive core metadata does not match its send");
}
return simulate(funcOp, streams, "realized");
}
} // namespace onnx_mlir::spatial
@@ -0,0 +1,27 @@
#pragma once
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LLVM.h"
namespace onnx_mlir::spatial {
struct PlannedCommunicationTransfer {
uint64_t exchangeId = 0;
uint64_t parentExchangeId = 0;
unsigned sourceStream = 0;
unsigned targetStream = 0;
unsigned producerStep = 0;
unsigned consumerStep = 0;
unsigned sourceInsertionStep = 0;
unsigned targetInsertionStep = 0;
};
mlir::LogicalResult verifyPlannedCommunicationDeadlockFree(
mlir::Operation *anchor,
unsigned streamCount,
mlir::ArrayRef<unsigned> stepCounts,
mlir::ArrayRef<PlannedCommunicationTransfer> transfers);
mlir::LogicalResult verifyRealizedCommunicationDeadlockFree(mlir::func::FuncOp funcOp);
} // namespace onnx_mlir::spatial
@@ -0,0 +1,432 @@
#include "DeferredCommunicationPlanning.hpp"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/IRMapping.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "src/Accelerators/PIM/Common/IR/AffineUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapingUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
namespace onnx_mlir::spatial {
using namespace mlir;
namespace {
static Value getBlockOperand(Block &block, ValueRange operands, Value value, unsigned firstArgument = 0) {
auto it = llvm::find(operands, value);
assert(it != operands.end() && "missing scheduled operand");
return block.getArgument(firstArgument + std::distance(operands.begin(), it));
}
static FailureOr<Value> getOriginalProducerValue(const ProducerValueRef &producer) {
auto outputs = getComputeInstanceOutputValues(producer.instance);
if (producer.resultIndex >= outputs.size())
return failure();
return outputs[producer.resultIndex];
}
static SmallVector<Value> getBlueprintFragments(SpatBlueprintOp blueprint) {
SmallVector<Value> fragments {blueprint.getInput()};
llvm::append_range(fragments, blueprint.getFragments());
return fragments;
}
static FailureOr<Value> buildBlueprintReconstruction(
OpBuilder &builder, Location loc, SpatBlueprintOp blueprint,
ValueRange sourceBlockArgs) {
auto resultType = dyn_cast<RankedTensorType>(blueprint.getOutput().getType());
auto operandIndices = blueprint.getFragmentOperandIndices();
auto sourceSlots = blueprint.getFragmentSourceSlots();
auto sourceOffsets = blueprint.getFragmentSourceOffsets();
auto strides = blueprint.getFragmentStrides();
if (!resultType || !resultType.hasStaticShape() || !operandIndices ||
!sourceSlots || !sourceOffsets || !strides)
return blueprint.emitOpError("phase 1 requires complete static fragment assembly metadata"), failure();
int64_t rank = resultType.getRank();
ArrayRef<int64_t> offsets = blueprint.getFragmentOffsets();
ArrayRef<int64_t> sizes = blueprint.getFragmentSizes();
if (offsets.size() != sizes.size() || offsets.size() != strides->size() ||
offsets.size() != operandIndices->size() * rank ||
sourceSlots->size() != operandIndices->size() ||
sourceOffsets->size() != operandIndices->size())
return blueprint.emitOpError("phase 1 fragment assembly metadata has inconsistent sizes"), failure();
Value result = tensor::EmptyOp::create(builder, loc, resultType.getShape(),
resultType.getElementType());
for (auto [fragmentIndex, operandIndex] : llvm::enumerate(*operandIndices)) {
if (operandIndex < 0 || operandIndex >= static_cast<int64_t>(sourceBlockArgs.size()))
return blueprint.emitOpError("phase 1 fragment assembly operand index is out of range"), failure();
auto physicalType = dyn_cast<RankedTensorType>(sourceBlockArgs[operandIndex].getType());
if (!physicalType || !physicalType.hasStaticShape() || physicalType.getRank() != rank + 1)
return blueprint.emitOpError("phase 1 fragment assembly source is not a physical fragment batch"), failure();
SmallVector<int64_t> fragmentShape(physicalType.getShape().drop_front());
int64_t linearOffset = (*sourceOffsets)[fragmentIndex];
SmallVector<int64_t> sourceCoordinates(rank);
for (int64_t dim = rank - 1; dim >= 0; --dim) {
sourceCoordinates[dim] = linearOffset % fragmentShape[dim];
linearOffset /= fragmentShape[dim];
}
if (linearOffset != 0)
return blueprint.emitOpError("phase 1 fragment source offset is out of range"), failure();
SmallVector<OpFoldResult> sliceOffsets, sliceSizes, sliceStrides;
sliceOffsets.push_back(builder.getIndexAttr((*sourceSlots)[fragmentIndex]));
sliceSizes.push_back(builder.getIndexAttr(1));
sliceStrides.push_back(builder.getIndexAttr(1));
SmallVector<int64_t> selectedShape {1};
for (int64_t dim = 0; dim < rank; ++dim) {
int64_t index = fragmentIndex * rank + dim;
int64_t size = sizes[index];
if ((*strides)[index] != 1 || sourceCoordinates[dim] < 0 || size <= 0 ||
sourceCoordinates[dim] + size > fragmentShape[dim])
return blueprint.emitOpError("phase 1 fragment geometry is unsupported"), failure();
sliceOffsets.push_back(builder.getIndexAttr(sourceCoordinates[dim]));
sliceSizes.push_back(builder.getIndexAttr(size));
sliceStrides.push_back(builder.getIndexAttr(1));
selectedShape.push_back(size);
}
auto selectedType = RankedTensorType::get(selectedShape, resultType.getElementType());
Value selected = tensor::ExtractSliceOp::create(
builder, loc, selectedType, sourceBlockArgs[operandIndex], sliceOffsets,
sliceSizes, sliceStrides);
SmallVector<int64_t> fragmentResultShape(selectedShape.begin() + 1,
selectedShape.end());
auto fragmentType = RankedTensorType::get(fragmentResultShape,
resultType.getElementType());
SmallVector<ReassociationIndices> reassociation {{0, 1}};
for (int64_t dim = 1; dim < rank; ++dim)
reassociation.push_back({dim + 1});
Value fragment = tensor::CollapseShapeOp::create(
builder, loc, fragmentType, selected, reassociation);
SmallVector<OpFoldResult> targetOffsets, targetSizes, targetStrides;
for (int64_t dim = 0; dim < rank; ++dim) {
int64_t index = fragmentIndex * rank + dim;
targetOffsets.push_back(builder.getIndexAttr(offsets[index]));
targetSizes.push_back(builder.getIndexAttr(sizes[index]));
targetStrides.push_back(builder.getIndexAttr((*strides)[index]));
}
result = tensor::InsertSliceOp::create(builder, loc, fragment, result,
targetOffsets, targetSizes,
targetStrides);
}
return result;
}
static FailureOr<Value> buildIndexSwitchSelection(OpBuilder &builder, Location loc,
Value selector, ValueRange candidates,
Operation *diagnosticOwner) {
if (candidates.empty())
return diagnosticOwner->emitOpError("direct selection requires at least one candidate"), failure();
Type type = candidates.front().getType();
if (llvm::any_of(candidates, [&](Value candidate) { return candidate.getType() != type; }))
return diagnosticOwner->emitOpError("direct selection requires identical candidate types"), failure();
if (candidates.size() == 1)
return candidates.front();
SmallVector<int64_t> cases;
for (int64_t index = 0; index < static_cast<int64_t>(candidates.size()) - 1; ++index)
cases.push_back(index);
auto selection = scf::IndexSwitchOp::create(
builder, loc, TypeRange {type}, selector, cases, cases.size());
auto buildYield = [&](Region &region, Value candidate) {
OpBuilder::InsertionGuard guard(builder);
Block *block = builder.createBlock(&region);
builder.setInsertionPointToEnd(block);
scf::YieldOp::create(builder, loc, candidate);
};
for (auto [region, candidate] : llvm::zip(selection.getCaseRegions(), candidates.drop_back()))
buildYield(region, candidate);
// The scheduled-lane verifier guarantees an in-range selector, so default is
// the final lane without an otherwise-unreachable extra branch.
buildYield(selection.getDefaultRegion(), candidates.back());
return selection.getResult(0);
}
static FailureOr<Value> buildSelectedDeferredSource(OpBuilder &builder, Location loc,
SpatDeferredCommunicationOp transfer,
Value scheduledLane,
ValueRange sourceBlockArgs,
ArrayRef<int64_t> sourceOperandForScheduledLane) {
if (sourceBlockArgs.size() == 1)
return sourceBlockArgs.front();
if (!scheduledLane || sourceOperandForScheduledLane.empty())
return transfer.emitOpError("multiple deferred sources require the enclosing scheduled lane"), failure();
auto scheduled = transfer->getParentOfType<SpatScheduledComputeBatch>();
if (!scheduled || sourceOperandForScheduledLane.size() != static_cast<size_t>(scheduled.getLaneCount()))
return transfer.emitOpError("deferred source mapping must cover every scheduled lane"), failure();
SmallVector<Value> candidates;
candidates.reserve(sourceOperandForScheduledLane.size());
for (int64_t sourceIndex : sourceOperandForScheduledLane) {
if (sourceIndex < 0 || sourceIndex >= static_cast<int64_t>(sourceBlockArgs.size()))
return transfer.emitOpError("deferred source mapping operand is out of range"), failure();
candidates.push_back(sourceBlockArgs[sourceIndex]);
}
return buildIndexSwitchSelection(builder, loc, scheduledLane, candidates, transfer.getOperation());
}
static bool isDeferredPayloadCandidateOp(Operation *op) {
return isShapingOnlyOp(op) || isCompileTimeOp(op) || isPureIndexComputationOp(op);
}
static bool isTopLevelDeferredOperation(Operation *op, Block &body,
const DeferredInputPlan &plan) {
(void)plan;
return op->getBlock() == &body
&& (isDeferredPayloadCandidateOp(op) || isa<scf::ForOp>(op));
}
static bool isEligible(Value value, Block &body, const DeferredInputPlan &plan,
llvm::SmallPtrSetImpl<Operation *> &seen) {
if (value == plan.graphInput || value == plan.graphLane || value == plan.scheduledLane)
return true;
auto arg = dyn_cast<BlockArgument>(value);
if (arg)
return false;
Operation *op = value.getDefiningOp();
if (op && op->hasTrait<OpTrait::ConstantLike>())
return true;
if (!op || !isTopLevelDeferredOperation(op, body, plan) || !seen.insert(op).second)
return op && seen.contains(op);
return llvm::all_of(op->getOperands(), [&](Value operand) { return isEligible(operand, body, plan, seen); });
}
static FailureOr<Value> clonePayloadRoot(Value root, Block &body, const DeferredInputPlan &plan,
OpBuilder &builder, SpatDeferredCommunicationOp transfer,
Value selectedSource, Value boundGraphLane) {
IRMapping mapping;
mapping.map(plan.graphInput, selectedSource);
std::function<FailureOr<Value>(Value)> cloneScheduledLane = [&](Value value) -> FailureOr<Value> {
if (mapping.contains(value)) return mapping.lookup(value);
if (value == plan.scheduledLane) return value;
if (isa<BlockArgument>(value))
return transfer.emitOpError("phase 1 payload shaping captures an unsupported block argument"), failure();
Operation *op = value.getDefiningOp();
if (!op || (!isDeferredPayloadCandidateOp(op) && !op->hasTrait<OpTrait::ConstantLike>()))
return transfer.emitOpError("phase 1 cannot clone the scheduled graph-lane expression"), failure();
for (Value operand : op->getOperands()) if (failed(cloneScheduledLane(operand))) return failure();
Operation *copy = builder.clone(*op, mapping);
for (auto pair : llvm::zip(op->getResults(), copy->getResults())) mapping.map(std::get<0>(pair), std::get<1>(pair));
return mapping.lookup(value);
};
std::function<FailureOr<Value>(Value)> clone = [&](Value value) -> FailureOr<Value> {
if (mapping.contains(value)) return mapping.lookup(value);
if (value == plan.graphLane) {
auto mappedLane = cloneScheduledLane(boundGraphLane ? boundGraphLane : plan.scheduledGraphLane);
if (failed(mappedLane)) return failure();
mapping.map(value, *mappedLane);
return *mappedLane;
}
if (isa<BlockArgument>(value))
return transfer.emitOpError("phase 1 payload shaping captures an unsupported block argument"), failure();
Operation *op = value.getDefiningOp();
if (!op || (!isTopLevelDeferredOperation(op, body, plan) && !op->hasTrait<OpTrait::ConstantLike>()))
return transfer.emitOpError("phase 1 payload shaping contains an unsupported operation"), failure();
for (Value operand : op->getOperands()) if (failed(clone(operand))) return failure();
Operation *copy = builder.clone(*op, mapping);
for (auto pair : llvm::zip(op->getResults(), copy->getResults())) mapping.map(std::get<0>(pair), std::get<1>(pair));
return mapping.lookup(value);
};
return clone(root);
}
static bool dependsOnGraphLane(Value value, Value graphLane, Block &body,
const DeferredInputPlan &plan,
llvm::SmallPtrSetImpl<Operation *> &seen) {
if (value == graphLane)
return true;
Operation *op = value.getDefiningOp();
if (!op || !isTopLevelDeferredOperation(op, body, plan) || !seen.insert(op).second)
return false;
if (auto loop = dyn_cast<scf::ForOp>(op)) {
bool depends = false;
loop.getRegion().walk([&](Operation *nested) {
depends |= llvm::is_contained(nested->getOperands(), graphLane);
});
if (depends)
return true;
}
return llvm::any_of(op->getOperands(), [&](Value operand) {
return dependsOnGraphLane(operand, graphLane, body, plan, seen);
});
}
static void collectClosure(Value value, Block &body, const DeferredInputPlan &plan,
llvm::SmallPtrSetImpl<Operation *> &ops) {
Operation *op = value.getDefiningOp();
if (!op || !isTopLevelDeferredOperation(op, body, plan) || !ops.insert(op).second) return;
if (auto loop = dyn_cast<scf::ForOp>(op))
loop.getRegion().walk([&](Operation *nested) { ops.insert(nested); });
for (Value operand : op->getOperands())
if (operand != plan.graphInput && operand != plan.graphLane) collectClosure(operand, body, plan, ops);
}
} // namespace
bool isDeferredFragmentAssemblyInput(
Value input, const ComputeInstance &consumerInstance) {
auto blueprint = input.getDefiningOp<SpatBlueprintOp>();
if (!blueprint || blueprint.getMode() != "fragment_assembly")
return false;
return llvm::all_of(getBlueprintFragments(blueprint), [&](Value fragment) {
return getProducerValueRef(fragment, &consumerInstance).has_value();
});
}
LogicalResult prepareSingleCpuInput(OpBuilder &, Location loc, Value input, BlockArgument graphInput,
const ComputeInstance &consumerInstance, const MergeScheduleResult &,
ValueRange scheduledInputs, Block &block, unsigned firstInputArgument,
ArrayRef<ProducerValueKey> carriedKeys, Value graphLane, Value scheduledGraphLane,
DeferredInputPlan &plan) {
plan = {graphInput, {}, {}, {}, graphLane, scheduledGraphLane, {}, {}, {}, {}, 1, nullptr};
if (isDeferredFragmentAssemblyInput(input, consumerInstance)) {
plan.blueprint = input.getDefiningOp<SpatBlueprintOp>();
plan.originalSources = getBlueprintFragments(plan.blueprint);
return success();
}
auto producer = getProducerValueRef(input, &consumerInstance);
if (!producer) { plan.availableValue = getBlockOperand(block, scheduledInputs, input, firstInputArgument); return success(); }
ProducerValueKey key {producer->instance, producer->resultIndex};
auto carried = llvm::find(carriedKeys, key);
if (carried != carriedKeys.end()) {
plan.availableValue = block.getArgument(firstInputArgument + scheduledInputs.size() + std::distance(carriedKeys.begin(), carried));
return success();
}
auto source = getOriginalProducerValue(*producer);
if (failed(source)) return emitError(loc) << "cannot resolve original graph producer value";
plan.originalSources.push_back(*source);
return success();
}
LogicalResult prepareMultiCpuTupleInput(OpBuilder &, Location loc, Value input, BlockArgument graphInput,
const ComputeStepTuple &tuple, const PeftClassPlan &,
const MergeScheduleResult &, ValueRange scheduledInputs, Block &block,
unsigned firstInputArgument, Value graphLane, Value scheduledGraphLane, Value scheduledLane,
DeferredInputPlan &plan) {
const ComputeInstance &representative = tuple.instances.front();
plan = {graphInput, {}, {}, {}, graphLane, scheduledGraphLane, scheduledLane, {}, {}, {}, 1, nullptr};
if (isDeferredFragmentAssemblyInput(input, representative)) {
plan.blueprint = input.getDefiningOp<SpatBlueprintOp>();
plan.originalSources = getBlueprintFragments(plan.blueprint);
return success();
}
auto producer = getProducerValueRef(input, &representative);
if (!producer) { plan.availableValue = getBlockOperand(block, scheduledInputs, input, firstInputArgument); return success(); }
auto inputs = getComputeInstanceInputs(representative);
auto it = llvm::find(inputs, input);
if (it == inputs.end()) return emitError(loc) << "cannot resolve scheduled batch step input";
unsigned inputIndex = std::distance(inputs.begin(), it);
for (const ComputeInstance &instance : tuple.instances) {
auto laneInputs = getComputeInstanceInputs(instance);
if (inputIndex >= laneInputs.size()) return emitError(loc) << "scheduled batch step input out of range";
auto laneProducer = getProducerValueRef(laneInputs[inputIndex], &instance);
if (!laneProducer) return emitError(loc) << "scheduled batch step mixes host and producer inputs";
auto source = getOriginalProducerValue(*laneProducer);
if (failed(source)) return emitError(loc) << "cannot resolve original graph producer value";
auto sourceIt = llvm::find(plan.originalSources, *source);
if (sourceIt == plan.originalSources.end()) { plan.sourceOperandForScheduledLane.push_back(plan.originalSources.size()); plan.originalSources.push_back(*source); }
else plan.sourceOperandForScheduledLane.push_back(std::distance(plan.originalSources.begin(), sourceIt));
}
return success();
}
LogicalResult materializeDeferredPayloadDemands(OpBuilder &builder, Location loc, Block &body,
ArrayRef<DeferredInputPlan> plans, IRMapping &mapper,
llvm::SmallPtrSetImpl<Operation *> &absorbed) {
for (const DeferredInputPlan &plan : plans) {
if (plan.availableValue) { mapper.map(plan.graphInput, plan.availableValue); continue; }
SmallVector<Value> roots;
bool needsIdentity = false;
SmallVector<Value> worklist {plan.graphInput};
llvm::SmallDenseSet<Value, 32> seen;
while (!worklist.empty()) {
Value value = worklist.pop_back_val();
if (!seen.insert(value).second) continue;
for (OpOperand &use : value.getUses()) {
Operation *user = use.getOwner();
if (!isTopLevelDeferredOperation(user, body, plan)) { needsIdentity = true; continue; }
llvm::SmallPtrSet<Operation *, 16> eligibility;
if (!isEligible(user->getResult(0), body, plan, eligibility)) { needsIdentity = true; continue; }
for (Value result : user->getResults()) {
bool hasShapingUse = llvm::any_of(result.getUses(), [&](OpOperand &next) { return isTopLevelDeferredOperation(next.getOwner(), body, plan); });
bool hasOtherUse = llvm::any_of(result.getUses(), [&](OpOperand &next) { return !isTopLevelDeferredOperation(next.getOwner(), body, plan); });
if (hasOtherUse) roots.push_back(result);
if (hasShapingUse) worklist.push_back(result);
}
}
}
if (needsIdentity) roots.push_back(plan.graphInput);
llvm::sort(roots, [](Value a, Value b) { return a.getAsOpaquePointer() < b.getAsOpaquePointer(); });
roots.erase(std::unique(roots.begin(), roots.end()), roots.end());
for (Value root : roots) {
llvm::SmallPtrSet<Operation *, 16> laneDependencies;
bool scalarize = plan.scalarizedGraphLaneBase
&& dependsOnGraphLane(root, plan.graphLane, body, plan, laneDependencies);
OpBuilder::InsertPoint restore = builder.saveInsertionPoint();
Operation *loop = nullptr;
if (scalarize) {
loop = builder.getInsertionBlock()->getParentOp();
if (loop && !isa<scf::ForOp>(loop))
loop = loop->getParentOfType<scf::ForOp>();
if (loop)
builder.setInsertionPoint(loop);
else if (plan.scalarizedHoistBlock)
builder.setInsertionPointToEnd(plan.scalarizedHoistBlock);
else
return emitError(loc) << "phase 1 scalarized deferred payload is missing a hoist point";
}
SmallVector<Value> payloads;
unsigned count = scalarize ? plan.scalarizedLaneCount : 1;
for (unsigned offset = 0; offset < count; ++offset) {
auto transfer = SpatDeferredCommunicationOp::create(builder, loc, root.getType(), plan.originalSources);
Block *deferred = builder.createBlock(&transfer.getBody(), transfer.getBody().end(),
TypeRange {transfer.getSources().getTypes()}, SmallVector<Location>(transfer.getSources().size(), loc));
builder.setInsertionPointToStart(deferred);
auto selected = plan.blueprint
? buildBlueprintReconstruction(builder, loc, plan.blueprint,
deferred->getArguments())
: buildSelectedDeferredSource(builder, loc, transfer,
plan.scheduledLane,
deferred->getArguments(),
plan.sourceOperandForScheduledLane);
if (failed(selected)) return failure();
Value boundGraphLane;
if (scalarize) {
boundGraphLane = affineAddConst(
builder, loc, plan.scalarizedGraphLaneBase, offset, transfer.getOperation());
}
auto payload = clonePayloadRoot(root, body, plan, builder, transfer, *selected, boundGraphLane);
if (failed(payload)) return failure();
SpatYieldOp::create(builder, loc, *payload);
payloads.push_back(transfer.getOutput());
builder.setInsertionPointAfter(transfer);
}
if (scalarize) {
builder.restoreInsertionPoint(restore);
auto selected = buildIndexSwitchSelection(
builder, loc, plan.scalarizedLocalLane, payloads, root.getDefiningOp());
if (failed(selected)) return failure();
mapper.map(root, *selected);
} else {
mapper.map(root, payloads.front());
}
collectClosure(root, body, plan, absorbed);
}
}
SmallVector<Operation *> notFullyAbsorbed;
for (Operation *op : absorbed) {
bool allResultsMapped = llvm::all_of(op->getResults(), [&](Value result) {
return mapper.contains(result) || llvm::all_of(result.getUses(), [&](OpOperand &use) { return absorbed.contains(use.getOwner()); });
});
if (!allResultsMapped)
notFullyAbsorbed.push_back(op);
}
for (Operation *op : notFullyAbsorbed)
absorbed.erase(op);
return success();
}
} // namespace onnx_mlir::spatial
@@ -0,0 +1,54 @@
#pragma once
#include "ScheduledComputePlan.hpp"
namespace onnx_mlir {
namespace spatial {
// A graph input is either already available in the scheduled block, or is a
// graph result whose individual deterministic payloads are materialized later.
struct DeferredInputPlan {
BlockArgument graphInput;
Value availableValue;
SmallVector<Value> originalSources;
SmallVector<int64_t> sourceOperandForScheduledLane;
Value graphLane;
Value scheduledGraphLane;
Value scheduledLane;
SpatBlueprintOp blueprint;
Value scalarizedLocalLane;
Value scalarizedGraphLaneBase;
int64_t scalarizedLaneCount = 1;
Block *scalarizedHoistBlock = nullptr;
};
bool isDeferredFragmentAssemblyInput(Value input,
const ComputeInstance &consumerInstance);
LogicalResult prepareSingleCpuInput(OpBuilder &builder, Location loc, Value input,
BlockArgument graphInput,
const ComputeInstance &consumerInstance,
const MergeScheduleResult &schedule,
ValueRange scheduledInputs, Block &block,
unsigned firstInputArgument,
ArrayRef<ProducerValueKey> carriedKeys,
Value graphLane, Value scheduledGraphLane,
DeferredInputPlan &plan);
LogicalResult prepareMultiCpuTupleInput(OpBuilder &builder, Location loc, Value input,
BlockArgument graphInput,
const ComputeStepTuple &stepTuple,
const PeftClassPlan &peftClassPlan,
const MergeScheduleResult &schedule,
ValueRange scheduledInputs, Block &block,
unsigned firstInputArgument, Value graphLane, Value scheduledGraphLane,
Value scheduledLane, DeferredInputPlan &plan);
LogicalResult materializeDeferredPayloadDemands(OpBuilder &builder, Location loc,
Block &graphBody,
ArrayRef<DeferredInputPlan> plans,
IRMapping &mapper,
llvm::SmallPtrSetImpl<Operation *> &absorbed);
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,9 @@
#pragma once
#include "mlir/Dialect/Func/IR/FuncOps.h"
namespace onnx_mlir::spatial {
mlir::LogicalResult realizeDeferredCommunication(mlir::func::FuncOp funcOp);
} // namespace onnx_mlir::spatial
@@ -0,0 +1,713 @@
#include "DeferredProjectionAnalysis.hpp"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/Matchers.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "src/Accelerators/PIM/Common/IR/ShapingUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/CompileTime.hpp"
#include <limits>
namespace onnx_mlir::spatial {
using namespace mlir;
namespace {
static FailureOr<int64_t> getSignedInt64(IntegerAttr value) {
return value.getValue().isSignedIntN(64) ? FailureOr<int64_t>(value.getValue().getSExtValue())
: FailureOr<int64_t>(failure());
}
static FailureOr<int64_t> evaluate(Value value, const StaticIndexEnvironment &environment,
llvm::SmallDenseSet<Value, 16> &visiting);
static FailureOr<int64_t> evaluateDenseExtract(tensor::ExtractOp extract,
const StaticIndexEnvironment &environment,
llvm::SmallDenseSet<Value, 16> &visiting) {
auto constant = extract.getTensor().getDefiningOp<arith::ConstantOp>();
auto elements = constant ? dyn_cast<DenseIntElementsAttr>(constant.getValue()) : DenseIntElementsAttr();
auto type = elements ? dyn_cast<RankedTensorType>(elements.getType()) : RankedTensorType();
if (!elements || !type || !type.hasStaticShape()
|| extract.getIndices().size() != static_cast<size_t>(type.getRank()))
return failure();
int64_t linear = 0;
for (auto [index, dim] : llvm::zip(extract.getIndices(), type.getShape())) {
auto folded = evaluate(index, environment, visiting);
if (failed(folded) || *folded < 0 || *folded >= dim
|| llvm::MulOverflow(linear, dim, linear) || llvm::AddOverflow(linear, *folded, linear))
return failure();
}
APInt value = elements.getValues<APInt>()[linear];
return value.isSignedIntN(64) ? FailureOr<int64_t>(value.getSExtValue())
: FailureOr<int64_t>(failure());
}
static FailureOr<int64_t> evaluate(Value value, const StaticIndexEnvironment &environment,
llvm::SmallDenseSet<Value, 16> &visiting) {
if (auto it = environment.bindings.find(value); it != environment.bindings.end())
return it->second;
Attribute constant;
if (matchPattern(value, m_Constant(&constant)))
if (auto integer = dyn_cast_or_null<IntegerAttr>(constant))
return getSignedInt64(integer);
if (isa<BlockArgument>(value) || !value.getDefiningOp())
return failure();
if (!visiting.insert(value).second)
return failure();
if (auto extract = value.getDefiningOp<tensor::ExtractOp>()) {
auto result = evaluateDenseExtract(extract, environment, visiting);
visiting.erase(value);
return result;
}
Operation *definingOp = value.getDefiningOp();
if (definingOp->getNumRegions() != 0 || !isPureIndexComputationOp(definingOp)) {
visiting.erase(value);
return failure();
}
SmallVector<Attribute> operandConstants;
operandConstants.reserve(definingOp->getNumOperands());
Builder builder(definingOp->getContext());
for (Value operand : definingOp->getOperands()) {
auto folded = evaluate(operand, environment, visiting);
if (failed(folded)) {
visiting.erase(value);
return failure();
}
operandConstants.push_back(builder.getIntegerAttr(operand.getType(), *folded));
}
SmallVector<OpFoldResult> results;
if (failed(definingOp->fold(operandConstants, results)) || results.size() != 1) {
visiting.erase(value);
return failure();
}
FailureOr<int64_t> result = failure();
if (auto integer = dyn_cast<Attribute>(results.front())) {
if (auto attr = dyn_cast<IntegerAttr>(integer))
result = getSignedInt64(attr);
} else if (auto foldedValue = dyn_cast<Value>(results.front())) {
result = evaluate(foldedValue, environment, visiting);
}
visiting.erase(value);
return result;
}
static FailureOr<std::optional<unsigned>> sourceArgument(Value value, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment) {
while (auto cast = value.getDefiningOp<tensor::CastOp>()) value = cast.getSource();
if (auto argument = dyn_cast<BlockArgument>(value);
argument && argument.getOwner() == &deferred.getBody().front()
&& argument.getArgNumber() < deferred.getSources().size())
return std::optional<unsigned>(argument.getArgNumber());
auto result = dyn_cast<OpResult>(value);
auto selection = result ? dyn_cast<scf::IndexSwitchOp>(result.getOwner()) : scf::IndexSwitchOp();
if (!selection || result.getResultNumber() != 0 || selection.getNumResults() != 1)
return std::optional<unsigned>();
auto selector = evaluateDeferredIndex(selection.getArg(), environment);
if (failed(selector))
return failure();
Region *selectedRegion = &selection.getDefaultRegion();
for (auto [caseValue, region] : llvm::zip(selection.getCases(), selection.getCaseRegions()))
if (caseValue == *selector) {
selectedRegion = &region;
break;
}
if (!selectedRegion->hasOneBlock())
return failure();
Block &block = selectedRegion->front();
auto yield = dyn_cast<scf::YieldOp>(block.getTerminator());
if (!yield || yield.getResults().size() != 1)
return failure();
for (Operation &op : block.without_terminator())
if (!isa<tensor::CastOp>(op))
return failure();
return sourceArgument(yield.getResults().front(), deferred, environment);
}
static SpatGraphComputeBatch graphBatchOwner(Value value) {
if (auto result = dyn_cast<OpResult>(value))
return dyn_cast<SpatGraphComputeBatch>(result.getOwner());
return {};
}
static Value getEnclosingScheduledLane(SpatDeferredCommunicationOp deferred,
SpatScheduledComputeBatch scheduled) {
Block *block = deferred->getBlock();
while (block && block->getParentOp() != scheduled) {
Operation *parent = block->getParentOp();
block = parent ? parent->getBlock() : nullptr;
}
return block && !block->empty() ? block->getArgument(0) : Value();
}
static bool isInsideDeferredLoop(Operation *op,
SpatDeferredCommunicationOp deferred) {
for (Operation *parent = op->getParentOp(); parent && parent != deferred;
parent = parent->getParentOp())
if (isa<scf::ForOp>(parent))
return true;
return false;
}
static bool isAllowedStaticIndexExpression(
Value value, Value scheduledLane,
llvm::SmallDenseSet<Value, 16> &visiting) {
if (value == scheduledLane)
return true;
Attribute constant;
if (matchPattern(value, m_Constant(&constant)))
return true;
if (isa<BlockArgument>(value) || !value.getDefiningOp()
|| !visiting.insert(value).second)
return false;
Operation *op = value.getDefiningOp();
bool allowed = op->getNumRegions() == 0
&& (isPureIndexComputationOp(op) || isCompileTimeOp(op))
&& llvm::all_of(op->getOperands(), [&](Value operand) {
return isAllowedStaticIndexExpression(operand, scheduledLane,
visiting);
});
visiting.erase(value);
return allowed;
}
static bool isAllowedStaticIndexExpression(Value value, Value scheduledLane) {
llvm::SmallDenseSet<Value, 16> visiting;
return isAllowedStaticIndexExpression(value, scheduledLane, visiting);
}
static bool originatesFromDeferredSource(
Value value, SpatDeferredCommunicationOp deferred,
llvm::SmallDenseSet<Value, 16> &visited) {
if (!visited.insert(value).second)
return false;
if (auto argument = dyn_cast<BlockArgument>(value))
return argument.getOwner() == &deferred.getBody().front()
&& argument.getArgNumber() < deferred.getSources().size();
Operation *op = value.getDefiningOp();
if (!op)
return false;
if (isa<scf::IndexSwitchOp>(op) && op->getBlock() == &deferred.getBody().front())
return true;
return llvm::any_of(op->getOperands(), [&](Value operand) {
return originatesFromDeferredSource(operand, deferred, visited);
});
}
static bool originatesFromDeferredSource(
Value value, SpatDeferredCommunicationOp deferred) {
llvm::SmallDenseSet<Value, 16> visited;
return originatesFromDeferredSource(value, deferred, visited);
}
static LogicalResult verifyCanonicalSourceSelector(
scf::IndexSwitchOp selection, SpatDeferredCommunicationOp deferred,
Value scheduledLane, int64_t laneCount) {
if (selection->getBlock() != &deferred.getBody().front()
|| selection.getNumResults() != 1
|| !selection.getArg().getType().isIndex()
|| !isAllowedStaticIndexExpression(selection.getArg(), scheduledLane))
return selection.emitOpError("is not a canonical deferred source selector");
if (laneCount < 2
|| selection.getCases().size() != static_cast<size_t>(laneCount - 1)
|| selection.getCaseRegions().size() != selection.getCases().size())
return selection.emitOpError(
"must cover every non-default scheduled lane");
for (auto [index, caseValue] : llvm::enumerate(selection.getCases()))
if (caseValue != static_cast<int64_t>(index))
return selection.emitOpError(
"must use consecutive scheduled-lane cases starting at zero");
auto verifyRegion = [&](Region &region) -> LogicalResult {
if (!region.hasOneBlock())
return selection.emitOpError("source-selector region must have one block");
Block &block = region.front();
auto yield = dyn_cast<scf::YieldOp>(block.getTerminator());
if (!yield || yield.getResults().size() != 1
|| yield.getResults().front().getType() != selection.getResult(0).getType())
return selection.emitOpError(
"source-selector region must yield one exact result type");
for (Operation &op : block.without_terminator())
if (!isa<tensor::CastOp>(op))
return selection.emitOpError(
"source-selector regions may contain only tensor.cast before scf.yield");
Value source = yield.getResults().front();
while (auto cast = source.getDefiningOp<tensor::CastOp>()) {
if (cast->getBlock() != &block)
return selection.emitOpError(
"source-selector casts must be local to their region");
source = cast.getSource();
}
auto argument = dyn_cast<BlockArgument>(source);
if (!argument || argument.getOwner() != &deferred.getBody().front()
|| argument.getArgNumber() >= deferred.getSources().size())
return selection.emitOpError(
"source-selector branch must resolve to a deferred source argument");
return success();
};
for (Region &region : selection.getCaseRegions())
if (failed(verifyRegion(region)))
return failure();
return verifyRegion(selection.getDefaultRegion());
}
static bool valueDependsOnAny(
Value value, const llvm::SmallDenseSet<Value, 16> &dependencies,
llvm::SmallDenseSet<Value, 16> &visited) {
if (dependencies.contains(value))
return true;
if (!visited.insert(value).second)
return false;
Operation *op = value.getDefiningOp();
return op && llvm::any_of(op->getOperands(), [&](Value operand) {
return valueDependsOnAny(operand, dependencies, visited);
});
}
static bool valueDependsOnAny(
Value value, const llvm::SmallDenseSet<Value, 16> &dependencies) {
llvm::SmallDenseSet<Value, 16> visited;
return valueDependsOnAny(value, dependencies, visited);
}
static LogicalResult specializeDeferredLoopStaticValues(
scf::ForOp loop, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment,
SpecializedDeferredProgram &program,
llvm::SmallDenseSet<Value, 16> dynamicIndices = {}) {
auto record = [&](Value value, StringRef diagnostic) -> LogicalResult {
auto folded = evaluateDeferredIndex(value, environment);
if (failed(folded))
return deferred.emitOpError(diagnostic);
program.staticValues.try_emplace(value, *folded);
return success();
};
auto step = evaluateDeferredIndex(loop.getStep(), environment);
if (failed(record(loop.getLowerBound(),
"deferred shaping loop lower bound did not specialize"))
|| failed(record(loop.getUpperBound(),
"deferred shaping loop upper bound did not specialize"))
|| failed(step) || *step <= 0)
return deferred.emitOpError(
"deferred shaping loop requires specialized bounds and a positive step");
program.staticValues.try_emplace(loop.getStep(), *step);
dynamicIndices.insert(loop.getInductionVar());
for (Operation &nested : loop.getBody()->without_terminator()) {
if (auto nestedLoop = dyn_cast<scf::ForOp>(nested)) {
if (failed(specializeDeferredLoopStaticValues(
nestedLoop, deferred, environment, program, dynamicIndices)))
return failure();
continue;
}
for (Value operand : nested.getOperands()) {
if ((!operand.getType().isIndex()
&& !isa<IntegerType>(operand.getType()))
|| valueDependsOnAny(operand, dynamicIndices))
continue;
if (failed(record(operand,
"deferred shaping loop captured an index that did not specialize")))
return failure();
}
}
return success();
}
static FailureOr<std::optional<DeferredInsertAssembly>>
analyzeDeferredInsertAssembly(
const SpecializedDeferredProgram &program,
const StaticIndexEnvironment &environment) {
auto finalInsert = program.yieldedValue.getDefiningOp<tensor::InsertSliceOp>();
if (!finalInsert || program.leaves.empty())
return std::optional<DeferredInsertAssembly>();
DenseMap<Value, unsigned> leafByRoot;
for (auto [leafIndex, leaf] : llvm::enumerate(program.leaves)) {
if (leaf.physicalSlots.size() != 1
|| !leafByRoot.try_emplace(leaf.replacementRoot, leafIndex).second)
return std::optional<DeferredInsertAssembly>();
}
SmallPtrSet<Operation *, 32> consumed;
SmallVector<DeferredInsertAssemblyEntry> reverseEntries;
llvm::SmallDenseSet<unsigned, 16> insertedLeaves;
Value current = program.yieldedValue;
while (auto insert = current.getDefiningOp<tensor::InsertSliceOp>()) {
Value source = insert.getSource();
SmallVector<Operation *> rankShaping;
while (Operation *shape = source.getDefiningOp()) {
if (auto collapse = dyn_cast<tensor::CollapseShapeOp>(shape))
source = collapse.getSrc();
else if (auto expand = dyn_cast<tensor::ExpandShapeOp>(shape))
source = expand.getSrc();
else
break;
rankShaping.push_back(shape);
}
auto leafIt = leafByRoot.find(source);
if (leafIt == leafByRoot.end()
|| !insertedLeaves.insert(leafIt->second).second)
return std::optional<DeferredInsertAssembly>();
DeferredInsertAssemblyEntry entry;
entry.leafIndex = leafIt->second;
entry.requirementIndex = leafIt->second;
auto foldGeometry = [&](ArrayRef<OpFoldResult> values,
SmallVectorImpl<int64_t> &folded) {
for (OpFoldResult value : values) {
auto result = evaluateDeferredIndex(value, environment);
if (failed(result))
return failure();
folded.push_back(*result);
}
return success();
};
if (failed(foldGeometry(insert.getMixedOffsets(),
entry.targetGeometry.offsets))
|| failed(foldGeometry(insert.getMixedSizes(),
entry.targetGeometry.sizes))
|| failed(foldGeometry(insert.getMixedStrides(),
entry.targetGeometry.strides)))
return std::optional<DeferredInsertAssembly>();
reverseEntries.push_back(std::move(entry));
consumed.insert(insert);
consumed.insert(rankShaping.begin(), rankShaping.end());
current = insert.getDest();
}
auto initial = current.getDefiningOp<tensor::EmptyOp>();
auto resultType = dyn_cast<RankedTensorType>(program.yieldedValue.getType());
if (!initial || !initial.getDynamicSizes().empty() || !resultType
|| !resultType.hasStaticShape() || initial.getType() != resultType
|| insertedLeaves.size() != program.leaves.size())
return std::optional<DeferredInsertAssembly>();
consumed.insert(initial);
for (const DeferredInsertAssemblyEntry &entry : reverseEntries) {
const StaticSliceGeometry &geometry = entry.targetGeometry;
if (geometry.offsets.size() != static_cast<size_t>(resultType.getRank())
|| geometry.sizes.size() != geometry.offsets.size()
|| geometry.strides.size() != geometry.offsets.size())
return std::optional<DeferredInsertAssembly>();
for (auto [offset, size, stride, dim] :
llvm::zip_equal(geometry.offsets, geometry.sizes,
geometry.strides, resultType.getShape())) {
int64_t span = 0;
int64_t last = 0;
if (offset < 0 || size <= 0 || stride <= 0
|| llvm::MulOverflow(size - 1, stride, span)
|| llvm::AddOverflow(offset, span, last) || last >= dim)
return std::optional<DeferredInsertAssembly>();
}
}
if (llvm::any_of(program.residualOps,
[&](Operation *op) { return !consumed.contains(op); })
|| consumed.size() != program.residualOps.size())
return std::optional<DeferredInsertAssembly>();
DeferredInsertAssembly assembly;
assembly.initialValue = initial;
assembly.resultType = resultType;
assembly.entries.assign(reverseEntries.rbegin(), reverseEntries.rend());
return std::optional<DeferredInsertAssembly>(std::move(assembly));
}
} // namespace
LogicalResult verifyDeferredProgramContract(
SpatDeferredCommunicationOp deferred) {
if (!deferred.getBody().hasOneBlock())
return deferred.emitOpError("deferred program must have exactly one body block");
Block &body = deferred.getBody().front();
auto terminator = dyn_cast<SpatYieldOp>(body.getTerminator());
if (!terminator || terminator.getOutputs().size() != 1)
return deferred.emitOpError(
"deferred program must have exactly one yielded value");
Value scheduledLane;
int64_t laneCount = 1;
if (auto scheduled = deferred->getParentOfType<SpatScheduledComputeBatch>()) {
scheduledLane = getEnclosingScheduledLane(deferred, scheduled);
laneCount = scheduled.getLaneCount();
if (!scheduledLane || laneCount <= 0)
return deferred.emitOpError(
"deferred program cannot locate a valid enclosing scheduled lane");
}
bool invalid = false;
WalkResult result = deferred.getBody().walk([&](Operation *op) {
if (invalid)
return WalkResult::interrupt();
auto reject = [&](StringRef message) {
op->emitOpError(message);
invalid = true;
return WalkResult::interrupt();
};
if (auto yield = dyn_cast<SpatYieldOp>(op)) {
if (op != body.getTerminator())
return reject("must be the unique top-level deferred terminator");
return WalkResult::advance();
}
if (auto yield = dyn_cast<scf::YieldOp>(op)) {
Operation *parent = op->getParentOp();
if (!parent || !isa<scf::ForOp, scf::IndexSwitchOp>(parent)
|| op != op->getBlock()->getTerminator())
return reject("is not a valid deferred control-flow terminator");
return WalkResult::advance();
}
if (auto selection = dyn_cast<scf::IndexSwitchOp>(op)) {
if (failed(verifyCanonicalSourceSelector(
selection, deferred, scheduledLane, laneCount))) {
invalid = true;
return WalkResult::interrupt();
}
return WalkResult::advance();
}
if (auto loop = dyn_cast<scf::ForOp>(op)) {
auto isStaticRankedTensor = [](Type type) {
auto tensor = dyn_cast<RankedTensorType>(type);
return tensor && tensor.hasStaticShape();
};
if (!llvm::all_of(loop.getInitArgs(), [&](Value value) {
return isStaticRankedTensor(value.getType());
})
|| !llvm::all_of(loop.getResultTypes(), isStaticRankedTensor))
return reject(
"requires static ranked tensor iter arguments and results");
auto yield = dyn_cast<scf::YieldOp>(loop.getBody()->getTerminator());
if (!yield || yield.getResults().getTypes() != loop.getResultTypes())
return reject("yield types must exactly match loop result types");
for (Value bound : {loop.getLowerBound(), loop.getUpperBound(),
loop.getStep()})
if (!isAllowedStaticIndexExpression(bound, scheduledLane))
return reject(
"bounds must use only constants, the scheduled lane, and pure index computation");
for (int64_t lane = 0; lane < laneCount; ++lane) {
StaticIndexEnvironment environment;
if (scheduledLane)
environment.bindings[scheduledLane] = lane;
auto lower = evaluateDeferredIndex(loop.getLowerBound(), environment);
auto upper = evaluateDeferredIndex(loop.getUpperBound(), environment);
auto step = evaluateDeferredIndex(loop.getStep(), environment);
if (failed(lower) || failed(upper) || failed(step) || *step <= 0)
return reject(
"bounds must specialize for every scheduled lane with a positive step");
}
return WalkResult::advance();
}
if (op->getNumRegions() != 0)
return reject("unsupported region operation in deferred program");
if (!isShapingOnlyOp(op) && !isCompileTimeOp(op)
&& !isPureIndexComputationOp(op))
return reject(
"deferred program permits only shaping, compile-time, or pure index operations");
for (Value operand : op->getOperands()) {
if (auto argument = dyn_cast<BlockArgument>(operand)) {
Operation *owner = argument.getOwner()->getParentOp();
bool local = owner == deferred
|| (owner && deferred->isAncestor(owner));
if (!local && operand != scheduledLane)
return reject("captures an unsupported external block argument");
}
if (isInsideDeferredLoop(op, deferred)
&& originatesFromDeferredSource(operand, deferred))
return reject(
"deferred source projection must remain outside residual loops");
}
return WalkResult::advance();
});
return success(!invalid && !result.wasInterrupted());
}
FailureOr<int64_t> evaluateDeferredIndex(Value value, const StaticIndexEnvironment &environment) {
llvm::SmallDenseSet<Value, 16> visiting;
return evaluate(value, environment, visiting);
}
FailureOr<int64_t> evaluateDeferredIndex(OpFoldResult value, const StaticIndexEnvironment &environment) {
if (auto attr = dyn_cast<Attribute>(value))
if (auto integer = dyn_cast<IntegerAttr>(attr)) return getSignedInt64(integer);
if (auto dynamic = dyn_cast<Value>(value)) return evaluateDeferredIndex(dynamic, environment);
return failure();
}
FailureOr<std::optional<ResolvedDeferredSource>> tryResolveDeferredSource(Value value, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment) {
auto index = sourceArgument(value, deferred, environment);
if (failed(index))
return failure();
if (!*index)
return std::optional<ResolvedDeferredSource>();
return std::optional<ResolvedDeferredSource>(ResolvedDeferredSource {**index, deferred.getSources()[**index]});
}
FailureOr<ResolvedDeferredSource> requireResolvedDeferredSource(Value value, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment) {
auto source = tryResolveDeferredSource(value, deferred, environment);
if (failed(source) || !*source)
return deferred.emitOpError("cannot statically resolve deferred source selection"), failure();
return **source;
}
FailureOr<SpecializedDeferredProgram> analyzeDeferredProgram(SpatDeferredCommunicationOp deferred,
std::optional<unsigned> targetScheduledLane) {
// The Phase 1 verifier must establish the deferred-program contract once;
// this analysis only specializes lane-dependent static semantics.
Block &body = deferred.getBody().front();
auto yield = dyn_cast<SpatYieldOp>(body.getTerminator());
if (!yield || yield.getOutputs().size() != 1)
return deferred.emitOpError("requires exactly one deferred yielded value"), failure();
StaticIndexEnvironment environment;
if (auto scheduled = deferred->getParentOfType<SpatScheduledComputeBatch>()) {
if (!targetScheduledLane) return deferred.emitOpError("scheduled-batch deferred program requires lane specialization"), failure();
Value scheduledLane = getEnclosingScheduledLane(deferred, scheduled);
if (!scheduledLane)
return deferred.emitOpError("cannot locate the enclosing scheduled lane"), failure();
environment.bindings[scheduledLane] = *targetScheduledLane;
} else if (targetScheduledLane) return deferred.emitOpError("scalar deferred program cannot have a target lane"), failure();
SpecializedDeferredProgram program;
program.deferred = deferred;
program.targetScheduledLane = targetScheduledLane;
if (auto scheduled = deferred->getParentOfType<SpatScheduledComputeBatch>())
program.scheduledLane = getEnclosingScheduledLane(deferred, scheduled);
program.yieldedValue = yield.getOutputs().front();
llvm::SmallDenseSet<Value, 32> visited;
std::function<LogicalResult(Value)> visit = [&](Value value) -> LogicalResult {
if (!visited.insert(value).second) return success();
// A graph-batch projection is semantically different from the canonical
// source-selector scaffold even though both contain extract/collapse ops.
if (auto slice = value.getDefiningOp<tensor::ExtractSliceOp>()) {
auto source = tryResolveDeferredSource(slice.getSource(), deferred, environment);
if (failed(source)) return failure();
if (*source && graphBatchOwner((*source)->selectedValue)) {
auto type = dyn_cast<RankedTensorType>((*source)->selectedValue.getType());
auto result = dyn_cast<RankedTensorType>(slice.getResult().getType());
if (!type || !result || type.getRank() == 0) return deferred.emitOpError("graph projection requires ranked tensors");
auto offset = evaluateDeferredIndex(slice.getMixedOffsets().front(), environment);
if (failed(offset)) return deferred.emitOpError("graph projection leading offset is not statically resolvable");
auto size = evaluateDeferredIndex(slice.getMixedSizes().front(), environment);
if (failed(size)) return deferred.emitOpError("graph projection leading size is not statically resolvable");
auto stride = evaluateDeferredIndex(slice.getMixedStrides().front(), environment);
if (failed(stride)) return deferred.emitOpError("graph projection leading stride is not statically resolvable");
if (*offset < 0) return deferred.emitOpError("graph projection leading offset is negative");
if (*size <= 0) return deferred.emitOpError("graph projection leading size must be positive");
if (*stride <= 0) return deferred.emitOpError("graph projection leading stride must be positive");
DeferredProjectionLeaf leaf;
leaf.kind = DeferredLeafKind::GraphBatchProjection; leaf.sourceOperandIndex = (*source)->sourceOperandIndex;
leaf.replacementRoot = value; leaf.leadingProjection = slice; leaf.reconstructedType = result;
for (int64_t i = 0; i < *size; ++i) {
int64_t slot;
if (llvm::MulOverflow(i, *stride, slot) || llvm::AddOverflow(*offset, slot, slot)
|| slot >= type.getDimSize(0))
return deferred.emitOpError("graph projection selects a physical slot outside the batch");
leaf.physicalSlots.push_back(slot);
}
for (unsigned i = 1; i < type.getRank(); ++i) {
auto innerOffset = evaluateDeferredIndex(slice.getMixedOffsets()[i], environment);
auto innerSize = evaluateDeferredIndex(slice.getMixedSizes()[i], environment);
auto innerStride = evaluateDeferredIndex(slice.getMixedStrides()[i], environment);
if (failed(innerOffset) || failed(innerSize) || failed(innerStride))
return deferred.emitOpError("graph projection has unresolved inner geometry");
leaf.innerGeometry.offsets.push_back(*innerOffset); leaf.innerGeometry.sizes.push_back(*innerSize); leaf.innerGeometry.strides.push_back(*innerStride);
}
program.leaves.push_back(std::move(leaf)); return success();
}
}
auto source = tryResolveDeferredSource(value, deferred, environment);
if (failed(source)) return failure();
if (*source) {
auto type = dyn_cast<RankedTensorType>((*source)->selectedValue.getType());
if (!type) return deferred.emitOpError("deferred source is not a ranked tensor");
DeferredProjectionLeaf leaf;
leaf.sourceOperandIndex = (*source)->sourceOperandIndex;
leaf.replacementRoot = value;
leaf.reconstructedType = type;
if (graphBatchOwner((*source)->selectedValue)) {
leaf.kind = DeferredLeafKind::GraphBatchIdentity;
for (int64_t slot = 0; slot < type.getDimSize(0); ++slot) leaf.physicalSlots.push_back(slot);
} else leaf.kind = DeferredLeafKind::ScalarSource;
program.leaves.push_back(std::move(leaf));
return success();
}
if (value.getType().isIndex() || isa<IntegerType>(value.getType())) {
auto folded = evaluateDeferredIndex(value, environment);
if (failed(folded))
return deferred.emitOpError("deferred index expression is not statically evaluable after specialization");
program.staticValues.try_emplace(value, *folded);
return success();
}
Operation *op = value.getDefiningOp();
if (!op || op->getBlock() != &body)
return deferred.emitOpError("deferred residual escapes its verified body");
if (auto loop = dyn_cast<scf::ForOp>(op)) {
if (failed(specializeDeferredLoopStaticValues(
loop, deferred, environment, program)))
return failure();
for (Value init : loop.getInitArgs())
if (failed(visit(init)))
return failure();
program.residualOps.push_back(op);
return success();
}
for (Value operand : op->getOperands()) if (failed(visit(operand))) return failure();
program.residualOps.push_back(op); return success();
};
if (failed(visit(program.yieldedValue))) return failure();
StaticIndexEnvironment assemblyEnvironment = environment;
for (auto [value, staticValue] : program.staticValues)
assemblyEnvironment.bindings.try_emplace(value, staticValue);
auto assembly = analyzeDeferredInsertAssembly(program, assemblyEnvironment);
if (failed(assembly))
return failure();
program.insertAssembly = std::move(*assembly);
return std::move(program);
}
FailureOr<const GraphBatchPublicationMap *> getGraphBatchPublicationMap(
SpatGraphComputeBatch graphBatch, unsigned resultIndex, GraphBatchPublicationCache &cache) {
GraphBatchPublicationKey key {graphBatch.getOperation(), resultIndex};
if (auto it = cache.find(key); it != cache.end()) return &it->second;
auto resultType = dyn_cast<RankedTensorType>(graphBatch.getResult(resultIndex).getType());
auto output = graphBatch.getOutputArgument(resultIndex);
auto lane = graphBatch.getLaneArgument();
if (!resultType || !output || !lane || resultType.getRank() == 0)
return graphBatch.emitOpError("graph batch publication is malformed"), failure();
tensor::ParallelInsertSliceOp publication;
auto parallel = dyn_cast<SpatInParallelOp>(graphBatch.getBody().front().getTerminator());
if (!parallel) return graphBatch.emitOpError("graph batch lacks publication region"), failure();
for (Operation &op : parallel.getRegion().front()) if (auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(op); insert && insert.getDest() == *output) {
if (publication) return graphBatch.emitOpError("graph result has multiple publications"), failure();
publication = insert;
}
if (!publication) return graphBatch.emitOpError("graph result lacks publication"), failure();
auto fragment = dyn_cast<RankedTensorType>(publication.getSource().getType());
if (!fragment || resultType.getRank() != fragment.getRank() + 1) return graphBatch.emitOpError("graph publication fragment type is invalid"), failure();
GraphBatchPublicationMap map;
map.physicalResultType = resultType;
map.publicationFragmentType = fragment;
map.graphLaneToPhysicalSlot.resize(graphBatch.getLaneCount(), -1);
map.physicalSlotToGraphLane.resize(resultType.getDimSize(0), -1);
for (int64_t index = 0; index < graphBatch.getLaneCount(); ++index) {
StaticIndexEnvironment environment; environment.bindings[*lane] = index;
auto slot = evaluateDeferredIndex(publication.getMixedOffsets().front(), environment);
auto size = evaluateDeferredIndex(publication.getMixedSizes().front(), environment);
auto stride = evaluateDeferredIndex(publication.getMixedStrides().front(), environment);
if (failed(slot) || failed(size) || failed(stride) || *size != 1 || *stride != 1 || *slot < 0 || *slot >= resultType.getDimSize(0))
return graphBatch.emitOpError("graph publication leading geometry is invalid"), failure();
for (unsigned dim = 1; dim < resultType.getRank(); ++dim) {
auto offset = evaluateDeferredIndex(publication.getMixedOffsets()[dim], environment);
auto extent = evaluateDeferredIndex(publication.getMixedSizes()[dim], environment);
auto step = evaluateDeferredIndex(publication.getMixedStrides()[dim], environment);
if (failed(offset) || failed(extent) || failed(step) || *offset != 0 || *extent != fragment.getDimSize(dim - 1) || *step != 1)
return graphBatch.emitOpError("graph publication inner geometry is invalid"), failure();
}
if (map.physicalSlotToGraphLane[*slot] != -1) return graphBatch.emitOpError("graph publication has duplicate physical slot"), failure();
map.graphLaneToPhysicalSlot[index] = *slot; map.physicalSlotToGraphLane[*slot] = index;
}
if (llvm::is_contained(map.physicalSlotToGraphLane, -1)) return graphBatch.emitOpError("graph publication has missing physical slot"), failure();
return &cache.try_emplace(key, std::move(map)).first->second;
}
} // namespace onnx_mlir::spatial
@@ -0,0 +1,118 @@
#pragma once
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/Operation.h"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "llvm/ADT/DenseMap.h"
namespace onnx_mlir::spatial {
struct StaticIndexEnvironment {
llvm::DenseMap<mlir::Value, int64_t> bindings;
};
mlir::FailureOr<int64_t> evaluateDeferredIndex(
mlir::Value value, const StaticIndexEnvironment &environment);
mlir::FailureOr<int64_t> evaluateDeferredIndex(
mlir::OpFoldResult value, const StaticIndexEnvironment &environment);
enum class DeferredLeafKind { ScalarSource, GraphBatchProjection, GraphBatchIdentity };
struct StaticSliceGeometry {
llvm::SmallVector<int64_t> offsets;
llvm::SmallVector<int64_t> sizes;
llvm::SmallVector<int64_t> strides;
};
struct DeferredProjectionLeaf {
DeferredLeafKind kind = DeferredLeafKind::ScalarSource;
unsigned sourceOperandIndex = 0;
mlir::Value replacementRoot;
mlir::tensor::ExtractSliceOp leadingProjection;
llvm::SmallVector<int64_t> physicalSlots;
StaticSliceGeometry innerGeometry;
mlir::RankedTensorType reconstructedType;
};
struct DeferredInsertAssemblyEntry {
unsigned requirementIndex = 0;
unsigned leafIndex = 0;
StaticSliceGeometry targetGeometry;
};
struct DeferredInsertAssembly {
mlir::tensor::EmptyOp initialValue;
mlir::RankedTensorType resultType;
llvm::SmallVector<DeferredInsertAssemblyEntry, 0> entries;
};
struct SpecializedDeferredProgram {
SpatDeferredCommunicationOp deferred;
std::optional<unsigned> targetScheduledLane;
mlir::Value scheduledLane;
mlir::Value yieldedValue;
llvm::SmallVector<DeferredProjectionLeaf, 0> leaves;
llvm::SmallVector<mlir::Operation *> residualOps;
llvm::DenseMap<mlir::Value, int64_t> staticValues;
std::optional<DeferredInsertAssembly> insertAssembly;
};
struct ResolvedDeferredSource {
unsigned sourceOperandIndex = 0;
mlir::Value selectedValue;
};
mlir::FailureOr<std::optional<ResolvedDeferredSource>> tryResolveDeferredSource(
mlir::Value value, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment);
mlir::FailureOr<ResolvedDeferredSource> requireResolvedDeferredSource(
mlir::Value value, SpatDeferredCommunicationOp deferred,
const StaticIndexEnvironment &environment);
mlir::LogicalResult verifyDeferredProgramContract(
SpatDeferredCommunicationOp deferred);
mlir::FailureOr<SpecializedDeferredProgram> analyzeDeferredProgram(
SpatDeferredCommunicationOp deferred,
std::optional<unsigned> targetScheduledLane);
struct GraphBatchPublicationMap {
mlir::RankedTensorType physicalResultType;
mlir::RankedTensorType publicationFragmentType;
llvm::SmallVector<int64_t> graphLaneToPhysicalSlot;
llvm::SmallVector<int64_t> physicalSlotToGraphLane;
};
struct GraphBatchPublicationKey {
mlir::Operation *graphBatch = nullptr;
unsigned resultIndex = 0;
bool operator==(const GraphBatchPublicationKey &other) const {
return graphBatch == other.graphBatch && resultIndex == other.resultIndex;
}
};
using GraphBatchPublicationCache =
llvm::DenseMap<GraphBatchPublicationKey, GraphBatchPublicationMap>;
mlir::FailureOr<const GraphBatchPublicationMap *> getGraphBatchPublicationMap(
SpatGraphComputeBatch graphBatch, unsigned resultIndex,
GraphBatchPublicationCache &cache);
} // namespace onnx_mlir::spatial
namespace llvm {
template <> struct DenseMapInfo<onnx_mlir::spatial::GraphBatchPublicationKey> {
static onnx_mlir::spatial::GraphBatchPublicationKey getEmptyKey() {
return {DenseMapInfo<mlir::Operation *>::getEmptyKey(), 0};
}
static onnx_mlir::spatial::GraphBatchPublicationKey getTombstoneKey() {
return {DenseMapInfo<mlir::Operation *>::getTombstoneKey(), 0};
}
static unsigned getHashValue(const onnx_mlir::spatial::GraphBatchPublicationKey &key) {
return hash_combine(key.graphBatch, key.resultIndex);
}
static bool isEqual(const onnx_mlir::spatial::GraphBatchPublicationKey &lhs,
const onnx_mlir::spatial::GraphBatchPublicationKey &rhs) {
return lhs == rhs;
}
};
} // namespace llvm
@@ -1,134 +0,0 @@
#include "HostOutputFinalization.hpp"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "MaterializedClassState.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
using namespace mlir;
namespace onnx_mlir::spatial {
LogicalResult finalizeProjectedHostOutputFragments(MaterializerState& state) {
if (state.pendingProjectedHostOutputFragments.empty())
return success();
DenseMap<Value, SmallVector<PendingProjectedHostOutputFragment*, 16>> byOutput;
for (PendingProjectedHostOutputFragment& fragment : state.pendingProjectedHostOutputFragments)
byOutput[fragment.originalOutput].push_back(&fragment);
SmallVector<Value, 8> outputs;
outputs.reserve(byOutput.size());
auto returnOp = dyn_cast<func::ReturnOp>(state.func.getBody().front().getTerminator());
if (!returnOp)
return state.func.emitError("expected func.return terminator while finalizing projected host output fragments");
DenseSet<Value> seenOutputs;
for (Value returned : returnOp.getOperands()) {
if (!byOutput.contains(returned) || !seenOutputs.insert(returned).second)
continue;
outputs.push_back(returned);
}
if (outputs.size() != byOutput.size())
return state.func.emitError("projected host output fragments must be keyed by returned logical host outputs");
for (Value originalOutput : outputs) {
if (isa_and_present<SpatScheduledCompute, SpatScheduledComputeBatch>(originalOutput.getDefiningOp())) {
return state.func.emitError(
"projected host output assembly must be keyed by the original logical host output, not by a materialized scheduled result");
}
auto resultType = dyn_cast<RankedTensorType>(originalOutput.getType());
if (!resultType || !resultType.hasStaticShape())
return state.func.emitError("projected host output must have static ranked tensor type");
SmallVector<PendingProjectedHostOutputFragment*, 16>& fragments = byOutput[originalOutput];
llvm::sort(fragments, [](const PendingProjectedHostOutputFragment* lhs,
const PendingProjectedHostOutputFragment* rhs) {
if (lhs->sourceClass != rhs->sourceClass)
return lhs->sourceClass < rhs->sourceClass;
if (lhs->publicationResultIndex != rhs->publicationResultIndex)
return lhs->publicationResultIndex < rhs->publicationResultIndex;
if (lhs->sourceFragmentOrdinal != rhs->sourceFragmentOrdinal)
return lhs->sourceFragmentOrdinal < rhs->sourceFragmentOrdinal;
return std::lexicographical_compare(lhs->offsets.begin(),
lhs->offsets.end(),
rhs->offsets.begin(),
rhs->offsets.end());
});
state.rewriter.setInsertionPoint(returnOp);
Location loc = fragments.front()->loc;
SmallVector<Value, 16> blueprintOperands;
SmallVector<int64_t, 16> fragmentOperandIndices;
SmallVector<int64_t, 16> fragmentSourceOffsets;
SmallVector<int64_t, 64> flatOffsets;
SmallVector<int64_t, 64> flatSizes;
SmallVector<int64_t, 64> flatStrides;
DenseMap<Value, int64_t> operandIndicesByValue;
for (PendingProjectedHostOutputFragment* fragmentRecord : fragments) {
if (fragmentRecord->sourceClass >= state.classes.size())
return state.func.emitError("projected host output fragment references an invalid source class");
MaterializedClass& sourceClass = state.classes[fragmentRecord->sourceClass];
if (fragmentRecord->publicationResultIndex >= sourceClass.op->getNumResults()) {
return sourceClass.op->emitError("projected host output fragment references an invalid publication result")
<< " sourceClass=" << sourceClass.id
<< " resultIndex=" << fragmentRecord->publicationResultIndex
<< " resultCount=" << sourceClass.op->getNumResults();
}
Value operand = sourceClass.op->getResult(fragmentRecord->publicationResultIndex);
auto [operandIt, inserted] =
operandIndicesByValue.try_emplace(operand, static_cast<int64_t>(blueprintOperands.size()));
if (inserted)
blueprintOperands.push_back(operand);
fragmentOperandIndices.push_back(operandIt->second);
fragmentSourceOffsets.push_back(fragmentRecord->sourceElementOffset);
llvm::append_range(flatOffsets, fragmentRecord->offsets);
llvm::append_range(flatSizes, fragmentRecord->sizes);
llvm::append_range(flatStrides, fragmentRecord->strides);
auto operandType = dyn_cast<RankedTensorType>(operand.getType());
if (!operandType || !operandType.hasStaticShape())
return state.func.emitError("projected host output assembly requires static ranked tensor operands");
}
if (blueprintOperands.empty())
return state.func.emitError("missing projected host output fragments");
Value input = blueprintOperands.front();
ValueRange extraFragments = ValueRange(blueprintOperands).drop_front();
auto blueprint = SpatBlueprintOp::create(
state.rewriter,
loc,
resultType,
input,
extraFragments,
state.rewriter.getStringAttr("nchw"),
state.rewriter.getStringAttr("fragmented"),
state.rewriter.getDenseI64ArrayAttr(flatOffsets),
state.rewriter.getDenseI64ArrayAttr(flatSizes),
state.rewriter.getStringAttr("identity"),
state.rewriter.getStringAttr("fragment_assembly"),
state.rewriter.getDenseI64ArrayAttr(fragmentOperandIndices),
state.rewriter.getDenseI64ArrayAttr(fragmentSourceOffsets),
state.rewriter.getDenseI64ArrayAttr(flatStrides),
state.rewriter.getStringAttr("disjoint"),
state.rewriter.getStringAttr("complete"));
state.hostReplacements[originalOutput] = blueprint.getOutput();
}
return success();
}
} // namespace onnx_mlir::spatial
@@ -1,11 +0,0 @@
#pragma once
#include "mlir/Support/LogicalResult.h"
namespace onnx_mlir::spatial {
struct MaterializerState;
mlir::LogicalResult finalizeProjectedHostOutputFragments(MaterializerState& state);
} // namespace onnx_mlir::spatial
@@ -1,17 +0,0 @@
#pragma once
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LogicalResult.h"
#include "Scheduling/MergeSchedule.hpp"
namespace onnx_mlir {
namespace spatial {
class MergeScheduleMaterializer {
public:
mlir::LogicalResult run(mlir::func::FuncOp func, const MergeScheduleResult& schedule, int64_t& nextChannelId);
};
} // namespace spatial
} // namespace onnx_mlir
@@ -1,252 +0,0 @@
#pragma once
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/FoldUtils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include <optional>
#include "MaterializeMergeSchedule.hpp"
#include "MergeMessages.hpp"
#include "MergeScheduleKeys.hpp"
#include "ProjectedFragments.hpp"
#include "Scheduling/ComputeInstanceUtils.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
namespace onnx_mlir::spatial {
struct MaterializedClass {
ClassId id = 0;
llvm::SmallVector<CpuId, 8> cpus;
mlir::Operation* op = nullptr;
mlir::Block* body = nullptr;
bool isBatch = false;
llvm::DenseMap<CpuId, unsigned> cpuToLane;
llvm::SmallVector<mlir::Value, 8> weights;
llvm::SmallVector<mlir::Value, 8> inputs;
llvm::SmallVector<mlir::Value, 4> hostOutputs;
llvm::DenseMap<mlir::Value, unsigned> publicationOutputToResultIndex;
llvm::DenseMap<mlir::Value, mlir::BlockArgument> weightArgs;
llvm::DenseMap<mlir::Value, mlir::BlockArgument> inputArgs;
llvm::DenseMap<mlir::Value, unsigned> hostOutputToResultIndex;
};
struct PackedScalarRunSlot {
llvm::SmallVector<ProducerKey, 8> keys;
};
enum class PackedScalarRunKind {
Materialized,
DeferredReceive,
DeferredLocalCompute
};
struct PackedScalarRunValue {
ClassId targetClass = 0;
mlir::Operation* sourceOp = nullptr;
size_t resultIndex = 0;
PackedScalarRunKind kind = PackedScalarRunKind::Materialized;
mlir::Value packed;
mlir::RankedTensorType fragmentType;
llvm::SmallVector<PackedScalarRunSlot, 8> slots;
MessageVector messages;
};
struct IndexedBatchRunValue {
ClassId targetClass = 0;
mlir::Operation* sourceOp = nullptr;
size_t resultIndex = 0;
mlir::Value packed;
mlir::RankedTensorType fragmentType;
llvm::SmallVector<PackedScalarRunSlot, 8> slots;
MessageVector messages;
};
struct LogicalSlotRange {
SlotId start = 0;
SlotId count = 0;
};
struct MaterializationRunSlot {
llvm::SmallVector<ComputeInstance, 8> peers;
};
using MaterializationRun = llvm::SmallVector<MaterializationRunSlot, 8>;
struct OutputDestinationGroup {
llvm::SmallVector<size_t, 4> resultIndices;
llvm::SmallVector<ClassId, 4> destinationClasses;
};
struct BatchRunSendPlan {
size_t resultIndex = 0;
ClassId destinationClass = 0;
MessageVector messages;
};
enum class TensorDemandActionKind {
DestinationFanout,
SameClassIndexedFragment,
TerminalBlueprintPublication,
WholeTensorBarrier
};
enum class WholeTensorBarrierReason {
FunctionReturnWithoutBlueprint,
DenseLogicalConsumer
};
struct TensorDemandAction {
TensorDemandActionKind kind = TensorDemandActionKind::DestinationFanout;
std::optional<ClassId> destinationClass;
std::optional<WholeTensorBarrierReason> barrierReason;
};
struct RunOutputDemand {
size_t resultIndex = 0;
mlir::Value originalOutput;
mlir::RankedTensorType fragmentType;
llvm::SmallVector<TensorDemandAction, 4> actions;
};
struct CompactRunPlan {
llvm::SmallVector<RunOutputDemand, 4> outputs;
};
enum class BatchInputDemandKind {
LaneFragment,
ProjectedFragment,
WholeTensorBarrier
};
struct BatchInputDemand {
BatchInputDemandKind kind = BatchInputDemandKind::LaneFragment;
std::optional<ProducerKey> wholeTensorProducer;
};
struct CloneIndexingContext {
std::optional<mlir::Value> runSlotIndex;
std::optional<mlir::Value> projectionSlotIndex;
};
struct MaterializerState;
class AvailableValueStore {
public:
struct ExactBatchFragmentRecord {
ProducerKey key;
mlir::Value value;
};
void record(ProducerKey key, ClassId classId, mlir::Value value) {
exactValues[key][classId] = value;
auto batch = mlir::dyn_cast_or_null<SpatComputeBatch>(key.instance.op);
if (!batch || key.instance.laneCount == 0)
return;
WholeBatchAssemblyLookupKey lookupKey {batch.getOperation(), key.resultIndex, classId};
llvm::SmallVector<ExactBatchFragmentRecord, 16>& bucket = exactBatchFragmentsByProducerResultClass[lookupKey];
for (ExactBatchFragmentRecord& record : bucket) {
if (!(record.key == key))
continue;
record.value = value;
return;
}
bucket.push_back({key, value});
}
void recordPackedRun(PackedScalarRunValue run) {
size_t runIndex = packedScalarRuns.size();
packedScalarRuns.push_back(std::move(run));
const PackedScalarRunValue& storedRun = packedScalarRuns[runIndex];
WholeBatchAssemblyLookupKey lookupKey {storedRun.sourceOp, storedRun.resultIndex, storedRun.targetClass};
packedRunsByProducerResultClass[lookupKey].push_back(runIndex);
}
void recordIndexedBatchRun(IndexedBatchRunValue run) { indexedBatchRuns.push_back(std::move(run)); }
std::optional<mlir::Value> lookupExact(ProducerKey key, ClassId classId) const;
std::optional<mlir::Value> lookup(MaterializerState& state, ProducerKey key, ClassId classId);
IndexedBatchRunValue* lookupIndexedBatchRun(ProducerKey key, ClassId classId);
llvm::ArrayRef<size_t> getPackedRunIndicesForWholeBatch(WholeBatchAssemblyLookupKey key) const {
auto it = packedRunsByProducerResultClass.find(key);
if (it == packedRunsByProducerResultClass.end())
return {};
return it->second;
}
llvm::ArrayRef<ExactBatchFragmentRecord> getExactFragmentsForWholeBatch(WholeBatchAssemblyLookupKey key) const {
auto it = exactBatchFragmentsByProducerResultClass.find(key);
if (it == exactBatchFragmentsByProducerResultClass.end())
return {};
return it->second;
}
PackedScalarRunValue& getPackedRun(size_t index) { return packedScalarRuns[index]; }
private:
std::optional<mlir::Value> lookupPackedRun(MaterializerState& state, ProducerKey key, ClassId classId);
llvm::DenseMap<ProducerKey, llvm::DenseMap<ClassId, mlir::Value>, ProducerKeyInfo> exactValues;
llvm::SmallVector<PackedScalarRunValue, 8> packedScalarRuns;
llvm::SmallVector<IndexedBatchRunValue, 8> indexedBatchRuns;
llvm::DenseMap<WholeBatchAssemblyLookupKey,
llvm::SmallVector<ExactBatchFragmentRecord, 16>,
WholeBatchAssemblyLookupKeyInfo>
exactBatchFragmentsByProducerResultClass;
llvm::DenseMap<WholeBatchAssemblyLookupKey, llvm::SmallVector<size_t, 16>, WholeBatchAssemblyLookupKeyInfo>
packedRunsByProducerResultClass;
};
struct MaterializerState {
mlir::func::FuncOp func;
const MergeScheduleResult& schedule;
mlir::IRRewriter rewriter;
mlir::OperationFolder constantFolder;
int64_t& nextChannelId;
llvm::SmallVector<MaterializedClass, 8> classes;
llvm::DenseMap<CpuId, ClassId> cpuToClass;
llvm::DenseMap<CpuId, llvm::SmallVector<ComputeInstance, 32>> logicalInstancesByCpu;
llvm::DenseMap<ComputeInstance, LogicalSlotRange> scheduledInstanceToLogicalSlots;
llvm::DenseMap<ComputeInstance, ComputeInstance> logicalInstanceToScheduledChunk;
llvm::DenseSet<ClassSlotKey> materializedLogicalSlots;
llvm::DenseMap<ProducerKey, llvm::SmallVector<ClassId, 4>, ProducerKeyInfo> producerDestClasses;
llvm::DenseMap<SameClassConsumerLookupKey, llvm::SmallVector<ProducerKey, 4>, SameClassConsumerLookupKeyInfo>
sameClassConsumerIndex;
llvm::DenseMap<ProjectedBatchInputKey, AffineProjectedInputSliceMatch, ProjectedBatchInputKeyInfo>
projectedInputMatches;
llvm::DenseSet<ProjectedBatchInputKey, ProjectedBatchInputKeyInfo> nonProjectedInputs;
llvm::DenseMap<mlir::Value, bool> liveExternalUseCache;
llvm::DenseMap<mlir::Operation*, llvm::SmallVector<mlir::Type, 4>> batchOutputFragmentTypesCache;
llvm::DenseMap<ComputeInstance, llvm::SmallVector<mlir::Value, 4>, llvm::DenseMapInfo<ComputeInstance>>
computeInstanceOutputsCache;
llvm::DenseMap<ProducerKey, llvm::DenseMap<ClassId, ProjectedTransferDescriptor>, ProducerKeyInfo>
projectedTransfers;
llvm::DenseMap<mlir::Operation*, llvm::DenseMap<ClassId, ProjectedExtractReplacement>>
projectedExtractReplacements;
AvailableValueStore availableValues;
llvm::DenseMap<mlir::Value, mlir::Value> hostReplacements;
llvm::DenseMap<mlir::Value, ClassId> hostOutputOwners;
llvm::SmallVector<PendingProjectedHostOutputFragment, 32> pendingProjectedHostOutputFragments;
llvm::DenseSet<mlir::Operation*> oldComputeOps;
MaterializerState(mlir::func::FuncOp func, const MergeScheduleResult& schedule, int64_t& nextChannelId)
: func(func),
schedule(schedule),
rewriter(func.getContext()),
constantFolder(func.getContext()),
nextChannelId(nextChannelId) {}
};
} // namespace onnx_mlir::spatial
@@ -1,391 +1,126 @@
#include "mlir/Analysis/TopologicalSortUtils.h" #include "ScheduledComputeMaterialization.hpp"
#include "mlir/Dialect/Func/IR/FuncOps.h" #include "ScheduledComputeReport.hpp"
#include "mlir/IR/IRMapping.h" #include "ScheduledComputeVerification.hpp"
#include "mlir/IR/Location.h" #include "SpatialDataflowCsvExporter.hpp"
#include "mlir/IR/PatternMatch.h" #include "DeferredCommunicationRealization.hpp"
#include "mlir/IR/Region.h" #include "DeferredCommunicationDeadlock.hpp"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
#include "mlir/Pass/Pass.h" #include "mlir/Pass/Pass.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_os_ostream.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "MaterializeMergeSchedule.hpp"
#include "Scheduling/ComputeGraph.hpp"
#include "Scheduling/ComputeInstanceUtils.hpp"
#include "Scheduling/MergeSchedulingAnalysis.hpp" #include "Scheduling/MergeSchedulingAnalysis.hpp"
#include "src/Accelerators/PIM/Common/IR/BatchCoreUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/CompactAsmUtils.hpp"
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
#include "src/Accelerators/PIM/Common/Support/ReportUtils.hpp"
#include "src/Accelerators/PIM/Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp" #include "src/Accelerators/PIM/Conversion/ONNXToSpatial/ONNXToSpatialVerifier.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include "src/Accelerators/PIM/Common/Support/DebugDump.hpp"
#include "src/Accelerators/PIM/Pass/PIMPasses.h"
using namespace mlir; using namespace mlir;
namespace onnx_mlir { namespace onnx_mlir {
namespace spatial {
namespace { namespace {
using namespace onnx_mlir::compact_asm;
using SpatCompute = spatial::SpatGraphCompute;
using SpatComputeBatch = spatial::SpatGraphComputeBatch;
bool isTrivialSerialMergeCandidate(SpatCompute compute) { struct MergeComputeNodesPass final : PassWrapper<MergeComputeNodesPass, OperationPass<ModuleOp>> {
if (!compute->hasOneUse())
return false;
auto& use = *compute->getUses().begin();
auto user = dyn_cast<SpatCompute>(use.getOwner());
return user && user.getInputs().size() == 1 && use.getOperandNumber() >= user.getWeights().size();
}
SmallVector<size_t> appendMissingWeightsAndBuildIndexMap(SmallVectorImpl<Value>& targetWeights,
ValueRange sourceWeights) {
DenseMap<Value, SmallVector<size_t, 4>> targetWeightIndices;
for (auto [weightIndex, weight] : llvm::enumerate(targetWeights))
targetWeightIndices[weight].push_back(weightIndex);
DenseMap<Value, size_t> usedSourceWeightOccurrences;
SmallVector<size_t> sourceToTargetIndex;
sourceToTargetIndex.reserve(sourceWeights.size());
for (Value weight : sourceWeights) {
size_t occurrence = usedSourceWeightOccurrences[weight]++;
auto& matchingIndices = targetWeightIndices[weight];
if (occurrence >= matchingIndices.size()) {
size_t newIndex = targetWeights.size();
targetWeights.push_back(weight);
matchingIndices.push_back(newIndex);
sourceToTargetIndex.push_back(newIndex);
continue;
}
sourceToTargetIndex.push_back(matchingIndices[occurrence]);
}
return sourceToTargetIndex;
}
void mergeTriviallyConnectedComputes(func::FuncOp funcOp) {
Location loc = funcOp.getLoc();
IRRewriter rewriter(funcOp->getContext());
SmallVector<SpatCompute> trivialComputes;
llvm::SmallSet<SpatCompute, 8> toErase;
for (auto compute : funcOp.getOps<SpatCompute>())
if (isTrivialSerialMergeCandidate(compute))
trivialComputes.push_back(compute);
while (!trivialComputes.empty()) {
auto compute = trivialComputes.front();
if (compute.use_empty()) {
std::swap(trivialComputes.front(), trivialComputes.back());
trivialComputes.pop_back();
continue;
}
auto& computeUse = *compute->getUses().begin();
auto child = cast<SpatCompute>(computeUse.getOwner());
auto usedResult = cast<OpResult>(computeUse.get()).getResultNumber();
auto childInputIndex = computeUse.getOperandNumber() - child.getWeights().size();
rewriter.setInsertionPointAfter(compute.getOperation());
SmallVector<Value> mergedWeights(compute.getWeights().begin(), compute.getWeights().end());
SmallVector<size_t> childWeightToNewIndex = appendMissingWeightsAndBuildIndexMap(mergedWeights, child.getWeights());
SmallVector<Value> mergedInputs(compute.getInputs().begin(), compute.getInputs().end());
auto newCompute = SpatCompute::create(rewriter, loc, child.getResultTypes(), mergedWeights, mergedInputs);
Block* newBody = rewriter.createBlock(&newCompute.getBodyRegion());
for (Value weight : mergedWeights)
newBody->addArgument(weight.getType(), loc);
for (Value input : mergedInputs)
newBody->addArgument(input.getType(), loc);
IRMapping mapper;
for (auto [weightIndex, _] : llvm::enumerate(compute.getWeights())) {
auto oldWeightArg = compute.getWeightArgument(weightIndex);
auto newWeightArg = newCompute.getWeightArgument(weightIndex);
assert(oldWeightArg && newWeightArg && "expected compute weight block arguments");
mapper.map(*oldWeightArg, *newWeightArg);
}
for (auto [inputIndex, _] : llvm::enumerate(compute.getInputs())) {
auto oldInputArg = compute.getInputArgument(inputIndex);
auto newInputArg = newCompute.getInputArgument(inputIndex);
assert(oldInputArg && newInputArg && "expected compute input block arguments");
mapper.map(*oldInputArg, *newInputArg);
}
for (auto [oldIndex, weight] : llvm::enumerate(child.getWeights())) {
auto oldWeightArg = child.getWeightArgument(oldIndex);
auto newWeightArg = newCompute.getWeightArgument(childWeightToNewIndex[oldIndex]);
assert(oldWeightArg && newWeightArg && "expected child compute weight block arguments");
mapper.map(*oldWeightArg, *newWeightArg);
}
rewriter.setInsertionPointToEnd(newBody);
auto computeYield = cast<spatial::SpatYieldOp>(compute.getBody().front().getTerminator());
for (Operation& op : compute.getBody().front().without_terminator())
rewriter.clone(op, mapper);
auto childInputArg = child.getInputArgument(childInputIndex);
assert(childInputArg && "expected child compute input block argument");
mapper.map(*childInputArg, mapper.lookupOrDefault(computeYield.getOperand(usedResult)));
rewriter.setInsertionPointToEnd(newBody);
for (auto& op : child.getBody().front())
rewriter.clone(op, mapper);
child.replaceAllUsesWith(newCompute);
toErase.insert(child);
std::swap(trivialComputes.front(), trivialComputes.back());
trivialComputes.pop_back();
toErase.insert(compute);
if (isTrivialSerialMergeCandidate(newCompute))
trivialComputes.push_back(newCompute);
}
for (auto compute : toErase) {
for (Value result : compute->getResults())
result.dropAllUses();
compute.erase();
}
}
void generateReport(func::FuncOp funcOp, const std::string& name, size_t usedCpuCount = 0) {
std::fstream file = openReportFile(name);
if (!file.is_open())
return;
llvm::raw_os_ostream os(file);
struct ReportRow {
uint64_t id = 0;
uint64_t logicalComputeCount = 0;
uint64_t crossbarCount = 0;
uint64_t instructionCount = 0;
bool isRebatched = false;
SmallVector<int32_t> coreIds;
};
//TODO Used for report refactor
struct CollectorConcatRow {
uint64_t computeId = 0;
int32_t coreId = -1;
uint64_t operandCount = 0;
};
uint64_t totalComputeOps = 0;
uint64_t totalLogicalComputes = 0;
uint64_t totalBatchComputeOps = 0;
uint64_t totalInstructionCount = 0;
uint64_t totalCrossbarCount = 0;
uint64_t nextBatchId = 0;
//TODO Used for report refactor
std::vector<ReportRow> collectedData;
//TODO Used for report refactor
std::vector<CollectorConcatRow> collectorConcatRows;
auto getPerInstanceCrossbarCount = [&](Operation* op) -> uint64_t {
return static_cast<uint64_t>(spatial::collectDistinctCrossbarWeights(op).size());
};
for (Operation& op : funcOp.getBody().front()) {
if (auto spatCompute = dyn_cast<spatial::SpatScheduledCompute>(&op)) {
uint64_t numInst = spatial::countComputeBodyInstructions(spatCompute.getBody());
uint64_t perInstanceCrossbarCount = getPerInstanceCrossbarCount(spatCompute.getOperation());
SmallVector<int32_t> coreIds;
auto coreId = getOptionalScheduledCoreId(spatCompute, "merge compute core id");
if (failed(coreId))
return;
if (*coreId)
coreIds.push_back(**coreId);
uint64_t computeId = totalComputeOps++;
collectedData.push_back({computeId, 1, perInstanceCrossbarCount, numInst, false, coreIds});
uint64_t maxConcatOperands = 0;
spatCompute.getBody().walk([&](spatial::SpatConcatOp concatOp) {
maxConcatOperands = std::max<uint64_t>(maxConcatOperands, concatOp.getInputs().size());
});
//TODO 128 is a magic number
if (maxConcatOperands >= 128 && !coreIds.empty())
collectorConcatRows.push_back({computeId, coreIds.front(), maxConcatOperands});
totalLogicalComputes += 1;
totalInstructionCount += numInst;
totalCrossbarCount += perInstanceCrossbarCount;
continue;
}
if (auto batch = dyn_cast<spatial::SpatScheduledComputeBatch>(&op)) {
uint64_t numInst = spatial::countComputeBodyInstructions(batch.getBody());
uint64_t logicalCount = static_cast<uint64_t>(batch.getLaneCount());
uint64_t perInstanceCrossbarCount = getPerInstanceCrossbarCount(batch.getOperation());
SmallVector<int32_t> coreIds;
auto optionalCoreIds = getOptionalScheduledBatchCoreIds(batch, "merge compute_batch core id");
if (failed(optionalCoreIds))
return;
if (*optionalCoreIds)
coreIds = std::move(**optionalCoreIds);
collectedData.push_back(
{nextBatchId++, logicalCount, perInstanceCrossbarCount * logicalCount, numInst, true, coreIds});
totalComputeOps += 1;
totalLogicalComputes += logicalCount;
totalBatchComputeOps += 1;
totalInstructionCount += numInst * logicalCount;
totalCrossbarCount += perInstanceCrossbarCount * logicalCount;
}
}
llvm::SmallVector<ReportField, 6> totalFields = {
{"Used cores", std::to_string(usedCpuCount) },
{"Number of top-level compute ops", std::to_string(totalComputeOps) },
{"Number of logical computes", std::to_string(totalLogicalComputes) },
{"Number of top-level batch compute ops", std::to_string(totalBatchComputeOps) },
{"Number of instructions", std::to_string(totalInstructionCount)},
{"Number of used crossbars", std::to_string(totalCrossbarCount) }
};
printReportTotalsBlock(os, totalFields);
if (!collectedData.empty() || !collectorConcatRows.empty())
os << "\n";
if (!collectorConcatRows.empty()) {
os << "Collector concat materialization:\n";
for (const CollectorConcatRow& row : collectorConcatRows)
os << "\tmaterialization_kind = single_collector_concat, compute = " << row.computeId
<< ", concat_operand_count = " << row.operandCount << ", collector_core = " << row.coreId << "\n";
os << "\n";
}
sortReportEntriesByFirstCore(collectedData);
for (uint64_t cI = 0; cI < totalComputeOps; ++cI) {
uint64_t lastIndex = cI;
ReportRow current = collectedData[cI];
for (uint64_t nI = cI + 1; nI < totalComputeOps; ++nI) {
ReportRow next = collectedData[nI];
if (current.isRebatched == next.isRebatched && current.crossbarCount == next.crossbarCount
&& current.instructionCount == next.instructionCount
&& current.logicalComputeCount == next.logicalComputeCount)
lastIndex = nI;
else
break;
}
if (current.isRebatched) {
os << "Batch ";
for (uint64_t index = cI; index <= lastIndex; ++index) {
if (index != cI)
os << ",\n ";
os << collectedData[index].id << " (cores ";
if (collectedData[index].coreIds.empty())
os << "unknown";
else
printCompressedIntegerEntries(os, ArrayRef<int32_t>(collectedData[index].coreIds));
os << ")";
}
}
else {
os << "Compute ";
SmallVector<uint64_t> opIds;
opIds.reserve(lastIndex - cI + 1);
for (uint64_t index = cI; index <= lastIndex; ++index)
opIds.push_back(collectedData[index].id);
printCompressedIntegerEntries(os, ArrayRef<uint64_t>(opIds));
}
os << ":\n";
uint64_t perCoreLogicalComputeCount = current.isRebatched ? 1 : current.logicalComputeCount;
uint64_t perCoreInstructionCount = current.instructionCount;
uint64_t perCoreCrossbarCount =
current.logicalComputeCount == 0 ? 0 : current.crossbarCount / current.logicalComputeCount;
uint64_t totalEntryInstructionCount = current.instructionCount * current.logicalComputeCount;
llvm::SmallVector<ReportField, 3> perCoreFields = {
{"Number of logical computes", std::to_string(perCoreLogicalComputeCount)},
{"Number of instructions", std::to_string(perCoreInstructionCount) },
{"Number of used crossbars", std::to_string(perCoreCrossbarCount) }
};
if (current.isRebatched) {
llvm::SmallVector<ReportField, 3> totalEntryFields = {
{"Number of logical computes", std::to_string(current.logicalComputeCount)},
{"Number of instructions", std::to_string(totalEntryInstructionCount) },
{"Number of used crossbars", std::to_string(current.crossbarCount) }
};
printReportPerCoreAndTotalFields(os, perCoreFields, totalEntryFields);
}
else {
printReportFlatFields(os, perCoreFields);
}
printReportEntrySeparator(os, lastIndex + 1 < totalComputeOps);
cI = lastIndex;
}
os.flush();
file.close();
}
struct MergeComputeNodesPass : PassWrapper<MergeComputeNodesPass, OperationPass<func::FuncOp>> {
private:
int64_t nextChannelId = 0;
public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MergeComputeNodesPass) MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(MergeComputeNodesPass)
StringRef getArgument() const override { return "pim-merge-compute-nodes-pass"; } StringRef getArgument() const override { return "pim-merge-compute-nodes"; }
StringRef getDescription() const override { StringRef getDescription() const override {
return "Merge Spatial-Compute-Nodes in order to reduce the total " return "Materialize scheduled Spatial compute with deferred communication placeholders.";
"execution time";
} }
LogicalResult initialize(MLIRContext* context) override { return success(); }
void runOnOperation() override { void runOnOperation() override {
func::FuncOp func = getOperation(); ModuleOp moduleOp = getOperation();
if (failed(verifyLogicalSpatialGraphInvariants(func))) { auto entryFunc = getPimEntryFunc(moduleOp);
func.emitOpError("logical Spatial graph verification failed at the start of MergeComputeNodes"); if (failed(entryFunc)) {
signalPassFailure(); moduleOp.emitError("failed to locate the PIM entry function during MergeComputeNodes");
return;
}
mergeTriviallyConnectedComputes(func);
if (failed(verifyLogicalSpatialGraphInvariants(func))) {
func.emitOpError("logical Spatial graph verification failed after trivial merge simplification");
signalPassFailure(); signalPassFailure();
return; return;
} }
const spatial::MergeScheduleResult* analysisResult = nullptr; func::FuncOp funcOp = *entryFunc;
analysisResult = &getAnalysis<spatial::MergeSchedulingAnalysis>().getResult(); MergeScheduleResult schedule = MergeSchedulingAnalysis(funcOp).getResult();
if (failed(spatial::MergeScheduleMaterializer().run(func, *analysisResult, nextChannelId))) { PatternRewriter rewriter(moduleOp.getContext());
FailureOr<ScheduledComputeMaterializationResult> materialization =
materializeScheduledCompute(funcOp, schedule, rewriter);
if (failed(materialization)) {
signalPassFailure(); signalPassFailure();
return; return;
} }
if (!sortTopologically(&func.getBody().front())) { // Phase 1 is intentionally dumped before its verifier: malformed deferred
func.emitOpError("failed to topologically order merged Spatial IR"); // payloads must be diagnosed from the producer-owned body.
dumpModule(moduleOp, "spatial2_scheduled_no_comm", /*assumeVerified=*/true);
if (failed(verifyMaterializedScheduleMapping(funcOp,
schedule,
materialization->peftClassPlans,
materialization->graphComputeToBlockMap,
materialization->materializedSchedules))) {
moduleOp.emitError("scheduled Spatial materialization mapping verification failed");
signalPassFailure(); signalPassFailure();
return; return;
} }
if (failed(verifyScheduledSpatialInvariants(func))) { if (failed(verifyDeferredTransferPhase1Invariants(funcOp))) {
func.emitOpError("scheduled Spatial verification failed after merge materialization"); moduleOp.emitError("scheduled Spatial deferred communication verification failed");
signalPassFailure(); signalPassFailure();
return; return;
} }
dumpModule(cast<ModuleOp>(func->getParentOp()), "spatial1_merged"); if (failed(verifyScheduledMaterializationRecords(materialization->materializedSchedules))) {
generateReport(func, "spatial_merge_report", analysisResult->cpuToLastComputeMap.size()); moduleOp.emitError("scheduled Spatial materialization record verification failed");
signalPassFailure();
return;
}
if (failed(verifyPeftMaterializationReportSummary(funcOp,
schedule,
materialization->peftClassPlans,
materialization->materializedSchedules))) {
moduleOp.emitError("scheduled Spatial report verification failed");
signalPassFailure();
return;
}
if (failed(verifyScheduledSpatialInvariants(funcOp))) {
moduleOp.emitError("scheduled Spatial phase 1 verification failed");
signalPassFailure();
return;
}
SpatialDataflowExportStage exportMode = getSpatialDataflowExportStage();
if (shouldExportSpatialDataflowStage(exportMode, SpatialDataflowExportStage::Spatial2)
&& failed(exportSpatialDataflowCsvScheduled(funcOp, "spatial2_scheduled_no_comm", "spatial2"))) {
signalPassFailure();
return;
}
dumpScheduledComputeReportAndModule(moduleOp,
funcOp,
schedule,
materialization->peftClassPlans,
materialization->materializedSchedules);
if (failed(realizeDeferredCommunication(funcOp))) {
moduleOp.emitError("MergeComputeNodes phase 2 communication realization failed");
signalPassFailure();
return;
}
dumpModule(moduleOp, "spatial3_scheduled", /*assumeVerified=*/true);
if (failed(verifyRealizedCommunicationDeadlockFree(funcOp))) {
moduleOp.emitError("MergeComputeNodes final communication verification failed");
signalPassFailure();
return;
}
if (failed(verifyScheduledSpatialInvariants(funcOp))) {
moduleOp.emitError("scheduled Spatial phase 2 verification failed");
signalPassFailure();
return;
}
if (shouldExportSpatialDataflowStage(exportMode, SpatialDataflowExportStage::Spatial3)
&& failed(exportSpatialDataflowCsvScheduled(funcOp, "spatial3_scheduled", "spatial3"))) {
signalPassFailure();
}
} }
}; };
} // namespace } // namespace
} // namespace spatial
std::unique_ptr<Pass> createMergeComputeNodesPass() { return std::make_unique<MergeComputeNodesPass>(); } std::unique_ptr<Pass> createMergeComputeNodesPass() { return std::make_unique<spatial::MergeComputeNodesPass>(); }
} // namespace onnx_mlir } // namespace onnx_mlir
@@ -1,67 +0,0 @@
#pragma once
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "src/Accelerators/PIM/Common/Support/CheckedArithmetic.hpp"
namespace onnx_mlir::spatial {
using CpuId = size_t;
inline mlir::FailureOr<int32_t> getCheckedCoreId(mlir::Operation* anchor, CpuId cpu, llvm::StringRef fieldName) {
return pim::checkedI32(static_cast<uint64_t>(cpu), anchor, fieldName);
}
inline mlir::FailureOr<llvm::SmallVector<int32_t, 8>>
getCheckedCoreIds(mlir::Operation* anchor, llvm::ArrayRef<CpuId> cpus, llvm::StringRef fieldName) {
llvm::SmallVector<int32_t, 8> coreIds;
coreIds.reserve(cpus.size());
for (CpuId cpu : cpus) {
auto checkedCoreId = getCheckedCoreId(anchor, cpu, fieldName);
if (mlir::failed(checkedCoreId))
return mlir::failure();
coreIds.push_back(*checkedCoreId);
}
return coreIds;
}
struct MessageVector {
llvm::SmallVector<int64_t, 16> channelIds;
llvm::SmallVector<int32_t, 16> sourceCoreIds;
llvm::SmallVector<int32_t, 16> targetCoreIds;
size_t size() const { return channelIds.size(); }
bool empty() const { return channelIds.empty(); }
mlir::LogicalResult verify(mlir::Operation* anchor) const {
if (channelIds.size() != sourceCoreIds.size() || channelIds.size() != targetCoreIds.size())
return anchor->emitError("message metadata is inconsistent");
return mlir::success();
}
void append(int64_t channelId, int32_t sourceCoreId, int32_t targetCoreId) {
channelIds.push_back(channelId);
sourceCoreIds.push_back(sourceCoreId);
targetCoreIds.push_back(targetCoreId);
}
void append(llvm::ArrayRef<int64_t> channels, llvm::ArrayRef<int32_t> sources, llvm::ArrayRef<int32_t> targets) {
assert(channels.size() == sources.size() && "channel/source count mismatch");
assert(channels.size() == targets.size() && "channel/target count mismatch");
llvm::append_range(channelIds, channels);
llvm::append_range(sourceCoreIds, sources);
llvm::append_range(targetCoreIds, targets);
}
MessageVector slice(size_t offset, size_t count) const {
MessageVector result;
result.append(llvm::ArrayRef<int64_t>(channelIds).slice(offset, count),
llvm::ArrayRef<int32_t>(sourceCoreIds).slice(offset, count),
llvm::ArrayRef<int32_t>(targetCoreIds).slice(offset, count));
return result;
}
};
} // namespace onnx_mlir::spatial
@@ -1,134 +0,0 @@
#pragma once
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <utility>
#include "Scheduling/ComputeInstanceUtils.hpp"
namespace onnx_mlir::spatial {
using ClassId = size_t;
using SlotId = size_t;
struct ProducerKey {
ComputeInstance instance;
size_t resultIndex = 0;
bool operator==(const ProducerKey& other) const {
return instance == other.instance && resultIndex == other.resultIndex;
}
};
struct ProducerKeyInfo {
static ProducerKey getEmptyKey() {
return {llvm::DenseMapInfo<ComputeInstance>::getEmptyKey(), std::numeric_limits<size_t>::max()};
}
static ProducerKey getTombstoneKey() {
return {llvm::DenseMapInfo<ComputeInstance>::getTombstoneKey(), std::numeric_limits<size_t>::max()};
}
static unsigned getHashValue(const ProducerKey& key) {
return llvm::hash_combine(llvm::DenseMapInfo<ComputeInstance>::getHashValue(key.instance), key.resultIndex);
}
static bool isEqual(const ProducerKey& lhs, const ProducerKey& rhs) { return lhs == rhs; }
};
struct SameClassConsumerLookupKey {
mlir::Operation* sourceOp = nullptr;
size_t resultIndex = 0;
ClassId classId = 0;
bool operator==(const SameClassConsumerLookupKey& other) const {
return sourceOp == other.sourceOp && resultIndex == other.resultIndex && classId == other.classId;
}
};
struct SameClassConsumerLookupKeyInfo {
static SameClassConsumerLookupKey getEmptyKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getEmptyKey(), std::numeric_limits<size_t>::max(),
std::numeric_limits<ClassId>::max()};
}
static SameClassConsumerLookupKey getTombstoneKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getTombstoneKey(), std::numeric_limits<size_t>::max(),
std::numeric_limits<ClassId>::max()};
}
static unsigned getHashValue(const SameClassConsumerLookupKey& key) {
return llvm::hash_combine(llvm::DenseMapInfo<mlir::Operation*>::getHashValue(key.sourceOp),
key.resultIndex,
key.classId);
}
static bool isEqual(const SameClassConsumerLookupKey& lhs, const SameClassConsumerLookupKey& rhs) {
return lhs == rhs;
}
};
struct WholeBatchAssemblyLookupKey {
mlir::Operation* sourceOp = nullptr;
size_t resultIndex = 0;
ClassId classId = 0;
bool operator==(const WholeBatchAssemblyLookupKey& other) const {
return sourceOp == other.sourceOp && resultIndex == other.resultIndex && classId == other.classId;
}
};
struct WholeBatchAssemblyLookupKeyInfo {
static WholeBatchAssemblyLookupKey getEmptyKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getEmptyKey(), std::numeric_limits<size_t>::max(),
std::numeric_limits<ClassId>::max()};
}
static WholeBatchAssemblyLookupKey getTombstoneKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getTombstoneKey(), std::numeric_limits<size_t>::max(),
std::numeric_limits<ClassId>::max()};
}
static unsigned getHashValue(const WholeBatchAssemblyLookupKey& key) {
return llvm::hash_combine(llvm::DenseMapInfo<mlir::Operation*>::getHashValue(key.sourceOp),
key.resultIndex,
key.classId);
}
static bool isEqual(const WholeBatchAssemblyLookupKey& lhs, const WholeBatchAssemblyLookupKey& rhs) {
return lhs == rhs;
}
};
using ClassSlotKey = std::pair<ClassId, SlotId>;
struct ProjectedBatchInputKey {
mlir::Operation* consumerOp = nullptr;
unsigned inputIndex = 0;
bool operator==(const ProjectedBatchInputKey& other) const {
return consumerOp == other.consumerOp && inputIndex == other.inputIndex;
}
};
struct ProjectedBatchInputKeyInfo {
static ProjectedBatchInputKey getEmptyKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getEmptyKey(), std::numeric_limits<unsigned>::max()};
}
static ProjectedBatchInputKey getTombstoneKey() {
return {llvm::DenseMapInfo<mlir::Operation*>::getTombstoneKey(), std::numeric_limits<unsigned>::max()};
}
static unsigned getHashValue(const ProjectedBatchInputKey& key) {
return llvm::hash_combine(key.consumerOp, key.inputIndex);
}
static bool isEqual(const ProjectedBatchInputKey& lhs, const ProjectedBatchInputKey& rhs) { return lhs == rhs; }
};
} // namespace onnx_mlir::spatial
@@ -1,104 +0,0 @@
#include "ProjectedFragments.hpp"
#include "mlir/IR/BuiltinTypes.h"
namespace onnx_mlir::spatial {
static mlir::FailureOr<mlir::RankedTensorType> getPackedBatchTensorType(mlir::Type laneType, size_t laneCount) {
auto tensorType = mlir::dyn_cast<mlir::RankedTensorType>(laneType);
if (!tensorType || !tensorType.hasStaticShape() || tensorType.getRank() == 0)
return mlir::failure();
llvm::SmallVector<int64_t, 4> shape(tensorType.getShape());
shape[0] *= static_cast<int64_t>(laneCount);
return mlir::RankedTensorType::get(shape, tensorType.getElementType(), tensorType.getEncoding());
}
unsigned getProjectedFragmentsPerLogicalSlot(llvm::ArrayRef<int64_t> loopTripCounts) {
unsigned fragmentsPerLogicalSlot = 1;
for (int64_t tripCount : loopTripCounts) {
assert(tripCount > 0 && "projected loop trip counts must be positive");
fragmentsPerLogicalSlot *= static_cast<unsigned>(tripCount);
}
return fragmentsPerLogicalSlot;
}
mlir::LogicalResult verifyProjectedFragmentLayout(mlir::Operation* anchor, const ProjectedFragmentLayout& layout) {
if (!layout.fragmentType || layout.fragmentShape.empty())
return anchor->emitError("projected fragment layout is missing fragment type metadata");
if (layout.fragmentShape.size() != static_cast<size_t>(layout.fragmentType.getRank()))
return anchor->emitError("projected fragment layout rank does not match fragment type");
if (layout.payloadFragmentCount == 0 || layout.fragmentsPerLogicalSlot == 0)
return anchor->emitError("projected fragment layout has an invalid fragment count");
if (layout.payloadFragmentCount % layout.fragmentsPerLogicalSlot != 0)
return anchor->emitError("projected fragment layout payload fragment count is incompatible with logical slots");
return mlir::success();
}
mlir::FailureOr<mlir::RankedTensorType>
getProjectedPayloadType(mlir::Operation* anchor, mlir::RankedTensorType fragmentType, unsigned payloadFragmentCount) {
auto packedType = getPackedBatchTensorType(fragmentType, payloadFragmentCount);
if (mlir::failed(packedType)) {
anchor->emitError("cannot create projected payload type");
return mlir::failure();
}
return *packedType;
}
llvm::SmallVector<llvm::SmallVector<int64_t, 16>, 4>
buildProjectedFragmentOffsetsByDim(llvm::ArrayRef<llvm::SmallVector<int64_t, 4>> fragmentOffsets, size_t rank) {
llvm::SmallVector<llvm::SmallVector<int64_t, 16>, 4> fragmentOffsetsByDim(rank);
for (llvm::ArrayRef<int64_t> offsets : fragmentOffsets) {
assert(offsets.size() == rank && "projected offset rank mismatch");
for (size_t dim = 0; dim < rank; ++dim)
fragmentOffsetsByDim[dim].push_back(offsets[dim]);
}
return fragmentOffsetsByDim;
}
mlir::LogicalResult verifyProjectedTransferDescriptor(mlir::Operation* anchor,
const ProjectedTransferDescriptor& descriptor) {
if (mlir::failed(verifyProjectedFragmentLayout(anchor, descriptor.layout)))
return mlir::failure();
if (!descriptor.payloadType)
return anchor->emitError("projected transfer descriptor is missing payload type");
if (descriptor.fragmentOffsets.empty())
return anchor->emitError("projected transfer descriptor expected at least one fragment offset");
if (descriptor.fragmentOffsetsByDim.size() != descriptor.layout.fragmentShape.size())
return anchor->emitError("projected transfer descriptor dimension-major offsets are inconsistent");
for (llvm::ArrayRef<int64_t> dimOffsets : descriptor.fragmentOffsetsByDim)
if (dimOffsets.size() != descriptor.fragmentOffsets.size())
return anchor->emitError("projected transfer descriptor dimension-major offsets are inconsistent");
for (llvm::ArrayRef<int64_t> offsets : descriptor.fragmentOffsets)
if (offsets.size() != descriptor.layout.fragmentShape.size())
return anchor->emitError("projected transfer offset rank does not match fragment rank");
return mlir::success();
}
mlir::LogicalResult verifyProjectedSendDescriptor(mlir::Operation* anchor,
const ProjectedTransferDescriptor& descriptor,
const MessageVector& messages) {
if (mlir::failed(verifyProjectedTransferDescriptor(anchor, descriptor)))
return mlir::failure();
if (messages.size() * descriptor.layout.payloadFragmentCount != descriptor.fragmentOffsets.size())
return anchor->emitError("projected send descriptor metadata is inconsistent");
return mlir::success();
}
mlir::LogicalResult finalizeProjectedTransferDescriptor(mlir::Operation* anchor,
ProjectedTransferDescriptor& descriptor) {
descriptor.fragmentOffsetsByDim =
buildProjectedFragmentOffsetsByDim(descriptor.fragmentOffsets, descriptor.layout.fragmentShape.size());
auto payloadType =
getProjectedPayloadType(anchor, descriptor.layout.fragmentType, descriptor.layout.payloadFragmentCount);
if (mlir::failed(payloadType))
return mlir::failure();
if (descriptor.payloadType && descriptor.payloadType != *payloadType)
return anchor->emitError("projected transfer descriptor payload type does not match projected layout");
descriptor.payloadType = *payloadType;
return verifyProjectedTransferDescriptor(anchor, descriptor);
}
} // namespace onnx_mlir::spatial
@@ -1,87 +0,0 @@
#pragma once
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/ValueRange.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include <cstdint>
#include "MergeMessages.hpp"
#include "MergeScheduleKeys.hpp"
namespace onnx_mlir::spatial {
struct ProjectedFragmentLayout {
mlir::RankedTensorType fragmentType;
llvm::SmallVector<int64_t, 4> fragmentShape;
unsigned fragmentsPerLogicalSlot = 1;
unsigned payloadFragmentCount = 1;
llvm::SmallVector<int64_t, 4> loopLowerBounds;
llvm::SmallVector<int64_t, 4> loopSteps;
llvm::SmallVector<int64_t, 4> loopTripCounts;
};
struct StaticProjectedLoopInfo {
mlir::BlockArgument iv;
int64_t lowerBound = 0;
int64_t step = 1;
int64_t tripCount = 1;
};
struct ProjectedTransferDescriptor {
ProjectedBatchInputKey inputKey;
mlir::Operation* extractOp = nullptr;
ProjectedFragmentLayout layout;
mlir::RankedTensorType payloadType;
llvm::SmallVector<llvm::SmallVector<int64_t, 4>, 16> fragmentOffsets;
llvm::SmallVector<llvm::SmallVector<int64_t, 16>, 4> fragmentOffsetsByDim;
};
struct ProjectedExtractReplacement {
mlir::Value payload;
ProjectedFragmentLayout layout;
};
struct PendingProjectedHostOutputFragment {
mlir::Value originalOutput;
ClassId sourceClass = 0;
ProducerKey producerKey;
unsigned publicationResultIndex = 0;
int64_t sourceFragmentOrdinal = 0;
int64_t sourceElementOffset = 0;
llvm::SmallVector<int64_t, 4> offsets;
llvm::SmallVector<int64_t, 4> sizes;
llvm::SmallVector<int64_t, 4> strides;
uint32_t sourceLane = 0;
mlir::Location loc;
};
struct AffineProjectedInputSliceMatch {
mlir::tensor::ExtractSliceOp extract;
mlir::RankedTensorType sourceType;
mlir::RankedTensorType fragmentType;
llvm::SmallVector<int64_t, 4> fragmentShape;
llvm::SmallVector<mlir::OpFoldResult, 4> offsets;
llvm::SmallVector<StaticProjectedLoopInfo, 4> loops;
};
unsigned getProjectedFragmentsPerLogicalSlot(llvm::ArrayRef<int64_t> loopTripCounts);
mlir::LogicalResult verifyProjectedFragmentLayout(mlir::Operation* anchor, const ProjectedFragmentLayout& layout);
mlir::FailureOr<mlir::RankedTensorType>
getProjectedPayloadType(mlir::Operation* anchor, mlir::RankedTensorType fragmentType, unsigned payloadFragmentCount);
llvm::SmallVector<llvm::SmallVector<int64_t, 16>, 4>
buildProjectedFragmentOffsetsByDim(llvm::ArrayRef<llvm::SmallVector<int64_t, 4>> fragmentOffsets, size_t rank);
mlir::LogicalResult verifyProjectedTransferDescriptor(mlir::Operation* anchor,
const ProjectedTransferDescriptor& descriptor);
mlir::LogicalResult verifyProjectedSendDescriptor(mlir::Operation* anchor,
const ProjectedTransferDescriptor& descriptor,
const MessageVector& messages);
mlir::LogicalResult finalizeProjectedTransferDescriptor(mlir::Operation* anchor,
ProjectedTransferDescriptor& descriptor);
} // namespace onnx_mlir::spatial
@@ -0,0 +1,997 @@
#include "ScheduledComputeMaterialization.hpp"
#include "DeferredCommunicationPlanning.hpp"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "src/Accelerators/PIM/Common/IR/AffineUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/LoopUtils.hpp"
#include "src/Accelerators/PIM/Common/PimCommon.hpp"
#include <map>
#include "llvm/ADT/SmallPtrSet.h"
namespace onnx_mlir {
namespace spatial {
using namespace mlir;
namespace {
struct BatchFragmentSpec {
RankedTensorType resultType;
RankedTensorType sourceSliceType;
};
static SmallVector<OpFoldResult> remapMixedOffsets(ArrayRef<OpFoldResult> mixedOffsets, IRMapping &mapper) {
SmallVector<OpFoldResult> remapped;
remapped.reserve(mixedOffsets.size());
for (OpFoldResult ofr : mixedOffsets) {
if (auto value = dyn_cast<Value>(ofr))
remapped.push_back(cast<Value>(mapper.lookupOrDefault(value)));
else
remapped.push_back(cast<Attribute>(ofr));
}
return remapped;
}
static void appendUnique(SmallVectorImpl<Value> &values, Value value) {
if (!llvm::is_contained(values, value))
values.push_back(value);
}
static Value getBlockOperand(Block &block, ValueRange operands, Value value, unsigned firstArgument = 0) {
auto it = llvm::find(operands, value);
assert(it != operands.end() && "missing scheduled operand");
return block.getArgument(firstArgument + std::distance(operands.begin(), it));
}
static Value getScheduledComputeOutputArgument(Block &block, ValueRange scheduledWeights, ValueRange scheduledInputs,
ArrayRef<ProducerValueKey> carriedKeys, ProducerValueKey key) {
unsigned base = scheduledWeights.size() + scheduledInputs.size();
auto it = llvm::find(carriedKeys, key);
assert(it != carriedKeys.end() && "missing carried output");
return block.getArgument(base + std::distance(carriedKeys.begin(), it));
}
static unsigned getScheduledComputeResultArgBase(SpatScheduledCompute scheduled) {
return scheduled.getWeights().size() + scheduled.getInputs().size();
}
static void appendComputeBlockArguments(SmallVectorImpl<Type> &argTypes,
SmallVectorImpl<Location> &argLocs,
ValueRange weights,
ValueRange inputs,
ArrayRef<ProducerValueKey> carriedKeys,
Location loc) {
for (Value weight : weights)
argTypes.push_back(weight.getType());
for (Value input : inputs)
argTypes.push_back(input.getType());
for (ProducerValueKey key : carriedKeys) {
auto outputs = getComputeInstanceOutputValues(key.instance);
assert(key.resultIndex < outputs.size() && "missing carried result");
argTypes.push_back(outputs[key.resultIndex].getType());
}
argLocs.append(argTypes.size(), loc);
}
static Block *createScheduledComputeBlock(PatternRewriter &rewriter,
SpatScheduledCompute scheduled,
ArrayRef<ProducerValueKey> carriedKeys,
Location loc) {
SmallVector<Type> argTypes;
SmallVector<Location> argLocs;
appendComputeBlockArguments(argTypes, argLocs, scheduled.getWeights(), scheduled.getInputs(), carriedKeys, loc);
return rewriter.createBlock(&scheduled.getBody(), scheduled.getBody().end(), TypeRange(argTypes), argLocs);
}
static void appendBlockYieldBaseAndCarriedOperands(Block &block,
unsigned baseArgCount,
size_t carriedCount,
SmallVectorImpl<Value> &operands) {
for (unsigned index = 0; index < baseArgCount; ++index)
operands.push_back(block.getArgument(index));
for (size_t index = 0; index < carriedCount; ++index)
operands.push_back(block.getArgument(baseArgCount + index));
}
static void createBlockYield(PatternRewriter &rewriter, Location loc, ValueRange outputs, Block *next = nullptr) {
OperationState state(loc, SpatBlockYieldOp::getOperationName());
state.addOperands(outputs);
if (next)
state.addSuccessors(next);
rewriter.create(state);
}
static FailureOr<BatchFragmentSpec> getBatchFragmentSpec(SpatComputeBatch batch,
unsigned resultIndex,
uint32_t fragmentLaneCount) {
auto inParallel = dyn_cast<SpatInParallelOp>(batch.getBody().front().getTerminator());
if (!inParallel)
return batch.emitOpError("scheduled materialization only supports resultful spat.graph_compute_batch");
auto outputArg = batch.getOutputArgument(resultIndex);
if (!outputArg)
return batch.emitOpError("scheduled materialization could not locate batch output block argument");
for (Operation &op : inParallel.getRegion().front()) {
auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(&op);
if (!insert)
return batch.emitOpError("scheduled materialization only supports regular leading-lane graph_compute_batch fragments");
if (insert.getDest() != *outputArg)
continue;
RankedTensorType destType = insert.getDestType();
RankedTensorType sourceType = insert.getSourceType();
if (!destType || !sourceType || !destType.hasStaticShape() || !sourceType.hasStaticShape())
return batch.emitOpError("scheduled materialization only supports regular leading-lane graph_compute_batch fragments");
if (destType.getRank() != sourceType.getRank() + 1 || destType.getDimSize(0) != batch.getLaneCount()
|| destType.getElementType() != sourceType.getElementType())
return batch.emitOpError("graph_compute_batch result must be a leading physical-slot dimension followed by its fragment");
if (!llvm::equal(destType.getShape().drop_front(), sourceType.getShape()))
return batch.emitOpError("graph_compute_batch result trailing shape must match its published fragment");
if (!insert.hasUnitStride())
return batch.emitOpError("scheduled materialization only supports regular leading-lane graph_compute_batch fragments");
auto offsets = insert.getMixedOffsets();
auto sizes = insert.getMixedSizes();
auto strides = insert.getMixedStrides();
if (offsets.size() != static_cast<size_t>(destType.getRank()) || sizes.size() != static_cast<size_t>(destType.getRank())
|| strides.size() != static_cast<size_t>(destType.getRank()))
return batch.emitOpError("scheduled materialization only supports regular leading-lane graph_compute_batch fragments");
if (!isa<Value>(offsets.front()) || !valueTransitivelyDependsOn(cast<Value>(offsets.front()), *batch.getLaneArgument()))
return batch.emitOpError("graph_compute_batch publication must select its physical slot in dimension zero");
for (unsigned dim = 1; dim < offsets.size(); ++dim) {
auto offset = dyn_cast<Attribute>(offsets[dim]);
auto integer = dyn_cast_or_null<IntegerAttr>(offset);
if (!integer || integer.getInt() != 0)
return batch.emitOpError("graph_compute_batch publication must have zero trailing offsets");
}
auto staticIndex = [](OpFoldResult value) -> std::optional<int64_t> {
auto attr = dyn_cast<Attribute>(value);
auto integer = dyn_cast_or_null<IntegerAttr>(attr);
return integer ? std::optional<int64_t>(integer.getInt()) : std::nullopt;
};
if (staticIndex(sizes.front()) != 1)
return batch.emitOpError("graph_compute_batch publication sizes must be [1] plus the fragment shape");
for (auto [size, dim] : llvm::zip_equal(ArrayRef<OpFoldResult>(sizes).drop_front(), sourceType.getShape()))
if (staticIndex(size) != dim)
return batch.emitOpError("graph_compute_batch publication sizes must be [1] plus the fragment shape");
return BatchFragmentSpec {spatial::getGraphBatchPhysicalResultType(fragmentLaneCount, sourceType), sourceType};
}
return batch.emitOpError("scheduled materialization only supports regular leading-lane graph_compute_batch fragments");
}
static SourceLaneSelector buildSourceLaneSelector(PatternRewriter &rewriter,
const ComputeStepTuple &stepTuple,
Operation *constantAnchor,
std::map<std::vector<uint32_t>, Value> &laneStartTableCache) {
if (std::optional<SourceLaneAffineMapping> affineMapping = getSourceLaneAffineMapping(stepTuple)) {
SourceLaneSelector selector;
selector.kind = SourceLaneSelector::Kind::Affine;
selector.affine = *affineMapping;
return selector;
}
SmallVector<uint32_t> tableValues = collectSourceLaneStarts(stepTuple);
std::vector<uint32_t> cacheKey(tableValues.begin(), tableValues.end());
auto cacheIt = laneStartTableCache.find(cacheKey);
if (cacheIt != laneStartTableCache.end()) {
SourceLaneSelector selector;
selector.kind = SourceLaneSelector::Kind::Table;
selector.table = cacheIt->second;
selector.tableValues = tableValues;
return selector;
}
SmallVector<int64_t> tableValuesI64;
tableValuesI64.reserve(tableValues.size());
for (uint32_t value : tableValues)
tableValuesI64.push_back(value);
Value table = createI64LookupTableConstant(rewriter, constantAnchor, tableValuesI64);
laneStartTableCache.emplace(std::move(cacheKey), table);
SourceLaneSelector selector;
selector.kind = SourceLaneSelector::Kind::Table;
selector.table = table;
selector.tableValues = tableValues;
return selector;
}
static FailureOr<Value> buildSourceLaneStartForScheduledLane(OpBuilder &builder,
Location loc,
Value scheduledLane,
const SourceLaneSelector &selector,
Operation *constantAnchor) {
if (selector.kind == SourceLaneSelector::Kind::Affine) {
if (selector.affine.baseLaneStart == 0 && selector.affine.laneCount == 1)
return scheduledLane;
AffineExpr d0 = builder.getAffineDimExpr(0);
AffineExpr expr = d0;
if (selector.affine.laneCount != 1)
expr = d0 * selector.affine.laneCount;
if (selector.affine.baseLaneStart != 0)
expr = expr + selector.affine.baseLaneStart;
return createOrFoldAffineApply(builder, loc, expr, ValueRange {scheduledLane}, constantAnchor);
}
if (!selector.table)
return failure();
Value sourceLaneStartI64 =
tensor::ExtractOp::create(builder, loc, selector.table, ValueRange {scheduledLane}).getResult();
return arith::IndexCastOp::create(builder, loc, builder.getIndexType(), sourceLaneStartI64).getResult();
}
static LogicalResult verifyPeftClassPlan(Operation *diagnosticAnchor,
const PeftClassPlan &peftClassPlan,
const MergeScheduleResult &schedule) {
if (peftClassPlan.cpus.empty())
return diagnosticAnchor->emitOpError("PEFT materialization class has no CPUs");
SmallVector<const SmallVector<ComputeInstance> *> schedules;
for (size_t cpu : peftClassPlan.cpus) {
auto it = peftClassPlan.instancesByCpu.find(cpu);
if (it == peftClassPlan.instancesByCpu.end())
return diagnosticAnchor->emitOpError("PEFT materialization class is missing a per-CPU schedule");
schedules.push_back(&it->second);
for (const ComputeInstance &instance : it->second)
if (!schedule.computeToCpuSlotMap.count(instance))
return diagnosticAnchor->emitOpError("PEFT materialization class references a compute instance without a scheduler position");
}
if (peftClassPlan.cpus.size() == 1)
return success();
auto emitNonIso = [&](size_t stepPosition) -> LogicalResult {
std::string cpus;
llvm::raw_string_ostream os(cpus);
llvm::interleaveComma(peftClassPlan.cpus, os, [&](size_t cpu) { os << cpu; });
diagnosticAnchor->emitOpError("PEFT equivalence class has non-isomorphic per-CPU schedules")
<< " class " << peftClassPlan.canonicalClassId << " cpus [" << os.str() << "] step " << stepPosition;
return failure();
};
size_t tupleCount = schedules.front()->size();
for (const SmallVector<ComputeInstance> *cpuSchedule : schedules)
if (cpuSchedule->size() != tupleCount)
return emitNonIso(0);
for (size_t stepPosition = 0; stepPosition < tupleCount; ++stepPosition) {
const ComputeInstance &reference = (*schedules.front())[stepPosition];
bool refIsScalar = isa<SpatCompute>(reference.op);
for (size_t cpuIndex = 1; cpuIndex < schedules.size(); ++cpuIndex) {
const ComputeInstance &instance = (*schedules[cpuIndex])[stepPosition];
if (instance.op != reference.op || instance.laneCount != reference.laneCount)
return emitNonIso(stepPosition);
if (isa<SpatCompute>(instance.op) != refIsScalar)
return emitNonIso(stepPosition);
}
}
return success();
}
static LogicalResult collectPeftClassOperandsAndResults(PeftClassPlan &peftClassPlan,
const MergeScheduleResult &schedule) {
peftClassPlan.weights.clear();
peftClassPlan.inputs.clear();
peftClassPlan.resultTypes.clear();
if (peftClassPlan.cpus.size() == 1) {
size_t cpu = peftClassPlan.cpus.front();
for (const ComputeInstance &instance : peftClassPlan.instancesByCpu.lookup(cpu)) {
if (auto compute = dyn_cast<SpatCompute>(instance.op)) {
llvm::append_range(peftClassPlan.resultTypes, compute.getResultTypes());
} else {
auto batch = cast<SpatComputeBatch>(instance.op);
for (unsigned resultIndex = 0; resultIndex < batch.getNumResults(); ++resultIndex) {
auto spec = getBatchFragmentSpec(batch, resultIndex, instance.laneCount);
if (failed(spec))
return failure();
peftClassPlan.resultTypes.push_back(spec->resultType);
}
}
for (Value weight : getComputeInstanceWeights(instance))
appendUnique(peftClassPlan.weights, weight);
for (Value input : getComputeInstanceInputs(instance))
if (!getProducerValueRef(input, &instance) &&
!isDeferredFragmentAssemblyInput(input, instance))
appendUnique(peftClassPlan.inputs, input);
}
return success();
}
for (const ScheduledStepPlan &stepPlan : buildScheduledStepPlans(peftClassPlan)) {
const ComputeStepTuple &stepTuple = stepPlan.stepTuple;
const ComputeInstance &representative = stepTuple.instances.front();
if (auto compute = dyn_cast<SpatCompute>(representative.op)) {
for (Type type : compute.getResultTypes()) {
auto tensorType = dyn_cast<RankedTensorType>(type);
if (!tensorType || !tensorType.hasStaticShape())
return compute.emitOpError("scheduled materialization only supports static ranked tensor scalar results");
SmallVector<int64_t> shape;
shape.push_back(static_cast<int64_t>(peftClassPlan.cpus.size()));
llvm::append_range(shape, tensorType.getShape());
peftClassPlan.resultTypes.push_back(RankedTensorType::get(shape, tensorType.getElementType()));
}
} else {
auto batch = cast<SpatComputeBatch>(representative.op);
uint32_t totalLanes = static_cast<uint32_t>(peftClassPlan.cpus.size()) * representative.laneCount;
for (unsigned resultIndex = 0; resultIndex < batch.getNumResults(); ++resultIndex) {
auto spec = getBatchFragmentSpec(batch, resultIndex, totalLanes);
if (failed(spec))
return failure();
peftClassPlan.resultTypes.push_back(spec->resultType);
}
}
for (const ComputeInstance &instance : stepTuple.instances) {
for (Value weight : getComputeInstanceWeights(instance))
appendUnique(peftClassPlan.weights, weight);
for (Value input : getComputeInstanceInputs(instance))
if (!getProducerValueRef(input, &instance) &&
!isDeferredFragmentAssemblyInput(input, instance))
appendUnique(peftClassPlan.inputs, input);
}
}
return success();
}
static void cloneComputeBody(OpBuilder &builder, Block &source, IRMapping &mapper,
SmallVectorImpl<Value> &yieldedValues,
const llvm::SmallPtrSetImpl<Operation *> &absorbed) {
for (Operation &op : source.without_terminator())
if (!absorbed.contains(&op))
builder.clone(op, mapper);
auto yield = cast<SpatYieldOp>(source.getTerminator());
for (Value output : yield.getOutputs())
yieldedValues.push_back(mapper.lookup(output));
}
static LogicalResult materializeResultfulBatchChunkAsScalar(PatternRewriter &rewriter,
SpatComputeBatch batch,
const ComputeInstance &instance,
ValueRange scheduledWeights,
ValueRange scheduledInputs,
Block &block,
const MergeScheduleResult &schedule,
SmallVectorImpl<Value> &yieldedValues) {
SmallVector<Value> initResults;
SmallVector<BatchFragmentSpec> fragmentSpecs;
for (unsigned resultIndex = 0; resultIndex < batch.getNumResults(); ++resultIndex) {
auto spec = getBatchFragmentSpec(batch, resultIndex, instance.laneCount);
if (failed(spec))
return failure();
fragmentSpecs.push_back(*spec);
auto empty = createEmptyTensorForType(rewriter, batch.getLoc(), spec->resultType);
if (failed(empty))
return batch.emitOpError("scheduled materialization requires a physical graph fragment publication");
initResults.push_back(*empty);
}
Value lower = getOrCreateIndexConstant(rewriter, batch.getOperation(), instance.laneStart);
Value upper = getOrCreateIndexConstant(rewriter, batch.getOperation(), instance.laneStart + instance.laneCount);
Value step = getOrCreateIndexConstant(rewriter, batch.getOperation(), 1);
auto loop = buildNormalizedScfFor(
rewriter,
batch.getLoc(),
lower,
upper,
step,
initResults,
[&](OpBuilder &builder, Location bodyLoc, Value originalLane, ValueRange iterArgs, SmallVectorImpl<Value> &yielded) -> LogicalResult {
IRMapping mapper;
mapper.map(*batch.getLaneArgument(), originalLane);
Value localLane = arith::SubIOp::create(builder,
bodyLoc,
originalLane,
getOrCreateIndexConstant(rewriter, batch.getOperation(), instance.laneStart))
.getResult();
for (auto [index, weight] : llvm::enumerate(batch.getWeights()))
mapper.map(*batch.getWeightArgument(index), getBlockOperand(block, scheduledWeights, weight));
SmallVector<DeferredInputPlan> inputPlans;
for (auto [index, input] : llvm::enumerate(batch.getInputs())) {
DeferredInputPlan plan;
if (failed(prepareSingleCpuInput(builder,
input.getLoc(),
input,
*batch.getInputArgument(index),
instance,
schedule,
scheduledInputs,
block,
scheduledWeights.size(),
ArrayRef<ProducerValueKey> {},
*batch.getLaneArgument(),
originalLane,
plan)))
return failure();
plan.scalarizedLocalLane = localLane;
plan.scalarizedGraphLaneBase = lower;
plan.scalarizedLaneCount = instance.laneCount;
plan.scalarizedHoistBlock = &block;
inputPlans.push_back(std::move(plan));
}
for (auto [index, outputArg] : llvm::enumerate(batch.getOutputs()))
(void)outputArg, mapper.map(*batch.getOutputArgument(index), iterArgs[index]);
Block &source = batch.getBody().front();
llvm::SmallPtrSet<Operation *, 32> absorbed;
if (failed(materializeDeferredPayloadDemands(builder, bodyLoc, source, inputPlans, mapper, absorbed)))
return failure();
for (Operation &op : source.without_terminator())
if (!absorbed.contains(&op))
builder.clone(op, mapper);
auto inParallel = dyn_cast<SpatInParallelOp>(source.getTerminator());
if (!inParallel)
return batch.emitOpError("expected spat.in_parallel in resultful spat.graph_compute_batch"), failure();
DenseMap<BlockArgument, size_t> outputIndexByArg;
for (size_t index = 0; index < batch.getNumResults(); ++index)
outputIndexByArg[*batch.getOutputArgument(index)] = index;
SmallVector<Value> current(iterArgs.begin(), iterArgs.end());
for (Operation &op : inParallel.getRegion().front()) {
auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(&op);
if (!insert)
return batch.emitOpError("scheduled materialization requires a physical graph fragment publication");
auto oldDest = dyn_cast<BlockArgument>(insert.getDest());
if (!oldDest || !outputIndexByArg.count(oldDest))
return batch.emitOpError("scheduled materialization requires a physical graph fragment publication"), failure();
size_t resultIndex = outputIndexByArg.lookup(oldDest);
SmallVector<OpFoldResult> offsets = remapMixedOffsets(insert.getMixedOffsets(), mapper);
offsets.front() = localLane;
current[resultIndex] = tensor::InsertSliceOp::create(builder,
insert.getLoc(),
mapper.lookup(insert.getSource()),
current[resultIndex],
offsets,
remapMixedOffsets(insert.getMixedSizes(), mapper),
remapMixedOffsets(insert.getMixedStrides(), mapper))
.getResult();
}
llvm::append_range(yielded, current);
return success();
});
if (failed(loop))
return failure();
llvm::append_range(yieldedValues, loop->results);
return success();
}
static LogicalResult materializeSingleCpuPeftClass(
PatternRewriter &rewriter,
SpatScheduledCompute scheduled,
const PeftClassPlan &peftClassPlan,
const MergeScheduleResult &schedule,
DenseMap<GraphComputeBlockKey, Block *> &graphComputeToBlockMap,
ScheduledMaterializationRecord &record) {
size_t cpu = peftClassPlan.cpus.front();
auto instancesIt = peftClassPlan.instancesByCpu.find(cpu);
assert(instancesIt != peftClassPlan.instancesByCpu.end() && "missing single-cpu schedule");
const SmallVector<ComputeInstance> &instances = instancesIt->second;
SmallVector<ProducerValueKey> carriedKeys;
Block *block = nullptr;
for (auto [ordinal, instance] : llvm::enumerate(instances)) {
if (!block)
block = createScheduledComputeBlock(rewriter, scheduled, carriedKeys, instance.op->getLoc());
GraphComputeBlockKey key = getGraphComputeBlockKey(instance);
graphComputeToBlockMap[key] = block;
record.computeKeys.push_back(key);
record.blocks.push_back(block);
rewriter.setInsertionPointToStart(block);
SmallVector<Value> yieldedValues;
if (auto compute = dyn_cast<SpatCompute>(instance.op)) {
IRMapping mapper;
for (auto [index, weight] : llvm::enumerate(compute.getWeights()))
mapper.map(*compute.getWeightArgument(index), getBlockOperand(*block, scheduled.getWeights(), weight));
SmallVector<DeferredInputPlan> inputPlans;
for (auto [index, input] : llvm::enumerate(compute.getInputs())) {
DeferredInputPlan plan;
if (failed(prepareSingleCpuInput(rewriter,
input.getLoc(),
input,
*compute.getInputArgument(index),
instance,
schedule,
scheduled.getInputs(),
*block,
scheduled.getWeights().size(),
carriedKeys,
{},
{},
plan)))
return failure();
inputPlans.push_back(std::move(plan));
}
llvm::SmallPtrSet<Operation *, 32> absorbed;
if (failed(materializeDeferredPayloadDemands(rewriter, compute.getLoc(), compute.getBody().front(), inputPlans, mapper, absorbed)))
return failure();
cloneComputeBody(rewriter, compute.getBody().front(), mapper, yieldedValues, absorbed);
} else {
auto batch = cast<SpatComputeBatch>(instance.op);
if (failed(materializeResultfulBatchChunkAsScalar(rewriter,
batch,
instance,
scheduled.getWeights(),
scheduled.getInputs(),
*block,
schedule,
yieldedValues)))
return failure();
}
SmallVector<ProducerValueKey> currentKeys;
for (size_t index = 0; index < yieldedValues.size(); ++index)
currentKeys.push_back({instance, index});
unsigned baseArgCount = getScheduledComputeResultArgBase(scheduled);
SmallVector<Value> blockYieldOperands;
bool hasNextBlock = ordinal + 1 < instances.size();
if (hasNextBlock) {
SmallVector<ProducerValueKey> nextCarriedKeys(carriedKeys);
llvm::append_range(nextCarriedKeys, currentKeys);
Block *nextBlock = createScheduledComputeBlock(rewriter, scheduled, nextCarriedKeys, instance.op->getLoc());
appendBlockYieldBaseAndCarriedOperands(*block, baseArgCount, carriedKeys.size(), blockYieldOperands);
llvm::append_range(blockYieldOperands, yieldedValues);
rewriter.setInsertionPointToEnd(block);
createBlockYield(rewriter, instance.op->getLoc(), blockYieldOperands, nextBlock);
carriedKeys = std::move(nextCarriedKeys);
block = nextBlock;
} else {
for (ProducerValueKey carried : carriedKeys)
blockYieldOperands.push_back(getScheduledComputeOutputArgument(*block,
scheduled.getWeights(),
scheduled.getInputs(),
carriedKeys,
carried));
llvm::append_range(blockYieldOperands, yieldedValues);
rewriter.setInsertionPointToEnd(block);
createBlockYield(rewriter, instance.op->getLoc(), blockYieldOperands);
}
}
return success();
}
// Builds offsets for inserting one per-CPU local fragment into the
// scheduled_compute_batch output. The lane offset is in scheduled-output
// lane space, not local fragment lane space.
static SmallVector<OpFoldResult> buildScheduledOutputInsertOffsets(OpBuilder &builder,
Location loc,
Value scheduledLane,
int64_t lanesPerScheduledLane,
RankedTensorType localFragmentType,
Operation *constantAnchor) {
SmallVector<OpFoldResult> offsets;
Value scheduledOutputLane = scheduledLane;
if (lanesPerScheduledLane != 1) {
scheduledOutputLane = affineMulConst(
builder, loc, scheduledLane, lanesPerScheduledLane, constantAnchor);
}
offsets.push_back(scheduledOutputLane);
offsets.append(localFragmentType.getRank() - 1, OpFoldResult(builder.getIndexAttr(0)));
return offsets;
}
static LogicalResult materializeMultiCpuPeftClass(
PatternRewriter &rewriter,
SpatScheduledComputeBatch scheduled,
const PeftClassPlan &peftClassPlan,
const MergeScheduleResult &schedule,
DenseMap<GraphComputeBlockKey, Block *> &graphComputeToBlockMap,
ScheduledMaterializationRecord &record) {
std::map<std::vector<uint32_t>, Value> laneStartTableCache;
ArrayRef<ScheduledStepPlan> stepPlans = record.stepPlans;
for (const ScheduledStepPlan &stepPlan : stepPlans) {
const ComputeStepTuple &stepTuple = stepPlan.stepTuple;
SourceLaneSelector sourceLaneSelector =
buildSourceLaneSelector(rewriter, stepTuple, scheduled.getOperation(), laneStartTableCache);
SmallVector<Type> blockArgTypes {rewriter.getIndexType()};
SmallVector<Location> blockArgLocs {scheduled.getLoc()};
for (Value weight : scheduled.getWeights()) {
blockArgTypes.push_back(weight.getType());
blockArgLocs.push_back(weight.getLoc());
}
for (Value input : scheduled.getInputs()) {
blockArgTypes.push_back(input.getType());
blockArgLocs.push_back(input.getLoc());
}
for (Type resultType : scheduled.getResultTypes()) {
blockArgTypes.push_back(resultType);
blockArgLocs.push_back(scheduled.getLoc());
}
Block *block = rewriter.createBlock(&scheduled.getBody(), scheduled.getBody().end(), blockArgTypes, blockArgLocs);
for (const ComputeInstance &instance : stepTuple.instances) {
GraphComputeBlockKey key = getGraphComputeBlockKey(instance);
graphComputeToBlockMap[key] = block;
record.computeKeys.push_back(key);
record.blocks.push_back(block);
}
rewriter.setInsertionPointToStart(block);
Value scheduledLane = block->getArgument(0);
const ComputeInstance &representative = stepTuple.instances.front();
SmallVector<Value> finalLocalFragments;
if (auto compute = dyn_cast<SpatCompute>(representative.op)) {
IRMapping mapper;
for (auto [index, weight] : llvm::enumerate(compute.getWeights()))
mapper.map(*compute.getWeightArgument(index),
getBlockOperand(*block, scheduled.getWeights(), weight, 1));
unsigned firstInputArg = 1 + scheduled.getWeights().size();
SmallVector<DeferredInputPlan> inputPlans;
for (auto [index, input] : llvm::enumerate(compute.getInputs())) {
DeferredInputPlan plan;
if (failed(prepareMultiCpuTupleInput(rewriter,
input.getLoc(),
input,
*compute.getInputArgument(index),
stepTuple,
peftClassPlan,
schedule,
scheduled.getInputs(),
*block,
firstInputArg,
{},
{},
scheduledLane,
plan)))
return failure();
inputPlans.push_back(std::move(plan));
}
SmallVector<Value> yieldedValues;
llvm::SmallPtrSet<Operation *, 32> absorbed;
if (failed(materializeDeferredPayloadDemands(rewriter, compute.getLoc(), compute.getBody().front(), inputPlans, mapper, absorbed)))
return failure();
cloneComputeBody(rewriter, compute.getBody().front(), mapper, yieldedValues, absorbed);
for (Value yielded : yieldedValues) {
auto tensorType = dyn_cast<RankedTensorType>(yielded.getType());
if (!tensorType || !tensorType.hasStaticShape() || tensorType.getRank() == 0)
return compute.emitOpError("scheduled materialization only supports static ranked tensor scalar step results");
SmallVector<ReassociationIndices> reassociation;
reassociation.push_back({0, 1});
for (int64_t dim = 1; dim < tensorType.getRank(); ++dim)
reassociation.push_back({static_cast<int64_t>(dim + 1)});
SmallVector<int64_t> expandedShape {1};
llvm::append_range(expandedShape, tensorType.getShape());
finalLocalFragments.push_back(tensor::ExpandShapeOp::create(rewriter,
scheduled.getLoc(),
RankedTensorType::get(expandedShape, tensorType.getElementType()),
yielded,
reassociation)
.getResult());
}
} else {
auto batch = cast<SpatComputeBatch>(representative.op);
SmallVector<Value> localFragments;
SmallVector<BatchFragmentSpec> fragmentSpecs;
for (unsigned resultIndex = 0; resultIndex < batch.getNumResults(); ++resultIndex) {
auto spec = getBatchFragmentSpec(batch, resultIndex, representative.laneCount);
if (failed(spec))
return failure();
fragmentSpecs.push_back(*spec);
auto empty = createEmptyTensorForType(rewriter, batch.getLoc(), spec->resultType);
if (failed(empty))
return failure();
localFragments.push_back(*empty);
}
Value lower = getOrCreateIndexConstant(rewriter, scheduled.getOperation(), 0);
Value upper = getOrCreateIndexConstant(rewriter, scheduled.getOperation(), representative.laneCount);
Value step = getOrCreateIndexConstant(rewriter, scheduled.getOperation(), 1);
FailureOr<Value> sourceLaneStart =
buildSourceLaneStartForScheduledLane(rewriter, batch.getLoc(), scheduledLane, sourceLaneSelector, scheduled.getOperation());
if (failed(sourceLaneStart))
return failure();
auto loop = buildNormalizedScfFor(
rewriter,
batch.getLoc(),
lower,
upper,
step,
localFragments,
[&](OpBuilder &builder, Location bodyLoc, Value innerLane, ValueRange iterArgs, SmallVectorImpl<Value> &yielded) -> LogicalResult {
IRMapping mapper;
Value sourceLane = createOrFoldAffineApply(
builder,
bodyLoc,
builder.getAffineDimExpr(0) + builder.getAffineDimExpr(1),
ValueRange {*sourceLaneStart, innerLane},
scheduled.getOperation());
mapper.map(*batch.getLaneArgument(), sourceLane);
for (auto [index, weight] : llvm::enumerate(batch.getWeights()))
mapper.map(*batch.getWeightArgument(index),
getBlockOperand(*block, scheduled.getWeights(), weight, 1));
unsigned firstInputArg = 1 + scheduled.getWeights().size();
SmallVector<DeferredInputPlan> inputPlans;
for (auto [index, input] : llvm::enumerate(batch.getInputs())) {
DeferredInputPlan plan;
if (failed(prepareMultiCpuTupleInput(builder,
input.getLoc(),
input,
*batch.getInputArgument(index),
stepTuple,
peftClassPlan,
schedule,
scheduled.getInputs(),
*block,
firstInputArg,
*batch.getLaneArgument(),
sourceLane,
scheduledLane,
plan)))
return failure();
plan.scalarizedLocalLane = innerLane;
plan.scalarizedGraphLaneBase = *sourceLaneStart;
plan.scalarizedLaneCount = representative.laneCount;
plan.scalarizedHoistBlock = block;
inputPlans.push_back(std::move(plan));
}
for (unsigned index = 0; index < batch.getNumResults(); ++index)
mapper.map(*batch.getOutputArgument(index), iterArgs[index]);
llvm::SmallPtrSet<Operation *, 32> absorbed;
if (failed(materializeDeferredPayloadDemands(builder, bodyLoc, batch.getBody().front(), inputPlans, mapper, absorbed)))
return failure();
for (Operation &op : batch.getBody().front().without_terminator())
if (!absorbed.contains(&op))
builder.clone(op, mapper);
auto inParallel = dyn_cast<SpatInParallelOp>(batch.getBody().front().getTerminator());
if (!inParallel)
return batch.emitOpError("expected spat.in_parallel in resultful spat.graph_compute_batch"), failure();
DenseMap<BlockArgument, size_t> outputIndexByArg;
for (size_t index = 0; index < batch.getNumResults(); ++index)
outputIndexByArg[*batch.getOutputArgument(index)] = index;
SmallVector<Value> current(iterArgs.begin(), iterArgs.end());
for (Operation &op : inParallel.getRegion().front()) {
auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(&op);
if (!insert)
return batch.emitOpError("scheduled materialization requires a physical graph fragment publication");
auto oldDest = dyn_cast<BlockArgument>(insert.getDest());
if (!oldDest || !outputIndexByArg.count(oldDest))
return batch.emitOpError("scheduled materialization requires a physical graph fragment publication"), failure();
size_t resultIndex = outputIndexByArg.lookup(oldDest);
SmallVector<OpFoldResult> offsets = remapMixedOffsets(insert.getMixedOffsets(), mapper);
offsets.front() = innerLane;
current[resultIndex] = tensor::InsertSliceOp::create(builder,
insert.getLoc(),
mapper.lookup(insert.getSource()),
current[resultIndex],
offsets,
remapMixedOffsets(insert.getMixedSizes(), mapper),
remapMixedOffsets(insert.getMixedStrides(), mapper))
.getResult();
}
llvm::append_range(yielded, current);
return success();
});
if (failed(loop))
return failure();
finalLocalFragments.assign(loop->results.begin(), loop->results.end());
}
struct Publication {
Value fragment;
SmallVector<OpFoldResult> offsets;
SmallVector<OpFoldResult> sizes;
SmallVector<OpFoldResult> strides;
};
SmallVector<Publication> publications;
for (auto [resultIndex, localFragment] : llvm::enumerate(finalLocalFragments)) {
auto localFragmentType = cast<RankedTensorType>(localFragment.getType());
int64_t lanesPerScheduledLane = isa<SpatCompute>(representative.op) ? 1 : representative.laneCount;
SmallVector<OpFoldResult> offsets = buildScheduledOutputInsertOffsets(
rewriter,
scheduled.getLoc(),
scheduledLane,
lanesPerScheduledLane,
localFragmentType,
scheduled.getOperation());
SmallVector<OpFoldResult> sizes;
SmallVector<OpFoldResult> strides;
for (int64_t dim : localFragmentType.getShape()) {
sizes.push_back(rewriter.getIndexAttr(dim));
strides.push_back(rewriter.getIndexAttr(1));
}
publications.push_back(
{localFragment, std::move(offsets), std::move(sizes),
std::move(strides)});
}
auto inParallel = SpatInParallelOp::create(rewriter, scheduled.getLoc());
rewriter.setInsertionPointToStart(&inParallel.getRegion().front());
for (auto [resultIndex, publication] : llvm::enumerate(publications))
tensor::ParallelInsertSliceOp::create(
rewriter,
scheduled.getLoc(),
publication.fragment,
block->getArgument(getScheduledBatchResultArgBase(scheduled) + stepPlan.resultOffset + resultIndex),
publication.offsets,
publication.sizes,
publication.strides);
}
return success();
}
} // namespace
FailureOr<ScheduledComputeMaterializationResult>
materializeScheduledCompute(func::FuncOp funcOp,
const MergeScheduleResult &schedule,
PatternRewriter &rewriter) {
DenseMap<Operation *, int64_t> graphIds;
int64_t nextGraphId = 0;
for (Operation &op : funcOp.getOps())
if (isa<SpatGraphCompute, SpatGraphComputeBatch>(op)) {
graphIds[&op] = nextGraphId;
op.setAttr("scheduled.graph_id", rewriter.getI64IntegerAttr(nextGraphId++));
}
llvm::MapVector<size_t, PeftClassPlan> peftClassPlans;
for (const ComputeInstance &instance : schedule.dominanceOrderCompute) {
size_t cpu = schedule.computeToCpuMap.lookup(instance);
size_t canonicalPeftClassId = getCanonicalPeftClassId(cpu, schedule);
auto &peftClassPlan = peftClassPlans[canonicalPeftClassId];
peftClassPlan.canonicalClassId = canonicalPeftClassId;
if (!llvm::is_contained(peftClassPlan.cpus, cpu))
peftClassPlan.cpus.push_back(cpu);
peftClassPlan.instancesByCpu[cpu].push_back(instance);
}
for (auto &entry : peftClassPlans) {
PeftClassPlan &peftClassPlan = entry.second;
llvm::sort(peftClassPlan.cpus);
for (size_t cpu : peftClassPlan.cpus)
llvm::sort(peftClassPlan.instancesByCpu[cpu], [&](const ComputeInstance &lhs, const ComputeInstance &rhs) {
return std::tie(graphIds.find(lhs.op)->second,
schedule.computeToCpuSlotMap.find(lhs)->second) <
std::tie(graphIds.find(rhs.op)->second,
schedule.computeToCpuSlotMap.find(rhs)->second);
});
if (failed(verifyPeftClassPlan(funcOp.getOperation(), peftClassPlan, schedule)))
return failure();
if (failed(collectPeftClassOperandsAndResults(peftClassPlan, schedule)))
return failure();
}
Operation *insertionPoint = funcOp.getBody().front().getTerminator();
DenseMap<GraphComputeBlockKey, Block *> graphComputeToBlockMap;
DenseMap<size_t, SpatScheduledCompute> scheduledComputes;
DenseMap<size_t, SpatScheduledComputeBatch> scheduledComputeBatches;
DenseMap<size_t, size_t> classToRecordIndex;
std::vector<ScheduledMaterializationRecord> materializedSchedules;
for (auto &entry : peftClassPlans) {
PeftClassPlan &peftClassPlan = entry.second;
rewriter.setInsertionPoint(insertionPoint);
ScheduledMaterializationRecord record;
record.canonicalPeftClassId = peftClassPlan.canonicalClassId;
record.cpus = peftClassPlan.cpus;
record.stepPlans = buildScheduledStepPlans(peftClassPlan);
if (peftClassPlan.cpus.size() == 1) {
auto scheduled = SpatScheduledCompute::create(
rewriter,
peftClassPlan.instancesByCpu.lookup(peftClassPlan.cpus.front()).front().op->getLoc(),
TypeRange(peftClassPlan.resultTypes),
peftClassPlan.weights,
peftClassPlan.inputs);
scheduled->setAttr(kCoreIdAttrName, rewriter.getI32IntegerAttr(static_cast<int32_t>(peftClassPlan.cpus.front())));
scheduled->setAttr("scheduled.peft_cpus", rewriter.getDenseI64ArrayAttr(toI64Array(peftClassPlan.cpus)));
SmallVector<Attribute> stepSources;
SmallVector<Attribute> sourceLaneSelectors;
SmallVector<int64_t> stepResultOffsets;
SmallVector<int64_t> stepResultCounts;
SmallVector<int64_t> sourceLaneStarts;
SmallVector<int64_t> sourceLaneCounts;
SmallVector<int64_t> stepSourceIds;
size_t resultOffset = 0;
for (const ComputeInstance &instance : peftClassPlan.instancesByCpu.lookup(peftClassPlan.cpus.front())) {
stepSources.push_back(rewriter.getStringAttr(getInstanceName(instance)));
stepSourceIds.push_back(graphIds.lookup(instance.op));
sourceLaneSelectors.push_back(rewriter.getStringAttr(isa<SpatCompute>(instance.op) ? "scalar" : "affine"));
size_t resultCount = getComputeInstanceResultValueCount(instance);
stepResultOffsets.push_back(static_cast<int64_t>(resultOffset));
stepResultCounts.push_back(static_cast<int64_t>(resultCount));
resultOffset += resultCount;
if (isa<SpatCompute>(instance.op)) {
sourceLaneStarts.push_back(0);
sourceLaneCounts.push_back(0);
} else {
sourceLaneStarts.push_back(instance.laneStart);
sourceLaneCounts.push_back(instance.laneCount);
}
}
scheduled->setAttr("scheduled.step_sources", rewriter.getArrayAttr(stepSources));
scheduled->setAttr("scheduled.step_source_ids", rewriter.getDenseI64ArrayAttr(stepSourceIds));
scheduled->setAttr("scheduled.step_result_offsets", rewriter.getDenseI64ArrayAttr(stepResultOffsets));
scheduled->setAttr("scheduled.step_result_counts", rewriter.getDenseI64ArrayAttr(stepResultCounts));
scheduled->setAttr("scheduled.source_lane_starts", rewriter.getDenseI64ArrayAttr(sourceLaneStarts));
scheduled->setAttr("scheduled.source_lane_counts", rewriter.getDenseI64ArrayAttr(sourceLaneCounts));
scheduled->setAttr("scheduled.source_lane_selector", rewriter.getArrayAttr(sourceLaneSelectors));
record.scheduledOp = scheduled.getOperation();
scheduledComputes[peftClassPlan.canonicalClassId] = scheduled;
} else {
auto scheduled = SpatScheduledComputeBatch::create(rewriter,
peftClassPlan.instancesByCpu.lookup(peftClassPlan.cpus.front()).front().op->getLoc(),
TypeRange(peftClassPlan.resultTypes),
rewriter.getI32IntegerAttr(static_cast<int32_t>(peftClassPlan.cpus.size())),
peftClassPlan.weights,
peftClassPlan.inputs);
scheduled->setAttr(kCoreIdsAttrName, rewriter.getDenseI32ArrayAttr(toI32Array(peftClassPlan.cpus)));
scheduled->setAttr("scheduled.peft_cpus", rewriter.getDenseI64ArrayAttr(toI64Array(peftClassPlan.cpus)));
SmallVector<Attribute> stepSources;
SmallVector<Attribute> sourceLaneSelectors;
SmallVector<int64_t> resultOffsets;
SmallVector<int64_t> resultCounts;
SmallVector<int64_t> sourceLaneStarts;
SmallVector<int64_t> sourceLaneCounts;
SmallVector<int64_t> stepSourceIds;
for (const ScheduledStepPlan &stepPlan : record.stepPlans) {
stepSources.push_back(rewriter.getStringAttr(getInstanceName(stepPlan.stepTuple.instances.front())));
stepSourceIds.push_back(graphIds.lookup(stepPlan.stepTuple.instances.front().op));
sourceLaneSelectors.push_back(rewriter.getStringAttr(usesAffineSourceLaneMapping(stepPlan.stepTuple) ? "affine" : "table"));
resultOffsets.push_back(static_cast<int64_t>(stepPlan.resultOffset));
resultCounts.push_back(static_cast<int64_t>(stepPlan.resultCount));
for (const ComputeInstance &instance : stepPlan.stepTuple.instances) {
sourceLaneStarts.push_back(instance.laneStart);
sourceLaneCounts.push_back(instance.laneCount);
}
}
RankedTensorType sourceLaneTableType = RankedTensorType::get(
{static_cast<int64_t>(record.stepPlans.size()), static_cast<int64_t>(peftClassPlan.cpus.size())},
rewriter.getI64Type());
scheduled->setAttr("scheduled.step_sources", rewriter.getArrayAttr(stepSources));
scheduled->setAttr("scheduled.step_source_ids", rewriter.getDenseI64ArrayAttr(stepSourceIds));
scheduled->setAttr("scheduled.step_result_offsets", rewriter.getDenseI64ArrayAttr(resultOffsets));
scheduled->setAttr("scheduled.step_result_counts", rewriter.getDenseI64ArrayAttr(resultCounts));
scheduled->setAttr("scheduled.source_lane_starts", DenseElementsAttr::get(sourceLaneTableType, ArrayRef<int64_t>(sourceLaneStarts)));
scheduled->setAttr("scheduled.source_lane_counts", DenseElementsAttr::get(sourceLaneTableType, ArrayRef<int64_t>(sourceLaneCounts)));
scheduled->setAttr("scheduled.source_lane_selector", rewriter.getArrayAttr(sourceLaneSelectors));
record.scheduledOp = scheduled.getOperation();
scheduledComputeBatches[peftClassPlan.canonicalClassId] = scheduled;
}
classToRecordIndex[peftClassPlan.canonicalClassId] = materializedSchedules.size();
materializedSchedules.push_back(std::move(record));
}
for (auto &entry : peftClassPlans) {
PeftClassPlan &peftClassPlan = entry.second;
ScheduledMaterializationRecord &record =
materializedSchedules[classToRecordIndex.lookup(peftClassPlan.canonicalClassId)];
if (peftClassPlan.cpus.size() == 1) {
if (failed(materializeSingleCpuPeftClass(rewriter,
scheduledComputes.lookup(peftClassPlan.canonicalClassId),
peftClassPlan,
schedule,
graphComputeToBlockMap,
record)))
return failure();
} else {
if (failed(materializeMultiCpuPeftClass(rewriter,
scheduledComputeBatches.lookup(peftClassPlan.canonicalClassId),
peftClassPlan,
schedule,
graphComputeToBlockMap,
record)))
return failure();
}
}
return ScheduledComputeMaterializationResult {std::move(peftClassPlans), std::move(materializedSchedules), std::move(graphComputeToBlockMap)};
}
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,20 @@
#pragma once
#include "ScheduledComputePlan.hpp"
namespace onnx_mlir {
namespace spatial {
struct ScheduledComputeMaterializationResult {
llvm::MapVector<size_t, PeftClassPlan> peftClassPlans;
std::vector<ScheduledMaterializationRecord> materializedSchedules;
DenseMap<GraphComputeBlockKey, Block *> graphComputeToBlockMap;
};
FailureOr<ScheduledComputeMaterializationResult>
materializeScheduledCompute(func::FuncOp funcOp,
const MergeScheduleResult &schedule,
PatternRewriter &rewriter);
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,320 @@
#pragma once
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/IRMapping.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <limits>
#include <optional>
#include <string>
#include <vector>
#include "Scheduling/ComputeInstanceUtils.hpp"
#include "Scheduling/MergeSchedulingAnalysis.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
namespace onnx_mlir {
namespace spatial {
using namespace mlir;
struct ProducerValueKey {
ComputeInstance instance;
size_t resultIndex = 0;
bool operator==(const ProducerValueKey &other) const {
return instance == other.instance && resultIndex == other.resultIndex;
}
};
struct GraphComputeBlockKey {
Operation *op = nullptr;
uint32_t laneStart = 0;
uint32_t laneCount = 1;
bool operator==(const GraphComputeBlockKey &other) const {
return op == other.op && laneStart == other.laneStart && laneCount == other.laneCount;
}
};
struct PeftClassPlan {
size_t canonicalClassId = 0;
SmallVector<size_t> cpus;
llvm::MapVector<size_t, SmallVector<ComputeInstance>> instancesByCpu;
SmallVector<Value> weights;
SmallVector<Value> inputs;
SmallVector<Type> resultTypes;
};
struct ComputeStepTuple {
SmallVector<ComputeInstance> instances;
};
struct ScheduledStepPlan {
ComputeStepTuple stepTuple;
size_t stepIndex = 0;
size_t resultOffset = 0;
size_t resultCount = 0;
};
struct SourceLaneAffineMapping {
uint32_t baseLaneStart = 0;
uint32_t laneCount = 1;
};
struct SourceLaneSelector {
enum class Kind { Affine, Table } kind = Kind::Affine;
SourceLaneAffineMapping affine;
Value table;
SmallVector<uint32_t> tableValues;
};
struct ScheduledMaterializationRecord {
Operation *scheduledOp = nullptr;
size_t canonicalPeftClassId = 0;
SmallVector<size_t> cpus;
SmallVector<ScheduledStepPlan> stepPlans;
SmallVector<GraphComputeBlockKey> computeKeys;
SmallVector<Block *> blocks;
};
struct ScheduledComputePrintContext {
mlir::AsmState asmState;
explicit ScheduledComputePrintContext(ModuleOp module, const OpPrintingFlags &flags = OpPrintingFlags())
: asmState(module.getOperation(), flags) {}
};
inline GraphComputeBlockKey getGraphComputeBlockKey(const ComputeInstance &instance) {
return {instance.op, instance.laneStart, instance.laneCount};
}
inline SmallVector<size_t> getPeftClassCpus(size_t cpu, const MergeScheduleResult &schedule) {
llvm::SmallDenseSet<size_t, 8> seen;
SmallVector<size_t> cpus;
auto append = [&](size_t value) {
if (seen.insert(value).second)
cpus.push_back(value);
};
append(cpu);
if (auto it = schedule.equivalentClass.find(cpu); it != schedule.equivalentClass.end())
for (size_t equivalentCpu : it->second)
append(equivalentCpu);
llvm::sort(cpus);
return cpus;
}
inline size_t getCanonicalPeftClassId(size_t cpu, const MergeScheduleResult &schedule) {
SmallVector<size_t> cpus = getPeftClassCpus(cpu, schedule);
return cpus.empty() ? cpu : cpus.front();
}
inline size_t getScheduledCpuForComputeInstance(const ComputeInstance &instance, const MergeScheduleResult &schedule) {
if (auto it = schedule.computeToCpuMap.find(instance); it != schedule.computeToCpuMap.end())
return it->second;
auto batch = dyn_cast<SpatComputeBatch>(instance.op);
assert(batch && instance.laneCount != 0 && "missing scheduled CPU for non-batch compute instance");
assert(instance.laneStart < static_cast<uint32_t>(batch.getLaneCount()) && "batch lane start out of range");
ComputeInstance chunk = getBatchChunkForLane(batch, instance.laneStart);
auto it = schedule.computeToCpuMap.find(chunk);
assert(it != schedule.computeToCpuMap.end() && "missing scheduled CPU for batch chunk");
return it->second;
}
inline std::string getInstanceName(const ComputeInstance &instance) {
return llvm::formatv("{0}[lanes={1}:{2}]",
instance.op->getName().getStringRef(),
instance.laneStart,
instance.laneStart + instance.laneCount)
.str();
}
inline SmallVector<int64_t> toI64Array(ArrayRef<size_t> values) {
SmallVector<int64_t> converted;
converted.reserve(values.size());
for (size_t value : values)
converted.push_back(static_cast<int64_t>(value));
return converted;
}
inline SmallVector<int32_t> toI32Array(ArrayRef<size_t> values) {
SmallVector<int32_t> converted;
converted.reserve(values.size());
for (size_t value : values)
converted.push_back(static_cast<int32_t>(value));
return converted;
}
inline unsigned getScheduledBatchResultArgBase(SpatScheduledComputeBatch scheduled) {
unsigned weightArgBase = 1;
unsigned inputArgBase = weightArgBase + scheduled.getWeights().size();
return inputArgBase + scheduled.getInputs().size();
}
inline SmallVector<GraphComputeBlockKey> collectExpectedGraphComputeBlockKeys(func::FuncOp funcOp) {
SmallVector<GraphComputeBlockKey> keys;
for (Operation &op : funcOp.getOps()) {
if (auto compute = dyn_cast<SpatGraphCompute>(&op))
keys.push_back(getGraphComputeBlockKey({compute.getOperation(), 0, 1}));
else if (auto batch = dyn_cast<SpatGraphComputeBatch>(&op))
for (ComputeInstance chunk : getBatchChunksForRange(batch, 0, static_cast<uint32_t>(batch.getLaneCount())))
keys.push_back(getGraphComputeBlockKey(chunk));
}
return keys;
}
inline size_t countPeftEquivalenceClasses(const MergeScheduleResult &schedule) {
llvm::SmallDenseSet<size_t, 16> classes;
for (const ComputeInstance &instance : schedule.dominanceOrderCompute) {
auto cpuIt = schedule.computeToCpuMap.find(instance);
if (cpuIt == schedule.computeToCpuMap.end())
continue;
classes.insert(getCanonicalPeftClassId(cpuIt->second, schedule));
}
return classes.size();
}
inline SmallVector<ComputeStepTuple> buildComputeStepTuples(const PeftClassPlan &peftClassPlan) {
SmallVector<ComputeStepTuple> stepTuples;
if (peftClassPlan.cpus.empty())
return stepTuples;
size_t stepCount = peftClassPlan.instancesByCpu.lookup(peftClassPlan.cpus.front()).size();
stepTuples.resize(stepCount);
for (size_t stepIndex = 0; stepIndex < stepCount; ++stepIndex)
for (size_t cpu : peftClassPlan.cpus)
stepTuples[stepIndex].instances.push_back(peftClassPlan.instancesByCpu.lookup(cpu)[stepIndex]);
return stepTuples;
}
inline size_t getComputeInstanceResultValueCount(const ComputeInstance &instance) {
return TypeSwitch<Operation *, size_t>(instance.op)
.Case<SpatCompute>([](SpatCompute compute) { return compute.getNumResults(); })
.Case<SpatComputeBatch>([](SpatComputeBatch batch) { return batch.getNumResults(); })
.Default([](Operation *) -> size_t {
llvm_unreachable("expected graph compute or graph compute batch");
});
}
inline SmallVector<ScheduledStepPlan> buildScheduledStepPlans(const PeftClassPlan &peftClassPlan) {
SmallVector<ScheduledStepPlan> stepPlans;
size_t resultOffset = 0;
for (auto [stepIndex, stepTuple] : llvm::enumerate(buildComputeStepTuples(peftClassPlan))) {
assert(!stepTuple.instances.empty() && "expected non-empty step tuple");
size_t resultCount = getComputeInstanceResultValueCount(stepTuple.instances.front());
stepPlans.push_back(ScheduledStepPlan {stepTuple, stepIndex, resultOffset, resultCount});
resultOffset += resultCount;
}
return stepPlans;
}
inline bool valueTransitivelyDependsOn(Value value, Value dependency) {
SmallVector<Value> worklist {value};
DenseSet<Value> visited;
while (!worklist.empty()) {
Value current = worklist.pop_back_val();
if (!visited.insert(current).second)
continue;
if (current == dependency)
return true;
Operation *def = current.getDefiningOp();
if (!def)
continue;
for (Value operand : def->getOperands())
worklist.push_back(operand);
}
return false;
}
inline std::optional<SourceLaneAffineMapping> getSourceLaneAffineMapping(const ComputeStepTuple &stepTuple) {
if (stepTuple.instances.empty())
return std::nullopt;
const ComputeInstance &reference = stepTuple.instances.front();
for (const ComputeInstance &instance : stepTuple.instances) {
if (instance.op != reference.op || instance.laneCount != reference.laneCount)
return std::nullopt;
}
for (size_t index = 0; index < stepTuple.instances.size(); ++index) {
uint32_t expectedLaneStart = reference.laneStart + static_cast<uint32_t>(index) * reference.laneCount;
if (stepTuple.instances[index].laneStart != expectedLaneStart)
return std::nullopt;
}
return SourceLaneAffineMapping {reference.laneStart, reference.laneCount};
}
inline bool usesAffineSourceLaneMapping(const ComputeStepTuple &stepTuple) {
return getSourceLaneAffineMapping(stepTuple).has_value();
}
inline SmallVector<uint32_t> collectSourceLaneStarts(const ComputeStepTuple &stepTuple) {
SmallVector<uint32_t> sourceLaneStarts;
sourceLaneStarts.reserve(stepTuple.instances.size());
for (const ComputeInstance &instance : stepTuple.instances)
sourceLaneStarts.push_back(instance.laneStart);
return sourceLaneStarts;
}
inline Value createI64LookupTableConstant(OpBuilder &builder, Operation *constantAnchor, ArrayRef<int64_t> values) {
RankedTensorType tableType = RankedTensorType::get({static_cast<int64_t>(values.size())}, builder.getI64Type());
DenseElementsAttr tableAttr = DenseElementsAttr::get(tableType, values);
return getOrCreateConstant(builder, constantAnchor, tableAttr, tableType);
}
inline FailureOr<Value> createEmptyTensorForType(OpBuilder &builder, Location loc, Type type) {
auto tensorType = dyn_cast<RankedTensorType>(type);
if (!tensorType || !tensorType.hasStaticShape())
return failure();
return tensor::EmptyOp::create(builder, loc, tensorType.getShape(), tensorType.getElementType()).getResult();
}
} // namespace spatial
} // namespace onnx_mlir
namespace llvm {
template <>
struct DenseMapInfo<onnx_mlir::spatial::ProducerValueKey> {
static onnx_mlir::spatial::ProducerValueKey getEmptyKey() {
return {DenseMapInfo<onnx_mlir::spatial::ComputeInstance>::getEmptyKey(), std::numeric_limits<size_t>::max()};
}
static onnx_mlir::spatial::ProducerValueKey getTombstoneKey() {
return {DenseMapInfo<onnx_mlir::spatial::ComputeInstance>::getTombstoneKey(), std::numeric_limits<size_t>::max()};
}
static unsigned getHashValue(const onnx_mlir::spatial::ProducerValueKey &key) {
return hash_combine(DenseMapInfo<onnx_mlir::spatial::ComputeInstance>::getHashValue(key.instance), key.resultIndex);
}
static bool isEqual(const onnx_mlir::spatial::ProducerValueKey &lhs,
const onnx_mlir::spatial::ProducerValueKey &rhs) {
return lhs == rhs;
}
};
template <>
struct DenseMapInfo<onnx_mlir::spatial::GraphComputeBlockKey> {
static onnx_mlir::spatial::GraphComputeBlockKey getEmptyKey() {
return {DenseMapInfo<mlir::Operation *>::getEmptyKey(), UINT32_MAX, UINT32_MAX};
}
static onnx_mlir::spatial::GraphComputeBlockKey getTombstoneKey() {
return {DenseMapInfo<mlir::Operation *>::getTombstoneKey(), UINT32_MAX, UINT32_MAX - 1};
}
static unsigned getHashValue(const onnx_mlir::spatial::GraphComputeBlockKey &key) {
return hash_combine(key.op, key.laneStart, key.laneCount);
}
static bool isEqual(const onnx_mlir::spatial::GraphComputeBlockKey &lhs,
const onnx_mlir::spatial::GraphComputeBlockKey &rhs) {
return lhs == rhs;
}
};
} // namespace llvm
@@ -0,0 +1,304 @@
#include "ScheduledComputeReport.hpp"
#include "llvm/Support/raw_os_ostream.h"
#include <fstream>
#include "src/Accelerators/PIM/Common/Support/DebugDump.hpp"
#include "src/Accelerators/PIM/Common/Support/Diagnostics.hpp"
namespace onnx_mlir {
namespace spatial {
using namespace mlir;
namespace {
static std::string formatValueLabel(Value value, AsmState &asmState) {
std::string storage;
llvm::raw_string_ostream os(storage);
value.printAsOperand(os, asmState);
return storage;
}
static std::string formatOperationLabel(Operation *op, AsmState &asmState) {
if (op->getNumResults() == 0)
return op->getName().getStringRef().str();
std::string storage;
llvm::raw_string_ostream os(storage);
llvm::interleaveComma(op->getResults(), os, [&](Value result) { os << formatValueLabel(result, asmState); });
return os.str();
}
static std::string formatGraphComputeBlockKey(const GraphComputeBlockKey &key, AsmState &asmState) {
return llvm::formatv("{0} {1}", formatOperationLabel(key.op, asmState), key.op->getName().getStringRef()).str();
}
static std::string formatComputeInstanceForReport(const ComputeInstance &instance, AsmState &asmState) {
std::string opLabel = formatGraphComputeBlockKey(getGraphComputeBlockKey(instance), asmState);
if (isa<SpatCompute>(instance.op))
return opLabel;
return llvm::formatv("{0} sourceLanes [{1}:{2}]",
opLabel,
instance.laneStart,
instance.laneStart + instance.laneCount)
.str();
}
template <typename T>
static void printIndexedList(raw_ostream &os, ArrayRef<T> values) {
os << "[";
llvm::interleaveComma(llvm::enumerate(values), os, [&](auto indexedValue) {
os << indexedValue.index() << ":" << indexedValue.value();
});
os << "]";
}
struct PeftMaterializationReportSummary {
size_t scalarGraphCompute = 0;
size_t graphComputeBatchOps = 0;
size_t scalarGraphComputeInstances = 0;
size_t graphComputeBatchInstances = 0;
size_t peftClasses = 0;
size_t singleCpuClasses = 0;
size_t multiCpuClasses = 0;
size_t scheduledCompute = 0;
size_t scheduledComputeBatch = 0;
size_t deferredCommunication = 0;
size_t deferredCommunicationMultiSourcePayloads = 0;
};
static PeftMaterializationReportSummary buildPeftMaterializationReportSummary(
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules) {
PeftMaterializationReportSummary summary;
for (Operation &op : funcOp.getOps()) {
if (isa<SpatGraphCompute>(op))
summary.scalarGraphCompute++;
else if (isa<SpatGraphComputeBatch>(op)) {
summary.graphComputeBatchOps++;
}
}
for (const ComputeInstance &instance : schedule.dominanceOrderCompute)
(isa<SpatCompute>(instance.op) ? summary.scalarGraphComputeInstances : summary.graphComputeBatchInstances)++;
summary.peftClasses = peftClassPlans.size();
for (const auto &entry : peftClassPlans)
(entry.second.cpus.size() == 1 ? summary.singleCpuClasses : summary.multiCpuClasses)++;
for (const ScheduledMaterializationRecord &record : materializedSchedules) {
if (isa<SpatScheduledCompute>(record.scheduledOp))
summary.scheduledCompute++;
else
summary.scheduledComputeBatch++;
}
funcOp.walk([&](SpatDeferredCommunicationOp transfer) {
summary.deferredCommunication++;
if (transfer.getSources().size() > 1)
summary.deferredCommunicationMultiSourcePayloads++;
});
return summary;
}
} // namespace
LogicalResult verifyPeftMaterializationReportSummary(func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules) {
PeftMaterializationReportSummary summary =
buildPeftMaterializationReportSummary(funcOp, schedule, peftClassPlans, materializedSchedules);
pim::CappedDiagnosticReporter diagnostics;
if (summary.peftClasses != peftClassPlans.size())
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check report PEFT total " << summary.peftClasses
<< " does not match classes.size() " << peftClassPlans.size();
});
if (summary.scalarGraphComputeInstances + summary.graphComputeBatchInstances != schedule.dominanceOrderCompute.size())
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check report total compute instances "
<< (summary.scalarGraphComputeInstances + summary.graphComputeBatchInstances)
<< " does not match schedule size " << schedule.dominanceOrderCompute.size();
});
if (summary.scheduledCompute + summary.scheduledComputeBatch != materializedSchedules.size())
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check report scheduled total "
<< (summary.scheduledCompute + summary.scheduledComputeBatch)
<< " does not match materialized scheduled ops " << materializedSchedules.size();
});
diagnostics.emitSuppressedSummary(funcOp, "scheduled Spatial report verification failed");
return success(!diagnostics.hasFailure());
}
namespace {
static std::string formatStepResultRange(size_t resultOffset, size_t resultCount) {
if (resultCount == 1)
return llvm::formatv("result[{0}]", resultOffset).str();
return llvm::formatv("result[{0}:{1}]", resultOffset, resultOffset + resultCount).str();
}
static void printMultiSourceDeferredInputs(raw_ostream &os, Block &block) {
unsigned deferredInputIndex = 0;
for (Operation &op : block.getOperations()) {
auto transfer = dyn_cast<SpatDeferredCommunicationOp>(&op);
if (!transfer)
continue;
auto multiSourcePayload = transfer->getAttrOfType<BoolAttr>("multi_source_payload");
auto sourceOperandForScheduledLane =
transfer->getAttrOfType<DenseI64ArrayAttr>("source_operand_for_scheduled_lane");
if (multiSourcePayload && multiSourcePayload.getValue() && sourceOperandForScheduledLane) {
SmallVector<size_t> sourceOperandIndexes;
for (int64_t sourceOperandIndex : sourceOperandForScheduledLane.asArrayRef())
sourceOperandIndexes.push_back(static_cast<size_t>(sourceOperandIndex));
os << " deferred input " << deferredInputIndex << ": multi-source uniqueSources="
<< transfer.getSources().size() << " sourceOperandForScheduledLane=";
printIndexedList(os, ArrayRef<size_t>(sourceOperandIndexes));
os << "\n";
}
deferredInputIndex++;
}
}
static void dumpPeftMaterializationReport(ModuleOp moduleOp,
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules,
ScheduledComputePrintContext &printContext) {
std::fstream file = openDialectDumpFileWithExtension("spatial2_scheduled_no_comm", "/reports", "txt");
if (!file.is_open())
return;
llvm::raw_os_ostream os(file);
AsmState &asmState = printContext.asmState;
PeftMaterializationReportSummary summary =
buildPeftMaterializationReportSummary(funcOp, schedule, peftClassPlans, materializedSchedules);
os << "Summary\n";
os << "=======\n";
os << "Graph computes:\n";
os << " total: " << (summary.scalarGraphCompute + summary.graphComputeBatchOps) << "\n";
os << " scalar graph_compute: " << summary.scalarGraphCompute << "\n";
os << " graph_compute_batch: " << summary.graphComputeBatchOps << "\n";
os << "Compute instances:\n";
os << " total: " << (summary.scalarGraphComputeInstances + summary.graphComputeBatchInstances) << "\n";
os << " scalar graph_compute instances: " << summary.scalarGraphComputeInstances << "\n";
os << " graph_compute_batch instances: " << summary.graphComputeBatchInstances << "\n";
os << "PEFT classes:\n";
os << " total: " << summary.peftClasses << "\n";
os << " single-cpu: " << summary.singleCpuClasses << "\n";
os << " multi-cpu: " << summary.multiCpuClasses << "\n";
os << "Scheduled ops:\n";
os << " total: " << (summary.scheduledCompute + summary.scheduledComputeBatch) << "\n";
os << " scheduled_compute: " << summary.scheduledCompute << "\n";
os << " scheduled_compute_batch: " << summary.scheduledComputeBatch << "\n";
os << "Deferred communications:\n";
os << " total: " << summary.deferredCommunication << "\n";
os << " multi-source payloads: " << summary.deferredCommunicationMultiSourcePayloads << "\n\n";
os << "PEFT Classes\n";
os << "============\n";
for (const auto &entry : peftClassPlans) {
const PeftClassPlan &peftClassPlan = entry.second;
os << "C" << peftClassPlan.canonicalClassId << " "
<< (peftClassPlan.cpus.size() == 1 ? "single-cpu" : "multi-cpu") << " PEFT class\n";
if (peftClassPlan.cpus.size() == 1) {
size_t cpu = peftClassPlan.cpus.front();
os << " cpu: " << cpu << "\n";
os << " steps: " << peftClassPlan.instancesByCpu.lookup(cpu).size() << "\n";
for (auto [stepIndex, instance] : llvm::enumerate(peftClassPlan.instancesByCpu.lookup(cpu)))
os << " step " << stepIndex << ": " << formatComputeInstanceForReport(instance, asmState) << "\n";
} else {
os << " scheduled lanes: " << peftClassPlan.cpus.size() << "\n";
os << " steps: " << peftClassPlan.instancesByCpu.lookup(peftClassPlan.cpus.front()).size() << "\n";
os << " cpus by scheduled lane:\n";
os << " ";
printIndexedList(os, ArrayRef<size_t>(peftClassPlan.cpus));
os << "\n";
os << " step sources:\n";
for (auto [stepIndex, stepTuple] : llvm::enumerate(buildComputeStepTuples(peftClassPlan)))
os << " step " << stepIndex << ": "
<< formatGraphComputeBlockKey(getGraphComputeBlockKey(stepTuple.instances.front()), asmState) << "\n";
}
os << "\n";
}
os << "Materialized Scheduled Ops\n";
os << "=========================\n";
for (const ScheduledMaterializationRecord &record : materializedSchedules) {
os << "C" << record.canonicalPeftClassId << " -> " << formatOperationLabel(record.scheduledOp, asmState) << " "
<< record.scheduledOp->getName().getStringRef() << "\n";
os << " kind: "
<< (isa<SpatScheduledCompute>(record.scheduledOp) ? "single-cpu scheduled_compute"
: "multi-cpu scheduled_compute_batch")
<< "\n";
if (isa<SpatScheduledCompute>(record.scheduledOp))
os << " cpu: " << record.cpus.front() << "\n";
else
os << " scheduled lanes: " << record.cpus.size() << "\n";
os << " results: " << record.scheduledOp->getNumResults() << "\n";
os << " steps: "
<< (isa<SpatScheduledCompute>(record.scheduledOp)
? peftClassPlans.lookup(record.canonicalPeftClassId).instancesByCpu.lookup(record.cpus.front()).size()
: record.stepPlans.size())
<< "\n";
if (isa<SpatScheduledComputeBatch>(record.scheduledOp)) {
os << " cpus by scheduled lane:\n";
os << " ";
printIndexedList(os, ArrayRef<size_t>(record.cpus));
os << "\n\n";
}
if (isa<SpatScheduledCompute>(record.scheduledOp)) {
const PeftClassPlan &peftClassPlan = peftClassPlans.lookup(record.canonicalPeftClassId);
size_t cpu = peftClassPlan.cpus.front();
size_t resultOffset = 0;
for (auto [stepIndex, instance] : llvm::enumerate(peftClassPlan.instancesByCpu.lookup(cpu))) {
size_t resultCount = getComputeInstanceResultValueCount(instance);
os << " step " << stepIndex << " " << formatStepResultRange(resultOffset, resultCount) << " "
<< formatComputeInstanceForReport(instance, asmState) << "\n";
resultOffset += resultCount;
}
} else {
auto scheduledBatch = cast<SpatScheduledComputeBatch>(record.scheduledOp);
for (auto [stepIndex, stepPlan] : llvm::enumerate(record.stepPlans)) {
const ComputeInstance &representative = stepPlan.stepTuple.instances.front();
SmallVector<uint32_t> sourceLaneStarts = collectSourceLaneStarts(stepPlan.stepTuple);
os << " step " << stepIndex << " " << formatStepResultRange(stepPlan.resultOffset, stepPlan.resultCount) << " "
<< formatGraphComputeBlockKey(getGraphComputeBlockKey(representative), asmState)
<< " lanesPerScheduledLane=" << representative.laneCount << " sourceLaneSelector="
<< (usesAffineSourceLaneMapping(stepPlan.stepTuple) ? "affine" : "table") << "\n";
os << " source lanes by scheduled lane:\n";
os << " ";
printIndexedList(os, ArrayRef<uint32_t>(sourceLaneStarts));
os << "\n";
Block &stepBlock = *std::next(scheduledBatch.getBody().begin(), stepIndex);
printMultiSourceDeferredInputs(os, stepBlock);
}
}
os << "\n";
}
}
} // namespace
void dumpScheduledComputeReportAndModule(ModuleOp moduleOp,
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules) {
OpPrintingFlags flags;
flags.elideLargeElementsAttrs().enableDebugInfo(false, false).assumeVerified();
ScheduledComputePrintContext printContext(moduleOp, flags);
dumpPeftMaterializationReport(moduleOp, funcOp, schedule, peftClassPlans, materializedSchedules, printContext);
std::fstream file = openDialectDumpFileWithExtension("spatial2_scheduled_no_comm", "/dialects", "mlir");
if (!file.is_open())
return;
llvm::raw_os_ostream os(file);
moduleOp.getOperation()->print(os, printContext.asmState);
os.flush();
}
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,21 @@
#pragma once
#include "ScheduledComputeMaterialization.hpp"
namespace onnx_mlir {
namespace spatial {
LogicalResult verifyPeftMaterializationReportSummary(
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules);
void dumpScheduledComputeReportAndModule(ModuleOp moduleOp,
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules);
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,307 @@
#include "ScheduledComputeVerification.hpp"
#include "DeferredProjectionAnalysis.hpp"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "src/Accelerators/PIM/Common/Support/Diagnostics.hpp"
namespace onnx_mlir {
namespace spatial {
using namespace mlir;
LogicalResult verifyMaterializedScheduleMapping(
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
const DenseMap<GraphComputeBlockKey, Block *> &graphComputeToBlockMap,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules) {
pim::CappedDiagnosticReporter diagnostics;
size_t expectedClassCount = countPeftEquivalenceClasses(schedule);
if (expectedClassCount != materializedSchedules.size()) {
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check expected " << expectedClassCount
<< " PEFT equivalence classes but materialized " << materializedSchedules.size()
<< " scheduled computes";
});
}
llvm::SmallDenseSet<size_t, 16> seenClasses;
for (const ScheduledMaterializationRecord &record : materializedSchedules) {
if (!seenClasses.insert(record.canonicalPeftClassId).second) {
diagnostics.report(record.scheduledOp, [&](Operation *illegalOp) {
illegalOp->emitOpError("phase-check multiple scheduled ops own the same PEFT equivalence class");
});
}
if (!peftClassPlans.count(record.canonicalPeftClassId)) {
diagnostics.report(record.scheduledOp, [&](Operation *illegalOp) {
illegalOp->emitOpError("phase-check scheduled op refers to a missing PEFT materialization class");
});
}
}
for (const auto &entry : peftClassPlans) {
if (!seenClasses.count(entry.first)) {
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check PEFT equivalence class " << entry.first
<< " was not materialized by any scheduled op";
});
}
}
for (GraphComputeBlockKey key : collectExpectedGraphComputeBlockKeys(funcOp)) {
if (graphComputeToBlockMap.count(key))
continue;
diagnostics.report(key.op, [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check graph compute is missing a scheduled MLIR block mapping for lanes ["
<< key.laneStart << ":" << (key.laneStart + key.laneCount) << "]";
});
}
for (const auto &entry : graphComputeToBlockMap) {
Block *block = entry.second;
if (!block || !isa<SpatScheduledCompute, SpatScheduledComputeBatch>(block->getParentOp())) {
diagnostics.report(entry.first.op, [&](Operation *illegalOp) {
illegalOp->emitOpError("phase-check graph compute block mapping does not target a scheduled compute block");
});
}
}
if (graphComputeToBlockMap.size() != collectExpectedGraphComputeBlockKeys(funcOp).size()) {
diagnostics.report(funcOp.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check expected "
<< collectExpectedGraphComputeBlockKeys(funcOp).size()
<< " graph compute block mappings but saw " << graphComputeToBlockMap.size();
});
}
diagnostics.emitSuppressedSummary(funcOp, "scheduled Spatial materialization verification failed");
return success(!diagnostics.hasFailure());
}
LogicalResult verifyDeferredTransferPhase1Invariants(func::FuncOp funcOp) {
pim::CappedDiagnosticReporter diagnostics;
GraphBatchPublicationCache publicationCache;
funcOp.walk([&](SpatDeferredCommunicationOp transfer) {
bool ownershipValid = true;
for (Value source : transfer.getSources()) {
auto result = dyn_cast<OpResult>(source);
if (!result || !isa<SpatGraphCompute, SpatGraphComputeBatch>(result.getOwner())) {
ownershipValid = false;
diagnostics.report(transfer.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError("phase-check deferred communication source operand must be an original graph SSA result");
});
}
}
if (!transfer->getParentOfType<SpatScheduledCompute>() &&
!transfer->getParentOfType<SpatScheduledComputeBatch>()) {
ownershipValid = false;
diagnostics.report(transfer.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError("phase-check deferred communication must be inside a scheduled compute");
});
}
if (!ownershipValid)
return;
if (failed(verifyDeferredProgramContract(transfer))) {
diagnostics.report(transfer.getOperation(), [&](Operation *) {});
return;
}
if (auto scheduled = transfer->getParentOfType<SpatScheduledComputeBatch>()) {
for (unsigned lane = 0; lane < static_cast<unsigned>(scheduled.getLaneCount()); ++lane) {
auto program = analyzeDeferredProgram(transfer, lane);
if (failed(program)) {
diagnostics.report(transfer.getOperation(), [&](Operation *) {});
continue;
}
for (const DeferredProjectionLeaf &leaf : program->leaves) {
if (leaf.kind == DeferredLeafKind::ScalarSource)
continue;
auto source = dyn_cast<OpResult>(transfer.getSources()[leaf.sourceOperandIndex]);
auto graph = source ? dyn_cast<SpatGraphComputeBatch>(source.getOwner()) : SpatGraphComputeBatch();
if (!graph) continue;
if (failed(getGraphBatchPublicationMap(graph, source.getResultNumber(), publicationCache)))
diagnostics.report(transfer.getOperation(), [&](Operation *) {});
}
}
} else if (failed(analyzeDeferredProgram(transfer, std::nullopt))) {
diagnostics.report(transfer.getOperation(), [&](Operation *) {});
}
});
diagnostics.emitSuppressedSummary(funcOp, "scheduled Spatial deferred communication verification failed");
return success(!diagnostics.hasFailure());
}
LogicalResult verifyMultiCpuStepResultRouting(SpatScheduledComputeBatch scheduled,
ArrayRef<ScheduledStepPlan> stepPlans) {
pim::CappedDiagnosticReporter diagnostics;
unsigned resultArgBase = getScheduledBatchResultArgBase(scheduled);
if (scheduled.getBody().getBlocks().size() != stepPlans.size()) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step routing expected " << stepPlans.size()
<< " blocks but saw " << scheduled.getBody().getBlocks().size();
});
diagnostics.emitSuppressedSummary(scheduled.getOperation(),
"scheduled batch step routing verification failed");
return failure();
}
SmallVector<unsigned> globalResultWrites(scheduled.getNumResults(), 0);
size_t stepIndex = 0;
for (Block &block : scheduled.getBody().getBlocks()) {
const ScheduledStepPlan &stepPlan = stepPlans[stepIndex++];
SmallVector<bool> localWrites(stepPlan.resultCount, false);
auto inParallel = dyn_cast<SpatInParallelOp>(block.getTerminator());
if (!inParallel) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step " << stepPlan.stepIndex
<< " is missing spat.in_parallel";
});
continue;
}
for (Operation &op : inParallel.getRegion().front()) {
auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(&op);
if (!insert)
continue;
auto dest = dyn_cast<BlockArgument>(insert.getDest());
if (!dest || dest.getOwner() != &block) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step " << stepPlan.stepIndex
<< " writes to a non-block result destination";
});
continue;
}
unsigned resultIndex = dest.getArgNumber() - resultArgBase;
if (dest.getArgNumber() < resultArgBase || resultIndex >= scheduled.getNumResults()) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step " << stepPlan.stepIndex
<< " writes to invalid result block argument " << dest.getArgNumber();
});
continue;
}
if (resultIndex < stepPlan.resultOffset
|| resultIndex >= stepPlan.resultOffset + stepPlan.resultCount) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step " << stepPlan.stepIndex
<< " expected result range [" << stepPlan.resultOffset << ":"
<< (stepPlan.resultOffset + stepPlan.resultCount)
<< ") but wrote result " << resultIndex;
});
continue;
}
localWrites[resultIndex - stepPlan.resultOffset] = true;
globalResultWrites[resultIndex]++;
}
for (size_t index = 0; index < localWrites.size(); ++index)
if (!localWrites[index])
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch step " << stepPlan.stepIndex
<< " did not write expected result " << (stepPlan.resultOffset + index);
});
}
for (size_t resultIndex = 0; resultIndex < globalResultWrites.size(); ++resultIndex)
if (globalResultWrites[resultIndex] != 1)
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "scheduled batch result " << resultIndex << " expected one producing step but saw "
<< globalResultWrites[resultIndex];
});
diagnostics.emitSuppressedSummary(scheduled.getOperation(), "scheduled batch step routing verification failed");
return success(!diagnostics.hasFailure());
}
LogicalResult verifyMultiCpuLocalFragmentOffsets(SpatScheduledComputeBatch scheduled) {
pim::CappedDiagnosticReporter diagnostics;
unsigned resultArgBase = getScheduledBatchResultArgBase(scheduled);
for (auto enumeratedBlock : llvm::enumerate(scheduled.getBody().getBlocks())) {
size_t stepIndex = enumeratedBlock.index();
Block &block = enumeratedBlock.value();
Value scheduledLane = block.getArgument(0);
auto inParallel = dyn_cast<SpatInParallelOp>(block.getTerminator());
if (!inParallel) {
diagnostics.report(scheduled.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError() << "phase-check scheduled batch step " << stepIndex
<< " is missing spat.in_parallel";
});
continue;
}
auto isFinalScheduledOutputInsert = [&](Operation *op) {
auto insert = dyn_cast<tensor::ParallelInsertSliceOp>(op);
if (!insert || op->getParentOp() != inParallel.getOperation())
return false;
auto dest = dyn_cast<BlockArgument>(insert.getDest());
return dest && dest.getOwner() == &block && dest.getArgNumber() >= resultArgBase;
};
block.walk([&](Operation *op) {
if (op == block.getTerminator())
return;
if (isFinalScheduledOutputInsert(op)) {
if (scheduled.getLaneCount() > 1) {
auto insert = cast<tensor::ParallelInsertSliceOp>(op);
bool dependsOnScheduledLane = false;
for (OpFoldResult offset : insert.getMixedOffsets()) {
if (auto value = dyn_cast<Value>(offset); value && valueTransitivelyDependsOn(value, scheduledLane)) {
dependsOnScheduledLane = true;
break;
}
}
if (!dependsOnScheduledLane)
diagnostics.report(insert.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError(
"phase-check scheduled batch final output insert must be indexed by scheduled lane");
});
}
return;
}
auto insertSlice = dyn_cast<tensor::InsertSliceOp>(op);
if (!insertSlice)
return;
auto dest = dyn_cast<BlockArgument>(insertSlice.getDest());
if (dest && dest.getOwner() == &block && dest.getArgNumber() >= resultArgBase)
return;
auto destType = dyn_cast<RankedTensorType>(insertSlice.getDestType());
if (!destType || !destType.hasStaticShape() || destType.getRank() == 0)
return;
for (OpFoldResult offset : insertSlice.getMixedOffsets()) {
auto value = dyn_cast<Value>(offset);
if (!value)
continue;
if (!valueTransitivelyDependsOn(value, scheduledLane))
continue;
diagnostics.report(insertSlice.getOperation(), [&](Operation *illegalOp) {
illegalOp->emitOpError()
<< "phase-check scheduled batch local fragment insert offset must use the source-instance inner lane, not the scheduled lane"
<< " step " << stepIndex;
});
break;
}
});
}
diagnostics.emitSuppressedSummary(scheduled.getOperation(),
"scheduled batch local fragment offset verification failed");
return success(!diagnostics.hasFailure());
}
LogicalResult verifyScheduledMaterializationRecords(ArrayRef<ScheduledMaterializationRecord> materializedSchedules) {
for (const ScheduledMaterializationRecord &record : materializedSchedules) {
auto scheduled = dyn_cast<SpatScheduledComputeBatch>(record.scheduledOp);
if (!scheduled)
continue;
if (failed(verifyMultiCpuStepResultRouting(scheduled, record.stepPlans)))
return failure();
if (failed(verifyMultiCpuLocalFragmentOffsets(scheduled)))
return failure();
}
return success();
}
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,22 @@
#pragma once
#include "ScheduledComputeMaterialization.hpp"
namespace onnx_mlir {
namespace spatial {
LogicalResult verifyMaterializedScheduleMapping(
func::FuncOp funcOp,
const MergeScheduleResult &schedule,
const llvm::MapVector<size_t, PeftClassPlan> &peftClassPlans,
const DenseMap<GraphComputeBlockKey, Block *> &graphComputeToBlockMap,
ArrayRef<ScheduledMaterializationRecord> materializedSchedules);
LogicalResult verifyDeferredTransferPhase1Invariants(func::FuncOp funcOp);
LogicalResult verifyMultiCpuStepResultRouting(SpatScheduledComputeBatch scheduled,
ArrayRef<ScheduledStepPlan> stepPlans);
LogicalResult verifyMultiCpuLocalFragmentOffsets(SpatScheduledComputeBatch scheduled);
LogicalResult verifyScheduledMaterializationRecords(ArrayRef<ScheduledMaterializationRecord> materializedSchedules);
} // namespace spatial
} // namespace onnx_mlir
@@ -106,10 +106,11 @@ static std::optional<uint32_t> getConstantExtractLane(tensor::ExtractSliceOp ext
static std::optional<ProducerValueRef> getResultfulBatchProducerValueRef(SpatComputeBatch batch, static std::optional<ProducerValueRef> getResultfulBatchProducerValueRef(SpatComputeBatch batch,
const ComputeInstance* consumerInstance) { const ComputeInstance* consumerInstance) {
if (!consumerInstance) if (!consumerInstance || !isa<SpatComputeBatch>(consumerInstance->op))
return std::nullopt; return ProducerValueRef {
if (!isa<SpatComputeBatch>(consumerInstance->op)) {batch.getOperation(), 0, static_cast<uint32_t>(batch.getLaneCount())},
return std::nullopt; 0
};
if (consumerInstance->laneStart + consumerInstance->laneCount > static_cast<uint32_t>(batch.getLaneCount())) if (consumerInstance->laneStart + consumerInstance->laneCount > static_cast<uint32_t>(batch.getLaneCount()))
return std::nullopt; return std::nullopt;
return ProducerValueRef { return ProducerValueRef {
@@ -0,0 +1,898 @@
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Value.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>
#include <fstream>
#include <optional>
#include <string>
#include <utility>
#include "SpatialDataflowCsvExporter.hpp"
#include "src/Accelerators/PIM/Common/IR/AffineUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/BatchCoreUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ConstantUtils.hpp"
#include "src/Accelerators/PIM/Common/IR/ShapeUtils.hpp"
#include "src/Accelerators/PIM/Common/Support/DebugDump.hpp"
#include "src/Accelerators/PIM/Compiler/PimCompilerOptions.hpp"
#include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp"
using namespace mlir;
namespace onnx_mlir {
namespace spatial {
namespace {
struct TopLevelOpInfo {
Operation* op = nullptr;
size_t opId = 0;
bool isScheduled = false;
std::optional<int32_t> scalarCore;
};
struct ExpandedNodeInfo {
std::string id;
std::optional<int32_t> core;
std::optional<uint32_t> lane;
};
struct ChannelSendRecord {
std::string sourceId;
std::optional<uint32_t> sourceLane;
};
enum class LogicalNodeSelector {
Scalar,
Lane,
RangeRepresentative,
};
struct ResolvedProducer {
Operation* op = nullptr;
size_t resultIndex = 0;
LogicalNodeSelector selector = LogicalNodeSelector::Scalar;
uint32_t lane = 0;
uint32_t laneStart = 0;
uint32_t laneCount = 1;
};
struct EdgeSource {
std::string id;
std::optional<uint32_t> sourceLane;
};
using ScheduledNodeByGraphLane = DenseMap<std::pair<int64_t, uint32_t>, ExpandedNodeInfo>;
void emitEdgeRow(std::fstream& edgesFile,
StringRef sourceId,
StringRef targetId,
std::optional<uint64_t> byteSize,
Type propagatedType,
StringRef stage,
std::optional<uint32_t> sourceLane,
std::optional<uint32_t> targetLane,
std::optional<int64_t> channelId);
std::string csvEscape(StringRef field) {
bool needsQuotes = field.contains(',') || field.contains('"') || field.contains('\n') || field.contains('\r');
if (!needsQuotes)
return field.str();
std::string escaped;
escaped.reserve(field.size() + 2);
escaped.push_back('"');
for (char ch : field)
if (ch == '"')
escaped += "\"\"";
else
escaped.push_back(ch);
escaped.push_back('"');
return escaped;
}
void writeCsvRow(std::fstream& file, ArrayRef<std::string> fields) {
for (size_t i = 0; i < fields.size(); ++i) {
if (i != 0)
file << ",";
file << csvEscape(fields[i]);
}
file << "\n";
}
template <typename NumberT>
std::string maybeNumber(std::optional<NumberT> value) {
if (!value)
return "";
return std::to_string(*value);
}
std::string stringifyType(Type type) {
std::string storage;
llvm::raw_string_ostream os(storage);
type.print(os);
return os.str();
}
std::string stringifyValueAsOperand(Value value, AsmState& asmState) {
std::string storage;
llvm::raw_string_ostream os(storage);
value.printAsOperand(os, asmState);
return os.str();
}
std::string stringifyResultSsaNames(Operation* op, AsmState* asmState) {
if (!asmState || op->getNumResults() == 0)
return "";
std::string storage;
llvm::raw_string_ostream os(storage);
llvm::interleave(
op->getResults(), [&](Value result) { os << stringifyValueAsOperand(result, *asmState); }, [&]() { os << ";"; });
return os.str();
}
std::optional<uint64_t> getTypeSizeBytes(Type type) {
if (auto shapedType = dyn_cast<ShapedType>(type)) {
if (!shapedType.hasStaticShape() || !hasByteSizedElementType(shapedType.getElementType()))
return std::nullopt;
return static_cast<uint64_t>(getShapedTypeSizeInBytes(shapedType));
}
if (isa<IndexType>(type))
return static_cast<uint64_t>(getElementTypeSizeInBytes(type));
if (auto intType = dyn_cast<IntegerType>(type)) {
if (intType.getWidth() <= 0 || intType.getWidth() % 8 != 0)
return std::nullopt;
return static_cast<uint64_t>(getElementTypeSizeInBytes(type));
}
if (auto floatType = dyn_cast<FloatType>(type)) {
if (floatType.getWidth() <= 0 || floatType.getWidth() % 8 != 0)
return std::nullopt;
return static_cast<uint64_t>(getElementTypeSizeInBytes(type));
}
return std::nullopt;
}
std::string getScalarId(bool isScheduled, size_t opId) { return (isScheduled ? "sc:" : "gc:") + std::to_string(opId); }
std::string getBatchLaneId(bool isScheduled, size_t opId, uint32_t lane) {
return (isScheduled ? "scb:" : "gcb:") + std::to_string(opId) + ":" + std::to_string(lane);
}
template <typename ComputeOpTy, typename BatchOpTy>
bool isTopLevelRelevantCompute(Operation& op) {
return isa<ComputeOpTy, BatchOpTy>(&op);
}
template <typename ComputeOpTy, typename BatchOpTy>
FailureOr<TopLevelOpInfo> buildTopLevelOpInfo(Operation& op, bool isScheduled, size_t opId) {
TopLevelOpInfo info;
info.op = &op;
info.opId = opId;
info.isScheduled = isScheduled;
if constexpr (std::is_same_v<ComputeOpTy, SpatScheduledCompute>) {
if (auto compute = dyn_cast<ComputeOpTy>(&op)) {
auto coreId = getOptionalScheduledCoreId(compute, "spatial dataflow export core id");
if (failed(coreId))
return failure();
if (*coreId)
info.scalarCore = **coreId;
}
}
return info;
}
template <typename BatchOpTy>
FailureOr<SmallVector<int32_t, 8>> getBatchLaneCoreIds(BatchOpTy batch) {
if constexpr (std::is_same_v<BatchOpTy, SpatScheduledComputeBatch>) {
auto coreIds = getOptionalScheduledBatchCoreIds(batch, "spatial dataflow export core ids");
if (failed(coreIds))
return failure();
if (!*coreIds)
return SmallVector<int32_t, 8> {};
return SmallVector<int32_t, 8>((**coreIds).begin(), (**coreIds).end());
}
return SmallVector<int32_t, 8> {};
}
std::string getExpandedNodeId(const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
Operation* op,
uint32_t lane) {
auto it = expandedNodes.find({op, lane});
if (it == expandedNodes.end())
return "";
return it->second.id;
}
void addScalarNodeRow(std::fstream& nodesFile,
DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
const TopLevelOpInfo& info,
AsmState* asmState = nullptr) {
std::string id = getScalarId(info.isScheduled, info.opId);
SmallVector<std::string, 5> row {id, std::to_string(info.opId), "", maybeNumber<int32_t>(info.scalarCore)};
if (asmState)
row.push_back(stringifyResultSsaNames(info.op, asmState));
writeCsvRow(nodesFile, row);
expandedNodes[{info.op, 0}] = {id, info.scalarCore, std::nullopt};
}
template <typename BatchOpTy>
void addBatchNodeRows(std::fstream& nodesFile,
DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
const TopLevelOpInfo& info,
BatchOpTy batch,
ArrayRef<std::optional<int32_t>> laneCoreIds,
AsmState* asmState = nullptr) {
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane) {
std::string id = getBatchLaneId(info.isScheduled, info.opId, lane);
SmallVector<std::string, 5> row {
id, std::to_string(info.opId), std::to_string(lane), maybeNumber<int32_t>(laneCoreIds[lane])};
if (asmState)
row.push_back(stringifyResultSsaNames(info.op, asmState));
writeCsvRow(nodesFile, row);
expandedNodes[{info.op, lane}] = {id, laneCoreIds[lane], lane};
}
}
std::optional<int64_t> evaluateIndexLike(Value value, Value laneArg, uint32_t lane);
std::optional<int64_t> evaluateIndexLike(Value value, Value laneArg, uint32_t lane) {
if (value == laneArg)
return static_cast<int64_t>(lane);
if (std::optional<int64_t> constant = matchConstantIndexValue(value))
return *constant;
if (auto constant = value.getDefiningOp<arith::ConstantOp>()) {
if (auto intAttr = dyn_cast<IntegerAttr>(constant.getValue()))
return intAttr.getInt();
}
if (auto extract = value.getDefiningOp<tensor::ExtractOp>()) {
auto constant = extract.getTensor().getDefiningOp<arith::ConstantOp>();
auto elements = constant ? dyn_cast<ElementsAttr>(constant.getValue()) : nullptr;
auto shapedType = elements ? dyn_cast<ShapedType>(elements.getType()) : nullptr;
if (!elements || !shapedType || shapedType.getRank() != 1 || extract.getIndices().size() != 1)
return std::nullopt;
std::optional<int64_t> index = evaluateIndexLike(extract.getIndices().front(), laneArg, lane);
if (!index || *index < 0 || *index >= static_cast<int64_t>(elements.getNumElements()))
return std::nullopt;
if (auto denseInts = dyn_cast<DenseIntElementsAttr>(elements))
return (*(denseInts.value_begin<APInt>() + *index)).getSExtValue();
return std::nullopt;
}
if (auto affineApply = value.getDefiningOp<affine::AffineApplyOp>())
if (FailureOr<int64_t> folded = evaluateAffineApply(affineApply,
[&](Value operand) -> FailureOr<int64_t> {
if (std::optional<int64_t> resolved =
evaluateIndexLike(operand, laneArg, lane))
return *resolved;
return failure();
});
succeeded(folded)) {
return *folded;
}
return std::nullopt;
}
SmallVector<int64_t, 8> collectPossibleIntValues(Value value, Value laneArg, uint32_t lane) {
if (std::optional<int64_t> exact = evaluateIndexLike(value, laneArg, lane))
return {*exact};
auto extract = value.getDefiningOp<tensor::ExtractOp>();
auto constant = extract ? extract.getTensor().getDefiningOp<arith::ConstantOp>() : nullptr;
auto elements = constant ? dyn_cast<ElementsAttr>(constant.getValue()) : nullptr;
if (!elements)
return {};
SmallVector<int64_t, 8> values;
if (auto denseInts = dyn_cast<DenseIntElementsAttr>(elements)) {
values.reserve(elements.getNumElements());
for (APInt element : denseInts.getValues<APInt>())
if (!llvm::is_contained(values, element.getSExtValue()))
values.push_back(element.getSExtValue());
}
return values;
}
template <typename BatchOpTy>
std::optional<Value> getBatchLaneInput(BatchOpTy batch, uint32_t lane, unsigned inputIndex) {
if (batch.getNumResults() != 0)
return batch.getInputs()[inputIndex];
size_t laneCount = static_cast<size_t>(batch.getLaneCount());
if (laneCount == 0 || batch.getInputs().size() % laneCount != 0)
return std::nullopt;
size_t inputsPerLane = batch.getInputs().size() / laneCount;
size_t flatIndex = static_cast<size_t>(lane) * inputsPerLane + inputIndex;
if (flatIndex >= batch.getInputs().size())
return std::nullopt;
return batch.getInputs()[flatIndex];
}
template <typename BatchOpTy>
unsigned getBatchLaneInputCount(BatchOpTy batch) {
if (batch.getNumResults() != 0)
return batch.getInputs().size();
size_t laneCount = static_cast<size_t>(batch.getLaneCount());
if (laneCount == 0 || batch.getInputs().size() % laneCount != 0)
return 0;
return static_cast<unsigned>(batch.getInputs().size() / laneCount);
}
template <typename ComputeOpTy, typename BatchOpTy>
std::optional<ResolvedProducer> resolveProducerForValue(Value value, std::optional<uint32_t> consumerLane) {
Operation* op = value.getDefiningOp();
if (!op)
return std::nullopt;
while (auto extract = dyn_cast<tensor::ExtractSliceOp>(op)) {
Value source = extract.getSource();
Operation* sourceOp = source.getDefiningOp();
auto sourceBatch = dyn_cast_or_null<BatchOpTy>(sourceOp);
if (sourceBatch && sourceBatch.getNumResults() != 0) {
auto staticOffsets = extract.getStaticOffsets();
if (!staticOffsets.empty() && staticOffsets.front() != ShapedType::kDynamic) {
uint32_t lane = static_cast<uint32_t>(staticOffsets.front());
return ResolvedProducer {sourceOp, 0, LogicalNodeSelector::Lane, lane, lane, 1};
}
if (consumerLane)
return ResolvedProducer {sourceOp, 0, LogicalNodeSelector::Lane, *consumerLane, *consumerLane, 1};
return ResolvedProducer {
sourceOp, 0, LogicalNodeSelector::RangeRepresentative, 0, 0, static_cast<uint32_t>(sourceBatch.getLaneCount())};
}
value = source;
op = sourceOp;
if (!op)
return std::nullopt;
}
if (auto compute = dyn_cast<ComputeOpTy>(op))
return ResolvedProducer {compute.getOperation(),
static_cast<size_t>(cast<OpResult>(value).getResultNumber()),
LogicalNodeSelector::Scalar,
0,
0,
1};
if (auto batch = dyn_cast<BatchOpTy>(op)) {
if (batch.getNumResults() != 0) {
if (consumerLane)
return ResolvedProducer {op, 0, LogicalNodeSelector::Lane, *consumerLane, *consumerLane, 1};
return ResolvedProducer {
op, 0, LogicalNodeSelector::RangeRepresentative, 0, 0, static_cast<uint32_t>(batch.getLaneCount())};
}
uint32_t lane = static_cast<uint32_t>(cast<OpResult>(value).getResultNumber());
return ResolvedProducer {op, static_cast<size_t>(lane), LogicalNodeSelector::Lane, lane, lane, 1};
}
return std::nullopt;
}
SmallVector<EdgeSource, 8>
resolveProducerSourcesForCsv(const ResolvedProducer& producer,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes) {
SmallVector<EdgeSource, 8> sources;
if (producer.selector == LogicalNodeSelector::Scalar) {
std::string id = getExpandedNodeId(expandedNodes, producer.op, 0);
if (!id.empty())
sources.push_back({id, std::nullopt});
return sources;
}
if (producer.selector == LogicalNodeSelector::Lane) {
std::string id = getExpandedNodeId(expandedNodes, producer.op, producer.lane);
if (!id.empty())
sources.push_back({id, producer.lane});
return sources;
}
for (uint32_t lane = producer.laneStart; lane < producer.laneStart + producer.laneCount; ++lane) {
std::string id = getExpandedNodeId(expandedNodes, producer.op, lane);
if (!id.empty())
sources.push_back({id, lane});
}
return sources;
}
FailureOr<SmallVector<int64_t>> getIntegerValues(Operation* op, StringRef name) {
Attribute attr = op->getAttr(name);
if (auto array = dyn_cast_or_null<DenseI64ArrayAttr>(attr))
return SmallVector<int64_t>(array.asArrayRef());
if (auto elements = dyn_cast_or_null<DenseIntElementsAttr>(attr))
return SmallVector<int64_t>(elements.getValues<int64_t>());
return op->emitOpError() << "expected " << name << " integer array for Spatial dataflow report";
}
FailureOr<ScheduledNodeByGraphLane>
buildScheduledNodesByGraphLane(const DenseMap<Operation*, TopLevelOpInfo>& topLevelInfo,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
const DenseMap<int64_t, Operation*>& graphOpsById) {
ScheduledNodeByGraphLane nodesByGraphLane;
for (const auto& entry : topLevelInfo) {
Operation* scheduledOp = entry.first;
auto sourceIds = getIntegerValues(scheduledOp, "scheduled.step_source_ids");
auto sourceStarts = getIntegerValues(scheduledOp, "scheduled.source_lane_starts");
auto sourceCounts = getIntegerValues(scheduledOp, "scheduled.source_lane_counts");
if (failed(sourceIds) || failed(sourceStarts) || failed(sourceCounts))
return failure();
uint32_t scheduledLaneCount = 1;
if (auto batch = dyn_cast<SpatScheduledComputeBatch>(scheduledOp))
scheduledLaneCount = static_cast<uint32_t>(batch.getLaneCount());
size_t expectedEntries = sourceIds->size() * scheduledLaneCount;
if (sourceStarts->size() != expectedEntries || sourceCounts->size() != expectedEntries)
return scheduledOp->emitOpError("inconsistent scheduling provenance arrays for Spatial dataflow report");
for (auto [step, graphId] : llvm::enumerate(*sourceIds)) {
auto graphIt = graphOpsById.find(graphId);
if (graphIt == graphOpsById.end())
return scheduledOp->emitOpError() << "references unknown scheduled graph id " << graphId;
bool graphIsBatch = isa<SpatGraphComputeBatch>(graphIt->second);
for (uint32_t scheduledLane = 0; scheduledLane < scheduledLaneCount; ++scheduledLane) {
auto nodeIt = expandedNodes.find({scheduledOp, scheduledLane});
if (nodeIt == expandedNodes.end())
continue;
size_t index = step * scheduledLaneCount + scheduledLane;
int64_t start = graphIsBatch ? (*sourceStarts)[index] : 0;
int64_t count = graphIsBatch ? (*sourceCounts)[index] : 1;
if (start < 0 || count < 0)
return scheduledOp->emitOpError("negative scheduling provenance range for Spatial dataflow report");
for (int64_t lane = start; lane < start + count; ++lane)
nodesByGraphLane[{graphId, static_cast<uint32_t>(lane)}] = nodeIt->second;
}
}
}
return nodesByGraphLane;
}
SmallVector<ExpandedNodeInfo, 8> resolveScheduledProducerNodes(const ResolvedProducer& producer,
const ScheduledNodeByGraphLane& nodesByGraphLane) {
SmallVector<ExpandedNodeInfo, 8> nodes;
auto graphId = producer.op->getAttrOfType<IntegerAttr>("scheduled.graph_id");
if (!graphId)
return nodes;
uint32_t laneStart = producer.selector == LogicalNodeSelector::Scalar ? 0 : producer.laneStart;
uint32_t laneCount = producer.selector == LogicalNodeSelector::RangeRepresentative ? producer.laneCount : 1;
for (uint32_t lane = laneStart; lane < laneStart + laneCount; ++lane)
if (auto it = nodesByGraphLane.find({graphId.getInt(), lane}); it != nodesByGraphLane.end())
nodes.push_back(it->second);
return nodes;
}
LogicalResult
emitScheduledPlanningEdges(std::fstream& edgesFile,
func::FuncOp func,
const DenseMap<Operation*, TopLevelOpInfo>& topLevelInfo,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
StringRef stage) {
DenseMap<int64_t, Operation*> graphOpsById;
for (Operation& op : func.getBody().front())
if (auto graphId = op.getAttrOfType<IntegerAttr>("scheduled.graph_id"))
graphOpsById[graphId.getInt()] = &op;
auto nodesByGraphLane = buildScheduledNodesByGraphLane(topLevelInfo, expandedNodes, graphOpsById);
if (failed(nodesByGraphLane))
return failure();
auto emitMappedEdge =
[&](const ResolvedProducer& producer, int64_t targetGraphId, uint32_t targetGraphLane, Type type) {
auto targetIt = nodesByGraphLane->find({targetGraphId, targetGraphLane});
if (targetIt == nodesByGraphLane->end())
return;
for (const ExpandedNodeInfo& source : resolveScheduledProducerNodes(producer, *nodesByGraphLane)) {
if (source.id == targetIt->second.id)
continue;
emitEdgeRow(edgesFile,
source.id,
targetIt->second.id,
getTypeSizeBytes(type),
type,
stage,
source.lane,
targetIt->second.lane,
std::nullopt);
}
};
for (Operation& op : func.getBody().front()) {
auto graphId = op.getAttrOfType<IntegerAttr>("scheduled.graph_id");
if (!graphId)
continue;
if (auto compute = dyn_cast<SpatGraphCompute>(&op)) {
for (Value input : compute.getInputs())
if (auto producer = resolveProducerForValue<SpatGraphCompute, SpatGraphComputeBatch>(input, std::nullopt))
emitMappedEdge(*producer, graphId.getInt(), 0, input.getType());
continue;
}
auto batch = dyn_cast<SpatGraphComputeBatch>(&op);
if (!batch)
continue;
unsigned inputCount = getBatchLaneInputCount(batch);
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane)
for (unsigned inputIndex = 0; inputIndex < inputCount; ++inputIndex)
if (std::optional<Value> input = getBatchLaneInput(batch, lane, inputIndex))
if (auto producer = resolveProducerForValue<SpatGraphCompute, SpatGraphComputeBatch>(*input, lane))
emitMappedEdge(*producer, graphId.getInt(), lane, input->getType());
}
return success();
}
void emitEdgeRow(std::fstream& edgesFile,
StringRef sourceId,
StringRef targetId,
std::optional<uint64_t> byteSize,
Type propagatedType,
StringRef stage,
std::optional<uint32_t> sourceLane,
std::optional<uint32_t> targetLane,
std::optional<int64_t> channelId) {
writeCsvRow(edgesFile,
{sourceId.str(),
targetId.str(),
maybeNumber<uint64_t>(byteSize),
stringifyType(propagatedType),
stage.str(),
maybeNumber<uint32_t>(sourceLane),
maybeNumber<uint32_t>(targetLane),
maybeNumber<int64_t>(channelId)});
}
template <typename ComputeOpTy, typename BatchOpTy>
LogicalResult emitDataEdges(std::fstream& edgesFile,
const DenseMap<Operation*, TopLevelOpInfo>& topLevelInfo,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
StringRef stage) {
for (const auto& entry : topLevelInfo) {
Operation* op = entry.first;
const TopLevelOpInfo& info = entry.second;
if (auto compute = dyn_cast<ComputeOpTy>(op)) {
for (Value input : compute.getInputs()) {
if (isa_and_nonnull<SpatChannelReceiveOp>(input.getDefiningOp()))
continue;
auto producer = resolveProducerForValue<ComputeOpTy, BatchOpTy>(input, std::nullopt);
if (!producer)
continue;
SmallVector<EdgeSource, 8> sources = resolveProducerSourcesForCsv(*producer, expandedNodes);
std::optional<uint64_t> byteSize = getTypeSizeBytes(input.getType());
std::string targetId = getScalarId(info.isScheduled, info.opId);
for (const EdgeSource& source : sources)
emitEdgeRow(edgesFile,
source.id,
targetId,
byteSize,
input.getType(),
stage,
source.sourceLane,
std::nullopt,
std::nullopt);
}
continue;
}
auto batch = dyn_cast<BatchOpTy>(op);
if (!batch)
continue;
unsigned inputCount = getBatchLaneInputCount(batch);
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane) {
std::string targetId = getBatchLaneId(info.isScheduled, info.opId, lane);
for (unsigned inputIndex = 0; inputIndex < inputCount; ++inputIndex) {
std::optional<Value> input = getBatchLaneInput(batch, lane, inputIndex);
if (!input || isa_and_nonnull<SpatChannelReceiveOp>((*input).getDefiningOp()))
continue;
auto producer = resolveProducerForValue<ComputeOpTy, BatchOpTy>(*input, lane);
if (!producer)
continue;
SmallVector<EdgeSource, 8> sources = resolveProducerSourcesForCsv(*producer, expandedNodes);
std::optional<uint64_t> byteSize = getTypeSizeBytes((*input).getType());
for (const EdgeSource& source : sources)
emitEdgeRow(
edgesFile, source.id, targetId, byteSize, (*input).getType(), stage, source.sourceLane, lane, std::nullopt);
}
}
}
return success();
}
template <typename BatchOpTy>
void collectChannelSends(DenseMap<int64_t, SmallVector<ChannelSendRecord, 4>>& sendsByChannelId,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
BatchOpTy batch) {
std::optional<BlockArgument> laneArg = batch.getLaneArgument();
if (!laneArg)
return;
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane) {
std::string sourceId = getExpandedNodeId(expandedNodes, batch.getOperation(), lane);
if (sourceId.empty())
continue;
batch.getBody().walk([&](SpatChannelSendOp send) {
std::optional<int64_t> channelId = evaluateIndexLike(send.getChannelId(), *laneArg, lane);
if (!channelId)
return;
sendsByChannelId[*channelId].push_back({sourceId, lane});
});
}
}
void collectChannelSends(DenseMap<int64_t, SmallVector<ChannelSendRecord, 4>>& sendsByChannelId,
const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes,
SpatScheduledCompute compute) {
std::string sourceId = getExpandedNodeId(expandedNodes, compute.getOperation(), 0);
if (sourceId.empty())
return;
compute.getBody().walk([&](SpatChannelSendOp send) {
std::optional<int64_t> channelId = evaluateIndexLike(send.getChannelId(), Value(), 0);
if (!channelId)
return;
sendsByChannelId[*channelId].push_back({sourceId, std::nullopt});
});
}
DenseMap<int32_t, SmallVector<ChannelSendRecord, 4>>
buildNodesByCore(const DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo>& expandedNodes) {
DenseMap<int32_t, SmallVector<ChannelSendRecord, 4>> nodesByCore;
for (const auto& entry : expandedNodes) {
const ExpandedNodeInfo& node = entry.second;
if (!node.core)
continue;
nodesByCore[*node.core].push_back({node.id, node.lane});
}
return nodesByCore;
}
template <typename ComputeOpTy, typename BatchOpTy, typename ResolveChannelSourcesFn>
LogicalResult emitExplicitChannelEdges(std::fstream& edgesFile,
const DenseMap<Operation*, TopLevelOpInfo>& topLevelInfo,
ResolveChannelSourcesFn&& resolveChannelSources,
StringRef stage) {
for (const auto& entry : topLevelInfo) {
Operation* op = entry.first;
const TopLevelOpInfo& info = entry.second;
if (auto compute = dyn_cast<ComputeOpTy>(op)) {
compute.getBody().walk([&](SpatChannelReceiveOp receive) {
SmallVector<ChannelSendRecord, 4> sources = resolveChannelSources(receive, 0);
if (sources.empty())
return;
std::optional<int64_t> channelId = evaluateIndexLike(receive.getChannelId(), Value(), 0);
std::string targetId = getScalarId(info.isScheduled, info.opId);
std::optional<uint64_t> byteSize = getTypeSizeBytes(receive.getType());
for (const ChannelSendRecord& source : sources)
emitEdgeRow(edgesFile,
source.sourceId,
targetId,
byteSize,
receive.getType(),
stage,
source.sourceLane,
std::nullopt,
channelId);
});
continue;
}
auto batch = dyn_cast<BatchOpTy>(op);
if (!batch)
continue;
auto laneArg = batch.getLaneArgument();
if (!laneArg)
continue;
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane) {
std::string targetId = getBatchLaneId(info.isScheduled, info.opId, lane);
batch.getBody().walk([&](SpatChannelReceiveOp receive) {
SmallVector<ChannelSendRecord, 4> sources = resolveChannelSources(receive, lane);
if (sources.empty())
return;
std::optional<int64_t> channelId = evaluateIndexLike(receive.getChannelId(), *laneArg, lane);
std::optional<uint64_t> byteSize = getTypeSizeBytes(receive.getType());
for (const ChannelSendRecord& source : sources)
emitEdgeRow(edgesFile,
source.sourceId,
targetId,
byteSize,
receive.getType(),
stage,
source.sourceLane,
lane,
channelId);
});
}
}
return success();
}
LogicalResult exportGraph(func::FuncOp func, StringRef reportName) {
std::fstream nodesFile = openDialectDumpFileWithExtension((reportName + ".nodes").str(), "/reports", "csv");
std::fstream edgesFile = openDialectDumpFileWithExtension((reportName + ".edges").str(), "/reports", "csv");
if (!nodesFile.is_open() || !edgesFile.is_open())
return success();
writeCsvRow(nodesFile, {"Id", "op_id", "lane", "core", "ssa_name"});
writeCsvRow(edgesFile, {"Source", "Target", "Weight", "Type", "stage", "source_lane", "target_lane", "channel_id"});
Operation* asmRoot = func.getOperation();
if (auto moduleOp = func->getParentOfType<ModuleOp>())
asmRoot = moduleOp.getOperation();
OpPrintingFlags flags;
flags.elideLargeElementsAttrs().enableDebugInfo(true, false);
AsmState asmState(asmRoot, flags);
DenseMap<Operation*, TopLevelOpInfo> topLevelInfo;
DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo> expandedNodes;
size_t opId = 0;
for (Operation& op : func.getBody().front()) {
if (!isTopLevelRelevantCompute<SpatGraphCompute, SpatGraphComputeBatch>(op))
continue;
FailureOr<TopLevelOpInfo> info = buildTopLevelOpInfo<SpatGraphCompute, SpatGraphComputeBatch>(op, false, opId++);
if (failed(info))
return failure();
topLevelInfo[&op] = *info;
if (auto compute = dyn_cast<SpatGraphCompute>(&op)) {
addScalarNodeRow(nodesFile, expandedNodes, *info, &asmState);
continue;
}
auto batch = cast<SpatGraphComputeBatch>(&op);
SmallVector<std::optional<int32_t>, 8> laneCoreIds(batch.getLaneCount());
addBatchNodeRows(nodesFile, expandedNodes, *info, batch, laneCoreIds, &asmState);
}
return emitDataEdges<SpatGraphCompute, SpatGraphComputeBatch>(edgesFile, topLevelInfo, expandedNodes, "spatial1");
}
LogicalResult exportScheduled(func::FuncOp func, StringRef reportName, StringRef stage) {
std::fstream nodesFile = openDialectDumpFileWithExtension((reportName + ".nodes").str(), "/reports", "csv");
std::fstream edgesFile = openDialectDumpFileWithExtension((reportName + ".edges").str(), "/reports", "csv");
if (!nodesFile.is_open() || !edgesFile.is_open())
return success();
writeCsvRow(nodesFile, {"Id", "op_id", "lane", "core", "ssa_name"});
writeCsvRow(edgesFile, {"Source", "Target", "Weight", "Type", "stage", "source_lane", "target_lane", "channel_id"});
Operation* asmRoot = func.getOperation();
if (auto moduleOp = func->getParentOfType<ModuleOp>())
asmRoot = moduleOp.getOperation();
OpPrintingFlags flags;
flags.elideLargeElementsAttrs().enableDebugInfo(true, false);
AsmState asmState(asmRoot, flags);
DenseMap<Operation*, TopLevelOpInfo> topLevelInfo;
DenseMap<std::pair<Operation*, uint32_t>, ExpandedNodeInfo> expandedNodes;
size_t opId = 0;
for (Operation& op : func.getBody().front()) {
if (!isTopLevelRelevantCompute<SpatScheduledCompute, SpatScheduledComputeBatch>(op))
continue;
FailureOr<TopLevelOpInfo> info =
buildTopLevelOpInfo<SpatScheduledCompute, SpatScheduledComputeBatch>(op, true, opId++);
if (failed(info))
return failure();
topLevelInfo[&op] = *info;
if (isa<SpatScheduledCompute>(&op)) {
addScalarNodeRow(nodesFile, expandedNodes, *info, &asmState);
continue;
}
auto batch = cast<SpatScheduledComputeBatch>(&op);
auto coreIds = getBatchLaneCoreIds(batch);
if (failed(coreIds))
return failure();
SmallVector<std::optional<int32_t>, 8> laneCoreIds(batch.getLaneCount());
for (uint32_t lane = 0; lane < static_cast<uint32_t>(batch.getLaneCount()); ++lane)
if (lane < coreIds->size())
laneCoreIds[lane] = (*coreIds)[lane];
addBatchNodeRows(nodesFile, expandedNodes, *info, batch, laneCoreIds, &asmState);
}
if (stage == "spatial2")
return emitScheduledPlanningEdges(edgesFile, func, topLevelInfo, expandedNodes, stage);
if (failed(
emitDataEdges<SpatScheduledCompute, SpatScheduledComputeBatch>(edgesFile, topLevelInfo, expandedNodes, stage)))
return failure();
DenseMap<int64_t, SmallVector<ChannelSendRecord, 4>> sendsByChannelId;
for (const auto& entry : topLevelInfo) {
Operation* op = entry.first;
if (auto compute = dyn_cast<SpatScheduledCompute>(op))
collectChannelSends(sendsByChannelId, expandedNodes, compute);
else if (auto batch = dyn_cast<SpatScheduledComputeBatch>(op))
collectChannelSends(sendsByChannelId, expandedNodes, batch);
}
DenseMap<int32_t, SmallVector<ChannelSendRecord, 4>> nodesByCore = buildNodesByCore(expandedNodes);
auto resolveChannelSources = [&](SpatChannelReceiveOp receive, uint32_t lane) {
SmallVector<ChannelSendRecord, 4> sources;
Value laneArg;
if (auto owner = receive->getParentOfType<SpatScheduledComputeBatch>())
if (auto maybeLaneArg = owner.getLaneArgument())
laneArg = *maybeLaneArg;
if (std::optional<int64_t> channelId = evaluateIndexLike(receive.getChannelId(), laneArg, lane)) {
if (auto it = sendsByChannelId.find(*channelId); it != sendsByChannelId.end())
return it->second;
}
for (int64_t sourceCore : collectPossibleIntValues(receive.getSourceCoreId(), laneArg, lane)) {
auto it = nodesByCore.find(static_cast<int32_t>(sourceCore));
if (it == nodesByCore.end())
continue;
llvm::append_range(sources, it->second);
}
return sources;
};
return emitExplicitChannelEdges<SpatScheduledCompute, SpatScheduledComputeBatch>(
edgesFile, topLevelInfo, resolveChannelSources, stage);
}
} // namespace
SpatialDataflowExportStage getSpatialDataflowExportStage() {
switch (pimExportSpatialDataflow.getValue()) {
case SpatialDataflowExportNone: return SpatialDataflowExportStage::None;
case SpatialDataflowExportSpatial1: return SpatialDataflowExportStage::Spatial1;
case SpatialDataflowExportSpatial2: return SpatialDataflowExportStage::Spatial2;
case SpatialDataflowExportSpatial3: return SpatialDataflowExportStage::Spatial3;
case SpatialDataflowExportAll: return SpatialDataflowExportStage::All;
}
llvm_unreachable("unknown spatial dataflow export mode");
}
bool shouldExportSpatialDataflowStage(SpatialDataflowExportStage mode, SpatialDataflowExportStage stage) {
switch (mode) {
case SpatialDataflowExportStage::None: return false;
case SpatialDataflowExportStage::Spatial1: return stage == SpatialDataflowExportStage::Spatial1;
case SpatialDataflowExportStage::Spatial2: return stage == SpatialDataflowExportStage::Spatial2;
case SpatialDataflowExportStage::Spatial3: return stage == SpatialDataflowExportStage::Spatial3;
case SpatialDataflowExportStage::All: return stage != SpatialDataflowExportStage::None;
}
return false;
}
LogicalResult exportSpatialDataflowCsvGraph(func::FuncOp func, StringRef reportName) {
return exportGraph(func, reportName);
}
LogicalResult exportSpatialDataflowCsvScheduled(func::FuncOp func, StringRef reportName, StringRef stage) {
return exportScheduled(func, reportName, stage);
}
} // namespace spatial
} // namespace onnx_mlir
@@ -0,0 +1,28 @@
#pragma once
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/StringRef.h"
namespace onnx_mlir {
namespace spatial {
enum class SpatialDataflowExportStage {
None,
Spatial1,
Spatial2,
Spatial3,
All,
};
SpatialDataflowExportStage getSpatialDataflowExportStage();
mlir::LogicalResult exportSpatialDataflowCsvGraph(mlir::func::FuncOp func, llvm::StringRef reportName);
mlir::LogicalResult
exportSpatialDataflowCsvScheduled(mlir::func::FuncOp func, llvm::StringRef reportName, llvm::StringRef stage);
bool shouldExportSpatialDataflowStage(SpatialDataflowExportStage mode, SpatialDataflowExportStage stage);
} // namespace spatial
} // namespace onnx_mlir
-2
View File
@@ -11,8 +11,6 @@ std::unique_ptr<mlir::Pass> createONNXToSpatialPass();
std::unique_ptr<mlir::Pass> createSpatialLayoutPlanningPass(); std::unique_ptr<mlir::Pass> createSpatialLayoutPlanningPass();
std::unique_ptr<mlir::Pass> createLowerSpatialPlansPass(); std::unique_ptr<mlir::Pass> createLowerSpatialPlansPass();
std::unique_ptr<mlir::Pass> createSpatialToGraphvizPass();
std::unique_ptr<mlir::Pass> createSpatialToPimPass(); std::unique_ptr<mlir::Pass> createSpatialToPimPass();
std::unique_ptr<mlir::Pass> createPimBufferizationPass(); std::unique_ptr<mlir::Pass> createPimBufferizationPass();
-1
View File
@@ -74,7 +74,6 @@ void PimAccelerator::registerPasses(int optLevel) const {
registerPass(createONNXToSpatialPass); registerPass(createONNXToSpatialPass);
registerPass(createSpatialLayoutPlanningPass); registerPass(createSpatialLayoutPlanningPass);
registerPass(createLowerSpatialPlansPass); registerPass(createLowerSpatialPlansPass);
registerPass(createSpatialToGraphvizPass);
registerPass(createSpatialToPimPass); registerPass(createSpatialToPimPass);
registerPass(createPimBufferizationPass); registerPass(createPimBufferizationPass);
registerPass(createPimMemoryCoalescingPass); registerPass(createPimMemoryCoalescingPass);
Vendored Submodule
+1
Submodule third_party/PIMCOMP-NN added at 0cfbfa55cc
+253
View File
@@ -0,0 +1,253 @@
#!/usr/bin/env python3.13
import argparse
import re
from collections import Counter, defaultdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterable
OP_PATTERNS = {
"tensor.extract_slice": re.compile(r"\btensor\.extract_slice\b"),
"tensor.insert_slice": re.compile(r"\btensor\.insert_slice\b"),
"spat.channel_send": re.compile(r"\bspat\.channel_send\b"),
"spat.channel_receive": re.compile(r"\bspat\.channel_receive\b"),
"scf.for": re.compile(r"\bscf\.for\b"),
"tensor.empty": re.compile(r"\btensor\.empty\b"),
}
VALUE_RE = re.compile(r"^\s*(%[\w.$-]+)\s*=\s*(.+)$")
TYPE_RE = re.compile(r":\s*([^:]+?)\s*(?:to|into|$)")
CHANNEL_RE = re.compile(r"channel\s+(%c[-\w.$]+)")
FROM_TO_RE = re.compile(r"from\s+(%c[-\w.$]+)\s+to\s+(%c[-\w.$]+)")
EXTRACT_SLICE_RE = re.compile(
r"^\s*(%[\w.$-]+)\s*=\s*tensor\.extract_slice\s+(%[\w.$-]+)\[(.*?)\]\s*\[(.*?)\]\s*\[(.*?)\]\s*:\s*(.*?)\s+to\s+(.*)$"
)
INSERT_SLICE_RE = re.compile(
r"^\s*(%[\w.$-]+)\s*=\s*tensor\.insert_slice\s+(%[\w.$-]+)\s+into\s+(%[\w.$-]+)\[(.*?)\]\s*\[(.*?)\]\s*\[(.*?)\]\s*:\s*(.*?)\s+into\s+(.*)$"
)
CHANNEL_RECEIVE_RE = re.compile(
r"^\s*(%[\w.$-]+)\s*=\s*spat\.channel_receive\s+channel\s+(%c[-\w.$]+)\s+from\s+(%c[-\w.$]+)\s+to\s+(%c[-\w.$]+)\s*:\s*(.*)$"
)
CHANNEL_SEND_RE = re.compile(
r"^\s*spat\.channel_send\s+(%[\w.$-]+)\s+channel\s+(%c[-\w.$]+)\s+from\s+(%c[-\w.$]+)\s+to\s+(%c[-\w.$]+)\s*:\s*(.*)$"
)
CONST_INDEX_RE = re.compile(r"^\s*(%c[\w.$-]+)\s*=\s*arith\.constant\s+(-?\d+)\s*:\s*index\b")
@dataclass
class ChainGroup:
kind: str
signature: str
count: int = 0
first_line: int = 0
last_line: int = 0
fragment_type: str = ""
dest_type: str = ""
varying_dims: set[int] = field(default_factory=set)
rows: list[int | None] = field(default_factory=list)
channels: list[int | None] = field(default_factory=list)
sources: list[int | None] = field(default_factory=list)
targets: list[int | None] = field(default_factory=list)
def add(self,
line_no: int,
fragment_type: str,
dest_type: str,
offsets: list[str],
channel: int | None = None,
source: int | None = None,
target: int | None = None) -> None:
self.count += 1
if self.first_line == 0:
self.first_line = line_no
self.last_line = line_no
self.fragment_type = fragment_type or self.fragment_type
self.dest_type = dest_type or self.dest_type
numeric_offsets = []
for idx, offset in enumerate(offsets):
try:
numeric_offsets.append(int(offset))
except ValueError:
self.varying_dims.add(idx)
numeric_offsets.append(None)
if self.rows is not None:
row = numeric_offsets[2] if len(numeric_offsets) > 2 else None
self.rows.append(row)
if len(self.rows) >= 2 and self.rows[-1] != self.rows[-2]:
self.varying_dims.add(2)
self.channels.append(channel)
self.sources.append(source)
self.targets.append(target)
def parse_const_indices(lines: Iterable[str]) -> dict[str, int]:
constants: dict[str, int] = {}
for line in lines:
match = CONST_INDEX_RE.match(line)
if match:
constants[match.group(1)] = int(match.group(2))
return constants
def split_index_list(value: str) -> list[str]:
return [piece.strip() for piece in value.split(",") if piece.strip()]
def decode_const_index(token: str, constants: dict[str, int]) -> int | None:
token = token.strip()
if token in constants:
return constants[token]
try:
return int(token)
except ValueError:
return None
def sequence_kind(values: list[int | None]) -> str:
concrete = [value for value in values if value is not None]
if not concrete:
return "dynamic"
if len(concrete) == len(values) and all(b - a == 1 for a, b in zip(concrete, concrete[1:])):
return "consecutive"
if len(set(concrete)) == 1:
return "constant"
return "table"
def analyze_file(path: Path) -> tuple[Counter, dict[tuple[str, str], ChainGroup]]:
text = path.read_text()
lines = text.splitlines()
consts = parse_const_indices(lines)
counts = Counter()
groups: dict[tuple[str, str], ChainGroup] = {}
for line in lines:
for name, pattern in OP_PATTERNS.items():
if pattern.search(line):
counts[name] += 1
value_defs: dict[str, tuple[str, int, re.Match[str] | None]] = {}
for line_no, line in enumerate(lines, start=1):
if match := CHANNEL_RECEIVE_RE.match(line):
value_defs[match.group(1)] = ("receive", line_no, match)
elif match := EXTRACT_SLICE_RE.match(line):
value_defs[match.group(1)] = ("extract", line_no, match)
elif match := INSERT_SLICE_RE.match(line):
source = match.group(2)
producer = value_defs.get(source)
if not producer:
continue
offsets = split_index_list(match.group(4))
sizes = split_index_list(match.group(5))
strides = split_index_list(match.group(6))
dest = match.group(3)
dest_type = match.group(8).strip()
if producer[0] == "receive":
recv = producer[2]
assert recv is not None
channel = decode_const_index(recv.group(2), consts)
source_core = decode_const_index(recv.group(3), consts)
target_core = decode_const_index(recv.group(4), consts)
signature = f"recv_insert:{recv.group(5).strip()}->{dest_type}|sizes={','.join(sizes)}|strides={','.join(strides)}"
group = groups.setdefault(
("receive_to_insert", signature),
ChainGroup("receive_to_insert", signature),
)
group.add(match.start() and producer[1] or line_no,
recv.group(5).strip(),
dest_type,
offsets,
channel=channel,
source=source_core,
target=target_core)
elif producer[0] == "extract":
extract = producer[2]
assert extract is not None
extract_offsets = split_index_list(extract.group(3))
signature = (
f"extract_insert:{extract.group(6).strip()}->{dest_type}|"
f"extract_sizes={extract.group(4).strip()}|insert_sizes={','.join(sizes)}|"
f"src={extract.group(2)}"
)
group = groups.setdefault(
("extract_to_insert", signature),
ChainGroup("extract_to_insert", signature),
)
group.add(producer[1], extract.group(7).strip(), dest_type, offsets)
if extract_offsets and decode_const_index(extract_offsets[0], consts) is None:
group.varying_dims.add(0)
elif match := CHANNEL_SEND_RE.match(line):
source_value = match.group(1)
producer = value_defs.get(source_value)
if not producer or producer[0] != "extract":
continue
extract = producer[2]
assert extract is not None
signature = (
f"extract_send:{extract.group(6).strip()}->{match.group(5).strip()}|"
f"extract_sizes={extract.group(4).strip()}|src={extract.group(2)}"
)
group = groups.setdefault(
("extract_to_send", signature),
ChainGroup("extract_to_send", signature),
)
group.add(
producer[1],
extract.group(7).strip(),
match.group(5).strip(),
split_index_list(extract.group(3)),
channel=decode_const_index(match.group(2), consts),
source=decode_const_index(match.group(3), consts),
target=decode_const_index(match.group(4), consts),
)
return counts, groups
def print_report(path: Path, counts: Counter, groups: dict[tuple[str, str], ChainGroup], limit: int) -> None:
print(f"== {path} ==")
print("counts:")
for name in OP_PATTERNS:
print(f" {name}: {counts[name]}")
ranked = sorted(groups.values(), key=lambda group: (-group.count, group.first_line))
print("hot chains:")
for group in ranked[:limit]:
varying = ",".join(str(dim) for dim in sorted(group.varying_dims)) or "none"
print(f" - kind: {group.kind}")
print(f" lines: {group.first_line}-{group.last_line}")
print(f" fragments: {group.count}")
print(f" fragment_type: {group.fragment_type}")
print(f" dest_type: {group.dest_type}")
print(f" varying_dims: {varying}")
if group.rows:
print(f" row_sequence: {sequence_kind(group.rows)}")
if group.channels:
print(f" channel_ids: {sequence_kind(group.channels)}")
if group.sources:
print(f" source_ids: {sequence_kind(group.sources)}")
if group.targets:
print(f" target_ids: {sequence_kind(group.targets)}")
print(f" signature: {group.signature}")
print()
def main() -> None:
parser = argparse.ArgumentParser(description="Analyze repeated Spatial/PIM tensor IR cardinality patterns.")
parser.add_argument("paths", nargs="+", help="MLIR files to analyze.")
parser.add_argument("--limit", type=int, default=12, help="Maximum number of hot chains to print per file.")
args = parser.parse_args()
for path_arg in args.paths:
path = Path(path_arg)
counts, groups = analyze_file(path)
print_report(path, counts, groups, args.limit)
if __name__ == "__main__":
main()
Regular → Executable
View File