





'''simple docstring'''

import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)





class lowerCamelCase						(unittest.TestCase					):







  def 				UpperCAmelCase_				(		self		)		->						Tuple:






        """simple docstring"""





        _snake_case		:	Tuple					     =     ['''a''', '''b''', '''c''']

        # Defaults to last layer if both are None
        _snake_case     ,							_snake_case		:	Dict					     =     get_aligned_output_features_output_indices(lowercase__     ,     lowercase__     ,     lowercase__		)
        self.assertEqual(lowercase__     ,     ['''c''']		)
        self.assertEqual(lowercase__     ,     [2]		)

        # Out indices set to match out features
        _snake_case     ,							_snake_case		:	str					     =     get_aligned_output_features_output_indices(['''a''', '''c''']     ,     lowercase__     ,     lowercase__		)
        self.assertEqual(lowercase__     ,     ['''a''', '''c''']		)
        self.assertEqual(lowercase__     ,     [0, 2]		)

        # Out features set to match out indices
        _snake_case     ,							_snake_case		:	Any					     =     get_aligned_output_features_output_indices(lowercase__     ,     [0, 2]     ,     lowercase__		)
        self.assertEqual(lowercase__     ,     ['''a''', '''c''']		)
        self.assertEqual(lowercase__     ,     [0, 2]		)

        # Out features selected from negative indices
        _snake_case     ,							_snake_case		:	Optional[int]					     =     get_aligned_output_features_output_indices(lowercase__     ,     [-3, -1]     ,     lowercase__		)
        self.assertEqual(lowercase__     ,     ['''a''', '''c''']		)
        self.assertEqual(lowercase__     ,     [-3, -1]		)







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(['''a''', '''b''']     ,     (0, 1)     ,     lowercase__		)

        # Out features must be a list
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(('''a''', '''b''')     ,     (0, 1)     ,     ['''a''', '''b''']		)

        # Out features must be a subset of stage names
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(['''a''', '''b''']     ,     (0, 1)     ,     ['''a''']		)

        # Out indices must be a list or tuple
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(lowercase__     ,     0     ,     ['''a''', '''b''']		)

        # Out indices must be a subset of stage names
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(lowercase__     ,     (0, 1)     ,     ['''a''']		)

        # Out features and out indices must be the same length
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(['''a''', '''b''']     ,     (0,)     ,     ['''a''', '''b''', '''c''']		)

        # Out features should match out indices
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(['''a''', '''b''']     ,     (0, 2)     ,     ['''a''', '''b''', '''c''']		)

        # Out features and out indices should be in order
        with self.assertRaises(lowercase__		):
              verify_out_features_out_indices(['''b''', '''a''']     ,     (0, 1)     ,     ['''a''', '''b''']		)

        # Check passes with valid inputs
        verify_out_features_out_indices(['''a''', '''b''', '''d''']     ,     (0, 1, -1)     ,     ['''a''', '''b''', '''c''', '''d''']		)







  def 				UpperCAmelCase_				(		self		)		->						Optional[Any]:






        """simple docstring"""





        _snake_case		:	Optional[int]					     =     BackboneMixin()

        _snake_case		:	List[Any]					     =     ['''a''', '''b''', '''c''']
        _snake_case		:	Union[str, Any]					     =     ['''a''', '''c''']
        _snake_case		:	Any					     =     [0, 2]

        # Check that the output features and indices are set correctly
        self.assertEqual(backbone.out_features     ,     ['''a''', '''c''']		)
        self.assertEqual(backbone.out_indices     ,     [0, 2]		)

        # Check out features and indices are updated correctly
        _snake_case		:	List[str]					     =     ['''a''', '''b''']
        self.assertEqual(backbone.out_features     ,     ['''a''', '''b''']		)
        self.assertEqual(backbone.out_indices     ,     [0, 1]		)

        _snake_case		:	Dict					     =     [-3, -1]
        self.assertEqual(backbone.out_features     ,     ['''a''', '''c''']		)
        self.assertEqual(backbone.out_indices     ,     [-3, -1]		)









'''simple docstring'''

import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
   import optax
   from flax.training.common_utils import onehot

   from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
   from transformers.models.ta.modeling_flax_ta import shift_tokens_right





@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class lowerCamelCase						(unittest.TestCase					):







  @slow
  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        _snake_case		:	Tuple					     =     FlaxMTaForConditionalGeneration.from_pretrained('''google/mt5-small'''		)
        _snake_case		:	Any					     =     AutoTokenizer.from_pretrained('''google/mt5-small'''		)

        _snake_case		:	List[str]					     =     tokenizer('''Hello there'''     ,     return_tensors='''np'''		).input_ids
        _snake_case		:	Dict					     =     tokenizer('''Hi I am'''     ,     return_tensors='''np'''		).input_ids

        _snake_case		:	Any					     =     shift_tokens_right(lowercase__     ,     model.config.pad_token_id     ,     model.config.decoder_start_token_id		)

        _snake_case		:	Any					     =     model(lowercase__     ,     decoder_input_ids=lowercase__		).logits
        _snake_case		:	Tuple					     =     optax.softmax_cross_entropy(lowercase__     ,     onehot(lowercase__     ,     logits.shape[-1]		)		).mean()

        _snake_case		:	Tuple					     =     -(labels.shape[-1] * loss.item())

        _snake_case		:	Union[str, Any]					     =     -84.9_127
        self.assertTrue(abs(mtf_score - EXPECTED_SCORE		) < 1E-4		)


