-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
15 changed files
with
142 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
python/paddle/fluid/tests/custom_op/context_pool_test_op.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters