From 11a5cc3719c4b47533f27dd0e37a46b83b3c26e9 Mon Sep 17 00:00:00 2001 From: Huang Yuyao Date: Fri, 19 Apr 2024 03:14:12 +0800 Subject: [PATCH] not successfully configured for windows --- README.md | 0 conda/env.yml | Bin 0 -> 320 bytes dev_env.sh | 1 + tests/test_torch.py | 37 ++++++++++++++++++++++++++ trace_commentor/__init__.py | 0 trace_commentor/interpretor/README.md | 15 +++++++++++ trace_commentor/parser.py | 32 ++++++++++++++++++++++ 7 files changed, 85 insertions(+) create mode 100644 README.md create mode 100644 conda/env.yml create mode 100644 dev_env.sh create mode 100644 tests/test_torch.py create mode 100644 trace_commentor/__init__.py create mode 100644 trace_commentor/interpretor/README.md create mode 100644 trace_commentor/parser.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/conda/env.yml b/conda/env.yml new file mode 100644 index 0000000000000000000000000000000000000000..344a37a6eefe209c47adb5323922062107259978 GIT binary patch literal 320 zcmZ9GQ3}E^5Jcx&@D9Czg7_zRnA%nvsIk~Y@bc>0)Y3$j4avNn*?it!wQlNkQl+3q zTWvUdR8MspG{H=*s2$y+Uf~fIpvFuaX`gPM1jp;&IqHR@*PXeuj@s)$<=FxD`0bBm tIo>&M*b)M$tuBRDrzuOZgfAr+FX#XO literal 0 HcmV?d00001 diff --git a/dev_env.sh b/dev_env.sh new file mode 100644 index 0000000..5b7e7d2 --- /dev/null +++ b/dev_env.sh @@ -0,0 +1 @@ +export PYTHONPATH=. diff --git a/tests/test_torch.py b/tests/test_torch.py new file mode 100644 index 0000000..744ce3f --- /dev/null +++ b/tests/test_torch.py @@ -0,0 +1,37 @@ +import torch +import torch.nn as nn + +from trace_commentor.parser import * + + +def test_func_no_arg(): + + @analyse + def target(): + + x = torch.ones(4, 5) + for i in range(3): + x = x[..., None, :] + + a = torch.randn(309, 110, 3)[:100] + f = nn.Linear(3, 128) + b = f(a.reshape(-1, 3)).reshape(309, 110, 128) + c = torch.concat((a, b), dim=-1) + + return c.flatten() + + print() + target() + + +def test_for_loop(): + + @analyse + def target(): + a = 1 + for i in range(3): + a += 1 + print(a) + + print() + target() diff --git a/trace_commentor/__init__.py b/trace_commentor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/trace_commentor/interpretor/README.md b/trace_commentor/interpretor/README.md new file mode 100644 index 0000000..2cd5cfc --- /dev/null +++ b/trace_commentor/interpretor/README.md @@ -0,0 +1,15 @@ +https://docs.python.org/3/library/ast.html + +- [ ] Literals + - [ ] Constant +- [ ] Variables +- [ ] Expressions + - [ ] Subscripting + - [ ] Comprehensions +- [ ] Statements + - [ ] Imports +- [ ] Control flow +- [ ] Pattern matching +- [ ] Type parameters +- [ ] Function and class definitions +- [ ] Async and await diff --git a/trace_commentor/parser.py b/trace_commentor/parser.py new file mode 100644 index 0000000..a8ad6a4 --- /dev/null +++ b/trace_commentor/parser.py @@ -0,0 +1,32 @@ +import inspect +import ast +import astor + +# from .interpretor import exec_ast + + +class Parser(object): + + + def __init__(self) -> None: + pass + + def __call__(self, func, **_globals): + + raw_lines, start_lineno = inspect.getsourcelines(func) + indent_size = len(raw_lines[0]) - len(raw_lines[0].lstrip()) + unindented_source = ''.join([l[indent_size:] for l in raw_lines]) + root = ast.parse(unindented_source).body[0] + + def proxy_func(*args, **kwargs): + _locals = kwargs + # exec_ast(root, _locals, _globals) + import ipdb; ipdb.set_trace() + root = ast.parse(unindented_source).body[0] + ast.dump(root) + ... + + return proxy_func + + +analyse = Parser()