Commit 0b96757
Prevent union-finding cycles for shared qspecs (#3011)
* Prevent union-finding cycles for shared qspecs
Certain graphs caused infinite recursion when unioning nodes to groups
based on shared quantization specs while implicit sharing was enabled.
The problem occurred when `NodeOrEdge` A with its own `QuantizationSpec`
received an edge (in `shared_with_map`) to `EdgeOrNode` B which in turn
had a `SharedQuantizationSpec` pointing back to A.
Remedy this problem by checking if B, from the scenario above, has a
`SharedQuantizationSpec` pointing to A; if that is the case, don't union
them together by letting A point back to B. Avoiding the union/cycle
preserves correctness because the nodes are effectively already united.
* Add test case of implicit sharing with two ops sharing one input
- Add test case of implicit sharing for a model where one input is
shared between two different ops.
- Add code comments to `_union_if_no_cycle`
* Simplify the model that reproduces the recursion bug
* Avoid forming a cycle by reversing the edge
* Add context of recursion bug in the test case
* Swap order of branches in if statement
---------
Co-authored-by: Martin Lindström <[email protected]>1 parent a53a4db commit 0b96757
File tree
2 files changed
+127
-9
lines changed- test/quantization/pt2e
- torchao/quantization/pt2e
2 files changed
+127
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
1215 | 1216 | | |
1216 | 1217 | | |
1217 | 1218 | | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
1218 | 1321 | | |
1219 | 1322 | | |
1220 | 1323 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| 150 | + | |
149 | 151 | | |
150 | 152 | | |
151 | 153 | | |
152 | 154 | | |
153 | | - | |
154 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
155 | 168 | | |
156 | 169 | | |
157 | 170 | | |
158 | 171 | | |
159 | 172 | | |
160 | 173 | | |
| 174 | + | |
161 | 175 | | |
162 | 176 | | |
163 | 177 | | |
| |||
167 | 181 | | |
168 | 182 | | |
169 | 183 | | |
170 | | - | |
| 184 | + | |
171 | 185 | | |
172 | 186 | | |
173 | 187 | | |
| |||
249 | 263 | | |
250 | 264 | | |
251 | 265 | | |
252 | | - | |
| 266 | + | |
253 | 267 | | |
254 | 268 | | |
255 | 269 | | |
| |||
311 | 325 | | |
312 | 326 | | |
313 | 327 | | |
314 | | - | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
315 | 331 | | |
316 | 332 | | |
317 | 333 | | |
| |||
332 | 348 | | |
333 | 349 | | |
334 | 350 | | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | 351 | | |
339 | 352 | | |
340 | 353 | | |
| |||
363 | 376 | | |
364 | 377 | | |
365 | 378 | | |
366 | | - | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
367 | 382 | | |
368 | 383 | | |
369 | 384 | | |
| |||
0 commit comments