__init__.py 16.1 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14
# 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.

15
# TODO: define the functions to manipulate devices
16
import re
T
taixiurong 已提交
17
import os
18 19
from paddle.fluid import core
from paddle.fluid import framework
20
from paddle.fluid.dygraph.parallel import ParallelEnv
21
from paddle.fluid.framework import is_compiled_with_cinn  # noqa: F401
22 23
from paddle.fluid.framework import is_compiled_with_cuda  # noqa: F401
from paddle.fluid.framework import is_compiled_with_rocm  # noqa: F401
24
from . import cuda
J
james 已提交
25
from . import xpu
26

27
__all__ = [  # noqa
28
    'get_cudnn_version',
29
    'set_device',
30 31
    'get_device',
    'XPUPlace',
J
jianghaicheng 已提交
32
    'IPUPlace',
33
    'MLUPlace',
W
Wenyu 已提交
34
    'is_compiled_with_xpu',
J
jianghaicheng 已提交
35
    'is_compiled_with_ipu',
36
    'is_compiled_with_cinn',
37
    'is_compiled_with_cuda',
38
    'is_compiled_with_rocm',
39
    'is_compiled_with_npu',
40
    'is_compiled_with_mlu',
41
    'is_compiled_with_custom_device',
42 43 44 45
    'get_all_device_type',
    'get_all_custom_device_type',
    'get_available_device',
    'get_available_custom_device',
46 47
]

48 49 50
_cudnn_version = None


51 52
# TODO: WITH_ASCEND_CL may changed to WITH_NPU or others in the future
# for consistent.
53 54
def is_compiled_with_npu():
    """
55
    Whether paddle was built with WITH_ASCEND_CL=ON to support Ascend NPU.
56

57 58
    Return:
        bool, ``True`` if NPU is supported, otherwise ``False``.
59 60 61 62 63

    Examples:
        .. code-block:: python

            import paddle
64
            support_npu = paddle.device.is_compiled_with_npu()
65 66 67 68
    """
    return core.is_compiled_with_npu()


69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
def is_compiled_with_custom_device(device_type):
    """
    Whether paddle was built with Paddle_CUSTOM_DEVICE .

    Args:
        std::string, the registered device type, like "npu".
    Return:
        bool, ``True`` if CustomDevice is supported, otherwise ``False``.

    Examples:
        .. code-block:: python

            import paddle
            support_npu = paddle.device.is_compiled_with_custom_device("npu")
    """
    return core.is_compiled_with_custom_device(device_type)


J
jianghaicheng 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
def is_compiled_with_ipu():
    """
    Whether paddle was built with WITH_IPU=ON to support Graphcore IPU.

    Returns (bool): `True` if IPU is supported, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_ipu = paddle.is_compiled_with_ipu()
    """
    return core.is_compiled_with_ipu()


def IPUPlace():
    """
    Return a Graphcore IPU Place

    Examples:
        .. code-block:: python

            # required: ipu

            import paddle
            place = paddle.device.IPUPlace()
    """
    return core.IPUPlace()


117 118 119 120 121 122 123 124 125 126
def is_compiled_with_xpu():
    """
    Whether paddle was built with WITH_XPU=ON to support Baidu Kunlun

    Returns (bool): whether paddle was built with WITH_XPU=ON

    Examples:
        .. code-block:: python

            import paddle
127
            support_xpu = paddle.device.is_compiled_with_xpu()
128 129 130 131 132 133 134 135 136 137 138 139 140
    """
    return core.is_compiled_with_xpu()


def XPUPlace(dev_id):
    """
    Return a Baidu Kunlun Place

    Parameters:
        dev_id(int): Baidu Kunlun device id

    Examples:
        .. code-block:: python
141

142
            # required: xpu
143

144
            import paddle
145
            place = paddle.device.XPUPlace(0)
146 147 148 149
    """
    return core.XPUPlace(dev_id)


150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
def is_compiled_with_mlu():
    """
    Whether paddle was built with WITH_MLU=ON to support Cambricon MLU

    Returns (bool): whether paddle was built with WITH_MLU=ON

    Examples:
        .. code-block:: python

            # required: mlu

            import paddle
            support_mlu = paddle.device.is_compiled_with_mlu()
    """
    return core.is_compiled_with_mlu()


