From 7e04d4d95c5859ece61ab42da792505458f088f7 Mon Sep 17 00:00:00 2001 From: liujuncheng Date: Fri, 1 Jul 2022 18:48:02 +0800 Subject: [PATCH 1/3] Format tensor on cpu --- python/oneflow/framework/tensor_str.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/oneflow/framework/tensor_str.py b/python/oneflow/framework/tensor_str.py index eaba9a96dbb..26f47fb260e 100644 --- a/python/oneflow/framework/tensor_str.py +++ b/python/oneflow/framework/tensor_str.py @@ -349,10 +349,8 @@ def _gen_tensor_str_template(tensor, is_meta): if tensor.is_global: suffixes.append(f"placement={str(tensor.placement)}") suffixes.append(f"sbp={str(tensor.sbp)}") - elif tensor.device.type == "cuda": - suffixes.append("device='" + str(tensor.device) + "'") elif tensor.device.type != "cpu": - raise RunTimeError("unknow device type") + suffixes.append("device='" + str(tensor.device) + "'") if tensor.is_lazy: suffixes.append("is_lazy='True'") @@ -366,7 +364,11 @@ def _gen_tensor_str_template(tensor, is_meta): tensor_str = "..." suffixes.append("size=" + str(tuple(tensor.shape))) else: - tensor_str = _tensor_str(tensor, indent) + if tensor.device.type != "cpu" and tensor.device.type != "cuda": + with flow.no_grad(): + tensor_str = _tensor_str(tensor.to("cpu"), indent) + else: + tensor_str = _tensor_str(tensor, indent) suffixes.append("dtype=" + str(tensor.dtype)) if tensor.grad_fn is not None: From e9989e391726abacf972ab2456acb7e5522a3d4b Mon Sep 17 00:00:00 2001 From: Juncheng Date: Fri, 1 Jul 2022 23:17:54 +0800 Subject: [PATCH 2/3] use tensor.detach --- python/oneflow/framework/tensor_str.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/oneflow/framework/tensor_str.py b/python/oneflow/framework/tensor_str.py index 26f47fb260e..c8192f1a276 100644 --- a/python/oneflow/framework/tensor_str.py +++ b/python/oneflow/framework/tensor_str.py @@ -365,8 +365,7 @@ def _gen_tensor_str_template(tensor, is_meta): suffixes.append("size=" + str(tuple(tensor.shape))) else: if tensor.device.type != "cpu" and tensor.device.type != "cuda": - with flow.no_grad(): - tensor_str = _tensor_str(tensor.to("cpu"), indent) + tensor_str = _tensor_str(tensor.detach().to("cpu"), indent) else: tensor_str = _tensor_str(tensor, indent) From 9e984658321c158ddbcf7bd63a45127f600cba2a Mon Sep 17 00:00:00 2001 From: liujuncheng Date: Mon, 4 Jul 2022 12:16:58 +0800 Subject: [PATCH 3/3] fix global --- python/oneflow/framework/tensor_str.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/oneflow/framework/tensor_str.py b/python/oneflow/framework/tensor_str.py index c8192f1a276..d4923df937a 100644 --- a/python/oneflow/framework/tensor_str.py +++ b/python/oneflow/framework/tensor_str.py @@ -339,6 +339,14 @@ def get_summarized_data(self): ) +def _format_tensor_on_cpu(tensor): + if tensor.is_global: + device = tensor.placement.type + else: + device = tensor.device.type + return device != "cpu" and device != "cuda" + + def _gen_tensor_str_template(tensor, is_meta): is_meta = is_meta or tensor.is_lazy prefix = "tensor(" @@ -364,7 +372,7 @@ def _gen_tensor_str_template(tensor, is_meta): tensor_str = "..." suffixes.append("size=" + str(tuple(tensor.shape))) else: - if tensor.device.type != "cpu" and tensor.device.type != "cuda": + if _format_tensor_on_cpu(tensor): tensor_str = _tensor_str(tensor.detach().to("cpu"), indent) else: tensor_str = _tensor_str(tensor, indent)