Commit af8d2c4
committed
fix(klu): keep topology indices in numpy so nested sax.circuit traces cleanly
When a SAX model composes a sub-circuit via ``sax.circuit(...)`` inside
its own body and the outer model is itself wrapped in another JAX trace
(``jax.jit``, ``jax.jacfwd``, or any external simulator that traces SAX
models for Jacobian assembly), ``analyze_circuit_klu`` raised
``NonConcreteBooleanIndexError`` at:
CSi = jnp.broadcast_to(Ci[None, :], match_2d.shape)[match_2d]
The boolean fancy-index requires ``match_2d`` to be concrete, but it
became a tracer because ``Si``/``Sj`` came from ``sax.scoo(instance)``
which turned them into tracers under the outer trace.
Index arrays Si / Sj / Ci / Cj are pure netlist topology (which (i, j)
positions of each model's S-matrix are non-zero, plus the connection
endpoints) — they never depend on traced parameters. Force them to
numpy *inside the klu backend only* — ``_scoo_with_numpy_indices``
extracts topology directly from the SDict's keys before SCoo wrapping
turns them into tracers, and the rest of ``analyze_circuit_klu`` then
runs concretely. ``sax.scoo`` and the rest of SAX continue to return
jnp indices, so other backends are unaffected.
Repro::
def coupler_with_sbends(*, wl=1.55, length=10.0, kappa=0.5):
sub, _ = sax.circuit(
netlist={
"instances": {"s1": "st", "s2": "st", "dc": "cp"},
"connections": {"s1,o2": "dc,o1", "s2,o2": "dc,o2"},
"ports": {"o1": "s1,o1", "o2": "s2,o1",
"o3": "dc,o3", "o4": "dc,o4"},
},
models={"st": straight, "cp": coupler_4port},
)
return sub(wl=wl, s1={"length": length / 2}, s2={"length": length / 2},
dc={"kappa": kappa})
jax.jit(coupler_with_sbends)(wl=1.55)
# before: NonConcreteBooleanIndexError
jax.jacfwd(lambda w: coupler_with_sbends(wl=w)[("o1","o3")].real)(1.55)
# before: NonConcreteBooleanIndexError
All 113 existing SAX tests pass.1 parent b4a9eae commit af8d2c4
1 file changed
Lines changed: 54 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
59 | 69 | | |
60 | 70 | | |
61 | 71 | | |
62 | 72 | | |
63 | 73 | | |
64 | 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 | + | |
65 | 99 | | |
66 | 100 | | |
67 | 101 | | |
| |||
110 | 144 | | |
111 | 145 | | |
112 | 146 | | |
113 | | - | |
114 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
115 | 154 | | |
116 | 155 | | |
117 | 156 | | |
| |||
120 | 159 | | |
121 | 160 | | |
122 | 161 | | |
123 | | - | |
124 | | - | |
| 162 | + | |
| 163 | + | |
125 | 164 | | |
126 | 165 | | |
127 | 166 | | |
| |||
130 | 169 | | |
131 | 170 | | |
132 | 171 | | |
| 172 | + | |
133 | 173 | | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
142 | 183 | | |
143 | 184 | | |
144 | 185 | | |
| |||
0 commit comments