from itertools import permutations





def 						__lowerCamelCase				(UpperCAmelCase__  :				Dict	):
					if num[3] % 2 != 0:
										return False

					if (num[2] + num[3] + num[4]) % 3 != 0:
										return False

					if num[5] % 5 != 0:
										return False

					SCREAMING_SNAKE_CASE           =							[7, 1_1, 1_3, 1_7]
					for i, test in enumerate(__lowercase	):
										if (num[i + 4] * 1_0_0 + num[i + 5] * 1_0 + num[i + 6]) % test != 0:
															return False
					return True





def 						__lowerCamelCase				(UpperCAmelCase__  :				Union[str, Any] = 1_0	):
					return sum(
					    int("".join(map(__lowercase   ,			__lowercase	)	)	)
					    for num in permutations(range(__lowercase	)	)
					    if is_substring_divisible(__lowercase	)	)


if __name__ == "__main__":
							print(f"""{solution() = }""")






"""simple docstring"""






import argparse
import math
import os
from copy import deepcopy

import torch
from audio_diffusion.models import DiffusionAttnUnetaD
from diffusion import sampling
from torch import nn

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel


__SCREAMING_SNAKE_CASE							=     {
    'gwf-440k': {
        'url': 'https://model-server.zqevans2.workers.dev/gwf-440k.ckpt',
        'sample_rate': 48_000,
        'sample_size': 65_536,
    },
    'jmann-small-190k': {
        'url': 'https://model-server.zqevans2.workers.dev/jmann-small-190k.ckpt',
        'sample_rate': 48_000,
        'sample_size': 65_536,
    },
    'jmann-large-580k': {
        'url': 'https://model-server.zqevans2.workers.dev/jmann-large-580k.ckpt',
        'sample_rate': 48_000,
        'sample_size': 131_072,
    },
    'maestro-uncond-150k': {
        'url': 'https://model-server.zqevans2.workers.dev/maestro-uncond-150k.ckpt',
        'sample_rate': 16_000,
        'sample_size': 65_536,
    },
    'unlocked-uncond-250k': {
        'url': 'https://model-server.zqevans2.workers.dev/unlocked-uncond-250k.ckpt',
        'sample_rate': 16_000,
        'sample_size': 65_536,
    },
    'honk-140k': {
        'url': 'https://model-server.zqevans2.workers.dev/honk-140k.ckpt',
        'sample_rate': 16_000,
        'sample_size': 65_536,
    },
}




def       A_  (				__lowercase						,	__lowercase	):
 return torch.atana(__lowercase						,	__lowercase	) / math.pi * 2




def       A_  (				__lowercase	):
 UpperCamelCase_     :					Dict   	=torch.sin(t * math.pi / 2	) ** 2
 UpperCamelCase_     :					Union[str, Any]   	=(1 - sigma**2) ** 0.5
 return alpha_sigma_to_t(__lowercase						,	__lowercase	)





class 				a__					(						A__       ):
     pass





class 				a__					(						nn.Module       ):



     def __init__(							self :Any      ,  _lowerCamelCase :Tuple   ):







      '''simple docstring'''



      super().__init__()

      UpperCamelCase_     :					Optional[int]   	=DiffusionAttnUnetaD(_lowerCamelCase      ,  n_attn_layers=4   )
      UpperCamelCase_     :					List[str]   	=deepcopy(self.diffusion   )
      UpperCamelCase_     :					int   	=torch.quasirandom.SobolEngine(1      ,  scramble=_lowerCamelCase   )




def       A_  (				__lowercase	):
 UpperCamelCase_     :					List[str]   	=MODELS_MAP[model_name]['url']
 os.system(F'''wget {url} ./'''	)

 return F'''./{model_name}.ckpt'''


__SCREAMING_SNAKE_CASE							=     {
    '1': 'resnets.0',
    '2': 'attentions.0',
    '3': 'resnets.1',
    '4': 'attentions.1',
    '5': 'resnets.2',
    '6': 'attentions.2',
}
__SCREAMING_SNAKE_CASE							=     {
    '8': 'resnets.0',
    '9': 'attentions.0',
    '10': 'resnets.1',
    '11': 'attentions.1',
    '12': 'resnets.2',
    '13': 'attentions.2',
}
__SCREAMING_SNAKE_CASE							=     {
    '1': 'resnets.0',
    '2': 'attentions.0',
    '3': 'resnets.1',
    '4': 'attentions.1',
    '5': 'resnets.2',
    '6': 'attentions.2',
    '8': 'resnets.3',
    '9': 'attentions.3',
    '10': 'resnets.4',
    '11': 'attentions.4',
    '12': 'resnets.5',
    '13': 'attentions.5',
}
__SCREAMING_SNAKE_CASE							=     {
    '0': 'resnets.0',
    '1': 'resnets.1',
    '2': 'resnets.2',
    '4': 'resnets.0',
    '5': 'resnets.1',
    '6': 'resnets.2',
}

__SCREAMING_SNAKE_CASE							=     {
    'skip': 'conv_skip',
    'main.0': 'conv_1',
    'main.1': 'group_norm_1',
    'main.3': 'conv_2',
    'main.4': 'group_norm_2',
}

__SCREAMING_SNAKE_CASE							=     {
    'norm': 'group_norm',
    'qkv_proj': ['query', 'key', 'value'],
    'out_proj': ['proj_attn'],
}




def       A_  (				__lowercase	):
 if name.startswith('skip'	):
  return name.replace('skip'						,	RES_CONV_MAP['skip']	)

 # name has to be of format main.{digit}
 if not name.startswith('main.'	):
  raise ValueError(F'''ResConvBlock error with {name}'''	)

 return name.replace(name[:6]						,	RES_CONV_MAP[name[:6]]	)




def       A_  (				__lowercase	):
 for key, value in ATTN_MAP.items():
  if name.startswith(__lowercase	) and not isinstance(__lowercase						,	__lowercase	):
   return name.replace(__lowercase						,	__lowercase	)
  elif name.startswith(__lowercase	):
   return [name.replace(__lowercase						,	__lowercase	) for v in value]
 raise ValueError(F'''Attn error with {name}'''	)




def       A_  (				__lowercase						,	__lowercase=13	):
 UpperCamelCase_     :					Optional[Any]   	=input_string

 if string.split('.'	)[0] == "timestep_embed":
  return string.replace('timestep_embed'						,	'time_proj'	)

 UpperCamelCase_     :					Tuple   	=0
 if string.startswith('net.3.'	):
  depth += 1
  UpperCamelCase_     :					str   	=string[6:]
 elif string.startswith('net.'	):
  UpperCamelCase_     :					List[str]   	=string[4:]

 while string.startswith('main.7.'	):
  depth += 1
  UpperCamelCase_     :					Optional[int]   	=string[7:]

 if string.startswith('main.'	):
  UpperCamelCase_     :					Union[str, Any]   	=string[5:]

 # mid block
 if string[:2].isdigit():
  UpperCamelCase_     :					List[str]   	=string[:2]
  UpperCamelCase_     :					Any   	=string[2:]
 else:
  UpperCamelCase_     :					List[Any]   	=string[0]
  UpperCamelCase_     :					Tuple   	=string[1:]

 if depth == max_depth:
  UpperCamelCase_     :					Optional[Any]   	=MID_NUM_TO_LAYER[layer_num]
  UpperCamelCase_     :					Optional[int]   	='mid_block'
 elif depth > 0 and int(__lowercase	) < 7:
  UpperCamelCase_     :					int   	=DOWN_NUM_TO_LAYER[layer_num]
  UpperCamelCase_     :					Dict   	=F'''down_blocks.{depth}'''
 elif depth > 0 and int(__lowercase	) > 7:
  UpperCamelCase_     :					Optional[Any]   	=UP_NUM_TO_LAYER[layer_num]
  UpperCamelCase_     :					Tuple   	=F'''up_blocks.{max_depth - depth - 1}'''
 elif depth == 0:
  UpperCamelCase_     :					Optional[int]   	=DEPTH_0_TO_LAYER[layer_num]
  UpperCamelCase_     :					List[str]   	=F'''up_blocks.{max_depth - 1}''' if int(__lowercase	) > 3 else 'down_blocks.0'

 if not string_left.startswith('.'	):
  raise ValueError(F'''Naming error with {input_string} and string_left: {string_left}.'''	)

 UpperCamelCase_     :					Union[str, Any]   	=string_left[1:]

 if "resnets" in new_layer:
  UpperCamelCase_     :					Dict   	=convert_resconv_naming(__lowercase	)
 elif "attentions" in new_layer:
  UpperCamelCase_     :					List[Any]   	=convert_attn_naming(__lowercase	)
  UpperCamelCase_     :					str   	=new_string_left

 if not isinstance(__lowercase						,	__lowercase	):
  UpperCamelCase_     :					int   	=prefix + '.' + new_layer + '.' + string_left
 else:
  UpperCamelCase_     :					List[Any]   	=[prefix + '.' + new_layer + '.' + s for s in string_left]
 return new_string




