PaDiff/Tutorial.md at develop · PaddlePaddle/PaDiff · GitHub

您所在的位置:网站首页 perform的形式 PaDiff/Tutorial.md at develop · PaddlePaddle/PaDiff · GitHub

PaDiff/Tutorial.md at develop · PaddlePaddle/PaDiff · GitHub

2023-03-25 16:41| 来源: 网络整理| 查看: 265

Tutorial 一、 使用方法 二、阅读输出信息 2.1 正确对齐时的输出信息 2.2 模型权重拷贝失败时的报错信息 2.3 模型前反向对齐失败时的输出信息 2.4 模型weight/grad对齐失败的报错信息 三、使用loss & optimizer 3.1 使用loss 3.2 使用optimizer 四、使用assign_weight 五、 使用API级别的对齐检查 Tutorial 一、 使用方法

auto_diff 详细参数解释详见:接口信息

使用 padiff 进行模型对齐检查有几个基本的步骤:- Tutorial

分别构造 paddle 和 torch 模型 分别构造两个模型的输入数据 调用 auto_diff API 接口

以下是一段使用 padiff 工具进行对齐的完整代码,

注意:在模型定义时,需要将forward中所使用的子模型在 __init__ 函数中定义,并保证其中的子模型定义顺序一致**,具体可见下方示例代码

from padiff import auto_diff import torch import paddle # 使用paddle与torch定义相同结构的模型: SimpleLayer 和 SimpleModule # 样例模型结构为: # x -> linear1 -> x -> relu -> x -> add -> linear2 -> output # | | # |----------------------------------| # 注意:两个模型定义顺序都是 linear1 linear2 ReLU,顺序必须对齐,submodule内部的定义也是一样。 class SimpleLayer(paddle.nn.Layer): def __init__(self): super(SimpleLayer, self).__init__() self.linear1 = paddle.nn.Linear(100, 100) self.linear2 = paddle.nn.Linear(100, 10) self.act = paddle.nn.ReLU() def forward(self, x): resdual = x x = self.linear1(x) x = self.act(x) x = x + resdual x = self.linear2(x) return x class SimpleModule(torch.nn.Module): def __init__(self): super(SimpleModule, self).__init__() self.linear1 = torch.nn.Linear(100, 100) self.linear2 = torch.nn.Linear(100, 10) self.act = torch.nn.ReLU() def forward(self, x): resdual = x x = self.linear1(x) x = self.act(x) x = x + resdual x = self.linear2(x) return x layer = SimpleLayer() module = SimpleModule() inp = paddle.rand((100, 100)).numpy().astype("float32") inp = ({'x': paddle.to_tensor(inp)}, ## 修改代码对齐,或使用 LayerMap 指定 子模型的 paddle 与 torch 实现方式不一致(权重等对不齐)=> 使用 LayerMap 指定

注:LayerMap 的使用方式详见:LayerMap使用说明

若不使用 padiff 的权重初始化功能,可以避免此类错误,但在权重与梯度检查时,会遇见同样的问题

[AutoDiff] Your options: { atol: `0.0001` rtol: `1e-07` diff_phase: `both` compare_mode: `mean` single_step: `False` } [AutoDiff] Assign weight Failed !!! Error occured between: paddle: `Linear(in_features=100, out_features=100, dtype=float32)` `SimpleLayerDiff.linear2.weight` torch: `Linear(in_features=100, out_features=10, bias=True)` `SimpleModule.linear2.weight` Shape of paddle param `weight` and torch param `weight` is not the same. [100, 100] vs [100, 10] Torch Model ========================= SimpleModule |--- Linear +--- Linear [AutoDiff] Your options: { atol: `0.0001` steps: `1` rtol: `1e-07` compare_mode: `mean` single_step: `False` use_loss: `False` use_opt: `False` } [AutoDiff] =================Train Step 0================= [AutoDiff] Max elementwise output diff is 4.575464248657227 [AutoDiff] FAILED !!! [AutoDiff] Diff found in `Forward Stage` in step: 0, net_id is -1 vs -1 [AutoDiff] Type of layer is : vs Not equal to tolerance rtol=1e-07, atol=0.0001 Mismatched elements: 1 / 1 (100%) Max absolute difference: 0.0622915 Max relative difference: 1.6068412 x: array(0.023525, dtype=float32) y: array(-0.038766, dtype=float32) [AutoDiff] Check model struct: Paddle Model ========================= (net) SimpleLayerDiff |--- (net) Linear | +--- (api) paddle.nn.functional.linear


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3