Skip to content

Commit 00333c4

Browse files
jiayisunxpytorchmergebot
authored andcommitted
[Inductor] Set prop_kind to forward_inference when grad is not needed for mkldnn_linear_pointwise and mkldnn_convolution_pointwise (pytorch#147072)
Summary: The `prop_kind` of `mkldnn._linear_pointwise`, `mkldnn._linear_pointwise.binary`, `mkldnn._convolution_pointwise.binary` and `mkldnn._convolution_pointwise_.binary` are always `dnnl_forward`, i.e., `dnnl_forward_training` , regardless of whether `grad` is needed. Setting `prop_kind` to `dnnl_forward_inference` for these ops when `grad` is not needed could have better performance. Pull Request resolved: pytorch#147072 Approved by: https://github.com/leslie-fang-intel, https://github.com/CaoE, https://github.com/jansel
1 parent c4d59e6 commit 00333c4

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

aten/src/ATen/native/mkldnn/Conv.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ Tensor mkldnn_convolution_pointwise_binary(
440440
po.append_eltwise(unary_alg, 0.f, 0.f);
441441
}
442442
op_attr.set_post_ops(po);
443+
auto aprop_kind = ideep::prop_kind::forward_inference;
443444

444445
if (bias.defined()) {
445446
const ideep::tensor b = itensor_from_tensor(bias);
@@ -456,7 +457,9 @@ Tensor mkldnn_convolution_pointwise_binary(
456457
padding_expanded,
457458
groups,
458459
/* is_channels_last */ true,
459-
op_attr);
460+
op_attr,
461+
ideep::algorithm::convolution_direct,
462+
aprop_kind);
460463
} else {
461464
ideep::convolution_forward::compute_binary(
462465
x,
@@ -470,7 +473,9 @@ Tensor mkldnn_convolution_pointwise_binary(
470473
padding_expanded,
471474
groups,
472475
/* is_channels_last */ true,
473-
op_attr);
476+
op_attr,
477+
ideep::algorithm::convolution_direct,
478+
aprop_kind);
474479
}
475480
return output;
476481
} else {
@@ -573,6 +578,7 @@ Tensor& mkldnn_convolution_pointwise_binary_(
573578
} else {
574579
op_attr = ideep::attr_t::fuse_sum();
575580
}
581+
auto aprop_kind = ideep::prop_kind::forward_inference;
576582
_mkldnn_convolution_out(
577583
input_t,
578584
weight_t,
@@ -584,7 +590,8 @@ Tensor& mkldnn_convolution_pointwise_binary_(
584590
padding_expanded,
585591
groups,
586592
true,
587-
op_attr);
593+
op_attr,
594+
aprop_kind);
588595
} else {
589596
// Fallback case, if inputs are not channels last or have different dtype,
590597
// OneDNN fusion may have performance regression.

aten/src/ATen/native/mkldnn/Linear.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ Tensor mkldnn_linear_pointwise(
187187
std::string_view attr,
188188
c10::List<std::optional<at::Scalar>> scalars,
189189
std::optional<std::string_view> algorithm) {
190+
auto aprop_kind = ideep::prop_kind::forward;
191+
bool maybe_backward = GradMode::is_enabled() &&
192+
(input_t.requires_grad() || weight_t.requires_grad() ||
193+
(bias_opt.has_value() && bias_opt->defined() &&
194+
bias_opt->requires_grad()));
195+
if (!maybe_backward) {
196+
aprop_kind = ideep::prop_kind::forward_inference;
197+
}
190198
auto input = input_t.contiguous();
191199
auto input_size = input.sizes();
192200

@@ -238,13 +246,15 @@ Tensor mkldnn_linear_pointwise(
238246
w,
239247
mkldnn_bias.value(),
240248
mkldnn_output,
241-
op_attr);
249+
op_attr,
250+
aprop_kind);
242251
} else {
243252
ideep::inner_product_forward::compute</*reorder_src=*/false, /*reorder_weight=*/false>(
244253
mkldnn_input,
245254
w,
246255
mkldnn_output,
247-
op_attr);
256+
op_attr,
257+
aprop_kind);
248258
}
249259

250260
if (dim != 2) {
@@ -317,6 +327,7 @@ Tensor mkldnn_linear_pointwise_binary(
317327

318328
auto other_desc = mkldnn_other.get_desc();
319329
auto op_attr = ideep::attr_t::fuse_binary(it_binary->second, other_desc);
330+
auto aprop_kind = ideep::prop_kind::forward_inference;
320331

321332
if (mkldnn_bias.has_value()) {
322333
ideep::inner_product_forward::compute_binary</*reorder_src=*/false, /*reorder_weight=*/false>(
@@ -325,10 +336,11 @@ Tensor mkldnn_linear_pointwise_binary(
325336
w,
326337
mkldnn_bias.value(),
327338
mkldnn_output,
328-
op_attr);
339+
op_attr,
340+
aprop_kind);
329341
} else {
330342
ideep::inner_product_forward::compute_binary</*reorder_src=*/false, /*reorder_weight=*/false>(
331-
mkldnn_input, mkldnn_other, w, mkldnn_output, op_attr);
343+
mkldnn_input, mkldnn_other, w, mkldnn_output, op_attr, aprop_kind);
332344
}
333345

334346
if (dim != 2) {

aten/src/ATen/native/mkldnn/MKLDNNConversions.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Tensor mkldnn_reorder_conv2d_weight(
182182
dilation_expanded,
183183
groups,
184184
ideep::algorithm::convolution_direct,
185-
ideep::prop_kind::forward,
185+
ideep::prop_kind::forward_inference,
186186
w.get_data_type(),
187187
src_dims,
188188
ideep::attr_t(),
@@ -229,7 +229,7 @@ Tensor mkldnn_reorder_conv3d_weight(
229229
dilation_expanded,
230230
groups,
231231
ideep::algorithm::convolution_direct,
232-
ideep::prop_kind::forward,
232+
ideep::prop_kind::forward_inference,
233233
w.get_data_type(),
234234
src_dims,
235235
ideep::attr_t(),
@@ -273,7 +273,8 @@ static Tensor mkldnn_reorder_linear_weight(
273273
{out_features, in_features},
274274
input_size,
275275
/* weight dtype */ dtype,
276-
/* src dtype */ dtype);
276+
/* src dtype */ dtype,
277+
ideep::prop_kind::forward_inference);
277278
ideep::tensor result;
278279
result.init(packed_desc);
279280
result.feed_from(w);

0 commit comments

Comments
 (0)