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

Re-raised exceptions generates redundant tracebacks in AST transformer #3785

Closed
lin-hitonami opened this issue Dec 13, 2021 · 0 comments · Fixed by #3873
Closed

Re-raised exceptions generates redundant tracebacks in AST transformer #3785

lin-hitonami opened this issue Dec 13, 2021 · 0 comments · Fixed by #3873
Labels
potential bug Something that looks like a bug but not yet confirmed

Comments

@lin-hitonami
Copy link
Contributor

lin-hitonami commented Dec 13, 2021

Describe the bug
The current error reporting mechanism of the AST transformer catches exceptions, adds the tracebacks in taichi program and re-raises the exception again and again until it is thrown outside the transformer. This makes python generate tracebacks to each exception and shows During handling of the above exception, another exception occurred because the program throws exceptions when processing the exception.

This can be solved by addingfrom None to raise TaichiCompilationError(msg). In this way, only the last traceback will be displayed. However, we may want to preserve the initial traceback if the error is raised from outside the AST transformer (for example, a python function outside the transformer or some malfunctioning taichi internal functions) like the following part from the example.

Traceback (most recent call last):
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 353, in build_Call
    node.ptr = node.func.ptr(*args, **keywords)
  File "/home/lin/tmp/test.py", line 4, in baz
    raise RuntimeError("an error")
RuntimeError: an error

To Reproduce

import taichi as ti
ti.init()

def baz():
    raise RuntimeError("an error")

@ti.func
def bar():
    baz()

@ti.kernel
def foo():
    bar()

foo()

Log/Screenshots

[Taichi] version 0.8.8, llvm 10.0.0, commit 6ef37747, linux, python 3.9.7
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 353, in build_Call
    node.ptr = node.func.ptr(*args, **keywords)
  File "/home/lin/tmp/test.py", line 4, in baz
    raise RuntimeError("an error")
RuntimeError: an error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 353, in build_Call
    node.ptr = node.func.ptr(*args, **keywords)
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 58, in decorated
    return fun.__call__(*args)
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 177, in __call__
    return transform_tree(tree, ctx)
  File "/home/lin/taichi2/python/taichi/lang/ast/transform.py", line 9, in transform_tree
    ASTTransformer()(ctx, tree)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 471, in build_Module
    build_stmt(ctx, stmt)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 441, in build_FunctionDef
    build_stmts(ctx, node.body)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 1059, in build_stmts
    build_stmt(ctx, stmt)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 940, in build_Expr
    build_stmt(ctx, node.value)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 32, in __call__
    raise TaichiCompilationError(msg)
taichi.lang.exception.TaichiCompilationError: On line 8 of file "/home/lin/tmp/test.py":
    baz()
    ^^^^^
RuntimeError: an error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lin/tmp/test.py", line 14, in <module>
    print(foo())
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 703, in wrapped
    return primal(*args, **kwargs)
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 631, in __call__
    key = self.ensure_compiled(*args)
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 617, in ensure_compiled
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 463, in materialize
    taichi_kernel = _ti_core.create_kernel(taichi_ast_generator,
  File "/home/lin/taichi2/python/taichi/lang/kernel_impl.py", line 458, in taichi_ast_generator
    transform_tree(tree, ctx)
  File "/home/lin/taichi2/python/taichi/lang/ast/transform.py", line 9, in transform_tree
    ASTTransformer()(ctx, tree)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 471, in build_Module
    build_stmt(ctx, stmt)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 441, in build_FunctionDef
    build_stmts(ctx, node.body)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 1059, in build_stmts
    build_stmt(ctx, stmt)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    raise e
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 23, in __call__
    return method(ctx, node)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer.py", line 940, in build_Expr
    build_stmt(ctx, node.value)
  File "/home/lin/taichi2/python/taichi/lang/ast/ast_transformer_utils.py", line 32, in __call__
    raise TaichiCompilationError(msg)
taichi.lang.exception.TaichiCompilationError: On line 12 of file "/home/lin/tmp/test.py":
    bar()
    ^^^^^
On line 8 of file "/home/lin/tmp/test.py":
    baz()
    ^^^^^
RuntimeError: an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
potential bug Something that looks like a bug but not yet confirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant