chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
cc_test(
|
||||
phi_test_backend
|
||||
SRCS test_backend.cc
|
||||
DEPS gtest)
|
||||
cc_test(
|
||||
phi_test_data_layout
|
||||
SRCS test_data_layout.cc
|
||||
DEPS gtest)
|
||||
cc_test(
|
||||
phi_test_data_type
|
||||
SRCS test_data_type.cc
|
||||
DEPS gtest)
|
||||
cc_test(
|
||||
phi_test_place
|
||||
SRCS test_place.cc
|
||||
DEPS phi common)
|
||||
cc_test(
|
||||
phi_test_int_array
|
||||
SRCS test_int_array.cc
|
||||
DEPS phi common)
|
||||
cc_test(
|
||||
phi_test_scalar_cpu
|
||||
SRCS test_scalar.cc
|
||||
DEPS phi common)
|
||||
if(WITH_GPU)
|
||||
nv_test(
|
||||
phi_test_scalar
|
||||
SRCS test_scalar.cu
|
||||
DEPS phi common)
|
||||
nv_test(
|
||||
transform_test
|
||||
SRCS transform_test.cu
|
||||
DEPS phi common)
|
||||
endif()
|
||||
if(WITH_ROCM)
|
||||
hip_test(
|
||||
phi_test_scalar
|
||||
SRCS test_scalar.cu
|
||||
DEPS phi common)
|
||||
hip_test(
|
||||
transform_test
|
||||
SRCS transform_test.cu
|
||||
DEPS phi common)
|
||||
endif()
|
||||
@@ -0,0 +1,77 @@
|
||||
/* 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. */
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "paddle/common/exception.h"
|
||||
#include "paddle/phi/common/backend.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
TEST(Backend, OStream) {
|
||||
std::ostringstream oss;
|
||||
oss << Backend::UNDEFINED;
|
||||
EXPECT_EQ(oss.str(), "Undefined");
|
||||
oss.str("");
|
||||
oss << Backend::CPU;
|
||||
EXPECT_EQ(oss.str(), "CPU");
|
||||
oss.str("");
|
||||
oss << Backend::GPU;
|
||||
EXPECT_EQ(oss.str(), "GPU");
|
||||
oss.str("");
|
||||
oss << Backend::XPU;
|
||||
EXPECT_EQ(oss.str(), "XPU");
|
||||
oss.str("");
|
||||
oss << Backend::ONEDNN;
|
||||
EXPECT_EQ(oss.str(), "ONEDNN");
|
||||
oss.str("");
|
||||
oss << Backend::GPUDNN;
|
||||
EXPECT_EQ(oss.str(), "GPUDNN");
|
||||
oss.str("");
|
||||
oss << Backend::KPS;
|
||||
EXPECT_EQ(oss.str(), "KPS");
|
||||
oss.str("");
|
||||
try {
|
||||
oss << Backend::NUM_BACKENDS;
|
||||
} catch (const std::exception& exception) {
|
||||
std::string ex_msg = exception.what();
|
||||
EXPECT_TRUE(ex_msg.find("Invalid enum backend type") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Backend, StringToBackend) {
|
||||
using paddle::experimental::StringToBackend;
|
||||
EXPECT_EQ(Backend::UNDEFINED, StringToBackend("Undefined"));
|
||||
EXPECT_EQ(Backend::CPU, StringToBackend("CPU"));
|
||||
EXPECT_EQ(Backend::GPU, StringToBackend("GPU"));
|
||||
EXPECT_EQ(Backend::XPU, StringToBackend("XPU"));
|
||||
EXPECT_EQ(Backend::ONEDNN, StringToBackend("OneDNN"));
|
||||
EXPECT_EQ(Backend::GPUDNN, StringToBackend("GPUDNN"));
|
||||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
|
||||
EXPECT_EQ(Backend::GPU, StringToBackend("KPS"));
|
||||
#else
|
||||
EXPECT_EQ(Backend::KPS, StringToBackend("KPS"));
|
||||
#endif
|
||||
EXPECT_EQ(static_cast<Backend>(
|
||||
static_cast<size_t>(Backend::NUM_BACKENDS) +
|
||||
phi::CustomRegisteredDeviceMap::Instance()
|
||||
.GetOrRegisterGlobalDeviceTypeId("CustomBackend")),
|
||||
StringToBackend("CustomBackend"));
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,52 @@
|
||||
/* 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. */
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "paddle/common/exception.h"
|
||||
#include "paddle/common/layout.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
TEST(DataLayout, OStream) {
|
||||
std::ostringstream oss;
|
||||
oss << DataLayout::UNDEFINED;
|
||||
EXPECT_EQ(oss.str(), "Undefined(AnyLayout)");
|
||||
oss.str("");
|
||||
oss << DataLayout::ANY;
|
||||
EXPECT_EQ(oss.str(), "Undefined(AnyLayout)");
|
||||
oss.str("");
|
||||
oss << DataLayout::NHWC;
|
||||
EXPECT_EQ(oss.str(), "NHWC");
|
||||
oss.str("");
|
||||
oss << DataLayout::NCHW;
|
||||
EXPECT_EQ(oss.str(), "NCHW");
|
||||
oss.str("");
|
||||
oss << DataLayout::ONEDNN;
|
||||
EXPECT_EQ(oss.str(), "ONEDNN");
|
||||
oss.str("");
|
||||
try {
|
||||
oss << DataLayout::NUM_DATA_LAYOUTS;
|
||||
} catch (const std::exception& exception) {
|
||||
std::string ex_msg = exception.what();
|
||||
EXPECT_TRUE(ex_msg.find("Unknown Data Layout type") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,90 @@
|
||||
/* 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. */
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "paddle/common/exception.h"
|
||||
#include "paddle/phi/common/data_type.h"
|
||||
#include "paddle/phi/common/type_traits.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
TEST(DataType, OStream) {
|
||||
std::ostringstream oss;
|
||||
oss << DataType::UNDEFINED;
|
||||
EXPECT_EQ(oss.str(), "Undefined");
|
||||
oss.str("");
|
||||
oss << DataType::BOOL;
|
||||
EXPECT_EQ(oss.str(), "bool");
|
||||
oss.str("");
|
||||
oss << DataType::INT8;
|
||||
EXPECT_EQ(oss.str(), "int8");
|
||||
oss.str("");
|
||||
oss << DataType::UINT8;
|
||||
EXPECT_EQ(oss.str(), "uint8");
|
||||
oss.str("");
|
||||
oss << DataType::INT16;
|
||||
EXPECT_EQ(oss.str(), "int16");
|
||||
oss.str("");
|
||||
oss << DataType::INT32;
|
||||
EXPECT_EQ(oss.str(), "int32");
|
||||
oss.str("");
|
||||
oss << DataType::INT64;
|
||||
EXPECT_EQ(oss.str(), "int64");
|
||||
oss.str("");
|
||||
oss << DataType::BFLOAT16;
|
||||
EXPECT_EQ(oss.str(), "bfloat16");
|
||||
oss.str("");
|
||||
oss << DataType::FLOAT16;
|
||||
EXPECT_EQ(oss.str(), "float16");
|
||||
oss.str("");
|
||||
oss << DataType::FLOAT32;
|
||||
EXPECT_EQ(oss.str(), "float32");
|
||||
oss.str("");
|
||||
oss << DataType::FLOAT64;
|
||||
EXPECT_EQ(oss.str(), "float64");
|
||||
oss.str("");
|
||||
oss << DataType::COMPLEX64;
|
||||
EXPECT_EQ(oss.str(), "complex64");
|
||||
oss.str("");
|
||||
oss << DataType::COMPLEX128;
|
||||
EXPECT_EQ(oss.str(), "complex128");
|
||||
oss.str("");
|
||||
oss << DataType::PSTRING;
|
||||
EXPECT_EQ(oss.str(), "pstring");
|
||||
oss.str("");
|
||||
try {
|
||||
oss << DataType::NUM_DATA_TYPES;
|
||||
} catch (const std::exception& exception) {
|
||||
std::string ex_msg = exception.what();
|
||||
EXPECT_TRUE(ex_msg.find("Invalid enum data type") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TypeTraits, Complex) {
|
||||
EXPECT_EQ(dtype::ToReal(DataType::COMPLEX64), DataType::FLOAT32);
|
||||
EXPECT_EQ(dtype::ToReal(DataType::COMPLEX128), DataType::FLOAT64);
|
||||
EXPECT_EQ(dtype::ToReal(DataType::FLOAT32), DataType::FLOAT32);
|
||||
|
||||
EXPECT_EQ(dtype::ToComplex(DataType::FLOAT32), DataType::COMPLEX64);
|
||||
EXPECT_EQ(dtype::ToComplex(DataType::FLOAT64), DataType::COMPLEX128);
|
||||
EXPECT_EQ(dtype::ToComplex(DataType::COMPLEX64), DataType::COMPLEX64);
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,154 @@
|
||||
/* Copyright (c) 2022 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. */
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/phi/api/include/api.h"
|
||||
#include "paddle/phi/api/include/context_pool.h"
|
||||
#include "paddle/phi/backends/cpu/cpu_context.h"
|
||||
#include "paddle/phi/backends/gpu/gpu_context.h"
|
||||
#include "paddle/phi/common/int_array.h"
|
||||
#include "paddle/phi/core/kernel_registry.h"
|
||||
#include "paddle/phi/kernels/full_kernel.h"
|
||||
|
||||
PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);
|
||||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
|
||||
PD_DECLARE_KERNEL(full, GPU, ALL_LAYOUT);
|
||||
#endif
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
TEST(IntArray, ConstructFromCPUDenseTensor) {
|
||||
auto& pool = paddle::experimental::DeviceContextPool::Instance();
|
||||
const auto* dev_ctx = static_cast<const CPUContext*>(pool.Get(CPUPlace()));
|
||||
DenseTensor shape = Full<int>(*dev_ctx, {2}, 3);
|
||||
DenseTensor out = Full<int>(*dev_ctx, shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromCPUDenseTensorVector) {
|
||||
auto& pool = paddle::experimental::DeviceContextPool::Instance();
|
||||
const auto* dev_ctx = static_cast<const CPUContext*>(pool.Get(CPUPlace()));
|
||||
DenseTensor shape0 = Full<int>(*dev_ctx, {1}, 3);
|
||||
DenseTensor shape1 = Full<int64_t>(*dev_ctx, {1}, 3);
|
||||
std::vector<DenseTensor> shape{shape0, shape1};
|
||||
DenseTensor out = Full<int>(*dev_ctx, shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromCPUTensor) {
|
||||
auto shape = paddle::experimental::full({2}, 3, DataType::INT64);
|
||||
auto out = paddle::experimental::full(shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromCPUTensorVector) {
|
||||
auto shape0 = paddle::experimental::full({2}, 3, DataType::INT64);
|
||||
auto shape1 = paddle::experimental::full({2}, 3, DataType::INT32);
|
||||
|
||||
std::vector<paddle::Tensor> shape{shape0, shape0};
|
||||
auto out = paddle::experimental::full(shape, 1);
|
||||
|
||||
std::vector<paddle::Tensor> shape_new{shape0, shape1};
|
||||
auto out1 = paddle::experimental::full(shape_new, 1);
|
||||
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
|
||||
ASSERT_EQ(out1.dims().size(), 2);
|
||||
ASSERT_EQ(out1.dims()[0], 3);
|
||||
ASSERT_EQ(out1.dims()[1], 3);
|
||||
ASSERT_EQ(out1.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ThrowException) {
|
||||
auto shape = paddle::experimental::full({2}, 3, DataType::FLOAT32);
|
||||
auto create_int_array = [&shape]() -> paddle::experimental::IntArray {
|
||||
paddle::experimental::IntArray int_array{shape};
|
||||
return int_array;
|
||||
};
|
||||
ASSERT_ANY_THROW(create_int_array());
|
||||
}
|
||||
|
||||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
|
||||
TEST(IntArray, ConstructFromGPUDenseTensor) {
|
||||
auto& pool = paddle::experimental::DeviceContextPool::Instance();
|
||||
const auto* dev_ctx =
|
||||
static_cast<const phi::GPUContext*>(pool.Get(GPUPlace()));
|
||||
DenseTensor shape = Full<int>(*dev_ctx, {2}, 3);
|
||||
DenseTensor out = Full<int>(*dev_ctx, shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromGPUDenseTensorVector) {
|
||||
auto& pool = paddle::experimental::DeviceContextPool::Instance();
|
||||
const auto* dev_ctx =
|
||||
static_cast<const phi::GPUContext*>(pool.Get(GPUPlace()));
|
||||
DenseTensor shape0 = Full<int>(*dev_ctx, {1}, 3);
|
||||
DenseTensor shape1 = Full<int64_t>(*dev_ctx, {1}, 3);
|
||||
std::vector<DenseTensor> shape{shape0, shape1};
|
||||
DenseTensor out = Full<int>(*dev_ctx, shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromGPUTensor) {
|
||||
auto shape = paddle::experimental::full({2}, 3, DataType::INT64, GPUPlace());
|
||||
auto out = paddle::experimental::full(shape, 1);
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
}
|
||||
|
||||
TEST(IntArray, ConstructFromGPUTensorVector) {
|
||||
auto shape0 = paddle::experimental::full({2}, 3, DataType::INT64, GPUPlace());
|
||||
auto shape1 = paddle::experimental::full({2}, 3, DataType::INT32, GPUPlace());
|
||||
|
||||
std::vector<paddle::Tensor> shape{shape0, shape0};
|
||||
auto out = paddle::experimental::full(shape, 1);
|
||||
|
||||
std::vector<paddle::Tensor> shape_new{shape0, shape1};
|
||||
auto out1 = paddle::experimental::full(shape_new, 1);
|
||||
|
||||
ASSERT_EQ(out.dims().size(), 2);
|
||||
ASSERT_EQ(out.dims()[0], 3);
|
||||
ASSERT_EQ(out.dims()[1], 3);
|
||||
ASSERT_EQ(out.numel(), 9);
|
||||
|
||||
ASSERT_EQ(out1.dims().size(), 2);
|
||||
ASSERT_EQ(out1.dims()[0], 3);
|
||||
ASSERT_EQ(out1.dims()[1], 3);
|
||||
ASSERT_EQ(out1.numel(), 9);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,82 @@
|
||||
/* Copyright (c) 2022 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. */
|
||||
|
||||
#include <map> // NOLINT
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/phi/common/place.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
TEST(PhiPlace, place) {
|
||||
Place place;
|
||||
EXPECT_EQ(place.GetType(), AllocationType::UNDEFINED);
|
||||
|
||||
place.Reset(AllocationType::GPU, 1);
|
||||
EXPECT_EQ(place.GetType(), AllocationType::GPU);
|
||||
EXPECT_EQ(place.GetDeviceId(), 1);
|
||||
}
|
||||
|
||||
TEST(Place, cpu_place) {
|
||||
CPUPlace place;
|
||||
EXPECT_EQ(place.GetType(), AllocationType::CPU);
|
||||
std::cout << "cpu place repr: " << place << std::endl;
|
||||
}
|
||||
|
||||
TEST(Place, gpu_place) {
|
||||
GPUPlace place;
|
||||
EXPECT_EQ(place.GetType(), AllocationType::GPU);
|
||||
EXPECT_EQ(place.GetDeviceId(), 0);
|
||||
|
||||
GPUPlace place1(2);
|
||||
EXPECT_EQ(place1.GetType(), AllocationType::GPU);
|
||||
EXPECT_EQ(place1.GetDeviceId(), 2);
|
||||
std::cout << "gpu place repr: " << place1 << std::endl;
|
||||
|
||||
GPUPinnedPlace place2;
|
||||
EXPECT_EQ(place2.GetType(), AllocationType::GPUPINNED);
|
||||
std::cout << "gpu pinned place repr: " << place2 << std::endl;
|
||||
|
||||
EXPECT_NE(place2, CPUPlace());
|
||||
}
|
||||
|
||||
TEST(Place, convert_place) {
|
||||
Place base_place(AllocationType::CPU);
|
||||
CPUPlace cpu_place = base_place;
|
||||
EXPECT_EQ(cpu_place.GetType(), base_place.GetType());
|
||||
base_place.Reset(AllocationType::GPU, 2);
|
||||
GPUPlace gpu_place = base_place;
|
||||
EXPECT_EQ(gpu_place.GetType(), base_place.GetType());
|
||||
EXPECT_EQ(gpu_place.GetDeviceId(), base_place.GetDeviceId());
|
||||
Place place = gpu_place;
|
||||
EXPECT_EQ(gpu_place.GetType(), place.GetType());
|
||||
EXPECT_EQ(gpu_place.GetDeviceId(), place.GetDeviceId());
|
||||
place = cpu_place;
|
||||
EXPECT_EQ(cpu_place.GetType(), place.GetType());
|
||||
|
||||
std::map<Place, int> maps;
|
||||
maps[CPUPlace()] = 1;
|
||||
maps[GPUPlace(0)] = 2;
|
||||
maps[GPUPlace(1)] = 3;
|
||||
maps[GPUPlace(2)] = 4;
|
||||
maps[GPUPlace(3)] = 5;
|
||||
maps[GPUPinnedPlace()] = 6;
|
||||
for (auto& map_item : maps) {
|
||||
std::cout << map_item.first << ":" << map_item.second << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright (c) 2023 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.
|
||||
|
||||
#include <complex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/phi/common/scalar.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
bool StartsWith(const std::string& s, const std::string& prefix) {
|
||||
return s.rfind(prefix, 0) == 0;
|
||||
}
|
||||
|
||||
TEST(Scalar, Formatting) {
|
||||
paddle::experimental::Scalar s;
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<float>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(float32(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<double>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(float64(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<int>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(int32(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<int64_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(int64(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<bool>(true));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(bool(");
|
||||
|
||||
s = paddle::experimental::Scalar(std::complex<float>(42.1, 42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(complex64(");
|
||||
|
||||
s = paddle::experimental::Scalar(std::complex<double>(42.1, 42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(complex128(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<phi::float16>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(float16(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<phi::bfloat16>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(bfloat16(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<int8_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(int8(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<int16_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(int16(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<uint8_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(uint8(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<uint16_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(uint16(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<uint32_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(uint32(");
|
||||
|
||||
s = paddle::experimental::Scalar(static_cast<uint64_t>(42.1));
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(uint64(");
|
||||
|
||||
std::stringstream ss;
|
||||
s = paddle::experimental::Scalar(static_cast<uint64_t>(42.1));
|
||||
ss << s;
|
||||
ASSERT_PRED2(StartsWith, s.ToString(), "Scalar(uint64(");
|
||||
}
|
||||
|
||||
TEST(Scalar, Equality) {
|
||||
auto s_bool = paddle::experimental::Scalar(static_cast<bool>(true));
|
||||
|
||||
auto s_int8 = paddle::experimental::Scalar(static_cast<int8_t>(42.1));
|
||||
auto s_int16 = paddle::experimental::Scalar(static_cast<int16_t>(42.1));
|
||||
auto s_int32 = paddle::experimental::Scalar(static_cast<int32_t>(42.1));
|
||||
auto s_int64 = paddle::experimental::Scalar(static_cast<int64_t>(42.1));
|
||||
|
||||
auto s_uint8 = paddle::experimental::Scalar(static_cast<uint8_t>(42.1));
|
||||
auto s_uint16 = paddle::experimental::Scalar(static_cast<uint16_t>(42.1));
|
||||
auto s_uint32 = paddle::experimental::Scalar(static_cast<uint32_t>(42.1));
|
||||
auto s_uint64 = paddle::experimental::Scalar(static_cast<uint64_t>(42.1));
|
||||
|
||||
auto s_float16 =
|
||||
paddle::experimental::Scalar(static_cast<phi::float16>(42.1));
|
||||
auto s_bfloat16 =
|
||||
paddle::experimental::Scalar(static_cast<phi::bfloat16>(42.1));
|
||||
auto s_float = paddle::experimental::Scalar(static_cast<float>(42.1));
|
||||
auto s_double = paddle::experimental::Scalar(static_cast<double>(42.1));
|
||||
|
||||
auto s_cfloat = paddle::experimental::Scalar(std::complex<float>(42.1, 42.1));
|
||||
auto s_cdouble =
|
||||
paddle::experimental::Scalar(std::complex<double>(42.1, 42.1));
|
||||
|
||||
ASSERT_EQ(s_bool, s_bool);
|
||||
|
||||
ASSERT_EQ(s_int8, s_int8);
|
||||
ASSERT_EQ(s_int16, s_int16);
|
||||
ASSERT_EQ(s_int32, s_int32);
|
||||
ASSERT_EQ(s_int64, s_int64);
|
||||
|
||||
ASSERT_EQ(s_uint8, s_uint8);
|
||||
ASSERT_EQ(s_uint16, s_uint16);
|
||||
ASSERT_EQ(s_uint32, s_uint32);
|
||||
ASSERT_EQ(s_uint64, s_uint64);
|
||||
|
||||
ASSERT_EQ(s_float16, s_float16);
|
||||
ASSERT_EQ(s_bfloat16, s_bfloat16);
|
||||
ASSERT_EQ(s_float, s_float);
|
||||
ASSERT_EQ(s_double, s_double);
|
||||
|
||||
ASSERT_EQ(s_cfloat, s_cfloat);
|
||||
ASSERT_EQ(s_cdouble, s_cdouble);
|
||||
|
||||
ASSERT_NE(s_float, s_double);
|
||||
}
|
||||
|
||||
TEST(Scalar, WrapAsScalars) {
|
||||
std::vector<int32_t> v{1, 2, 3};
|
||||
auto out = paddle::experimental::WrapAsScalars(v);
|
||||
ASSERT_EQ(out[0].dtype(), DataType::INT32);
|
||||
ASSERT_EQ(out[0].to<int32_t>(), 1);
|
||||
ASSERT_EQ(out[1].to<int32_t>(), 2);
|
||||
ASSERT_EQ(out[2].to<int32_t>(), 3);
|
||||
}
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,178 @@
|
||||
/* Copyright (c) 2022 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. */
|
||||
|
||||
#include <map> // NOLINT
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "paddle/phi/api/include/tensor.h"
|
||||
#include "paddle/phi/api/lib/utils/allocator.h"
|
||||
#include "paddle/phi/backends/context_pool.h"
|
||||
#include "paddle/phi/backends/gpu/gpu_context.h"
|
||||
#include "paddle/phi/common/complex.h"
|
||||
#include "paddle/phi/common/float16.h"
|
||||
#include "paddle/phi/common/place.h"
|
||||
#include "paddle/phi/common/scalar.h"
|
||||
#include "paddle/phi/core/dense_tensor.h"
|
||||
#include "paddle/phi/core/kernel_registry.h"
|
||||
|
||||
namespace phi {
|
||||
namespace tests {
|
||||
|
||||
__global__ void FillTensor(float* data) { data[0] = 1; }
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor1) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(alloc.get(),
|
||||
phi::DenseTensorMeta(phi::DataType::FLOAT16,
|
||||
common::make_ddim({1}),
|
||||
phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<float16>(&dense_x);
|
||||
dense_x_data[0] = 1;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
ASSERT_NEAR(1, scalar_test.to<float16>(), 1e-6);
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor2) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(
|
||||
alloc.get(),
|
||||
phi::DenseTensorMeta(
|
||||
phi::DataType::INT16, common::make_ddim({1}), phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<int16_t>(&dense_x);
|
||||
dense_x_data[0] = 1;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
ASSERT_EQ(1, scalar_test.to<int16_t>());
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor3) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(
|
||||
alloc.get(),
|
||||
phi::DenseTensorMeta(
|
||||
phi::DataType::INT8, common::make_ddim({1}), phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<int8_t>(&dense_x);
|
||||
dense_x_data[0] = 1;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
ASSERT_EQ(1, scalar_test.to<int8_t>());
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor4) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(
|
||||
alloc.get(),
|
||||
phi::DenseTensorMeta(
|
||||
phi::DataType::BOOL, common::make_ddim({1}), phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<bool>(&dense_x);
|
||||
dense_x_data[0] = true;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
ASSERT_EQ(true, scalar_test.to<bool>());
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor5) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(alloc.get(),
|
||||
phi::DenseTensorMeta(phi::DataType::COMPLEX64,
|
||||
common::make_ddim({1}),
|
||||
phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<complex64>(&dense_x);
|
||||
dense_x_data[0] = 1;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
complex64 expected_value(1, 0);
|
||||
EXPECT_TRUE(expected_value == scalar_test.to<complex64>());
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor6) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace());
|
||||
phi::DenseTensor dense_x(alloc.get(),
|
||||
phi::DenseTensorMeta(phi::DataType::COMPLEX128,
|
||||
common::make_ddim({1}),
|
||||
phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::CPUContext*>(pool.Get(phi::CPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<complex128>(&dense_x);
|
||||
dense_x_data[0] = 1;
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
complex128 expected_value(1, 0);
|
||||
EXPECT_TRUE(expected_value == scalar_test.to<complex128>());
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromDenseTensor7) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::GPUPlace());
|
||||
phi::DenseTensor dense_x(alloc.get(),
|
||||
phi::DenseTensorMeta(phi::DataType::FLOAT32,
|
||||
common::make_ddim({1}),
|
||||
phi::DataLayout::NCHW));
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::GPUContext*>(pool.Get(phi::GPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<float>(&dense_x);
|
||||
FillTensor<<<1, 1, 0, dev_ctx->stream()>>>(dense_x_data);
|
||||
dev_ctx->Wait();
|
||||
phi::Scalar scalar_test(dense_x);
|
||||
ASSERT_NEAR(1, scalar_test.to<float>(), 1e-6);
|
||||
}
|
||||
|
||||
TEST(Scalar, ConstructFromTensor) {
|
||||
// 1. create tensor
|
||||
const auto alloc =
|
||||
std::make_unique<paddle::experimental::DefaultAllocator>(phi::GPUPlace());
|
||||
auto dense_x = std::make_shared<phi::DenseTensor>(
|
||||
alloc.get(),
|
||||
phi::DenseTensorMeta(phi::DataType::FLOAT32,
|
||||
common::make_ddim({1}),
|
||||
phi::DataLayout::NCHW));
|
||||
|
||||
phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance();
|
||||
auto* dev_ctx = reinterpret_cast<phi::GPUContext*>(pool.Get(phi::GPUPlace()));
|
||||
|
||||
auto* dense_x_data = dev_ctx->Alloc<float>(dense_x.get());
|
||||
FillTensor<<<1, 1, 0, dev_ctx->stream()>>>(dense_x_data);
|
||||
dev_ctx->Wait();
|
||||
paddle::Tensor x(dense_x);
|
||||
paddle::experimental::Scalar scalar_test(x);
|
||||
ASSERT_NEAR(1, scalar_test.to<float>(), 1e-6);
|
||||
}
|
||||
|
||||
} // namespace tests
|
||||
} // namespace phi
|
||||
@@ -0,0 +1,99 @@
|
||||
/* Copyright (c) 2016 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. */
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "paddle/phi/common/transform.h"
|
||||
|
||||
#include "paddle/common/hostdevice.h"
|
||||
#include "paddle/phi/backends/context_pool.h"
|
||||
#include "paddle/phi/common/memory_utils.h"
|
||||
|
||||
namespace phi {
|
||||
template <typename T>
|
||||
class Scale {
|
||||
public:
|
||||
explicit Scale(const T& scale) : scale_(scale) {}
|
||||
HOSTDEVICE T operator()(const T& a) const { return a * scale_; }
|
||||
|
||||
private:
|
||||
T scale_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Multiply {
|
||||
public:
|
||||
HOSTDEVICE T operator()(const T& a, const T& b) const { return a * b; }
|
||||
};
|
||||
|
||||
TEST(Transform, CPUUnary) {
|
||||
CPUContext ctx;
|
||||
float buf[4] = {0.1, 0.2, 0.3, 0.4};
|
||||
Transform<CPUContext> trans;
|
||||
trans(ctx, buf, buf + 4, buf, Scale<float>(10));
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ASSERT_NEAR(buf[i], static_cast<float>(i + 1), 1e-5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Transform, GPUUnary) {
|
||||
GPUPlace gpu0(0);
|
||||
DeviceContextPool& pool = DeviceContextPool::Instance();
|
||||
auto* ctx = reinterpret_cast<GPUContext*>(pool.Get(GPUPlace()));
|
||||
|
||||
float cpu_buf[4] = {0.1, 0.2, 0.3, 0.4};
|
||||
auto gpu_allocation = memory_utils::Alloc(gpu0, sizeof(float) * 4);
|
||||
float* gpu_buf = static_cast<float*>(gpu_allocation->ptr());
|
||||
memory_utils::Copy(
|
||||
gpu0, gpu_buf, CPUPlace(), cpu_buf, sizeof(cpu_buf), ctx->stream());
|
||||
Transform<GPUContext> trans;
|
||||
trans(*ctx, gpu_buf, gpu_buf + 4, gpu_buf, Scale<float>(10));
|
||||
ctx->Wait();
|
||||
memory_utils::Copy(
|
||||
CPUPlace(), cpu_buf, gpu0, gpu_buf, sizeof(cpu_buf), ctx->stream());
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ASSERT_NEAR(cpu_buf[i], static_cast<float>(i + 1), 1e-5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Transform, CPUBinary) {
|
||||
int buf[4] = {1, 2, 3, 4};
|
||||
Transform<CPUContext> trans;
|
||||
CPUContext ctx;
|
||||
trans(ctx, buf, buf + 4, buf, buf, Multiply<int>());
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ASSERT_EQ((i + 1) * (i + 1), buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Transform, GPUBinary) {
|
||||
int buf[4] = {1, 2, 3, 4};
|
||||
GPUPlace gpu0(0);
|
||||
DeviceContextPool& pool = DeviceContextPool::Instance();
|
||||
auto* ctx = reinterpret_cast<GPUContext*>(pool.Get(GPUPlace()));
|
||||
|
||||
auto gpu_allocation = memory_utils::Alloc(gpu0, sizeof(buf));
|
||||
int* gpu_buf = static_cast<int*>(gpu_allocation->ptr());
|
||||
memory_utils::Copy(
|
||||
gpu0, gpu_buf, CPUPlace(), buf, sizeof(buf), ctx->stream());
|
||||
Transform<GPUContext> trans;
|
||||
trans(*ctx, gpu_buf, gpu_buf + 4, gpu_buf, gpu_buf, Multiply<int>());
|
||||
ctx->Wait();
|
||||
memory_utils::Copy(
|
||||
CPUPlace(), buf, gpu0, gpu_buf, sizeof(buf), ctx->stream());
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
ASSERT_EQ((i + 1) * (i + 1), buf[i]);
|
||||
}
|
||||
}
|
||||
} // namespace phi
|
||||
Reference in New Issue
Block a user