MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / todevice

Function todevice

eval_code/recons/relpose/evo_utils.py:19–44  ·  view source on GitHub ↗

Transfer some variables to another device (i.e. GPU, CPU:torch, CPU:numpy). batch: list, tuple, dict of tensors or other things device: pytorch device or 'numpy' callback: function that would be called on every sub-elements.

(batch, device, callback=None, non_blocking=False)

Source from the content-addressed store, hash-verified

17
18
19def todevice(batch, device, callback=None, non_blocking=False):
20 """Transfer some variables to another device (i.e. GPU, CPU:torch, CPU:numpy).
21
22 batch: list, tuple, dict of tensors or other things
23 device: pytorch device or 'numpy'
24 callback: function that would be called on every sub-elements.
25 """
26 if callback:
27 batch = callback(batch)
28
29 if isinstance(batch, dict):
30 return {k: todevice(v, device) for k, v in batch.items()}
31
32 if isinstance(batch, (tuple, list)):
33 return type(batch)(todevice(x, device) for x in batch)
34
35 x = batch
36 if device == "numpy":
37 if isinstance(x, torch.Tensor):
38 x = x.detach().cpu().numpy()
39 elif x is not None:
40 if isinstance(x, np.ndarray):
41 x = torch.from_numpy(x)
42 if torch.is_tensor(x):
43 x = x.to(device, non_blocking=non_blocking)
44 return x
45
46
47def to_numpy(x):

Callers 1

to_numpyFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected