[Profiler] Fix lost C call events problem in Python 3.12.0-3.12.4 (#155446)

Hi team,

Please help review this patch.

This PR https://github.com/pytorch/pytorch/pull/150370 tried to fix the "Empty C Call Queue" problem on Python 3.12. It added C calls for each starting Python event with a callable.

I found the root cause is not that we cannot get C function frames by `PyFrame_GetBack` when PythonTracer is filling start frames, but the c call event loss problem bug on Python 3.12.0-3.12.4. And that problem was fixed by 257c413cd1 on 3.12.5.

So I think the https://github.com/pytorch/pytorch/pull/150370 cannot fix the problem, this patch reverts the change of it.

There are solutions to fix the problem correctly, such as we can add a new monitoring callback to compensate call events of methods with C function or we can override the callback registered by `PyEval_SetProfile`.  These solutions may make the code hard to maintain.

~~Since upgrading the micro version of Python is not difficult for users, we can just ignore C functions and suggest user upgrade.~~

Pull Request resolved: https://github.com/pytorch/pytorch/pull/155446
Approved by: https://github.com/sraikund16, https://github.com/cyyever
This commit is contained in:
Denghui Dong
2025-07-23 20:03:48 +00:00
committed by PyTorch MergeBot
parent c996aff6ed
commit da94023b02
3 changed files with 306 additions and 35 deletions

View File

@ -5764,3 +5764,16 @@ def recover_orig_fp32_precision(fn):
torch.backends.cuda.matmul.fp32_precision = old_cuda_matmul_p
return recover()(fn)
def skipIfPythonVersionMismatch(predicate):
vi = sys.version_info
def dec_fn(fn):
@wraps(fn)
def wrap_fn(self, *args, **kwargs):
if predicate(vi.major, vi.minor, vi.micro):
return fn(self, *args, **kwargs)
else:
raise unittest.SkipTest("Python version mismatch")
return wrap_fn
return dec_fn