deepspeed训练大模型出现nan的情况之初始化问题

最近遇到一个头疼的事情,每次在模型部分加一些新的模块,就出现grad=nan的情况。仔细检查后发现是初始化参数的问题,torch自带的初始化模块(torch.nn.init)不知道为啥不起作用,而且数值非常大,有可能是没有适配bf16?

import torch
from timm.models.layers import trunc_normal_


class ...:

    def _init_weights(self, m):
        if isinstance(m, (nn.Conv2d, nn.Linear)):
            trunc_normal_(m.weight, std=.01, a=-1.0, b=1.0) # okay
            # torch.nn.init.trunc_normal_ does not work here

            try:
                # nn.init.constant_(m.bias, 0) # does not work
                m.bias.data.fill_(0) # okay
            except:
                pass

写在2024年11月

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值