-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[MLIR][XeGPU] Allow some nd ops to have argument shapes mismatch for … #120566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kurapov-peter
merged 4 commits into
llvm:main
from
kurapov-peter:xegpu-nd-ops-validation-relaxation
Jan 22, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1cef75
[MLIR][XeGPU] Allow some nd ops to have argument shapes mismatch for …
kurapov-peter 103db33
Validate tensor descriptor rank against a value rank
kurapov-peter dfca5d6
Add invalid IR test for distribution type mismatch
kurapov-peter 9213451
Prevent non 2d shaped loads/stores to have an sg_map
kurapov-peter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,39 @@ static bool isWriteHintOrNone(const CachePolicyAttr &attr) { | |
kind == CachePolicy::WRITE_BACK || kind == CachePolicy::WRITE_THROUGH; | ||
} | ||
|
||
// Validations for nd instruction arguments is successful if any of these are | ||
// true: | ||
// - tensor descriptor and the output vector shapes exactly match. | ||
// - tensor descriptor has a sg_map attribute and the distributed vector shape | ||
// matches the tensor descriptor shape when scaled using sg_map factors on | ||
// each dimension. | ||
static bool isArgShapesValid(ArrayRef<int64_t> descShape, | ||
ArrayRef<int64_t> valShape, SGMapAttr sgMap) { | ||
if (descShape == valShape) { | ||
if (!sgMap) | ||
return true; | ||
|
||
// this can be relaxed if necessary by supporting non-2d shapes distribution | ||
// until the constraints are defined this lives here instead of the tensor | ||
// descriptor type. | ||
return valShape.size() == sgMap.getWiLayout().size(); | ||
} | ||
|
||
if (!sgMap) | ||
return false; | ||
|
||
if (valShape.size() != descShape.size()) | ||
return false; | ||
|
||
for (const auto &[factor, dim, expected] : | ||
llvm::zip_equal(sgMap.getWiLayout(), valShape, descShape)) { | ||
if (factor * dim != expected) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// XeGPU_CreateNdDescOp | ||
//===----------------------------------------------------------------------===// | ||
|
@@ -210,13 +243,13 @@ LogicalResult PrefetchNdOp::verify() { | |
return emitOpError("Expects a non-scattered TensorDesc.\n"); | ||
|
||
if (!isReadHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing these typos. |
||
if (!isReadHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
return success(); | ||
} | ||
|
@@ -238,13 +271,13 @@ LogicalResult LoadNdOp::verify() { | |
return emitOpError("Invalid result, it should be a VectorType.\n"); | ||
|
||
if (!isReadHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
auto array_len = tdescTy.getArrayLength(); | ||
auto tdescShape = getShapeOf(tdescTy); | ||
|
@@ -280,8 +313,9 @@ LogicalResult LoadNdOp::verify() { | |
auto it = tdescShape.begin(); | ||
tdescShape.insert(it, array_len); | ||
} | ||
auto sgMap = tdescTy.getSGMapAttr(); | ||
|
||
if (tdescShape != valueShape) | ||
if (!isArgShapesValid(tdescShape, valueShape, sgMap)) | ||
return emitOpError() << "Result shape doesn't match TensorDesc shape." | ||
<< "The expected shape is " << makeString(tdescShape) | ||
<< ". But the given shape is " | ||
|
@@ -303,17 +337,26 @@ LogicalResult StoreNdOp::verify() { | |
return emitOpError("Expects a non-scattered TensorDesc.\n"); | ||
|
||
if (!valTy) | ||
return emitOpError("Exepcting a VectorType result.\n"); | ||
return emitOpError("Expecting a VectorType result.\n"); | ||
|
||
if (!isWriteHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
if (!isWriteHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isWriteHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
auto tdescShape = getShapeOf(dstTy); | ||
auto valueShape = getShapeOf(valTy); | ||
auto sgMap = dstTy.getSGMapAttr(); | ||
|
||
if (!isArgShapesValid(tdescShape, valueShape, sgMap)) | ||
return emitOpError() << "Result shape doesn't match TensorDesc shape." | ||
<< "The expected shape is " << makeString(tdescShape) | ||
<< ". But the given shape is " | ||
<< makeString(valueShape) << ".\n"; | ||
return success(); | ||
} | ||
|
||
|
@@ -423,13 +466,13 @@ LogicalResult PrefetchOp::verify() { | |
return emitOpError("Expects a scattered TensorDesc.\n"); | ||
|
||
if (!isReadHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
return success(); | ||
} | ||
|
@@ -446,13 +489,13 @@ LogicalResult LoadGatherOp::verify() { | |
return emitOpError("Expects a scattered TensorDesc.\n"); | ||
|
||
if (!isReadHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isReadHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
auto tdescElemTy = tdescTy.getElementType(); | ||
auto valueElemTy = getElementType(); | ||
|
@@ -490,13 +533,13 @@ LogicalResult StoreScatterOp::verify() { | |
return emitOpError("Expects a scattered TensorDesc.\n"); | ||
|
||
if (!isWriteHintOrNone(getL1HintAttr())) | ||
return emitOpError("invlid l1_hint: ") << getL1HintAttr(); | ||
return emitOpError("invalid l1_hint: ") << getL1HintAttr(); | ||
|
||
if (!isWriteHintOrNone(getL2HintAttr())) | ||
return emitOpError("invlid l2_hint: ") << getL2HintAttr(); | ||
return emitOpError("invalid l2_hint: ") << getL2HintAttr(); | ||
|
||
if (!isWriteHintOrNone(getL3HintAttr())) | ||
return emitOpError("invlid l3_hint: ") << getL3HintAttr(); | ||
return emitOpError("invalid l3_hint: ") << getL3HintAttr(); | ||
|
||
auto maskTy = getMaskType(); | ||
auto valueTy = getValueType(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,17 @@ gpu.func @test_load_nd_vc_2(%src: memref<8x16xf16>) { | |
gpu.return | ||
} | ||
|
||
// load_nd args may have different shapes, validated against sg_map | ||
// CHECK: func @test_load_nd_vc_3(%[[arg0:.*]]: memref<24x32xf32>) { | ||
gpu.func @test_load_nd_vc_3(%src: memref<24x32xf32>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chencha3 do you remember what does 'vc' suffix stand for? I feel like for SIMT-ish examples it is not relevant :) |
||
// CHECK: %[[R0:.*]] = xegpu.create_nd_tdesc %arg0[0, 0] : memref<24x32xf32> -> !xegpu.tensor_desc<8x16xf32, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
%1 = xegpu.create_nd_tdesc %src[0, 0] : memref<24x32xf32> -> | ||
!xegpu.tensor_desc<8x16xf32, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
// CHECK: %[[R1:.*]] = xegpu.load_nd %[[R0]] <{l1_hint = #xegpu.cache_hint<cached>, l2_hint = #xegpu.cache_hint<uncached>}> : !xegpu.tensor_desc<8x16xf32, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> -> vector<8x1xf32> | ||
%2 = xegpu.load_nd %1 <{l1_hint = #xegpu.cache_hint<cached>, l2_hint = #xegpu.cache_hint<uncached>}> : !xegpu.tensor_desc<8x16xf32, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> -> vector<8x1xf32> | ||
gpu.return | ||
} | ||
|
||
// CHECK: func @test_store_nd_vc(%[[arg0:.*]]: memref<24x32xf16>) { | ||
gpu.func @test_store_nd_vc(%dst: memref<24x32xf16>) { | ||
// CHECK: %[[C:.*]] = arith.constant dense<1.000000e+00> : vector<24x32xf16> | ||
|
@@ -108,6 +119,19 @@ gpu.func @test_store_nd_vc_2(%dst: memref<24x32xf16>) { | |
gpu.return | ||
} | ||
|
||
// store_nd args may have different shapes, validated against sg_map | ||
// CHECK: func @test_store_nd_vc_3(%[[arg0:.*]]: memref<24x32xf16>) { | ||
gpu.func @test_store_nd_vc_3(%src: memref<24x32xf16>) { | ||
// CHECK: %[[C:.*]] = arith.constant dense<1.000000e+00> : vector<24x2xf16> | ||
%1 = arith.constant dense<1.0>: vector<24x2xf16> | ||
// CHECK: %[[R0:.*]] = xegpu.create_nd_tdesc %arg0[0, 0] : memref<24x32xf16> -> !xegpu.tensor_desc<24x32xf16, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
%2 = xegpu.create_nd_tdesc %src[0, 0] : memref<24x32xf16> -> | ||
!xegpu.tensor_desc<24x32xf16, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
// CHECK: xegpu.store_nd %[[C]], %[[R0]] <{l1_hint = #xegpu.cache_hint<write_back>, l2_hint = #xegpu.cache_hint<uncached>}> : vector<24x2xf16>, !xegpu.tensor_desc<24x32xf16, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
xegpu.store_nd %1, %2 <{l1_hint = #xegpu.cache_hint<write_back>, l2_hint = #xegpu.cache_hint<uncached>}>: vector<24x2xf16>, !xegpu.tensor_desc<24x32xf16, #xegpu.sg_map<wi_layout = [1, 16], wi_data = [1, 1]>> | ||
gpu.return | ||
} | ||
|
||
// CHECK: gpu.func @test_create_update_nd_tdesc_vc(%[[arg0:.*]]: memref<24x32xf32>) { | ||
gpu.func @test_create_update_nd_tdesc_vc(%src: memref<24x32xf32>) { | ||
// CHECK: %[[REG:.*]] = xegpu.create_nd_tdesc %arg0[0, 0] : memref<24x32xf32> -> !xegpu.tensor_desc<8x16xf32> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.