Skip to main content

A tiny symbolic autodiff demo built on Sympy.

Project description

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.py
  • python examples/matrix_arith.py
  • python examples/matrix_sum.py
  • python examples/mlp.py(三层 MLP 符号反向)

MLP 梯度示例

下图为 examples/mlp.py 生成的三层 MLP 梯度表达式(截自 LaTeX 渲染):

MLP Grad

Qwen2 MLP 梯度示例

下图为 examples/qwen2_mlp.py 生成的 Qwen2 风格 MLP 梯度表达式(截自 LaTeX 渲染):

Qwen2 MLP Grad

已支持的 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 提高可读性)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

symgrad4t-0.2.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

symgrad4t-0.2.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file symgrad4t-0.2.1.tar.gz.

File metadata

  • Download URL: symgrad4t-0.2.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for symgrad4t-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d65d57eea8cc196711ee504efc947bcd908e98f1ea228bd9ae1f14a72ff21b5f
MD5 d66290b640ec5541fa26d713e1c53381
BLAKE2b-256 971b5de50ae6e7c4e9702ec1f78e248f9fb81249ca5f529273da51552af01167

See more details on using hashes here.

File details

Details for the file symgrad4t-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: symgrad4t-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for symgrad4t-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd85244584f02aeb74667bf20c28cc55b1d046b08ff1af184a7c98ada7026f96
MD5 b7da0d921979a29e668600517f84db5e
BLAKE2b-256 e57f75024b43b5fb48a07eba1021777393a4761fcf7cb371a600f6cb08ee5ceb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page