Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1bd468e2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
1bd468e2
编写于
4月 27, 2023
作者:
J
JYChen
提交者:
GitHub
4月 27, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Hack__getitem__ from 0-d to 1-d with FLAGS_set_to_1d (#53358)
上级
2c12abd7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
56 addition
and
7 deletion
+56
-7
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+13
-3
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+13
-0
paddle/phi/kernels/funcs/slice_utils.h
paddle/phi/kernels/funcs/slice_utils.h
+21
-2
python/paddle/fft.py
python/paddle/fft.py
+2
-2
python/paddle/fluid/variable_index.py
python/paddle/fluid/variable_index.py
+7
-0
未找到文件。
paddle/fluid/pybind/eager_method.cc
浏览文件 @
1bd468e2
...
@@ -136,17 +136,18 @@ static PyObject* tensor_method_numpy(TensorObject* self,
...
@@ -136,17 +136,18 @@ static PyObject* tensor_method_numpy(TensorObject* self,
}
}
}
}
if
(
set_to_1d
)
{
if
(
set_to_1d
)
{
// 0D Tensor hack process to 1D numpy, will remove in
future
// 0D Tensor hack process to 1D numpy, will remove in
release 2.6
VLOG
(
0
)
VLOG
(
0
)
<<
"Warning:: 0D Tensor cannot be used as 'Tensor.numpy()[0]' . In "
<<
"Warning:: 0D Tensor cannot be used as 'Tensor.numpy()[0]' . In "
"order to avoid this problem, "
"order to avoid this problem, "
"0D Tensor will be changed to 1D numpy currently, but it's not "
"0D Tensor will be changed to 1D numpy currently, but it's not "
"correct and will be "
"correct and will be "
"removed in future. For Tensor contain only one element, Please "
"removed in release 2.6. For Tensor contain only one element, "
"Please "
"modify "
"modify "
" 'Tensor.numpy()[0]' to 'float(Tensor)' as soon as "
" 'Tensor.numpy()[0]' to 'float(Tensor)' as soon as "
"possible, "
"possible, "
"otherwise 'Tensor.numpy()[0]' will raise error in
future
."
;
"otherwise 'Tensor.numpy()[0]' will raise error in
release 2.6
."
;
py_rank
=
1
;
py_rank
=
1
;
py_dims
[
0
]
=
1
;
py_dims
[
0
]
=
1
;
py_strides
[
0
]
=
sizeof_dtype
*
numel
;
py_strides
[
0
]
=
sizeof_dtype
*
numel
;
...
@@ -923,7 +924,16 @@ static PyObject* tensor__getitem_index_not_tensor(TensorObject* self,
...
@@ -923,7 +924,16 @@ static PyObject* tensor__getitem_index_not_tensor(TensorObject* self,
}
}
}
}
bool
set_to_1d
=
FLAGS_set_to_1d
;
if
(
!
none_axes
.
empty
())
{
if
(
!
none_axes
.
empty
())
{
if
(
set_to_1d
)
{
// NOTE(zoooo0820): When all axes are decreased, the output will be 1-D
// with FLAGS_set_to_1d=True. In this case, one `None` should be pop out,
// otherwise the output shape will be not correct.
if
(
static_cast
<
int
>
(
decrease_axis
.
size
())
==
tensor
->
dims
().
size
())
{
none_axes
.
pop_back
();
}
}
if
(
!
none_axes
.
empty
())
{
if
(
!
none_axes
.
empty
())
{
paddle
::
Tensor
new_out
;
paddle
::
Tensor
new_out
;
{
{
...
...
paddle/fluid/pybind/imperative.cc
浏览文件 @
1bd468e2
...
@@ -63,6 +63,7 @@ limitations under the License. */
...
@@ -63,6 +63,7 @@ limitations under the License. */
#include "paddle/phi/core/compat/arg_map_context.h"
#include "paddle/phi/core/compat/arg_map_context.h"
#include "paddle/phi/core/type_defs.h"
#include "paddle/phi/core/type_defs.h"
PHI_DECLARE_bool
(
set_to_1d
);
namespace
paddle
{
namespace
paddle
{
namespace
pybind
{
namespace
pybind
{
...
@@ -1064,7 +1065,19 @@ void BindImperative(py::module *m_ptr) {
...
@@ -1064,7 +1065,19 @@ void BindImperative(py::module *m_ptr) {
}
}
tracer
->
TraceOp
(
op_type
,
ins
,
outs
,
std
::
move
(
attrs
));
tracer
->
TraceOp
(
op_type
,
ins
,
outs
,
std
::
move
(
attrs
));
}
}
bool
set_to_1d
=
FLAGS_set_to_1d
;
if
(
!
none_axes
.
empty
())
{
if
(
!
none_axes
.
empty
())
{
if
(
set_to_1d
)
{
// NOTE(zoooo0820): When all axes are decreased, the output
// will be 1-D with FLAGS_set_to_1d=True. In this case, one
// `None` should be pop out, otherwise the output shape will be
// not correct.
if
(
static_cast
<
int
>
(
decrease_axis
.
size
())
==
tensor
->
dims
().
size
())
{
none_axes
.
pop_back
();
}
}
if
(
!
none_axes
.
empty
())
{
if
(
!
none_axes
.
empty
())
{
// Deal with cases that decrease_axes is not empty
// Deal with cases that decrease_axes is not empty
// For example:
// For example:
...
...
paddle/phi/kernels/funcs/slice_utils.h
浏览文件 @
1bd468e2
...
@@ -13,10 +13,13 @@ See the License for the specific language governing permissions and
...
@@ -13,10 +13,13 @@ See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#pragma once
#pragma once
#include <glog/logging.h>
#include <paddle/phi/core/ddim.h>
#include <paddle/phi/core/ddim.h>
#include <string>
#include <string>
#include <vector>
#include <vector>
#include "paddle/phi/core/flags.h"
PHI_DECLARE_bool
(
set_to_1d
);
namespace
phi
{
namespace
phi
{
...
@@ -202,7 +205,23 @@ inline DDim GetDecreasedDims(const DDim slice_dims,
...
@@ -202,7 +205,23 @@ inline DDim GetDecreasedDims(const DDim slice_dims,
new_shape
.
push_back
(
decreased_dims
[
i
]);
new_shape
.
push_back
(
decreased_dims
[
i
]);
}
}
}
}
if
(
FLAGS_set_to_1d
&&
new_shape
.
size
()
==
0
)
{
// NOTE(zoooo0820): Hack procssing to 1-D, when axes decrease to 0-D in
// slice. This will remove in release 2.6.
VLOG
(
0
)
<<
"Warning:: In Tensor '__getitem__', if the number of scalar "
"elements "
"in the index is equal to the rank of the Tensor, the output "
"should "
"be 0-D. In order to be consistent with the behavior of previous "
"versions, it will be processed to 1-D. But it is not correct and "
"will be "
"removed in release 2.6. "
"If 1-D is still wanted, please modify the index element from "
"scalar to slice "
"(e.g. 'x[i]' => 'x[i:i+1]'). "
;
new_shape
.
push_back
(
1
);
}
decreased_dims
=
phi
::
make_ddim
(
new_shape
);
decreased_dims
=
phi
::
make_ddim
(
new_shape
);
}
}
return
decreased_dims
;
return
decreased_dims
;
...
...
python/paddle/fft.py
浏览文件 @
1bd468e2
...
@@ -1371,7 +1371,7 @@ def fftshift(x, axes=None, name=None):
...
@@ -1371,7 +1371,7 @@ def fftshift(x, axes=None, name=None):
elif
isinstance
(
axes
,
int
):
elif
isinstance
(
axes
,
int
):
shifts
=
shape
[
axes
]
//
2
shifts
=
shape
[
axes
]
//
2
else
:
else
:
shifts
=
paddle
.
stack
([
shape
[
ax
]
//
2
for
ax
in
axes
])
shifts
=
paddle
.
concat
([
shape
[
ax
:
ax
+
1
]
//
2
for
ax
in
axes
])
return
paddle
.
roll
(
x
,
shifts
,
axes
,
name
=
name
)
return
paddle
.
roll
(
x
,
shifts
,
axes
,
name
=
name
)
...
@@ -1416,7 +1416,7 @@ def ifftshift(x, axes=None, name=None):
...
@@ -1416,7 +1416,7 @@ def ifftshift(x, axes=None, name=None):
elif
isinstance
(
axes
,
int
):
elif
isinstance
(
axes
,
int
):
shifts
=
-
shape
[
axes
]
//
2
shifts
=
-
shape
[
axes
]
//
2
else
:
else
:
shifts
=
paddle
.
stack
([
-
shape
[
ax
]
//
2
for
ax
in
axes
])
shifts
=
paddle
.
concat
([
-
shape
[
ax
:
ax
+
1
]
//
2
for
ax
in
axes
])
return
paddle
.
roll
(
x
,
shifts
,
axes
,
name
=
name
)
return
paddle
.
roll
(
x
,
shifts
,
axes
,
name
=
name
)
...
...
python/paddle/fluid/variable_index.py
浏览文件 @
1bd468e2
...
@@ -574,6 +574,13 @@ def _getitem_impl_(var, item):
...
@@ -574,6 +574,13 @@ def _getitem_impl_(var, item):
out
=
reverse
(
out
,
axis
=
reverse_axes
)
out
=
reverse
(
out
,
axis
=
reverse_axes
)
# NOTE(zoooo0820): When all axes are decreased, the output will be 1-D
# with FLAGS_set_to_1d=True. In this case, one `None` should be pop out,
# otherwise the output shape will be not correct.
set_to_1d
=
paddle
.
get_flags
(
'FLAGS_set_to_1d'
)[
'FLAGS_set_to_1d'
]
if
set_to_1d
and
len
(
decrease_axes
)
==
len
(
var
.
shape
):
none_axes
=
none_axes
[
1
:]
if
len
(
none_axes
)
>
0
:
if
len
(
none_axes
)
>
0
:
# Deal with cases that decrease_axes is not empty
# Deal with cases that decrease_axes is not empty
# For example:
# For example:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录