From fd850c3204fe774cd41414e9886ceb4131f0765c Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 19 Sep 2024 22:22:16 +0800 Subject: [PATCH 1/3] [Dy2St] Optimize `int(Value)` and `float(Value)` error messages --- python/paddle/pir/math_op_patch.py | 55 ++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/python/paddle/pir/math_op_patch.py b/python/paddle/pir/math_op_patch.py index f12603c56c5c7..a9df41789002b 100644 --- a/python/paddle/pir/math_op_patch.py +++ b/python/paddle/pir/math_op_patch.py @@ -477,18 +477,53 @@ def _T_(self): return _C_ops.transpose(self, perm) def _int_(self): - raise TypeError( - "int(Value) is not supported in static graph mode. If you are using @to_static, you can try this:\n" - "1. If you want to get the value of Value, you can switch to non-fullgraph mode by setting @to_static(full_graph=True).\n" - "2. If you want to run it in full graph mode, you need use Value.astype(paddle.int32), and do not use int(Value)." - ) + error_msg = """\ + int(Value) is not supported in static graph mode. Because it's value is not available during the static mode. + If you haven't call int(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: + >>> logging.info("The value of x is: {int(x)}") + ^ `x` is Tensor, `int(x)` triggers int(Tensor) + + There are two common workarounds available: + If you are logging Tensor values, then consider logging only at dynamic graphs, for example: + + Modify the following code + >>> logging.info("The value of x is: {int(x)}") + to + >>> if paddle.in_dynamic_mode(): + ... logging.info("The value of x is: {int(x)}") + + If you need to convert the Tensor type, for example: + Modify the following code + >>> x = int(x) + to + >>> x = x.astype("int64") + """ + + raise TypeError(textwrap.dedent(error_msg)) def _float_(self): - raise TypeError( - "float(Value) is not supported in static graph mode. If you are using @to_static, you can try this:\n" - "1. If you want to get the value of Value, you can switch to non-fullgraph mode by setting @to_static(full_graph=True).\n" - "2. If you want to run it in full graph mode, you need use Value directly, and do not use float(Value)." - ) + error_msg = """\ + float(Value) is not supported in static graph mode. Because it's value is not available during the static mode. + If you haven't call float(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: + >>> logging.info("The value of x is: {float(x)}") + ^ `x` is Tensor, `float(x)` triggers float(Tensor) + + There are two common workarounds available: + If you are logging Tensor values, then consider logging only at dynamic graphs, for example: + + Modify the following code + >>> logging.info("The value of x is: {float(x)}") + to + >>> if paddle.in_dynamic_mode(): + ... logging.info("The value of x is: {float(x)}") + + If you need to convert the Tensor type, for example: + Modify the following code + >>> x = float(x) + to + >>> x = x.astype("float64") + """ + raise TypeError(textwrap.dedent(error_msg)) def _bool_(self): error_msg = """\ From 983e58e567a87f1546b723c319c68ea04159e3ec Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Thu, 19 Sep 2024 22:30:52 +0800 Subject: [PATCH 2/3] rename --- python/paddle/pir/math_op_patch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/pir/math_op_patch.py b/python/paddle/pir/math_op_patch.py index a9df41789002b..f8f2079a87d2a 100644 --- a/python/paddle/pir/math_op_patch.py +++ b/python/paddle/pir/math_op_patch.py @@ -478,7 +478,7 @@ def _T_(self): def _int_(self): error_msg = """\ - int(Value) is not supported in static graph mode. Because it's value is not available during the static mode. + int(Tensor) is not supported in static graph mode. Because it's value is not available during the static mode. If you haven't call int(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: >>> logging.info("The value of x is: {int(x)}") ^ `x` is Tensor, `int(x)` triggers int(Tensor) @@ -503,7 +503,7 @@ def _int_(self): def _float_(self): error_msg = """\ - float(Value) is not supported in static graph mode. Because it's value is not available during the static mode. + float(Tensor) is not supported in static graph mode. Because it's value is not available during the static mode. If you haven't call float(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: >>> logging.info("The value of x is: {float(x)}") ^ `x` is Tensor, `float(x)` triggers float(Tensor) From e0a9052c3570c30682aa5dca96d02be9bb88ea83 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Mon, 23 Sep 2024 20:44:31 +0800 Subject: [PATCH 3/3] fix review --- python/paddle/pir/math_op_patch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/pir/math_op_patch.py b/python/paddle/pir/math_op_patch.py index f8f2079a87d2a..8d43e53c95239 100644 --- a/python/paddle/pir/math_op_patch.py +++ b/python/paddle/pir/math_op_patch.py @@ -479,7 +479,7 @@ def _T_(self): def _int_(self): error_msg = """\ int(Tensor) is not supported in static graph mode. Because it's value is not available during the static mode. - If you haven't call int(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: + It's usually triggered by the logging implicitly, for example: >>> logging.info("The value of x is: {int(x)}") ^ `x` is Tensor, `int(x)` triggers int(Tensor) @@ -504,7 +504,7 @@ def _int_(self): def _float_(self): error_msg = """\ float(Tensor) is not supported in static graph mode. Because it's value is not available during the static mode. - If you haven't call float(Tensor) explicitly, it's usually triggered by the logging implicitly, for example: + It's usually triggered by the logging implicitly, for example: >>> logging.info("The value of x is: {float(x)}") ^ `x` is Tensor, `float(x)` triggers float(Tensor)