


'''simple docstring'''





import warnings

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances

warnings.filterwarnings('''ignore''')

UpperCAmelCase__     			=							'''K-MEANS-CLUST/ '''
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case=None		)  ->    List[Any]:







 """simple docstring"""


 if seed is not None:  # useful for obtaining consistent results
  np.random.seed(__snake_case		)
 _lowercase							   =data.shape[0]  # number of data points

 # Pick K indices from range [0, N).
 _lowercase							   =np.random.randint(0  ,		__snake_case  ,		__snake_case		)

 # Keep centroids as dense format, as many entries will be nonzero due to averaging.
 # As long as at least one document in a cluster contains a word,
 # it will carry a nonzero weight in the TF-IDF vector of the centroid.
 _lowercase							   =data[rand_indices, :]

 return centroids
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case		)  ->    Any:







 """simple docstring"""


 return pairwise_distances(__snake_case  ,		__snake_case  ,		metric='''euclidean'''		)
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case		)  ->    Optional[Any]:







 """simple docstring"""


 _lowercase							   =centroid_pairwise_dist(__snake_case  ,		__snake_case		)

 # Compute cluster assignments for each data point:
 # Fill in the blank (RHS only)
 _lowercase							   =np.argmin(__snake_case  ,		axis=1		)

 return cluster_assignment
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case		)  ->    Tuple:







 """simple docstring"""


 _lowercase							   =[]
 for i in range(__snake_case		):
  # Select all data points that belong to cluster i. Fill in the blank (RHS only)
  _lowercase							   =data[cluster_assignment == i]
  # Compute the mean of the data points. Fill in the blank (RHS only)
  _lowercase							   =member_data_points.mean(axis=0		)
  new_centroids.append(__snake_case		)
 _lowercase							   =np.array(__snake_case		)

 return new_centroids
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case  ,		__snake_case		)  ->    Optional[Any]:







 """simple docstring"""


 _lowercase							   =0.0
 for i in range(__snake_case		):
  # Select all data points that belong to cluster i. Fill in the blank (RHS only)
  _lowercase							   =data[cluster_assignment == i, :]

  if member_data_points.shape[0] > 0:  # check if i-th cluster is non-empty
   # Compute distances from centroid to data points (RHS only)
   _lowercase							   =pairwise_distances(
       __snake_case  ,		[centroids[i]]  ,		metric='''euclidean'''		)
   _lowercase							   =distances**2
   heterogeneity += np.sum(__snake_case		)

 return heterogeneity
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case		)  ->    Optional[int]:







 """simple docstring"""


 plt.figure(figsize=(7, 4)		)
 plt.plot(__snake_case  ,		linewidth=4		)
 plt.xlabel('''# Iterations'''		)
 plt.ylabel('''Heterogeneity'''		)
 plt.title(F"Heterogeneity of clustering over time, K={k:d}"		)
 plt.rcParams.update({'''font.size''': 16}		)
 plt.show()
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case  ,		__snake_case=500  ,		__snake_case=None  ,		__snake_case=False		)  ->    Dict:







 """simple docstring"""


 _lowercase							   =initial_centroids[:]
 _lowercase							   =None

 for itr in range(__snake_case		):
  if verbose:
   print(__snake_case  ,		end=''''''		)

  # 1. Make cluster assignments using nearest centroids
  _lowercase							   =assign_clusters(__snake_case  ,		__snake_case		)

  # 2. Compute a new centroid for each of the k clusters, averaging all data
  #    points assigned to that cluster.
  _lowercase							   =revise_centroids(__snake_case  ,		__snake_case  ,		__snake_case		)

  # Check for convergence: if none of the assignments changed, stop
  if (
      prev_cluster_assignment is not None
      and (prev_cluster_assignment == cluster_assignment).all()
  ):
   break

  # Print number of new assignments
  if prev_cluster_assignment is not None:
   _lowercase							   =np.sum(prev_cluster_assignment != cluster_assignment		)
   if verbose:
    print(
        F"    {num_changed:5d} elements changed their cluster assignment."		)

        # Record heterogeneity convergence metric
  if record_heterogeneity is not None:
   # YOUR CODE HERE
   _lowercase							   =compute_heterogeneity(__snake_case  ,		__snake_case  ,		__snake_case  ,		__snake_case		)
   record_heterogeneity.append(__snake_case		)

  _lowercase							   =cluster_assignment[:]

 return centroids, cluster_assignment


