[Inductor] Lift the maximum depth of the Python interpreter stack to adapt large/deep models (#87130)

Partly fixes https://github.com/pytorch/torchdynamo/issues/1693

Pull Request resolved: https://github.com/pytorch/pytorch/pull/87130
Approved by: https://github.com/jansel
diff --git a/torch/_inductor/compile_fx.py b/torch/_inductor/compile_fx.py
index 46224d7..2243f82 100644
--- a/torch/_inductor/compile_fx.py
+++ b/torch/_inductor/compile_fx.py
@@ -2,6 +2,7 @@
 import functools
 import itertools
 import logging
+import sys
 from typing import List
 
 import functorch
@@ -89,6 +90,10 @@
     if dynamo_utils.count_calls(gm.graph) == 0:
         return make_boxed_func(gm.forward)
 
+    # lift the maximum depth of the Python interpreter stack
+    # to adapt large/deep models
+    sys.setrecursionlimit(max(sys.getrecursionlimit(), 2000))
+
     _step_logger()(
         logging.INFO,
         "torchinductor compiling "