Skip to content

Commit

Permalink
[CustomOp] Add context pool unittests (PaddlePaddle#41085)
Browse files Browse the repository at this point in the history
* add context pool unittests

* fix timeout

* polish details

* change option pos

* add dll decl for wndows

* fix pre-commit error

* move dll_decl and export DeviceContext

* replace lost dll_decl.h
  • Loading branch information
chenwhql committed Apr 13, 2022
1 parent 9c0f994 commit 1a4ab96
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 11 deletions.
2 changes: 1 addition & 1 deletion paddle/phi/api/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License. */

// new phi apis
#include "paddle/phi/api/include/api.h"
#include "paddle/phi/api/include/context_pool.h"
#include "paddle/phi/api/include/sparse_api.h"
#include "paddle/phi/api/include/tensor.h"

Expand All @@ -38,7 +39,6 @@ limitations under the License. */

// original custom op headers
#include "paddle/phi/api/ext/dispatch.h"
#include "paddle/phi/api/ext/dll_decl.h"
#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/api/ext/op_meta_info.h"
#include "paddle/phi/api/ext/place.h"
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/ext/op_meta_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ limitations under the License. */
#include <utility>
#include <vector>

#include "paddle/phi/api/ext/dll_decl.h"
#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/api/include/dll_decl.h"
#include "paddle/phi/api/include/tensor.h"
#include "paddle/utils/any.h"

Expand Down
7 changes: 6 additions & 1 deletion paddle/phi/api/include/context_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */

#include <mutex>

#include "paddle/phi/api/include/dll_decl.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/macros.h"
#include "paddle/utils/flat_hash_map.h"
Expand Down Expand Up @@ -55,8 +56,12 @@ struct DefaultDeviceContextType<AllocationType::GPU> {
* In order not to depend on the fluid's DeviceContextPool,
* the DeviceContextPool here needs to be initialized in the fluid, and cannot
* be initialized by itself.
*
* Note: DeviceContextPool is an experimental API and may be removed in the
* future. From 2.3, we recommend directly using the C++ API to combine new
* perators.
*/
class DeviceContextPool {
class PADDLE_API DeviceContextPool {
public:
static DeviceContextPool& Instance();

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion paddle/phi/api/include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ using gpuStream_t = cudaStream_t;
using gpuStream_t = hipStream_t;
#endif

#include "paddle/phi/api/ext/dll_decl.h"
#include "paddle/phi/api/ext/place.h"
#include "paddle/phi/api/include/dll_decl.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/layout.h"
#include "paddle/phi/common/place.h"
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/api/lib/api_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ limitations under the License. */

#pragma once

#include "paddle/phi/api/ext/dll_decl.h"
#include "paddle/phi/api/include/dll_decl.h"

namespace paddle {
namespace experimental {
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/backends/cpu/cpu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License. */

namespace phi {

class CPUContext : public DeviceContext {
class PADDLE_API CPUContext : public DeviceContext {
public:
CPUContext();
CPUContext(CPUContext&&);
Expand Down
2 changes: 1 addition & 1 deletion paddle/phi/backends/gpu/gpu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DnnWorkspaceHandle {
std::unique_ptr<std::mutex> mtx_;
};

class GPUContext : public DeviceContext {
class PADDLE_API GPUContext : public DeviceContext {
public:
GPUContext();
GPUContext(GPUContext&&);
Expand Down
10 changes: 7 additions & 3 deletions paddle/phi/common/place.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License. */

#include <string>

#include "paddle/phi/api/include/dll_decl.h"

namespace phi {

enum class AllocationType : int8_t {
Expand All @@ -33,11 +35,13 @@ enum class AllocationType : int8_t {

const char* AllocationTypeStr(AllocationType type);

size_t GetOrRegisterGlobalDeviceTypeId(const std::string& device_type);
std::string GetGlobalDeviceType(size_t device_type_id_);
PADDLE_API size_t
GetOrRegisterGlobalDeviceTypeId(const std::string& device_type);

PADDLE_API std::string GetGlobalDeviceType(size_t device_type_id_);

/// \brief The place is used to specify where the data is stored.
class Place {
class PADDLE_API Place {
public:
Place() : device(0), alloc_type_(AllocationType::UNDEFINED) {}

Expand Down
3 changes: 2 additions & 1 deletion paddle/phi/core/device_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */

#include <memory>

#include "paddle/phi/api/include/dll_decl.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/allocator.h"
Expand All @@ -30,7 +31,7 @@ class TensorBase;
* All kernels must access the interfaces provided by the backend through
* DeviceContext.
*/
class DeviceContext {
class PADDLE_API DeviceContext {
using DataType = paddle::experimental::DataType;

public:
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/fluid/tests/custom_op/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ if(WITH_GPU OR APPLE)
py_test(test_custom_relu_op_setup SRCS test_custom_relu_op_setup.py)
py_test(test_custom_relu_op_jit SRCS test_custom_relu_op_jit.py)
py_test(test_custom_relu_model SRCS test_custom_relu_model.py)
py_test(test_context_pool SRCS test_context_pool.py)

# Compiling shared library will cost some time, but running process is very fast.
set_tests_properties(test_custom_relu_op_setup PROPERTIES TIMEOUT 250)
set_tests_properties(test_custom_relu_op_jit PROPERTIES TIMEOUT 180)
set_tests_properties(test_custom_relu_model PROPERTIES TIMEOUT 180)
set_tests_properties(test_context_pool PROPERTIES TIMEOUT 180)
endif()

py_test(test_custom_raw_op_kernel_op SRCS test_custom_raw_op_kernel_op.py)
Expand Down
54 changes: 54 additions & 0 deletions python/paddle/fluid/tests/custom_op/context_pool_test_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 <iostream>

#include "paddle/extension.h"
#include "paddle/phi/backends/all_context.h"

#define CHECK_INPUT(x) \
PD_CHECK(x.place() == paddle::PlaceType::kCPU, #x " must be a CPU Tensor.")

std::vector<paddle::Tensor> ContextPoolTest(const paddle::Tensor& x) {
// 1. test cpu context
paddle::experimental::Place cpu_place(
paddle::experimental::AllocationType::CPU);
auto* cpu_ctx =
paddle::experimental::DeviceContextPool::Instance()
.Get<paddle::experimental::AllocationType::CPU>(cpu_place);
PD_CHECK(cpu_ctx->GetPlace() == cpu_place);
// if want to use the eigen_device here, need to include eigen headers
auto* cpu_eigen_device = cpu_ctx->eigen_device();
PD_CHECK(cpu_eigen_device != nullptr);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
// 2. test gpu context
paddle::experimental::Place gpu_place(
paddle::experimental::AllocationType::GPU);
auto* gpu_ctx =
paddle::experimental::DeviceContextPool::Instance()
.Get<paddle::experimental::AllocationType::GPU>(gpu_place);
PD_CHECK(gpu_ctx->GetPlace() == gpu_place);
// if want to use the eigen_device here, need to include eigen headers
auto* gpu_eigen_device = gpu_ctx->eigen_device();
PD_CHECK(gpu_eigen_device != nullptr);
#endif

return {x};
}

PD_BUILD_OP(context_pool_test)
.Inputs({"X"})
.Outputs({"Out"})
.SetKernelFn(PD_KERNEL(ContextPoolTest));
Empty file.
62 changes: 62 additions & 0 deletions python/paddle/fluid/tests/custom_op/test_context_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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.

import os
import unittest
import numpy as np

import paddle
from paddle.utils.cpp_extension import load, get_build_directory
from utils import paddle_includes, extra_cc_args, extra_nvcc_args
from paddle.utils.cpp_extension.extension_utils import run_cmd
from paddle.fluid.framework import _test_eager_guard

# Because Windows don't use docker, the shared lib already exists in the
# cache dir, it will not be compiled again unless the shared lib is removed.
file = '{}\\context_pool_jit\\context_pool_jit.pyd'.format(get_build_directory(
))
if os.name == 'nt' and os.path.isfile(file):
cmd = 'del {}'.format(file)
run_cmd(cmd, True)

# Compile and load custom op Just-In-Time.
custom_ops = load(
name='context_pool_jit',
sources=['context_pool_test_op.cc'],
extra_include_paths=paddle_includes, # add for Coverage CI
extra_cxx_cflags=extra_cc_args, # test for cflags
extra_cuda_cflags=extra_nvcc_args, # test for cflags
verbose=True)


class TestContextPool(unittest.TestCase):
def setUp(self):
self.devices = ['cpu']
if paddle.is_compiled_with_cuda():
self.devices.append('gpu')

def use_context_pool(self):
x = paddle.ones([2, 2], dtype='float32')
out = custom_ops.context_pool_test(x)

self.assertTrue(np.array_equal(x.numpy(), out.numpy()))

def test_using_context_pool(self):
with _test_eager_guard():
self.use_context_pool()
self.use_context_pool()


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions python/paddle/utils/cpp_extension/extension_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ def normalize_extension_kwargs(kwargs, use_cuda=False):
runtime_library_dirs.extend(find_paddle_libraries(use_cuda))
kwargs['runtime_library_dirs'] = runtime_library_dirs

if compile_dir is None:
# Add this compile option to isolate fluid headers
add_compile_flag(extra_compile_args, ['-DPADDLE_WITH_CUSTOM_KERNEL'])
kwargs['extra_compile_args'] = extra_compile_args

kwargs['language'] = 'c++'
Expand Down

0 comments on commit 1a4ab96

Please sign in to comment.