Get the current stacktrace (between trace() and __torch_dispatch__()) Include the filename, function name, line number, and source code from the start of the function to the given instruction. Return: A list of stacktraces for each instruction along with the source code
()
| 67 | |
| 68 | |
| 69 | def get_stacktrace() -> List[Dict[str, str]]: |
| 70 | """ |
| 71 | Get the current stacktrace (between trace() and __torch_dispatch__()) |
| 72 | Include the filename, function name, line number, and source code from the |
| 73 | start of the function to the given instruction. |
| 74 | |
| 75 | Return: |
| 76 | A list of stacktraces for each instruction along with the source code |
| 77 | context surrounding each instruction |
| 78 | """ |
| 79 | |
| 80 | stacktrace = traceback.extract_stack() |
| 81 | |
| 82 | # The stacktrace typically looks like this: |
| 83 | # |
| 84 | # 1. I stack frames from the top level runner (e.g., the |
| 85 | # test suite runner) |
| 86 | # 2. J frames in executorch/exir/tracer.py setting up tracing |
| 87 | # (call this INIT_EXIR) |
| 88 | # 3. K frames in user model code (this is what we want to save!) |
| 89 | # 4. 1 frame in executorch/exir/tracer.py __torch_function__ |
| 90 | # returning to tracer (call this TRACE_EXIR) |
| 91 | # 5. H frames in executorch/exir/tracer.py AND torch/_tensor.py |
| 92 | # doing all of the internal tracer handling |
| 93 | # |
| 94 | # The PyE tests assert that executorch/exir/tracer.py never shows |
| 95 | # up in the user provided stack traces, so we must oblige them. |
| 96 | # |
| 97 | # Assumptions: |
| 98 | # - Reentrant tracing is not a thing. Thus, the first time |
| 99 | # executorch/exir/tracer.py shows up in the trace, we know |
| 100 | # THAT is the point at which we start tracing. (An alternative |
| 101 | # is that the tracer entry point could record the stack trace |
| 102 | # at this time, but I didn't do this.) |
| 103 | # |
| 104 | # Our plan is to do a miniature stack machine traversing these |
| 105 | # stack machines. |
| 106 | |
| 107 | # Remove parts before the trace function and parts after entering |
| 108 | # __torch_dispatch__. Defaults to returning the entire stack trace. |
| 109 | init_exir_end = 0 |
| 110 | trace_exir_start = None |
| 111 | # A miniature state machine, referring to the frame segments described |
| 112 | # above. The locations are closed-open interval. |
| 113 | FIND_INIT_EXIR_START, FIND_INIT_EXIR_END, FIND_TRACE_EXIR_START = range(3) |
| 114 | state = FIND_INIT_EXIR_START |
| 115 | for i, frame in enumerate(stacktrace): |
| 116 | if state == FIND_INIT_EXIR_START: |
| 117 | if "executorch/exir/tracer.py" in frame.filename: |
| 118 | state = FIND_INIT_EXIR_END |
| 119 | elif state == FIND_INIT_EXIR_END: |
| 120 | if "executorch/exir/tracer.py" not in frame.filename: |
| 121 | init_exir_end = i |
| 122 | state = FIND_TRACE_EXIR_START |
| 123 | elif state == FIND_TRACE_EXIR_START: |
| 124 | if "executorch/exir/tracer.py" in frame.filename: |
| 125 | trace_exir_start = i |
| 126 | break |
no test coverage detected