SymGrad4T
一个用 Sympy 做符号表达的极简自动求导/张量广播示例,当前版本 v0.2.1。已支持经典三层 MLP 的符号反向传播(示例见 examples/mlp.py),打印出的 LaTeX 可直接粘贴到 https://www.latexlive.com/ 查看。
安装
- PyPI:
pip install symgrad4t - 更新:
pip install --upgrade symgrad4t - 本地开发:
pip install -e .
运行测试
- 项目根目录:
pytest -q - 仅库内测试:
cd python && pytest -q
运行示例
python examples/rms_norm/rms_norm.pypython examples/matrix_arith.pypython examples/matrix_sum.pypython examples/mlp.py(三层 MLP 符号反向)
MLP 梯度示例
下图为 examples/mlp.py 生成的三层 MLP 梯度表达式(截自 LaTeX 渲染):
Qwen2 MLP 梯度示例
下图为 examples/qwen2_mlp.py 生成的 Qwen2 风格 MLP 梯度表达式(截自 LaTeX 渲染):
已支持的 OP / 功能
- 元素级:
Add/Sub/Mul/Div - 幂与根:
Power、标量幂、Sqrt - 归约:
Sum(dim, keepdim=False)(支持负维度) - 广播:
Broadcast/Tensor.broadcast_to(自动应用于元素级算子) - 矩阵:
MatMul/Transpose - 非线性:
Max/ReLU/Softmax - 损失:
CrossEntropy(内置 Softmax+CE 简化) - 工具:
Tensor.latex_expr()/Tensor.latex_grad()(乘号用\cdot提高可读性)
Release files for symgrad4t 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| symgrad4t-0.2.1.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| symgrad4t-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.2 kB
Release files / symgrad4t-0.2.1.tar.gz
| Download URL | symgrad4t-0.2.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d65d57eea8cc196711ee504efc947bcd908e98f1ea228bd9ae1f14a72ff21b5f
|
|
BLAKE2b-256 checksum How to use checksums |
971b5de50ae6e7c4e9702ec1f78e248f9fb81249ca5f529273da51552af01167
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Release files / symgrad4t-0.2.1-py3-none-any.whl
| Download URL | symgrad4t-0.2.1-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dd85244584f02aeb74667bf20c28cc55b1d046b08ff1af184a7c98ada7026f96
|
|
BLAKE2b-256 checksum How to use checksums |
e57f75024b43b5fb48a07eba1021777393a4761fcf7cb371a600f6cb08ee5ceb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|