TaiChi v0.9.0 发布,引入矩阵动态索引和更稳定的 API

栏目: 软件资讯 · 发布时间: 3年前

内容简介:Taichi(太极)0.9.0 已经发布,这是专为高性能计算机图形学设计的编程语言。该版本引进了矩阵的动态索引(实验功能)、改善了当前 API 集的稳定性... 新特性 1、矩阵的动态索引(实验功能) 之前版本的矩阵只能通...

Taichi(太极)0.9.0 已经发布,这是专为高性能计算机图形学设计的编程语言。该版本引进了矩阵的动态索引(实验功能)、改善了当前 API 集的稳定性...

新特性

1、矩阵的动态索引(实验功能)

之前版本的矩阵只能通过常量索引访问,导致有些操作无法执行。比如无法将向量中的最小元素钳制(clamp 运算)到 0 :

@ti.kernel
def clamp():
    ...  # assume we have a n-d vector A
    min_index = 0
    for i in range(n):
        if A[i] < A[min_index]:
            min_index = i
    A[min_index] = 0

当然也可以利用循环展开,但这样写既不直观也不高效:

@ti.kernel
def clamp():
    ...  # assume we have a n-d vector A
    min_index = 0
    for i in ti.static(range(n)):
        if A[i] < A[min_index]:
            min_index = i
    for i in ti.static(range(n)):
        if i == min_index:
            A[i] = 0

有了这个矩阵动态索引的新特性,现在就可以平稳地运行第一段代码。此外,v0.9.0 版本添加了一个新的隐式 FEM(有限元方法)示例(),在这个例子中,为隐式时间积分构造了一个巨大的 (12 × 12) Hessian 矩阵。如果没有动态索引,整个矩阵构造循环需要展开,编译需要70秒;使用动态索引,可以应用传统的循环版本,编译时间缩短到2.5秒。

矩阵动态索引特性可以通过设置 ti.init(dynamic_index=True) 来启用。

2、macOS 上的 Vulkan 后端

在 macOS 10.15+ 上添加对 ti.vulkan 后端的支持,现在可以在 MacBook 上运行 GGUI,运行以下 GGUI 示例:

# prerequisites: taichi >= v0.9.0 and macOS >= 10.15
# run GGUI examples
ti example fractal3d_ggui 
ti example fem128_ggui

3、与 Google Colab 的兼容性

0.9.0 版本重构了编译器实现,使 Taichi 与 Google Colab 兼容。(如果在 Google Colab 笔记本环境中运行早期版本的 Taichi ,系统会崩溃 #235

Google Colab 中运行!pip install taichi以安装 Taichi 。

功能改进

1.、更稳定、更完善的 API

0.9.0 版本重新组织包结构,并弃用一些过时或内部 API :

种类 已弃用的 API 替代品
Builtin max() ti.max()
Builtin min() ti.min()
Atomic operation obj.atomic_add() ti.atomic_add()
Image-specific ti.imread() ti.tools.imread()
Image-specific ti.imwrite() ti.tools.imwrite()
Image-specific ti.imshow() ti.tools.imshow()
Profiler-specific ti.print_profile_info() ti.profiler.print_scoped_profiler_info()
Profiler-specific ti.print_kernel_profile_info() ti.profiler.print_kernel_profiler_info()

完整的 API 更改列表请参阅此 Google 文档

2、错误报告更简洁

该版本改善了错误报告的展示,在错误报告中删除了与开发人员无关的琐碎回溯,以改善调试体验。以下面这段代码为例:

import taichi as ti

ti.init()

@ti.func
def bar(a):
    a = a + 2j

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

foo()

0.9.0 版本之前,这段代码的错误报告如下:

[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "error.py", line 13, in 
    foo()
  File "/path_to_taichi/lang/kernel_impl.py", line 709, in wrapped
    return primal(*args, **kwargs)
  File "/path_to_taichi/lang/kernel_impl.py", line 636, in __call__
    key = self.ensure_compiled(*args)
  File "/path_to_taichi/lang/kernel_impl.py", line 627, in ensure_compiled
    self.materialize(key=key, args=args, arg_features=arg_features)
  File "/path_to_taichi/lang/kernel_impl.py", line 493, in materialize
    taichi_kernel = _ti_core.create_kernel(taichi_ast_generator,
  File "/path_to_taichi/lang/kernel_impl.py", line 488, in taichi_ast_generator
    compiled()
  File "error.py", line 11, in foo
    bar(1)
  File "/path_to_taichi/lang/kernel_impl.py", line 76, in decorated
    return fun.__call__(*args)
  File "/path_to_taichi/lang/kernel_impl.py", line 156, in __call__
    ret = self.compiled(*args)
  File "error.py", line 7, in bar
    a = a + 2j
  File "/path_to_taichi/lang/common_ops.py", line 16, in __add__
    return ti.add(self, other)
  File "/path_to_taichi/lang/ops.py", line 78, in wrapped
    return imp_foo(a, b)
  File "/path_to_taichi/lang/ops.py", line 63, in imp_foo
    return foo(x, y)
  File "/path_to_taichi/lang/ops.py", line 427, in add
    return _binary_operation(_ti_core.expr_add, _bt_ops_mod.add, a, b)
  File "/path_to_taichi/lang/ops.py", line 173, in _binary_operation
    a, b = wrap_if_not_expr(a), wrap_if_not_expr(b)
  File "/path_to_taichi/lang/ops.py", line 36, in wrap_if_not_expr
    return Expr(a) if not is_taichi_expr(a) else a
  File "/path_to_taichi/lang/expr.py", line 33, in __init__
    self.ptr = impl.make_constant_expr(arg).ptr
  File "/path_to_taichi/lang/util.py", line 196, in wrapped
    return func(*args, **kwargs)
  File "/path_to_taichi/lang/impl.py", line 414, in make_constant_expr
    raise ValueError(f'Invalid constant scalar expression: {type(val)}')
ValueError: Invalid constant scalar expression: 

在 0.9.0 版本中,其错误报告如下:

Traceback (most recent call last):
  File "/path_to_test/error.py", line 13, in 
    foo()
  File "/path_to_taichi/lang/kernel_impl.py", line 732, in wrapped
    raise type(e)('\\n' + str(e)) from None
taichi.lang.exception.TaichiTypeError: 
On line 11 of file "/path_to_test/error.py", in foo:
    bar(1)
    ^^^^^^
On line 7 of file "/path_to_test/error.py", in bar:
    a = a + 2j
        ^^^^^^
Invalid constant scalar data type: 

3、官网文档页改版

为了提高文档的可读性和用户友好性,Taichi 的文档站点已重构,并将 API 参考纳入其中。

除上述版本高光内容外,该版本还包含大量其他更新项,包含新的测试、修复、重构等,详情可在发布公告中阅览。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Git

Pro Git

Scott Chacon / Apress / 2009-8-27 / USD 34.99

Git is the version control system developed by Linus Torvalds for Linux kernel development. It took the open source world by storm since its inception in 2005, and is used by small development shops a......一起来看看 《Pro Git》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具