# Mock test below
if False:  # change to true to run this test case.
  from sklearn import datasets as ds

  UpperCAmelCase__     			=							ds.load_iris()
  UpperCAmelCase__     			=							3
  UpperCAmelCase__     			=							[]
  UpperCAmelCase__     			=							get_initial_centroids(dataset['''data'''], k, seed=0)
  UpperCAmelCase__			,UpperCAmelCase__     			=							kmeans(
      dataset['''data'''],
      k,
      initial_centroids,
      maxiter=400,
      record_heterogeneity=heterogeneity,
      verbose=True,
  )
  plot_heterogeneity(heterogeneity, k)
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case=None		)  ->    pd.DataFrame:







 """simple docstring"""


 if fill_missing_report:
  _lowercase							   =df.fillna(value=__snake_case		)
 _lowercase							   =1
 _lowercase							   =df.select_dtypes(np.number		).columns
 _lowercase							   =(
     df.groupby(['''Cluster''']		)[  # construct report dataframe
         numeric_cols
     ]  # group by cluster number
     .agg(
         [
             ('''sum''', np.sum),
             ('''mean_with_zeros''', lambda __snake_case		: np.mean(np.nan_to_num(__snake_case		)		)),
             ('''mean_without_zeros''', lambda __snake_case		: x.replace(0  ,		np.NaN		).mean()),
             (
                 '''mean_25-75''',
                 lambda __snake_case		: np.mean(
                     np.nan_to_num(
                         sorted(__snake_case		)[
                             round(len(__snake_case		) * 25 / 100		) : round(len(__snake_case		) * 75 / 100		)
                         ]		)		),
             ),
             ('''mean_with_na''', np.mean),
             ('''min''', lambda __snake_case		: x.min()),
             ('''5%''', lambda __snake_case		: x.quantile(0.05		)),
             ('''25%''', lambda __snake_case		: x.quantile(0.25		)),
             ('''50%''', lambda __snake_case		: x.quantile(0.50		)),
             ('''75%''', lambda __snake_case		: x.quantile(0.75		)),
             ('''95%''', lambda __snake_case		: x.quantile(0.95		)),
             ('''max''', lambda __snake_case		: x.max()),
             ('''count''', lambda __snake_case		: x.count()),
             ('''stdev''', lambda __snake_case		: x.std()),
             ('''mode''', lambda __snake_case		: x.mode()[0]),
             ('''median''', lambda __snake_case		: x.median()),
             ('''# > 0''', lambda __snake_case		: (x > 0).sum()),
         ]		)
     .T.reset_index()
     .rename(index=__snake_case  ,		columns={'''level_0''': '''Features''', '''level_1''': '''Type'''}		)
 )  # rename columns
 # calculate the size of cluster(count of clientID's)
 _lowercase							   =report[
     (report['''Features'''] == '''dummy''') & (report['''Type'''] == '''count''')
 ].copy()  # avoid SettingWithCopyWarning
 _lowercase							   =(
     '''ClusterSize'''  # rename created cluster df to match report column names
 )
 _lowercase							   ='''# of Customers'''
 _lowercase							   =pd.DataFrame(
     clustersize.iloc[:, 2:].values
     / clustersize.iloc[:, 2:].values.sum()		)
 _lowercase							   ='''% of Customers'''  # rename created cluster df to match report column names
 _lowercase							   ='''ClusterProportion'''
 _lowercase							   =clusterproportion.columns.tolist()
 _lowercase							   =cols[-2:] + cols[:-2]
 _lowercase							   =clusterproportion[cols]  # rearrange columns to match report
 _lowercase							   =report.columns
 _lowercase							   =pd.DataFrame(
     abs(
         report[report['''Type'''] == '''count'''].iloc[:, 2:].values
         - clustersize.iloc[:, 2:].values		)		)  # generating df with count of nan values
 _lowercase							   =0
 _lowercase							   ='''# of nan'''
 _lowercase							   =report[
     report['''Type'''] == '''count'''
 ].Features.tolist()  # filling values in order to match report
 _lowercase							   =a.columns.tolist()
 _lowercase							   =cols[-2:] + cols[:-2]
 _lowercase							   =a[cols]  # rearrange columns to match report
 _lowercase							   =report.columns  # rename columns to match report
 _lowercase							   =report.drop(
     report[report.Type == '''count'''].index		)  # drop count values except cluster size
 _lowercase							   =pd.concat(
     [report, a, clustersize, clusterproportion]  ,		axis=0		)  # concat report with clustert size and nan values
 _lowercase							   =report['''Features'''].isin(__snake_case		)
 _lowercase							   =report.columns.tolist()
 _lowercase							   =cols[0:2] + cols[-1:] + cols[2:-1]
 _lowercase							   =report[cols]
 _lowercase							   ={
     '''ClusterSize''': 9,
     '''ClusterProportion''': 8,
     '''mean_with_zeros''': 7,
     '''mean_with_na''': 6,
     '''max''': 5,
     '''50%''': 4,
     '''min''': 3,
     '''25%''': 2,
     '''75%''': 1,
     '''# of nan''': 0,
     '''# > 0''': -1,
     '''sum_with_na''': -2,
 }
 _lowercase							   =(
     report.assign(
         Sortera=lambda __snake_case		: x.Type.map(__snake_case		)  ,		Sortera=lambda __snake_case		: list(reversed(range(len(__snake_case		)		)		)		)  ,		)
     .sort_values(['''Sorter1''', '''Mark''', '''Sorter2''']  ,		ascending=__snake_case		)
     .drop(['''Sorter1''', '''Sorter2''']  ,		axis=1		)
 )
 _lowercase							   =''''''
 _lowercase							   =report.reset_index()
 _lowercase							   =report.drop(columns=['''index''']		)
 return report


