





import argparse
import json
import os
from collections import OrderedDict

import numpy as np
import tensorflow as tf
import torch


def 			SCREAMING_SNAKE_CASE (			_UpperCAmelCase					)     ->	Optional[Any]:
    lowerCamelCase__       : Union[str, Any]   					=						os.path.join(args.tf_model_dir		,      'parameters.json'					)
    lowerCamelCase__       : Optional[int]   					=						json.loads(open(_UpperCAmelCase					).read()					)
    if not params:
        raise ValueError(
            F"""It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file."""					)
    if not args.output.endswith('.pt'					):
        lowerCamelCase__       : Dict   					=						args.output + '.pt'
    lowerCamelCase__       : Optional[int]   					=						OrderedDict()
    with tf.device('/CPU:0'					):
        lowerCamelCase__       : List[str]   					=						tf.train.load_checkpoint(args.tf_model_dir					)
        lowerCamelCase__       : Optional[int]   					=						reader.get_variable_to_shape_map()
        for key_name in shapes.keys():
            lowerCamelCase__       : Optional[Any]   					=						reader.get_tensor(_UpperCAmelCase					).astype(np.floataa					)
            if key_name.endswith('/adam_m'					) or key_name.endswith('/adam_v'					):
                continue
            if key_name.startswith('pasts/'					):
                if key_name.startswith('pasts/mlp'					):
                    lowerCamelCase__       : List[str]   					=						int(key_name[9]					)
                elif key_name.startswith('pasts/out'					):
                    lowerCamelCase__       : Optional[Any]   					=						8
                lowerCamelCase__       : Optional[Any]   					=						'model.sqout.%d.weight' % (player * 2)  # enter to nn.Sequencial with Tanh, so 2 at a time
                lowerCamelCase__       : str   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                lowerCamelCase__       : int   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/moe'					):
                lowerCamelCase__       : List[str]   					=						int(key_name[9:].split('/'					)[0]					)
                if key_name.endswith('/switch_gating/kernel'					):
                    lowerCamelCase__       : Dict   					=						'model.blocks.%d.feed_forward.mlp.router.classifier.weight' % player
                    lowerCamelCase__       : List[Any]   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/softmlp/kernel'					):
                    lowerCamelCase__       : Optional[Any]   					=						'model.blocks.%d.feed_forward.soft_bypass_mlp.weight' % player
                    lowerCamelCase__       : Any   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/wo/kernel'					) or key_name.endswith('/wi/kernel'					):
                    lowerCamelCase__       : List[str]   					=						key_name[-9:-7]
                    for i in range(16					):
                        lowerCamelCase__       : Any   					=						'model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight' % (player, i, nlayer)
                        lowerCamelCase__       : str   					=						(
                            vnp[i].transpose([1, 0]					).copy()
                        )  # In Mesh-Tensorflow, it is one array, so it is divided
                        lowerCamelCase__       : List[str]   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/mlp'					):
                lowerCamelCase__       : Union[str, Any]   					=						int(key_name[9:].split('/'					)[0]					)
                if key_name.endswith('/p1/kernel'					):
                    lowerCamelCase__       : Any   					=						'model.blocks.%d.feed_forward.mlp.wi.weight' % player
                    lowerCamelCase__       : Optional[int]   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Any   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/p1/bias'					):
                    lowerCamelCase__       : List[str]   					=						'model.blocks.%d.feed_forward.mlp.wi.bias' % player
                    lowerCamelCase__       : Optional[int]   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : Dict   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/p2/kernel'					):
                    lowerCamelCase__       : Optional[Any]   					=						'model.blocks.%d.feed_forward.mlp.wo.weight' % player
                    lowerCamelCase__       : str   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/p2/bias'					):
                    lowerCamelCase__       : Optional[Any]   					=						'model.blocks.%d.feed_forward.mlp.wo.bias' % player
                    lowerCamelCase__       : Optional[int]   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : List[str]   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/ln'					):
                lowerCamelCase__       : Tuple   					=						int(key_name[8:].split('/'					)[0]					)
                if key_name.endswith('/b'					):
                    lowerCamelCase__       : Optional[Any]   					=						'model.blocks.%d.feed_forward.norm.bias' % player
                    lowerCamelCase__       : str   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : Any   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/g'					):
                    lowerCamelCase__       : Union[str, Any]   					=						'model.blocks.%d.feed_forward.norm.weight' % player
                    lowerCamelCase__       : Union[str, Any]   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/att'					):
                lowerCamelCase__       : Union[str, Any]   					=						int(key_name[9:].split('/'					)[0]					)
                if key_name.endswith('/qkv/kernel'					):
                    lowerCamelCase__       : int   					=						vnp.copy()  # Compute same dimension as Mesh-tensorflow using einsum
                    lowerCamelCase__       : int   					=						state[:, 0, :, :]
                    lowerCamelCase__       : Optional[int]   					=						state[:, 1, :, :]
                    lowerCamelCase__       : Optional[int]   					=						state[:, 2, :, :]
                    lowerCamelCase__       : List[str]   					=						(
                        state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]]					)
                        .transpose([1, 0]					)
                        .copy()
                    )  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Tuple   					=						(
                        state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]]					)
                        .transpose([1, 0]					)
                        .copy()
                    )  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Optional[Any]   					=						(
                        state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]]					)
                        .transpose([1, 0]					)
                        .copy()
                    )  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : int   					=						'model.blocks.%d.self_attn.self_attn.q_proj.weight' % player
                    lowerCamelCase__       : Optional[int]   					=						torch.tensor(_UpperCAmelCase					)
                    lowerCamelCase__       : List[str]   					=						'model.blocks.%d.self_attn.self_attn.k_proj.weight' % player
                    lowerCamelCase__       : List[Any]   					=						torch.tensor(_UpperCAmelCase					)
                    lowerCamelCase__       : List[str]   					=						'model.blocks.%d.self_attn.self_attn.v_proj.weight' % player
                    lowerCamelCase__       : Dict   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/o/kernel'					):
                    lowerCamelCase__       : Optional[int]   					=						'model.blocks.%d.self_attn.self_attn.out_proj.weight' % player
                    lowerCamelCase__       : List[str]   					=						(
                        vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]]					).transpose([1, 0]					).copy()
                    )  # Mesh-Tensorflow is a diagonal matrix
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/an'					):
                lowerCamelCase__       : Optional[Any]   					=						int(key_name[8:].split('/'					)[0]					)
                if key_name.endswith('/b'					):
                    lowerCamelCase__       : int   					=						'model.blocks.%d.self_attn.norm.bias' % player
                    lowerCamelCase__       : Optional[int]   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : Optional[int]   					=						torch.tensor(_UpperCAmelCase					)
                elif key_name.endswith('/g'					):
                    lowerCamelCase__       : Optional[Any]   					=						'model.blocks.%d.self_attn.norm.weight' % player
                    lowerCamelCase__       : int   					=						vnp.copy()  # same because it is one dimensional
                    lowerCamelCase__       : int   					=						torch.tensor(_UpperCAmelCase					)
            elif (
                key_name.startswith('model/wte'					)
                or key_name.startswith('model/wpe'					)
                or key_name.startswith('model/ete'					)
            ):
                lowerCamelCase__       : List[Any]   					=						{'wte': 'embed_tokens', 'wpe': 'position_embeddings', 'ete': 'extra_position_embeddings'}[
                    key_name[-3:]
                ]
                lowerCamelCase__       : Optional[Any]   					=						'model.%s.weight' % nlayer
                lowerCamelCase__       : Tuple   					=						vnp.copy()  # same in embedded
                lowerCamelCase__       : Any   					=						torch.tensor(_UpperCAmelCase					)
                if key_name.startswith('model/wte'					):
                    lowerCamelCase__       : List[Any]   					=						'lm_head.weight'
                    lowerCamelCase__       : int   					=						vnp.copy()  # same in embedded
                    lowerCamelCase__       : Tuple   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name.startswith('model/wob'					):
                lowerCamelCase__       : Tuple   					=						'final_logits_bias'
                lowerCamelCase__       : str   					=						vnp.copy()  # same in embedded
                lowerCamelCase__       : Optional[Any]   					=						state.reshape((1, -1)					)
                lowerCamelCase__       : Any   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name == "model/dense/kernel":
                lowerCamelCase__       : Union[str, Any]   					=						'model.last_project.weight'
                lowerCamelCase__       : Optional[Any]   					=						vnp.transpose([1, 0]					).copy()  # Mesh-Tensorflow is a diagonal matrix
                lowerCamelCase__       : Optional[int]   					=						torch.tensor(_UpperCAmelCase					)
            elif key_name == "model/dense_1/bias":
                lowerCamelCase__       : List[Any]   					=						'model.last_project.bias'
                lowerCamelCase__       : Any   					=						vnp.copy()  # same because it is one dimensional
                lowerCamelCase__       : List[Any]   					=						torch.tensor(_UpperCAmelCase					)
    torch.save(_UpperCAmelCase		,      args.output					)


if __name__ == "__main__":
    _UpperCAmelCase							:			str        =							argparse.ArgumentParser(
        description="""model converter.""", formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("""--tf_model_dir""", metavar="""PATH""", type=str, required=True, help="""import model""")
    parser.add_argument("""--output""", metavar="""PATH""", type=str, required=True, help="""output model""")
    _UpperCAmelCase							:			Optional[Any]        =							parser.parse_args()
    convert_tf_gptsan_to_pt(args)












import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        FalconForCausalLM,
        FalconForQuestionAnswering,
        FalconForSequenceClassification,
        FalconForTokenClassification,
        FalconModel,
    )

class   lowerCAmelCase				:
 def __init__( self    : List[Any]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Any=3     ,	UpperCAmelCase    : List[str]=7     ,	UpperCAmelCase    : Any=True     ,	UpperCAmelCase    : str=True     ,	UpperCAmelCase    : Dict=False     ,	UpperCAmelCase    : Union[str, Any]=True     ,	UpperCAmelCase    : int=99     ,	UpperCAmelCase    : Tuple=32     ,	UpperCAmelCase    : int=5     ,	UpperCAmelCase    : str=4     ,	UpperCAmelCase    : Optional[Any]=37     ,	UpperCAmelCase    : Dict="gelu"     ,	UpperCAmelCase    : Optional[int]=0.1     ,	UpperCAmelCase    : Any=0.1     ,	UpperCAmelCase    : List[str]=512     ,	UpperCAmelCase    : Dict=16     ,	UpperCAmelCase    : Optional[int]=2     ,	UpperCAmelCase    : Optional[Any]=0.0_2     ,	UpperCAmelCase    : Dict=3     ,	UpperCAmelCase    : int=4     ,	UpperCAmelCase    : List[str]=None     ,	)     ->      Dict:
     lowerCamelCase__       : Optional[Any]   					=						parent
     lowerCamelCase__       : Union[str, Any]   					=						batch_size
     lowerCamelCase__       : int   					=						seq_length
     lowerCamelCase__       : Optional[Any]   					=						is_training
     lowerCamelCase__       : Any   					=						use_input_mask
     lowerCamelCase__       : Dict   					=						use_token_type_ids
     lowerCamelCase__       : Dict   					=						use_labels
     lowerCamelCase__       : Optional[Any]   					=						vocab_size
     lowerCamelCase__       : List[Any]   					=						hidden_size
     lowerCamelCase__       : List[Any]   					=						num_hidden_layers
     lowerCamelCase__       : Optional[int]   					=						num_attention_heads
     lowerCamelCase__       : Tuple   					=						intermediate_size
     lowerCamelCase__       : List[str]   					=						hidden_act
     lowerCamelCase__       : Optional[int]   					=						hidden_dropout_prob
     lowerCamelCase__       : Optional[Any]   					=						attention_probs_dropout_prob
     lowerCamelCase__       : Any   					=						max_position_embeddings
     lowerCamelCase__       : Any   					=						type_vocab_size
     lowerCamelCase__       : Any   					=						type_sequence_label_size
     lowerCamelCase__       : Tuple   					=						initializer_range
     lowerCamelCase__       : Union[str, Any]   					=						num_labels
     lowerCamelCase__       : str   					=						num_choices
     lowerCamelCase__       : int   					=						scope
 def 	A_      ( self    : Dict      )     ->      Any:
     lowerCamelCase__       : Optional[int]   					=						ids_tensor([self.batch_size, self.seq_length]     ,	self.vocab_size      )

     lowerCamelCase__       : str   					=						None
     if self.use_input_mask:
         lowerCamelCase__       : Tuple   					=						random_attention_mask([self.batch_size, self.seq_length]      )

     lowerCamelCase__       : Any   					=						None

     lowerCamelCase__       : Tuple   					=						None
     lowerCamelCase__       : Optional[Any]   					=						None
     lowerCamelCase__       : Optional[int]   					=						None
     if self.use_labels:
         lowerCamelCase__       : List[Any]   					=						ids_tensor([self.batch_size]     ,	self.type_sequence_label_size      )
         lowerCamelCase__       : List[Any]   					=						ids_tensor([self.batch_size, self.seq_length]     ,	self.num_labels      )
         lowerCamelCase__       : Tuple   					=						ids_tensor([self.batch_size]     ,	self.num_choices      )

     lowerCamelCase__       : Optional[int]   					=						self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 	A_      ( self    : Any      )     ->      Any:
     return FalconConfig(
         vocab_size=self.vocab_size     ,	hidden_size=self.hidden_size     ,	num_hidden_layers=self.num_hidden_layers     ,	num_attention_heads=self.num_attention_heads     ,	intermediate_size=self.intermediate_size     ,	hidden_act=self.hidden_act     ,	hidden_dropout_prob=self.hidden_dropout_prob     ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,	max_position_embeddings=self.max_position_embeddings     ,	type_vocab_size=self.type_vocab_size     ,	is_decoder=UpperCAmelCase     ,	initializer_range=self.initializer_range     ,	pad_token_id=1     ,	new_decoder_architecture=UpperCAmelCase     ,	)
 def 	A_      ( self    : Tuple     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Union[str, Any]      )     ->      Tuple:
     lowerCamelCase__       : Tuple   					=						FalconModel(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase      )
     lowerCamelCase__       : List[Any]   					=						model(UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : List[Any]     ,	UpperCAmelCase    : int     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Union[str, Any]     ,	)     ->      Optional[Any]:
     lowerCamelCase__       : Optional[int]   					=						True
     lowerCamelCase__       : Optional[Any]   					=						FalconModel(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : str   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	)
     lowerCamelCase__       : Union[str, Any]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	)
     lowerCamelCase__       : Dict   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape     ,	(self.batch_size, self.seq_length, self.hidden_size)      )
 def 	A_      ( self    : str     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : str     ,	)     ->      List[str]:
     lowerCamelCase__       : Optional[Any]   					=						FalconForCausalLM(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : int   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.vocab_size)      )
 def 	A_      ( self    : Union[str, Any]     ,	UpperCAmelCase    : Union[str, Any]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Tuple     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : str     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[Any]     ,	)     ->      List[str]:
     lowerCamelCase__       : int   					=						True
     lowerCamelCase__       : Optional[int]   					=						True
     lowerCamelCase__       : str   					=						FalconForCausalLM(config=UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()

     # first forward pass
     lowerCamelCase__       : List[str]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	use_cache=UpperCAmelCase     ,	)
     lowerCamelCase__       : Dict   					=						outputs.past_key_values

     # create hypothetical multiple next token and extent to next_input_ids
     lowerCamelCase__       : List[str]   					=						ids_tensor((self.batch_size, 3)     ,	config.vocab_size      )
     lowerCamelCase__       : List[Any]   					=						ids_tensor((self.batch_size, 3)     ,	vocab_size=2      )

     # append to next input_ids and
     lowerCamelCase__       : str   					=						torch.cat([input_ids, next_tokens]     ,	dim=-1      )
     lowerCamelCase__       : Union[str, Any]   					=						torch.cat([input_mask, next_mask]     ,	dim=-1      )

     lowerCamelCase__       : Union[str, Any]   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	output_hidden_states=UpperCAmelCase     ,	)['hidden_states'][0]
     lowerCamelCase__       : Tuple   					=						model(
         UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	encoder_hidden_states=UpperCAmelCase     ,	encoder_attention_mask=UpperCAmelCase     ,	past_key_values=UpperCAmelCase     ,	output_hidden_states=UpperCAmelCase     ,	)['hidden_states'][0]

     # select random slice
     lowerCamelCase__       : Dict   					=						ids_tensor((1,)     ,	output_from_past.shape[-1]      ).item()
     lowerCamelCase__       : List[Any]   					=						output_from_no_past[:, -3:, random_slice_idx].detach()
     lowerCamelCase__       : Dict   					=						output_from_past[:, :, random_slice_idx].detach()

     self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]      )

     # test that outputs are equal for slice
     self.parent.assertTrue(torch.allclose(UpperCAmelCase     ,	UpperCAmelCase     ,	atol=1e-3      )      )







 def 	A_      ( self    : Optional[Any]      )     ->      Optional[int]:
     lowerCamelCase__       : Any   					=						self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				(
         lowerCamelCase__
     )					,				
     )       : Union[str, Any]   					=						config_and_inputs
     lowerCamelCase__       : List[str]   					=						{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict


@require_torch
class   lowerCAmelCase				(   __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase,    unittest.TestCase     ):
 UpperCAmelCase__						=						(
     (
         FalconModel,
         FalconForCausalLM,
         FalconForSequenceClassification,
         FalconForTokenClassification,
         FalconForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase__						=						(FalconForCausalLM,) if is_torch_available() else ()
 UpperCAmelCase__						=						(
     {
         """feature-extraction""": FalconModel,
         """text-classification""": FalconForSequenceClassification,
         """text-generation""": FalconForCausalLM,
         """question-answering""": FalconForQuestionAnswering,
         """token-classification""": FalconForTokenClassification,
         """zero-shot""": FalconForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase__						=						False
 UpperCAmelCase__						=						False
 def 	A_      ( self    : List[str]      )     ->      List[Any]:
     lowerCamelCase__       : Union[str, Any]   					=						FalconModelTester(self      )
     lowerCamelCase__       : List[Any]   					=						ConfigTester(self     ,	config_class=UpperCAmelCase     ,	hidden_size=37      )
 def 	A_      ( self    : Optional[int]      )     ->      int:
     self.config_tester.run_common_tests()
 def 	A_      ( self    : Dict      )     ->      Dict:
     lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase      )
 def 	A_      ( self    : List[str]      )     ->      int:
     lowerCamelCase__					,				*lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
         lowerCamelCase__       : List[Any]   					=						alibi
         self.model_tester.create_and_check_model(UpperCAmelCase     ,	*UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]      )     ->      Optional[int]:
     lowerCamelCase__					,				lowerCamelCase__       : Any   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : int   					=						3
     lowerCamelCase__       : List[str]   					=						input_dict['input_ids']
     lowerCamelCase__       : Dict   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : Optional[Any]   					=						ids_tensor([self.model_tester.batch_size]     ,	self.model_tester.type_sequence_label_size      )
     lowerCamelCase__       : int   					=						FalconForSequenceClassification(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : List[str]   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.assertEqual(result.logits.shape     ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )
 def 	A_      ( self    : Optional[Any]      )     ->      Optional[Any]:
     lowerCamelCase__					,				lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : Tuple   					=						3
     lowerCamelCase__       : List[str]   					=						'single_label_classification'
     lowerCamelCase__       : Optional[int]   					=						input_dict['input_ids']
     lowerCamelCase__       : Union[str, Any]   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : List[Any]   					=						ids_tensor([self.model_tester.batch_size]     ,	self.model_tester.type_sequence_label_size      )
     lowerCamelCase__       : Union[str, Any]   					=						FalconForSequenceClassification(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : Any   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.assertEqual(result.logits.shape     ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )
 def 	A_      ( self    : Any      )     ->      Union[str, Any]:
     lowerCamelCase__					,				lowerCamelCase__       : List[Any]   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : List[str]   					=						input_dict['input_ids']
     lowerCamelCase__       : Optional[Any]   					=						FalconForCausalLM(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : Any   					=						model(UpperCAmelCase     ,	use_cache=UpperCAmelCase      )
     lowerCamelCase__       : int   					=						input_ids.shape[0]
     lowerCamelCase__       : str   					=						model._convert_to_rw_cache(result.past_key_values      )
     lowerCamelCase__       : Optional[Any]   					=						model._convert_cache_to_standard_format(UpperCAmelCase     ,	UpperCAmelCase      )
     for layer in range(len(UpperCAmelCase      )      ):
         for tensor_idx in range(2      ):
             self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3      )
             self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4      )
             self.assertTrue(
                 torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]      )      )
 def 	A_      ( self    : List[str]      )     ->      str:
     lowerCamelCase__					,				lowerCamelCase__       : int   					=						self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__       : Optional[Any]   					=						3
     lowerCamelCase__       : List[str]   					=						'multi_label_classification'
     lowerCamelCase__       : Optional[int]   					=						input_dict['input_ids']
     lowerCamelCase__       : Tuple   					=						input_ids.ne(1      ).to(UpperCAmelCase      )
     lowerCamelCase__       : int   					=						ids_tensor(
         [self.model_tester.batch_size, config.num_labels]     ,	self.model_tester.type_sequence_label_size      ).to(torch.float      )
     lowerCamelCase__       : Any   					=						FalconForSequenceClassification(UpperCAmelCase      )
     model.to(UpperCAmelCase      )
     model.eval()
     lowerCamelCase__       : Any   					=						model(UpperCAmelCase     ,	attention_mask=UpperCAmelCase     ,	labels=UpperCAmelCase      )
     self.assertEqual(result.logits.shape     ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )







 def 	A_      ( self    : Any      )     ->      Tuple:
     # Falcon can have different numbers of KV-heads than the number of query heads, so we need
     # to override this test to use the right head counts.
     for model_class in self.all_generative_model_classes:
         lowerCamelCase__					,				lowerCamelCase__       : Any   					=						self.model_tester.prepare_config_and_inputs_for_common()

         # If it doesn't support cache, pass the test
         if not hasattr(UpperCAmelCase     ,	'use_cache'      ):
             return

         lowerCamelCase__       : Dict   					=						model_class(UpperCAmelCase      ).to(UpperCAmelCase      )
         if "use_cache" not in inputs:
             lowerCamelCase__       : Optional[Any]   					=						True
         lowerCamelCase__       : Any   					=						model(**UpperCAmelCase      )

         # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
         if "past_key_values" not in outputs:
             return

         lowerCamelCase__       : Any   					=						(
             getattr(UpperCAmelCase     ,	'decoder_layers'     ,	UpperCAmelCase      )
             or getattr(UpperCAmelCase     ,	'num_decoder_layers'     ,	UpperCAmelCase      )
             or config.num_hidden_layers
         )
         lowerCamelCase__       : Dict   					=						getattr(UpperCAmelCase     ,	'num_kv_heads'     ,	config.num_attention_heads      )
         lowerCamelCase__       : Dict   					=						getattr(UpperCAmelCase     ,	'd_model'     ,	config.hidden_size      )
         lowerCamelCase__       : str   					=						embed_dim // num_attention_heads

         lowerCamelCase__       : List[Any]   					=						outputs['past_key_values']
         self.assertEqual(len(UpperCAmelCase      )     ,	UpperCAmelCase      )

         lowerCamelCase__					,				lowerCamelCase__       : Any   					=						inputs['input_ids'].shape
         for i in range(UpperCAmelCase      ):
             if config.new_decoder_architecture:
                 lowerCamelCase__       : int   					=						config.num_attention_heads
             elif config.multi_query:
                 lowerCamelCase__       : Optional[Any]   					=						1
             self.assertEqual(len(past_kv[0]      )     ,	2      )  # K V for the decoder = 2
             self.assertEqual(
                 past_kv[i][0].shape     ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )
             self.assertEqual(
                 past_kv[i][1].shape     ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )


@require_torch
class   lowerCAmelCase				(   unittest.TestCase     ):
 @slow
 def 	A_      ( self    : int      )     ->      Union[str, Any]:
     lowerCamelCase__       : str   					=						AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b'      )
     lowerCamelCase__       : List[Any]   					=						FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b'      )
     model.eval()
     model.to(UpperCAmelCase      )
     lowerCamelCase__       : Any   					=						tokenizer('My favorite food is'     ,	return_tensors='pt'      ).to(UpperCAmelCase      )

     lowerCamelCase__       : Optional[Any]   					=						(
         'My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'
     )

     lowerCamelCase__       : Optional[Any]   					=						model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=19      )
     lowerCamelCase__       : Union[str, Any]   					=						tokenizer.batch_decode(UpperCAmelCase      )[0]

     self.assertEqual(UpperCAmelCase     ,	UpperCAmelCase      )
 @slow
 def 	A_      ( self    : Any      )     ->      int:
     # The big models are way too big for the CI, so we use tiny random models that resemble their
     # architectures but with much smaller and fewer layers
     for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
         lowerCamelCase__       : str   					=						AutoTokenizer.from_pretrained(UpperCAmelCase      )
         lowerCamelCase__       : int   					=						FalconForCausalLM.from_pretrained(UpperCAmelCase      )
         model.eval()
         model.to(UpperCAmelCase      )
         lowerCamelCase__       : Tuple   					=						tokenizer('My favorite food is'     ,	return_tensors='pt'      ).to(UpperCAmelCase      )

         # We just test that these run without errors - the models are randomly initialized
         # and so the actual text outputs will be garbage
         model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=4      )
         model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=4      )
         model.generate(**UpperCAmelCase     ,	num_beams=2     ,	max_new_tokens=4      )







 @slow
 def 	A_      ( self    : Union[str, Any]      )     ->      str:
     # The big models are way too big for the CI, so we use tiny random models that resemble their
     # architectures but with much smaller and fewer layers
     with torch.no_grad():
         for repo in [
             "Rocketknight1/falcon-rw-1b",
             "Rocketknight1/tiny-random-falcon-7b",
             "Rocketknight1/tiny-random-falcon-40b",
         ]:
             lowerCamelCase__       : Dict   					=						AutoTokenizer.from_pretrained(UpperCAmelCase      )
             lowerCamelCase__       : Tuple   					=						FalconForCausalLM.from_pretrained(UpperCAmelCase      )
             model.eval()
             model.to(device=UpperCAmelCase      )
             lowerCamelCase__       : Any   					=						tokenizer('My favorite food is'     ,	return_tensors='pt'      ).to(UpperCAmelCase      )

             # Test results are the same with and without cache
             lowerCamelCase__       : List[str]   					=						model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=20     ,	use_cache=UpperCAmelCase      )
             lowerCamelCase__       : Union[str, Any]   					=						model.generate(**UpperCAmelCase     ,	do_sample=UpperCAmelCase     ,	max_new_tokens=20     ,	use_cache=UpperCAmelCase      )
             self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0      )





