Differentiable programming (WIP)

This page is work in progress. Please check out the DiffTaichi paper and video to learn more about Taichi differentiable programming.

DiffTaichi仓库 包含10个使用Taichi可微分编程构建的可微编程物理模拟器。

注解

与TensorFlow等生成 不可变 输出缓冲区的工具不同,Taichi采用的 命令式 编程范式允许程序员自由修改全局张量(多维数组):

全局数据访问规则:

  • 如果全局张量元素被多次写入,则从第二次写入开始,写入 必须 以原子加法的形式出现( 累加,使用 ti.atomic_add 或直接使用 + = )。
  • 在完成全局张量元素的累加之前,不会对全局张量元素进行读取访问。

内核(Kernel)简化规则: 内核主体由多个 简单嵌套 的for循环组成。 即,每个for循环可以只包含一个(嵌套的)for循环(不包含其他语句),也可以包含一组没有循环的语句。

例子:

@ti.kernel
def differentiable_task():
  for i in x:
    x[i] = y[i]

  for i in range(10):
    for j in range(20):
      for k in range(300):
        ... do whatever you want, as long as there are no loops

  # Not allowed. The outer for loop contains two for loops
  for i in range(10):
    for j in range(20):
      ...
    for j in range(20):
      ...

违反此规则的Taichi程序将在执行梯度运算时产生未定义的行为。

注解

静态for循环 (例如 for i in ti.static(range(4)) )将被Python前端预处理器展开,并且不算作循环级别。

使用微分模拟器和蛮力梯度下降对神经网络控制器进行优化的一些示例:

https://github.com/yuanming-hu/public_files/raw/master/learning/difftaichi/ms3_final-cropped.gif https://github.com/yuanming-hu/public_files/raw/master/learning/difftaichi/rb_final2.gif https://github.com/yuanming-hu/public_files/raw/master/learning/difftaichi/diffmpm3d.gif

注解

Apart from differentiating the simulation time steps, you can also automatically differentiate (negative) potential energies to get forces. Here is an example.

文档制作中。