def       A_  (				__lowercase	):
 UpperCamelCase_     :					Union[str, Any]   	={}
 for k, v in state_dict.items():
  if k.endswith('kernel'	):
   # up- and downsample layers, don't have trainable weights
   continue

  UpperCamelCase_     :					Optional[Any]   	=rename(__lowercase	)

  # check if we need to transform from Conv => Linear for attention
  if isinstance(__lowercase						,	__lowercase	):
   UpperCamelCase_     :					Union[str, Any]   	=transform_conv_attns(__lowercase						,	__lowercase						,	__lowercase	)
  else:
   UpperCamelCase_     :					Union[str, Any]   	=v

 return new_state_dict




def       A_  (				__lowercase						,	__lowercase						,	__lowercase	):
 if len(__lowercase	) == 1:
  if len(v.shape	) == 3:
   # weight
   UpperCamelCase_     :					Tuple   	=v[:, :, 0]
  else:
   # bias
   UpperCamelCase_     :					Any   	=v
 else:
  # qkv matrices
  UpperCamelCase_     :					Tuple   	=v.shape[0]
  UpperCamelCase_     :					Optional[Any]   	=trippled_shape // 3
  for i in range(3	):
   if len(v.shape	) == 3:
    UpperCamelCase_     :					str   	=v[i * single_shape : (i + 1) * single_shape, :, 0]
   else:
    UpperCamelCase_     :					List[str]   	=v[i * single_shape : (i + 1) * single_shape]
 return new_state_dict




