 # 小白开启taichi源码研究

1 Like

TaichiCon里面有很多面向开发者的信息：https://github.com/taichi-dev/taichicon

auto_diff.cpp可以看 https://arxiv.org/abs/1910.00935 现在auto_diff系统比这个文章里面的更复杂一些，但是总体思路差不多。

1 Like

Let’s take an example piece in `auto_diff.cpp`:

``````    } else if (stmt->op_type == UnaryOpType::cos) {
``````

`stmt` is statement, one kind of statement is unary operation statement, i.e. only have one operand like `-x`, `cos(x)`.
The `stmt->op_type` tells you exactly which operator the statement is. It’s `cos` in this case.
The `stmt->operand` is the operand of this statement, it can be variable like `x`, or constant like `233`.

`accumulate(stmt->operand, negate(mul(adjoint(stmt), sin(stmt->operand))));`

This is basically calculating the derivitive (dao shu) of `cos(x)`. Recall that `d cos(x) = -sin(x) dx`, we use `negate`, `mul`, `adjoint` to represents the computation in C++ code.
If you take a close look,

`negate(mul(adjoint(stmt), sin(stmt->operand)))`

`negate(mul(dx, sin(x)))`

`negate(sin(x) * dx)`

`-sin(x) * dx`

You can easily reassemble the original mathematical expression.

``````} else if (stmt->op_type == UnaryOpType::tan) {
TI_NOT_IMPLEMENTED
``````

Here, we use the `TI_NOT_IMPLEMENTED` macro, it will raise an error when executed. That means the derivite calculation is not yet implemented for `tan`, would you complete this by using the method above to check if you’ve learnt it? (If you’re OK with calculas).

(Sorry about not typing in Chinese… my ShuRuFa gets broken and I have to talk English now)

1 Like