-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from xdsl.dialects.builtin import IntegerAttrTypeConstr, i1 | ||
from xdsl.ir import Dialect, VerifyException | ||
from xdsl.irdl import ( | ||
IRDLOperation, | ||
irdl_op_definition, | ||
prop_def, | ||
result_def, | ||
) | ||
from xdsl.parser import Float64Type, FloatAttr, IndexType, IntegerType | ||
|
||
|
||
@irdl_op_definition | ||
class BernoulliOp(IRDLOperation): | ||
name = "prob.bernoulli" | ||
|
||
prob = prop_def(FloatAttr[Float64Type]) | ||
|
||
out = result_def(i1) | ||
|
||
assembly_format = "$prob attr-dict" | ||
|
||
def __init__(self, prob: float | FloatAttr[Float64Type]): | ||
if isinstance(prob, float): | ||
prob = FloatAttr(prob, 64) | ||
|
||
# Why is this needed? | ||
assert not isinstance(prob, int) | ||
|
||
super().__init__( | ||
properties={ | ||
"prob": prob, | ||
}, | ||
result_types=(i1,), | ||
) | ||
|
||
def verify_(self) -> None: | ||
prob = self.prob.value.data | ||
if prob < 0 or prob > 1: | ||
raise VerifyException( | ||
f"Property 'prob' = {prob} should be in the range [0, 1]" | ||
) | ||
|
||
|
||
@irdl_op_definition | ||
class UniformOp(IRDLOperation): | ||
name = "prob.uniform" | ||
|
||
out = result_def(IntegerAttrTypeConstr) | ||
|
||
assembly_format = "attr-dict `:` type($out)" | ||
|
||
def __init__(self, out_type: IntegerType | IndexType): | ||
super().__init__( | ||
result_types=(out_type,), | ||
) | ||
|
||
|
||
Prob = Dialect( | ||
"prob", | ||
[ | ||
BernoulliOp, | ||
UniformOp, | ||
], | ||
[], | ||
) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// RUN: QUOPT_ROUNDTRIP | ||
// RUN: QUOPT_GENERIC_ROUNDTRIP | ||
|
||
// CHECK: %{{.*}} = prob.bernoulli 5.000000e-01 : f64 | ||
// CHECK-GENERIC: %{{.*}} = "prob.bernoulli"() <{"prob" = 5.000000e-01 : f64}> : () -> i1 | ||
%0 = prob.bernoulli 0.5 | ||
|
||
// CHECK: %{{.*}} = prob.uniform : i32 | ||
// CHECK-GENERIC: %{{.*}} = "prob.uniform"() : () -> i32 | ||
%1 = prob.uniform : i32 |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: quopt %s --verify-diagnostics --split-input-file | filecheck %s | ||
|
||
%0 = prob.bernoulli -1.0 | ||
// CHECK: Property 'prob' = -1.0 should be in the range [0, 1] | ||
|
||
// ----- | ||
|
||
%1 = prob.bernoulli 1.5 | ||
// CHECK: Property 'prob' = 1.5 should be in the range [0, 1] |