def MLUPlace(dev_id):
    """
    Return a Cambricon MLU Place

    Parameters:
        dev_id(int): MLU device id

    Examples:
        .. code-block:: python

            # required: mlu

            import paddle
            place = paddle.device.MLUPlace(0)
    """
    return core.MLUPlace(dev_id)


185 186
def get_cudnn_version():
    """
187
    This funciton return the version of cudnn. the retuen value is int which represents the
188
    cudnn version. For example, if it return 7600, it represents the version of cudnn is 7.6.
189

190 191 192 193 194
    Returns:
        int: A int value which represents the cudnn version. If cudnn version is not installed, it return None.

    Examples:
        .. code-block:: python
195

196 197
            import paddle

198
            cudnn_version = paddle.device.get_cudnn_version()
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215



    """
    global _cudnn_version
    if not core.is_compiled_with_cuda():
        return None
    if _cudnn_version is None:
        cudnn_version = int(core.cudnn_version())
        _cudnn_version = cudnn_version
        if _cudnn_version < 0:
            return None
        else:
            return cudnn_version
    else:
        return _cudnn_version

216

C
chentianyu03 已提交
217
def _convert_to_place(device):
218
    lower_device = device.lower()
S
shentanyue 已提交
219 220 221 222 223 224 225
    if device in core.get_all_custom_device_type():
        selected_devices = os.getenv(
            "FLAGS_selected_{}s".format(device), "0"
        ).split(",")
        device_id = int(selected_devices[0])
        place = core.CustomPlace(device, device_id)
    elif lower_device == 'cpu':
226
        place = core.CPUPlace()
227 228
    elif lower_device == 'gpu':
        if not core.is_compiled_with_cuda():
229 230 231 232
            raise ValueError(
                "The device should not be 'gpu', "
                "since PaddlePaddle is not compiled with CUDA"
            )
233
        place = core.CUDAPlace(ParallelEnv().dev_id)
234 235
    elif lower_device == 'xpu':
        if not core.is_compiled_with_xpu():
236 237 238 239
            raise ValueError(
                "The device should not be 'xpu', "
                "since PaddlePaddle is not compiled with XPU"
            )
T
taixiurong 已提交
240 241 242
        selected_xpus = os.getenv("FLAGS_selected_xpus", "0").split(",")
        device_id = int(selected_xpus[0])
        place = core.XPUPlace(device_id)
H
houj04 已提交
243 244
    elif lower_device == 'npu':
        if not core.is_compiled_with_npu():
245 246 247 248
            raise ValueError(
                "The device should not be 'npu', "
                "since PaddlePaddle is not compiled with NPU"
            )
H
houj04 已提交
249 250 251
        selected_npus = os.getenv("FLAGS_selected_npus", "0").split(",")
        device_id = int(selected_npus[0])
        place = core.NPUPlace(device_id)
J
jianghaicheng 已提交
252 253 254
    elif lower_device == 'ipu':
        if not core.is_compiled_with_ipu():
            raise ValueError(
255 256 257
                "The device should not be 'ipu', "
                "since PaddlePaddle is not compiled with IPU"
            )
J
jianghaicheng 已提交
258
        place = core.IPUPlace()
259 260
    elif lower_device == 'mlu':
        if not core.is_compiled_with_mlu():
261 262 263 264
            raise ValueError(
                "The device should not be 'mlu', "
                "since PaddlePaddle is not compiled with MLU"
            )
265 266 267
        selected_mlus = os.getenv("FLAGS_selected_mlus", "0").split(",")
        device_id = int(selected_mlus[0])
        place = core.MLUPlace(device_id)
268
    else:
269 270
        avaliable_gpu_device = re.match(r'gpu:\d+', lower_device)
        avaliable_xpu_device = re.match(r'xpu:\d+', lower_device)
H
houj04 已提交
271
        avaliable_npu_device = re.match(r'npu:\d+', lower_device)
