MCPcopy Create free account
hub / github.com/AIS-SNU/Smart-Infinity / copy_to_device

Function copy_to_device

deepspeed/runtime/utils.py:126–147  ·  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

124
125
126def copy_to_device(item, device, criterion_func):
127 """
128 Return a copy of tensor on specified device.
129 Works on individual tensors, and tensors contained/nested in lists, tuples, and dicts.
130 Parameters:
131 item: tensor to copy or (possibly nested) container of tensors to copy.
132 device: target device
133 criterion_func: Function to restrict copy operation to items meet criterion
134
135 Returns:
136 None
137 """
138 if criterion_func(item):
139 return item.to(device)
140 elif isinstance(item, list):
141 return [copy_to_device(v, device, criterion_func) for v in item]
142 elif isinstance(item, tuple):
143 return tuple([copy_to_device(v, device, criterion_func) for v in item])
144 elif isinstance(item, dict):
145 return {k: copy_to_device(v, device, criterion_func) for k, v in item.items()}
146 else:
147 return item
148
149
150def move_to_device(item, device, criterion_func):

Callers 1

forwardMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected