@@ -637,6 +637,78 @@ def randn_with_ptpu_complex_cpu_fallback(*args, **kwargs):
637637 setattr (torch , patched_attr , True )
638638
639639
640+ def _patch_torch_abs_long_runtime_error ():
641+ """Run ``torch.abs`` on CPU for PTPU int64 eager helper tensors.
642+
643+ Sunrise's eager ``UNARY_ABS`` kernel raises a plain ``RuntimeError`` for
644+ ``torch.long`` inputs. Keep the workaround outside ``use_gems()`` so it
645+ only covers setup/reference helpers such as flash-attention ALiBi bias
646+ construction and cannot hide a missing FlagGems ``abs`` implementation.
647+ """
648+ patched_attr = "_flag_gems_sunrise_abs_long_runtime_error_patched"
649+ if getattr (torch , patched_attr , False ):
650+ return
651+
652+ original_fn = torch .abs
653+ runtime_marker = "unary_op<ptpu_kernel::UNARY_ABS>"
654+ dtype_marker = "failed to dispatch data type Long"
655+
656+ @functools .wraps (original_fn )
657+ def abs_with_ptpu_long_cpu_fallback (* args , ** kwargs ):
658+ tensor = args [0 ] if args else kwargs .get ("input" )
659+ try :
660+ return original_fn (* args , ** kwargs )
661+ except RuntimeError as exc :
662+ message = str (exc )
663+ if (
664+ _flag_gems_use_gems_active ()
665+ or not _is_ptpu_tensor (tensor )
666+ or tensor .dtype != torch .long
667+ or runtime_marker not in message
668+ or dtype_marker not in message
669+ ):
670+ raise
671+ return _torch_function_cpu_fallback (tensor , args , kwargs , original_fn )
672+
673+ torch .abs = abs_with_ptpu_long_cpu_fallback
674+ setattr (torch , patched_attr , True )
675+
676+
677+ def _patch_torch_all_keepdim_runtime_error ():
678+ """Run unsupported PTPU bool ``torch.all(..., keepdim=True)`` on CPU.
679+
680+ Sunrise eager reduction raises a plain ``RuntimeError`` for this form.
681+ Limit the fallback to the exact runtime message and reference/setup code
682+ outside ``use_gems()`` so FlagGems' real ``all`` kernel remains visible.
683+ """
684+ patched_attr = "_flag_gems_sunrise_all_keepdim_runtime_error_patched"
685+ if getattr (torch , patched_attr , False ):
686+ return
687+
688+ original_fn = torch .all
689+ runtime_marker = "all_out with keepdim true is not implemented yet."
690+
691+ @functools .wraps (original_fn )
692+ def all_with_ptpu_keepdim_cpu_fallback (* args , ** kwargs ):
693+ tensor = args [0 ] if args else kwargs .get ("input" )
694+ keepdim = args [2 ] if len (args ) > 2 else kwargs .get ("keepdim" , False )
695+ try :
696+ return original_fn (* args , ** kwargs )
697+ except RuntimeError as exc :
698+ if (
699+ _flag_gems_use_gems_active ()
700+ or not _is_ptpu_tensor (tensor )
701+ or tensor .dtype != torch .bool
702+ or keepdim is not True
703+ or runtime_marker not in str (exc )
704+ ):
705+ raise
706+ return _torch_function_cpu_fallback (tensor , args , kwargs , original_fn )
707+
708+ torch .all = all_with_ptpu_keepdim_cpu_fallback
709+ setattr (torch , patched_attr , True )
710+
711+
640712def _patch_torch_cudnn_convolution ():
641713 """Run `torch.cudnn_convolution(...)` on CPU via `F.conv{1,2,3}d` for PTPU.
642714
@@ -3365,6 +3437,7 @@ def apply_sunrise_monkey_patches():
33653437 _patch_torch_function ("logsumexp" , "aten::amax.out" )
33663438 _patch_tensor_method ("mean" , "aten::mean" )
33673439 _patch_torch_function ("mean" , "aten::mean" )
3440+ _patch_torch_all_keepdim_runtime_error ()
33683441 _patch_torch_function ("norm" , "aten::linalg_vector_norm.out" )
33693442 _patch_torch_linalg_function ("vector_norm" , "aten::linalg_vector_norm.out" )
33703443 _patch_torch_linalg_function ("qr" , "aten::linalg_qr.out" )
@@ -3403,6 +3476,7 @@ def apply_sunrise_monkey_patches():
34033476 _patch_torch_nn_functional ("logsigmoid" , "aten::log_sigmoid_forward" )
34043477 _patch_torch_nn_functional_one_hot_cpu_reference ()
34053478 _patch_torch_randn_complex_dtype ()
3479+ _patch_torch_abs_long_runtime_error ()
34063480 _patch_torch_cudnn_convolution ()
34073481 _patch_conv_depthwise2d_cpu_reference ()
34083482 _patch_thnn_fused_lstm_cell_cpu_reference ()
0 commit comments