Skip to content

Commit

Permalink
[Cherry-pick] Fix incompatible error for place type (#43830)
Browse files Browse the repository at this point in the history
* Create Tensor by paddle::empty  in custom operator (#41840)

* create tensor by empty in custom op

* fix some bug

* update relu custom op demo (#43173)

* Fix incompatible error for custom op Placetype (#43749)

* fix incompatible error

* rmeove default constructor

* add macro

* fix cpu make error

* add DefaultGPUPlace api

Co-authored-by: zyfncg <[email protected]>
  • Loading branch information
chenwhql and zyfncg authored Jun 27, 2022
1 parent 5124033 commit 9e776f6
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'int' : 'int', 'int32_t' : 'int32_t', 'int64_t' : 'int64_t', 'size_t' : 'size_t', \
'float' : 'float', 'double' : 'double', 'bool' : 'bool', \
'str' : 'std::string', \
'Place' : 'paddle::experimental::Place', 'DataLayout' : 'paddle::experimental::DataLayout', 'DataType' : 'paddle::experimental::DataType', \
'Place' : 'paddle::Place', 'DataLayout' : 'paddle::experimental::DataLayout', 'DataType' : 'paddle::experimental::DataType', \
'int64_t[]' : 'std::vector<int64_t>', 'int[]' : 'std::vector<int>',
'Tensor' : 'Tensor',
'Tensor[]' : 'std::vector<Tensor>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def SkipAPIGeneration(forward_api_name):
"std::vector<std::string>": "CastPyArg2Strings",
"paddle::experimental::Scalar": "CastPyArg2Scalar",
"paddle::experimental::IntArray": "CastPyArg2IntArray",
"paddle::experimental::Place": "CastPyArg2Place",
"paddle::Place": "CastPyArg2Place",
"paddle::experimental::DataType": "CastPyArg2DataType",
}

Expand Down
10 changes: 4 additions & 6 deletions paddle/fluid/pybind/eager_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1194,15 +1194,13 @@ std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs(
return result;
}

paddle::experimental::Place CastPyArg2Place(PyObject* obj,
const std::string& op_type,
ssize_t arg_pos) {
paddle::Place CastPyArg2Place(PyObject* obj, const std::string& op_type,
ssize_t arg_pos) {
return CastPyArg2Place(obj, arg_pos);
}

paddle::experimental::DataType CastPyArg2DataType(PyObject* obj,
const std::string& op_type,
ssize_t arg_pos) {
paddle::DataType CastPyArg2DataType(PyObject* obj, const std::string& op_type,
ssize_t arg_pos) {
if (obj == Py_None) {
return paddle::experimental::DataType::UNDEFINED;
}
Expand Down
10 changes: 4 additions & 6 deletions paddle/fluid/pybind/eager_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,11 @@ paddle::experimental::IntArray CastPyArg2IntArray(PyObject* obj,
const std::string& op_type,
ssize_t arg_pos);

paddle::experimental::Place CastPyArg2Place(PyObject* obj,
const std::string& op_type,
ssize_t arg_pos);
paddle::Place CastPyArg2Place(PyObject* obj, const std::string& op_type,
ssize_t arg_pos);

paddle::experimental::DataType CastPyArg2DataType(PyObject* obj,
const std::string& op_type,
ssize_t arg_pos);
paddle::DataType CastPyArg2DataType(PyObject* obj, const std::string& op_type,
ssize_t arg_pos);

paddle::optional<const paddle::experimental::Tensor&> GetOptionalTensorFromArgs(
const std::string& op_type, const std::string& arg_name, PyObject* args,
Expand Down
22 changes: 2 additions & 20 deletions paddle/phi/api/lib/tensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ limitations under the License. */

namespace paddle {
namespace experimental {
namespace detail {
static Place GetCorrectPlaceByPlaceType(const Place &place_type) {
auto alloc_type = place_type.GetType();
switch (alloc_type) {
case AllocationType::CPU:
return place_type;
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case AllocationType::GPU:
return phi::Place(AllocationType::GPU,
phi::backends::gpu::GetCurrentDeviceId());
#endif
default:
PADDLE_THROW(phi::errors::Unavailable(
"The PlaceType is a legacy design, only supports CPU and GPU, "
"and will not support other place types in the future."));
}
}
} // namespace detail

/////// Tensor Methods ////////

Expand All @@ -76,7 +58,7 @@ Tensor::Tensor(const Place &place) {
"Reason: A legal tensor cannot be constructed only based on "
"the `place`, and datatype, shape, layout, etc. is also "
"required.";
DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place));
DefaultAllocator alloc(place);
impl_ = std::move(std::make_shared<phi::DenseTensor>(
&alloc,
std::move(phi::DenseTensorMeta(
Expand All @@ -92,7 +74,7 @@ Tensor::Tensor(const Place &place, const std::vector<int64_t> &shape) {
"Reason: A legal tensor cannot be constructed only based on "
"the `place` and `shape`, and datatype, layout, etc. is also "
"required.";
DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place));
DefaultAllocator alloc(place);
impl_ = std::move(std::make_shared<phi::DenseTensor>(
&alloc,
std::move(phi::DenseTensorMeta(phi::DataType::FLOAT32,
Expand Down
20 changes: 18 additions & 2 deletions paddle/phi/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
cc_library(phi_place SRCS place.cc)
cc_library(scalar SRCS scalar.cc DEPS phi_enforce tensor)
if(WITH_GPU)
nv_library(
phi_place
SRCS place.cc
DEPS phi_gpu_info)
elseif(WITH_ROCM)
hip_library(
phi_place
SRCS place.cc
DEPS phi_gpu_info)
else()
cc_library(phi_place SRCS place.cc)
endif()

cc_library(
scalar
SRCS scalar.cc
DEPS phi_enforce tensor)
32 changes: 30 additions & 2 deletions paddle/phi/common/place.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License. */
#include "glog/logging.h"

#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/backends/gpu/gpu_info.h"

namespace phi {

Expand Down Expand Up @@ -110,14 +111,32 @@ uint32_t Place::Hash::operator()(const Place &place) const {
return hash_value;
}

namespace detail {
static int8_t GetCorrectDeviceIdByPlaceType(
const paddle::PlaceType &place_type) {
switch (place_type) {
case paddle::PlaceType::kCPU:
return 0;
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
case paddle::PlaceType::kGPU:
return phi::backends::gpu::GetCurrentDeviceId();
#endif
default:
PD_THROW(
"The PlaceType is a legacy design, only supports CPU and GPU, "
"and will not support other place types in the future.");
}
}
} // namespace detail

Place::Place(paddle::PlaceType type)
: device(0),
: device(detail::GetCorrectDeviceIdByPlaceType(type)),
alloc_type_(static_cast<AllocationType>(type)),
device_type_id_(GetOrRegisterGlobalDeviceTypeId("")) {
LOG_FIRST_N(WARNING, 1)
<< "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version "
"2.3, and will be removed in version 2.4! Please use "
"`paddle::CPUPlace()/GPUPlace()` to represent the place type.";
"`paddle::CPUPlace()/DefaultGPUPlace()` to represent the place type.";
}

} // namespace phi
Expand All @@ -140,4 +159,13 @@ bool operator==(PlaceType place_type, const Place &place) {
return static_cast<AllocationType>(place_type) == place.GetType();
}

GPUPlace DefaultGPUPlace() {
return GPUPlace(
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
phi::backends::gpu::GetCurrentDeviceId());
#else
0);
#endif
}

} // namespace paddle
5 changes: 2 additions & 3 deletions paddle/phi/common/place.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ std::ostream& operator<<(std::ostream&, const Place&);
namespace paddle {
namespace experimental {
using AllocationType = phi::AllocationType;
using Place = phi::Place;
using CPUPlace = phi::CPUPlace;
using GPUPlace = phi::GPUPlace;
using GPUPinnedPlace = phi::GPUPinnedPlace;
using XPUPlace = phi::XPUPlace;
using NPUPlace = phi::NPUPlace;
Expand Down Expand Up @@ -259,4 +256,6 @@ enum class PlaceType {
PADDLE_API bool operator==(const Place& place, PlaceType place_type);
PADDLE_API bool operator==(PlaceType place_type, const Place& place);

PADDLE_API GPUPlace DefaultGPUPlace();

} // namespace paddle
19 changes: 8 additions & 11 deletions paddle/phi/tests/api/test_data_transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ namespace tests {
// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, data_transform_same_place) {
// 1. create tensor
auto x = paddle::experimental::full({3, 3},
1.0,
experimental::DataType::COMPLEX128,
experimental::CPUPlace());
auto x =
paddle::experimental::full({3, 3}, 1.0, DataType::COMPLEX128, CPUPlace());

auto y = paddle::experimental::full(
{3, 3}, 2.0, experimental::DataType::FLOAT32, experimental::CPUPlace());
auto y =
paddle::experimental::full({3, 3}, 2.0, DataType::FLOAT32, CPUPlace());

std::vector<phi::dtype::complex<double>> sum(9, 6.0);

Expand Down Expand Up @@ -75,10 +73,10 @@ TEST(API, data_transform_same_place) {
TEST(Tensor, data_transform_diff_place) {
// 1. create tensor
auto x = paddle::experimental::full(
{3, 3}, 1.0, experimental::DataType::FLOAT64, experimental::CPUPlace());
{3, 3}, 1.0, experimental::DataType::FLOAT64, CPUPlace());

auto y = paddle::experimental::full(
{3, 3}, 2.0, experimental::DataType::FLOAT64, experimental::GPUPlace());
{3, 3}, 2.0, experimental::DataType::FLOAT64, GPUPlace());

std::vector<float> sum(9, 6.0);

Expand All @@ -93,10 +91,9 @@ TEST(Tensor, data_transform_diff_place) {
ASSERT_EQ(out.dtype(), phi::DataType::FLOAT64);
ASSERT_EQ(out.layout(), phi::DataLayout::NCHW);
ASSERT_EQ(out.initialized(), true);
ASSERT_EQ(out.impl()->place(),
phi::TransToPhiPlace(experimental::Backend::GPU));
ASSERT_EQ(out.impl()->place(), phi::TransToPhiPlace(phi::Backend::GPU));

auto ref_out = experimental::copy_to(out, experimental::CPUPlace(), true);
auto ref_out = experimental::copy_to(out, CPUPlace(), true);

auto dense_out = std::dynamic_pointer_cast<phi::DenseTensor>(ref_out.impl());
for (size_t i = 0; i < 9; i++) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/tests/api/test_scale_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace tests {

TEST(API, scale) {
auto x = experimental::full(
{3, 4}, 1.0, experimental::DataType::FLOAT32, experimental::CPUPlace());
{3, 4}, 1.0, experimental::DataType::FLOAT32, CPUPlace());

const size_t cycles = 300;
phi::tests::Timer timer;
Expand Down
6 changes: 2 additions & 4 deletions python/paddle/fluid/tests/custom_op/context_pool_test_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

std::vector<paddle::Tensor> ContextPoolTest(const paddle::Tensor& x) {
// 1. test cpu context
paddle::experimental::Place cpu_place(
paddle::experimental::AllocationType::CPU);
paddle::Place cpu_place(paddle::experimental::AllocationType::CPU);
auto* cpu_ctx =
paddle::experimental::DeviceContextPool::Instance()
.Get<paddle::experimental::AllocationType::CPU>(cpu_place);
Expand All @@ -34,8 +33,7 @@ std::vector<paddle::Tensor> ContextPoolTest(const paddle::Tensor& x) {

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
// 2. test gpu context
paddle::experimental::Place gpu_place(
paddle::experimental::AllocationType::GPU);
paddle::Place gpu_place(paddle::experimental::AllocationType::GPU);
auto* gpu_ctx =
paddle::experimental::DeviceContextPool::Instance()
.Get<paddle::experimental::AllocationType::GPU>(gpu_place);
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/custom_op/custom_concat_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::vector<paddle::Tensor> ConcatForwardDynamicAxis(
auto out_shape = ComputeOutShape(in_shapes, axis);

// create output
auto out = paddle::Tensor(paddle::PlaceType::kCPU, out_shape);
auto out = paddle::empty(out_shape, inputs[0].type(), paddle::CPUPlace());

// calc
PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
Expand Down Expand Up @@ -106,7 +106,7 @@ std::vector<paddle::Tensor> ConcatBackwardDynamicAxis(
// create outputs
std::vector<paddle::Tensor> grad_inputs;
for (auto& t : inputs) {
auto grad = paddle::Tensor(paddle::PlaceType::kCPU, t.shape());
auto grad = paddle::empty(t.shape(), t.dtype(), t.place());
grad_inputs.emplace_back(grad);
}

Expand Down Expand Up @@ -161,7 +161,7 @@ std::vector<paddle::Tensor> ConcatForwardStaticAxis(
auto out_shape = ComputeOutShape(in_shapes, final_axis);

// create output
auto out = paddle::Tensor(paddle::PlaceType::kCPU, out_shape);
auto out = paddle::empty(out_shape, inputs[0].type(), paddle::CPUPlace());

// calc
PD_DISPATCH_FLOATING_AND_INTEGRAL_TYPES(
Expand Down Expand Up @@ -190,7 +190,7 @@ std::vector<paddle::Tensor> ConcatBackwardStaticAxis(
// create outputs
std::vector<paddle::Tensor> grad_inputs;
for (auto& t : inputs) {
auto grad = paddle::Tensor(paddle::PlaceType::kCPU, t.shape());
auto grad = paddle::empty(t.shape(), t.dtype(), t.place());
grad_inputs.emplace_back(grad);
}

Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/custom_op/custom_conj_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void ConjCPUKernel(const data_t* x_data, int64_t numel, data_t* out_data) {
std::vector<paddle::Tensor> ConjFunction(const paddle::Tensor& x) {
CHECK_INPUT(x);

paddle::Tensor out(x.place(), x.shape());
paddle::Tensor out = paddle::empty(x.shape(), x.dtype(), x.place());

PD_DISPATCH_FLOATING_AND_COMPLEX_TYPES(
x.type(), "ConjCPUKernel", ([&] {
Expand Down
Loading

0 comments on commit 9e776f6

Please sign in to comment.