Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow
提交
15817f19
O
oneflow
项目概览
Oneflow-Inc
/
oneflow
上一次同步 接近 3 年
通知
13
Star
2733
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
15817f19
编写于
12月 28, 2020
作者:
Y
YongtaoShi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code style
Former-commit-id: cb374a84fcfe12b6f1e6a6654896c9a276ea8538
上级
591a14c4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
30 addition
and
89 deletion
+30
-89
oneflow/user/kernels/unfold_kernel.cpp
oneflow/user/kernels/unfold_kernel.cpp
+23
-82
oneflow/user/kernels/unfold_kernel_util.cpp
oneflow/user/kernels/unfold_kernel_util.cpp
+1
-1
oneflow/user/kernels/unfold_kernel_util.cu
oneflow/user/kernels/unfold_kernel_util.cu
+1
-1
oneflow/user/kernels/unfold_kernel_util.h
oneflow/user/kernels/unfold_kernel_util.h
+5
-5
未找到文件。
oneflow/user/kernels/unfold_kernel.cpp
浏览文件 @
15817f19
...
...
@@ -24,77 +24,14 @@ namespace user_op {
namespace
{
class
UnfoldOpKernelState
final
:
public
user_op
::
OpKernelState
{
public:
UnfoldOpKernelState
()
{}
template
<
size_t
NDims
>
static
std
::
shared_ptr
<
user_op
::
OpKernelState
>
DoCreateOpKernelState
(
user_op
::
KernelInitContext
*
ctx
)
{
const
Shape
&
x_shape
=
ctx
->
TensorDesc4ArgNameAndIndex
(
"x"
,
0
)
->
shape
();
const
std
::
string
&
data_format
=
ctx
->
Attr
<
std
::
string
>
(
"data_format"
);
std
::
vector
<
int32_t
>
padding_before
=
ctx
->
Attr
<
std
::
vector
<
int32_t
>>
(
"padding_before"
);
std
::
vector
<
int32_t
>
padding_after
=
ctx
->
Attr
<
std
::
vector
<
int32_t
>>
(
"padding_after"
);
const
std
::
vector
<
int32_t
>&
kernel_size
=
ctx
->
Attr
<
std
::
vector
<
int32_t
>>
(
"kernel_size"
);
const
std
::
vector
<
int32_t
>&
strides
=
ctx
->
Attr
<
std
::
vector
<
int32_t
>>
(
"strides"
);
const
std
::
vector
<
int32_t
>&
dilation_rate
=
ctx
->
Attr
<
std
::
vector
<
int32_t
>>
(
"dilation_rate"
);
const
int32_t
idx_offset
=
IdxOffset
(
data_format
);
const
size_t
c_dim
=
data_format
==
"channels_first"
?
1
:
NDims
+
1
;
std
::
shared_ptr
<
UnfoldOpKernelState
>
state
(
new
UnfoldOpKernelState
());
auto
Gen5DShape
=
[](
const
Shape
&
shape
,
int32_t
idx_offset
)
->
Shape
{
DimVector
ret_vec
(
shape
.
dim_vec
());
int32_t
ndims
=
ret_vec
.
size
()
-
2
;
ret_vec
.
insert
(
ret_vec
.
begin
()
+
idx_offset
,
3
-
ndims
,
1
);
return
Shape
(
ret_vec
);
};
auto
Gen3DVec
=
[](
const
std
::
vector
<
int32_t
>&
origin_vec
,
int32_t
num
)
->
std
::
vector
<
int32_t
>
{
std
::
vector
<
int32_t
>
ret_vec
=
origin_vec
;
ret_vec
.
insert
(
ret_vec
.
begin
(),
3
-
ret_vec
.
size
(),
num
);
return
ret_vec
;
};
DimVector
native_shape
(
NDims
+
2
);
native_shape
.
at
(
0
)
=
x_shape
.
At
(
0
);
native_shape
.
at
(
c_dim
)
=
x_shape
.
At
(
c_dim
);
for
(
int32_t
i
=
0
;
i
<
NDims
;
++
i
)
{
native_shape
[
idx_offset
+
i
]
=
(
x_shape
.
At
(
idx_offset
+
i
)
+
padding_before
[
i
]
+
padding_after
[
i
]
-
dilation_rate
[
i
]
*
(
kernel_size
[
i
]
-
1
)
-
1
)
/
strides
[
i
]
+
1
;
}
state
->
in_5d_shape_
=
Gen5DShape
(
x_shape
,
idx_offset
);
state
->
out_5d_shape_
=
Gen5DShape
(
Shape
(
native_shape
),
idx_offset
);
state
->
out_shape_
=
ctx
->
TensorDesc4ArgNameAndIndex
(
"y"
,
0
)
->
shape
();
state
->
kernel_size_3d_
=
Gen3DVec
(
kernel_size
,
1
);
state
->
strides_3d_
=
Gen3DVec
(
strides
,
1
);
state
->
dilation_rate_3d_
=
Gen3DVec
(
dilation_rate
,
1
);
state
->
padding_before_3d_
=
Gen3DVec
(
padding_before
,
0
);
return
std
::
move
(
state
);
}
public:
Shape
in_5d_shape_
;
Shape
out_5d_shape_
;
Shape
out_shape_
;
std
::
vector
<
int32_t
>
kernel_size_3d_
;
std
::
vector
<
int32_t
>
strides_3d_
;
std
::
vector
<
int32_t
>
dilation_rate_3d_
;
std
::
vector
<
int32_t
>
padding_before_3d_
;
};
template
<
typename
INDEX_T
,
int
NDIM
,
int
SDIM
>
class
UnfoldOpKernelState
V2
:
public
OpKernelState
{
class
UnfoldOpKernelState
:
public
OpKernelState
{
public:
using
ParamType
=
UnfoldParams
<
INDEX_T
,
NDIM
,
SDIM
>
;
UnfoldOpKernelState
V2
(
const
ShapeView
&
input_shape
,
const
std
::
vector
<
int32_t
>&
kernel_size
,
const
std
::
vector
<
int32_t
>&
padding_before
,
const
std
::
vector
<
int32_t
>&
padding_after
,
const
std
::
vector
<
int32_t
>&
stride
,
const
std
::
vector
<
int32_t
>&
dilation
)
UnfoldOpKernelState
(
const
ShapeView
&
input_shape
,
const
std
::
vector
<
int32_t
>&
kernel_size
,
const
std
::
vector
<
int32_t
>&
padding_before
,
const
std
::
vector
<
int32_t
>&
padding_after
,
const
std
::
vector
<
int32_t
>&
stride
,
const
std
::
vector
<
int32_t
>&
dilation
)
:
params_
(
input_shape
.
At
(
0
),
input_shape
.
At
(
ParamType
::
kInputChannelDim
),
input_shape
.
ptr
()
+
SDIM
,
kernel_size
.
data
(),
padding_before
.
data
(),
padding_after
.
data
(),
stride
.
data
(),
dilation
.
data
())
{}
...
...
@@ -111,13 +48,13 @@ std::shared_ptr<OpKernelState> CreateUnfoldOpKernelState(const ShapeView& input_
const
std
::
vector
<
int32_t
>&
padding_after
,
const
std
::
vector
<
int32_t
>&
stride
,
const
std
::
vector
<
int32_t
>&
dilation
)
{
return
std
::
make_shared
<
UnfoldOpKernelState
V2
<
INDEX_T
,
NDIM
,
SDIM
>>
(
return
std
::
make_shared
<
UnfoldOpKernelState
<
INDEX_T
,
NDIM
,
SDIM
>>
(
input_shape
,
kernel_size
,
padding_before
,
padding_after
,
stride
,
dilation
);
}
template
<
typename
INDEX_T
,
int
NDIM
,
int
SDIM
>
const
void
*
GetUnfoldParams
(
OpKernelState
*
state
)
{
auto
*
unfold_state
=
dynamic_cast
<
UnfoldOpKernelState
V2
<
INDEX_T
,
NDIM
,
SDIM
>*>
(
state
);
auto
*
unfold_state
=
dynamic_cast
<
UnfoldOpKernelState
<
INDEX_T
,
NDIM
,
SDIM
>*>
(
state
);
CHECK_NOTNULL
(
unfold_state
);
return
static_cast
<
const
void
*>
(
&
unfold_state
->
params
());
}
...
...
@@ -133,10 +70,10 @@ DEFINE_UNFOLD_SWITCH_FUNC(const void*, GetUnfoldParams);
#undef SWITCH_ENTRY
template
<
DeviceType
device_type
,
typename
T
>
class
UnfoldKernel
V2
final
:
public
OpKernel
{
class
UnfoldKernel
final
:
public
OpKernel
{
public:
UnfoldKernel
V2
()
=
default
;
~
UnfoldKernel
V2
()
=
default
;
UnfoldKernel
()
=
default
;
~
UnfoldKernel
()
=
default
;
std
::
shared_ptr
<
OpKernelState
>
CreateOpKernelState
(
KernelInitContext
*
ctx
)
const
override
{
const
TensorDesc
*
input_desc
=
ctx
->
TensorDesc4ArgNameAndIndex
(
"x"
,
0
);
...
...
@@ -171,7 +108,7 @@ class UnfoldKernelV2 final : public OpKernel {
}
#define SWITCH_ENTRY(func_name, itype, ndim, sdim) \
UnfoldKernelUtil
V2
<device_type, T, itype, ndim, sdim>::func_name
UnfoldKernelUtil<device_type, T, itype, ndim, sdim>::func_name
DEFINE_STATIC_SWITCH_FUNC
(
void
,
Forward
,
SWITCH_ENTRY
,
MAKE_DATA_TYPE_CTRV_SEQ
(
INDEX_DATA_TYPE_SEQ
),
MAKE_NDIM_CTRV_SEQ
(
SPATIAL_NDIM_SEQ
),
...
...
@@ -226,17 +163,21 @@ class UnfoldGradKernel final : public user_op::OpKernel {
}
// namespace
#define REGISTER_UNFOLD_KERNEL_V2(device, dtype) \
REGISTER_USER_KERNEL("unfold").SetCreateFn<UnfoldKernelV2<device, dtype>>().SetIsMatchedHob( \
(user_op::HobDeviceTag() == device) \
& (user_op::HobDataType("x", 0) == GetDataType<dtype>::value));
#define REGISTER_UNFOLD_KERNEL(device, dtype) \
REGISTER_USER_KERNEL("unfold").SetCreateFn<UnfoldKernel<device, dtype>>().SetIsMatchedHob( \
(user_op::HobDeviceTag() == device) \
& (user_op::HobDataType("x", 0) == GetDataType<dtype>::value)); \
REGISTER_USER_KERNEL("unfold_grad") \
.SetCreateFn<UnfoldGradKernel<device, dtype>>() \
.SetIsMatchedHob((user_op::HobDeviceTag() == device) \
& (user_op::HobDataType("x", 0) == GetDataType<dtype>::value));
REGISTER_UNFOLD_KERNEL
_V2
(
DeviceType
::
kCPU
,
float
)
REGISTER_UNFOLD_KERNEL
_V2
(
DeviceType
::
kCPU
,
double
)
REGISTER_UNFOLD_KERNEL
(
DeviceType
::
kCPU
,
float
)
REGISTER_UNFOLD_KERNEL
(
DeviceType
::
kCPU
,
double
)
#ifdef WITH_CUDA
REGISTER_UNFOLD_KERNEL
_V2
(
DeviceType
::
kGPU
,
float
)
REGISTER_UNFOLD_KERNEL
_V2
(
DeviceType
::
kGPU
,
double
)
REGISTER_UNFOLD_KERNEL
(
DeviceType
::
kGPU
,
float
)
REGISTER_UNFOLD_KERNEL
(
DeviceType
::
kGPU
,
double
)
#endif // WITH_CUDA
}
// namespace user_op
...
...
oneflow/user/kernels/unfold_kernel_util.cpp
浏览文件 @
15817f19
...
...
@@ -20,7 +20,7 @@ namespace oneflow {
namespace
user_op
{
template
<
typename
T
,
typename
INDEX_T
,
int
NDIM
,
int
SDIM
>
struct
UnfoldKernelUtil
V2
<
DeviceType
::
kCPU
,
T
,
INDEX_T
,
NDIM
,
SDIM
>
{
struct
UnfoldKernelUtil
<
DeviceType
::
kCPU
,
T
,
INDEX_T
,
NDIM
,
SDIM
>
{
using
ParamType
=
UnfoldParams
<
INDEX_T
,
NDIM
,
SDIM
>
;
static
void
Forward
(
DeviceCtx
*
ctx
,
const
void
*
raw_params
,
const
T
*
input_ptr
,
T
*
output_ptr
)
{
const
auto
*
params
=
static_cast
<
const
ParamType
*>
(
raw_params
);
...
...
oneflow/user/kernels/unfold_kernel_util.cu
浏览文件 @
15817f19
...
...
@@ -53,7 +53,7 @@ __global__ void CudaUnfoldForward(UnfoldParams<INDEX_T, NDIM, SDIM> params, cons
}
// namespace
template
<
typename
T
,
typename
INDEX_T
,
int
NDIM
,
int
SDIM
>
struct
UnfoldKernelUtil
V2
<
DeviceType
::
kGPU
,
T
,
INDEX_T
,
NDIM
,
SDIM
>
{
struct
UnfoldKernelUtil
<
DeviceType
::
kGPU
,
T
,
INDEX_T
,
NDIM
,
SDIM
>
{
using
ParamType
=
UnfoldParams
<
INDEX_T
,
NDIM
,
SDIM
>
;
static
void
Forward
(
DeviceCtx
*
ctx
,
const
void
*
params
,
const
T
*
input_ptr
,
T
*
output_ptr
)
{
const
auto
*
unfold_params
=
static_cast
<
const
ParamType
*>
(
params
);
...
...
oneflow/user/kernels/unfold_kernel_util.h
浏览文件 @
15817f19
...
...
@@ -111,19 +111,19 @@ OF_DEVICE_FUNC bool UnfoldIndexTransform(const UnfoldParams<INDEX_T, NDIM, SDIM>
}
template
<
DeviceType
device_type
,
typename
T
,
typename
INDEX_T
,
int
NDIM
,
int
SDIM
>
struct
UnfoldKernelUtil
V2
{
struct
UnfoldKernelUtil
{
static
void
Forward
(
DeviceCtx
*
ctx
,
const
void
*
params
,
const
T
*
input_ptr
,
T
*
output_ptr
);
};
#define SPATIAL_NDIM_SEQ OF_PP_MAKE_TUPLE_SEQ(1) OF_PP_MAKE_TUPLE_SEQ(2) OF_PP_MAKE_TUPLE_SEQ(3)
#define SPATIAL_DIM_SEQ OF_PP_MAKE_TUPLE_SEQ(1) OF_PP_MAKE_TUPLE_SEQ(2)
#define INSTANTIATE_UNFOLD_KERNEL_UTIL
_V2
(device, dtype, itype, ndim, sdim) \
template struct UnfoldKernelUtil
V2
<device, dtype, itype, ndim, sdim>;
#define INSTANTIATE_UNFOLD_KERNEL_UTIL(device, dtype, itype, ndim, sdim) \
template struct UnfoldKernelUtil<device, dtype, itype, ndim, sdim>;
#define INSTANTIATE_UNFOLD_KERNEL_UTIL_WITH_TYPE_PAIR(device, dtype_pair, itype_pair, ndim, sdim) \
INSTANTIATE_UNFOLD_KERNEL_UTIL
_V2(device, OF_PP_PAIR_FIRST(dtype_pair),
\
OF_PP_PAIR_FIRST(itype_pair), ndim, sdim)
INSTANTIATE_UNFOLD_KERNEL_UTIL
(device, OF_PP_PAIR_FIRST(dtype_pair),
\
OF_PP_PAIR_FIRST(itype_pair), ndim, sdim)
#define INSTANTIATE_UNFOLD_KERNEL_UTIL_FOR_DEVICE(device) \
OF_PP_SEQ_PRODUCT_FOR_EACH_TUPLE(INSTANTIATE_UNFOLD_KERNEL_UTIL_WITH_TYPE_PAIR, (device), \
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录