From d37e52d43cec4e3044c4a986f5be2a2553b4e1e1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fran=C3=A7ois=20Fleuret?= Date: Mon, 28 Nov 2022 07:15:14 +0100 Subject: [PATCH] Initial commit --- stack.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 stack.py diff --git a/stack.py b/stack.py new file mode 100755 index 0000000..27de204 --- /dev/null +++ b/stack.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +# Written by Francois Fleuret + +from torch import is_tensor + +import sys + +def exception_hook(exc_type, exc_value, tb): + + tb = tb.tb_next + + while tb: + #x=tb.tb_frame.f_code + # for field in dir(x): + # print(f'@@@ {field} {getattr(x, field)}') + + filename = tb.tb_frame.f_code.co_filename + name = tb.tb_frame.f_code.co_name + line_no = tb.tb_lineno + print(f' File "{filename}", line {line_no}, in {name}') + print(open(filename, 'r').readlines()[line_no-1], end='') + + local_vars = tb.tb_frame.f_locals + + for n,v in local_vars.items(): + if is_tensor(v): + print(f' {n} -> {v.size()}:{v.dtype}:{v.device}') + else: + print(f' {n} -> {v}') + + tb = tb.tb_next + + print(f'{exc_type.__name__}: {exc_value}') + +sys.excepthook = exception_hook + +###################################################################### + +if __name__ == '__main__': + + import torch + + def dummy(a,b): + print(a@b) + + def blah(a,b): + c=b+b + dummy(a,c) + + m=torch.randn(2,3) + x=torch.randn(3) + blah(m,x) + blah(x,m) + -- 2.39.5