import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
    import os

    import jax.numpy as jnp
    from jax import jit

    from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
    from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

    __lowercase							 =					'''0.12'''  # assumed parallelism: 8


if is_torch_available():
    import torch



def lowerCamelCase  (   SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE=None	):
    '''simple docstring'''







    if rng is None:
        __UpperCamelCase     :Optional[Any]			  =		random.Random()

    __UpperCamelCase     :Union[str, Any]			  =		1
    for dim in shape:
        total_dims *= dim

    __UpperCamelCase     :str			  =		[]
    for _ in range(__lowerCamelCase	):
        values.append(rng.randint(0  ,			vocab_size - 1	)	)

    __UpperCamelCase     :List[Any]			  =		np.array(__lowerCamelCase  ,			dtype=jnp.intaa	).reshape(__lowerCamelCase	)

    return output



def lowerCamelCase  (   SCREAMING_SNAKE_CASE  ,			SCREAMING_SNAKE_CASE=None	):
    '''simple docstring'''







    __UpperCamelCase     :Any			  =		ids_tensor(__lowerCamelCase  ,			vocab_size=2  ,			rng=__lowerCamelCase	)
    # make sure that at least one token is attended to for each batch
    __UpperCamelCase     :List[str]			  =		1
    return attn_mask


@require_flax
class 	lowerCamelCase_     :






    '''simple docstring'''






    a__	:						Dict						=      None
    a__	:						Optional[int]						=      ()







    def 			UpperCamelCase__   (	self)       ->	Any:
        __UpperCamelCase     :List[str]			  =		self.model_tester.prepare_config_and_inputs_for_common()

        # cut to half length & take max batch_size 3
        __UpperCamelCase     :str			  =		2
        __UpperCamelCase     :int			  =		inputs["input_ids"].shape[-1] // 2
        __UpperCamelCase     :Union[str, Any]			  =		inputs["input_ids"][:max_batch_size, :sequence_length]

        __UpperCamelCase     :Tuple			  =		jnp.ones_like(snake_case__)
        __UpperCamelCase     :str			  =		attention_mask[:max_batch_size, :sequence_length]

        # generate max 5 tokens
        __UpperCamelCase     :Any			  =		input_ids.shape[-1] + 5
        if config.eos_token_id is not None and config.pad_token_id is None:
            # hack to allow generate for models such as GPT2 as is done in `generate()`
            __UpperCamelCase     :Union[str, Any]			  =		config.eos_token_id
        return config, input_ids, attention_mask, max_length







    @is_pt_flax_cross_test
    def 			UpperCamelCase__   (	self)       ->	Dict:
        __UpperCamelCase     :Tuple			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :Union[str, Any]			  =		False
        __UpperCamelCase     :Union[str, Any]			  =		max_length
        __UpperCamelCase     :List[Any]			  =		0

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :List[Any]			  =		model_class(snake_case__)

            __UpperCamelCase     :Optional[Any]			  =		model_class.__name__[4:]  # Skip the "Flax" at the beginning
            __UpperCamelCase     :List[str]			  =		getattr(snake_case__ , snake_case__)
            __UpperCamelCase     :Optional[int]			  =		pt_model_class(snake_case__).eval()
            __UpperCamelCase     :Tuple			  =		load_flax_weights_in_pytorch_model(snake_case__ , flax_model.params)

            __UpperCamelCase     :str			  =		flax_model.generate(snake_case__).sequences
            __UpperCamelCase     :str			  =		pt_model.generate(torch.tensor(snake_case__ , dtype=torch.long))

            if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
                __UpperCamelCase     :Tuple			  =		flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

            self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	Tuple:
        __UpperCamelCase     :str			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :Union[str, Any]			  =		False
        __UpperCamelCase     :List[str]			  =		max_length

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :int			  =		model_class(snake_case__)

            __UpperCamelCase     :Dict			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :str			  =		jit(model.generate)
            __UpperCamelCase     :Optional[int]			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	Any:
        __UpperCamelCase     :List[Any]			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :Optional[Any]			  =		True
        __UpperCamelCase     :int			  =		max_length

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :List[Any]			  =		model_class(snake_case__)

            __UpperCamelCase     :List[str]			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :Optional[int]			  =		jit(model.generate)
            __UpperCamelCase     :int			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	str:
        __UpperCamelCase     :int			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :List[str]			  =		False
        __UpperCamelCase     :Optional[Any]			  =		max_length
        __UpperCamelCase     :List[Any]			  =		2

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :int			  =		model_class(snake_case__)

            __UpperCamelCase     :Any			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :int			  =		jit(model.generate)
            __UpperCamelCase     :Dict			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	Optional[Any]:
        __UpperCamelCase     :List[Any]			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :str			  =		False
        __UpperCamelCase     :Optional[int]			  =		max_length
        __UpperCamelCase     :Union[str, Any]			  =		2
        __UpperCamelCase     :Optional[int]			  =		2

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :str			  =		model_class(snake_case__)

            __UpperCamelCase     :Dict			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences)







    def 			UpperCamelCase__   (	self)       ->	str:
        __UpperCamelCase     :Any			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :int			  =		True
        __UpperCamelCase     :Dict			  =		max_length
        __UpperCamelCase     :Optional[int]			  =		0.8
        __UpperCamelCase     :Dict			  =		10
        __UpperCamelCase     :Optional[int]			  =		0.3
        __UpperCamelCase     :Tuple			  =		1
        __UpperCamelCase     :Optional[Any]			  =		8
        __UpperCamelCase     :List[Any]			  =		9

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :Optional[int]			  =		model_class(snake_case__)

            __UpperCamelCase     :Union[str, Any]			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :Optional[int]			  =		jit(model.generate)
            __UpperCamelCase     :Any			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	Optional[int]:
        __UpperCamelCase     :List[str]			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :int			  =		max_length
        __UpperCamelCase     :int			  =		1
        __UpperCamelCase     :Optional[int]			  =		8
        __UpperCamelCase     :Any			  =		9

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :Optional[int]			  =		model_class(snake_case__)

            __UpperCamelCase     :int			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :List[Any]			  =		jit(model.generate)
            __UpperCamelCase     :Union[str, Any]			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	str:
        __UpperCamelCase     :List[Any]			  =		self._get_input_ids_and_config()
        __UpperCamelCase     :List[Any]			  =		max_length
        __UpperCamelCase     :Dict			  =		2
        __UpperCamelCase     :Any			  =		1
        __UpperCamelCase     :str			  =		8
        __UpperCamelCase     :Union[str, Any]			  =		9

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :Union[str, Any]			  =		model_class(snake_case__)

            __UpperCamelCase     :Union[str, Any]			  =		model.generate(snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :Optional[int]			  =		jit(model.generate)
            __UpperCamelCase     :Optional[Any]			  =		jit_generate(snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	List[Any]:
        __UpperCamelCase     :Tuple			  =		self._get_input_ids_and_config()

        # pad attention mask on the left
        __UpperCamelCase     :List[Any]			  =		attention_mask.at[(0, 0)].set(0)

        __UpperCamelCase     :Tuple			  =		False
        __UpperCamelCase     :Tuple			  =		max_length

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :Optional[int]			  =		model_class(snake_case__)

            __UpperCamelCase     :str			  =		model.generate(snake_case__ , attention_mask=snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :List[str]			  =		jit(model.generate)
            __UpperCamelCase     :Dict			  =		jit_generate(snake_case__ , attention_mask=snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	Tuple:
        __UpperCamelCase     :Any			  =		self._get_input_ids_and_config()

        # pad attention mask on the left
        __UpperCamelCase     :List[str]			  =		attention_mask.at[(0, 0)].set(0)

        __UpperCamelCase     :Optional[int]			  =		True
        __UpperCamelCase     :Any			  =		max_length

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :str			  =		model_class(snake_case__)

            __UpperCamelCase     :Optional[Any]			  =		model.generate(snake_case__ , attention_mask=snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :Optional[Any]			  =		jit(model.generate)
            __UpperCamelCase     :List[str]			  =		jit_generate(snake_case__ , attention_mask=snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())







    def 			UpperCamelCase__   (	self)       ->	int:
        __UpperCamelCase     :Optional[int]			  =		self._get_input_ids_and_config()

        # pad attention mask on the left
        __UpperCamelCase     :Optional[int]			  =		attention_mask.at[(0, 0)].set(0)

        __UpperCamelCase     :Optional[Any]			  =		2
        __UpperCamelCase     :Optional[Any]			  =		max_length

        for model_class in self.all_generative_model_classes:
            __UpperCamelCase     :Union[str, Any]			  =		model_class(snake_case__)

            __UpperCamelCase     :Optional[Any]			  =		model.generate(snake_case__ , attention_mask=snake_case__).sequences
            self.assertEqual(generation_outputs.shape[-1] , snake_case__)

            __UpperCamelCase     :List[Any]			  =		jit(model.generate)
            __UpperCamelCase     :str			  =		jit_generate(snake_case__ , attention_mask=snake_case__).sequences

            self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist())


@require_flax
class 	lowerCamelCase_     (   unittest.TestCase ):






    '''simple docstring'''






    def 			UpperCamelCase__   (	self)       ->	Any:
        __UpperCamelCase     :Tuple			  =		AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert''')
        __UpperCamelCase     :List[str]			  =		FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''')

        __UpperCamelCase     :Any			  =		"Hello world"
        __UpperCamelCase     :str			  =		tokenizer(snake_case__ , return_tensors='''np''').input_ids

        # typos are quickly detected (the correct argument is `do_sample`)
        with self.assertRaisesRegex(snake_case__ , '''do_samples'''):
            model.generate(snake_case__ , do_samples=snake_case__)

        # arbitrary arguments that will not be used anywhere are also not accepted
        with self.assertRaisesRegex(snake_case__ , '''foo'''):
            __UpperCamelCase     :Optional[Any]			  =		{"foo": "bar"}
            model.generate(snake_case__ , **snake_case__)







from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map


def 			UpperCamelCase					(    __lowerCamelCase     : Dict[str, torch.Tensor]    ):
       snake_case       :   List[str]							=      []
       snake_case       :   Optional[int]							=      []
       snake_case       :   Any							=      []

       for rt in rc.restypes:
              snake_case       :   List[Any]							=      rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
              restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]    )
              snake_case       :   str							=      {name: i for i, name in enumerate(__lowerCamelCase    )}
              restype_atomaa_to_atomaa_list.append(
                  [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]    )

              restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]    )

       # Add dummy mapping for restype 'UNK'
       restype_atomaa_to_atomaa_list.append([0] * 14    )
       restype_atomaa_to_atomaa_list.append([0] * 37    )
       restype_atomaa_mask_list.append([0.0] * 14    )

       snake_case       :   Optional[Any]							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.intaa				,					device=protein["aatype"].device				,					)
       snake_case       :   List[Any]							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.intaa				,					device=protein["aatype"].device				,					)
       snake_case       :   int							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.floataa				,					device=protein["aatype"].device				,					)
       snake_case       :   int							=      protein["aatype"].to(torch.long    )

       # create the mapping for (residx, atom14) --> atom37, i.e. an array
       # with shape (num_res, 14) containing the atom37 indices for this protein
       snake_case       :   List[Any]							=      restype_atomaa_to_atomaa[protein_aatype]
       snake_case       :   str							=      restype_atomaa_mask[protein_aatype]

       snake_case       :   str							=      residx_atomaa_mask
       snake_case       :   Any							=      residx_atomaa_to_atomaa.long()

       # create the gather indices for mapping back
       snake_case       :   List[str]							=      restype_atomaa_to_atomaa[protein_aatype]
       snake_case       :   List[Any]							=      residx_atomaa_to_atomaa.long()

       # create the corresponding mask
       snake_case       :   Union[str, Any]							=      torch.zeros([21, 37]				,					dtype=torch.floataa				,					device=protein["aatype"].device    )
       for restype, restype_letter in enumerate(rc.restypes    ):
              snake_case       :   Optional[int]							=      rc.restype_atoa[restype_letter]
              snake_case       :   Any							=      rc.residue_atoms[restype_name]
              for atom_name in atom_names:
                     snake_case       :   List[Any]							=      rc.atom_order[atom_name]
                     snake_case       :   Optional[Any]							=      1

       snake_case       :   List[Any]							=      restype_atomaa_mask[protein_aatype]
       snake_case       :   int							=      residx_atomaa_mask

       return protein




def 			UpperCamelCase					(    __lowerCamelCase     : Dict[str, torch.Tensor]    ):
       snake_case       :   Dict							=      tree_map(lambda __lowerCamelCase    : torch.tensor(__lowerCamelCase				,					device=batch["aatype"].device    )				,					__lowerCamelCase				,					np.ndarray    )
       snake_case       :   List[str]							=      tensor_tree_map(lambda __lowerCamelCase    : np.array(__lowerCamelCase    )				,					make_atomaa_masks(__lowerCamelCase    )    )
       return out