if __name__ == "__main__":
  import doctest

  doctest.testmod()





import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
  import jax
  import jax.numpy as jnp

  from transformers.modeling_flax_pytorch_utils import (
      convert_pytorch_state_dict_to_flax,
      load_flax_weights_in_pytorch_model,
  )
  from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
  import torch







class    lowerCamelCase__     :





     def __init__(self	,  UpperCAmelCase	,  UpperCAmelCase=1_4	,  UpperCAmelCase=7	,  UpperCAmelCase=True	,  UpperCAmelCase=True	,  UpperCAmelCase=False	,  UpperCAmelCase=True	,  UpperCAmelCase=9_9	,  UpperCAmelCase=3_2	,  UpperCAmelCase=5	,  UpperCAmelCase=4	,  UpperCAmelCase=3_7	,  UpperCAmelCase="gelu"	,  UpperCAmelCase=0.1	,  UpperCAmelCase=0.1	,  UpperCAmelCase=5_1_2	,  UpperCAmelCase=0.02	,  )  ->     Any:
      _lowercase							   =parent
      _lowercase							   =batch_size
      _lowercase							   =seq_length
      _lowercase							   =is_training
      _lowercase							   =use_input_mask
      _lowercase							   =use_token_type_ids
      _lowercase							   =use_labels
      _lowercase							   =vocab_size
      _lowercase							   =hidden_size
      _lowercase							   =num_hidden_layers
      _lowercase							   =num_attention_heads
      _lowercase							   =intermediate_size
      _lowercase							   =hidden_act
      _lowercase							   =hidden_dropout_prob
      _lowercase							   =attention_probs_dropout_prob
      _lowercase							   =max_position_embeddings
      _lowercase							   =initializer_range
      _lowercase							   =None
      _lowercase							   =vocab_size - 1
      _lowercase							   =vocab_size - 1
      _lowercase							   =vocab_size - 1





     def   __A      (self )  ->     int:
      _lowercase							   =ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size )

      _lowercase							   =None
      if self.use_input_mask:
       _lowercase							   =random_attention_mask([self.batch_size, self.seq_length] )

      _lowercase							   =GPTaConfig(
          vocab_size=self.vocab_size	,  n_embd=self.hidden_size	,  n_layer=self.num_hidden_layers	,  n_head=self.num_attention_heads	,  n_positions=self.max_position_embeddings	,  use_cache=UpperCAmelCase	,  bos_token_id=self.bos_token_id	,  eos_token_id=self.eos_token_id	,  pad_token_id=self.pad_token_id	,  )

      return (config, input_ids, input_mask)





     def   __A      (self )  ->     List[Any]:
      _lowercase							   =self.prepare_config_and_inputs()
      _lowercase   ,					_lowercase   ,					_lowercase							   =config_and_inputs
      _lowercase							   ={'''input_ids''': input_ids, '''attention_mask''': attention_mask}
      return config, inputs_dict





     def   __A      (self )  ->     Tuple:
      _lowercase   ,					_lowercase   ,					_lowercase							   =self.prepare_config_and_inputs()

      _lowercase							   =floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
      _lowercase							   =ids_tensor([self.batch_size, self.seq_length]	,  vocab_size=2 )

      return (
          config,
          input_ids,
          attention_mask,
          encoder_hidden_states,
          encoder_attention_mask,
      )





     def   __A      (self	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase )  ->     Dict:
      _lowercase							   =2_0
      _lowercase							   =model_class_name(UpperCAmelCase )

      _lowercase							   =model.init_cache(input_ids.shape[0]	,  UpperCAmelCase )
      _lowercase							   =jnp.ones((input_ids.shape[0], max_decoder_length)	,  dtype='''i4''' )

      _lowercase							   =jnp.broadcast_to(
          jnp.arange(input_ids.shape[-1] - 1 )[None, :]	,  (input_ids.shape[0], input_ids.shape[-1] - 1) )
      _lowercase							   =model(
          input_ids[:, :-1]	,  attention_mask=UpperCAmelCase	,  past_key_values=UpperCAmelCase	,  position_ids=UpperCAmelCase	,  )

      _lowercase							   =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,  dtype='''i4''' )
      _lowercase							   =model(
          input_ids[:, -1:]	,  attention_mask=UpperCAmelCase	,  past_key_values=outputs_cache.past_key_values	,  position_ids=UpperCAmelCase	,  )

      _lowercase							   =model(UpperCAmelCase )

      _lowercase							   =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) )
      self.parent.assertTrue(diff < 1e-3	,  msg=f"Max diff is {diff}" )





     def   __A      (self	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase )  ->     List[str]:
      _lowercase							   =2_0
      _lowercase							   =model_class_name(UpperCAmelCase )

      _lowercase							   =jnp.concatenate(
          [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )]	,  axis=-1	,  )

      _lowercase							   =model.init_cache(input_ids.shape[0]	,  UpperCAmelCase )
      _lowercase							   =jnp.broadcast_to(
          jnp.arange(input_ids.shape[-1] - 1 )[None, :]	,  (input_ids.shape[0], input_ids.shape[-1] - 1) )

      _lowercase							   =model(
          input_ids[:, :-1]	,  attention_mask=UpperCAmelCase	,  past_key_values=UpperCAmelCase	,  position_ids=UpperCAmelCase	,  )
      _lowercase							   =jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,  dtype='''i4''' )
      _lowercase							   =model(
          input_ids[:, -1:]	,  past_key_values=outputs_cache.past_key_values	,  attention_mask=UpperCAmelCase	,  position_ids=UpperCAmelCase	,  )

      _lowercase							   =model(UpperCAmelCase	,  attention_mask=UpperCAmelCase )

      _lowercase							   =np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) )
      self.parent.assertTrue(diff < 1e-3	,  msg=f"Max diff is {diff}" )







