debugging.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import contextlib
  15. import random
  16. from enum import Enum
  17. import numpy as np
  18. import paddle
  19. from paddle import _C_ops
  20. from paddle.base import core
  21. from ..framework import LayerHelper, in_dynamic_or_pir_mode
  22. __all__ = [
  23. "DebugMode",
  24. "TensorCheckerConfig",
  25. "check_numerics",
  26. "enable_operator_stats_collection",
  27. "disable_operator_stats_collection",
  28. "collect_operator_stats",
  29. "enable_tensor_checker",
  30. "disable_tensor_checker",
  31. "compare_accuracy",
  32. "check_layer_numerics",
  33. ]
  34. class DebugMode(Enum):
  35. """
  36. The DebugMode is a feature that helps to present the state of the TensorCheckerConfig. Each DebugMode has a specific meaning, which is explained below:
  37. - DebugMode.CHECK_NAN_INF_AND_ABORT: This mode prints or saves information about Tensors that contain NaN/Inf and interrupts the program.
  38. - DebugMode.CHECK_NAN_INF: This mode prints or saves critical information about Tensors that contain NaN/Inf but allows the program to continue running.
  39. - DebugMode.CHECK_ALL_FOR_OVERFLOW: This mode checks the output of the FP32 operator and prints or saves information about key Tensors that exceed the FP16 representation range, such as overflow or underflow.
  40. - DebugMode.CHECK_ALL: This mode prints or saves output Tensor key information for all operators.
  41. """
  42. CHECK_NAN_INF_AND_ABORT = 0
  43. CHECK_NAN_INF = 1
  44. CHECK_ALL_FOR_OVERFLOW = 2
  45. CHECK_ALL = 3
  46. # CHECK_ALL_AND_ABORT = 4
  47. # DUMP_ALL = 5
  48. def check_layer_numerics(func):
  49. """
  50. This decorator is used to check the numerical values of the layer's input and output data.
  51. Args:
  52. func (callable): The function to be decorated.
  53. Returns:
  54. callable: The decorated function.
  55. Raises:
  56. None.
  57. Example:
  58. .. code-block:: python
  59. >>> import paddle
  60. >>> class MyLayer(paddle.nn.Layer):
  61. ... def __init__(self, dtype):
  62. ... super().__init__()
  63. ... self._w = self.create_parameter([2, 3], dtype=dtype)
  64. ... self._b = self.create_parameter([2, 3], dtype=dtype)
  65. ... @paddle.amp.debugging.check_layer_numerics
  66. ... def forward(self, x):
  67. ... # return 1/x * self._w + self._b open it you will see the error log
  68. ... return x @ self._w + self._b
  69. ...
  70. >>> dtype = 'float32'
  71. >>> x = paddle.rand([10, 2, 2], dtype=dtype)
  72. >>> model = MyLayer(dtype)
  73. >>> x[0] = float(0)
  74. >>> loss = model(x)
  75. >>> adam = paddle.optimizer.Adam(parameters=model.parameters())
  76. >>> loss.backward()
  77. >>> adam.step()
  78. >>> # error log
  79. >>> # [PRECISION] [ERROR] in [device=gpu:0, op=divide, tensor=, dtype=fp32], numel=40, num_nan=0, num_inf=4, num_zero=0, max=inf, min=1.048930e+00, mean=inf
  80. >>> # Traceback (most recent call last):
  81. >>> # File "tmp.py", line 16, in <module>
  82. >>> # loss = model(x)
  83. >>> # File "/paddle/nn/layer/layers.py", line 1254, in __call__
  84. >>> # return self.forward(*inputs, **kwargs)
  85. >>> # File "/paddle/amp/debugging.py", line 116, in wrapper
  86. >>> # out_data = func(self, *modified_args, **kwargs)
  87. >>> # File "test.py", line 10, in forward
  88. >>> # return 1/x * self._w+ self._b
  89. >>> # RuntimeError: (PreconditionNotMet) There are NAN or INF (num_nan=0, num_inf=4, num_zero=0) in [device=gpu:0, op=divide, tensor=, dtype=fp32].
  90. """
  91. def wrapper(self, *args, **kwargs):
  92. if args:
  93. # Set temp data and temp.gradient = False
  94. start_data = args[0]
  95. if not isinstance(start_data, paddle.Tensor):
  96. raise RuntimeError("First input of this layer must be tensor.")
  97. start_data.stop_gradient = False
  98. modified_args = list(args) # Convert args to a mutable list
  99. # Set FLAGS_check_nan_inf = 1
  100. modified_args[0] = _C_ops.enable_check_model_nan_inf(start_data, 1)
  101. # Call the forward function
  102. out_data = func(self, *modified_args, **kwargs)
  103. # Set FLAGS_check_nan_inf = 0
  104. out = _C_ops.disable_check_model_nan_inf(out_data, 0)
  105. return out
  106. else:
  107. raise RuntimeError("No elements found in args.")
  108. out = func(self, *args, **kwargs)
  109. return out
  110. return wrapper
  111. def set_checked_op_list(checked_op_list):
  112. # check checked_op_list
  113. if checked_op_list is not None:
  114. if isinstance(checked_op_list, (list, tuple)):
  115. check_op_list = ",".join(value for value in checked_op_list)
  116. paddle.base.core.set_checked_op_list(check_op_list)
  117. else:
  118. raise ValueError("checked_op_list must be list or tuple")
  119. def set_skipped_op_list(skipped_op_list):
  120. # check skipped_op_list
  121. if skipped_op_list is not None:
  122. if isinstance(skipped_op_list, (list, tuple)):
  123. skip_op_list = ",".join(value for value in skipped_op_list)
  124. paddle.base.core.set_skipped_op_list(skip_op_list)
  125. else:
  126. raise ValueError("skipped_op_list must be list or tuple")
  127. class TensorCheckerConfig:
  128. """
  129. The purpose of this class is to collect the configuration for checking NaN and Inf values in the tensors of a module or operator. It takes the following arguments:
  130. Args:
  131. enable(bool): Indicating whether to enable the detection of NaN and Inf values in tensors. The default value is False, which means that these tools will not be used.
  132. debug_mode(DebugMode, optional): A parameter that determines the type of debugging to be used. Default is DebugMode.CHECK_NAN_INF_AND_ABORT.
  133. output_dir(string, optional): The path to store collected data. If this parameter is set to None, the data will be printed to the terminal. Default is None.
  134. checked_op_list(list|tuple, optional): Specifies a list of operators that need to be checked during program execution, for example, checked_op_list=['elementwise_add', 'conv2d'], indicating that the output results of elementwise_add and conv2d should be checked for nan/inf during program execution. Default is None.
  135. skipped_op_list(list|tuple, optional): Specifies a list of operators that do not need to be checked during program execution, for example, skipped_op_list=['elementwise_add', 'conv2d'], indicating that the output results of elementwise_add and conv2d should not be checked for nan/inf during program execution. None is None.
  136. debug_step(list|tuple, optional): A list or tuple used primarily for nan/inf checking during model training. For example, debug_step=[1,5] indicates that nan/inf checking should only be performed on model training iterations 1 to 5. Default is None.
  137. stack_height_limit(int, optional): An integer value specifying the maximum depth of the call stack. This feature supports printing the call stack at the error location. Currently, only enabling or disabling call stack printing is supported. If you want to print the corresponding C++ call stack when NaN is detected in GPU Kernel, set stack_height_limit to 1, otherwise set it to 0. Default is 1.
  138. Examples:
  139. .. code-block:: python
  140. >>> import paddle
  141. >>> checker_config = paddle.amp.debugging.TensorCheckerConfig(enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF)
  142. >>> paddle.amp.debugging.enable_tensor_checker(checker_config)
  143. >>> x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False)
  144. >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32')
  145. >>> res = paddle.pow(x, y)
  146. >>> paddle.autograd.backward(res, retain_graph=True)
  147. >>> paddle.amp.debugging.disable_tensor_checker()
  148. >>> # [PRECISION] [ERROR] in [device=cpu, op=elementwise_pow_grad, tensor=, dtype=fp32], numel=3, num_nan=1, num_inf=0, num_zero=0, max=2.886751e-01, min=2.000000e-01, mean=-nan
  149. >>> # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1
  150. >>> # Traceback (most recent call last):
  151. >>> # res = paddle.pow(x, y)
  152. >>> # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow
  153. >>> # return _C_ops.elementwise_pow(x, y)
  154. """
  155. # For module debugging
  156. current_step_id = 0
  157. def __init__(
  158. self,
  159. enable,
  160. debug_mode=DebugMode.CHECK_NAN_INF_AND_ABORT,
  161. output_dir=None,
  162. checked_op_list=None,
  163. skipped_op_list=None,
  164. debug_step=None,
  165. stack_height_limit=1,
  166. ):
  167. self.enable = enable
  168. self.debug_mode = debug_mode
  169. self.output_dir = output_dir
  170. self.checked_op_list = checked_op_list
  171. self.skipped_op_list = skipped_op_list
  172. self.debug_step = debug_step
  173. self.stack_height_limit = stack_height_limit
  174. self.start_step = None
  175. self.end_step = None
  176. self.seed = 123
  177. self.initial_seed = 123
  178. # check debug_step
  179. if debug_step is not None:
  180. if isinstance(debug_step, (tuple, list)):
  181. assert (
  182. len(self.debug_step) == 2
  183. and self.debug_step[1] > self.debug_step[0]
  184. )
  185. self.start_step, self.end_step = self.debug_step
  186. self.start_step = max(self.start_step, 0)
  187. else:
  188. raise ValueError("debug_step must be list or tuple")
  189. if core.is_compiled_with_cuda():
  190. for i in range(core.get_cuda_device_count()):
  191. self.initial_seed = core.default_cuda_generator(
  192. i
  193. ).initial_seed()
  194. elif core.is_compiled_with_xpu():
  195. for i in range(core.get_xpu_device_count()):
  196. self.initial_seed = core.default_xpu_generator(i).initial_seed()
  197. self.initial_seed = core.default_cpu_generator().initial_seed()
  198. # check debug_mode
  199. if self.debug_mode.name not in DebugMode.__members__:
  200. raise ValueError(
  201. "debug_mode in DebugMode",
  202. self.debug_mode,
  203. DebugMode.__members__,
  204. )
  205. set_checked_op_list(self.checked_op_list)
  206. set_skipped_op_list(self.skipped_op_list)
  207. if self.enable:
  208. self._set_seed(self.enable)
  209. def _set_seed(self, flag):
  210. if self.initial_seed != self.seed:
  211. self.seed = self.initial_seed
  212. if self.seed > np.iinfo(np.uint32).max or self.seed < 0:
  213. print("[Warning: Seed must be between 0 and 2**32 - 1")
  214. self.seed = 123
  215. # get random seed
  216. paddle.seed(self.seed)
  217. np.random.seed(self.seed)
  218. random.seed(self.seed)
  219. # info
  220. print("AMP Debugging TensorCheckerConfig: seed ", self.seed)
  221. # set cudnn and cpu
  222. if core.is_compiled_with_cuda():
  223. paddle.set_flags({"FLAGS_cudnn_deterministic": flag})
  224. print(
  225. "AMP Debugging TensorCheckerConfig: FLAGS_cudnn_deterministic is ",
  226. flag,
  227. )
  228. paddle.set_flags({"FLAGS_cpu_deterministic": flag})
  229. print(
  230. "AMP Debugging TensorCheckerConfig: FLAGS_cpu_deterministic is ",
  231. flag,
  232. )
  233. def _set_env(self, check_flag):
  234. paddle.set_flags({"FLAGS_check_nan_inf": check_flag})
  235. if check_flag:
  236. # set debug level
  237. paddle.set_flags(
  238. {"FLAGS_check_nan_inf_level": self.debug_mode.value}
  239. )
  240. # set output_dir
  241. if self.output_dir is not None:
  242. paddle.base.core.set_nan_inf_debug_path(self.output_dir)
  243. # set stack_height_limit
  244. if isinstance(self.stack_height_limit, (int)):
  245. paddle.base.core.set_nan_inf_stack_limit(
  246. self.stack_height_limit
  247. )
  248. else:
  249. raise ValueError("stack_height_limit must be int")
  250. def update_and_check_step_id(self):
  251. if self.enable:
  252. if self.start_step is not None and self.end_step is not None:
  253. if (
  254. self.start_step > TensorCheckerConfig.current_step_id
  255. or TensorCheckerConfig.current_step_id >= self.end_step
  256. ):
  257. return False
  258. else:
  259. TensorCheckerConfig.current_step_id += 1
  260. return True
  261. return False
  262. def start_check_nan_inf(self):
  263. if self.enable:
  264. self._set_env(self.enable)
  265. def stop_check_nan_inf(self):
  266. self._set_env(False)
  267. def check_numerics(
  268. tensor, op_type, var_name, debug_mode=DebugMode.CHECK_NAN_INF_AND_ABORT
  269. ):
  270. """
  271. This function is used to debugging a tensor, finding the number of NaNs, Infs and zeros in the tensor.
  272. Args:
  273. tensor(Tensor): The target tensor to check.
  274. op_type(str): The OP or API name which produce the target tensor.
  275. var_name(str): The name of target tensor.
  276. debug_mode(paddle.amp.debugging.DebugMode, optional): The mode of debugging to be used. Default is DebugMode.CHECK_NAN_INF_AND_ABORT.
  277. Returns:
  278. (Tensor, Tensor): A tuple of tensors containing
  279. - **stats** (Tensor): Returns the number of NaNs, Infs and zeros of input tensor. The shape is [3] and dtype is int64.
  280. - **values** (Tensor): Returns the maximum, minimum and mean value of input tensor. The shape is [3] and dtype is float.
  281. Examples:
  282. .. code-block:: python
  283. >>> import paddle
  284. >>> checker_config = paddle.amp.debugging.TensorCheckerConfig(
  285. ... enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF)
  286. >>> x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32')
  287. >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32')
  288. >>> res = paddle.pow(x, y)
  289. >>> paddle.amp.debugging.check_numerics(res, "pow", "res")
  290. """
  291. stack_height_limit = -1
  292. output_dir = ""
  293. if in_dynamic_or_pir_mode():
  294. return _C_ops.check_numerics(
  295. tensor,
  296. op_type,
  297. var_name,
  298. debug_mode.value,
  299. stack_height_limit,
  300. output_dir,
  301. )
  302. helper = LayerHelper("check_numerics", **locals())
  303. stats = helper.create_variable_for_type_inference(dtype="int64")
  304. values = helper.create_variable_for_type_inference(dtype="float")
  305. helper.append_op(
  306. type='check_numerics',
  307. inputs={
  308. 'tensor': tensor,
  309. },
  310. attrs={
  311. 'op_type': op_type,
  312. 'var_name': var_name,
  313. 'check_nan_inf_level': debug_mode.value,
  314. 'stack_height_limit': stack_height_limit,
  315. 'output_dir': output_dir,
  316. },
  317. outputs={'stats': [stats], 'values': [values]},
  318. )
  319. return stats, values
  320. def _get_operator_stats_flag():
  321. flags = paddle.get_flags(["FLAGS_low_precision_op_list"])
  322. return flags["FLAGS_low_precision_op_list"]
  323. def _print_operator_stats(op_count_dict):
  324. """
  325. Parse and print the stats of operators, mainly including the calls of
  326. dtypes such as different fp32, fp16, bf16 and others.
  327. Args:
  328. op_count_dict(dict): a dict to record the number of calls for different
  329. operator and dtype. An example is
  330. {'conv2d': '1,0,0,0', 'elementwise_add': '1,0,0,0'} or
  331. {'conv2d': [1, 0, 0, 0], 'elementwise_add': [1, 0, 0, 0]}.
  332. """
  333. print("<{:-^120}>".format(" op list "))
  334. total_ops = 0
  335. print(
  336. "<{:-^40}".format(" Op Name "),
  337. "|",
  338. "{:-^17}".format(" FP16 Calls "),
  339. "|",
  340. "{:-^17}".format(" BF16 Calls "),
  341. "|",
  342. "{:-^17}".format(" FP32 Calls"),
  343. "|",
  344. "{:-^17}>".format(" Other Calls "),
  345. )
  346. if op_count_dict is not None and isinstance(op_count_dict, dict):
  347. for op_type in sorted(op_count_dict):
  348. # fp16, bf16, fp32, other
  349. value = op_count_dict[op_type]
  350. if isinstance(value, list):
  351. called = value
  352. elif isinstance(value, str):
  353. called = value.split(",")
  354. else:
  355. raise ValueError(
  356. f"Input {value} is expected to be a list of str, but received {type(value)}."
  357. )
  358. print(
  359. " %-40s| %-17s| %-17s| %-17s| %-17s"
  360. % (op_type, called[0], called[1], called[2], called[3])
  361. )
  362. total_ops += 1
  363. print("<{:-^120}>\n".format(" op count: " + str(total_ops) + " "))
  364. def enable_operator_stats_collection():
  365. """
  366. Enable to collect the number of operators for different data types.
  367. The statistical data are categorized according to four data types, namely
  368. float32, float16, bfloat16 and others. This function is used in pair with
  369. the corresponding disable function.
  370. Examples:
  371. .. code-block:: python
  372. >>> # doctest: +REQUIRES(env:GPU)
  373. >>> import paddle
  374. >>> paddle.device.set_device('gpu')
  375. >>> conv = paddle.nn.Conv2D(3, 2, 3)
  376. >>> x = paddle.rand([10, 3, 32, 32])
  377. >>> paddle.amp.debugging.enable_operator_stats_collection()
  378. >>> # AMP list including conv2d, elementwise_add, reshape2, cast (transfer_dtype)
  379. >>> with paddle.amp.auto_cast(enable=True, level='O2'):
  380. ... out = conv(x)
  381. >>> # Print to the standard output.
  382. >>> paddle.amp.debugging.disable_operator_stats_collection()
  383. >>> # <------------------------------------------------------- op list -------------------------------------------------------->
  384. >>> # <--------------- Op Name ---------------- | -- FP16 Calls --- | -- BF16 Calls --- | --- FP32 Calls--- | -- Other Calls -->
  385. >>> # conv2d | 1 | 0 | 0 | 0
  386. >>> # elementwise_add | 0 | 0 | 1 | 0
  387. >>> # reshape2 | 0 | 0 | 1 | 0
  388. >>> # transfer_dtype | 1 | 0 | 2 | 0
  389. >>> # <----------------------------------------------------- op count: 4 ------------------------------------------------------>
  390. """
  391. # Clear the previous stats.
  392. paddle.base.core.clear_low_precision_op_list()
  393. paddle.set_flags({'FLAGS_low_precision_op_list': 1})
  394. def disable_operator_stats_collection():
  395. """
  396. Disable the collection the number of operators for different data types.
  397. This function is used in pair with the corresponding enable function.
  398. The statistical data are categorized according to four data types, namely
  399. float32, float16, bfloat16 and others, and will be printed after the
  400. function call.
  401. Examples:
  402. .. code-block:: python
  403. >>> import paddle
  404. >>> conv = paddle.nn.Conv2D(3, 2, 3)
  405. >>> x = paddle.rand([10, 3, 32, 32])
  406. >>> paddle.amp.debugging.enable_operator_stats_collection()
  407. >>> # AMP list including conv2d, elementwise_add, reshape2, cast (transfer_dtype)
  408. >>> with paddle.amp.auto_cast(enable=True, level='O2'):
  409. ... out = conv(x)
  410. >>> # Print to the standard output.
  411. >>> paddle.amp.debugging.disable_operator_stats_collection()
  412. >>> # <------------------------------------------------------- op list -------------------------------------------------------->
  413. >>> # <--------------- Op Name ---------------- | -- FP16 Calls --- | -- BF16 Calls --- | --- FP32 Calls--- | -- Other Calls -->
  414. >>> # conv2d | 1 | 0 | 0 | 0
  415. >>> # elementwise_add | 0 | 0 | 1 | 0
  416. >>> # reshape2 | 0 | 0 | 1 | 0
  417. >>> # transfer_dtype | 1 | 0 | 2 | 0
  418. >>> # <----------------------------------------------------- op count: 4 ------------------------------------------------------>
  419. """
  420. if not _get_operator_stats_flag():
  421. return
  422. op_count_dict = paddle.base.core.get_low_precision_op_list()
  423. _print_operator_stats(op_count_dict)
  424. paddle.set_flags({'FLAGS_low_precision_op_list': 0})
  425. @contextlib.contextmanager
  426. def collect_operator_stats():
  427. """
  428. The context switcher to enable to collect the number of operators for
  429. different data types. The statistical data are categorized according
  430. to four data types, namely float32, float16, bfloat16 and others, and
  431. will be printed when exiting the context.
  432. Examples:
  433. .. code-block:: python
  434. >>> import paddle
  435. >>> conv = paddle.nn.Conv2D(3, 2, 3)
  436. >>> x = paddle.rand([10, 3, 32, 32])
  437. >>> with paddle.amp.debugging.collect_operator_stats():
  438. ... # AMP list including conv2d, elementwise_add, reshape2, cast (transfer_dtype)
  439. ... with paddle.amp.auto_cast(enable=True, level='O2'):
  440. ... out = conv(x)
  441. >>> # Print to the standard output.
  442. >>> # <------------------------------------------------------- op list -------------------------------------------------------->
  443. >>> # <--------------- Op Name ---------------- | -- FP16 Calls --- | -- BF16 Calls --- | --- FP32 Calls--- | -- Other Calls -->
  444. >>> # conv2d | 1 | 0 | 0 | 0
  445. >>> # elementwise_add | 0 | 0 | 1 | 0
  446. >>> # reshape2 | 0 | 0 | 1 | 0
  447. >>> # transfer_dtype | 1 | 0 | 2 | 0
  448. >>> # <----------------------------------------------------- op count: 4 ------------------------------------------------------>
  449. """
  450. enable_operator_stats_collection()
  451. yield
  452. disable_operator_stats_collection()
  453. def compare_accuracy(
  454. dump_path,
  455. another_dump_path,
  456. output_filename,
  457. loss_scale=1,
  458. dump_all_tensors=False,
  459. ):
  460. r"""
  461. This is a precision comparison tool that can be used to compare log data of float16 and float32.
  462. Args:
  463. dump_path(str): The path of the running log, such as the log for execution using the float32 data type.
  464. another_dump_path(str): the path of another running log ,such as the log for execution using the float16 data type.
  465. output_filename(str): the excel file name of compare output.
  466. loss_scale(float, optional): the loss_scale during the training phase. Default is 1.
  467. dump_all_tensors(bool, optional): dump all tensor, It is currently not support. Default is False.
  468. Examples:
  469. .. code-block:: python
  470. >>> import paddle
  471. >>> from paddle.base import core
  472. >>> try:
  473. ... import xlsxwriter as xlw
  474. ... except ImportError:
  475. ... import subprocess
  476. ... subprocess.check_call(
  477. ... ['python', '-m', 'pip', 'install', 'xlsxwriter==3.0.9']
  478. ... )
  479. ... import xlsxwriter as xlw
  480. ... if core.is_compiled_with_cuda():
  481. ... paddle.set_flags(
  482. ... {"FLAGS_check_nan_inf": 1, "FLAGS_check_nan_inf_level": 3}
  483. ... )
  484. ... path = "workerlog_log_dir"
  485. ... paddle.base.core.set_nan_inf_debug_path(path)
  486. ... x = paddle.to_tensor(
  487. ... [2, 3, 4, 0], dtype="float32"
  488. ... )
  489. ... y = paddle.to_tensor(
  490. ... [1, 5, 2, 0], dtype="float32"
  491. ... )
  492. ... z1 = x + y
  493. ... out_excel = "compare_accuracy_out_excel.csv"
  494. ... paddle.amp.debugging.compare_accuracy(
  495. ... path, path, out_excel, loss_scale=1, dump_all_tensors=False
  496. ... )
  497. """
  498. assert dump_all_tensors is False, "It is currently not supported."
  499. paddle.amp.accuracy_compare.compare_accuracy(
  500. dump_path,
  501. another_dump_path,
  502. output_filename,
  503. loss_scale,
  504. dump_all_tensors=False,
  505. )
  506. def enable_tensor_checker(checker_config):
  507. """
  508. The enable_tensor_checker(checker_config) function enables model-level accuracy checking and is used in combination with disables_tensor_checker() to achieve model-level precision checking by checking the output Tensors of all operators within the specified range.
  509. Args:
  510. checker_config(TensorCheckerConfig): Checker_config is to collect the configuration for checking NaN and Inf values in the tensors of a module or operator.
  511. Note:
  512. If disable_tensor_checker() is called before backward(), the gradient operator will not be checked.
  513. If disable_tensor_checker() is called before optimizer.step(), the optimizer and other weight update related operators will not be checked.
  514. Examples:
  515. .. code-block:: python
  516. >>> import paddle
  517. >>> checker_config = paddle.amp.debugging.TensorCheckerConfig(enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF)
  518. >>> paddle.amp.debugging.enable_tensor_checker(checker_config)
  519. >>> x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False)
  520. >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32')
  521. >>> res = paddle.pow(x, y)
  522. >>> paddle.autograd.backward(res, retain_graph=True)
  523. >>> paddle.amp.debugging.disable_tensor_checker()
  524. >>> #[PRECISION] [ERROR] in [device=cpu, op=elementwise_pow_grad, tensor=, dtype=fp32], numel=3, num_nan=1, num_inf=0, num_zero=0, max=2.886751e-01, min=2.000000e-01, mean=-nan
  525. >>> # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1
  526. >>> # Traceback (most recent call last):
  527. >>> # File "tp.py", line 8, in <module>
  528. >>> # res = paddle.pow(x, y)
  529. >>> # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow
  530. >>> # return _C_ops.elementwise_pow(x, y)
  531. """
  532. if checker_config.update_and_check_step_id():
  533. checker_config.start_check_nan_inf()
  534. else:
  535. checker_config.stop_check_nan_inf()
  536. def disable_tensor_checker():
  537. """
  538. disable_tensor_checker() is used to disable accuracy checking, and is used together with enable_tensor_checker(config) to achieve model-level precision checking by checking the output Tensors of all operators within the specified range.
  539. Note:
  540. If disable_tensor_checker() is called before backward(), the gradient operator will not be checked;
  541. If disable_tensor_checker() is called before optimizer.step(), the optimizer and other weight update related operators will not be checked.
  542. Examples:
  543. .. code-block:: python
  544. >>> import paddle
  545. >>> checker_config = paddle.amp.debugging.TensorCheckerConfig(enable=True, debug_mode=paddle.amp.debugging.DebugMode.CHECK_NAN_INF)
  546. >>> paddle.amp.debugging.enable_tensor_checker(checker_config)
  547. >>> x = paddle.to_tensor([1, 0, 3], place=paddle.CPUPlace(), dtype='float32', stop_gradient=False)
  548. >>> y = paddle.to_tensor([0.2, 0, 0.5], place=paddle.CPUPlace(), dtype='float32')
  549. >>> res = paddle.pow(x, y)
  550. >>> paddle.autograd.backward(res, retain_graph=True)
  551. >>> paddle.amp.debugging.disable_tensor_checker()
  552. >>> # [PRECISION] [ERROR] in [device=cpu, op=elementwise_pow_grad, tensor=, dtype=fp32], numel=3, num_nan=1, num_inf=0, num_zero=0, max=2.886751e-01, min=2.000000e-01, mean=-nan
  553. >>> # when DebugMode.CHECK_NAN_INF_AND_ABORT and stack_height_limit = 1
  554. >>> # Traceback (most recent call last):
  555. >>> # res = paddle.pow(x, y)
  556. >>> # File "/usr/local/lib/python3.8/dist-packages/paddle/tensor/math.py", line 447, in pow
  557. >>> # return _C_ops.elementwise_pow(x, y)
  558. """
  559. paddle.set_flags({"FLAGS_check_nan_inf": 0})