mindspore-ai / mindspore

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
https://gitee.com/mindspore/mindspore
Apache License 2.0
4.26k stars 705 forks source link

mindnlp.core.nn.init.uniform函数生成异常 #301

Open ShangJingLi opened 1 month ago

ShangJingLi commented 1 month ago

Environment

Hardware Environment(Ascend/GPU/CPU):

CPU

Software Environment:

Describe the current behavior

mindnlp套件的nn.init.uniform在一些情况下生成异常,该函数基于mindspore的Initializer类和其他初始化相关模块封装。当下界大于或等于0时,输出结果正常;下界小于0,上界大于0时,输出结果异常,异常结果:输出均为上界;当上界小于0时,输出结果异常,输出逻辑不明。当设置下界大于上界时,应抛出错误,但实际未抛出且输出结果逻辑不明

Describe the expected behavior

当上界和下界被设置时且上界大于下界时,生成的序列应在上界和下界之间呈现均匀分布;当下界被设置大于上界时应抛出错误信息

Steps to reproduce the issue

a1 = np.ones(shape=(1, 10)).astype(np.float32) ta1 = mindnlp.core.nn.init.uniform(Tensor(a1), a=-1, b=1) # 不正常 ta2 = mindnlp.core.nn.init.uniform(Tensor(a1), a=0, b=1) # 看似正常 ta3 = mindnlp.core.nn.init.uniform(Tensor(a1), a=1, b=10) # 看似正常 ta4 = mindnlp.core.nn.init.uniform(Tensor(a1), a=-2, b=0) # 不正常 print(ta1, "理论上下界为-1,上界为1的均匀分布\n") print(ta2, "理论上下界为0,上界为1的均匀分布\n") print(ta3, "理论上为下界为1,上界为10的均匀分布\n") print(ta4, "理论上为下界为-2,上界为0的均匀分布\n")

Related log / screenshot

image

Special notes for this issue

moyanxinxu commented 1 month ago

https://github.com/mindspore-lab/mindnlp/commit/56774dd920186e9956c0e3a68ccabfef5174e786

moyanxinxu commented 1 month ago
理论上下界为-1,上界为1的均匀分布:
[[-0.24242505  0.18378146  0.3106798  -0.33906373  0.6060065  -0.7903174   -0.95910054  0.00910801 -0.37601295  0.25596344]]

理论上下界为0,上界为1的均匀分布:
[[0.71938884 0.6745291  0.69897014 0.9580319  0.05868613 0.22249985    0.75021434 0.4646567  0.290779   0.9318139 ]]

理论上为下界为1,上界为10的均匀分布:
[[3.4718785 4.2812524 7.0038333 4.4340143 9.26519   4.8634377 3.8028338    6.1175847 3.104072  5.806433 ]]

理论上为下界为-2,上界为0的均匀分布:
[[-0.7266476  -1.8067375  -0.7297912  -1.4816263  -0.7031752  -0.04069502   -0.8378217  -1.6065764  -1.4900833  -1.3968813 ]]