-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
framework.py
8249 lines (6816 loc) · 283 KB
/
framework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
451
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
from __future__ import annotations
import collections
import copy
import functools
import multiprocessing
import os
import re
import subprocess
import sys
import textwrap
import threading
import traceback
import warnings
from collections.abc import Iterable
from types import FunctionType, MethodType
from typing import TYPE_CHECKING
import numpy as np
import paddle.version as paddle_version
from .. import pir
from . import core, unique_name
from .libpaddle import DataType
from .proto import (
data_feed_pb2, # noqa: F401
framework_pb2,
)
from .variable_index import _getitem_static, _setitem_static
from .wrapped_decorator import signature_safe_contextmanager, wrap_decorator
if TYPE_CHECKING:
from paddle.static.amp.fp16_utils import AmpOptions
__all__ = []
EMPTY_VAR_NAME = core.kEmptyVarName()
TEMP_VAR_NAME = core.kTempVarName()
GRAD_VAR_SUFFIX = core.kGradVarSuffix()
ZERO_VAR_SUFFIX = core.kZeroVarSuffix()
CONTROL_DEP_VAR_PREFIX = core.kControlDepVarName()
_global_flags_ = core.globals()
SUPPORT_PROMOTION_OPS_AND_INPUTNAME = {
"elementwise_add": ['X', 'Y'],
"elementwise_add_grad": ['X', 'Y'],
"elementwise_sub": ['X', 'Y'],
"elementwise_sub_grad": ['X', 'Y'],
"elementwise_mul": ['X', 'Y'],
"elementwise_mul_grad": ['X', 'Y'],
"where": ['X', 'Y'],
"where_grad": ['X', 'Y'],
}
def _global_flags():
return _global_flags_
def set_flags(flags):
"""
This function sets the GFlags value in Paddle.
For FLAGS please refer to :ref:`en_guides_flags_flags`
Args:
flags (dict): A dict contains flags and its value.
Examples:
.. code-block:: python
>>> import paddle
>>> paddle.set_flags({'FLAGS_eager_delete_tensor_gb': 1.0})
"""
if not isinstance(flags, dict):
raise TypeError('flags in set_flags should be a dict')
for key, value in flags.items():
if _global_flags().is_public(key):
_global_flags()[key] = value
else:
raise ValueError(
"Flag %s cannot set its value through this function." % (key)
)
def get_flags(flags):
"""
This function gets the GFlags value in Paddle.
For FLAGS please refer to :ref:`en_guides_flags_flags`
Args:
flags(list|tuple|str): A list/tuple of string or a string which is the flag's name.
Returns:
flag's value in Paddle.
Examples:
.. code-block:: python
>>> import paddle
>>> flags = ['FLAGS_eager_delete_tensor_gb', 'FLAGS_check_nan_inf']
>>> res = paddle.get_flags(flags)
>>> print(res)
{'FLAGS_eager_delete_tensor_gb': 0.0, 'FLAGS_check_nan_inf': False}
"""
flags_value = {}
if isinstance(flags, (list, tuple)):
for key in flags:
if _global_flags().is_public(key):
value = _global_flags()[key]
temp = {key: value}
flags_value.update(temp)
else:
raise ValueError(
'Flag %s cannot get its value through this function.'
% (key)
)
elif isinstance(flags, str):
if _global_flags().is_public(flags):
value = _global_flags()[flags]
temp = {flags: value}
flags_value.update(temp)
else:
raise ValueError(
'Flag %s cannot get its value through this function.' % (flags)
)
else:
raise TypeError('Flags in get_flags should be a list, tuple or string.')
return flags_value
# use thread local to create thread save global variables.
class GlobalThreadLocal(threading.local):
def __init__(self):
"""
init the thread local data.
TODO(xiongkun): how to access another thread local data ?
"""
global _dygraph_tracer_
self._in_to_static_mode_ = False
self._functional_dygraph_context_manager = None
self._dygraph_tracer_ = _dygraph_tracer_
self._use_pir_api_ = get_flags("FLAGS_enable_pir_api")[
'FLAGS_enable_pir_api'
]
def __str__(self):
strings = []
strings.append("_in_to_static_mode_:" + str(self._in_to_static_mode_))
strings.append(
"_functional_dygraph_context_manager:"
+ str(self._functional_dygraph_context_manager)
)
strings.append("_dygraph_tracer_:" + str(self._dygraph_tracer_))
return "\n".join(strings)
def __setattr__(self, name, val):
if name == '_dygraph_tracer_':
global _dygraph_tracer_
_dygraph_tracer_ = val
core._switch_tracer(val)
self.__dict__[name] = val
_dygraph_tracer_ = None
global_var = GlobalThreadLocal()
_global_expected_place_ = None
_current_device = None
global_prog_seed = 0
_current_pipeline_stage = None
_current_cuda_graph_mode = None
_stride_in_no_check_dy2st_diff_mode = False
# special_op_attrs, extra_op_attrs are prepared for printing warnings
# when turning on FLAGS_print_extra_attrs
special_op_attrs = {
"elementwise_add": [{"axis": -1}],
"elementwise_sub": [{"axis": -1}],
"elementwise_mul": [{"axis": -1}],
"elementwise_div": [{"axis": -1}],
"elementwise_max": [{"axis": -1}],
"elementwise_min": [{"axis": -1}],
"elementwise_pow": [{"axis": -1}],
"elementwise_mod": [{"axis": -1}],
"elementwise_floordiv": [{"axis": -1}],
"less_than": [{"axis": -1}],
"less_equal": [{"axis": -1}],
"greater_than": [{"axis": -1}],
"greater_equal": [{"axis": -1}],
"equal": [{"axis": -1}],
"not_equal": [{"axis": -1}],
"amax": [{"reduce_all": False}],
"amin": [{"reduce_all": False}],
"any": [{"reduce_all": False}],
"frobenius_norm": [{"reduce_all": False}],
"logsumexp": [{"reduce_all": False}],
"reduce_max": [{"reduce_all": False}],
"reduce_min": [{"reduce_all": False}],
"reduce_mean": [{"reduce_all": False}],
"reduce_prod": [{"reduce_all": False}],
"reduce_sum": [{"reduce_all": False}],
}
extra_op_attrs = {
"gather": ["overwrite"],
"graph_reindex": ["flag_buffer_hashtable"],
"graph_sample_neighbors": ["flag_perm_buffer"],
"relu6": ["threshold"],
"swish": ["beta"],
"hsigmoid_loss": ["remote_prefetch"],
"max_pool2d_with_index": ["global_pooling"],
"uniform": ["diag_num"],
"unique": ["is_sorted"],
}
paddle_type_to_proto_type = {
DataType.BOOL: core.VarDesc.VarType.BOOL,
DataType.FLOAT16: core.VarDesc.VarType.FP16,
DataType.UINT16: core.VarDesc.VarType.BF16,
DataType.BFLOAT16: core.VarDesc.VarType.BF16,
DataType.FLOAT32: core.VarDesc.VarType.FP32,
DataType.FLOAT64: core.VarDesc.VarType.FP64,
DataType.INT8: core.VarDesc.VarType.INT8,
DataType.INT16: core.VarDesc.VarType.INT16,
DataType.INT32: core.VarDesc.VarType.INT32,
DataType.INT64: core.VarDesc.VarType.INT64,
DataType.UINT8: core.VarDesc.VarType.UINT8,
DataType.COMPLEX64: core.VarDesc.VarType.COMPLEX64,
DataType.COMPLEX128: core.VarDesc.VarType.COMPLEX128,
}
def in_dygraph_mode():
"""
.. note::
Dynamic graph mode is turn ON by default since paddle 2.0.0
This API checks whether paddle runs in dynamic graph mode.
You can turn ON static graph mode by `enable_static <../dygraph/base/disable_dygraph_en.html>`_ ,
and turn OFF static graph mode by `disable_static <../dygraph/base/enable_dygraph_en.html>`_ .
Returns:
bool: Whether paddle runs in dynamic graph mode.
Examples:
.. code-block:: python
>>> import paddle
>>> print(paddle.in_dynamic_mode()) # dynamic mode is turn ON by default since paddle 2.0.
True
>>> paddle.enable_static()
>>> print(paddle.in_dynamic_mode()) # Now we are in static graph mode
False
>>> paddle.disable_static()
>>> print(paddle.in_dynamic_mode()) # Now we are in dynamic mode
True
"""
return global_var._dygraph_tracer_ is not None
def in_pir_mode():
"""
This API checks whether paddle runs in static graph mode and use pir api.
Returns:
bool: Whether paddle runs in static graph mode and use pir api.
Examples:
.. code-block:: python
>>> import paddle
>>> print(paddle.framework.in_pir_mode())
False
>>> paddle.enable_static()
>>> with paddle.pir_utils.IrGuard():
... print(paddle.framework.in_pir_mode())
True
"""
return global_var._use_pir_api_ and not in_dygraph_mode()
def use_pir_api():
return global_var._use_pir_api_
def in_dynamic_or_pir_mode():
"""
This API checks whether paddle runs in dynamic graph or pir mode.
Returns:
bool: Whether paddle runs in static graph mode and use pir api.
Examples:
.. code-block:: python
>>> import paddle
>>> print(paddle.framework.in_dynamic_or_pir_mode())
True
>>> paddle.enable_static()
>>> print(paddle.framework.in_dynamic_or_pir_mode())
False
>>> with paddle.pir_utils.IrGuard():
... print(paddle.framework.in_dynamic_or_pir_mode())
True
"""
return global_var._dygraph_tracer_ is not None or global_var._use_pir_api_
global_ipu_index = -1
global_ipu_stage = -1
ipu_index_attr_name = 'ipu_index'
ipu_stage_attr_name = 'ipu_stage'
@signature_safe_contextmanager
def ipu_shard_guard(index=-1, stage=-1):
"""
Used to shard the graph on IPUs. Set each Op run on which IPU in the sharding and which stage in the pipelining.
Args:
index(int, optional): Specify which ipu the Tensor is computed on, (such as '0, 1, 2, 3').
The default value is -1, which means the Op only run on IPU 0.
stage(int, optional): Specify the computation order of the sharded model(such as '0, 1, 2, 3').
The sharded model will be computed from small to large. The default value is -1,
which means no pipelining computation order and run Ops in terms of graph.
Note:
Only if the enable_manual_shard=True, the 'index' is able to be set not -1. Please refer
to :ref:`api_paddle_static_IpuStrategy`.
Only if the enable_pipelining=True, the 'stage' is able to be set not -1. Please refer
to :ref:`api_paddle_static_IpuStrategy`.
A index is allowed to match none stage or a stage. A stage is only allowed to match a new or
duplicated index.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:IPU)
>>> import paddle
>>> paddle.device.set_device('ipu')
>>> paddle.enable_static()
>>> a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
>>> with paddle.static.ipu_shard_guard(index=0, stage=0):
... b = a + 1
>>> with paddle.static.ipu_shard_guard(index=1, stage=1):
... c = b + 1
>>> with paddle.static.ipu_shard_guard(index=0, stage=2):
... d = c + 1
"""
if not core.is_compiled_with_ipu():
raise ValueError(
"Can not use this function since PaddlePaddle is not compiled with IPU"
)
global global_ipu_index
global global_ipu_stage
prev_ipu_index = global_ipu_index
prev_ipu_stage = global_ipu_stage
global_ipu_index = index
global_ipu_stage = stage
try:
yield
finally:
global_ipu_index = prev_ipu_index
global_ipu_stage = prev_ipu_stage
def set_ipu_shard(call_func, index=-1, stage=-1):
"""
Shard the ipu with the given call function. Set every ops in call function to the given ipu sharding.
Note:
Only when enable_manual_shard=True to set the index to a value other than -1. please refer to :ref:`api_paddle_static_IpuStrategy` .
Only when enable_pipelining=True to set stage to a value other than -1. please refer to :ref:`api_paddle_static_IpuStrategy` .
An index supports a corresponding None stage or a stage, and a stage only supports a new index or a duplicate index.
Args:
call_func(Layer|function): Specify the call function to be wrapped.
index(int, optional): Specify which ipu the Tensor is computed on, (such as ‘0, 1, 2, 3’).
The default value is -1, which means the Op only run on IPU 0.
stage(int, optional): Specify the computation order of the sharded model(such as ‘0, 1, 2, 3’).
The sharded model will be computed from small to large. The default value is -1,
which means no pipelining computation order and run Ops in terms of graph.
Returns:
The wrapped call function.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:IPU)
>>> import paddle
>>> paddle.device.set_device('ipu')
>>> paddle.enable_static()
>>> a = paddle.static.data(name='data', shape=[None, 1], dtype='float32')
>>> relu = paddle.nn.ReLU()
>>> relu = paddle.static.set_ipu_shard(relu, index=1, stage=1)
>>> relu(a)
"""
def decorate(func):
def wrapper(*args, **kwargs):
with ipu_shard_guard(index=index, stage=stage):
return func(*args, **kwargs)
return wrapper
from paddle.nn import Layer
if not isinstance(call_func, Layer):
if callable(call_func):
return decorate(call_func)
else:
raise TypeError(
"Unsupported type. Only accept paddle.nn.Layer or function."
)
# patch paddle.nn.Layer
class BlockFn(type(call_func)):
def __call__(self, *args, **kwargs):
with ipu_shard_guard(index=index, stage=stage):
return super().__call__(*args, **kwargs)
BlockFn.__name__ = type(call_func).__name__
call_func.__class__ = BlockFn
return call_func
def require_version(min_version, max_version=None):
"""
Check if the installed version of PaddlePaddle is in [min_version, max_version],
if the installed version is lower than ``min_version`` or higher than ``max_version``,
an exception will be thrown, NO returns if the installed version is satisfied.
Args:
min_version (str): the minimum version required (like '1.4.0').
max_version (str, optional): the max version required (like '1.6.0'), default is None,
meaning any version equal or higher than ``min_version`` is acceptable.
Returns:
None.
Raises:
TypeError: if the type of ``min_version`` is not str.
TypeError: if the type of ``max_version`` is not str or type(None).
ValueError: if the value of ``min_version`` is not in version format.
ValueError: if the value of ``max_version`` is not in version format or None.
Exception: if the installed version is lower than ``min_version`` or higher than ``max_version``.
Examples:
.. code-block:: python
>>> import paddle
>>> # any version >= 0.1.0 is acceptable.
>>> paddle.utils.require_version('0.1.0')
>>> # if 0.1.0 <= version <= 10.0.0, it is acceptable.
>>> paddle.utils.require_version(min_version='0.1.0', max_version='10.0.0')
"""
if not isinstance(min_version, str):
raise TypeError(
"The type of 'min_version' in require_version must be str, but received %s."
% (type(min_version))
)
if not isinstance(max_version, (str, type(None))):
raise TypeError(
"The type of 'max_version' in require_version must be str or type(None), but received %s."
% (type(max_version))
)
check_format = re.match(r'\d+(\.\d+){0,3}', min_version)
if check_format is None or check_format.group() != min_version:
raise ValueError(
"The value of 'min_version' in require_version must be in format '\\d+(\\.\\d+){0,3}', "
"like '1.5.2.0', but received %s" % min_version
)
if max_version is not None:
check_format = re.match(r'\d+(\.\d+){0,3}', max_version)
if check_format is None or check_format.group() != max_version:
raise ValueError(
"The value of 'max_version' in require_version must be in format '\\d+(\\.\\d+){0,3}', "
"like '1.5.2.0', but received %s" % max_version
)
version_installed = [
paddle_version.major,
paddle_version.minor,
paddle_version.patch,
paddle_version.rc,
]
zero_version = ['0', '0', '0', '0']
def version_cmp(ver_a, ver_b):
for i in range(len(ver_a)):
if int(ver_a[i]) > int(ver_b[i]):
return 1
elif int(ver_a[i]) < int(ver_b[i]):
return -1
return 0
if version_cmp(version_installed, zero_version) == 0:
if max_version is not None:
warnings.warn(
"PaddlePaddle version in [{}, {}] required, but {} installed. "
"Maybe you are using a develop version, "
"please make sure the version is good with your code.".format(
min_version, max_version, paddle_version.full_version
)
)
else:
warnings.warn(
"PaddlePaddle version {} or higher is required, but {} installed, "
"Maybe you are using a develop version, "
"please make sure the version is good with your code.".format(
min_version, paddle_version.full_version
)
)
return
min_version_split = min_version.split('.')
min_version_to_check = (
min_version_split + zero_version[len(min_version_split) :]
)
if max_version is not None:
max_version_split = max_version.split('.')
max_version_to_check = (
max_version_split + zero_version[len(max_version_split) :]
)
if (
version_cmp(version_installed, max_version_to_check) > 0
or version_cmp(version_installed, min_version_to_check) < 0
):
raise Exception(
"VersionError: PaddlePaddle version in [{}, {}] required, but {} installed.".format(
min_version, max_version, paddle_version.full_version
)
)
else:
if version_cmp(version_installed, min_version_to_check) < 0:
raise Exception(
"VersionError: PaddlePaddle version {} or higher is required, but {} installed, "
"please upgrade your PaddlePaddle to {} or other higher version.".format(
min_version, paddle_version.full_version, min_version
)
)
def _dygraph_not_support_(func):
def __impl__(*args, **kwargs):
assert not in_dygraph_mode(), (
"We don't support %s in dynamic graph mode" % func.__name__
)
return func(*args, **kwargs)
return __impl__
def _dygraph_only_(func):
def __impl__(*args, **kwargs):
assert in_dygraph_mode(), (
"We only support '%s()' in dynamic graph mode, please call 'paddle.disable_static()' to enter dynamic graph mode."
% func.__name__
)
return func(*args, **kwargs)
return __impl__
def _non_static_only_(func):
def __impl__(*args, **kwargs):
from .dygraph.base import in_to_static_mode
assert in_dygraph_mode() or in_to_static_mode(), (
"We only support '%s()' in dynamic graph mode, please call 'paddle.disable_static()' to enter dynamic graph mode."
% func.__name__
)
return func(*args, **kwargs)
return __impl__
def _static_only_(func):
def __impl__(*args, **kwargs):
assert not in_dygraph_mode(), (
"In PaddlePaddle 2.x, we turn on dynamic graph mode by default, and '%s()' is only supported in static graph mode. So if you want to use this api, please call 'paddle.enable_static()' before this api to enter static graph mode."
% func.__name__
)
return func(*args, **kwargs)
return __impl__
def _set_pipeline_stage(stage):
global _current_pipeline_stage
_current_pipeline_stage = stage
# NOTE(zhiqiu): This decorator is used for the APIs of Variable which is only
# used to make Variable and Tensor has same interfaces, like numpy. Since Tensor is not exposed in our
# official docments, logically, we want to keep Tensor and logically consistent. While, actually,
# in our implementation, there some APIs not supported, like numpy, because Variable contains the desc.
# So, those APIs are listed under class Variable to generate docs only.
# TODO(zhiqiu): We should make Tensor consistent with Variable in future, for example, by inheritting
# same base class.
def _fake_interface_only_(func):
def __impl__(*args, **kwargs):
raise AssertionError(
f"'{func.__name__}' only can be called by `paddle.Tensor` in dynamic graph mode. Suggestions:\n"
" 1. If you are in static graph mode, you can switch to dynamic graph mode by turning off `paddle.enable_static()` or calling `paddle.disable_static()`.\n"
" 2. If you are using `@paddle.jit.to_static`, you can call `paddle.jit.enable_to_static(False)`. "
f"If you have to translate dynamic graph to static graph, please use other API to replace '{func.__name__}'."
)
return __impl__
# NOTE(chenweihang): There is argument name typo (stat_dict, correct name is state_dict)
# in base api Layer.set_dict, Optimizer.load, in order to correct the argument without
# introducing compatibility issues, add this decorator
# NOTE(chenweihang): not using `wrap_decorator` here is because `wrap_decorator` will
# move kwargs to args, which doesn't work in this decorate case
def deprecate_stat_dict(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if 'stat_dict' in kwargs:
warnings.warn(
"The argument `stat_dict` has deprecated, please change it to `state_dict`.",
DeprecationWarning,
)
kwargs['state_dict'] = kwargs['stat_dict']
kwargs.pop('stat_dict')
return func(*args, **kwargs)
return wrapper
dygraph_not_support = wrap_decorator(_dygraph_not_support_)
dygraph_only = wrap_decorator(_dygraph_only_)
static_only = wrap_decorator(_static_only_)
fake_interface_only = wrap_decorator(_fake_interface_only_)
non_static_only = wrap_decorator(_non_static_only_)
def _dygraph_tracer():
return global_var._dygraph_tracer_
def _current_expected_place_():
global _global_expected_place_
if _global_expected_place_ is None:
if core.is_compiled_with_cuda():
try:
device_count = core.get_cuda_device_count()
except Exception as e:
device_count = 0
if device_count > 0:
_global_expected_place_ = core.CUDAPlace(_cuda_ids()[0])
else:
warnings.warn(
"You are using GPU version Paddle, but your CUDA device is not set properly. CPU device will be used by default."
)
_global_expected_place_ = core.CPUPlace()
elif core.is_compiled_with_xpu():
try:
device_count = core.get_xpu_device_count()
except Exception as e:
device_count = 0
if device_count > 0:
_global_expected_place_ = core.XPUPlace(_xpu_ids()[0])
else:
warnings.warn(
"You are using XPU version Paddle, but your XPU device is not set properly. CPU device will be used by default."
)
_global_expected_place_ = core.CPUPlace()
elif len(core.get_all_custom_device_type()) > 0:
dev_type = core.get_all_custom_device_type()[0]
try:
device_count = core.get_custom_device_count(dev_type)
except Exception as e:
device_count = 0
if device_count > 0:
_global_expected_place_ = core.CustomPlace(
dev_type, _custom_device_ids(dev_type)[0]
)
else:
warnings.warn(
"You are using CUSTOM_DEVICE version Paddle, but your custom device is not set properly. CPU device will be used by default."
)
_global_expected_place_ = core.CPUPlace()
else:
_global_expected_place_ = core.CPUPlace()
return _global_expected_place_
def _current_expected_place():
if in_pir_mode():
return core.Place()
return _current_expected_place_()
def _set_dygraph_tracer_expected_place(place):
if global_var._dygraph_tracer_ is not None:
global_var._dygraph_tracer_._expected_place = place
def _set_expected_place(place):
global _global_expected_place_
_global_expected_place_ = place
_set_dygraph_tracer_expected_place(place)
def _cpu_num():
if "CPU_NUM" not in os.environ.keys():
if multiprocessing.cpu_count() > 1:
sys.stderr.write(
'!!! The CPU_NUM is not specified, you should set CPU_NUM in the environment variable list.\n'
'CPU_NUM indicates that how many CPUPlace are used in the current task.\n'
'And if this parameter are set as N (equal to the number of physical CPU core) the program may be faster.\n\n'
'export CPU_NUM={} # for example, set CPU_NUM as number of physical CPU core which is {}.\n\n'
'!!! The default number of CPU_NUM=1.\n'.format(
multiprocessing.cpu_count(), multiprocessing.cpu_count()
)
)
os.environ['CPU_NUM'] = str(1)
cpu_num = os.environ.get('CPU_NUM')
return int(cpu_num)
def _cuda_ids():
gpus_env = os.getenv("FLAGS_selected_gpus")
if gpus_env:
device_ids = [int(s) for s in gpus_env.split(",")]
else:
device_ids = range(core.get_cuda_device_count())
return device_ids
def _xpu_ids():
xpus_env = os.getenv("FLAGS_selected_xpus")
if xpus_env:
device_ids = [int(s) for s in xpus_env.split(",")]
else:
device_ids = range(core.get_xpu_device_count())
return device_ids
def _custom_device_ids(device_type):
custom_devices_env = os.getenv("FLAGS_selected_" + device_type + "s")
if custom_devices_env:
device_ids = [int(s) for s in custom_devices_env.split(",")]
else:
device_ids = range(core.get_custom_device_count(device_type))
return device_ids
def is_compiled_with_xpu():
"""
Whether this whl package can be used to run the model on XPU.
Returns (bool): support xpu or not.
Examples:
.. code-block:: python
>>> import paddle.base as base
>>> support_xpu = base.is_compiled_with_xpu()
"""
return core.is_compiled_with_xpu()
def disable_signal_handler():
"""
Reset signal handler registered by Paddle.
Paddle installs signal handlers at C++ level to log debug information upon failing.
However, conflicts can happen if another python module is making use of such signal.
Such being the case, one may disblae paddle signal handler via this interface.
Known frameworks that require disabling signal handler includes:
1. TVM
2. ADLIK
Make sure you called paddle.disable_signal_handler() before using above mentioned frameworks.
Returns:
None
Examples:
.. code-block:: python
>>> import paddle
>>> paddle.disable_signal_handler()
"""
core.disable_signal_handler()
def is_compiled_with_cinn():
"""
Whether this whl package can be used to run the model on CINN.
Returns:
Bool: `True` if CINN is currently available, otherwise `False`.
Examples:
.. code-block:: python
>>> import paddle
>>> support_cinn = paddle.device.is_compiled_with_cinn()
"""
return core.is_compiled_with_cinn()
def is_compiled_with_cuda():
"""
Whether this whl package can be used to run the model on GPU.
Returns:
Bool: `True` if CUDA is currently available, otherwise `False`.
Examples:
.. code-block:: python
>>> import paddle
>>> support_gpu = paddle.device.is_compiled_with_cuda()
"""
return core.is_compiled_with_cuda()
def is_compiled_with_distribute():
"""
Whether this whl package can be used to run the model with distribute.
Returns:
Bool: `True` if distribute is currently available, otherwise `False`.
Examples:
.. code-block:: python
>>> import paddle
>>> support_distribute = paddle.device.is_compiled_with_distribute()
"""
return core.is_compiled_with_distribute()
def is_compiled_with_rocm():
"""
Whether this whl package can be used to run the model on AMD or Hygon GPU(ROCm).
Returns:
Bool: `True` if ROCm is currently available, otherwise `False`.
Examples:
.. code-block:: python
>>> import paddle
>>> support_gpu = paddle.device.is_compiled_with_rocm()
"""
return core.is_compiled_with_rocm()
def cuda_places(device_ids=None):
"""
Note:
For multi-card tasks, please use `FLAGS_selected_gpus` environment variable to set the visible GPU device.
The next version will fix the problem with `CUDA_VISIBLE_DEVICES` environment variable.
This function creates a list of :code:`paddle.CUDAPlace` objects.
If :code:`device_ids` is None, environment variable of
:code:`FLAGS_selected_gpus` would be checked first. For example, if
:code:`FLAGS_selected_gpus=0,1,2`, the returned list would
be [paddle.CUDAPlace(0), paddle.CUDAPlace(1), paddle.CUDAPlace(2)].
If :code:`FLAGS_selected_gpus` is not set, all visible
gpu places would be returned according to the :code:`CUDA_VISIBLE_DEVICES` environment variable.
If :code:`device_ids` is not None, it should be the device
ids of GPUs. For example, if :code:`device_ids=[0,1,2]`,
the returned list would be
[paddle.CUDAPlace(0), paddle.CUDAPlace(1), paddle.CUDAPlace(2)].
Parameters:
device_ids (list|tuple, optional): A list/tuple of int of GPU device ids.
Returns:
list of paddle.CUDAPlace: Created GPU place list.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:GPU)
>>> import paddle
>>> import paddle.static as static
>>> paddle.device.set_device('gpu')
>>> paddle.enable_static()
>>> cuda_places = static.cuda_places()
"""
assert core.is_compiled_with_cuda(), "Not compiled with CUDA"
if device_ids is None:
device_ids = _cuda_ids()
elif not isinstance(device_ids, (list, tuple)):
device_ids = [device_ids]
return [core.CUDAPlace(dev_id) for dev_id in device_ids]
def xpu_places(device_ids=None):
"""
**Note**:
For multi-card tasks, please use `FLAGS_selected_xpus` environment variable to set the visible XPU device.
This function creates a list of :code:`paddle.XPUPlace` objects.
If :code:`device_ids` is None, environment variable of
:code:`FLAGS_selected_xpus` would be checked first. For example, if
:code:`FLAGS_selected_xpus=0,1,2`, the returned list would
be [paddle.XPUPlace(0), paddle.XPUPlace(1), paddle.XPUPlace(2)].
If :code:`FLAGS_selected_xpus` is not set, all visible
xpu places would be returned.
If :code:`device_ids` is not None, it should be the device
ids of XPUs. For example, if :code:`device_ids=[0,1,2]`,
the returned list would be
[paddle.XPUPlace(0), paddle.XPUPlace(1), paddle.XPUPlace(2)].
Parameters:
device_ids (list or tuple of int, optional): list of XPU device ids.
Returns:
list of paddle.XPUPlace: Created XPU place list.
Examples:
.. code-block:: python
>>> # doctest: +REQUIRES(env:XPU)
>>> import paddle
>>> import paddle.static as static
>>> paddle.device.set_device('xpu')
>>> paddle.enable_static()
>>> xpu_places = static.xpu_places()
"""
assert core.is_compiled_with_xpu(), "Not compiled with XPU"
if device_ids is None:
device_ids = _xpu_ids()
elif not isinstance(device_ids, (list, tuple)):
device_ids = [device_ids]
return [core.XPUPlace(dev_id) for dev_id in device_ids]
def cpu_places(device_count=None):
"""
This function creates a list of :code:`paddle.CPUPlace` objects, and returns the created list.
If :code:`device_count` is None, the device count would
be determined by environment variable :code:`CPU_NUM`.
If :code:`CPU_NUM` is not set, the default value is 1,
i.e. CPU_NUM=1.
:code:`CPU_NUM` indicates the number of devices used in the current task.
The running of the program can be accelerated if :code:`CPU_NUM` is the same as the number of physical cores.
Parameters:
device_count (int, optional): device number. Default: None.
Returns:
list of paddle.CPUPlace: Created list of CPU places.