gstaichi 0.1.18.dev1__cp310-cp310-macosx_15_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gstaichi-0.1.18.dev1.data/data/include/GLFW/glfw3.h +6389 -0
- gstaichi-0.1.18.dev1.data/data/include/GLFW/glfw3native.h +594 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv-tools/instrument.hpp +268 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv-tools/libspirv.h +907 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv-tools/libspirv.hpp +375 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv-tools/linker.hpp +97 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv-tools/optimizer.hpp +970 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/GLSL.std.450.h +114 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv.h +2568 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv.hpp +2579 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cfg.hpp +168 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_common.hpp +1920 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cpp.hpp +93 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross.hpp +1171 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_c.h +1074 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_containers.hpp +754 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_error_handling.hpp +94 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_parsed_ir.hpp +256 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_cross_util.hpp +37 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_glsl.hpp +1001 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_hlsl.hpp +406 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_msl.hpp +1273 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_parser.hpp +103 -0
- gstaichi-0.1.18.dev1.data/data/include/spirv_cross/spirv_reflect.hpp +91 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget-release.cmake +29 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget.cmake +114 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceConfig.cmake +5 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Config.cmake +3 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3ConfigVersion.cmake +65 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Targets-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/lib/cmake/glfw3/glfw3Targets.cmake +107 -0
- gstaichi-0.1.18.dev1.data/data/lib/libSPIRV-Tools-shared.dylib +0 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig.cmake +106 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig.cmake +106 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig-release.cmake +19 -0
- gstaichi-0.1.18.dev1.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig.cmake +123 -0
- gstaichi-0.1.18.dev1.dist-info/METADATA +108 -0
- gstaichi-0.1.18.dev1.dist-info/RECORD +219 -0
- gstaichi-0.1.18.dev1.dist-info/WHEEL +5 -0
- gstaichi-0.1.18.dev1.dist-info/entry_points.txt +2 -0
- gstaichi-0.1.18.dev1.dist-info/licenses/LICENSE +201 -0
- gstaichi-0.1.18.dev1.dist-info/top_level.txt +1 -0
- taichi/__init__.py +44 -0
- taichi/__main__.py +5 -0
- taichi/_funcs.py +706 -0
- taichi/_kernels.py +420 -0
- taichi/_lib/__init__.py +3 -0
- taichi/_lib/c_api/include/taichi/cpp/taichi.hpp +1401 -0
- taichi/_lib/c_api/include/taichi/taichi.h +29 -0
- taichi/_lib/c_api/include/taichi/taichi_core.h +1111 -0
- taichi/_lib/c_api/include/taichi/taichi_cpu.h +29 -0
- taichi/_lib/c_api/include/taichi/taichi_metal.h +72 -0
- taichi/_lib/c_api/include/taichi/taichi_platform.h +55 -0
- taichi/_lib/c_api/include/taichi/taichi_unity.h +64 -0
- taichi/_lib/c_api/include/taichi/taichi_vulkan.h +151 -0
- taichi/_lib/c_api/lib/libtaichi_c_api.dylib +0 -0
- taichi/_lib/c_api/runtime/libMoltenVK.dylib +0 -0
- taichi/_lib/c_api/runtime/runtime_arm64.bc +0 -0
- taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiConfig.cmake +29 -0
- taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiConfigVersion.cmake +65 -0
- taichi/_lib/c_api/taichi/lib/cmake/taichi/TaichiTargets.cmake +121 -0
- taichi/_lib/core/__init__.py +0 -0
- taichi/_lib/core/py.typed +0 -0
- taichi/_lib/core/taichi_python.cpython-310-darwin.so +0 -0
- taichi/_lib/core/taichi_python.pyi +3077 -0
- taichi/_lib/runtime/libMoltenVK.dylib +0 -0
- taichi/_lib/runtime/runtime_arm64.bc +0 -0
- taichi/_lib/utils.py +249 -0
- taichi/_logging.py +131 -0
- taichi/_main.py +552 -0
- taichi/_snode/__init__.py +5 -0
- taichi/_snode/fields_builder.py +189 -0
- taichi/_snode/snode_tree.py +34 -0
- taichi/_ti_module/__init__.py +3 -0
- taichi/_ti_module/cppgen.py +309 -0
- taichi/_ti_module/module.py +145 -0
- taichi/_version.py +1 -0
- taichi/_version_check.py +100 -0
- taichi/ad/__init__.py +3 -0
- taichi/ad/_ad.py +530 -0
- taichi/algorithms/__init__.py +3 -0
- taichi/algorithms/_algorithms.py +117 -0
- taichi/aot/__init__.py +12 -0
- taichi/aot/_export.py +28 -0
- taichi/aot/conventions/__init__.py +3 -0
- taichi/aot/conventions/gfxruntime140/__init__.py +38 -0
- taichi/aot/conventions/gfxruntime140/dr.py +244 -0
- taichi/aot/conventions/gfxruntime140/sr.py +613 -0
- taichi/aot/module.py +253 -0
- taichi/aot/utils.py +151 -0
- taichi/assets/.git +1 -0
- taichi/assets/Go-Regular.ttf +0 -0
- taichi/assets/static/imgs/ti_gallery.png +0 -0
- taichi/examples/minimal.py +28 -0
- taichi/experimental.py +16 -0
- taichi/graph/__init__.py +3 -0
- taichi/graph/_graph.py +292 -0
- taichi/lang/__init__.py +50 -0
- taichi/lang/_ndarray.py +348 -0
- taichi/lang/_ndrange.py +152 -0
- taichi/lang/_texture.py +172 -0
- taichi/lang/_wrap_inspect.py +189 -0
- taichi/lang/any_array.py +99 -0
- taichi/lang/argpack.py +411 -0
- taichi/lang/ast/__init__.py +5 -0
- taichi/lang/ast/ast_transformer.py +1806 -0
- taichi/lang/ast/ast_transformer_utils.py +328 -0
- taichi/lang/ast/checkers.py +106 -0
- taichi/lang/ast/symbol_resolver.py +57 -0
- taichi/lang/ast/transform.py +9 -0
- taichi/lang/common_ops.py +310 -0
- taichi/lang/exception.py +80 -0
- taichi/lang/expr.py +180 -0
- taichi/lang/field.py +464 -0
- taichi/lang/impl.py +1246 -0
- taichi/lang/kernel_arguments.py +157 -0
- taichi/lang/kernel_impl.py +1415 -0
- taichi/lang/matrix.py +1877 -0
- taichi/lang/matrix_ops.py +341 -0
- taichi/lang/matrix_ops_utils.py +190 -0
- taichi/lang/mesh.py +687 -0
- taichi/lang/misc.py +807 -0
- taichi/lang/ops.py +1489 -0
- taichi/lang/runtime_ops.py +13 -0
- taichi/lang/shell.py +35 -0
- taichi/lang/simt/__init__.py +5 -0
- taichi/lang/simt/block.py +94 -0
- taichi/lang/simt/grid.py +7 -0
- taichi/lang/simt/subgroup.py +191 -0
- taichi/lang/simt/warp.py +96 -0
- taichi/lang/snode.py +487 -0
- taichi/lang/source_builder.py +150 -0
- taichi/lang/struct.py +855 -0
- taichi/lang/util.py +381 -0
- taichi/linalg/__init__.py +8 -0
- taichi/linalg/matrixfree_cg.py +310 -0
- taichi/linalg/sparse_cg.py +59 -0
- taichi/linalg/sparse_matrix.py +303 -0
- taichi/linalg/sparse_solver.py +123 -0
- taichi/math/__init__.py +11 -0
- taichi/math/_complex.py +204 -0
- taichi/math/mathimpl.py +886 -0
- taichi/profiler/__init__.py +6 -0
- taichi/profiler/kernel_metrics.py +260 -0
- taichi/profiler/kernel_profiler.py +592 -0
- taichi/profiler/memory_profiler.py +15 -0
- taichi/profiler/scoped_profiler.py +36 -0
- taichi/shaders/Circles_vk.frag +29 -0
- taichi/shaders/Circles_vk.vert +45 -0
- taichi/shaders/Circles_vk_frag.spv +0 -0
- taichi/shaders/Circles_vk_vert.spv +0 -0
- taichi/shaders/Lines_vk.frag +9 -0
- taichi/shaders/Lines_vk.vert +11 -0
- taichi/shaders/Lines_vk_frag.spv +0 -0
- taichi/shaders/Lines_vk_vert.spv +0 -0
- taichi/shaders/Mesh_vk.frag +71 -0
- taichi/shaders/Mesh_vk.vert +68 -0
- taichi/shaders/Mesh_vk_frag.spv +0 -0
- taichi/shaders/Mesh_vk_vert.spv +0 -0
- taichi/shaders/Particles_vk.frag +95 -0
- taichi/shaders/Particles_vk.vert +73 -0
- taichi/shaders/Particles_vk_frag.spv +0 -0
- taichi/shaders/Particles_vk_vert.spv +0 -0
- taichi/shaders/SceneLines2quad_vk_comp.spv +0 -0
- taichi/shaders/SceneLines_vk.frag +9 -0
- taichi/shaders/SceneLines_vk.vert +12 -0
- taichi/shaders/SceneLines_vk_frag.spv +0 -0
- taichi/shaders/SceneLines_vk_vert.spv +0 -0
- taichi/shaders/SetImage_vk.frag +21 -0
- taichi/shaders/SetImage_vk.vert +15 -0
- taichi/shaders/SetImage_vk_frag.spv +0 -0
- taichi/shaders/SetImage_vk_vert.spv +0 -0
- taichi/shaders/Triangles_vk.frag +16 -0
- taichi/shaders/Triangles_vk.vert +29 -0
- taichi/shaders/Triangles_vk_frag.spv +0 -0
- taichi/shaders/Triangles_vk_vert.spv +0 -0
- taichi/shaders/lines2quad_vk_comp.spv +0 -0
- taichi/sparse/__init__.py +3 -0
- taichi/sparse/_sparse_grid.py +77 -0
- taichi/tools/__init__.py +12 -0
- taichi/tools/diagnose.py +124 -0
- taichi/tools/np2ply.py +364 -0
- taichi/tools/vtk.py +38 -0
- taichi/types/__init__.py +19 -0
- taichi/types/annotations.py +47 -0
- taichi/types/compound_types.py +90 -0
- taichi/types/enums.py +49 -0
- taichi/types/ndarray_type.py +147 -0
- taichi/types/primitive_types.py +203 -0
- taichi/types/quant.py +88 -0
- taichi/types/texture_type.py +85 -0
- taichi/types/utils.py +13 -0
taichi/aot/module.py
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
# type: ignore
|
2
|
+
|
3
|
+
import datetime
|
4
|
+
import os
|
5
|
+
import warnings
|
6
|
+
from contextlib import contextmanager
|
7
|
+
from glob import glob
|
8
|
+
from pathlib import Path, PurePosixPath
|
9
|
+
from shutil import rmtree
|
10
|
+
from tempfile import mkdtemp
|
11
|
+
from zipfile import ZipFile
|
12
|
+
|
13
|
+
import taichi
|
14
|
+
from taichi.aot.utils import produce_injected_args, produce_injected_args_from_template
|
15
|
+
from taichi.lang import impl, kernel_impl
|
16
|
+
from taichi.lang.field import ScalarField
|
17
|
+
from taichi.lang.matrix import MatrixField
|
18
|
+
from taichi.types.annotations import template
|
19
|
+
|
20
|
+
|
21
|
+
class KernelTemplate:
|
22
|
+
def __init__(self, kernel_fn, aot_module):
|
23
|
+
self._kernel_fn = kernel_fn
|
24
|
+
self._aot_module = aot_module
|
25
|
+
|
26
|
+
@staticmethod
|
27
|
+
def keygen(v, key_p, fields):
|
28
|
+
if isinstance(v, (int, float, bool)):
|
29
|
+
key_p += "=" + str(v) + ","
|
30
|
+
return key_p
|
31
|
+
for ky, val in fields:
|
32
|
+
if val is v:
|
33
|
+
key_p += "=" + ky + ","
|
34
|
+
return key_p
|
35
|
+
raise RuntimeError(
|
36
|
+
"Arg type must be of type int/float/boolean" f" or taichi field. Type {str(type(v))}" " is not supported"
|
37
|
+
)
|
38
|
+
|
39
|
+
def instantiate(self, **kwargs):
|
40
|
+
name = self._kernel_fn.__name__
|
41
|
+
kernel = self._kernel_fn._primal
|
42
|
+
assert isinstance(kernel, kernel_impl.Kernel)
|
43
|
+
injected_args = []
|
44
|
+
key_p = ""
|
45
|
+
anno_index = 0
|
46
|
+
template_args = {}
|
47
|
+
|
48
|
+
for index, (key, value) in enumerate(kwargs.items()):
|
49
|
+
template_args[index] = (key, value)
|
50
|
+
|
51
|
+
for arg in kernel.arguments:
|
52
|
+
if arg.annotation == template or isinstance(arg.annotation, template):
|
53
|
+
(k, v) = template_args[anno_index]
|
54
|
+
key_p += k
|
55
|
+
key_p = self.keygen(v, key_p, self._aot_module._fields.items())
|
56
|
+
injected_args.append(v)
|
57
|
+
anno_index += 1
|
58
|
+
else:
|
59
|
+
injected_args.append(0)
|
60
|
+
kernel.ensure_compiled(*injected_args)
|
61
|
+
self._aot_module._aot_builder.add_kernel_template(name, key_p, kernel.kernel_cpp)
|
62
|
+
|
63
|
+
# kernel AOT
|
64
|
+
self._aot_module._kernels.append(kernel)
|
65
|
+
|
66
|
+
|
67
|
+
class Module:
|
68
|
+
"""An AOT module to save and load Taichi kernels.
|
69
|
+
|
70
|
+
This module serializes the Taichi kernels for a specific arch. The
|
71
|
+
serialized module can later be loaded to run on that backend, without the
|
72
|
+
Python environment.
|
73
|
+
|
74
|
+
Example:
|
75
|
+
Usage::
|
76
|
+
|
77
|
+
m = ti.aot.Module(ti.metal)
|
78
|
+
m.add_kernel(foo)
|
79
|
+
m.add_kernel(bar)
|
80
|
+
|
81
|
+
m.save('/path/to/module')
|
82
|
+
|
83
|
+
# Now the module file '/path/to/module' contains the Metal kernels
|
84
|
+
# for running ``foo`` and ``bar``.
|
85
|
+
"""
|
86
|
+
|
87
|
+
def __init__(self, arch=None, caps=None):
|
88
|
+
"""Creates a new AOT module instance
|
89
|
+
|
90
|
+
Args:
|
91
|
+
arch: Target backend architecture. Default to the one initialized in :func:`~taichi.lang.init` if not specified.
|
92
|
+
caps (List[str]): Enabled device capabilities.
|
93
|
+
"""
|
94
|
+
if caps is None:
|
95
|
+
caps = []
|
96
|
+
curr_arch = impl.current_cfg().arch
|
97
|
+
if arch is None:
|
98
|
+
arch = curr_arch
|
99
|
+
elif arch != curr_arch:
|
100
|
+
# TODO: we'll support this eventually but not yet...
|
101
|
+
warnings.warn(
|
102
|
+
f"AOT compilation to a different arch than the current one is not yet supported, switching to {curr_arch}"
|
103
|
+
)
|
104
|
+
arch = curr_arch
|
105
|
+
|
106
|
+
self._arch = arch
|
107
|
+
self._kernels = []
|
108
|
+
self._fields = {}
|
109
|
+
rtm = impl.get_runtime()
|
110
|
+
rtm._finalize_root_fb_for_aot()
|
111
|
+
self._aot_builder = rtm.prog.make_aot_module_builder(arch, caps)
|
112
|
+
self._content = []
|
113
|
+
|
114
|
+
def add_field(self, name, field):
|
115
|
+
"""Add a taichi field to the AOT module.
|
116
|
+
|
117
|
+
Args:
|
118
|
+
name: name of taichi field
|
119
|
+
field: taichi field
|
120
|
+
|
121
|
+
Example::
|
122
|
+
|
123
|
+
>>> a = ti.field(ti.f32, shape=(4,4))
|
124
|
+
>>> b = ti.field("something")
|
125
|
+
>>>
|
126
|
+
>>> m.add_field(a)
|
127
|
+
>>> m.add_field(b)
|
128
|
+
>>>
|
129
|
+
>>> # Must add in sequence
|
130
|
+
"""
|
131
|
+
is_scalar = True
|
132
|
+
self._fields[name] = field
|
133
|
+
column_num = 1
|
134
|
+
row_num = 1
|
135
|
+
if isinstance(field, MatrixField):
|
136
|
+
is_scalar = False
|
137
|
+
row_num = field.m
|
138
|
+
column_num = field.n
|
139
|
+
else:
|
140
|
+
assert isinstance(field, ScalarField)
|
141
|
+
self._aot_builder.add_field(
|
142
|
+
name,
|
143
|
+
field.snode.ptr,
|
144
|
+
is_scalar,
|
145
|
+
field.dtype,
|
146
|
+
field.snode.shape,
|
147
|
+
row_num,
|
148
|
+
column_num,
|
149
|
+
)
|
150
|
+
|
151
|
+
def add_kernel(self, kernel_fn, template_args=None, name=None):
|
152
|
+
"""Add a taichi kernel to the AOT module.
|
153
|
+
|
154
|
+
Args:
|
155
|
+
kernel_fn (Function): the function decorated by taichi `kernel`.
|
156
|
+
template_args (Dict[str, Any]): a dict where key is the template
|
157
|
+
parameter name, and value is the instantiating arg. Note that this
|
158
|
+
works for both :class:`~taichi.types.template` and for
|
159
|
+
`:class:`~taichi.types.ndarray`.
|
160
|
+
name (str): Name to identify this kernel in the module. If not
|
161
|
+
provided, uses the built-in ``__name__`` attribute of `kernel_fn`.
|
162
|
+
|
163
|
+
"""
|
164
|
+
kernel_name = name or kernel_fn.__name__
|
165
|
+
kernel = kernel_fn._primal
|
166
|
+
assert isinstance(kernel, kernel_impl.Kernel)
|
167
|
+
if template_args is not None:
|
168
|
+
injected_args = produce_injected_args_from_template(kernel, template_args)
|
169
|
+
else:
|
170
|
+
injected_args = produce_injected_args(kernel)
|
171
|
+
kernel.ensure_compiled(*injected_args)
|
172
|
+
self._aot_builder.add(kernel_name, kernel.kernel_cpp)
|
173
|
+
|
174
|
+
# kernel AOT
|
175
|
+
self._kernels.append(kernel)
|
176
|
+
|
177
|
+
self._content += ["kernel:" + kernel_name]
|
178
|
+
|
179
|
+
def add_graph(self, name, graph):
|
180
|
+
self._aot_builder.add_graph(name, graph._compiled_graph)
|
181
|
+
self._content += ["cgraph:" + name]
|
182
|
+
|
183
|
+
@contextmanager
|
184
|
+
def add_kernel_template(self, kernel_fn):
|
185
|
+
"""Add a taichi kernel (with template parameters) to the AOT module.
|
186
|
+
|
187
|
+
Args:
|
188
|
+
kernel_fn (Function): the function decorated by taichi `kernel`.
|
189
|
+
|
190
|
+
Example::
|
191
|
+
|
192
|
+
>>> @ti.kernel
|
193
|
+
>>> def bar_tmpl(a: ti.template()):
|
194
|
+
>>> x = a
|
195
|
+
>>> # or y = a
|
196
|
+
>>> # do something with `x` or `y`
|
197
|
+
>>>
|
198
|
+
>>> m = ti.aot.Module(arch)
|
199
|
+
>>> with m.add_kernel_template(bar_tmpl) as kt:
|
200
|
+
>>> kt.instantiate(a=x)
|
201
|
+
>>> kt.instantiate(a=y)
|
202
|
+
>>>
|
203
|
+
>>> @ti.kernel
|
204
|
+
>>> def bar_tmpl_multiple_args(a: ti.template(), b: ti.template())
|
205
|
+
>>> x = a
|
206
|
+
>>> y = b
|
207
|
+
>>> # do something with `x` and `y`
|
208
|
+
>>>
|
209
|
+
>>> with m.add_kernel_template(bar_tmpl) as kt:
|
210
|
+
>>> kt.instantiate(a=x, b=y)
|
211
|
+
|
212
|
+
TODO:
|
213
|
+
* Support external array
|
214
|
+
"""
|
215
|
+
kt = KernelTemplate(kernel_fn, self)
|
216
|
+
yield kt
|
217
|
+
|
218
|
+
def save(self, filepath):
|
219
|
+
"""
|
220
|
+
Args:
|
221
|
+
filepath (str): path to a folder to store aot files.
|
222
|
+
"""
|
223
|
+
filepath = str(PurePosixPath(Path(filepath)))
|
224
|
+
self._aot_builder.dump(filepath, "")
|
225
|
+
with open(f"{filepath}/__content__", "w") as f:
|
226
|
+
f.write("\n".join(self._content))
|
227
|
+
with open(f"{filepath}/__version__", "w") as f:
|
228
|
+
f.write(".".join(str(x) for x in taichi.__version__))
|
229
|
+
|
230
|
+
def archive(self, filepath: str):
|
231
|
+
"""
|
232
|
+
Args:
|
233
|
+
filepath (str): path to the stored archive of aot artifacts, MUST
|
234
|
+
end with `.tcm`.
|
235
|
+
"""
|
236
|
+
assert filepath.endswith(".tcm"), "AOT module artifact archive must ends with .tcm"
|
237
|
+
tcm_path = Path(filepath).absolute()
|
238
|
+
assert tcm_path.parent.exists(), "Output directory doesn't exist"
|
239
|
+
|
240
|
+
temp_dir = mkdtemp(prefix="tcm_")
|
241
|
+
# Save first as usual.
|
242
|
+
self.save(temp_dir)
|
243
|
+
|
244
|
+
fixed_time = datetime.datetime(2000, 12, 1).timestamp()
|
245
|
+
|
246
|
+
# Package all artifacts into a zip archive and attach contend data.
|
247
|
+
with ZipFile(tcm_path, "w") as z:
|
248
|
+
for path in glob(f"{temp_dir}/*", recursive=True):
|
249
|
+
os.utime(path, (fixed_time, fixed_time))
|
250
|
+
z.write(path, Path.relative_to(Path(path), temp_dir))
|
251
|
+
|
252
|
+
# Remove cached files
|
253
|
+
rmtree(temp_dir)
|
taichi/aot/utils.py
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# type: ignore
|
2
|
+
|
3
|
+
from typing import Any
|
4
|
+
|
5
|
+
from taichi.lang._ndarray import ScalarNdarray
|
6
|
+
from taichi.lang._texture import Texture
|
7
|
+
from taichi.lang.exception import TaichiCompilationError
|
8
|
+
from taichi.lang.matrix import (
|
9
|
+
Matrix,
|
10
|
+
MatrixNdarray,
|
11
|
+
MatrixType,
|
12
|
+
VectorNdarray,
|
13
|
+
VectorType,
|
14
|
+
)
|
15
|
+
from taichi.lang.util import cook_dtype
|
16
|
+
from taichi.types.annotations import template
|
17
|
+
from taichi.types.enums import Format
|
18
|
+
from taichi.types.ndarray_type import NdarrayType
|
19
|
+
from taichi.types.texture_type import RWTextureType, TextureType
|
20
|
+
|
21
|
+
template_types = (NdarrayType, TextureType, template)
|
22
|
+
|
23
|
+
|
24
|
+
def check_type_match(lhs, rhs):
|
25
|
+
if isinstance(lhs, MatrixType) and isinstance(rhs, MatrixType):
|
26
|
+
return lhs.n == rhs.n and lhs.m == rhs.m and (lhs.dtype == rhs.dtype or lhs.dtype is None or rhs.dtype is None)
|
27
|
+
if isinstance(lhs, MatrixType) or isinstance(rhs, MatrixType):
|
28
|
+
return False
|
29
|
+
|
30
|
+
return cook_dtype(lhs) == cook_dtype(rhs)
|
31
|
+
|
32
|
+
|
33
|
+
def produce_injected_args_from_template(kernel, template_args):
|
34
|
+
injected_args = []
|
35
|
+
num_template_args = len([arg.annotation for arg in kernel.arguments if isinstance(arg.annotation, template_types)])
|
36
|
+
assert num_template_args == len(
|
37
|
+
template_args
|
38
|
+
), f"Need {num_template_args} inputs to instantiate the template parameters, got {len(template_args)}"
|
39
|
+
for arg in kernel.arguments:
|
40
|
+
anno = arg.annotation
|
41
|
+
if isinstance(anno, template_types):
|
42
|
+
injected_args.append(template_args[arg.name])
|
43
|
+
elif isinstance(anno, RWTextureType):
|
44
|
+
texture_shape = (2,) * anno.num_dimensions
|
45
|
+
fmt = anno.fmt
|
46
|
+
injected_args.append(Texture(fmt, texture_shape))
|
47
|
+
else:
|
48
|
+
injected_args.append(0)
|
49
|
+
return injected_args
|
50
|
+
|
51
|
+
|
52
|
+
def produce_injected_args(kernel, symbolic_args=None):
|
53
|
+
injected_args = []
|
54
|
+
for i, arg in enumerate(kernel.arguments):
|
55
|
+
anno = arg.annotation
|
56
|
+
if isinstance(anno, NdarrayType):
|
57
|
+
if symbolic_args is not None:
|
58
|
+
# TODO: reconstruct dtype to be TensorType from taichi_core instead of the Python ones
|
59
|
+
element_dim = len(symbolic_args[i].element_shape)
|
60
|
+
if element_dim == 0 or symbolic_args[i].element_shape == (1,):
|
61
|
+
dtype = symbolic_args[i].dtype()
|
62
|
+
elif element_dim == 1:
|
63
|
+
dtype = VectorType(symbolic_args[i].element_shape[0], symbolic_args[i].dtype())
|
64
|
+
elif element_dim == 2:
|
65
|
+
dtype = MatrixType(
|
66
|
+
symbolic_args[i].element_shape[0],
|
67
|
+
symbolic_args[i].element_shape[1],
|
68
|
+
2,
|
69
|
+
symbolic_args[i].dtype(),
|
70
|
+
)
|
71
|
+
else:
|
72
|
+
raise TaichiCompilationError("Not supported")
|
73
|
+
ndim = symbolic_args[i].field_dim
|
74
|
+
else:
|
75
|
+
ndim = anno.ndim
|
76
|
+
dtype = anno.dtype
|
77
|
+
|
78
|
+
if anno.ndim is not None and ndim != anno.ndim:
|
79
|
+
raise TaichiCompilationError(
|
80
|
+
f"{ndim} from Arg {arg.name} doesn't match kernel's annotated ndim={anno.ndim}"
|
81
|
+
)
|
82
|
+
|
83
|
+
if anno.dtype is not None and not check_type_match(dtype, anno.dtype):
|
84
|
+
raise TaichiCompilationError(
|
85
|
+
f" Arg {arg.name}'s dtype {dtype.to_string()} doesn't match kernel's annotated dtype={anno.dtype.to_string()}"
|
86
|
+
)
|
87
|
+
|
88
|
+
if isinstance(dtype, VectorType):
|
89
|
+
injected_args.append(VectorNdarray(dtype.n, dtype=dtype.dtype, shape=(2,) * ndim))
|
90
|
+
elif isinstance(dtype, MatrixType):
|
91
|
+
injected_args.append(MatrixNdarray(dtype.n, dtype.m, dtype=dtype.dtype, shape=(2,) * ndim))
|
92
|
+
else:
|
93
|
+
injected_args.append(ScalarNdarray(dtype, (2,) * ndim))
|
94
|
+
elif isinstance(anno, RWTextureType):
|
95
|
+
texture_shape = (2,) * anno.num_dimensions
|
96
|
+
fmt = anno.fmt
|
97
|
+
injected_args.append(Texture(fmt, texture_shape))
|
98
|
+
elif isinstance(anno, TextureType):
|
99
|
+
texture_shape = (2,) * anno.num_dimensions
|
100
|
+
injected_args.append(Texture(Format.rgba8, texture_shape))
|
101
|
+
elif isinstance(anno, MatrixType):
|
102
|
+
if symbolic_args is not None:
|
103
|
+
symbolic_mat_n = symbolic_args[i].element_shape[0]
|
104
|
+
symbolic_mat_m = symbolic_args[i].element_shape[1]
|
105
|
+
|
106
|
+
if symbolic_mat_m != anno.m or symbolic_mat_n != anno.n:
|
107
|
+
raise RuntimeError(
|
108
|
+
f"Matrix dimension mismatch, expected ({anno.n}, {anno.m}) "
|
109
|
+
f"but dispatched shape ({symbolic_mat_n}, {symbolic_mat_m})."
|
110
|
+
)
|
111
|
+
injected_args.append(Matrix([0] * anno.n * anno.m, dt=anno.dtype))
|
112
|
+
else:
|
113
|
+
if symbolic_args is not None:
|
114
|
+
dtype = symbolic_args[i].dtype()
|
115
|
+
else:
|
116
|
+
dtype = anno
|
117
|
+
|
118
|
+
if not check_type_match(dtype, anno):
|
119
|
+
raise TaichiCompilationError(
|
120
|
+
f" Arg {arg.name}'s dtype {dtype.to_string()} doesn't match kernel's annotated dtype={anno.to_string()}"
|
121
|
+
)
|
122
|
+
# For primitive types, we can just inject a dummy value.
|
123
|
+
injected_args.append(0)
|
124
|
+
return injected_args
|
125
|
+
|
126
|
+
|
127
|
+
def json_data_model(f):
|
128
|
+
"""
|
129
|
+
Decorates a JSON data model. A JSON data model MUST NOT have any member
|
130
|
+
functions and it MUST be constructible from a JSON object.
|
131
|
+
|
132
|
+
This is merely a marker.
|
133
|
+
"""
|
134
|
+
f._is_json_data_model = True
|
135
|
+
return f
|
136
|
+
|
137
|
+
|
138
|
+
def is_json_data_model(cls) -> bool:
|
139
|
+
return hasattr(cls, "_is_json_data_model")
|
140
|
+
|
141
|
+
|
142
|
+
def dump_json_data_model(x: object) -> Any:
|
143
|
+
if isinstance(x, (int, float, str, bool, type(None))):
|
144
|
+
return x
|
145
|
+
if isinstance(x, (list, tuple)):
|
146
|
+
return [dump_json_data_model(e) for e in x]
|
147
|
+
if isinstance(x, dict):
|
148
|
+
return {k: dump_json_data_model(v) for k, v in x.items()}
|
149
|
+
if is_json_data_model(x):
|
150
|
+
return {k: dump_json_data_model(v) for k, v in x.__dict__.items() if k != "_is_json_data_model"}
|
151
|
+
return x
|
taichi/assets/.git
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gitdir: ../../.git/modules/external/assets
|
Binary file
|
Binary file
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import taichi as ti
|
2
|
+
|
3
|
+
|
4
|
+
@ti.kernel
|
5
|
+
def lcg_ti(B: int, lcg_its: int, a: ti.types.ndarray(ti.i32, 1)) -> None:
|
6
|
+
"""
|
7
|
+
Linear congruential generator https://en.wikipedia.org/wiki/Linear_congruential_generator
|
8
|
+
"""
|
9
|
+
for i in range(B):
|
10
|
+
x = a[i]
|
11
|
+
for j in range(lcg_its):
|
12
|
+
x = (1664525 * x + 1013904223) % 2147483647
|
13
|
+
a[i] = x
|
14
|
+
|
15
|
+
|
16
|
+
def main() -> None:
|
17
|
+
ti.init(arch=ti.cpu)
|
18
|
+
|
19
|
+
B = 10
|
20
|
+
lcg_its = 10
|
21
|
+
|
22
|
+
a = ti.ndarray(ti.int32, (B,))
|
23
|
+
|
24
|
+
lcg_ti(B, lcg_its, a)
|
25
|
+
print(f"LCG for B={B}, lcg_its={lcg_its}: ", a.to_numpy()) # pylint: disable=no-member
|
26
|
+
|
27
|
+
|
28
|
+
main()
|
taichi/experimental.py
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# type: ignore
|
2
|
+
|
3
|
+
import warnings
|
4
|
+
|
5
|
+
from taichi.lang.kernel_impl import real_func as _real_func
|
6
|
+
|
7
|
+
|
8
|
+
def real_func(func):
|
9
|
+
warnings.warn(
|
10
|
+
"ti.experimental.real_func is deprecated because it is no longer experimental. " "Use ti.real_func instead.",
|
11
|
+
DeprecationWarning,
|
12
|
+
)
|
13
|
+
return _real_func(func)
|
14
|
+
|
15
|
+
|
16
|
+
__all__ = ["real_func"]
|
taichi/graph/__init__.py
ADDED