|
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 |
@@ -493,12 +521,32 @@ def facto_testcase_gen( # noqa: C901 |
493 | 521 | apply_tensor_contraints(op_name, index) |
494 | 522 | ) |
495 | 523 | elif in_spec.type.is_dim_list(): |
496 | | - spec.inspec[index].constraints.extend( |
497 | | - [ |
498 | | - cp.Length.Ge(lambda deps: 1), |
499 | | - cp.Optional.Eq(lambda deps: False), |
500 | | - ] |
501 | | - ) |
| 524 | + # Special handling for permute_copy.default to ensure valid permutation |
| 525 | + if op_name == "permute_copy.default": |
| 526 | + spec.inspec[index].constraints.extend( |
| 527 | + [ |
| 528 | + cp.Length.Ge(lambda deps: 1), |
| 529 | + cp.Length.Eq( |
| 530 | + lambda deps: deps[0].dim() |
| 531 | + ), # Must be a complete permutation |
| 532 | + cp.Optional.Eq(lambda deps: False), |
| 533 | + # Generate valid permutations using only positive indices |
| 534 | + # Cadence/Xtensa hardware kernels do not support negative dimension indices |
| 535 | + cp.Value.Gen( |
| 536 | + lambda deps, length: ( |
| 537 | + _positive_valid_dim_list(deps[0], length), |
| 538 | + fn.invalid_dim_list(deps[0], length), |
| 539 | + ) |
| 540 | + ), |
| 541 | + ] |
| 542 | + ) |
| 543 | + else: |
| 544 | + spec.inspec[index].constraints.extend( |
| 545 | + [ |
| 546 | + cp.Length.Ge(lambda deps: 1), |
| 547 | + cp.Optional.Eq(lambda deps: False), |
| 548 | + ] |
| 549 | + ) |
502 | 550 | elif in_spec.type.is_bool(): |
503 | 551 | spec.inspec[index].constraints.extend( |
504 | 552 | [ |
|
0 commit comments