|
23 | 23 |
|
24 | 24 |
|
25 | 25 | def apply_tensor_contraints(op_name: str, index: int) -> list[object]:
|
26 |
| - # Constraint to limit tensor size product to < 4000 |
27 |
| - max_size_constraint = cp.Size.Le(lambda deps, r, d: max(1, int((3999) ** (1 / r)))) |
| 26 | + # Constraint to limit tensor size product to < 4000 with fully randomized shapes |
| 27 | + import random |
| 28 | + |
| 29 | + # Global cache to store generated shapes per tensor to ensure consistency |
| 30 | + _shape_cache: dict[str, list[int]] = {} |
| 31 | + |
| 32 | + def generate_random_shape_with_product_limit( |
| 33 | + rank: int, max_product: int = 3999, seed_base: int = 42 |
| 34 | + ) -> list[int]: |
| 35 | + """Generate a random shape with given rank ensuring product < max_product""" |
| 36 | + random.seed(seed_base + rank) |
| 37 | + |
| 38 | + # Start with all dimensions as 1 |
| 39 | + shape = [1] * rank |
| 40 | + remaining_product = max_product - 1 # Leave room since we start with product=1 |
| 41 | + |
| 42 | + # Randomly distribute the remaining capacity across dimensions |
| 43 | + for i in range(rank): |
| 44 | + if remaining_product <= 1: |
| 45 | + break |
| 46 | + |
| 47 | + # Calculate maximum size this dimension can have without exceeding limit |
| 48 | + current_product = 1 |
| 49 | + for j in range(rank): |
| 50 | + if j != i: |
| 51 | + current_product *= shape[j] |
| 52 | + |
| 53 | + max_size_for_dim = min( |
| 54 | + remaining_product // current_product, 50 |
| 55 | + ) # Cap at 50 |
| 56 | + if max_size_for_dim > shape[i]: |
| 57 | + # Randomly choose a size between current and max |
| 58 | + new_size = random.randint(shape[i], max_size_for_dim) |
| 59 | + shape[i] = new_size |
| 60 | + remaining_product = max_product // (current_product * new_size) |
| 61 | + remaining_product = max(1, remaining_product) |
| 62 | + |
| 63 | + # Final random shuffle of the dimensions to make it more random |
| 64 | + random.shuffle(shape) |
| 65 | + return shape |
| 66 | + |
| 67 | + def random_size_constraint(deps: object, r: int, d: int) -> int: |
| 68 | + """Generate random sizes ensuring total product < 4000""" |
| 69 | + # Create a unique key for this tensor configuration |
| 70 | + cache_key = f"{r}_{d}" |
| 71 | + |
| 72 | + if cache_key not in _shape_cache: |
| 73 | + # Generate a new random shape for this rank |
| 74 | + shape = generate_random_shape_with_product_limit( |
| 75 | + r, max_product=3999, seed_base=42 + r * 10 |
| 76 | + ) |
| 77 | + _shape_cache[cache_key] = shape |
| 78 | + |
| 79 | + # Return the size for dimension d, ensuring we don't go out of bounds |
| 80 | + cached_shape = _shape_cache[cache_key] |
| 81 | + return cached_shape[d] if d < len(cached_shape) else 1 |
| 82 | + |
| 83 | + max_size_constraint = cp.Size.Le( |
| 84 | + lambda deps, r, d: random_size_constraint(deps, r, d) |
| 85 | + ) |
28 | 86 |
|
29 | 87 | tensor_constraints = (
|
30 | 88 | [
|
|
0 commit comments