MCPcopy Create free account
hub / github.com/CompVis/zigma / grad_clip

Function grad_clip

utils/train_utils.py:124–133  ·  view source on GitHub ↗
(opt, model, max_grad_norm=2.0)

Source from the content-addressed store, hash-verified

122
123
124def grad_clip(opt, model, max_grad_norm=2.0):
125 if hasattr(opt, "clip_grad_norm"):
126 # Some optimizers (like the sharded optimizer) have a specific way to do gradient clipping
127 opt.clip_grad_norm(max_grad_norm)
128 else:
129 # Revert to normal clipping otherwise, handling Apex or full precision
130 torch.nn.utils.clip_grad_norm_(
131 model.parameters(), # amp.master_params(self.opt) if self.use_apex else
132 max_grad_norm,
133 )
134
135
136def get_latest_checkpoint(checkpoint_dir):

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected