data_transform.cc 4.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
Qiao Longfei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/data_transform.h"
16

Y
Yi Wang 已提交
17 18 19
#include "paddle/fluid/framework/data_device_transform.h"
#include "paddle/fluid/framework/data_layout_transform.h"
#include "paddle/fluid/framework/data_type_transform.h"
Q
Qiao Longfei 已提交
20

21
#ifdef PADDLE_WITH_MKLDNN
22
#include <algorithm>
23 24 25
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif

Q
Qiao Longfei 已提交
26 27 28
namespace paddle {
namespace framework {

Y
yuyang18 已提交
29
static void PassTensorData(Tensor *from, Tensor *to) {
30 31 32 33
  to->ShareDataWith(*from);
  *from = Tensor();
}

Y
yuyang18 已提交
34 35 36
void TransformData(const OpKernelType &expected_kernel_type,
                   const OpKernelType &kernel_type_for_var,
                   const Tensor &input_tensor, Tensor *output_tensor) {
37 38 39 40
  bool transformed = false;
  Tensor in;
  in.ShareDataWith(input_tensor);
  Tensor out;
M
mozga-intel 已提交
41 42
  DataLayout lin = kernel_type_for_var.data_layout_;
  DataLayout lout = expected_kernel_type.data_layout_;
43 44

  // do layout transform
M
mozga-intel 已提交
45
  if (NeedTransformLayout(lout, lin)) {
46
#ifdef PADDLE_WITH_MKLDNN
M
mozga-intel 已提交
47
    if (lin == DataLayout::kMKLDNN || lout == DataLayout::kMKLDNN) {
48 49 50 51
      PADDLE_ENFORCE_EQ(
          !(lin == DataLayout::kMKLDNN && lout == DataLayout::kMKLDNN), true,
          platform::errors::PreconditionNotMet(
              "No layout transform needed between two MKLDNN OPKernels."));
M
mozga-intel 已提交
52 53 54 55

      if (lin != DataLayout::kMKLDNN && lout == DataLayout::kMKLDNN) {
        // Case1 - transform from Non-MKLDNN OPKernel to MKLDNN OPKernel
        // Just set layout/format. No real transform occur
56

57 58 59
        auto out_format = platform::MKLDNNFormatForSize(in.dims().size(),
                                                        ToMKLDNNFormat(lin));
        out.ShareDataWith(input_tensor);
60 61
        // For NHWC data we need reshape of tensors as MKL-DNN
        // is expecting NHWC dims description order
62
        platform::MatchShapeToLayout(&out, lin, lout);
63 64
        paddle::platform::MKLDNNDeviceContext::tls().set_cur_paddle_data_layout(
            lin);
65 66
        out.set_layout(DataLayout::kMKLDNN);
        out.set_format(out_format);
M
mozga-intel 已提交
67 68 69 70 71 72 73 74 75 76
      } else {
        // Case2 - transfrom from MKLDNN OPKernel to Non-MKLDNN OPKernel
        // Do transform via MKLDNN lib
        TransDataLayoutFromMKLDNN(kernel_type_for_var, expected_kernel_type, in,
                                  &out);
      }
    } else {
      // Case3 - transfrom between Non-MKLDNN OPKernels
      TransDataLayout(kernel_type_for_var, expected_kernel_type, in, &out);
    }
77 78 79 80
#else
    // Case3 - transfrom between Non-MKLDNN OPKernels
    TransDataLayout(kernel_type_for_var, expected_kernel_type, in, &out);
#endif
81 82 83 84
    transformed = true;
    PassTensorData(&out, &in);
  }

85
  // do data type transform
Q
Qiao Longfei 已提交
86 87 88 89 90 91
  if (expected_kernel_type.data_type_ != kernel_type_for_var.data_type_) {
    TransDataType(kernel_type_for_var, expected_kernel_type, in, &out);
    transformed = true;
    PassTensorData(&out, &in);
  }

92
  // do device transform
93 94
  if (!platform::is_same_place(kernel_type_for_var.place_,
                               expected_kernel_type.place_)) {
Q
Qiao Longfei 已提交
95
    TransDataDevice(in, expected_kernel_type.place_, &out);
96 97
    transformed = true;
    PassTensorData(&out, &in);
98
  }
99

100 101 102 103
  PADDLE_ENFORCE_EQ(
      transformed, true,
      platform::errors::PreconditionNotMet(
          "No transform is applied for the data needs to be transformed."));
104 105
  // get output data
  output_tensor->ShareDataWith(in);
106 107
}

Y
yuyang18 已提交
108 109
void SetTensorToVariable(const Variable &in_var, const Tensor &tensor,
                         Variable *out_var) {
110
  if (in_var.IsType<LoDTensor>()) {
Y
yuyang18 已提交
111 112
    auto &in_lod_tensor = in_var.Get<LoDTensor>();
    auto *tran_lod_tensor = out_var->GetMutable<LoDTensor>();
113 114 115 116
    tran_lod_tensor->set_lod(in_lod_tensor.lod());
    tran_lod_tensor->set_layout(in_lod_tensor.layout());
    tran_lod_tensor->ShareDataWith(tensor);
  } else if (in_var.IsType<SelectedRows>()) {
Y
yuyang18 已提交
117 118
    auto &in_selected_rows = in_var.Get<SelectedRows>();
    auto *trans_selected_rows = out_var->GetMutable<SelectedRows>();
119 120 121 122
    trans_selected_rows->set_height(in_selected_rows.height());
    trans_selected_rows->set_rows(in_selected_rows.rows());
    trans_selected_rows->mutable_value()->ShareDataWith(tensor);
  } else {
123 124 125 126
    PADDLE_THROW(platform::errors::Unavailable(
        "Unsupported variable type, only supports LoDTensor or SelectedRows, "
        "but the input variable type is %s.",
        ToTypeName(in_var.Type())));
127 128 129
  }
}

Q
Qiao Longfei 已提交
130 131
}  // namespace framework
}  // namespace paddle