Skip to content

Commit dff8a59

Browse files
authored
[Jarvis][Nightly] address error jarvis-nightly-operators-test-aten-permute-copy-out
Differential Revision: D85364547 Pull Request resolved: #15497
1 parent e977f0f commit dff8a59

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

backends/cadence/utils/facto_util.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import torch
1616
from facto.inputgen.argtuple.gen import ArgumentTupleGenerator
1717
from facto.inputgen.specs.model import ConstraintProducer as cp
18+
from facto.inputgen.utils.random_manager import seeded_random_manager as rm
1819
from facto.inputgen.variable.type import ScalarDtype
1920
from facto.specdb.db import SpecDictDB
2021

@@ -26,6 +27,33 @@
2627
_shape_cache: dict[str, list[int]] = {}
2728

2829

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+
2957
def apply_tensor_contraints(op_name: str, index: int) -> list[object]:
3058
# Constraint to limit tensor size to < 4000 bytes with fully randomized shapes
3159
import random
@@ -493,12 +521,32 @@ def facto_testcase_gen( # noqa: C901
493521
apply_tensor_contraints(op_name, index)
494522
)
495523
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+
)
502550
elif in_spec.type.is_bool():
503551
spec.inspec[index].constraints.extend(
504552
[

0 commit comments

Comments
 (0)