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

[pyc] use pyc file instead py file is not working #2217

Closed
WangWei90 opened this issue Mar 17, 2021 · 9 comments
Closed

[pyc] use pyc file instead py file is not working #2217

WangWei90 opened this issue Mar 17, 2021 · 9 comments
Labels
potential bug Something that looks like a bug but not yet confirmed

Comments

@WangWei90
Copy link

Describe the bug
Thank you for creating taichi language. I want to know how to use pyc files in computation.

To Reproduce
Consider 3 files below.

# hello.py
from core import *

print(add(1,2))

print(six_times_big(10))
# core.py
import taichi as ti


def add(a, b):
    return a+b


@ti.func
def triple(a):
    return a * 3


@ti.kernel
def six_times_big(a:float) -> float:
    temp = triple(a)
    return temp * 2
# compile.py
import py_compile
for i in ["hello.py", "core.py"]:
    py_compile.compile(i)

Use python3 compile.py and rename files in __pycache__ to hello.pyc and core.pyc then run python3 hello.pyc, this will work if using module numpy but not module taichi.

Log/Screenshots
Please post the full log of the program (instead of just a few lines around the error message, unless the log is > 1000 lines). This will help us diagnose what's happening. For example:

$ python my_sample_code.py
[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-fcvyqhf8
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8
3
[Taichi] materializing...
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 150, in our_findsource
    return inspect._si_old_findsource(object)
  File "/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 798, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 96, in blender_findsourcetext
    import bpy
ModuleNotFoundError: No module named 'bpy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 153, in our_findsource
    return blender_findsource(object)
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 110, in blender_findsource
    lines, text_name = blender_findsourcetext(object)
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 98, in blender_findsourcetext
    raise IOError('Not in Blender environment!')
OSError: Not in Blender environment!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 38, in remote_findsource
    assert module[0] == '<' and module[-1] == '>' or module in ['__console__', '__main__']
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 156, in our_findsource
    return remote_findsource(object)
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 40, in remote_findsource
    raise IOError(f'Object `{repr(object)}` does not come from console')
OSError: Object `<function six_times_big at 0x1329a3310>` does not come from console

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 159, in our_findsource
    return dill_findsource(object)
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 145, in dill_findsource
    return dill.source.findsource(object)
  File "/usr/local/lib/python3.8/site-packages/dill/source.py", line 154, in findsource
    raise IOError('could not extract source code')
OSError: could not extract source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "hello.py", line 5, in <module>
  File "/usr/local/lib/python3.8/site-packages/taichi/lang/kernel.py", line 571, in wrapped
    return primal(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/taichi/lang/kernel.py", line 499, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "/usr/local/lib/python3.8/site-packages/taichi/lang/kernel.py", line 311, in materialize
    src = remove_indent(oinspect.getsource(self.func))
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 185, in getsource
    return inspect.getsource(object)
  File "/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 997, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 979, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/local/lib/python3.8/site-packages/sourceinspect/__init__.py", line 161, in our_findsource
    raise IOError(f'Could not get source code for object: {repr(object)}')
OSError: Could not get source code for object: <function six_times_big at 0x1329a3310>

Additional comments
If possible, please also consider attaching the output of command ti diagnose. This produces the detailed environment information and hopefully helps us diagnose faster.

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-0hhdclqp
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://taichi.rtfd.io/zh_CN/latest
GitHub: https://github.com/taichi-dev/taichi
Forum:  https://forum.taichi.graphics

Taichi system diagnose:

python: 3.8.8 (default, Feb 27 2021, 02:19:17) 
[Clang 12.0.0 (clang-1200.0.32.29)]
system: darwin
executable: /usr/local/opt/python@3.8/bin/python3.8
platform: macOS-11.2.3-x86_64-i386-64bit
architecture: 64bit 
uname: uname_result(system='Darwin', node='appledeMacBook-Pro.local', release='20.3.0', version='Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64', machine='x86_64', processor='i386')
locale: zh_CN.UTF-8
PATH: /Users/apple/Documents/1_gery/2_tool_by_gery:/usr/local/texlive/2020/bin/x86_64-darwin:/Users/apple/.nimble/bin:/Users/apple/.local/bin:/usr/local/opt/coreutils/libexec/gnubin:/Users/apple/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PYTHONPATH: ['/usr/local/bin', '/Users/apple/Documents/1_gery/2_tool_by_gery', '/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/usr/local/Cellar/python@3.8/3.8.8_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages', '/Users/apple/Documents/1_gery/1_coding_zone/3_python_价值投资_极其仰赖/09stackprinter-master', '/usr/local/lib/python3.8/site-packages/taichi/core/../lib']

`lsb_release` not available: [Errno 2] No such file or directory: 'lsb_release'
TAICHI_REPO_DIR=


import: <module 'taichi' from '/usr/local/lib/python3.8/site-packages/taichi/__init__.py'>

cc: False
cpu: True
metal: True
opengl: False
cuda: False

`glewinfo` not available: [Errno 2] No such file or directory: 'glewinfo'

`nvidia-smi` not available: [Errno 2] No such file or directory: 'nvidia-smi'
[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-6v_tgwo0
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-3hhj9d5z
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8
[Taichi] Starting on arch=x64

[W 03/17/21 18:38:50.703] [__init__.py:adaptive_arch_select@589] Arch=[<Arch.opengl: 6>] is not supported, falling back to CPU
[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-7p8g2sww
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8
[Taichi] Starting on arch=x64

[W 03/17/21 18:38:52.431] [__init__.py:adaptive_arch_select@589] Arch=[<Arch.cuda: 4>] is not supported, falling back to CPU
[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-6u47384s
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8
[Taichi] Starting on arch=x64

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/25/2mxhd1b50sbc1_njhz_93jn80000gn/T/taichi-f7hachrp
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, osx, python 3.8.8

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://taichi.rtfd.io/zh_CN/latest
GitHub: https://github.com/taichi-dev/taichi
Forum:  https://forum.taichi.graphics

Running example minimal ...
[Taichi] Starting on arch=x64
[Taichi] materializing...
>>> Running time: 0.28s
42

Consider attaching this log when maintainers ask about system information.
>>> Running time: 19.70s
@WangWei90 WangWei90 added the potential bug Something that looks like a bug but not yet confirmed label Mar 17, 2021
@WangWei90
Copy link
Author

Maybe this issue is related, I do not know.

#1186

I just begin to use taichi, maybe there are some options to find out.

@archibate
Copy link
Collaborator

archibate commented Mar 17, 2021 via email

@WangWei90
Copy link
Author

Thank you for the answer!

Can you show me a minimal process to do that? I want to evaluate the complexity.

@WangWei90
Copy link
Author

In fact, Taichi uses the inspect module to extract the source of function @ti.kernel is decorated, then modify the source using ast module in order to translate Python syntax into Taichi API calls, then re-compile the modifed source into binary code (pyc) and run it.

The print and range inside the taichi scope make me confusing at begin.
If it is possible to alias them to ti.print and ti.range (What should I do).
Maybe I will find more such needing in the future.

@archibate
Copy link
Collaborator

archibate commented Mar 17, 2021 via email

@k-ye
Copy link
Member

k-ye commented Mar 17, 2021

Hi,

The entire python AST transformation is done in https://github.com/taichi-dev/taichi/blob/master/python/taichi/lang/transformer.py. Specifically for your questions:

  1. print:
    if func_name == 'print':
    node.func = self.parse_expr('ti.ti_print')
  2. range for:
    elif isinstance(node.iter, ast.Call) and isinstance(
    node.iter.func, ast.Name) and node.iter.func.id == 'range':
    return self.visit_range_for(node)

As @archibate has pointed out, things that are inside @ti.kernel is completely cut off from Python, and the complexity to make it work in .pyc could be very high. Instead, we should consider AOT compiling the kernels (the issue you have pointed out). Hopefully #2193 will get us closer to AOT a bit :) We are also finding ways to parallelize the JIT compilation to speed up the launch, if that's what motivated you in the first place.

@archibate
Copy link
Collaborator

archibate commented Mar 17, 2021 via email

@WangWei90
Copy link
Author

Initial AST:
def six_times_big(a: float) ->float:
    temp = triple(a)
    return temp * 2

Preprocessed:
def six_times_big():
    import taichi as ti
    ti.decl_scalar_ret(float)
    a = ti.decl_scalar_arg(float)
    temp = ti.expr_init(triple(a))
    ti.core.create_kernel_return(ti.cast(ti.Expr(temp * 2), float).ptr)

Checked:
def six_times_big():
    import taichi as ti
    ti.decl_scalar_ret(float)
    a = ti.decl_scalar_arg(float)
    temp = ti.expr_init(triple(a))
    ti.core.create_kernel_return(ti.cast(ti.Expr(temp * 2), float).ptr)

Final AST:
def six_times_big():
    import taichi as ti
    ti.decl_scalar_ret(float)
    a = ti.decl_scalar_arg(float)
    temp = ti.expr_init(triple(a))
    ti.core.create_kernel_return(ti.cast(ti.Expr(temp * 2), float).ptr)

I think the transform has lost the argument?

@k-ye
Copy link
Member

k-ye commented Mar 17, 2021

I think the transform has lost the argument?

Hi, this is actually intentional. The python source code inside @ti.kernel is only used to generate a series of Taichi's own instructions, which are then JIT compiled to backends like CPU/CUDA. When you call a @ti.kernel function, it's not the original Python function that's been executed. Instead, the decorator finds the JIT'ed function (in the C++ side) and forwards the args to that function. See

def __call__(self, *args, **kwargs):
_taichi_skip_traceback = 1
assert len(kwargs) == 0, 'kwargs not supported for Taichi kernels'
instance_id, arg_features = self.mapper.lookup(args)
key = (self.func, instance_id)
self.materialize(key=key, args=args, arg_features=arg_features)
return self.compiled_functions[key](*args)

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

No branches or pull requests

5 participants
@k-ye @bobcao3 @archibate @WangWei90 and others