272
        avaliable_mlu_device = re.match(r'mlu:\d+', lower_device)
273 274 275
        if avaliable_gpu_device:
            if not core.is_compiled_with_cuda():
                raise ValueError(
276
                    "The device should not be {}, since PaddlePaddle is "
277 278
                    "not compiled with CUDA".format(avaliable_gpu_device)
                )
279 280 281 282 283 284 285
            device_info_list = device.split(':', 1)
            device_id = device_info_list[1]
            device_id = int(device_id)
            place = core.CUDAPlace(device_id)
        if avaliable_xpu_device:
            if not core.is_compiled_with_xpu():
                raise ValueError(
286
                    "The device should not be {}, since PaddlePaddle is "
287 288
                    "not compiled with XPU".format(avaliable_xpu_device)
                )
289 290 291 292
            device_info_list = device.split(':', 1)
            device_id = device_info_list[1]
            device_id = int(device_id)
            place = core.XPUPlace(device_id)
H
houj04 已提交
293 294
        if avaliable_npu_device:
            if not core.is_compiled_with_npu():
S
shentanyue 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308
                device_info_list = device.split(':', 1)
                device_type = device_info_list[0]
                if device_type in core.get_all_custom_device_type():
                    device_id = device_info_list[1]
                    device_id = int(device_id)
                    place = core.CustomPlace(device_type, device_id)
                    return place
                else:
                    raise ValueError(
                        "The device should not be {}, since PaddlePaddle is "
                        "not compiled with NPU or compiled with custom device".format(
                            avaliable_npu_device
                        )
                    )
H
houj04 已提交
309 310 311 312
            device_info_list = device.split(':', 1)
            device_id = device_info_list[1]
            device_id = int(device_id)
            place = core.NPUPlace(device_id)
313 314 315 316
        if avaliable_mlu_device:
            if not core.is_compiled_with_mlu():
                raise ValueError(
                    "The device should not be {}, since PaddlePaddle is "
317 318
                    "not compiled with mlu".format(avaliable_mlu_device)
                )
319 320 321 322
            device_info_list = device.split(':', 1)
            device_id = device_info_list[1]
            device_id = int(device_id)
            place = core.MLUPlace(device_id)
S
shentanyue 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        if (
            not avaliable_gpu_device
            and not avaliable_xpu_device
            and not avaliable_npu_device
            and not avaliable_mlu_device
        ):
            device_info_list = device.split(':', 1)
            device_type = device_info_list[0]
            if device_type in core.get_all_custom_device_type():
                device_id = device_info_list[1]
                device_id = int(device_id)
                place = core.CustomPlace(device_type, device_id)
            else:
                raise ValueError(
                    "The device must be a string which is like 'cpu', {}".format(
                        ', '.join(
                            "'{}', '{}:x'".format(x, x)
                            for x in ['gpu', 'xpu', 'npu', 'mlu']
                            + core.get_all_custom_device_type()
                        )
                    )
                )
C
chentianyu03 已提交
345
    return place
346

C
chentianyu03 已提交
347 348 349

def set_device(device):
    """
350
    Paddle supports running calculations on various types of devices, including CPU, GPU, XPU, NPU, MLU and IPU.
C
chentianyu03 已提交
351 352 353 354 355
    They are represented by string identifiers. This function can specify the global device
    which the OP will run.

    Parameters:
        device(str): This parameter determines the specific running device.
356 357
            It can be ``cpu``, ``gpu``, ``xpu``, ``npu``, ``mlu``, ``gpu:x``, ``xpu:x``, ``npu:x``, ``mlu:x`` and ``ipu``,
            where ``x`` is the index of the GPUs, XPUs, NPUs or MLUs.
C
chentianyu03 已提交
358 359 360 361

    Examples:

     .. code-block:: python
362

C
chentianyu03 已提交
363 364
        import paddle

365
        paddle.device.set_device("cpu")
C
chentianyu03 已提交
366 367 368 369 370
        x1 = paddle.ones(name='x1', shape=[1, 2], dtype='int32')
        x2 = paddle.zeros(name='x2', shape=[1, 2], dtype='int32')
        data = paddle.stack([x1,x2], axis=1)
    """
    place = _convert_to_place(device)
