




"""simple docstring"""




# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import List
def 					A_			(		snake_case_     :       List[str]						,snake_case_     :       str    ):







    '''simple docstring'''
    if name != "training_function" and name != "main":
        raise ValueError(f'Incorrect function name passed: {name}, choose either \'main\' or \'training_function\''    )
    UpperCamelCase       ,							UpperCamelCase				:   Optional[int]          =							[], False
    for line in lines:
        if not found_start and f'def {name}' in line:
            UpperCamelCase				:   List[str]          =							True
            good_lines.append(snake_case_    )
            continue
        if found_start:
            if name == "training_function" and "def main" in line:
                return good_lines
            if name == "main" and "if __name__" in line:
                return good_lines
            good_lines.append(snake_case_    )
def 					A_			(		snake_case_     :       List[str]    ):







    '''simple docstring'''
    return [line for line in lines if not line.lstrip().startswith("""#"""    ) and line != "\n"]


def 					A_			(		snake_case_     :       str						,snake_case_     :       str						,snake_case_     :       bool						,snake_case_     :       str = None    ):







    '''simple docstring'''
    with open(snake_case_						,"""r"""    ) as f:
        UpperCamelCase				:   Tuple          =							f.readlines()
    with open(os.path.abspath(os.path.join("""examples"""						,"""nlp_example.py"""    )    )						,"""r"""    ) as f:
        UpperCamelCase				:   Optional[Any]          =							f.readlines()
    with open(snake_case_						,"""r"""    ) as f:
        UpperCamelCase				:   Optional[Any]          =							f.readlines()
    if secondary_filename is not None:
        with open(snake_case_						,"""r"""    ) as f:
            UpperCamelCase				:   List[Any]          =							f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
    if parser_only:
        UpperCamelCase				:   List[Any]          =							clean_lines(get_function_contents_by_name(snake_case_						,"""main"""    )    )
        UpperCamelCase				:   Optional[int]          =							clean_lines(get_function_contents_by_name(snake_case_						,"""main"""    )    )
        UpperCamelCase				:   Optional[Any]          =							clean_lines(get_function_contents_by_name(snake_case_						,"""main"""    )    )
        if secondary_filename is not None:
            UpperCamelCase				:   Tuple          =							clean_lines(get_function_contents_by_name(snake_case_						,"""main"""    )    )
    else:
        UpperCamelCase				:   List[str]          =							clean_lines(get_function_contents_by_name(snake_case_						,"""training_function"""    )    )
        UpperCamelCase				:   int          =							clean_lines(get_function_contents_by_name(snake_case_						,"""training_function"""    )    )
        UpperCamelCase				:   int          =							clean_lines(get_function_contents_by_name(snake_case_						,"""training_function"""    )    )
        if secondary_filename is not None:
            UpperCamelCase				:   Dict          =							clean_lines(
                get_function_contents_by_name(snake_case_						,"""training_function"""    )    )

    UpperCamelCase				:   Optional[Any]          =							"""train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n"""

    # Specific code in our script that differs from the full version, aka what is new
    UpperCamelCase				:   Any          =							[]
    UpperCamelCase				:   int          =							[]  # We keep track of the idxs just in case it's a repeated statement
    UpperCamelCase				:   Optional[Any]          =							iter(snake_case_    )
    for i in range(len(snake_case_    ) - 1    ):
        if i not in passed_idxs:
            UpperCamelCase				:   Any          =							next(snake_case_    )
            if (line not in full_file_func) and (line.lstrip() != _dl_line):
                if "TESTING_MOCKED_DATALOADERS" not in line:
                    new_feature_code.append(snake_case_    )
                    passed_idxs.append(snake_case_    )
                else:
                    # Skip over the `config['num_epochs'] = 2` statement
                    UpperCamelCase				:   List[str]          =							next(snake_case_    )

    # Extract out just the new parts from the full_file_training_func
    UpperCamelCase				:   Any          =							[]
    UpperCamelCase				:   List[Any]          =							[]  # We keep track of the idxs just in case it's a repeated statement
    for i, line in enumerate(snake_case_    ):
        if i not in passed_idxs:
            if (line not in full_file_func) and (line.lstrip() != _dl_line):
                if "TESTING_MOCKED_DATALOADERS" not in line:
                    new_full_example_parts.append(snake_case_    )
                    passed_idxs.append(snake_case_    )

    # Finally, get the overall diff
    UpperCamelCase				:   Any          =							[line for line in new_feature_code if line not in new_full_example_parts]
    if secondary_filename is not None:
        UpperCamelCase				:   Tuple          =							[line for line in full_file_contents if line not in secondary_file_func]
        UpperCamelCase				:   Optional[Any]          =							[line for line in diff_from_example if line not in diff_from_two]

    return diff_from_example










