|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from dataclasses import dataclass |
| 8 | +from enum import Enum |
| 9 | +from typing import Callable |
| 10 | + |
| 11 | +from inputgen.attribute.model import Attribute |
| 12 | + |
| 13 | + |
| 14 | +class ConstraintSuffix(str, Enum): |
| 15 | + EQ = "eq" # == |
| 16 | + NE = "ne" # != |
| 17 | + IN = "in" # in list |
| 18 | + NOTIN = "notin" # not in list |
| 19 | + LE = "le" # <= |
| 20 | + LT = "lt" # < |
| 21 | + GE = "ge" # >= |
| 22 | + GT = "gt" # > |
| 23 | + # TODO(mcandales): Enable Such That |
| 24 | + # ST = "st" # such that. |
| 25 | + GEN = "gen" # generate. |
| 26 | + # This constraint is used to provide functions that generate |
| 27 | + # valid and invalid values |
| 28 | + BE = "be" # be |
| 29 | + # This constraint is used to provide values that we want to |
| 30 | + # sample from, in cases where there are no imposed constraints |
| 31 | + # on the attribute, but we still have a preference for certain |
| 32 | + # values. |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class Constraint: |
| 37 | + attribute: Attribute |
| 38 | + suffix: ConstraintSuffix |
| 39 | + fn: Callable |
| 40 | + |
| 41 | + |
| 42 | +class ConstraintAttributeSuffixes: |
| 43 | + def __init__(self, attr: Attribute): |
| 44 | + self.Eq = lambda fn: Constraint(attr, ConstraintSuffix.EQ, fn) |
| 45 | + self.Ne = lambda fn: Constraint(attr, ConstraintSuffix.NE, fn) |
| 46 | + self.In = lambda fn: Constraint(attr, ConstraintSuffix.IN, fn) |
| 47 | + self.NotIn = lambda fn: Constraint(attr, ConstraintSuffix.NOTIN, fn) |
| 48 | + if attr in [Attribute.LENGTH, Attribute.RANK, Attribute.SIZE, Attribute.VALUE]: |
| 49 | + self.Le = lambda fn: Constraint(attr, ConstraintSuffix.LE, fn) |
| 50 | + self.Lt = lambda fn: Constraint(attr, ConstraintSuffix.LT, fn) |
| 51 | + self.Ge = lambda fn: Constraint(attr, ConstraintSuffix.GE, fn) |
| 52 | + self.Gt = lambda fn: Constraint(attr, ConstraintSuffix.GT, fn) |
| 53 | + # TODO(mcandales): Enable Such That |
| 54 | + # self.St = lambda fn: Constraint(attr, ConstraintSuffix.ST, fn) |
| 55 | + self.Be = lambda fn: Constraint(attr, ConstraintSuffix.BE, fn) |
| 56 | + self.Gen = lambda fn: Constraint(attr, ConstraintSuffix.GEN, fn) |
| 57 | + |
| 58 | + |
| 59 | +class ConstraintProducer: |
| 60 | + Optional = ConstraintAttributeSuffixes(Attribute.OPTIONAL) |
| 61 | + Dtype = ConstraintAttributeSuffixes(Attribute.DTYPE) |
| 62 | + Length = ConstraintAttributeSuffixes(Attribute.LENGTH) |
| 63 | + Rank = ConstraintAttributeSuffixes(Attribute.RANK) |
| 64 | + Size = ConstraintAttributeSuffixes(Attribute.SIZE) |
| 65 | + Value = ConstraintAttributeSuffixes(Attribute.VALUE) |
0 commit comments