@@ -60,24 +60,24 @@ std::tuple<Tensor, Tensor, Tensor> layer_norm_cpu(
60
60
const Tensor& bias = *bias_maybe_owned;
61
61
62
62
63
- auto inputs = _prepare_layer_norm_inputs (input, normalized_shape, weight, bias);
64
- auto X = std::get< 0 >(inputs) ;
65
- auto gamma = std::get< 1 >(inputs) ;
66
- auto beta = std::get< 2 >(inputs );
67
- auto M = std::get< 3 >(inputs );
68
- auto N = std::get< 4 >(inputs );
63
+ auto M_N = _check_layer_norm_inputs (input, normalized_shape, weight, bias);
64
+ auto M = M_N. first ;
65
+ auto N = M_N. second ;
66
+ auto X = input. expect_contiguous ( );
67
+ auto gamma = weight. expect_contiguous ( );
68
+ auto beta = bias. expect_contiguous ( );
69
69
70
70
Tensor Y = at::native::empty_like (
71
- X,
71
+ * X,
72
72
c10::nullopt /* dtype */ ,
73
73
c10::nullopt /* layout */ ,
74
74
c10::nullopt /* device */ ,
75
75
c10::nullopt /* pin_memory */ ,
76
76
at::MemoryFormat::Contiguous);
77
- Tensor mean = at::empty ({M}, X. options ());
78
- Tensor rstd = at::empty ({M}, X. options ());
77
+ Tensor mean = at::empty ({M}, X-> options ());
78
+ Tensor rstd = at::empty ({M}, X-> options ());
79
79
80
- layer_norm_cpu_out (Y, mean, rstd, X, normalized_shape, gamma , beta, eps, M, N);
80
+ layer_norm_cpu_out (Y, mean, rstd, * X, normalized_shape, * gamma , * beta, eps, M, N);
81
81
return std::make_tuple (std::move (Y), std::move (mean), std::move (rstd));
82
82
}
83
83
@@ -86,70 +86,74 @@ std::tuple<Tensor, Tensor, Tensor> layer_norm_backward_cpu(
86
86
const Tensor& input,
87
87
IntArrayRef normalized_shape,
88
88
const Tensor& mean,
89
- const Tensor& rstd, const c10::optional<Tensor>& weight_opt /* optional */ , const c10::optional<Tensor>& bias_opt /* optional */ ,
89
+ const Tensor& rstd,
90
+ const c10::optional<Tensor>& weight_opt /* optional */ ,
91
+ const c10::optional<Tensor>& bias_opt /* optional */ ,
90
92
std::array<bool , 3 > grad_input_mask) {
91
93
// See [Note: hacky wrapper removal for optional tensor]
92
- c10::MaybeOwned<Tensor> weight_maybe_owned = at::borrow_from_optional_tensor (weight_opt);
94
+ c10::MaybeOwned<Tensor> weight_maybe_owned =
95
+ at::borrow_from_optional_tensor (weight_opt);
93
96
const Tensor& weight = *weight_maybe_owned;
94
- const Tensor& bias = c10::value_or_else (bias_opt, [] {return Tensor ();});
95
-
96
-
97
- auto inputs = _prepare_layer_norm_inputs (input, normalized_shape, weight, bias);
98
- auto X = std::get<0 >(inputs);
99
- auto gamma = std::get<1 >(inputs);
100
- auto beta = std::get<2 >(inputs);
101
- auto M = std::get<3 >(inputs);
102
- auto N = std::get<4 >(inputs);
103
-
104
- Tensor dX;
105
- Tensor dgamma;
106
- Tensor dbeta;
107
- if (grad_input_mask[0 ]) {
108
- dX = at::native::empty_like (
109
- X,
110
- c10::nullopt /* dtype */ ,
111
- c10::nullopt /* layout */ ,
112
- c10::nullopt /* device */ ,
113
- c10::nullopt /* pin_memory */ ,
114
- at::MemoryFormat::Contiguous);
115
- }
116
- if (grad_input_mask[1 ]) {
117
- dgamma = M > 0 ? at::native::empty_like (
118
- gamma ,
119
- c10::nullopt /* dtype */ ,
120
- c10::nullopt /* layout */ ,
121
- c10::nullopt /* device */ ,
122
- c10::nullopt /* pin_memory */ ,
123
- at::MemoryFormat::Contiguous)
124
- : at::native::zeros_like (
125
- gamma ,
126
- c10::nullopt /* dtype */ ,
127
- c10::nullopt /* layout */ ,
128
- c10::nullopt /* device */ ,
129
- c10::nullopt /* pin_memory */ ,
130
- at::MemoryFormat::Contiguous);
131
- }
132
- if (grad_input_mask[2 ]) {
133
- dbeta = M > 0 ? at::native::empty_like (
134
- beta,
135
- c10::nullopt /* dtype */ ,
136
- c10::nullopt /* layout */ ,
137
- c10::nullopt /* device */ ,
138
- c10::nullopt /* pin_memory */ ,
139
- at::MemoryFormat::Contiguous)
140
- : at::native::zeros_like (
141
- beta,
142
- c10::nullopt /* dtype */ ,
143
- c10::nullopt /* layout */ ,
144
- c10::nullopt /* device */ ,
145
- c10::nullopt /* pin_memory */ ,
146
- at::MemoryFormat::Contiguous);
147
- }
148
- if (M > 0 ) {
149
- LayerNormBackwardKernel (
150
- kCPU , dY, X, mean, rstd, gamma , M, N, &dX, &dgamma, &dbeta);
151
- }
152
- return std::make_tuple (std::move (dX), std::move (dgamma), std::move (dbeta));
97
+ c10::MaybeOwned<Tensor> bias_maybe_owned =
98
+ at::borrow_from_optional_tensor (bias_opt);
99
+ const Tensor& bias = *bias_maybe_owned;
100
+
101
+ auto M_N = _check_layer_norm_inputs (input, normalized_shape, weight, bias);
102
+ auto M = M_N.first ;
103
+ auto N = M_N.second ;
104
+ auto X = input.expect_contiguous ();
105
+ auto gamma = weight.expect_contiguous ();
106
+ auto beta = bias.expect_contiguous ();
107
+
108
+ Tensor dX;
109
+ Tensor dgamma;
110
+ Tensor dbeta;
111
+ if (grad_input_mask[0 ]) {
112
+ dX = at::native::empty_like (
113
+ *X,
114
+ c10::nullopt /* dtype */ ,
115
+ c10::nullopt /* layout */ ,
116
+ c10::nullopt /* device */ ,
117
+ c10::nullopt /* pin_memory */ ,
118
+ at::MemoryFormat::Contiguous);
119
+ }
120
+ if (grad_input_mask[1 ]) {
121
+ dgamma = M > 0 ? at::native::empty_like (
122
+ *gamma ,
123
+ c10::nullopt /* dtype */ ,
124
+ c10::nullopt /* layout */ ,
125
+ c10::nullopt /* device */ ,
126
+ c10::nullopt /* pin_memory */ ,
127
+ at::MemoryFormat::Contiguous)
128
+ : at::native::zeros_like (
129
+ *gamma ,
130
+ c10::nullopt /* dtype */ ,
131
+ c10::nullopt /* layout */ ,
132
+ c10::nullopt /* device */ ,
133
+ c10::nullopt /* pin_memory */ ,
134
+ at::MemoryFormat::Contiguous);
135
+ }
136
+ if (grad_input_mask[2 ]) {
137
+ dbeta = M > 0 ? at::native::empty_like (
138
+ *beta,
139
+ c10::nullopt /* dtype */ ,
140
+ c10::nullopt /* layout */ ,
141
+ c10::nullopt /* device */ ,
142
+ c10::nullopt /* pin_memory */ ,
143
+ at::MemoryFormat::Contiguous)
144
+ : at::native::zeros_like (
145
+ *beta,
146
+ c10::nullopt /* dtype */ ,
147
+ c10::nullopt /* layout */ ,
148
+ c10::nullopt /* device */ ,
149
+ c10::nullopt /* pin_memory */ ,
150
+ at::MemoryFormat::Contiguous);
151
+ }
152
+ if (M > 0 ) {
153
+ LayerNormBackwardKernel (
154
+ kCPU , dY, *X, mean, rstd, *gamma , M, N, &dX, &dgamma, &dbeta);
155
+ }
156
+ return std::make_tuple (std::move (dX), std::move (dgamma), std::move (dbeta));
153
157
}
154
158
155
159
Tensor layer_norm (
@@ -160,7 +164,8 @@ Tensor layer_norm(
160
164
// See [Note: hacky wrapper removal for optional tensor]
161
165
c10::MaybeOwned<Tensor> weight_maybe_owned = at::borrow_from_optional_tensor (weight_opt);
162
166
const Tensor& weight = *weight_maybe_owned;
163
- const Tensor& bias = c10::value_or_else (bias_opt, [] {return Tensor ();});
167
+ c10::MaybeOwned<Tensor> bias_maybe_owned = at::borrow_from_optional_tensor (bias_opt);
168
+ const Tensor& bias = *bias_maybe_owned;
164
169
165
170
166
171
return std::get<0 >(at::native_layer_norm (input, normalized_shape, weight, bias, eps));
@@ -179,15 +184,14 @@ std::tuple<Tensor, Tensor, Tensor> math_native_layer_norm(
179
184
// See [Note: hacky wrapper removal for optional tensor]
180
185
c10::MaybeOwned<Tensor> weight_maybe_owned = at::borrow_from_optional_tensor (weight_opt);
181
186
const Tensor& weight = *weight_maybe_owned;
182
- const Tensor& bias = c10::value_or_else (bias_opt, [] {return Tensor ();});
183
-
184
- auto inputs = _prepare_layer_norm_inputs (input, normalized_shape, weight, bias);
185
- auto X = std::get<0 >(inputs);
186
- auto gamma = std::get<1 >(inputs);
187
- auto beta = std::get<2 >(inputs);
188
- auto M = std::get<3 >(inputs);
189
- // NOLINTNEXTLINE(clang-diagnostic-unused-variable,clang-analyzer-deadcode.DeadStores)
190
- auto N = std::get<4 >(inputs);
187
+ c10::MaybeOwned<Tensor> bias_maybe_owned = at::borrow_from_optional_tensor (bias_opt);
188
+ const Tensor& bias = *bias_maybe_owned;
189
+
190
+ auto M_N = _check_layer_norm_inputs (input, normalized_shape, weight, bias);
191
+ auto M = M_N.first ;
192
+ auto X = input.expect_contiguous ();
193
+ auto gamma = weight.expect_contiguous ();
194
+
191
195
auto input_shape = input.sizes ();
192
196
const auto input_ndim = input.dim ();
193
197
const int normalized_ndim = normalized_shape.size ();
0 commit comments