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]])
| 101 | |
| 102 | |
| 103 | def 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 | |
| 119 | def randn_like(x: T.Union[torch.Tensor, T.Sequence[torch.Tensor]]): |