MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / copy_to_device

Function copy_to_device

deepspeed/runtime/utils.py:151–172  ·  view source on GitHub ↗

Return a copy of tensor on specified device. Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts. Parameters: item: tensor to copy or (possibly nested) container of tensors to copy. device: target device criterion_func: Function

(item, device, criterion_func)

Source from the content-addressed store, hash-verified

149
150
151def copy_to_device(item, device, criterion_func):
152 """
153 Return a copy of tensor on specified device.
154 Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts.
155 Parameters:
156 item: tensor to copy or (possibly nested) container of tensors to copy.
157 device: target device
158 criterion_func: Function to restrict copy operation to items meet criterion
159
160 Returns:
161 None
162 """
163 if criterion_func(item):
164 return item.to(device)
165 elif isinstance(item, list):
166 return [copy_to_device(v, device, criterion_func) for v in item]
167 elif isinstance(item, tuple):
168 return tuple([copy_to_device(v, device, criterion_func) for v in item])
169 elif isinstance(item, dict):
170 return {k: copy_to_device(v, device, criterion_func) for k, v in item.items()}
171 else:
172 return item
173
174
175def move_to_device(item, device, criterion_func=None):

Callers 2

forwardMethod · 0.90
non_reentrant_checkpointFunction · 0.90

Calls 1

toMethod · 0.45

Tested by

no test coverage detected