def       A_  (				__lowercase	):
 UpperCamelCase_     :					int   	=torch.device('cuda' if torch.cuda.is_available() else 'cpu'	)

 UpperCamelCase_     :					Dict   	=args.model_path.split('/'	)[-1].split('.'	)[0]
 if not os.path.isfile(args.model_path	):
  assert (
      model_name == args.model_path
  ), F'''Make sure to provide one of the official model names {MODELS_MAP.keys()}'''
  UpperCamelCase_     :					Optional[Any]   	=download(__lowercase	)

 UpperCamelCase_     :					str   	=MODELS_MAP[model_name]['sample_rate']
 UpperCamelCase_     :					Any   	=MODELS_MAP[model_name]['sample_size']

 UpperCamelCase_     :					str   	=Object()
 UpperCamelCase_     :					List[str]   	=sample_size
 UpperCamelCase_     :					Optional[int]   	=sample_rate
 UpperCamelCase_     :					Tuple   	=0

 UpperCamelCase_     :					Optional[int]   	=UNetaDModel(sample_size=__lowercase						,	sample_rate=__lowercase	)
 UpperCamelCase_     :					Union[str, Any]   	=diffusers_model.state_dict()

 UpperCamelCase_     :					Any   	=DiffusionUncond(__lowercase	)
 orig_model.load_state_dict(torch.load(args.model_path						,	map_location=__lowercase	)['state_dict']	)
 UpperCamelCase_     :					List[str]   	=orig_model.diffusion_ema.eval()
 UpperCamelCase_     :					Tuple   	=orig_model.state_dict()
 UpperCamelCase_     :					Optional[int]   	=rename_orig_weights(__lowercase	)

 UpperCamelCase_     :					Any   	=set(renamed_state_dict.keys()	) - set(diffusers_state_dict.keys()	)
 UpperCamelCase_     :					Tuple   	=set(diffusers_state_dict.keys()	) - set(renamed_state_dict.keys()	)

 assert len(__lowercase	) == 0, F'''Problem with {renamed_minus_diffusers}'''
 assert all(k.endswith('kernel'	) for k in list(__lowercase	)	), F'''Problem with {diffusers_minus_renamed}'''

 for key, value in renamed_state_dict.items():
  assert (
      diffusers_state_dict[key].squeeze().shape == value.squeeze().shape
  ), F'''Shape for {key} doesn\'t match. Diffusers: {diffusers_state_dict[key].shape} vs. {value.shape}'''
  if key == "time_proj.weight":
   UpperCamelCase_     :					List[str]   	=value.squeeze()

  UpperCamelCase_     :					str   	=value

 diffusers_model.load_state_dict(__lowercase	)

 UpperCamelCase_     :					Optional[Any]   	=1_00
 UpperCamelCase_     :					List[str]   	=33

 UpperCamelCase_     :					Any   	=IPNDMScheduler(num_train_timesteps=__lowercase	)

 UpperCamelCase_     :					List[Any]   	=torch.manual_seed(__lowercase	)
 UpperCamelCase_     :					List[str]   	=torch.randn([1, 2, config.sample_size]						,	generator=__lowercase	).to(__lowercase	)

 UpperCamelCase_     :					str   	=torch.linspace(1						,	0						,	steps + 1						,	device=__lowercase	)[:-1]
 UpperCamelCase_     :					Dict   	=get_crash_schedule(__lowercase	)

 UpperCamelCase_     :					Optional[Any]   	=DanceDiffusionPipeline(unet=__lowercase						,	scheduler=__lowercase	)

 UpperCamelCase_     :					Optional[Any]   	=torch.manual_seed(33	)
 UpperCamelCase_     :					Optional[Any]   	=pipe(num_inference_steps=__lowercase						,	generator=__lowercase	).audios

 UpperCamelCase_     :					Dict   	=sampling.iplms_sample(__lowercase						,	__lowercase						,	__lowercase						,	{}	)
 UpperCamelCase_     :					List[Any]   	=generated.clamp(-1						,	1	)

 UpperCamelCase_     :					Optional[int]   	=(generated - audio).abs().sum()
 UpperCamelCase_     :					Union[str, Any]   	=(generated - audio).abs().max()

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

 print('Diff sum'						,	__lowercase	)
 print('Diff max'						,	__lowercase	)

 assert diff_max < 1e-3, F'''Diff max: {diff_max} is too much :-/'''

 print(F'''Conversion for {model_name} successful!'''	)


if __name__ == "__main__":
 __SCREAMING_SNAKE_CASE							=     argparse.ArgumentParser()

 parser.add_argument('--model_path', default=None, type=str, required=True, help='Path to the model to convert.')
 parser.add_argument(
     '--save', default=True, type=bool, required=False, help='Whether to save the converted model or not.'
 )
 parser.add_argument('--checkpoint_path', default=None, type=str, required=True, help='Path to the output model.')
 __SCREAMING_SNAKE_CASE							=     parser.parse_args()

 main(args)
