


from manim import *


class 	a__						(    lowerCAmelCase_     ):

       def     UpperCAmelCase( self   :  Optional[int]     ):
            a_     :  str				      =     Rectangle(height=0.5   ,				width=0.5     )
            a_     :  Optional[int]				      =     Rectangle(height=0.4_6   ,				width=0.4_6     ).set_stroke(width=0     )
            a_     :  str				      =     Rectangle(height=0.2_5   ,				width=0.2_5     )

            a_     :  Any				      =     [mem.copy() for i in range(6     )]
            a_     :  List[Any]				      =     [mem.copy() for i in range(6     )]
            a_     :  List[str]				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  List[str]				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  Dict				      =     VGroup(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  int				      =     Text("""CPU"""   ,				font_size=2_4     )
            a_     :  Union[str, Any]				      =     Group(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0.5   ,				aligned_edge=lowerCamelCase_     )
            cpu.move_to([-2.5, -0.5, 0]     )
            self.add(lowerCamelCase_     )

            a_     :  Optional[int]				      =     [mem.copy() for i in range(4     )]
            a_     :  Union[str, Any]				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  Optional[int]				      =     Text("""GPU"""   ,				font_size=2_4     )
            a_     :  Optional[Any]				      =     Group(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0.5   ,				aligned_edge=lowerCamelCase_     )
            gpu.move_to([-1, -1, 0]     )
            self.add(lowerCamelCase_     )

            a_     :  Tuple				      =     [mem.copy() for i in range(6     )]
            a_     :  Optional[int]				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )

            a_     :  List[str]				      =     Text("""Model"""   ,				font_size=2_4     )
            a_     :  List[str]				      =     Group(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0.5   ,				aligned_edge=lowerCamelCase_     )
            model.move_to([3, -1.0, 0]     )
            self.add(lowerCamelCase_     )

            a_     :  Optional[Any]				      =     []
            a_     :  Dict				      =     []

            for i, rect in enumerate(lowerCamelCase_     ):
                 rect.set_stroke(lowerCamelCase_     )

                 a_     :  Tuple				      =     Rectangle(height=0.4_6 / 4   ,				width=0.4_6 / 3     ).set_stroke(width=0.0     ).set_fill(lowerCamelCase_   ,				opacity=0.7     )

                 if i == 0:
                      cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT     )   ,				buff=0.0_2   ,				direction=lowerCamelCase_     )
                      cpu_target.set_x(cpu_target.get_x() + 0.1     )
                 elif i == 3:
                      cpu_target.next_to(model_cpu_arr[0]   ,				direction=lowerCamelCase_   ,				buff=0.0     )
                 else:
                      cpu_target.next_to(model_cpu_arr[i - 1]   ,				direction=lowerCamelCase_   ,				buff=0.0     )
                 self.add(lowerCamelCase_     )
                 model_cpu_arr.append(lowerCamelCase_     )

            self.add(*lowerCamelCase_   ,				*lowerCamelCase_     )

            a_     :  List[Any]				      =     [meta_mem.copy() for i in range(6     )]
            a_     :  List[str]				      =     [meta_mem.copy() for i in range(6     )]
            a_     :  int				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  Tuple				      =     VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  List[Any]				      =     VGroup(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0     )
            a_     :  Dict				      =     Text("""Disk"""   ,				font_size=2_4     )
            a_     :  Optional[int]				      =     Group(lowerCamelCase_   ,				lowerCamelCase_     ).arrange(lowerCamelCase_   ,				buff=0.5   ,				aligned_edge=lowerCamelCase_     )
            disk.move_to([-4.0, -1.2_5, 0]     )
            self.add(lowerCamelCase_   ,				lowerCamelCase_     )

            a_     :  List[Any]				      =     []

            for i in range(6     ):
                 a_     :  Optional[Any]				      =     fill.copy().set_fill(lowerCamelCase_   ,				opacity=0.8     )
                 target.move_to(disk_left_col_base[i]     ).scale(0.5     )
                 cpu_disk_arr.append(lowerCamelCase_     )

            self.add(*lowerCamelCase_     )

            a_     :  str				      =     Square(side_length=2.2     )
            key.move_to([-5, 2, 0]     )

            a_     :  Optional[Any]				      =     MarkupText(
                F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''   ,				font_size=1_8   ,				)

            key_text.move_to([-5, 2.4, 0]     )

            self.add(lowerCamelCase_   ,				lowerCamelCase_     )

            a_     :  Optional[Any]				      =     MarkupText(
                F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''   ,				font_size=1_8   ,				)

            blue_text.next_to(lowerCamelCase_   ,				DOWN * 2.4   ,				aligned_edge=key_text.get_left()     )
            self.add(lowerCamelCase_     )

            a_     :  Dict				      =     MarkupText(F'''The offloaded weights are all sent to the CPU.'''   ,				font_size=2_4     )
            step_a.move_to([2, 2, 0]     )

            self.play(Write(lowerCamelCase_   ,				run_time=3     )     )

            for i in range(6     ):
                 a_     :  List[str]				      =     cpu_disk_arr[i]
                 a_     :  List[str]				      =     rect.copy().set_fill(lowerCamelCase_   ,				opacity=0.8     ).scale(2.0     )
                 cpa.generate_target()
                 cpa.target.move_to(model_base[i]     )

                 if i == 0:
                      rect.set_fill(lowerCamelCase_   ,				opacity=0.8     )
                      rect.generate_target()
                      rect.target.move_to(cpu_left_col_base[0]     ).scale(2.0     )

                      self.remove(
                          *lowerCamelCase_   ,				*lowerCamelCase_   ,				)

                 else:
                      rect.generate_target()
                      rect.target.move_to(cpu_left_col_base[i]     ).scale(2.0     )
                 self.play(MoveToTarget(lowerCamelCase_     )   ,				MoveToTarget(lowerCamelCase_     )   ,				model_base[i].animate.set_stroke(lowerCamelCase_     )     )
            self.play(FadeOut(lowerCamelCase_     )     )

            a_     :  int				      =     MarkupText(
                F'''Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed.'''   ,				font_size=2_4   ,				)
            step_a.move_to([2, 2, 0]     )

            self.play(Write(lowerCamelCase_   ,				run_time=3     )     )

            a_     :  List[Any]				      =     []
            a_     :  List[str]				      =     []
            for i in range(6     ):
                 a_     :  str				      =     Arrow(start=lowerCamelCase_   ,				end=lowerCamelCase_   ,				color=lowerCamelCase_   ,				buff=0.5     )
                 a.next_to(model_base[i].get_left()   ,				lowerCamelCase_   ,				buff=0.2     )
                 arrows.append(lowerCamelCase_     )
                 animations.append(Write(lowerCamelCase_     )     )
            self.play(*lowerCamelCase_     )
            self.wait()