371 372
    framework._set_expected_place(place)
    return place
373 374 375 376 377


def get_device():
    """
    This funciton can get the current global device of the program is running.
378
    It's a string which is like 'cpu', 'gpu:x', 'xpu:x', 'mlu:x' and 'npu:x'. if the global device is not
379
    set, it will return a string which is 'gpu:x' when cuda is avaliable or it
380 381 382 383 384
    will return a string which is 'cpu' when cuda is not avaliable.

    Examples:

     .. code-block:: python
385

386
        import paddle
387
        device = paddle.device.get_device()
388 389 390 391 392 393 394 395 396

    """
    device = ''
    place = framework._current_expected_place()
    if isinstance(place, core.CPUPlace):
        device = 'cpu'
    elif isinstance(place, core.CUDAPlace):
        device_id = place.get_device_id()
        device = 'gpu:' + str(device_id)
397 398 399
    elif isinstance(place, core.XPUPlace):
        device_id = place.get_device_id()
        device = 'xpu:' + str(device_id)
H
houj04 已提交
400 401 402
    elif isinstance(place, core.NPUPlace):
        device_id = place.get_device_id()
        device = 'npu:' + str(device_id)
J
jianghaicheng 已提交
403 404 405
    elif isinstance(place, core.IPUPlace):
        num_devices = core.get_ipu_device_count()
        device = "ipus:{{0-{}}}".format(num_devices - 1)
406 407 408
    elif isinstance(place, core.MLUPlace):
        device_id = place.get_device_id()
        device = 'mlu:' + str(device_id)
409 410 411 412
    elif isinstance(place, core.CustomPlace):
        device_id = place.get_device_id()
        device_type = place.get_device_type()
        device = device_type + ':' + str(device_id)
J
jianghaicheng 已提交
413 414
    else:
        raise ValueError("The device specification {} is invalid".format(place))
415 416

    return device
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450


def get_all_device_type():
    """
    Get all available device types.

    Returns:
        A list of all available device types.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_all_device_type()

            # Case 1: paddlepaddle-cpu package installed, and no custom device registerd.
            # Output: ['cpu']

            # Case 2: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: ['cpu', 'gpu']

            # Case 3: paddlepaddle-cpu package installed, and custom deivce 'CustomCPU' is registerd.
            # Output: ['cpu', 'CustomCPU']

            # Case 4: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['cpu', 'gpu', 'CustomCPU', 'CustomGPU']
    """
    return core.get_all_device_type()


def get_all_custom_device_type():
    """
    Get all available custom device types.

451
    Returns:
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        A list of all available custom device types.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_all_custom_device_type()

            # Case 1: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: None

            # Case 2: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['CustomCPU', 'CustomGPU']
    """
    return core.get_all_custom_device_type()


def get_available_device():
    """
    Get all available devices.

    Returns:
        A list of all available devices.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_available_device()

            # Case 1: paddlepaddle-cpu package installed, and no custom device registerd.
            # Output: ['cpu']

            # Case 2: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: ['cpu', 'gpu:0', 'gpu:1']

            # Case 3: paddlepaddle-cpu package installed, and custom deivce 'CustomCPU' is registerd.
            # Output: ['cpu', 'CustomCPU']

            # Case 4: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['cpu', 'gpu:0', 'gpu:1', 'CustomCPU', 'CustomGPU:0', 'CustomGPU:1']
    """
    return core.get_available_device()


def get_available_custom_device():
    """
    Get all available custom devices.

    Returns:
       A list of all available custom devices.

    Examples:
        .. code-block:: python

            import paddle
            paddle.device.get_available_custom_device()

            # Case 1: paddlepaddle-gpu package installed, and no custom device registerd.
            # Output: None

            # Case 2: paddlepaddle-gpu package installed, and custom deivce 'CustomCPU' and 'CustomGPU' is registerd.
            # Output: ['CustomCPU', 'CustomGPU:0', 'CustomGPU:1']
    """
    return core.get_available_custom_device()