MCPcopy Create free account
hub / github.com/modelscope/modelscope / _tie_or_clone_weights

Function _tie_or_clone_weights

modelscope/utils/checkpoint.py:516–539  ·  view source on GitHub ↗
(output_embeddings,
                              input_embeddings,
                              torchscript=False)

Source from the content-addressed store, hash-verified

514 return retrieved_modules
515
516 def _tie_or_clone_weights(output_embeddings,
517 input_embeddings,
518 torchscript=False):
519 if torchscript:
520 output_embeddings.weight = nn.Parameter(
521 input_embeddings.weight.clone())
522 else:
523 output_embeddings.weight = input_embeddings.weight
524
525 if getattr(output_embeddings, 'bias', None) is not None:
526 output_embeddings.bias.data = nn.functional.pad(
527 output_embeddings.bias.data,
528 (
529 0,
530 output_embeddings.weight.shape[0]
531 - output_embeddings.bias.shape[0],
532 ),
533 'constant',
534 0,
535 )
536
537 if hasattr(output_embeddings, 'out_features') and hasattr(
538 input_embeddings, 'num_embeddings'):
539 output_embeddings.out_features = input_embeddings.num_embeddings
540
541 def tie_weights(model, tie_word_embeddings=False):
542 if tie_word_embeddings:

Callers 1

tie_weightsFunction · 0.85

Calls 2

cloneMethod · 0.45
padMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…