Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Error] Show pretty & intuitive stack traceback when ti.enable_excepthook() used #1405

Merged

Conversation

archibate
Copy link
Collaborator

Related issue = #

[Click here for the format server]


Didn't add ti.init(excepthook=True) yet to prevent conflict with #1396.
@k-ye Could you double check that my changes doesn't break your cool ODOP functions? I didn't dear to use a decorator at all..

@archibate
Copy link
Collaborator Author

import taichi as ti

@ti.func
def func3():
    ti.static_assert(1 + 1 == 3)

@ti.func
def func2():
    func3()

@ti.func
def func1():
    func2()

@ti.kernel
def func0():
    func1()

ti.enable_excepthook()
func0()

Before:

[Taichi] mode=development
[Taichi] preparing sandbox at /tmp/taichi-4pwjc_r7
[Taichi] <dev mode>, llvm 10.0.0, commit c24d6da8, python 3.8.3
Traceback (most recent call last):
  File "misc/w.py", line 20, in <module>
    func0()
  File "/root/taichi/python/taichi/lang/kernel.py", line 553, in wrapped
    return primal(*args, **kwargs)
  File "/root/taichi/python/taichi/lang/kernel.py", line 483, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "/root/taichi/python/taichi/lang/kernel.py", line 363, in materialize
    taichi_kernel = taichi_kernel.define(taichi_ast_generator)
  File "/root/taichi/python/taichi/lang/kernel.py", line 360, in taichi_ast_generator
    compiled()
  File "misc/w.py", line 17, in func0
    func1()
  File "/root/taichi/python/taichi/lang/kernel.py", line 38, in decorated
    return fun.__call__(*args)
  File "/root/taichi/python/taichi/lang/kernel.py", line 77, in __call__
    ret = self.compiled(*args)
  File "misc/w.py", line 13, in func1
    func2()
  File "/root/taichi/python/taichi/lang/kernel.py", line 38, in decorated
    return fun.__call__(*args)
  File "/root/taichi/python/taichi/lang/kernel.py", line 77, in __call__
    ret = self.compiled(*args)
  File "misc/w.py", line 9, in func2
    func3()
  File "/root/taichi/python/taichi/lang/kernel.py", line 38, in decorated
    return fun.__call__(*args)
  File "/root/taichi/python/taichi/lang/kernel.py", line 77, in __call__
    ret = self.compiled(*args)
  File "misc/w.py", line 5, in func3
    ti.static_assert(1 + 1 == 3)
  File "/root/taichi/python/taichi/lang/impl.py", line 249, in static_assert
    assert cond
AssertionError

After:

[Taichi] mode=development
[Taichi] preparing sandbox at /tmp/taichi-0x77_x75
[Taichi] <dev mode>, llvm 10.0.0, commit c24d6da8, python 3.8.3
========== Taichi Stack Traceback ==========
In <module>() at misc/w.py:20:
--------------------------------------------
def func0():
    func1()
ti.enable_excepthook()
func0()  <--
--------------------------------------------
In func0() at misc/w.py:17:
--------------------------------------------
    func2()
@ti.kernel
def func0():
    func1()  <--
ti.enable_excepthook()
--------------------------------------------
In func1() at misc/w.py:13:
--------------------------------------------
    func3()
@ti.func
def func1():
    func2()  <--
@ti.kernel
--------------------------------------------
In func2() at misc/w.py:9:
--------------------------------------------
    ti.static_assert(1 + 1 == 3)
@ti.func
def func2():
    func3()  <--
@ti.func
--------------------------------------------
In func3() at misc/w.py:5:
--------------------------------------------
import taichi as ti
@ti.func
def func3():
    ti.static_assert(1 + 1 == 3)  <--
@ti.func
--------------------------------------------
AssertionError

and, with colors!

@archibate archibate requested a review from rexwangcc July 4, 2020 18:04
@archibate archibate added the python Python engineering related label Jul 4, 2020
@codecov

This comment has been minimized.

Copy link
Member

@k-ye k-ye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm not super familiar with the stack manipulation in Python, but the handler code LGTM.

Could you double check that my changes doesn't break your cool ODOP functions?

Thanks.. so long as ti.func, ti.kernel is not decorated with no_traceback, and the tests passed, we should be fine?

docs/debugging.rst Outdated Show resolved Hide resolved
docs/debugging.rst Outdated Show resolved Hide resolved
docs/debugging.rst Outdated Show resolved Hide resolved
docs/debugging.rst Outdated Show resolved Hide resolved
docs/debugging.rst Outdated Show resolved Hide resolved
python/taichi/lang/error.py Show resolved Hide resolved
print(
f'In {Fore.LIGHTYELLOW_EX}{name}{Fore.RESET}() at {Fore.LIGHTMAGENTA_EX}{filename}{Fore.RESET}:{Fore.LIGHTCYAN_EX}{lineno}{Fore.RESET}:\n{bar}'
)
with open(filename) as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opt (maybe another PR, this one is not trivial already..): maybe we can cache the file content?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ultimate solution is import inspect..

Co-authored-by: Ye Kuang <k-ye@users.noreply.github.com>
@archibate archibate added the LGTM label Jul 6, 2020
@archibate archibate merged commit ea46275 into taichi-dev:master Jul 6, 2020
@FantasyVR FantasyVR mentioned this pull request Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Python engineering related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants