


import argparse
import json
import os

import torch
from torch import nn

from transformers import NllbMoeConfig, NllbMoeModel
from transformers.modeling_utils import dtype_byte_size
from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME
def      snake_case_						( __lowercase     :   List[Any]       ):
   UpperCAmelCase_							:      Tuple						=   [
       '''encoder.version''',
       '''decoder.version''',
       '''model.encoder.version''',
       '''model.decoder.version''',
       '''decoder.output_projection.weight''',
       '''_float_tensor''',
       '''encoder.embed_positions._float_tensor''',
       '''decoder.embed_positions._float_tensor''',
   ]
   for k in ignore_keys:
      state_dict.pop(__lowercase			,       __lowercase       )
def      snake_case_						( __lowercase     :   List[str]       ):
   UpperCAmelCase_							:      Any						=   emb.weight.shape
   UpperCAmelCase_							:      str						=   nn.Linear(__lowercase			,       __lowercase			,       bias=__lowercase       )
   UpperCAmelCase_							:      int						=   emb.weight.data
   return lin_layer
def      snake_case_						( __lowercase     :   Any			,       __lowercase     :   Any=None       ):
   UpperCAmelCase_							:      Optional[Any]						=   {}
   for old_key in state_dict.keys():
      UpperCAmelCase_							:      Optional[Any]						=   old_key
      if "moe_layer.experts." in key:
         if expert_idx is not None:
            UpperCAmelCase_							:      Tuple						=   key.replace('''moe_layer.experts.0'''			,       F'''ffn.experts.expert_{expert_idx}'''       )
         else:
            UpperCAmelCase_							:      Union[str, Any]						=   key.replace('''moe_layer.experts.'''			,       '''ffn.experts.expert_'''       )
      if "gate" in key:
         UpperCAmelCase_							:      Dict						=   key.replace('''.moe_layer.gate.wg'''			,       '''.ffn.router.classifier'''       )
      if "fc2" and "experts" not in key:
         UpperCAmelCase_							:      Optional[int]						=   key.replace('''.fc2.'''			,       '''.ffn.fc2.'''       )
      if "fc1" and "experts" not in key:
         UpperCAmelCase_							:      Any						=   key.replace('''.fc1.'''			,       '''.ffn.fc1.'''       )
      if ".encoder_attn." in key:
         UpperCAmelCase_							:      Optional[int]						=   key.replace('''.encoder_attn.'''			,       '''.cross_attention.'''       )
      if "encoder_attn_layer_norm" in key:
         UpperCAmelCase_							:      Optional[Any]						=   key.replace('''encoder_attn_layer_norm'''			,       '''cross_attention_layer_norm'''       )
      if "final_layer_norm" in key:
         UpperCAmelCase_							:      Optional[int]						=   key.replace('''final_layer_norm'''			,       '''ff_layer_norm'''       )
      UpperCAmelCase_							:      Union[str, Any]						=   state_dict[old_key]
   return new_dict
def      snake_case_						( __lowercase     :   int			,       __lowercase     :   List[Any]			,       __lowercase     :   List[str]			,       __lowercase     :   Union[str, Any]			,       __lowercase     :   Any = WEIGHTS_NAME       ):
   UpperCAmelCase_							:      List[str]						=   []
   UpperCAmelCase_							:      List[Any]						=   0
   os.makedirs(__lowercase			,       exist_ok=__lowercase       )

   for expert in range(__lowercase       ):
      UpperCAmelCase_							:      str						=   switch_checkpoint_path + F'''-rank-{expert}.pt'''
      if os.path.isfile(__lowercase       ):
         UpperCAmelCase_							:      Optional[int]						=   torch.load(__lowercase       )['''model''']
         remove_ignore_keys_(__lowercase       )
         UpperCAmelCase_							:      int						=   rename_fairseq_keys(__lowercase			,       __lowercase       )
         UpperCAmelCase_							:      Dict						=   os.path.join(
             __lowercase			,       weights_name.replace('''.bin'''			,       F'''-{len(__lowercase       )+1:05d}-of-???.bin'''       )       )
         torch.save(__lowercase			,       __lowercase       )
         sharded_state_dicts.append(expert_state.keys()       )
         total_size += sum([value.numel() for key, value in expert_state.items()]       ) * dtype_byte_size(
             expert_state[list(__lowercase       )[0]].dtype       )

    # Add the last block
   UpperCAmelCase_							:      Dict						=   os.path.join(__lowercase			,       weights_name.replace('''.bin'''			,       F'''-{len(__lowercase       )+1:05d}-of-???.bin'''       )       )
   UpperCAmelCase_							:      Tuple						=   torch.load(switch_checkpoint_path + '''-shared.pt'''       )['''model''']
   remove_ignore_keys_(__lowercase       )
   UpperCAmelCase_							:      Tuple						=   rename_fairseq_keys(__lowercase			,       __lowercase       )
   UpperCAmelCase_							:      Tuple						=   shared_weights['''decoder.embed_tokens.weight''']
   sharded_state_dicts.append(shared_weights.keys()       )

   # If we only have the shared weights (dummy model/experts saved on the same file)
   if len(__lowercase       ) == 1:
      UpperCAmelCase_							:      Any						=   os.path.join(__lowercase			,       __lowercase       )
      torch.save(__lowercase			,       __lowercase       )
      return {weights_name: sharded_state_dicts[0]}, None
   else:
      torch.save(__lowercase			,       __lowercase       )
   # Otherwise, let's build the index
   UpperCAmelCase_							:      Optional[Any]						=   {}
   for idx, shard in enumerate(__lowercase       ):
      UpperCAmelCase_							:      Tuple						=   weights_name.replace('''.bin'''			,       F'''-{idx+1:05d}-of-{len(__lowercase       ):05d}.bin'''       )
      UpperCAmelCase_							:      int						=   os.path.join(__lowercase			,       weights_name.replace('''.bin'''			,       F'''-{idx+1:05d}-of-???.bin'''       )       )
      os.rename(__lowercase			,       os.path.join(__lowercase			,       __lowercase       )       )
      for key in shard:
         UpperCAmelCase_							:      int						=   shard_file

    # Add the metadata
   UpperCAmelCase_							:      List[Any]						=   {'''total_size''': total_size}
   UpperCAmelCase_							:      Any						=   {'''metadata''': metadata, '''weight_map''': weight_map}

   with open(os.path.join(__lowercase			,       __lowercase       )			,       '''w'''			,       encoding='''utf-8'''       ) as f:
      UpperCAmelCase_							:      Union[str, Any]						=   json.dumps(__lowercase			,       indent=2			,       sort_keys=__lowercase       ) + '''\n'''
      f.write(__lowercase       )

   return metadata, index


if __name__ == "__main__":
       __UpperCamelCase				:  Optional[int]      					=							argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '--nllb_moe_checkpoint_path',
           default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000',
           type=str,
           required=False,
           help='Path to a directory containing a folder per layer. Follows the original Google format.',
       )
       parser.add_argument('--dtype', default='float32', type=str, required=False, help='dtype of the saved model')
       parser.add_argument(
           '--pytorch_dump_folder_path',
           default='/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b',
           type=str,
           required=False,
           help='Path to the output pytorch model.',
       )
       __UpperCamelCase				:  int      					=							parser.parse_args()
       __UpperCamelCase				:  Dict      					=							shard_on_the_fly(
           args.nllb_moe_checkpoint_path,
           args.pytorch_dump_folder_path,
           128,
           args.dtype,
       )

       __UpperCamelCase				:  Optional[int]      					=							NllbMoeConfig.from_pretrained(
           'facebook/nllb-200-3.3B', encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128
       )
       config.save_pretrained(args.pytorch_dump_folder_path)
       __UpperCamelCase				:  List[Any]      					=							NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path)
       print('Done')
       model.save_pretrained(args.pytorch_dump_folder_path)



import math

import qiskit
def      snake_case_						( __lowercase = 1			,       __lowercase = 1			,       __lowercase = 1       ):
   if (
       isinstance(__lowercase			,       __lowercase       )
       or isinstance(__lowercase			,       __lowercase       )
       or isinstance(__lowercase			,       __lowercase       )
   ):
      raise TypeError('''inputs must be integers.'''       )

   if (input_a < 0) or (input_a < 0) or (carry_in < 0):
      raise ValueError('''inputs must be positive.'''       )

   if (
       (math.floor(__lowercase       ) != input_a)
       or (math.floor(__lowercase       ) != input_a)
       or (math.floor(__lowercase       ) != carry_in)
   ):
      raise ValueError('''inputs must be exact integers.'''       )

   if (input_a > 2) or (input_a > 2) or (carry_in > 2):
      raise ValueError('''inputs must be less or equal to 2.'''       )

   # build registers
   UpperCAmelCase_							:      Any						=   qiskit.QuantumRegister(4			,       '''qr'''       )
   UpperCAmelCase_							:      List[str]						=   qiskit.ClassicalRegister(2			,       '''cr'''       )
   # list the entries
   UpperCAmelCase_							:      Any						=   [input_a, input_a, carry_in]

   UpperCAmelCase_							:      Dict						=   qiskit.QuantumCircuit(__lowercase			,       __lowercase       )

   for i in range(0			,       3       ):
      if entry[i] == 2:
         quantum_circuit.h(__lowercase       )  # for hadamard entries
      elif entry[i] == 1:
         quantum_circuit.x(__lowercase       )  # for 1 entries
      elif entry[i] == 0:
         quantum_circuit.i(__lowercase       )  # for 0 entries

    # build the circuit
   quantum_circuit.ccx(0			,       1			,       3       )  # ccx = toffoli gate
   quantum_circuit.cx(0			,       1       )
   quantum_circuit.ccx(1			,       2			,       3       )
   quantum_circuit.cx(1			,       2       )
   quantum_circuit.cx(0			,       1       )

   quantum_circuit.measure([2, 3]			,       __lowercase       )  # measure the last two qbits

   UpperCAmelCase_							:      Optional[int]						=   qiskit.Aer.get_backend('''aer_simulator'''       )
   UpperCAmelCase_							:      List[str]						=   qiskit.execute(__lowercase			,       __lowercase			,       shots=1_0_0_0       )

   return job.result().get_counts(__lowercase       )


if __name__ == "__main__":
       print(F'Total sum count for state is: {quantum_full_adder(1, 1, 1)}')