Commit 7f70edd
ascend-robot
[fix] use deepcopy for BatchMeta init
Co-authored-by: 0oshowero0<o0shower0o@outlook.com>
# message auto-generated for no-merge-commit merge:
!2 merge main into main
[fix] use deepcopy for BatchMeta init
Created-by: hanzhenyu8
Commit-by: 0oshowero0
Merged-by: ascend-robot
Description: ## Background
We nofity there might be some error during `BatchMeta.chunk()` due to shallow copy.
Reproduction script:
```python3
from transfer_queue.metadata import BatchMeta, FieldMeta, SampleMeta
from transfer_queue.utils.utils import ProductionStatus
import pickle
samples = [
SampleMeta(
partition_id="p1",
global_index=0,
fields={"image": FieldMeta("image", None, None)},
),
SampleMeta(
partition_id="p1",
global_index=1,
fields={"image": FieldMeta("image", None, None)},
),
SampleMeta(
partition_id="p1",
global_index=2,
fields={"image": FieldMeta("image", None, None)},
),
SampleMeta(
partition_id="p1",
global_index=3,
fields={"image": FieldMeta("image", None, None)},
),
]
extra_info = {"key1": [1, 2], "key2": {"nested": "value"}}
batch_meta = BatchMeta(samples=samples, extra_info=extra_info)
with open('before_chunk', "wb") as f:
pickle.dump(batch_meta, f)
# Execute chunk
chunks = batch_meta.chunk(2)
import pickle
with open('after_chunk', "wb") as f:
pickle.dump(batch_meta, f)
```
```bash
# You will see different md5 values!
md5sum before_chunk
md5sum after_chunk
```
This happens because during the `__post_init__` of `BatchMeta`, we set the attributes of the underlying `self.samples`:
```python3
class BatchMeta:
"""Records the metadata of a batch of data samples."""
samples: list[SampleMeta]
extra_info: dict[str, Any] = dataclasses.field(default_factory=dict)
def __post_init__(self):
"""Initialize all computed properties during initialization"""
# Basic properties
object.__setattr__(self, "_size", len(self.samples))
object.__setattr__(self, "_is_ready", all(sample.is_ready for sample in self.samples))
# Pre-compute all list properties for better performance
if self.samples:
for idx, sample in enumerate(self.samples):
object.__setattr__(sample, "_batch_index", idx) # Ensure batch_index is set correctly
...
```
## Solution
During `__post_init__`, we deepcopy the input `samples` and `extra_info`.
```python3
def __post_init__(self):
"""Initialize all computed properties during initialization"""
self.samples = copy.deepcopy(self.samples)
self.extra_info = copy.deepcopy(self.extra_info)
```
See merge request: Ascend/TransferQueue!21 parent a1645ea commit 7f70edd
1 file changed
Lines changed: 9 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
179 | 183 | | |
180 | 184 | | |
181 | 185 | | |
| |||
253 | 257 | | |
254 | 258 | | |
255 | 259 | | |
256 | | - | |
| 260 | + | |
257 | 261 | | |
258 | 262 | | |
259 | 263 | | |
| |||
296 | 300 | | |
297 | 301 | | |
298 | 302 | | |
299 | | - | |
| 303 | + | |
300 | 304 | | |
301 | 305 | | |
302 | 306 | | |
| |||
315 | 319 | | |
316 | 320 | | |
317 | 321 | | |
318 | | - | |
| 322 | + | |
319 | 323 | | |
320 | 324 | | |
321 | 325 | | |
| |||
326 | 330 | | |
327 | 331 | | |
328 | 332 | | |
329 | | - | |
| 333 | + | |
330 | 334 | | |
331 | 335 | | |
332 | 336 | | |
| |||
359 | 363 | | |
360 | 364 | | |
361 | 365 | | |
362 | | - | |
| 366 | + | |
363 | 367 | | |
364 | 368 | | |
365 | 369 | | |
| |||
0 commit comments