Skip to content

ding.data.buffer.middleware.clone_object

ding.data.buffer.middleware.clone_object

clone_object()

Overview

This middleware freezes the objects saved in memory buffer and return copies during sampling, try this middleware when you need to keep the object unchanged in buffer, and modify the object after sampling it (usually in multiple threads)

Full Source Code

../ding/data/buffer/middleware/clone_object.py

1from typing import Callable, Any, List, Union 2from ding.data.buffer import BufferedData 3from ding.utils import fastcopy 4 5 6def clone_object(): 7 """ 8 Overview: 9 This middleware freezes the objects saved in memory buffer and return copies during sampling, 10 try this middleware when you need to keep the object unchanged in buffer, and modify\ 11 the object after sampling it (usually in multiple threads) 12 """ 13 14 def push(chain: Callable, data: Any, *args, **kwargs) -> BufferedData: 15 data = fastcopy.copy(data) 16 return chain(data, *args, **kwargs) 17 18 def sample(chain: Callable, *args, **kwargs) -> Union[List[BufferedData], List[List[BufferedData]]]: 19 data = chain(*args, **kwargs) 20 return fastcopy.copy(data) 21 22 def _clone_object(action: str, chain: Callable, *args, **kwargs): 23 if action == "push": 24 return push(chain, *args, **kwargs) 25 elif action == "sample": 26 return sample(chain, *args, **kwargs) 27 return chain(*args, **kwargs) 28 29 return _clone_object