Skip to content

Commit b91c61d

Browse files
authored
Fix dnn related tests for custom device (PaddlePaddle#75609)
* fix cudnn related tests for custom device * fix compile error * add dnn available for custom device * add header * fix index_add
1 parent 984aee4 commit b91c61d

19 files changed

+166
-67
lines changed

paddle/phi/backends/custom/custom_device.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,16 @@ class CustomDevice : public DeviceInterface {
648648
return supported;
649649
}
650650

651+
bool IsDnnAvailable(size_t dev_id) override {
652+
const auto device = &devices_pool[dev_id];
653+
bool supported = false;
654+
if (pimpl_->is_dnn_supported) {
655+
pimpl_->is_dnn_supported(device, &supported);
656+
}
657+
VLOG(10) << Type() << " is dnn available: " << supported;
658+
return supported;
659+
}
660+
651661
void* InitEigenDevice(const Place& place,
652662
phi::stream::stream_t stream,
653663
phi::Allocator* allocator) override {

paddle/phi/backends/device_base.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ bool DeviceInterface::IsBFloat16Supported(size_t dev_id) {
8383
return false;
8484
}
8585

86+
bool DeviceInterface::IsDnnAvailable(size_t dev_id) {
87+
VLOG(10) << Type() << " is dnn available: " << false;
88+
return false;
89+
}
90+
8691
void* DeviceInterface::InitEigenDevice(const Place& place,
8792
phi::stream::stream_t stream,
8893
phi::Allocator* allocator) {

paddle/phi/backends/device_base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class DeviceInterface { // Driver / Runtime
8383

8484
virtual bool IsBFloat16Supported(size_t dev_id);
8585

86+
virtual bool IsDnnAvailable(size_t dev_id);
87+
8688
virtual void* InitEigenDevice(const Place& place,
8789
phi::stream::stream_t stream,
8890
phi::Allocator* allocator);

paddle/phi/backends/device_ext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,13 @@ struct C_DeviceInterface {
608608
*/
609609
C_Status (*is_bfloat16_supported)(const C_Device device, bool* supported);
610610

611+
/**
612+
* @brief Is dnn supported
613+
*
614+
* @param[C_Device, bool*] device, supported
615+
*/
616+
C_Status (*is_dnn_supported)(const C_Device device, bool* supported);
617+
611618
/**
612619
* @brief init eigen device
613620
*

paddle/phi/backends/device_manager.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ bool DeviceManager::IsBFloat16Supported(const Place& place) {
537537
return dev_impl->IsBFloat16Supported(device_id);
538538
}
539539

540+
bool DeviceManager::IsDnnAvailable(const Place& place) {
541+
auto device_type = place.GetDeviceType();
542+
auto device_id = place.GetDeviceId();
543+
auto dev_impl = GetDeviceInterfaceWithType(device_type);
544+
return dev_impl->IsDnnAvailable(device_id);
545+
}
546+
540547
void* DeviceManager::InitEigenDevice(const Place& place,
541548
phi::stream::stream_t stream,
542549
phi::Allocator* allocator) {

paddle/phi/backends/device_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class PADDLE_API DeviceManager {
190190

191191
static bool IsBFloat16Supported(const Place& place);
192192

193+
static bool IsDnnAvailable(const Place& place);
194+
193195
static void* InitEigenDevice(const Place& place,
194196
phi::stream::stream_t stream,
195197
phi::Allocator* allocator);

paddle/phi/kernels/gpu/grid_sample_utils.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#include <limits.h>
1818

19+
#ifdef PADDLE_WITH_CUSTOM_DEVICE
20+
#include "paddle/phi/backends/device_manager.h"
21+
#endif
1922
namespace phi {
2023

2124
enum class Mode {
@@ -42,7 +45,22 @@ static __forceinline__ __device__ bool InBounds3D(
4245
}
4346

4447
inline bool cudnnIsAvailable() {
45-
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
48+
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
49+
// Get all custom device types
50+
auto custom_device_types = phi::DeviceManager::GetAllCustomDeviceTypes();
51+
52+
// Use the first custom device type
53+
if (!custom_device_types.empty()) {
54+
const std::string& device_type = custom_device_types[0];
55+
// Get current device ID for this device type
56+
int device_id = phi::DeviceManager::GetDevice(device_type);
57+
// Create place for the current device
58+
phi::Place place(phi::CustomPlace(device_type, device_id));
59+
// Check if this device has DNN support
60+
return phi::DeviceManager::IsDnnAvailable(place);
61+
}
62+
return false;
63+
#elif defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
4664
// cuDNN/MIOpen version > 0 means DNN lib loaded; require v7+ for sampler
4765
return phi::backends::gpu::DnnVersion() >= 7000;
4866
#else

test/legacy_test/op_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,60 @@ def is_custom_device():
467467
return False
468468

469469

470+
def check_cudnn_version_and_compute_capability(
471+
min_cudnn_version=None, min_device_capability=None
472+
):
473+
"""
474+
Check if the current environment meets the specified cuDNN version and device capability requirements.
475+
476+
Args:
477+
min_cudnn_version (int, optional): Minimum required cuDNN version. If None, cuDNN version check is skipped.
478+
min_device_capability (int, optional): Minimum required device capability. If None, device capability check is skipped.
479+
480+
Returns:
481+
bool: True if the environment meets the requirements or if using custom device, False otherwise.
482+
"""
483+
if is_custom_device():
484+
return True
485+
486+
if not core.is_compiled_with_cuda():
487+
return False
488+
489+
# Check cuDNN version if specified
490+
cudnn_check = True
491+
if min_cudnn_version is not None:
492+
cudnn_check = core.cudnn_version() >= min_cudnn_version
493+
494+
# Check device capability if specified
495+
device_check = True
496+
if min_device_capability is not None:
497+
device_check = (
498+
paddle.device.cuda.get_device_capability()[0]
499+
>= min_device_capability
500+
)
501+
502+
return cudnn_check and device_check
503+
504+
505+
def get_cuda_version():
506+
if paddle.is_compiled_with_cuda():
507+
import re
508+
509+
result = os.popen("nvcc --version").read()
510+
regex = r'release (\S+),'
511+
match = re.search(regex, result)
512+
if match:
513+
num = str(match.group(1))
514+
integer, decimal = num.split('.')
515+
return int(integer) * 1000 + int(float(decimal) * 10)
516+
else:
517+
return -1
518+
elif is_custom_device():
519+
return 13000
520+
else:
521+
return -1
522+
523+
470524
@contextmanager
471525
def auto_parallel_test_guard(test_info_path, generated_test_file_path):
472526
test_info_file, generated_test_file = None, None

test/legacy_test/test_elementwise_max_op.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import numpy as np
1818
from op_test import (
1919
OpTest,
20+
check_cudnn_version_and_compute_capability,
2021
convert_float_to_uint16,
21-
is_custom_device,
2222
skip_check_grad_ci,
2323
)
2424

2525
import paddle
26-
from paddle.base import core
2726

2827

2928
class TestElementwiseOp(OpTest):
@@ -169,12 +168,8 @@ def init_data(self):
169168

170169

171170
@unittest.skipIf(
172-
(core.is_compiled_with_cuda() or is_custom_device())
173-
and (
174-
core.cudnn_version() < 8100
175-
or paddle.device.cuda.get_device_capability()[0] < 8
176-
),
177-
"run test when gpu is available and the minimum cudnn version is 8.1.0 and gpu's compute capability is at least 8.0.",
171+
not check_cudnn_version_and_compute_capability(8100, 8),
172+
"only support compiled with CUDA or custom device, and for CUDA cudnn version need larger than 8.1.0 and device's compute capability is at least 8.0",
178173
)
179174
class TestElementwiseBF16Op(OpTest):
180175
def init_data(self):

test/legacy_test/test_elementwise_tensor_split.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_float16_sub(self):
3232
if not (core.is_compiled_with_cuda() or is_custom_device()):
3333
return
3434

35-
gpu_info = paddle.device.cuda.get_device_properties()
35+
gpu_info = paddle.device.get_device_properties()
3636

3737
gpu_name = gpu_info.name
3838
try:

0 commit comments

Comments
 (0)