Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
a4644c50
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2310
Star
20933
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a4644c50
编写于
7月 26, 2023
作者:
zhouweiwei2014
提交者:
GitHub
7月 26, 2023
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[BUG] fix bug of float/int/long/index Tensor (#55568)
上级
ee506c2f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
27 addition
and
4 deletion
+27
-4
python/paddle/fluid/dygraph/math_op_patch.py
python/paddle/fluid/dygraph/math_op_patch.py
+12
-4
test/legacy_test/test_math_op_patch_var_base.py
test/legacy_test/test_math_op_patch_var_base.py
+15
-0
未找到文件。
python/paddle/fluid/dygraph/math_op_patch.py
浏览文件 @
a4644c50
...
...
@@ -114,21 +114,27 @@ def monkey_patch_math_tensor():
),
"only one element variable can be converted to float."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
float
(
np
.
array
(
var
).
flatten
()[
0
])
if
var
.
dtype
==
core
.
VarDesc
.
VarType
.
BF16
:
var
=
var
.
astype
(
'float32'
)
return
float
(
np
.
array
(
var
))
def
_long_
(
var
):
numel
=
np
.
prod
(
var
.
shape
)
assert
numel
==
1
,
"only one element variable can be converted to long."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
np
.
array
(
var
).
flatten
()[
0
])
if
var
.
dtype
==
core
.
VarDesc
.
VarType
.
BF16
:
var
=
var
.
astype
(
'float32'
)
return
int
(
np
.
array
(
var
))
def
_int_
(
var
):
numel
=
np
.
prod
(
var
.
shape
)
assert
numel
==
1
,
"only one element variable can be converted to int."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
np
.
array
(
var
).
flatten
()[
0
])
if
var
.
dtype
==
core
.
VarDesc
.
VarType
.
BF16
:
var
=
var
.
astype
(
'float32'
)
return
int
(
np
.
array
(
var
))
def
_len_
(
var
):
assert
var
.
ndim
>
0
,
"len() of a 0-D tensor is wrong"
...
...
@@ -146,7 +152,9 @@ def monkey_patch_math_tensor():
),
"only one element variable can be converted to python index."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
np
.
array
(
var
).
flatten
()[
0
])
if
var
.
dtype
==
core
.
VarDesc
.
VarType
.
BF16
:
var
=
var
.
astype
(
'float32'
)
return
int
(
np
.
array
(
var
))
@
property
def
_ndim_
(
var
):
...
...
test/legacy_test/test_math_op_patch_var_base.py
浏览文件 @
a4644c50
...
...
@@ -242,6 +242,11 @@ class TestMathOpPatchesVarBase(unittest.TestCase):
self
.
assertTrue
(
int
(
a
)
==
100
)
self
.
assertTrue
(
int
(
a
)
==
100
)
a
=
paddle
.
to_tensor
(
1000000.0
,
dtype
=
'bfloat16'
)
self
.
assertTrue
(
float
(
a
)
==
999424.0
)
self
.
assertTrue
(
int
(
a
)
==
999424
)
self
.
assertTrue
(
int
(
a
)
==
999424
)
def
test_len
(
self
):
a_np
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
with
fluid
.
dygraph
.
guard
():
...
...
@@ -260,6 +265,16 @@ class TestMathOpPatchesVarBase(unittest.TestCase):
str1
=
"just test"
self
.
assertTrue
(
str1
[
var1
]
==
's'
)
var1
=
paddle
.
to_tensor
(
2.0
,
dtype
=
'bfloat16'
)
i_tmp
=
0
for
i
in
range
(
var1
):
self
.
assertTrue
(
i
==
i_tmp
)
i_tmp
=
i_tmp
+
1
list1
=
[
1
,
2
,
3
,
4
,
5
]
self
.
assertTrue
(
list1
[
var1
]
==
3
)
str1
=
"just test"
self
.
assertTrue
(
str1
[
var1
]
==
's'
)
def
test_np_left_mul
(
self
):
with
fluid
.
dygraph
.
guard
():
t
=
np
.
sqrt
(
2.0
*
np
.
pi
)
...
...
saxon_zh
@saxon_zh
mentioned in commit
d353e2fe
·
9月 09, 2023
mentioned in commit
d353e2fe
mentioned in commit d353e2fee480702626e7638164911966beb62feb
开关提交列表
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录