MCPcopy Create free account
hub / github.com/THUDM/GLM / detach_variable

Function detach_variable

mpu/random.py:62–83  ·  view source on GitHub ↗
(inputs, device=None)

Source from the content-addressed store, hash-verified

60transport_stream = None
61cuda_device=None
62def detach_variable(inputs, device=None):
63 if isinstance(inputs, tuple):
64 out = []
65 for inp in inputs:
66 if not isinstance(inp, torch.Tensor):
67 out.append(inp)
68 continue
69
70 requires_grad = inp.requires_grad
71
72 if device is not None:
73 x = inp.to(device=device)
74 else:
75 x = inp
76
77 x = x.detach()
78 x.requires_grad = requires_grad
79 out.append(x)
80 return tuple(out)
81 else:
82 raise RuntimeError(
83 "Only tuple of tensors is supported. Got Unsupported input type: ", type(inputs).__name__)
84
85def _set_cuda_rng_state(new_state, device=-1):
86 """Sets the random number generator state of the current GPU.

Callers 1

backwardMethod · 0.85

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected