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

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 "paddle/framework/data_transform.h"
16 17

#include "paddle/framework/data_device_transform.h"
18
#include "paddle/framework/data_layout_transform.h"
Q
Qiao Longfei 已提交
19 20 21 22

namespace paddle {
namespace framework {

23 24 25 26 27
static void PassTensorData(Tensor* from, Tensor* to) {
  to->ShareDataWith(*from);
  *from = Tensor();
}

28 29
void DataTransform(const OpKernelType& expected_kernel_type,
                   const OpKernelType& kernel_type_for_var,
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
                   const Tensor& input_tensor, Tensor* output_tensor) {
  bool transformed = false;
  Tensor in;
  in.ShareDataWith(input_tensor);
  Tensor out;

  // do layout transform
  if (NeedTransformLayout(expected_kernel_type.data_layout_,
                          kernel_type_for_var.data_layout_)) {
    TransDataLayout(kernel_type_for_var, expected_kernel_type, in, &out);
    transformed = true;
    PassTensorData(&out, &in);
  }

  // do device transform
45 46
  if (!platform::is_same_place(kernel_type_for_var.place_,
                               expected_kernel_type.place_)) {
47 48 49
    DeviceTransform(in, expected_kernel_type.place_, &out);
    transformed = true;
    PassTensorData(&out, &in);
50
  }
51 52 53 54

  PADDLE_ENFORCE(transformed, "no transform is done, please check!");
  // get output data
  output_tensor->ShareDataWith(in);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

void CopyVariableWithTensor(const Variable& in_var, const Tensor& tensor,
                            Variable& out_var) {
  if (in_var.IsType<LoDTensor>()) {
    auto& in_lod_tensor = in_var.Get<LoDTensor>();
    auto* tran_lod_tensor = out_var.GetMutable<LoDTensor>();
    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>()) {
    auto& in_selected_rows = in_var.Get<SelectedRows>();
    auto* trans_selected_rows = out_var.GetMutable<SelectedRows>();
    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 {
    PADDLE_THROW("unknown var type");
  }
}

Q
Qiao Longfei 已提交
76 77
}  // namespace framework
}  // namespace paddle