"""simple docstring"""




import argparse
import os

import torch
from torchvision.datasets.utils import download_url

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, TransformeraDModel


__A :		int      							=    {512: '''DiT-XL-2-512x512.pt''', 256: '''DiT-XL-2-256x256.pt'''}
def 					A_			(		snake_case_     :       Dict    ):







    '''simple docstring'''
    UpperCamelCase				:   Any          =							f'pretrained_models/{model_name}'
    if not os.path.isfile(snake_case_    ):
        os.makedirs("""pretrained_models"""						,exist_ok=snake_case_    )
        UpperCamelCase				:   Dict          =							f'https://dl.fbaipublicfiles.com/DiT/models/{model_name}'
        download_url(snake_case_						,"""pretrained_models"""    )
    UpperCamelCase				:   List[Any]          =							torch.load(snake_case_						,map_location=lambda snake_case_						,snake_case_    : storage    )
    return model
def 					A_			(		snake_case_     :       List[str]    ):







    '''simple docstring'''
    UpperCamelCase				:   int          =							download_model(pretrained_models[args.image_size]    )

    UpperCamelCase				:   str          =							state_dict["""x_embedder.proj.weight"""]
    UpperCamelCase				:   Optional[Any]          =							state_dict["""x_embedder.proj.bias"""]
    state_dict.pop("""x_embedder.proj.weight"""    )
    state_dict.pop("""x_embedder.proj.bias"""    )

    for depth in range(2_8    ):
        UpperCamelCase				:   Optional[int]          =							state_dict[
            """t_embedder.mlp.0.weight"""
        ]
        UpperCamelCase				:   Optional[Any]          =							state_dict[
            """t_embedder.mlp.0.bias"""
        ]
        UpperCamelCase				:   str          =							state_dict[
            """t_embedder.mlp.2.weight"""
        ]
        UpperCamelCase				:   Optional[int]          =							state_dict[
            """t_embedder.mlp.2.bias"""
        ]
        UpperCamelCase				:   int          =							state_dict[
            """y_embedder.embedding_table.weight"""
        ]

        UpperCamelCase				:   List[Any]          =							state_dict[
            f'blocks.{depth}.adaLN_modulation.1.weight'
        ]
        UpperCamelCase				:   Any          =							state_dict[
            f'blocks.{depth}.adaLN_modulation.1.bias'
        ]

        UpperCamelCase       ,							UpperCamelCase       ,							UpperCamelCase				:   Dict          =							torch.chunk(state_dict[f'blocks.{depth}.attn.qkv.weight']						,3						,dim=0    )
        UpperCamelCase       ,							UpperCamelCase       ,							UpperCamelCase				:   Dict          =							torch.chunk(state_dict[f'blocks.{depth}.attn.qkv.bias']						,3						,dim=0    )

        UpperCamelCase				:   int          =							q
        UpperCamelCase				:   Tuple          =							q_bias
        UpperCamelCase				:   List[Any]          =							k
        UpperCamelCase				:   int          =							k_bias
        UpperCamelCase				:   List[str]          =							v
        UpperCamelCase				:   str          =							v_bias

        UpperCamelCase				:   Any          =							state_dict[
            f'blocks.{depth}.attn.proj.weight'
        ]
        UpperCamelCase				:   Optional[Any]          =							state_dict[f'blocks.{depth}.attn.proj.bias']

        UpperCamelCase				:   str          =							state_dict[f'blocks.{depth}.mlp.fc1.weight']
        UpperCamelCase				:   Tuple          =							state_dict[f'blocks.{depth}.mlp.fc1.bias']
        UpperCamelCase				:   str          =							state_dict[f'blocks.{depth}.mlp.fc2.weight']
        UpperCamelCase				:   Tuple          =							state_dict[f'blocks.{depth}.mlp.fc2.bias']

        state_dict.pop(f'blocks.{depth}.attn.qkv.weight'    )
        state_dict.pop(f'blocks.{depth}.attn.qkv.bias'    )
        state_dict.pop(f'blocks.{depth}.attn.proj.weight'    )
        state_dict.pop(f'blocks.{depth}.attn.proj.bias'    )
        state_dict.pop(f'blocks.{depth}.mlp.fc1.weight'    )
        state_dict.pop(f'blocks.{depth}.mlp.fc1.bias'    )
        state_dict.pop(f'blocks.{depth}.mlp.fc2.weight'    )
        state_dict.pop(f'blocks.{depth}.mlp.fc2.bias'    )
        state_dict.pop(f'blocks.{depth}.adaLN_modulation.1.weight'    )
        state_dict.pop(f'blocks.{depth}.adaLN_modulation.1.bias'    )

    state_dict.pop("""t_embedder.mlp.0.weight"""    )
    state_dict.pop("""t_embedder.mlp.0.bias"""    )
    state_dict.pop("""t_embedder.mlp.2.weight"""    )
    state_dict.pop("""t_embedder.mlp.2.bias"""    )
    state_dict.pop("""y_embedder.embedding_table.weight"""    )

    UpperCamelCase				:   List[str]          =							state_dict["""final_layer.adaLN_modulation.1.weight"""]
    UpperCamelCase				:   str          =							state_dict["""final_layer.adaLN_modulation.1.bias"""]
    UpperCamelCase				:   Tuple          =							state_dict["""final_layer.linear.weight"""]
    UpperCamelCase				:   List[Any]          =							state_dict["""final_layer.linear.bias"""]

    state_dict.pop("""final_layer.linear.weight"""    )
    state_dict.pop("""final_layer.linear.bias"""    )
    state_dict.pop("""final_layer.adaLN_modulation.1.weight"""    )
    state_dict.pop("""final_layer.adaLN_modulation.1.bias"""    )

    # DiT XL/2
    UpperCamelCase				:   int          =							TransformeraDModel(
        sample_size=args.image_size // 8						,num_layers=2_8						,attention_head_dim=7_2						,in_channels=4						,out_channels=8						,patch_size=2						,attention_bias=snake_case_						,num_attention_heads=1_6						,activation_fn="""gelu-approximate"""						,num_embeds_ada_norm=1_0_0_0						,norm_type="""ada_norm_zero"""						,norm_elementwise_affine=snake_case_						,)
    transformer.load_state_dict(snake_case_						,strict=snake_case_    )

    UpperCamelCase				:   str          =							DDIMScheduler(
        num_train_timesteps=1_0_0_0						,beta_schedule="""linear"""						,prediction_type="""epsilon"""						,clip_sample=snake_case_						,)

    UpperCamelCase				:   Optional[Any]          =							AutoencoderKL.from_pretrained(args.vae_model    )

    UpperCamelCase				:   List[Any]          =							DiTPipeline(transformer=snake_case_						,vae=snake_case_						,scheduler=snake_case_    )

    if args.save:
        pipeline.save_pretrained(args.checkpoint_path    )


if __name__ == "__main__":
  __A :		Any      							=    argparse.ArgumentParser()

  parser.add_argument(
      '''--image_size''',
      default=256,
      type=int,
      required=False,
      help='''Image size of pretrained model, either 256 or 512.''',
  )
  parser.add_argument(
      '''--vae_model''',
      default='''stabilityai/sd-vae-ft-ema''',
      type=str,
      required=False,
      help='''Path to pretrained VAE model, either stabilityai/sd-vae-ft-mse or stabilityai/sd-vae-ft-ema.''',
  )
  parser.add_argument(
      '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted pipeline or not.'''
  )
  parser.add_argument(
      '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the output pipeline.'''
  )

  __A :		Dict      							=    parser.parse_args()
  main(args)




