This does the final conversion step: take locally converted weights and apply a global renaming to them. It splits attention layers, and takes into account additional replacements that may arise. Assigns the weights to the new checkpoint.
(paths, checkpoint, old_checkpoint, additional_replacements=None, config=None)
| 647 | |
| 648 | |
| 649 | def assign_to_checkpoint(paths, checkpoint, old_checkpoint, additional_replacements=None, config=None): |
| 650 | """ |
| 651 | This does the final conversion step: take locally converted weights and apply a global renaming to them. It splits |
| 652 | attention layers, and takes into account additional replacements that may arise. |
| 653 | |
| 654 | Assigns the weights to the new checkpoint. |
| 655 | """ |
| 656 | assert isinstance(paths, list), "Paths should be a list of dicts containing 'old' and 'new' keys." |
| 657 | |
| 658 | for path in paths: |
| 659 | new_path = path["new"] |
| 660 | |
| 661 | # Global renaming happens here |
| 662 | new_path = new_path.replace("middle_block.0", "mid_block.resnets.0") |
| 663 | new_path = new_path.replace("middle_block.1", "mid_block.attentions.0") |
| 664 | new_path = new_path.replace("middle_block.2", "mid_block.resnets.1") |
| 665 | |
| 666 | if additional_replacements is not None: |
| 667 | for replacement in additional_replacements: |
| 668 | new_path = new_path.replace(replacement["old"], replacement["new"]) |
| 669 | |
| 670 | # proj_attn.weight has to be converted from conv 1D to linear |
| 671 | if "proj_attn.weight" in new_path or "to_out.0.weight" in new_path: |
| 672 | checkpoint[new_path] = old_checkpoint[path["old"]][:, :, 0] |
| 673 | else: |
| 674 | checkpoint[new_path] = old_checkpoint[path["old"]] |
| 675 | |
| 676 | |
| 677 | # TODO maybe document and/or can do more efficiently (build indices in for loop and extract once for each split?) |
no outgoing calls
no test coverage detected