-
Notifications
You must be signed in to change notification settings - Fork 656
/
pad2d_ops.cpp
155 lines (144 loc) · 11.2 KB
/
pad2d_ops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2020 The OneFlow Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneflow/core/common/balanced_splitter.h"
#include "oneflow/core/framework/framework.h"
#include "oneflow/user/ops/nn_util.h"
#include "pad2d_seq.h"
namespace oneflow {
namespace {
Maybe<void> GetOpSbpSignature(user_op::SbpContext* ctx) {
const user_op::TensorDesc& x_tensor = ctx->LogicalTensorDesc4InputArgNameAndIndex("x", 0);
const int64_t input_dims = x_tensor.shape().NumAxes();
CHECK_EQ_OR_RETURN(input_dims, 4);
// NOTE(Liang Depeng): assume data format is NCHW.
const int64_t first_two_dims = input_dims - 2;
FOR_RANGE(int64_t, i, 0, first_two_dims) {
ctx->NewBuilder().Split(ctx->inputs(), i).Split(ctx->outputs(), i).Build();
}
return Maybe<void>::Ok();
}
Maybe<void> GetOpGradSbpSignature(user_op::SbpContext* ctx) {
const user_op::TensorDesc& dy_tensor = ctx->LogicalTensorDesc4InputArgNameAndIndex("dy", 0);
const int64_t grad_dims = dy_tensor.shape().NumAxes();
CHECK_EQ_OR_RETURN(grad_dims, 4);
const int64_t first_two_dims = grad_dims - 2;
FOR_RANGE(int64_t, i, 0, first_two_dims) {
ctx->NewBuilder().Split(ctx->inputs(), i).Split(ctx->outputs(), i).Build();
}
return Maybe<void>::Ok();
}
} // namespace
#define REGISTER_PAD_2D_OP_AND_GRAD(pad_2d_type) \
REGISTER_USER_OP(pad_2d_type) \
.Input("x") \
.Output("y") \
.Attr<std::vector<int64_t>>("padding") \
.Attr<double>("floating_value") \
.Attr<int64_t>("integral_value") \
.SetTensorDescInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
const Shape& x_shape = ctx->InputShape("x", 0); \
const auto& padding = ctx->Attr<std::vector<int64_t>>("padding"); \
CHECK_EQ_OR_RETURN(padding.size(), x_shape.NumAxes()); \
const int64_t n_idx = 0; \
const int64_t c_idx = 1; \
const int64_t h_idx = 2; \
const int64_t w_idx = 3; \
CHECK_LT_OR_RETURN(padding[0], x_shape.At(w_idx)); \
CHECK_LT_OR_RETURN(padding[1], x_shape.At(w_idx)); \
CHECK_LT_OR_RETURN(padding[2], x_shape.At(h_idx)); \
CHECK_LT_OR_RETURN(padding[3], x_shape.At(h_idx)); \
\
DimVector y_dim_vec(x_shape.NumAxes()); \
const int64_t h_x = x_shape.At(h_idx); \
const int64_t w_x = x_shape.At(w_idx); \
\
y_dim_vec[n_idx] = x_shape.At(n_idx); \
y_dim_vec[c_idx] = x_shape.At(c_idx); \
y_dim_vec[h_idx] = h_x + padding[2] + padding[3]; \
y_dim_vec[w_idx] = w_x + padding[0] + padding[1]; \
\
*ctx->OutputShape("y", 0) = Shape(y_dim_vec); \
return Maybe<void>::Ok(); \
}) \
.SetDataTypeInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
*ctx->OutputDType("y", 0) = *ctx->Dtype4ArgNameAndIndex("x", 0); \
return Maybe<void>::Ok(); \
}) \
.SetGetSbpFn(GetOpSbpSignature) \
.SetInputArgModifyFn([](user_op::GetInputArgModifier GetInputArgModifierFn, \
const user_op::UserOpConfWrapper&) { \
user_op::InputArgModifier* x_modifier = GetInputArgModifierFn("x", 0); \
CHECK_NOTNULL(x_modifier); \
x_modifier->set_requires_grad(true); \
}) \
.SetDataTypeInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
*ctx->OutputDType("y", 0) = *ctx->Dtype4ArgNameAndIndex("x", 0); \
return Maybe<void>::Ok(); \
}); \
\
REGISTER_USER_OP((std::string("") + pad_2d_type + "_grad")) \
.Input("dy") \
.Output("dx") \
.Attr<std::vector<int64_t>>("padding") \
.Attr<double>("floating_value") \
.Attr<int64_t>("integral_value") \
.SetTensorDescInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
const Shape& dy_shape = ctx->InputShape("dy", 0); \
const auto& padding = ctx->Attr<std::vector<int64_t>>("padding"); \
CHECK_EQ_OR_RETURN(padding.size(), dy_shape.NumAxes()); \
const int64_t n_idx = 0; \
const int64_t c_idx = 1; \
const int64_t h_idx = 2; \
const int64_t w_idx = 3; \
\
DimVector dx_dim_vec(dy_shape.NumAxes()); \
int64_t h_dy, w_dy; \
h_dy = dy_shape.At(h_idx); \
w_dy = dy_shape.At(w_idx); \
\
dx_dim_vec[n_idx] = dy_shape.At(0); \
dx_dim_vec[c_idx] = dy_shape.At(1); \
dx_dim_vec[h_idx] = h_dy - padding[2] - padding[3]; \
dx_dim_vec[w_idx] = w_dy - padding[0] - padding[1]; \
\
*ctx->OutputShape("dx", 0) = Shape(dx_dim_vec); \
return Maybe<void>::Ok(); \
}) \
.SetDataTypeInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
*ctx->OutputDType("dx", 0) = *ctx->Dtype4ArgNameAndIndex("dy", 0); \
return Maybe<void>::Ok(); \
}) \
.SetGetSbpFn(GetOpGradSbpSignature) \
.SetDataTypeInferFn([](user_op::InferContext* ctx) -> Maybe<void> { \
*ctx->OutputDType("dx", 0) = *ctx->Dtype4ArgNameAndIndex("dy", 0); \
return Maybe<void>::Ok(); \
}); \
\
REGISTER_USER_OP_GRAD(pad_2d_type) \
.SetGenBackwardOpConfFn([](const user_op::UserOpWrapper& op, user_op::AddOpFn AddOp) { \
if (op.NeedGenGradTensor4OpInput("x", 0)) { \
user_op::UserOpConfWrapperBuilder builder(op.op_name() + "_grad"); \
user_op::UserOpConfWrapper grad_op = \
builder.Op((std::string("") + pad_2d_type + "_grad")) \
.Input("dy", op.GetGradTensorWithOpOutput("y", 0)) \
.Output("dx") \
.Attr("padding", op.attr<std::vector<int64_t>>("padding")) \
.Attr("floating_value", op.attr<double>("floating_value")) \
.Attr("integral_value", op.attr<int64_t>("integral_value")) \
.Build(); \
op.BindGradTensorWithOpInput(grad_op.output("dx", 0), "x", 0); \
AddOp(grad_op); \
} \
});
OF_PP_FOR_EACH_TUPLE(REGISTER_PAD_2D_OP_AND_GRAD, PAD_2D_TYPE_SEQ)
} // namespace oneflow