Skip to content

Commit 86d9f1b

Browse files
add more verification
1 parent 33785ec commit 86d9f1b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,27 @@ LogicalResult SparseTensorEncodingAttr::verify(
666666
[](LevelType i) { return isDenseLT(i); }))
667667
return emitError() << "expected all dense lvlTypes "
668668
"before a n_out_of_m level";
669+
670+
if (dimToLvl && (dimToLvl.getNumDims() != dimToLvl.getNumResults())) {
671+
if (!isBlockSparsity(dimToLvl)) {
672+
return emitError()
673+
<< "expected 1xm block structure for n_out_of_m level";
674+
}
675+
auto sizes = getBlockSize(dimToLvl);
676+
unsigned coefficient = 0;
677+
for (const auto &elem : sizes) {
678+
if (elem != 0 && coefficient != 0) {
679+
return emitError() << "expected only one blocked level with the same "
680+
"coefficients";
681+
} else {
682+
coefficient = elem;
683+
}
684+
}
685+
if (coefficient != getM(*it)) {
686+
return emitError() << "expected coeffiencts of Affine expressions "
687+
"to be equal to m of n_out_of_m level";
688+
}
689+
}
669690
}
670691
// Before we can check that the level-rank is consistent/coherent
671692
// across all fields, we need to define it. The source-of-truth for

mlir/test/Dialect/SparseTensor/invalid_encoding.mlir

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,49 @@ func.func private @NOutOfM(%arg0: tensor<?x?x?xf64, #NOutOfM>) {
375375
func.func private @NOutOfM(%arg0: tensor<?x?x?xf64, #NOutOfM>) {
376376
return
377377
}
378+
379+
// -----
380+
381+
// expected-error@+1 {{expected 1xm block structure for n_out_of_m_level}}
382+
#NOutOfM = #sparse_tensor.encoding<{
383+
map = ( i, j, k ) ->
384+
( i : dense,
385+
k floordiv 2 : dense,
386+
j : dense,
387+
k mod 4 : structured[2, 4]
388+
)
389+
}>
390+
func.func private @NOutOfM(%arg0: tensor<?x?x?xf64, #NOutOfM>) {
391+
return
392+
}
393+
394+
// -----
395+
396+
// expected-error@+1 {{expected coeffiencts of Affine expressions to be equal to m of n_out_of_m level}}
397+
#NOutOfM = #sparse_tensor.encoding<{
398+
map = ( i, j, k ) ->
399+
( i : dense,
400+
k floordiv 2 : dense,
401+
j : dense,
402+
k mod 2 : structured[2, 4]
403+
)
404+
}>
405+
func.func private @NOutOfM(%arg0: tensor<?x?x?xf64, #NOutOfM>) {
406+
return
407+
}
408+
409+
// -----
410+
411+
// expected-error@+1 {{expected only one blocked level with the same coefficients}}
412+
#NOutOfM = #sparse_tensor.encoding<{
413+
map = ( i, j, k ) ->
414+
( i floordiv 2 : dense,
415+
i mod 2 : dense,
416+
j : dense,
417+
k floordiv 4 : dense,
418+
k mod 4 : structured[2, 4]
419+
)
420+
}>
421+
func.func private @NOutOfM(%arg0: tensor<?x?x?xf64, #NOutOfM>) {
422+
return
423+
}

0 commit comments

Comments
 (0)