Skip to content

Commit c868152

Browse files
committed
addressing review comments
1 parent 2bfc381 commit c868152

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

python/src/coreai_models/export/mlir_ops.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,19 @@ def _replace_cache_update_autofuncs(
287287
getitem_by_idx: dict[int, fx.Node] = {}
288288
for user in autofunc_node.users:
289289
if user.target is operator.getitem:
290-
getitem_by_idx[user.args[1]] = user
290+
idx = user.args[1]
291+
assert idx not in getitem_by_idx, (
292+
f"{autofunc_node.name} has multiple getitem users at index {idx} "
293+
f"({getitem_by_idx[idx].name}, {user.name}); expected one per index."
294+
)
295+
getitem_by_idx[idx] = user
291296

292297
getitem_fetched = getitem_by_idx.get(0) # 4D fetched slice -> SDPA
293298
getitem_cache = getitem_by_idx.get(1) # 5D mutated cache -> handle
299+
assert getitem_fetched is not None and getitem_cache is not None, (
300+
f"{autofunc_node.name}: expected getitem indices {{0, 1}} for "
301+
f"(fetched slice, mutated cache); found {sorted(getitem_by_idx)}."
302+
)
294303

295304
with graph.inserting_before(autofunc_node):
296305
update = autofunc_node.kwargs["update"]
@@ -313,8 +322,7 @@ def _replace_cache_update_autofuncs(
313322
autofunc_node.kwargs["end"],
314323
)
315324
# 5D meta comes from the mutated-cache getitem.
316-
if getitem_cache is not None:
317-
_copy_node_provenance(isu_node, getitem_cache)
325+
_copy_node_provenance(isu_node, getitem_cache)
318326

319327
# narrow(0, layer_idx, 1) -> [narrow(seq_dim, 0, seq_len)] -> squeeze(0).
320328
# The seq narrow is skipped when seq_len is None.
@@ -353,18 +361,15 @@ def _replace_cache_update_autofuncs(
353361
args=(pre_squeeze, [0]),
354362
)
355363
# 4D meta comes from the fetched-slice getitem (what SDPA expects).
356-
if getitem_fetched is not None:
357-
_copy_node_provenance(squeeze_op, getitem_fetched)
358-
359-
if getitem_fetched is not None:
360-
getitem_fetched.replace_all_uses_with(squeeze_op)
361-
get_items.append(getitem_fetched)
362-
get_item_replacements[getitem_fetched.name] = squeeze_op
363-
364-
if getitem_cache is not None:
365-
getitem_cache.replace_all_uses_with(isu_node)
366-
get_items.append(getitem_cache)
367-
get_item_replacements[getitem_cache.name] = isu_node
364+
_copy_node_provenance(squeeze_op, getitem_fetched)
365+
366+
getitem_fetched.replace_all_uses_with(squeeze_op)
367+
get_items.append(getitem_fetched)
368+
get_item_replacements[getitem_fetched.name] = squeeze_op
369+
370+
getitem_cache.replace_all_uses_with(isu_node)
371+
get_items.append(getitem_cache)
372+
get_item_replacements[getitem_cache.name] = isu_node
368373

369374

370375
def _erase_autofunc_nodes(

0 commit comments

Comments
 (0)