From 2994f0a476b79e1af7e5afb8c664c3d6cb91d290 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 30 Sep 2022 18:29:45 +0800 Subject: [PATCH 01/10] remove long_type --- python/paddle/compat.py | 1 - .../fluid/tests/unittests/dist_transformer.py | 19 +++++++++---------- .../fluid/tests/unittests/test_compat.py | 1 - .../tests/unittests/test_lookup_table_op.py | 6 +++--- .../unittests/test_lookup_table_v2_op.py | 2 +- .../xpu/test_lookup_table_v2_op_xpu.py | 2 +- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/python/paddle/compat.py b/python/paddle/compat.py index c94132e43981f..9ea7a94c9c6e2 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -18,7 +18,6 @@ __all__ = [] int_type = int -long_type = int # str and bytes related functions diff --git a/python/paddle/fluid/tests/unittests/dist_transformer.py b/python/paddle/fluid/tests/unittests/dist_transformer.py index 7b514e4d57226..9821ee697a732 100644 --- a/python/paddle/fluid/tests/unittests/dist_transformer.py +++ b/python/paddle/fluid/tests/unittests/dist_transformer.py @@ -35,7 +35,6 @@ from paddle.fluid import core from test_dist_base import TestDistRunnerBase, runtime_main, RUN_STEP import paddle.compat as cpt -from paddle.compat import long_type import hashlib @@ -183,10 +182,10 @@ def merge_cfg_from_list(cfg_list, g_cfgs): input_descs = { # The actual data shape of src_word is: # [batch_size * max_src_len_in_batch, 1] - "src_word": [(batch_size, seq_len, long_type(1)), "int64", 2], + "src_word": [(batch_size, seq_len, 1), "int64", 2], # The actual data shape of src_pos is: # [batch_size * max_src_len_in_batch, 1] - "src_pos": [(batch_size, seq_len, long_type(1)), "int64"], + "src_pos": [(batch_size, seq_len, 1), "int64"], # This input is used to remove attention weights on paddings in the # encoder. # The actual data shape of src_slf_attn_bias is: @@ -195,11 +194,11 @@ def merge_cfg_from_list(cfg_list, g_cfgs): [(batch_size, ModelHyperParams.n_head, seq_len, seq_len), "float32"], # The actual data shape of trg_word is: # [batch_size * max_trg_len_in_batch, 1] - "trg_word": [(batch_size, seq_len, long_type(1)), "int64", + "trg_word": [(batch_size, seq_len, 1), "int64", 2], # lod_level is only used in fast decoder. # The actual data shape of trg_pos is: # [batch_size * max_trg_len_in_batch, 1] - "trg_pos": [(batch_size, seq_len, long_type(1)), "int64"], + "trg_pos": [(batch_size, seq_len, 1), "int64"], # This input is used to remove attention weights on paddings and # subsequent words in the decoder. # The actual data shape of trg_slf_attn_bias is: @@ -218,15 +217,15 @@ def merge_cfg_from_list(cfg_list, g_cfgs): "enc_output": [(batch_size, seq_len, ModelHyperParams.d_model), "float32"], # The actual data shape of label_word is: # [batch_size * max_trg_len_in_batch, 1] - "lbl_word": [(batch_size * seq_len, long_type(1)), "int64"], + "lbl_word": [(batch_size * seq_len, 1), "int64"], # This input is used to mask out the loss of padding tokens. # The actual data shape of label_weight is: # [batch_size * max_trg_len_in_batch, 1] - "lbl_weight": [(batch_size * seq_len, long_type(1)), "float32"], + "lbl_weight": [(batch_size * seq_len, 1), "float32"], # These inputs are used to change the shape tensor in beam-search decoder. - "trg_slf_attn_pre_softmax_shape_delta": [(long_type(2), ), "int32"], - "trg_slf_attn_post_softmax_shape_delta": [(long_type(4), ), "int32"], - "init_score": [(batch_size, long_type(1)), "float32"], + "trg_slf_attn_pre_softmax_shape_delta": [(2, ), "int32"], + "trg_slf_attn_post_softmax_shape_delta": [(4, ), "int32"], + "init_score": [(batch_size, 1), "float32"], } # Names of word embedding table which might be reused for weight sharing. diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index 7ff25fcfddd38..6077115f9454d 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -20,7 +20,6 @@ class TestCompatible(unittest.TestCase): def test_type(self): self.assertEqual(cpt.int_type, int) - self.assertEqual(cpt.long_type, int) def test_to_text(self): self.assertIsNone(cpt.to_text(None)) diff --git a/python/paddle/fluid/tests/unittests/test_lookup_table_op.py b/python/paddle/fluid/tests/unittests/test_lookup_table_op.py index 1470ae4df56c1..ce79317866bc8 100644 --- a/python/paddle/fluid/tests/unittests/test_lookup_table_op.py +++ b/python/paddle/fluid/tests/unittests/test_lookup_table_op.py @@ -82,7 +82,7 @@ def test_check_output(self): flatten_idx = ids.flatten() padding_idx = np.random.choice(flatten_idx, 1)[0] self.outputs['Out'][np.squeeze(ids == padding_idx)] = np.zeros(31) - self.attrs = {'padding_idx': cpt.long_type(padding_idx)} + self.attrs = {'padding_idx': padding_idx} self.check_output() @@ -250,7 +250,7 @@ def test_check_output(self): flatten_idx = ids.flatten() padding_idx = np.random.choice(flatten_idx, 1)[0] self.outputs['Out'][np.squeeze(ids == padding_idx)] = np.zeros(31) - self.attrs = {'padding_idx': cpt.long_type(padding_idx)} + self.attrs = {'padding_idx': padding_idx} self.check_output() def test_check_grad(self): @@ -380,7 +380,7 @@ def test_check_output(self): flatten_idx = ids.flatten() padding_idx = np.random.choice(flatten_idx, 1)[0] self.outputs['Out'][np.squeeze(ids == padding_idx)] = np.zeros(31) - self.attrs = {'padding_idx': cpt.long_type(padding_idx)} + self.attrs = {'padding_idx': padding_idx} self.check_output() diff --git a/python/paddle/fluid/tests/unittests/test_lookup_table_v2_op.py b/python/paddle/fluid/tests/unittests/test_lookup_table_v2_op.py index 5552d2cd68f5d..3b0a093426053 100644 --- a/python/paddle/fluid/tests/unittests/test_lookup_table_v2_op.py +++ b/python/paddle/fluid/tests/unittests/test_lookup_table_v2_op.py @@ -123,7 +123,7 @@ def test_check_output(self): flatten_idx = ids.flatten() padding_idx = np.random.choice(flatten_idx, 1)[0] self.outputs['Out'][np.squeeze(ids == padding_idx)] = np.zeros(31) - self.attrs = {'padding_idx': cpt.long_type(padding_idx)} + self.attrs = {'padding_idx': padding_idx} self.check_output() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_lookup_table_v2_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_lookup_table_v2_op_xpu.py index 77f09fc9864a6..6bb54c8f74edf 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_lookup_table_v2_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_lookup_table_v2_op_xpu.py @@ -95,7 +95,7 @@ def test_check_output(self): flatten_idx = ids.flatten() padding_idx = np.random.choice(flatten_idx, 1)[0] self.outputs['Out'][np.squeeze(ids == padding_idx)] = np.zeros(31) - self.attrs = {'padding_idx': cpt.long_type(padding_idx)} + self.attrs = {'padding_idx': padding_idx} self.check_output_with_place(place=paddle.XPUPlace(0)) From adc61f8cddcfdb3e22be5f0d73c21770e793a1d3 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Fri, 30 Sep 2022 22:01:42 +0800 Subject: [PATCH 02/10] int_type --- python/paddle/compat.py | 2 -- python/paddle/fluid/tests/unittests/test_compat.py | 3 --- 2 files changed, 5 deletions(-) diff --git a/python/paddle/compat.py b/python/paddle/compat.py index 9ea7a94c9c6e2..24d86103693d9 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -17,8 +17,6 @@ __all__ = [] -int_type = int - # str and bytes related functions def to_text(obj, encoding='utf-8', inplace=False): diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index 6077115f9454d..454add0f16b63 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -18,9 +18,6 @@ class TestCompatible(unittest.TestCase): - def test_type(self): - self.assertEqual(cpt.int_type, int) - def test_to_text(self): self.assertIsNone(cpt.to_text(None)) From 5a5220c4d78c4d72333dc415caf92ec157e2ee95 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 00:38:36 +0800 Subject: [PATCH 03/10] get_exception_message --- python/paddle/compat.py | 16 ---------------- python/paddle/fluid/core.py | 15 ++++++--------- python/paddle/fluid/optimizer.py | 2 +- .../paddle/fluid/tests/unittests/test_compat.py | 15 --------------- .../paddle/fluid/tests/unittests/test_dataset.py | 4 ++-- .../fluid/tests/unittests/test_exception.py | 3 +-- .../test_imperative_data_loader_exception.py | 6 ++---- .../test_imperative_data_loader_exit_func.py | 2 +- .../unittests/test_imperative_double_grad.py | 6 +++--- .../unittests/test_imperative_signal_handler.py | 7 +++---- .../fluid/tests/unittests/test_operator_desc.py | 5 ++--- .../fluid/tests/unittests/test_optimizer.py | 2 +- .../paddle/fluid/tests/unittests/test_prune.py | 2 +- .../fluid/tests/unittests/test_zeros_op.py | 2 +- 14 files changed, 24 insertions(+), 63 deletions(-) diff --git a/python/paddle/compat.py b/python/paddle/compat.py index 24d86103693d9..9655a9f4dc225 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -240,19 +240,3 @@ def floor_division(x, y): division result of x // y """ return x // y - - -# exception related functions -def get_exception_message(exc): - """ - Get the error message of a specific exception - - Args: - exec(Exception) : The exception to get error message. - - Returns: - the error message of exec - """ - assert exc is not None - - return str(exc) diff --git a/python/paddle/fluid/core.py b/python/paddle/fluid/core.py index 7c7fff32fb268..1654fd827e5ec 100644 --- a/python/paddle/fluid/core.py +++ b/python/paddle/fluid/core.py @@ -47,18 +47,17 @@ from .. import compat as cpt if os.name == 'nt': executable_path = os.path.abspath(os.path.dirname(sys.executable)) - raise ImportError( - """NOTE: You may need to run \"set PATH=%s;%%PATH%%\" + raise ImportError("""NOTE: You may need to run \"set PATH=%s;%%PATH%%\" if you encounters \"DLL load failed\" errors. If you have python installed in other directory, replace \"%s\" with your own directory. The original error is: \n %s""" % - (executable_path, executable_path, cpt.get_exception_message(e))) + (executable_path, executable_path, str(e))) else: raise ImportError( """NOTE: You may need to run \"export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH\" if you encounters \"libmkldnn.so not found\" errors. If you have python installed in other directory, replace \"/usr/local/lib\" with your own - directory. The original error is: \n""" + cpt.get_exception_message(e)) + directory. The original error is: \n""" + str(e)) except Exception as e: raise e @@ -75,8 +74,7 @@ def avx_supported(): has_avx = os.popen('cat /proc/cpuinfo | grep -i avx').read() != '' except Exception as e: sys.stderr.write('Can not get the AVX flag from /proc/cpuinfo.\n' - 'The original error is: %s\n' % - cpt.get_exception_message(e)) + 'The original error is: %s\n' % str(e)) return has_avx elif sysstr == 'darwin': try: @@ -85,7 +83,7 @@ def avx_supported(): except Exception as e: sys.stderr.write( 'Can not get the AVX flag from machdep.cpu.features.\n' - 'The original error is: %s\n' % cpt.get_exception_message(e)) + 'The original error is: %s\n' % str(e)) if not has_avx: import subprocess pipe = subprocess.Popen( @@ -155,8 +153,7 @@ def asm_func(code_str, restype=ctypes.c_uint32, argtypes=()): ctypes.c_size_t(0), ONE_PAGE) except Exception as e: sys.stderr.write('Failed getting the AVX flag on Windows.\n' - 'The original error is: %s\n' % - cpt.get_exception_message(e)) + 'The original error is: %s\n' % str(e)) return (retval & (1 << avx_bit)) > 0 else: sys.stderr.write('Do not get AVX flag on %s\n' % sysstr) diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index 4de37354230fb..ac0eada4f9525 100755 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -6272,7 +6272,7 @@ def mlp(input_x, input_y, hid_dim=128, label_dim=2): state_dict = {} sgd.load(state_dict) except NotImplementedError as e: - print(cpt.get_exception_message(e)) + print(e) """ raise NotImplementedError( "load function is not supported by Recompute Optimizer for now") diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index 454add0f16b63..0175338a9653d 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -257,21 +257,6 @@ def test_floor_division(self): self.assertRaises(ZeroDivisionError, cpt.floor_division, 3, 0) self.assertRaises(TypeError, cpt.floor_division, None, None) - def test_get_exception_message(self): - exception_message = "test_message" - self.assertRaises(AssertionError, cpt.get_exception_message, None) - try: - raise RuntimeError(exception_message) - except Exception as e: - self.assertEqual(exception_message, cpt.get_exception_message(e)) - self.assertIsNotNone(e) - - try: - raise Exception(exception_message) - except Exception as e: - self.assertEqual(exception_message, cpt.get_exception_message(e)) - self.assertIsNotNone(e) - if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_dataset.py b/python/paddle/fluid/tests/unittests/test_dataset.py index 7a41b6aeddde3..290470f7c91ce 100644 --- a/python/paddle/fluid/tests/unittests/test_dataset.py +++ b/python/paddle/fluid/tests/unittests/test_dataset.py @@ -906,7 +906,7 @@ def test_dataset_none(self): print("warning: we skip trainer_desc_pb2 import problem in windows") except RuntimeError as e: error_msg = "dataset is need and should be initialized" - self.assertEqual(error_msg, cpt.get_exception_message(e)) + self.assertEqual(error_msg, str(e)) except Exception as e: self.assertTrue(False) @@ -950,7 +950,7 @@ def test_fetch_handler(self): print("warning: we skip trainer_desc_pb2 import problem in windows") except RuntimeError as e: error_msg = "dataset is need and should be initialized" - self.assertEqual(error_msg, cpt.get_exception_message(e)) + self.assertEqual(error_msg, str(e)) except Exception as e: self.assertTrue(False) diff --git a/python/paddle/fluid/tests/unittests/test_exception.py b/python/paddle/fluid/tests/unittests/test_exception.py index 266c83106f2ea..a3d9e744481b4 100644 --- a/python/paddle/fluid/tests/unittests/test_exception.py +++ b/python/paddle/fluid/tests/unittests/test_exception.py @@ -28,8 +28,7 @@ def test_exception(self): try: core.__unittest_throw_exception__() except RuntimeError as ex: - self.assertIn("This is a test of exception", - cpt.get_exception_message(ex)) + self.assertIn("This is a test of exception", str(ex)) exception = ex self.assertIsNotNone(exception) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py index 034d38c4e8ae3..ac982a99addad 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exception.py @@ -68,8 +68,7 @@ def __reader__(): for _ in loader(): print("test_single_process_with_thread_expection") except core.EnforceNotMet as ex: - self.assertIn("Blocking queue is killed", - cpt.get_exception_message(ex)) + self.assertIn("Blocking queue is killed", str(ex)) exception = ex self.assertIsNotNone(exception) @@ -131,8 +130,7 @@ def __reader__(): for image, _ in loader(): fluid.layers.relu(image) except core.EnforceNotMet as ex: - self.assertIn("Blocking queue is killed", - cpt.get_exception_message(ex)) + self.assertIn("Blocking queue is killed", str(ex)) exception = ex self.assertIsNotNone(exception) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exit_func.py b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exit_func.py index 021637ec3a60b..71693c2ef163b 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exit_func.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_data_loader_exit_func.py @@ -61,7 +61,7 @@ def func_test_not_callable_func(self): try: CleanupFuncRegistrar.register(5) except TypeError as ex: - self.assertIn("is not callable", cpt.get_exception_message(ex)) + self.assertIn("is not callable", str(ex)) exception = ex self.assertIsNotNone(exception) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py index d71d2aba63dde..b3cb4f51b1996 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_double_grad.py @@ -110,7 +110,7 @@ def func_simple_example_eager_grad_not_allow_unused(self): # allow_unused is false in default dx = fluid.dygraph.grad(out, [x, z]) except ValueError as e: - error_msg = cpt.get_exception_message(e) + error_msg = str(e) assert error_msg.find("allow_unused") > 0 def test_simple_example_eager_grad_not_allow_unused(self): @@ -134,7 +134,7 @@ def func_simple_example_eager_grad_duplicate_input(self): # duplicate input will arise RuntimeError errors dx = fluid.dygraph.grad(out, [x, x]) except RuntimeError as e: - error_msg = cpt.get_exception_message(e) + error_msg = str(e) assert error_msg.find("duplicate") > 0 def test_simple_example_eager_grad_duplicate_input(self): @@ -158,7 +158,7 @@ def func_simple_example_eager_grad_duplicate_output(self): # duplicate output will arise RuntimeError errors dx = fluid.dygraph.grad([out, out], [x]) except RuntimeError as e: - error_msg = cpt.get_exception_message(e) + error_msg = str(e) assert error_msg.find("duplicate") > 0 def test_simple_example_eager_grad_duplicate_output(self): diff --git a/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py b/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py index 3f2a897b6b3e0..72be7aafaf0d3 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_signal_handler.py @@ -55,7 +55,7 @@ def try_except_exit(): set_child_signal_handler(id(self), test_process.pid) time.sleep(5) except SystemError as ex: - self.assertIn("Fatal", cpt.get_exception_message(ex)) + self.assertIn("Fatal", str(ex)) exception = ex return exception @@ -88,8 +88,7 @@ def try_except_exit(): set_child_signal_handler(id(self), test_process.pid) time.sleep(5) except SystemError as ex: - self.assertIn("Segmentation fault", - cpt.get_exception_message(ex)) + self.assertIn("Segmentation fault", str(ex)) exception = ex return exception @@ -122,7 +121,7 @@ def try_except_exit(): set_child_signal_handler(id(self), test_process.pid) time.sleep(5) except SystemError as ex: - self.assertIn("Bus error", cpt.get_exception_message(ex)) + self.assertIn("Bus error", str(ex)) exception = ex return exception diff --git a/python/paddle/fluid/tests/unittests/test_operator_desc.py b/python/paddle/fluid/tests/unittests/test_operator_desc.py index 7cf5b36f764c0..2b2c7287014a9 100644 --- a/python/paddle/fluid/tests/unittests/test_operator_desc.py +++ b/python/paddle/fluid/tests/unittests/test_operator_desc.py @@ -31,15 +31,14 @@ def test_error_type(self): self.assertFail() except ValueError as v_err: self.assertEqual( - cpt.get_exception_message(v_err), + str(v_err), "`type` to initialized an Operator can not be None.") try: block.append_op(type="no_such_op") self.assertFail() except ValueError as a_err: self.assertEqual( - cpt.get_exception_message(a_err), - "Operator \"no_such_op\" has not been registered.") + str(a_err), "Operator \"no_such_op\" has not been registered.") def test_op_desc_creation(self): program = Program() diff --git a/python/paddle/fluid/tests/unittests/test_optimizer.py b/python/paddle/fluid/tests/unittests/test_optimizer.py index 8e127f3c60c0a..52ce642c0e7de 100644 --- a/python/paddle/fluid/tests/unittests/test_optimizer.py +++ b/python/paddle/fluid/tests/unittests/test_optimizer.py @@ -990,7 +990,7 @@ def test_load(self): except NotImplementedError as e: self.assertEqual( "load function is not supported by Recompute Optimizer for now", - cpt.get_exception_message(e)) + str(e)) def test_dropout(self): """ diff --git a/python/paddle/fluid/tests/unittests/test_prune.py b/python/paddle/fluid/tests/unittests/test_prune.py index 4899d5c4d4883..dc44f99d670b6 100644 --- a/python/paddle/fluid/tests/unittests/test_prune.py +++ b/python/paddle/fluid/tests/unittests/test_prune.py @@ -100,7 +100,7 @@ def test_prune_target_none(self): except ValueError as e: self.assertIn( "All targets of Program._prune_with_input() can only be Variable or Operator", - cpt.get_exception_message(e)) + str(e)) def mock(self, program, feed, fetch, optimize_ops): diff --git a/python/paddle/fluid/tests/unittests/test_zeros_op.py b/python/paddle/fluid/tests/unittests/test_zeros_op.py index 031439171d5fe..0144424b12b78 100644 --- a/python/paddle/fluid/tests/unittests/test_zeros_op.py +++ b/python/paddle/fluid/tests/unittests/test_zeros_op.py @@ -106,7 +106,7 @@ def test_shape_errors(self): shape = [-1, 5] out = paddle.zeros(shape) except Exception as e: - error_msg = cpt.get_exception_message(e) + error_msg = str(e) assert error_msg.find("expected to be no less than 0") > 0 def test_eager(self): From e672339cc38544c7f59de3fcd8145e2b1b372aa8 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 01:13:51 +0800 Subject: [PATCH 04/10] fix example code --- python/paddle/fluid/optimizer.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index ac0eada4f9525..a26253247accd 100755 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -6239,7 +6239,7 @@ def _enable_offload(self): @framework.deprecate_stat_dict def load(self, state_dict): """ - :api_attr: Static Graph + :api_attr: Static Graph load function is not supported by Recompute Optimizer for now. :return: None @@ -6250,8 +6250,10 @@ def load(self, state_dict): Examples: .. code-block:: python + import paddle import paddle.fluid as fluid - import paddle.compat as cpt + + paddle.enable_static() def mlp(input_x, input_y, hid_dim=128, label_dim=2): fc_1 = fluid.layers.fc(input=input_x, size=hid_dim) From ff0852d1b0ec7eeae62c7e4f48ce71549bc5f2fa Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 02:12:36 +0800 Subject: [PATCH 05/10] revert example code change --- python/paddle/fluid/optimizer.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index a26253247accd..4de37354230fb 100755 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -6239,7 +6239,7 @@ def _enable_offload(self): @framework.deprecate_stat_dict def load(self, state_dict): """ - :api_attr: Static Graph + :api_attr: Static Graph load function is not supported by Recompute Optimizer for now. :return: None @@ -6250,10 +6250,8 @@ def load(self, state_dict): Examples: .. code-block:: python - import paddle import paddle.fluid as fluid - - paddle.enable_static() + import paddle.compat as cpt def mlp(input_x, input_y, hid_dim=128, label_dim=2): fc_1 = fluid.layers.fc(input=input_x, size=hid_dim) @@ -6274,7 +6272,7 @@ def mlp(input_x, input_y, hid_dim=128, label_dim=2): state_dict = {} sgd.load(state_dict) except NotImplementedError as e: - print(e) + print(cpt.get_exception_message(e)) """ raise NotImplementedError( "load function is not supported by Recompute Optimizer for now") From 12011a695e8fde9abadfc3fa5ab31435674309ef Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 15:03:33 +0800 Subject: [PATCH 06/10] floor_divison --- python/paddle/compat.py | 16 ---------------- .../paddle/fluid/tests/unittests/test_compat.py | 9 --------- 2 files changed, 25 deletions(-) diff --git a/python/paddle/compat.py b/python/paddle/compat.py index 9655a9f4dc225..c278041b46126 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -224,19 +224,3 @@ def round(x, d=0): else: import __builtin__ return __builtin__.round(x, d) - - -def floor_division(x, y): - """ - Compatible division which act the same behaviour in Python3 and Python2, - whose result will be a int value of floor(x / y) in Python3 and value of - (x / y) in Python2. - - Args: - x(int|float) : The number to divide. - y(int|float) : The number to be divided - - Returns: - division result of x // y - """ - return x // y diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index 0175338a9653d..e7ecdeda8b6c8 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -248,15 +248,6 @@ def test_round(self): self.assertEqual(5.0, cpt.round(5)) self.assertRaises(TypeError, cpt.round, None) - def test_floor_division(self): - self.assertEqual(0.0, cpt.floor_division(3, 4)) - self.assertEqual(1.0, cpt.floor_division(4, 3)) - self.assertEqual(2.0, cpt.floor_division(6, 3)) - self.assertEqual(-2.0, cpt.floor_division(-4, 3)) - self.assertEqual(-2.0, cpt.floor_division(-6, 3)) - self.assertRaises(ZeroDivisionError, cpt.floor_division, 3, 0) - self.assertRaises(TypeError, cpt.floor_division, None, None) - if __name__ == "__main__": unittest.main() From 222967e89f51f7a8bb1e5ebf44d62ec204b6ffd9 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 15:06:55 +0800 Subject: [PATCH 07/10] round --- python/paddle/fluid/tests/unittests/test_compat.py | 12 ------------ .../paddle/fluid/tests/unittests/test_roi_pool_op.py | 9 ++++----- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index e7ecdeda8b6c8..e4fedede47ce3 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -236,18 +236,6 @@ def test_to_bytes(self): for i in l2: self.assertTrue(isinstance(i, bytes)) - def test_round(self): - self.assertEqual(3.0, cpt.round(3.4)) - self.assertEqual(4.0, cpt.round(3.5)) - self.assertEqual(0.0, cpt.round(0.1)) - self.assertEqual(0.0, cpt.round(0.0)) - self.assertEqual(-0.0, cpt.round(-0.0)) - self.assertEqual(-0.0, cpt.round(-0.1)) - self.assertEqual(-3.0, cpt.round(-3.4)) - self.assertEqual(-4.0, cpt.round(-3.5)) - self.assertEqual(5.0, cpt.round(5)) - self.assertRaises(TypeError, cpt.round, None) - if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py index 97b058f0e1327..8b413c8f5d433 100644 --- a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py +++ b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py @@ -17,7 +17,6 @@ import numpy as np import math import sys -import paddle.compat as cpt from op_test import OpTest import paddle.fluid as fluid @@ -67,10 +66,10 @@ def calc_roi_pool(self): for i in range(self.rois_num): roi = self.rois[i] roi_batch_id = int(roi[0]) - roi_start_w = int(cpt.round(roi[1] * self.spatial_scale)) - roi_start_h = int(cpt.round(roi[2] * self.spatial_scale)) - roi_end_w = int(cpt.round(roi[3] * self.spatial_scale)) - roi_end_h = int(cpt.round(roi[4] * self.spatial_scale)) + roi_start_w = int(round(roi[1] * self.spatial_scale)) + roi_start_h = int(round(roi[2] * self.spatial_scale)) + roi_end_w = int(round(roi[3] * self.spatial_scale)) + roi_end_h = int(round(roi[4] * self.spatial_scale)) roi_height = int(max(roi_end_h - roi_start_h + 1, 1)) roi_width = int(max(roi_end_w - roi_start_w + 1, 1)) From a9ad9f4301288690fa208025f9c75bf42d7ae806 Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 16:13:51 +0800 Subject: [PATCH 08/10] use np.testing.assert_allclose in op_test --- .../paddle/fluid/tests/unittests/op_test.py | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index 18eaae293b3df..d95a168762e4c 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -1438,14 +1438,14 @@ def find_actual_value(self, name): raise NotImplementedError("base class, not implement!") def _compare_numpy(self, name, actual_np, expect_np): - self.op_test.assertTrue( - np.allclose( - actual_np, - expect_np, - atol=atol, - rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, - equal_nan=equal_nan), "Output (" + name + - ") has diff at " + str(place) + " in " + self.checker_name) + np.testing.assert_allclose( + actual_np, + expect_np, + atol=atol, + rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, + equal_nan=equal_nan, + err_msg="Output (" + name + ") has diff at " + str(place) + + " in " + self.checker_name) def _compare_list(self, name, actual, expect): """ if expect is a tuple, we need to compare list. @@ -1584,15 +1584,14 @@ def _compare_numpy(self, name, actual_np, expect_np): 1) == 0: pass else: - self.op_test.assertTrue( - np.allclose( - actual_np, - expect_np, - atol=atol, - rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, - equal_nan=equal_nan), - "Output (" + name + ") has diff at " + str(place) + - " in " + self.checker_name) + np.testing.assert_allclose( + actual_np, + expect_np, + atol=atol, + rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, + equal_nan=equal_nan, + err_msg="Output (" + name + ") has diff at " + + str(place) + " in " + self.checker_name) class EagerChecker(DygraphChecker): From 2289b4b68101468b368b73f60d941db00231e3ea Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 16:52:04 +0800 Subject: [PATCH 09/10] Revert "round" This reverts commit 222967e89f51f7a8bb1e5ebf44d62ec204b6ffd9. --- python/paddle/fluid/tests/unittests/test_compat.py | 12 ++++++++++++ .../paddle/fluid/tests/unittests/test_roi_pool_op.py | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_compat.py b/python/paddle/fluid/tests/unittests/test_compat.py index e4fedede47ce3..e7ecdeda8b6c8 100644 --- a/python/paddle/fluid/tests/unittests/test_compat.py +++ b/python/paddle/fluid/tests/unittests/test_compat.py @@ -236,6 +236,18 @@ def test_to_bytes(self): for i in l2: self.assertTrue(isinstance(i, bytes)) + def test_round(self): + self.assertEqual(3.0, cpt.round(3.4)) + self.assertEqual(4.0, cpt.round(3.5)) + self.assertEqual(0.0, cpt.round(0.1)) + self.assertEqual(0.0, cpt.round(0.0)) + self.assertEqual(-0.0, cpt.round(-0.0)) + self.assertEqual(-0.0, cpt.round(-0.1)) + self.assertEqual(-3.0, cpt.round(-3.4)) + self.assertEqual(-4.0, cpt.round(-3.5)) + self.assertEqual(5.0, cpt.round(5)) + self.assertRaises(TypeError, cpt.round, None) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py index 8b413c8f5d433..97b058f0e1327 100644 --- a/python/paddle/fluid/tests/unittests/test_roi_pool_op.py +++ b/python/paddle/fluid/tests/unittests/test_roi_pool_op.py @@ -17,6 +17,7 @@ import numpy as np import math import sys +import paddle.compat as cpt from op_test import OpTest import paddle.fluid as fluid @@ -66,10 +67,10 @@ def calc_roi_pool(self): for i in range(self.rois_num): roi = self.rois[i] roi_batch_id = int(roi[0]) - roi_start_w = int(round(roi[1] * self.spatial_scale)) - roi_start_h = int(round(roi[2] * self.spatial_scale)) - roi_end_w = int(round(roi[3] * self.spatial_scale)) - roi_end_h = int(round(roi[4] * self.spatial_scale)) + roi_start_w = int(cpt.round(roi[1] * self.spatial_scale)) + roi_start_h = int(cpt.round(roi[2] * self.spatial_scale)) + roi_end_w = int(cpt.round(roi[3] * self.spatial_scale)) + roi_end_h = int(cpt.round(roi[4] * self.spatial_scale)) roi_height = int(max(roi_end_h - roi_start_h + 1, 1)) roi_width = int(max(roi_end_w - roi_start_w + 1, 1)) From c6592daf2e10016d4143fb04512f9c469707369d Mon Sep 17 00:00:00 2001 From: SigureMo Date: Sat, 1 Oct 2022 16:52:10 +0800 Subject: [PATCH 10/10] Revert "use np.testing.assert_allclose in op_test" This reverts commit a9ad9f4301288690fa208025f9c75bf42d7ae806. --- .../paddle/fluid/tests/unittests/op_test.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index d95a168762e4c..18eaae293b3df 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -1438,14 +1438,14 @@ def find_actual_value(self, name): raise NotImplementedError("base class, not implement!") def _compare_numpy(self, name, actual_np, expect_np): - np.testing.assert_allclose( - actual_np, - expect_np, - atol=atol, - rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, - equal_nan=equal_nan, - err_msg="Output (" + name + ") has diff at " + str(place) + - " in " + self.checker_name) + self.op_test.assertTrue( + np.allclose( + actual_np, + expect_np, + atol=atol, + rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, + equal_nan=equal_nan), "Output (" + name + + ") has diff at " + str(place) + " in " + self.checker_name) def _compare_list(self, name, actual, expect): """ if expect is a tuple, we need to compare list. @@ -1584,14 +1584,15 @@ def _compare_numpy(self, name, actual_np, expect_np): 1) == 0: pass else: - np.testing.assert_allclose( - actual_np, - expect_np, - atol=atol, - rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, - equal_nan=equal_nan, - err_msg="Output (" + name + ") has diff at " + - str(place) + " in " + self.checker_name) + self.op_test.assertTrue( + np.allclose( + actual_np, + expect_np, + atol=atol, + rtol=self.rtol if hasattr(self, 'rtol') else 1e-5, + equal_nan=equal_nan), + "Output (" + name + ") has diff at " + str(place) + + " in " + self.checker_name) class EagerChecker(DygraphChecker):