custom_tensor_utils.h 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2021 PaddlePaddle 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. */

#pragma once

#include <memory>

19
#include "paddle/fluid/extension/include/ext_tensor.h"
20 21 22
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/platform/gpu_info.h"
#include "paddle/fluid/platform/place.h"
23 24 25
#ifdef PADDLE_WITH_CUDA
#endif
#include "paddle/fluid/platform/device_context.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39

namespace paddle {
namespace framework {

class CustomTensorUtils {
 public:
  /// \brief Share data TO another tensor.
  /// Use this to pass tensor from op to op
  /// \return void.
  static void ShareDataTo(const paddle::Tensor& src, void* dst);

  /// \brief Share data FROM another tensor.
  /// Use this to pass tensor from op to op
  /// \return void.
40
  static void ShareDataFrom(const void* src, const paddle::Tensor& dst);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  static framework::proto::VarType::Type ConvertEnumDTypeToInnerDType(
      const paddle::DataType& dtype) {
    switch (dtype) {
      case paddle::DataType::FLOAT64:
        return framework::proto::VarType::FP64;
      case paddle::DataType::FLOAT32:
        return framework::proto::VarType::FP32;
      case paddle::DataType::UINT8:
        return framework::proto::VarType::UINT8;
      case paddle::DataType::INT8:
        return framework::proto::VarType::INT8;
      case paddle::DataType::INT32:
        return framework::proto::VarType::INT32;
      case paddle::DataType::INT64:
        return framework::proto::VarType::INT64;
      case paddle::DataType::INT16:
        return framework::proto::VarType::INT16;
59 60 61 62
      case paddle::DataType::COMPLEX64:
        return framework::proto::VarType::COMPLEX64;
      case paddle::DataType::COMPLEX128:
        return framework::proto::VarType::COMPLEX128;
63 64
      case paddle::DataType::FLOAT16:
        return framework::proto::VarType::FP16;
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
      case paddle::DataType::BOOL:
        return framework::proto::VarType::BOOL;
      default:
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported data type code(%d) when casting enum data type into "
            "paddle data type.",
            static_cast<int>(dtype)));
    }
  }

  static paddle::DataType ConvertInnerDTypeToEnumDType(
      const framework::proto::VarType::Type& dtype) {
    switch (dtype) {
      case framework::proto::VarType::FP64:
        return paddle::DataType::FLOAT64;
      case framework::proto::VarType::FP32:
        return paddle::DataType::FLOAT32;
      case framework::proto::VarType::INT64:
        return paddle::DataType::INT64;
      case framework::proto::VarType::INT32:
        return paddle::DataType::INT32;
      case framework::proto::VarType::INT8:
        return paddle::DataType::INT8;
      case framework::proto::VarType::UINT8:
        return paddle::DataType::UINT8;
      case framework::proto::VarType::INT16:
        return paddle::DataType::INT16;
92 93 94 95
      case framework::proto::VarType::COMPLEX64:
        return paddle::DataType::COMPLEX64;
      case framework::proto::VarType::COMPLEX128:
        return paddle::DataType::COMPLEX128;
96 97
      case framework::proto::VarType::FP16:
        return paddle::DataType::FLOAT16;
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
      case framework::proto::VarType::BOOL:
        return paddle::DataType::BOOL;
      default:
        PADDLE_THROW(platform::errors::Unimplemented(
            "Unsupported data type `%s` when casting paddle data type into "
            "enum data type.",
            DataTypeToString(dtype)));
    }
  }

  // PaddlePlace <-> platform::Place
  static platform::Place ConvertEnumPlaceToInnerPlace(const PlaceType& pc) {
    if (pc == PlaceType::kCPU) {
      return platform::Place(platform::CPUPlace());
    } else if (pc == PlaceType::kGPU) {
#ifdef PADDLE_WITH_CUDA
      return platform::Place(
          platform::CUDAPlace(platform::GetCurrentDeviceId()));
#endif
    } else {
      PADDLE_THROW(platform::errors::Unimplemented(
          "Unsupported place type code(%d) when "
          "casting enum place to paddle place.",
          static_cast<int>(pc)));
    }
    return platform::Place();
  }

  static PlaceType ConvertInnerPlaceToEnumPlace(const platform::Place& pc) {
    if (platform::is_cpu_place(pc)) {
      return PlaceType::kCPU;
    } else if (platform::is_gpu_place(pc)) {
#ifdef PADDLE_WITH_CUDA
      return PlaceType::kGPU;
#endif
    } else {
      PADDLE_THROW(
          platform::errors::Unimplemented("Unsupported place type `%s` when "
                                          "casting paddle place to enum place.",
                                          pc));
    }
    return PlaceType::kUNK;
  }
141 142 143 144 145 146 147 148 149 150 151 152 153

  static void SetTensorCurrentStream(paddle::Tensor* src,
                                     const platform::Place& pc) {
    if (platform::is_gpu_place(pc)) {
#ifdef PADDLE_WITH_CUDA
      auto* dev_ctx = static_cast<platform::CUDADeviceContext*>(
          platform::DeviceContextPool::Instance().Get(pc));
      src->stream_.SetStream(reinterpret_cast<void*>(dev_ctx->stream()));
#endif
    } else {
      return;
    }
  }
154 155 156 157
};

}  // namespace framework
}  // namespace paddle