import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


__lowerCamelCase :    Any      						=      get_logger(__name__)







def 		lowercase__ (		__A:						str				,__A:						str					):





					'''simple docstring'''






					if text.startswith(__A					):
										return text[len(__A					) :]
					return text  # or whatever







def 		lowercase__ (		__A:						List[str]					):





					'''simple docstring'''






					return {remove_prefix(__A				,'''model.'''					): v for k, v in sd.items()}







def 		lowercase__ (		__A:						List[Dict[str, torch.Tensor]]					):





					'''simple docstring'''






					__magic_name__				:						List[Any]     				=				{}
					for k in state_dicts[0].keys():
										__magic_name__				:						str     				=				[sd[k] for sd in state_dicts]
										__magic_name__				:						List[Any]     				=				sum(__A					) / len(__A					)
										assert isinstance(__A				,torch.Tensor					)
										__magic_name__				:						Any     				=				new_t
					return new_sd







def 		lowercase__ (		__A:						str				,__A:						str				,__A:						str					):





					'''simple docstring'''






					__magic_name__				:						List[Any]     				=				AutoModelForSeqaSeqLM.from_pretrained(__A					)
					if os.path.isfile(__A					):
										__magic_name__				:						str     				=				[pl_ckpt_path]
					else:
										assert os.path.isdir(__A					)
										__magic_name__				:						List[str]     				=				list(Path(__A					).glob('''*.ckpt'''					)					)
										assert ckpt_files, F'''could not find any ckpt files inside the {pl_ckpt_path} directory'''

					if len(__A					) > 1:
										logger.info(F'''averaging the weights of {ckpt_files}'''					)

					__magic_name__				:						Union[str, Any]     				=				[sanitize(torch.load(__A				,map_location='''cpu'''					)['''state_dict''']					) for x in ckpt_files]
					__magic_name__				:						Dict     				=				average_state_dicts(__A					)

					__magic_name__				,     __magic_name__				:						Union[str, Any]     				=				hf_model.load_state_dict(__A				,strict=__A					)
					assert not missing, F'''missing keys: {missing}'''
					hf_model.save_pretrained(__A					)
					try:
										__magic_name__				:						Optional[Any]     				=				AutoTokenizer.from_pretrained(__A					)
										tok.save_pretrained(__A					)
					except Exception:
										pass
										# dont copy tokenizer if cant


if __name__ == "__main__":
	fire.Fire(convert_pl_to_hf)



