Matching
Utilities for comparing and partitioning named dimension tuples, including wildcard/ellipsis support.
torchlinops.nameddim.isequal
isequal(
shape1: Sequence,
shape2: Sequence,
shape1_consume_all: bool = False,
shape2_consume_all: bool = False,
) -> tuple[bool, dict[int, list] | None]
Test if two sequences with ellipses are length-compatible and value-compatible.
Implemented with bottom-up DP
| PARAMETER | DESCRIPTION |
|---|---|
shape1
|
The sequences of tokens to compare.
TYPE:
|
shape2
|
The sequences of tokens to compare.
TYPE:
|
shape1_consume_all
|
Whether to consume all elements of the sequence greedily.
This usually means maximizing matches with ELLIPSES tokens.
These flags do not change whether the two shapes are
considered compatible; they only affect the assignment
mapping returned. Without
TYPE:
|
shape2_consume_all
|
Whether to consume all elements of the sequence greedily.
This usually means maximizing matches with ELLIPSES tokens.
These flags do not change whether the two shapes are
considered compatible; they only affect the assignment
mapping returned. Without
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether shape1 and shape2 are compatible. |
dict[int, list]
|
A mapping from |
Examples:
>>> isequal(("A", "B"), ("A", "B"))[0]
True
>>> isequal(("A", "C"), ("A",))[0]
False
>>> isequal(("A", "C"), tuple())[0]
False
>>> isequal(("A", "C"), ("...",))[0]
True
>>> isequal(("A", "C", "..."), ("...",))[0]
True
>>> isequal(("A", "B", "C"), ("A", "...", "C"))[0]
True
>>> isequal(("...", "A", "C", "..."), ("...",))[0]
True
>>> isequal(("...", "A", "C"), ("B", "C"))[0]
False
Wildcards
Think about this one...
consume_all affects the assignment mapping, not compatibility.
By default, "A" matches only the last "A" token in shape2:
>>> isequal(("A",), ("...", "A"))[0]
True
>>> sorted(isequal(("A",), ("...", "A"))[1].items())
[(0, [1])]
With shape1_consume_all, "A" also absorbs the "..." token:
Similarly, by default only shape1's "A" is assigned:
With shape2_consume_all, shape2's ellipses is assigned too:
Leftover tokens on the non-consuming side are not assigned:
But with both flags set, shape1's ellipses absorbs everything:
>>> sorted(isequal(("...",), ("...", "A"), shape1_consume_all=True, shape2_consume_all=True)[1].items())
[(0, [0, 1])]
Source code in src/torchlinops/nameddim/_matching.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
torchlinops.nameddim.iscompatible
Whether the two shapes are length-compatible.
Greedily consume shape1, but not shape2
iscompatible(("A","B"), ("C", "D"))[0] True iscompatible(("...",), ("A","B"))[0] True iscompatible(("B","..."), ("A","..."))[0] True iscompatible(("...",), tuple())[0] True iscompatible(("C", "C"), ("D", "E"))[0] True
Source code in src/torchlinops/nameddim/_matching.py
torchlinops.nameddim.partition
Split a sequence on the first occurence of some value
Examples: