MCPcopy Create free account
hub / github.com/apple/ml-pointersect / detach

Function detach

cdslib/core/nn/nn_utils.py:103–116  ·  view source on GitHub ↗

Detach each element in x, regardless if it is a tensor or nested list of tensors.

(x: T.Union[torch.Tensor, T.Dict[str, T.Any], T.Sequence[torch.Tensor]])

Source from the content-addressed store, hash-verified

101
102
103def detach(x: T.Union[torch.Tensor, T.Dict[str, T.Any], T.Sequence[torch.Tensor]]):
104 """
105 Detach each element in x, regardless if it is a tensor or nested list of tensors.
106 """
107 if isinstance(x, torch.Tensor):
108 return x.detach()
109 elif isinstance(x, dict):
110 for key, val in x.items():
111 x[key] = detach(val)
112 return x
113 elif isinstance(x, T.Sequence):
114 return [detach(xi) for xi in x]
115 else:
116 raise NotImplementedError
117
118
119def randn_like(x: T.Union[torch.Tensor, T.Sequence[torch.Tensor]]):

Callers 2

_build_modelMethod · 0.90
test_1Method · 0.90

Calls 1

detachMethod · 0.45

Tested by 2

_build_modelMethod · 0.72
test_1Method · 0.72