MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / _convert_weights_to_fp16

Function _convert_weights_to_fp16

clip/model.py:384–400  ·  view source on GitHub ↗
(l)

Source from the content-addressed store, hash-verified

382 """Convert applicable model parameters to fp16"""
383
384 def _convert_weights_to_fp16(l):
385 if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)):
386 l.weight.data = l.weight.data.half()
387 if l.bias is not None:
388 l.bias.data = l.bias.data.half()
389
390 if isinstance(l, MultiheadAttention):
391 for attr in [*[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v"]:
392 tensor = getattr(l, attr)
393 if tensor is not None:
394 tensor.data = tensor.data.half()
395
396 for name in ["text_projection", "proj"]:
397 if hasattr(l, name):
398 attr = getattr(l, name)
399 if attr is not None:
400 attr.data = attr.data.half()
401
402 model.apply(_convert_weights_to_fp16)
403

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected