@@ -374,37 +374,45 @@ def _negative_checks(
374374 return checks
375375
376376
377+ _ARGMAX_OPS = (
378+ torch .ops .aten .argmax .default ,
379+ exir_ops .edge .aten .argmax .default ,
380+ )
381+
382+ _TO_DIM_ORDER_COPY_OPS = (
383+ torch .ops .dim_order_ops ._to_dim_order_copy .default ,
384+ exir_ops .edge .dim_order_ops ._to_dim_order_copy .default ,
385+ )
386+
387+
388+ def _argmax_all_users_cast_to_int32 (node : fx .Node ) -> bool :
389+ # TOSA ARGMAX produces int32 indices. This is only a faithful lowering
390+ # when every user immediately narrows aten.argmax's int64 result:
391+ #
392+ # aten.argmax -> _to_dim_order_copy(dtype=int32) -> users
393+ if node .target not in _ARGMAX_OPS or not node .users :
394+ return False
395+
396+ return all (
397+ user .target in _TO_DIM_ORDER_COPY_OPS
398+ and user .kwargs .get ("dtype" ) == torch .int32
399+ for user in node .users
400+ )
401+
402+
377403class CheckKnownUnsupportedTOSASemantics (OperatorSupportBase ):
378404 """Reject ops whose TOSA lowering is known to differ from PyTorch."""
379405
380406 def __init__ (self , reporter : WhyNoPartitionReporter ):
381407 self .reporter = reporter
382408
383- def _argmax_all_users_cast_to_int32 (self , node : fx .Node ) -> bool :
384- # TOSA ARGMAX produces int32 indices. This is only a faithful lowering
385- # when every user immediately narrows aten.argmax's int64 result:
386- #
387- # aten.argmax -> _to_dim_order_copy(dtype=int32) -> users
388- cast_ops = (
389- torch .ops .dim_order_ops ._to_dim_order_copy .default ,
390- exir_ops .edge .dim_order_ops ._to_dim_order_copy .default ,
391- )
392- # A live argmax should normally have users. Treat a no-user node as not
393- # proving the int64 result is intentionally narrowed to int32.
394- if not node .users :
395- return False
396- for user in node .users :
397- if user .target not in cast_ops :
398- return False
399- if user .kwargs .get ("dtype" ) != torch .int32 :
400- return False
401- return True
402-
403409 def _check_argmax (self , node : fx .Node ) -> bool :
404- if self . _argmax_all_users_cast_to_int32 (node ):
410+ if _argmax_all_users_cast_to_int32 (node ):
405411 return True
412+
406413 self .reporter .report_reject (
407- node , "TOSA ARGMAX produces int32 but aten.argmax returns int64."
414+ node ,
415+ "TOSA ARGMAX produces int32 but aten.argmax returns int64." ,
408416 )
409417 return False
410418
@@ -773,17 +781,34 @@ def inside_int32_bounds(self, node: torch.fx.Node) -> bool:
773781 def has_rejected_int64_output (
774782 self , node : torch .fx .Node , tensor_list : Sequence [typing .Any ]
775783 ) -> bool :
776- if node .target in (
777- torch .ops .aten .argmax .default ,
778- exir_ops .edge .aten .argmax .default ,
779- ):
784+ if node .target in _ARGMAX_OPS :
780785 return not self ._is_tosa_argmax_supported (node )
786+
781787 return any (
782788 tensor .dtype == torch .int64
783789 for tensor in tensor_list
784790 if isinstance (tensor , FakeTensor )
785791 )
786792
793+ def _is_argmax_int32_cast (
794+ self ,
795+ node : torch .fx .Node ,
796+ input_node : torch .fx .Node ,
797+ ) -> bool :
798+ if node .target not in _TO_DIM_ORDER_COPY_OPS :
799+ return False
800+
801+ if node .kwargs .get ("dtype" ) != torch .int32 :
802+ return False
803+
804+ if input_node .target not in _ARGMAX_OPS :
805+ return False
806+
807+ if not _argmax_all_users_cast_to_int32 (input_node ):
808+ return False
809+
810+ return self ._is_tosa_argmax_supported (input_node )
811+
787812 def _is_tosa_argmax_dtype_supported (
788813 self , node : torch .fx .Node , input_dtype : torch .dtype
789814 ) -> bool :
@@ -882,6 +907,13 @@ def _check_int64_input_nodes(self, node: torch.fx.Node) -> bool:
882907 tensor_in = get_first_fake_tensor (input_node )
883908 if tensor_in .dtype != torch .int64 :
884909 continue
910+
911+ # aten.argmax is nominally int64, but TOSA ARGMAX produces int32.
912+ # Allow the explicit argmax -> int32 narrowing pattern so both nodes
913+ # can be placed in the same delegate.
914+ if self ._is_argmax_int32_cast (node , input_node ):
915+ continue
916+
885917 # Constant placeholder
886918 if (
887919 input_node .op != "call_function"
0 commit comments