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

[Bug] [lang] Fix error when subscripting a dict #1950

Merged
merged 3 commits into from
Oct 14, 2020

Conversation

archibate
Copy link
Collaborator

@archibate archibate commented Oct 14, 2020

Related issue = #

A collection of several random yet trivial fixes.

[Click here for the format server]


import taichi as ti

x = dict(a=1, b=2)

@ti.kernel
def func():
    ti.static_print(x['a'])

func()

[Taichi] mode=release
[Taichi] version 0.6.40, llvm 10.0.0, commit 78231814, win, python 3.8.5
[Taichi] materializing...
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-95603a473205> in <module>
      7     ti.static_print(x['a'])
      8 
----> 9 func()

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py in wrapped(*args, **kwargs)
    572         def wrapped(*args, **kwargs):
    573             _taichi_skip_traceback = 1
--> 574             return primal(*args, **kwargs)
    575 
    576         wrapped.grad = adjoint

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\shell.py in new_call(*args, **kwargs)
     34     def new_call(*args, **kwargs):
     35         _taichi_skip_traceback = 1
---> 36         ret = old_call(*args, **kwargs)
     37         # print's in kernel won't take effect until ti.sync(), discussion:
     38         # https://github.com/taichi-dev/taichi/pull/1303#discussion_r444897102

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py in __call__(self, *args, **kwargs)
    499         instance_id, arg_features = self.mapper.lookup(args)
    500         key = (self.func, instance_id)
--> 501         self.materialize(key=key, args=args, arg_features=arg_features)
    502         return self.compiled_functions[key](*args)
    503 

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py in materialize(self, key, args, arg_features)
    368             self.runtime.inside_kernel = False
    369 
--> 370         taichi_kernel = taichi_kernel.define(taichi_ast_generator)
    371 
    372         assert key not in self.compiled_functions

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\kernel.py in taichi_ast_generator()
    365                 )
    366             self.runtime.inside_kernel = True
--> 367             compiled()
    368             self.runtime.inside_kernel = False
    369 

<ipython-input-1-95603a473205> in func()
      5 @ti.kernel
      6 def func():
----> 7     ti.static_print(x['a'])
      8 
      9 func()

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\util.py in wrapped(*args, **kwargs)
    197         assert in_taichi_scope(), \
    198                 f'{func.__name__} cannot be called in Python-scope'
--> 199         return func(*args, **kwargs)
    200 
    201     return wrapped

~\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\lang\impl.py in subscript(value, *indices)
    128         return Expr(taichi_lang_core.subscript(value.ptr, indices_expr_group))
    129     else:
--> 130         return value[indices]
    131 
    132 

KeyError: ('a',)

ti.ndrange, ti.GroupedNDRange)) or x is None:
return x
elif isinstance(x, ti.lang.expr.Expr) and x.ptr.is_global_var():
ti.ndrange, ti.GroupedNDRange, zip, filter, map)) or x is None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OFT further extend static-for possibilities.

python/taichi/lang/impl.py Show resolved Hide resolved
@archibate archibate requested review from yuanming-hu, k-ye and taichi-gardener and removed request for yuanming-hu October 14, 2020 09:18
@codecov
Copy link

codecov bot commented Oct 14, 2020

Codecov Report

Merging #1950 into master will increase coverage by 0.84%.
The diff coverage is 45.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1950      +/-   ##
==========================================
+ Coverage   42.80%   43.64%   +0.84%     
==========================================
  Files          45       45              
  Lines        6413     6225     -188     
  Branches     1101     1106       +5     
==========================================
- Hits         2745     2717      -28     
+ Misses       3498     3334     -164     
- Partials      170      174       +4     
Impacted Files Coverage Δ
python/taichi/lang/ndrange.py 73.52% <0.00%> (ø)
python/taichi/misc/gui.py 8.89% <16.66%> (ø)
python/taichi/lang/impl.py 66.75% <100.00%> (ø)
...ld/taichi-dev/taichi/python/taichi/tools/np2ply.py
.../taichi-dev/taichi/python/taichi/lang/exception.py
...uild/taichi-dev/taichi/python/taichi/misc/error.py
...avis/build/taichi-dev/taichi/python/taichi/main.py
...ild/taichi-dev/taichi/python/taichi/lang/kernel.py
...build/taichi-dev/taichi/python/taichi/lang/expr.py
...d/taichi-dev/taichi/python/taichi/core/__init__.py
... and 83 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fa0d60...4148044. Read the comment docs.

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.

LGTM!

python/taichi/lang/ndrange.py Show resolved Hide resolved
@archibate archibate added the LGTM label Oct 14, 2020
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Cool! LGTM. Thank you!

python/taichi/lang/ndrange.py Show resolved Hide resolved
@yuanming-hu yuanming-hu merged commit 45f52a9 into taichi-dev:master Oct 14, 2020
@yuanming-hu yuanming-hu mentioned this pull request Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants