Skip to content

[mlir][polynomial] verify from_tensor coeff type #93243

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
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ void FromTensorOp::build(OpBuilder &builder, OperationState &result,
LogicalResult FromTensorOp::verify() {
ArrayRef<int64_t> tensorShape = getInput().getType().getShape();
RingAttr ring = getOutput().getType().getRing();
unsigned polyDegree = ring.getPolynomialModulus().getPolynomial().getDegree();
bool compatible = tensorShape.size() == 1 && tensorShape[0] <= polyDegree;
if (!compatible) {
InFlightDiagnostic diag = emitOpError()
<< "input type " << getInput().getType()
<< " does not match output type "
<< getOutput().getType();
diag.attachNote() << "the input type must be a tensor of shape [d] where d "
"is at most the degree of the polynomialModulus of "
"the output type's ring attribute";
return diag;
IntPolynomialAttr polyMod = ring.getPolynomialModulus();
if (polyMod) {
unsigned polyDegree = polyMod.getPolynomial().getDegree();
bool compatible = tensorShape.size() == 1 && tensorShape[0] <= polyDegree;
if (!compatible) {
InFlightDiagnostic diag = emitOpError()
<< "input type " << getInput().getType()
<< " does not match output type "
<< getOutput().getType();
diag.attachNote()
<< "the input type must be a tensor of shape [d] where d "
"is at most the degree of the polynomialModulus of "
"the output type's ring attribute";
return diag;
}
}

APInt coefficientModulus = ring.getCoefficientModulus().getValue();
unsigned cmodBitWidth = coefficientModulus.ceilLogBase2();
unsigned inputBitWidth = getInput().getType().getElementTypeBitWidth();

if (inputBitWidth > cmodBitWidth) {
if (inputBitWidth > ring.getCoefficientType().getIntOrFloatBitWidth()) {
InFlightDiagnostic diag = emitOpError()
<< "input tensor element type "
<< getInput().getType().getElementType()
Expand All @@ -67,24 +68,27 @@ LogicalResult FromTensorOp::verify() {

LogicalResult ToTensorOp::verify() {
ArrayRef<int64_t> tensorShape = getOutput().getType().getShape();
unsigned polyDegree = getInput()
.getType()
.getRing()
.getPolynomialModulus()
.getPolynomial()
.getDegree();
bool compatible = tensorShape.size() == 1 && tensorShape[0] == polyDegree;
IntPolynomialAttr polyMod =
getInput().getType().getRing().getPolynomialModulus();
if (polyMod) {
unsigned polyDegree = polyMod.getPolynomial().getDegree();
bool compatible = tensorShape.size() == 1 && tensorShape[0] == polyDegree;

if (compatible)
return success();
if (compatible)
return success();

InFlightDiagnostic diag = emitOpError()
<< "input type " << getInput().getType()
<< " does not match output type "
<< getOutput().getType();
diag.attachNote()
<< "the output type must be a tensor of shape [d] where d "
"is at most the degree of the polynomialModulus of "
"the input type's ring attribute";
return diag;
}

InFlightDiagnostic diag =
emitOpError() << "input type " << getInput().getType()
<< " does not match output type " << getOutput().getType();
diag.attachNote() << "the output type must be a tensor of shape [d] where d "
"is at most the degree of the polynomialModulus of "
"the input type's ring attribute";
return diag;
return success();
}

LogicalResult MulScalarOp::verify() {
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Polynomial/ops_errors.mlir
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: mlir-opt --split-input-file --verify-diagnostics %s

#my_poly = #polynomial.int_polynomial<1 + x**1024>
#ring = #polynomial.ring<coefficientType=i16, coefficientModulus=256:i32, polynomialModulus=#my_poly>
#ring = #polynomial.ring<coefficientType=i16>
!ty = !polynomial.polynomial<ring=#ring>

func.func @test_from_tensor_too_large_coeffs() {
Expand Down
Loading