Skip to content

Commit 9f01abc

Browse files
manuelcandalesfacebook-github-bot
authored andcommitted
InputGen: introduce attribute constraints
Reviewed By: SS-JIA Differential Revision: D52047183 fbshipit-source-id: 6438c62fc4116d3c3a2e5050b2fe6190ff63b0e1
1 parent 63cbfc8 commit 9f01abc

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

inputgen/specs/__init__.py

Whitespace-only changes.

inputgen/specs/model.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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)

test/inputgen/test_constraints.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import unittest
8+
9+
from inputgen.attribute.model import Attribute
10+
from inputgen.specs.model import Constraint, ConstraintProducer as cp, ConstraintSuffix
11+
12+
13+
class TestConstraint(unittest.TestCase):
14+
def test_constraint(self):
15+
constraint = cp.Optional.Ne(lambda deps: False)
16+
self.assertTrue(isinstance(constraint, Constraint))
17+
self.assertEqual(constraint.attribute, Attribute.OPTIONAL)
18+
self.assertEqual(constraint.suffix, ConstraintSuffix.NE)
19+
self.assertEqual(constraint.fn(None), False)
20+
21+
22+
if __name__ == "__main__":
23+
unittest.main()

0 commit comments

Comments
 (0)