|
| 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 random |
| 8 | +from typing import Any, List, Optional |
| 9 | + |
| 10 | +from inputgen.argument.type import ArgType |
| 11 | +from inputgen.attribute.engine import AttributeEngine |
| 12 | +from inputgen.attribute.model import Attribute |
| 13 | +from inputgen.specs.model import Constraint |
| 14 | + |
| 15 | + |
| 16 | +class StructuralEngine: |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + argtype: ArgType, |
| 20 | + constraints: List[Constraint], |
| 21 | + deps: List[Any], |
| 22 | + valid: bool, |
| 23 | + ): |
| 24 | + self.argtype = argtype |
| 25 | + self.constraints = constraints |
| 26 | + self.deps = deps |
| 27 | + self.valid = valid |
| 28 | + self.hierarchy = StructuralEngine.hierarchy(argtype) |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def hierarchy(argtype) -> List[Attribute]: |
| 32 | + """Return the structural hierarchy for a given argument type""" |
| 33 | + if argtype.is_tensor_list(): |
| 34 | + return [Attribute.LENGTH, Attribute.RANK, Attribute.SIZE] |
| 35 | + elif argtype.is_tensor(): |
| 36 | + return [Attribute.RANK, Attribute.SIZE] |
| 37 | + elif argtype.is_list(): |
| 38 | + return [Attribute.LENGTH, Attribute.VALUE] |
| 39 | + else: |
| 40 | + return [Attribute.VALUE] |
| 41 | + |
| 42 | + def gen_structure_with_depth_and_length( |
| 43 | + self, depth: int, length: int, focus: Attribute |
| 44 | + ): |
| 45 | + if length == 0: |
| 46 | + yield () |
| 47 | + return |
| 48 | + |
| 49 | + attr = self.hierarchy[-(depth + 1)] |
| 50 | + focus_ixs = range(length) if focus == attr else (random.choice(range(length)),) |
| 51 | + for focus_ix in focus_ixs: |
| 52 | + values = [()] |
| 53 | + for ix in range(length): |
| 54 | + if ix == focus_ix: |
| 55 | + elements = self.gen_structure_with_depth(depth, focus, length, ix) |
| 56 | + else: |
| 57 | + elements = self.gen_structure_with_depth(depth, None, length, ix) |
| 58 | + new_values = [] |
| 59 | + for elem in elements: |
| 60 | + new_values += [t + (elem,) for t in values] |
| 61 | + values = new_values |
| 62 | + yield from values |
| 63 | + |
| 64 | + def gen_structure_with_depth( |
| 65 | + self, |
| 66 | + depth: int, |
| 67 | + focus: Attribute, |
| 68 | + length: Optional[int] = None, |
| 69 | + ix: Optional[int] = None, |
| 70 | + ): |
| 71 | + attr = self.hierarchy[-(depth + 1)] |
| 72 | + |
| 73 | + if ix is not None: |
| 74 | + args = (self.deps, length, ix) |
| 75 | + elif length is not None: |
| 76 | + args = ( |
| 77 | + self.deps, |
| 78 | + length, |
| 79 | + ) |
| 80 | + else: |
| 81 | + args = (self.deps,) |
| 82 | + |
| 83 | + values = AttributeEngine(attr, self.constraints, self.valid, self.argtype).gen( |
| 84 | + focus, *args |
| 85 | + ) |
| 86 | + |
| 87 | + for v in values: |
| 88 | + if depth == 0: |
| 89 | + yield v |
| 90 | + else: |
| 91 | + yield from self.gen_structure_with_depth_and_length(depth - 1, v, focus) |
| 92 | + |
| 93 | + def gen(self, focus: Attribute): |
| 94 | + depth = len(self.hierarchy) - 1 |
| 95 | + yield from self.gen_structure_with_depth(depth, focus) |
0 commit comments