|
15 | 15 | import torch |
16 | 16 | from facto.inputgen.argtuple.gen import ArgumentTupleGenerator |
17 | 17 | from facto.inputgen.specs.model import ConstraintProducer as cp |
| 18 | +from facto.inputgen.utils.random_manager import seeded_random_manager as rm |
18 | 19 | from facto.inputgen.variable.type import ScalarDtype |
19 | 20 | from facto.specdb.db import SpecDictDB |
20 | 21 |
|
|
26 | 27 | _shape_cache: dict[str, list[int]] = {} |
27 | 28 |
|
28 | 29 |
|
| 30 | +def _positive_valid_dim_list(tensor: torch.Tensor, length: int) -> set[tuple[int, ...]]: |
| 31 | + """ |
| 32 | + Generate valid permutations using only positive dimension indices. |
| 33 | + This is required for Cadence/Xtensa kernels that don't support negative indexing. |
| 34 | + |
| 35 | + Args: |
| 36 | + tensor: Input tensor to generate permutations for |
| 37 | + length: Number of dimensions in the permutation (must equal tensor.dim()) |
| 38 | + |
| 39 | + Returns: |
| 40 | + Set of valid permutation tuples containing only positive indices [0, rank-1] |
| 41 | + """ |
| 42 | + if length > tensor.dim(): |
| 43 | + return set() |
| 44 | + |
| 45 | + n = tensor.dim() |
| 46 | + pool = list(range(n)) |
| 47 | + |
| 48 | + # Generate multiple valid permutations (only positive indices) |
| 49 | + permutations: set[tuple[int, ...]] = set() |
| 50 | + for _ in range(3): # Generate 3 different permutations for diversity |
| 51 | + perm = tuple(rm.get_random().sample(pool, length)) |
| 52 | + permutations.add(perm) |
| 53 | + |
| 54 | + return permutations |
| 55 | + |
| 56 | + |
29 | 57 | def apply_tensor_contraints(op_name: str, index: int) -> list[object]: |
30 | 58 | # Constraint to limit tensor size to < 4000 bytes with fully randomized shapes |
31 | 59 | import random |
@@ -489,12 +517,30 @@ def facto_testcase_gen( # noqa: C901 |
489 | 517 | apply_tensor_contraints(op_name, index) |
490 | 518 | ) |
491 | 519 | elif in_spec.type.is_dim_list(): |
492 | | - spec.inspec[index].constraints.extend( |
493 | | - [ |
494 | | - cp.Length.Ge(lambda deps: 1), |
495 | | - cp.Optional.Eq(lambda deps: False), |
496 | | - ] |
497 | | - ) |
| 520 | + # Special handling for permute_copy.default to ensure valid permutation |
| 521 | + if op_name == "permute_copy.default": |
| 522 | + spec.inspec[index].constraints.extend( |
| 523 | + [ |
| 524 | + cp.Length.Ge(lambda deps: 1), |
| 525 | + cp.Length.Eq(lambda deps: deps[0].dim()), # Must be a complete permutation |
| 526 | + cp.Optional.Eq(lambda deps: False), |
| 527 | + # Generate valid permutations using only positive indices |
| 528 | + # Cadence/Xtensa hardware kernels do not support negative dimension indices |
| 529 | + cp.Value.Gen( |
| 530 | + lambda deps, length: ( |
| 531 | + _positive_valid_dim_list(deps[0], length), |
| 532 | + fn.invalid_dim_list(deps[0], length), |
| 533 | + ) |
| 534 | + ), |
| 535 | + ] |
| 536 | + ) |
| 537 | + else: |
| 538 | + spec.inspec[index].constraints.extend( |
| 539 | + [ |
| 540 | + cp.Length.Ge(lambda deps: 1), |
| 541 | + cp.Optional.Eq(lambda deps: False), |
| 542 | + ] |
| 543 | + ) |
498 | 544 | elif in_spec.type.is_bool(): |
499 | 545 | spec.inspec[index].constraints.extend( |
500 | 546 | [ |
|
0 commit comments