Back to home

Compare

Comparing: 60 Lines of Handwritten Autograd: Seeing AI Training’s Foundations Matters More Than Calling APIs & 60 行手写自动微分 — 看清 AI 训练底层比会调 API 更重要

AEN
PyTorchAutomatic Differentiationtanh·

60 Lines of Handwritten Autograd: Seeing AI Training’s Foundations Matters More Than Calling APIs

An open-source mini-project implements an automatic differentiation engine in roughly 60 lines of code. The relative error between compound expressions and numerical gradients is approximately 1e-12 to 3e-11. That result does not deserve exaggeration, but it is enough to help readers understand that large-model training is not a mysterious black box.

What this is

Automatic differentiation is the foundation of neural-network training: the program records its operations to form a computation graph, then traverses that graph backward to calculate how each parameter should be adjusted. The author uses a Value class to store values, gradients, and computational relationships, and defines derivative rules for nine operations, including addition, subtraction, multiplication, division, powers, exponentials, and logarithms.

The project also uses central differences to check its results. A numerical gradient estimates the slope from changes in a function near its original value; because it does not depend on hand-derived formulas, it is well suited to testing whether the code is correct. Ordinary expressions match exactly, but a 50-layer tanh chain exposes vanishing gradients: the influence of parameters decays rapidly, rather than the engine simply calculating incorrectly.

Industry view

We believe the project’s value lies not in replacing PyTorch, but in laying bare the engineering details the industry often skips over: the forward pass computes the result, while the backward pass propagates gradients; a gradient is an accumulated quantity, not an ordinary operand; and when extremely small gradients are measured against relative error using a crude formula, the error can be hidden.

The risks are equally clear. It supports only scalars and nine operators, with no tensors, parallel scheduling, memory release, higher-order differentiation, or detection of in-place modifications. The small size of the codebase does not mean it can indicate whether large models will train stably. More concerning are silent failures such as “tests pass, but training does not move,” which are harder to diagnose than explicit errors. This repository is suitable for learning, not for direct use in production.

Impact on regular people

For enterprise IT: An implementation like this can serve as internal training material, helping technical teams understand why errors in training frameworks often appear as inaccurate results rather than program crashes. Framework selection should still prioritize mature frameworks and testing systems.

For individual careers: It shows that AI skills are extending toward underlying engineering knowledge, but writing prompts and evaluating model training are not the same capability. The learning focus should be on principles, validation methods, and fault isolation.

For the consumer market: More reliable training and validation mechanisms can help reduce unstable model updates, but they will not directly change how quickly consumers buy AI products for now. Model performance and cost still depend on data and infrastructure.

Source: juejin.cn
BZH
PyTorch自动微分tanh·

60 行手写自动微分 — 看清 AI 训练底层比会调 API 更重要

一个开源小项目用约 60 行代码实现了自动微分引擎,复合表达式与数值梯度的相对误差约为 1e-12 至 3e-11;这个结果不值得夸大,但足以帮助读者理解大模型训练并非神秘黑箱。

这是什么

自动微分是训练神经网络的基础:程序记录运算过程形成计算图,再沿计算图反向计算每个参数的调整方向。作者用一个 Value 类保存数值、梯度和计算关系,并为加、减、乘、除、幂、指数、对数等九种运算配置求导规则。

项目同时采用中心差分检查结果。所谓数值梯度,是用函数在原值附近的变化估算斜率;它不依赖人工公式,因此很适合检验代码是否写对。普通表达式对拍结果精确,但 50 层 tanh 链暴露出梯度消失:参数影响快速衰减,并非引擎单纯算错。

行业怎么看

我们认为,这个项目的价值不在于替代 PyTorch,而在于把行业经常略过的工程细节摊开:前向负责算结果,反向负责传梯度;梯度是累积量,不能当普通操作数;极小梯度若用粗糙公式计算相对误差,误差会被掩盖。

风险也很明确。它只支持标量和九个算子,没有张量、并行调度、内存释放、高阶求导及原地修改检测;代码规模小,不代表可以据此判断大型模型能否稳定训练。更值得警惕的是“测试通过但训练不动”这类静默错误,它比直接报错更难定位。这个仓库适合学习,不适合直接投入生产。

对普通人的影响

对企业 IT:这类实现可作为内部培训材料,帮助技术团队理解训练框架的错误为何常表现为结果不准,而不是程序崩溃;选型仍应优先考虑成熟框架与测试体系。

对个人职场:它说明 AI 能力正在向底层工程知识延伸,但会写提示词与能评估模型训练并非同一种能力;学习重点可以放在原理、验证方法和故障定位。

对消费市场:更可靠的训练与验证机制有助于减少模型更新不稳定的问题,但目前不会直接改变消费者购买 AI 产品的速度,模型效果与成本仍取决于数据和基础设施。

Source: juejin.cn