Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
fd4a97d1
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fd4a97d1
编写于
12月 06, 2022
作者:
K
kuizhiqing
提交者:
Wei Shengyu
12月 07, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lazy roll
上级
a49e11db
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
18 addition
and
1 deletion
+18
-1
ppcls/arch/backbone/legendary_models/swin_transformer.py
ppcls/arch/backbone/legendary_models/swin_transformer.py
+18
-1
未找到文件。
ppcls/arch/backbone/legendary_models/swin_transformer.py
浏览文件 @
fd4a97d1
...
@@ -42,6 +42,9 @@ MODEL_URLS = {
...
@@ -42,6 +42,9 @@ MODEL_URLS = {
__all__
=
list
(
MODEL_URLS
.
keys
())
__all__
=
list
(
MODEL_URLS
.
keys
())
# The following re-implementation of roll is inspired by
# https://gitee.com/ascend/pytorch/blob/master/torch_npu/contrib/function/roll.py
class
RollWithIndexSelect
(
paddle
.
autograd
.
PyLayer
):
class
RollWithIndexSelect
(
paddle
.
autograd
.
PyLayer
):
@
staticmethod
@
staticmethod
def
forward
(
ctx
,
input1
,
index_fp
,
index_bp
):
def
forward
(
ctx
,
input1
,
index_fp
,
index_bp
):
...
@@ -62,6 +65,7 @@ class RollWithIndexSelect(paddle.autograd.PyLayer):
...
@@ -62,6 +65,7 @@ class RollWithIndexSelect(paddle.autograd.PyLayer):
roll_with_index_select
=
RollWithIndexSelect
.
apply
roll_with_index_select
=
RollWithIndexSelect
.
apply
def
get_roll_index
(
H
,
W
,
shifts
,
place
):
def
get_roll_index
(
H
,
W
,
shifts
,
place
):
# following tensors will be created on cpu place with npu custom device
index
=
paddle
.
arange
(
0
,
H
*
W
,
dtype
=
'int64'
).
reshape
([
H
,
W
])
# cpu
index
=
paddle
.
arange
(
0
,
H
*
W
,
dtype
=
'int64'
).
reshape
([
H
,
W
])
# cpu
index_fp
=
paddle
.
roll
(
index
,
shifts
=
shifts
,
axis
=
(
0
,
1
)).
reshape
([
-
1
])
# cpu
index_fp
=
paddle
.
roll
(
index
,
shifts
=
shifts
,
axis
=
(
0
,
1
)).
reshape
([
-
1
])
# cpu
index_bp
=
{
i
:
idx
for
idx
,
i
in
enumerate
(
index_fp
.
numpy
().
tolist
())}
index_bp
=
{
i
:
idx
for
idx
,
i
in
enumerate
(
index_fp
.
numpy
().
tolist
())}
...
@@ -85,7 +89,14 @@ class NpuRollWithIndexSelect():
...
@@ -85,7 +89,14 @@ class NpuRollWithIndexSelect():
index_fp
,
index_bp
=
self
.
index_dict
[
key
]
index_fp
,
index_bp
=
self
.
index_dict
[
key
]
return
roll_with_index_select
(
x
,
index_fp
,
index_bp
)
return
roll_with_index_select
(
x
,
index_fp
,
index_bp
)
roll
=
NpuRollWithIndexSelect
()
if
'npu'
in
paddle
.
device
.
get_all_custom_device_type
()
else
paddle
.
roll
roll
=
None
def
_lazy_init_roll
(
x
):
global
roll
if
'npu'
in
paddle
.
device
.
get_all_custom_device_type
()
and
hasattr
(
x
,
'_place_str'
)
and
'npu'
in
x
.
_place_str
:
roll
=
NpuRollWithIndexSelect
()
else
:
roll
=
paddle
.
roll
class
Mlp
(
nn
.
Layer
):
class
Mlp
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -400,6 +411,9 @@ class SwinTransformerBlock(nn.Layer):
...
@@ -400,6 +411,9 @@ class SwinTransformerBlock(nn.Layer):
# cyclic shift
# cyclic shift
if
self
.
shift_size
>
0
:
if
self
.
shift_size
>
0
:
if
roll
is
None
:
_lazy_init_roll
(
x
)
shifted_x
=
roll
(
shifted_x
=
roll
(
x
,
shifts
=
(
-
self
.
shift_size
,
-
self
.
shift_size
),
axis
=
(
1
,
2
))
x
,
shifts
=
(
-
self
.
shift_size
,
-
self
.
shift_size
),
axis
=
(
1
,
2
))
else
:
else
:
...
@@ -424,6 +438,9 @@ class SwinTransformerBlock(nn.Layer):
...
@@ -424,6 +438,9 @@ class SwinTransformerBlock(nn.Layer):
# reverse cyclic shift
# reverse cyclic shift
if
self
.
shift_size
>
0
:
if
self
.
shift_size
>
0
:
if
roll
is
None
:
_lazy_init_roll
(
shifted_x
)
x
=
roll
(
x
=
roll
(
shifted_x
,
shifted_x
,
shifts
=
(
self
.
shift_size
,
self
.
shift_size
),
shifts
=
(
self
.
shift_size
,
self
.
shift_size
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录