@require_flax
class    lowerCamelCase__     ( lowerCAmelCase     ,							lowerCAmelCase     ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__       =    (FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
     SCREAMING_SNAKE_CASE__       =    (FlaxGPTaLMHeadModel,) if is_flax_available() else ()





     def   __A      (self )  ->     str:
      _lowercase							   =FlaxGPTaModelTester(self )





     def   __A      (self )  ->     Optional[int]:
      for model_class_name in self.all_model_classes:
       _lowercase   ,					_lowercase   ,					_lowercase							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.check_use_cache_forward(UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase )





     def   __A      (self )  ->     Dict:
      for model_class_name in self.all_model_classes:
       _lowercase   ,					_lowercase   ,					_lowercase							   =self.model_tester.prepare_config_and_inputs()
       self.model_tester.check_use_cache_forward_with_attn_mask(
           UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase	,  UpperCAmelCase )





     @slow
     def   __A      (self )  ->     Dict:
      _lowercase							   =GPTaTokenizer.from_pretrained('''gpt2'''	,  pad_token='''</s>'''	,  padding_side='''left''' )
      _lowercase							   =tokenizer(['''Hello this is a long string''', '''Hey''']	,  return_tensors='''np'''	,  padding=UpperCAmelCase	,  truncation=UpperCAmelCase )

      _lowercase							   =FlaxGPTaLMHeadModel.from_pretrained('''gpt2''' )
      _lowercase							   =False
      _lowercase							   =model.config.eos_token_id

      _lowercase							   =jax.jit(model.generate )

      _lowercase							   =jit_generate(inputs['''input_ids''']	,  attention_mask=inputs['''attention_mask'''] ).sequences

      _lowercase							   =tokenizer.batch_decode(UpperCAmelCase	,  skip_special_tokens=UpperCAmelCase )

      _lowercase							   =[
          '''Hello this is a long string of words. I\'m going to start with the first one.\n''',
          '''Hey, I\'m not sure if I\'m going to be able to do''',
      ]

      self.assertListEqual(UpperCAmelCase	,  UpperCAmelCase )





     @is_pt_flax_cross_test
     def   __A      (self )  ->     Optional[Any]:
      _lowercase   ,					_lowercase							   =self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__ ):
        # prepare inputs
        _lowercase							   =self._prepare_for_class(UpperCAmelCase	,  UpperCAmelCase )
        _lowercase							   ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()}

        # load corresponding PyTorch class
        _lowercase							   =model_class.__name__[4:]  # Skip the "Flax" at the beginning
        _lowercase							   =getattr(UpperCAmelCase	,  UpperCAmelCase )

        _lowercase   ,					_lowercase							   =pt_inputs['''input_ids'''].shape
        _lowercase							   =np.random.randint(0	,  seq_length - 1	,  size=(batch_size,) )
        for batch_idx, start_index in enumerate(UpperCAmelCase ):
         _lowercase							   =0
         _lowercase							   =1
         _lowercase							   =0
         _lowercase							   =1
        _lowercase							   =pt_model_class(UpperCAmelCase ).eval()
        _lowercase							   =model_class(UpperCAmelCase	,  dtype=jnp.floataa )

        _lowercase							   =convert_pytorch_state_dict_to_flax(pt_model.state_dict()	,  UpperCAmelCase )
        _lowercase							   =fx_state

        with torch.no_grad():
         _lowercase							   =pt_model(**UpperCAmelCase ).to_tuple()

        _lowercase							   =fx_model(**UpperCAmelCase ).to_tuple()
        self.assertEqual(len(UpperCAmelCase )	,  len(UpperCAmelCase )	,  '''Output lengths differ between Flax and PyTorch''' )
        for fx_output, pt_output in zip(UpperCAmelCase	,  UpperCAmelCase ):
         self.assert_almost_equals(fx_output[:, -1]	,  pt_output[:, -1].numpy()	,  4e-2 )

        with tempfile.TemporaryDirectory() as tmpdirname:
         pt_model.save_pretrained(UpperCAmelCase )
         _lowercase							   =model_class.from_pretrained(UpperCAmelCase	,  from_pt=UpperCAmelCase )

        _lowercase							   =fx_model_loaded(**UpperCAmelCase ).to_tuple()
        self.assertEqual(
            len(UpperCAmelCase )	,  len(UpperCAmelCase )	,  '''Output lengths differ between Flax and PyTorch''' )
        for fx_output_loaded, pt_output in zip(UpperCAmelCase	,  UpperCAmelCase ):
         self.assert_almost_equals(fx_output_loaded[:, -1]	,  pt_output[:, -1].numpy()	,  4e-2 )





     @is_pt_flax_cross_test
     def   __A      (self )  ->     Optional[Any]:
      _lowercase   ,					_lowercase							   =self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__ ):
        # prepare inputs
        _lowercase							   =self._prepare_for_class(UpperCAmelCase	,  UpperCAmelCase )
        _lowercase							   ={k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()}

        # load corresponding PyTorch class
        _lowercase							   =model_class.__name__[4:]  # Skip the "Flax" at the beginning
        _lowercase							   =getattr(UpperCAmelCase	,  UpperCAmelCase )

        _lowercase							   =pt_model_class(UpperCAmelCase ).eval()
        _lowercase							   =model_class(UpperCAmelCase	,  dtype=jnp.floataa )

        _lowercase							   =load_flax_weights_in_pytorch_model(UpperCAmelCase	,  fx_model.params )
        _lowercase   ,					_lowercase							   =pt_inputs['''input_ids'''].shape
        _lowercase							   =np.random.randint(0	,  seq_length - 1	,  size=(batch_size,) )
        for batch_idx, start_index in enumerate(UpperCAmelCase ):
         _lowercase							   =0
         _lowercase							   =1
         _lowercase							   =0
         _lowercase							   =1

        # make sure weights are tied in PyTorch
        pt_model.tie_weights()

        with torch.no_grad():
         _lowercase							   =pt_model(**UpperCAmelCase ).to_tuple()

        _lowercase							   =fx_model(**UpperCAmelCase ).to_tuple()
        self.assertEqual(len(UpperCAmelCase )	,  len(UpperCAmelCase )	,  '''Output lengths differ between Flax and PyTorch''' )
        for fx_output, pt_output in zip(UpperCAmelCase	,  UpperCAmelCase ):
         self.assert_almost_equals(fx_output[:, -1]	,  pt_output[:, -1].numpy()	,  4e-2 )

        with tempfile.TemporaryDirectory() as tmpdirname:
         fx_model.save_pretrained(UpperCAmelCase )
         _lowercase							   =pt_model_class.from_pretrained(UpperCAmelCase	,  from_flax=UpperCAmelCase )

        with torch.no_grad():
         _lowercase							   =pt_model_loaded(**UpperCAmelCase ).to_tuple()

        self.assertEqual(
            len(UpperCAmelCase )	,  len(UpperCAmelCase )	,  '''Output lengths differ between Flax and PyTorch''' )
        for fx_output, pt_output in zip(UpperCAmelCase	,  UpperCAmelCase ):
         self.assert_almost_equals(fx_output[:, -1]	,  pt_output[:, -1].numpy()	,  4e-2 )





     @slow
     def   __A      (self )  ->     Union[str, Any]:
      for model_class_name in self.all_model_classes:
       _lowercase							   =model_class_name.from_pretrained('''gpt2'''	,  from_pt=UpperCAmelCase )
       _lowercase							   =model(np.ones((1, 1) ) )
       self.assertIsNotNone(UpperCAmelCase )

