From 300317357669c98637ef724ee341ecba8a0a4e7e Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Tue, 9 Jan 2024 21:05:25 +0000 Subject: [PATCH 1/9] deepcopy -> clone --- mmengine/structures/base_data_element.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 454a224371..0e72a5736e 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -228,7 +228,8 @@ def set_metainfo(self, metainfo: dict) -> None: assert isinstance( metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' - meta = copy.deepcopy(metainfo) + # meta = copy.deepcopy(metainfo) + meta = metainfo.clone() for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From ea569621bead8cc226412cb5c89c7b2a79e26c07 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Tue, 9 Jan 2024 21:23:46 +0000 Subject: [PATCH 2/9] manual check for torch.Tensor during copy --- mmengine/structures/base_data_element.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 0e72a5736e..409313c6a4 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -229,7 +229,12 @@ def set_metainfo(self, metainfo: dict) -> None: metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' # meta = copy.deepcopy(metainfo) - meta = metainfo.clone() + meta = {} + for key in metainfo: + if isinstance(metainfo[key], torch.Tensor): + meta[key] = metainfo[key].clone() + else: + meta[key] = copy.deepcopy(metainfo[key]) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From a98628613b071d2fd7e7c63e2df2e527893f2149 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Tue, 9 Jan 2024 22:26:29 +0000 Subject: [PATCH 3/9] bug fix --- mmengine/structures/base_data_element.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 409313c6a4..d46a4ca146 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -230,11 +230,12 @@ def set_metainfo(self, metainfo: dict) -> None: dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' # meta = copy.deepcopy(metainfo) meta = {} - for key in metainfo: - if isinstance(metainfo[key], torch.Tensor): - meta[key] = metainfo[key].clone() + for key in list(metainfo.keys()): + value = metainfo[key] + if isinstance(value, torch.Tensor): + meta[key] = value.clone() else: - meta[key] = copy.deepcopy(metainfo[key]) + meta[key] = copy.deepcopy(value) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From a8897016cf82f9d094ada39c2bf2d6b616f2cff1 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Wed, 10 Jan 2024 14:15:48 +0000 Subject: [PATCH 4/9] add more logs --- mmengine/structures/base_data_element.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index d46a4ca146..44a58d0470 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -230,11 +230,14 @@ def set_metainfo(self, metainfo: dict) -> None: dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' # meta = copy.deepcopy(metainfo) meta = {} - for key in list(metainfo.keys()): - value = metainfo[key] + for key, value in metainfo.items(): + print("VALUE", value) + print("TYPE(VALUE)", type(value)) if isinstance(value, torch.Tensor): + print("CLONE") meta[key] = value.clone() else: + print("DEEPCOPY") meta[key] = copy.deepcopy(value) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From 48f45273101f40921a9ca81e4d03eec362a119c3 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Wed, 10 Jan 2024 15:15:49 +0000 Subject: [PATCH 5/9] torch.Size check --- mmengine/structures/base_data_element.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 44a58d0470..a441bb7e84 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -233,7 +233,7 @@ def set_metainfo(self, metainfo: dict) -> None: for key, value in metainfo.items(): print("VALUE", value) print("TYPE(VALUE)", type(value)) - if isinstance(value, torch.Tensor): + if isinstance(value, (torch.Tensor, torch.Size)): print("CLONE") meta[key] = value.clone() else: From 357aaef41498a5f3f7d5accd5090227b344bfaa3 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Wed, 10 Jan 2024 21:42:44 +0000 Subject: [PATCH 6/9] print --- mmengine/structures/base_data_element.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index a441bb7e84..f95f133ab4 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -228,17 +228,8 @@ def set_metainfo(self, metainfo: dict) -> None: assert isinstance( metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' - # meta = copy.deepcopy(metainfo) - meta = {} - for key, value in metainfo.items(): - print("VALUE", value) - print("TYPE(VALUE)", type(value)) - if isinstance(value, (torch.Tensor, torch.Size)): - print("CLONE") - meta[key] = value.clone() - else: - print("DEEPCOPY") - meta[key] = copy.deepcopy(value) + print("METAINFO", metainfo) + meta = copy.deepcopy(metainfo) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From 50d43a355378f4f1a37e7e91a161d06fc74c2ad2 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Thu, 11 Jan 2024 22:50:16 +0000 Subject: [PATCH 7/9] use dict copy over deepcopy --- mmengine/structures/base_data_element.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index f95f133ab4..6ae01bcea5 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -229,7 +229,8 @@ def set_metainfo(self, metainfo: dict) -> None: metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' print("METAINFO", metainfo) - meta = copy.deepcopy(metainfo) + meta = metainfo.copy() + # meta = copy.deepcopy(metainfo) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From cd00ac3e6b91f94d809f8d36829ace86d5fa9537 Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Mon, 15 Jan 2024 18:21:54 +0000 Subject: [PATCH 8/9] refactor --- mmengine/structures/base_data_element.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 6ae01bcea5..4b3a0a6acf 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -228,9 +228,7 @@ def set_metainfo(self, metainfo: dict) -> None: assert isinstance( metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' - print("METAINFO", metainfo) meta = metainfo.copy() - # meta = copy.deepcopy(metainfo) for k, v in meta.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) From 055bfcdf5846f8a1e8606d93bc6531c22e8dfb5c Mon Sep 17 00:00:00 2001 From: Prathik Rao Date: Mon, 22 Jan 2024 21:42:01 +0000 Subject: [PATCH 9/9] remove copy all together --- mmengine/structures/base_data_element.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 4b3a0a6acf..e3a4bdd3ad 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -228,8 +228,7 @@ def set_metainfo(self, metainfo: dict) -> None: assert isinstance( metainfo, dict), f'metainfo should be a ``dict`` but got {type(metainfo)}' - meta = metainfo.copy() - for k, v in meta.items(): + for k, v in metainfo.items(): self.set_field(name=k, value=v, field_type='metainfo', dtype=None) def set_data(self, data: dict) -> None: