
import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Image
from datasets.packaged_modules.text.text import Text

from ..utils import require_pil







@pytest.fixture
def 		_snake_case(						SCREAMING_SNAKE_CASE__       )				->					int:
       lowercase							:  Union[str, Any] 			=   tmp_path / 'text.txt'
       lowercase							:  Optional[Any] 			=   textwrap.dedent(
           """\\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n        Second paragraph:\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n        """       )
       with open(lowercase_      ,			"""w"""      ,			encoding="""utf-8"""       ) as f:
              f.write(lowercase_       )
       return str(lowercase_       )







@pytest.fixture
def 		_snake_case(						SCREAMING_SNAKE_CASE__      ,			SCREAMING_SNAKE_CASE__       )				->					List[str]:
       lowercase							:  Optional[Any] 			=   tmp_path / 'text_with_image.txt'
       with open(lowercase_      ,			"""w"""      ,			encoding="""utf-8"""       ) as f:
              f.write(lowercase_       )
       return str(lowercase_       )







@pytest.mark.parametrize("""keep_linebreaks"""      ,			[True, False]       )
def 		_snake_case(						SCREAMING_SNAKE_CASE__      ,			SCREAMING_SNAKE_CASE__       )				->					Optional[Any]:
       with open(lowercase_      ,			encoding="""utf-8"""       ) as f:
              lowercase							:  int 			=   f.read().splitlines(keepends=lowercase_       )
       lowercase							:  Optional[Any] 			=   Text(keep_linebreaks=lowercase_      ,			encoding="""utf-8"""       )
       lowercase							:  Any 			=   text._generate_tables([[text_file]]       )
       lowercase							:  Dict 			=   pa.concat_tables([table for _, table in generator]       ).to_pydict()['text']
       assert generated_content == expected_content







@require_pil
def 		_snake_case(						SCREAMING_SNAKE_CASE__       )				->					Optional[int]:
       with open(lowercase_      ,			encoding="""utf-8"""       ) as f:
              lowercase							:  Tuple 			=   f.read().splitlines()[0]
       lowercase							:  Any 			=   Text(encoding="""utf-8"""      ,			features=Features({"""image""": Image()}       )       )
       lowercase							:  int 			=   text._generate_tables([[text_file_with_image]]       )
       lowercase							:  int 			=   pa.concat_tables([table for _, table in generator]       )
       assert pa_table.schema.field("""image"""       ).type == Image()()
       lowercase							:  Optional[int] 			=   pa_table.to_pydict()['image']
       assert generated_content == [{"path": image_file, "bytes": None}]







@pytest.mark.parametrize("""sample_by"""      ,			["""line""", """paragraph""", """document"""]       )
def 		_snake_case(						SCREAMING_SNAKE_CASE__      ,			SCREAMING_SNAKE_CASE__       )				->					List[str]:
       with open(lowercase_      ,			encoding="""utf-8"""       ) as f:
              lowercase							:  Optional[int] 			=   f.read()
       if sample_by == "line":
              lowercase							:  Union[str, Any] 			=   expected_content.splitlines()
       elif sample_by == "paragraph":
              lowercase							:  str 			=   expected_content.split("""\n\n"""       )
       elif sample_by == "document":
              lowercase							:  Tuple 			=   [expected_content]
       lowercase							:  int 			=   Text(sample_by=lowercase_      ,			encoding="""utf-8"""      ,			chunksize=100       )
       lowercase							:  Any 			=   text._generate_tables([[text_file]]       )
       lowercase							:  Optional[Any] 			=   pa.concat_tables([table for _, table in generator]       ).to_pydict()['text']
       assert generated_content == expected_content







import unittest
from unittest.util import safe_repr

from transformers import AutoTokenizer, RwkvConfig, 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, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
      RwkvForCausalLM,
      RwkvModel,
  )
  from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
  lowercase						:      Any							=							False

class __snake_case	:
    def __init__(						self	,snake_case	,snake_case=14	,snake_case=7	,snake_case=True	,snake_case=False	,snake_case=True	,snake_case=True	,snake_case=True	,snake_case=99	,snake_case=32	,snake_case=5	,snake_case=37	,snake_case="gelu"	,snake_case=0.1	,snake_case=0.1	,snake_case=512	,snake_case=16	,snake_case=2	,snake_case=3	,snake_case=4	,snake_case=None	,):



           '''simple docstring'''







           lowercase							:  Optional[Any] 			=   parent
           lowercase							:  str 			=   batch_size
           lowercase							:  str 			=   seq_length
           lowercase							:  int 			=   is_training
           lowercase							:  Optional[Any] 			=   use_token_type_ids
           lowercase							:  List[str] 			=   use_input_mask
           lowercase							:  List[Any] 			=   use_labels
           lowercase							:  List[Any] 			=   use_mc_token_ids
           lowercase							:  int 			=   vocab_size
           lowercase							:  Any 			=   hidden_size
           lowercase							:  Optional[int] 			=   num_hidden_layers
           lowercase							:  Optional[int] 			=   intermediate_size
           lowercase							:  Optional[int] 			=   hidden_act
           lowercase							:  Optional[int] 			=   hidden_dropout_prob
           lowercase							:  List[str] 			=   attention_probs_dropout_prob
           lowercase							:  Dict 			=   max_position_embeddings
           lowercase							:  Optional[Any] 			=   type_vocab_size
           lowercase							:  str 			=   type_sequence_label_size
           lowercase							:  int 			=   num_labels
           lowercase							:  List[Any] 			=   num_choices
           lowercase							:  int 			=   scope
           lowercase							:  Any 			=   vocab_size - 1
           lowercase							:  str 			=   vocab_size - 1
           lowercase							:  int 			=   vocab_size - 1
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           return RwkvConfig.from_pretrained("""sgugger/rwkv-4-pile-7b"""   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case=False	,snake_case=False	,snake_case=False   ):



           '''simple docstring'''







           lowercase							:  Optional[Any] 			=   ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size   )

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

           lowercase							:  List[Any] 			=   None
           if self.use_token_type_ids:
                  lowercase							:  str 			=   ids_tensor([self.batch_size, self.seq_length]	,self.type_vocab_size   )

           lowercase							:  Any 			=   None
           if self.use_mc_token_ids:
                  lowercase							:  Tuple 			=   ids_tensor([self.batch_size, self.num_choices]	,self.seq_length   )

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

           lowercase							:  List[str] 			=   self.get_config(
               gradient_checkpointing=snake_case	,scale_attn_by_inverse_layer_idx=snake_case	,reorder_and_upcast_attn=snake_case	,)

           return (
               config,
               input_ids,
               input_mask,
               None,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case=False	,snake_case=False	,snake_case=False   ):



           '''simple docstring'''







           return RwkvConfig(
               vocab_size=self.vocab_size	,hidden_size=self.hidden_size	,num_hidden_layers=self.num_hidden_layers	,intermediate_size=self.intermediate_size	,activation_function=self.hidden_act	,resid_pdrop=self.hidden_dropout_prob	,attn_pdrop=self.attention_probs_dropout_prob	,n_positions=self.max_position_embeddings	,type_vocab_size=self.type_vocab_size	,use_cache=snake_case	,bos_token_id=self.bos_token_id	,eos_token_id=self.eos_token_id	,pad_token_id=self.pad_token_id	,gradient_checkpointing=snake_case	,scale_attn_by_inverse_layer_idx=snake_case	,reorder_and_upcast_attn=snake_case	,)
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[Any] 			=   self.get_config()
           lowercase							:  str 			=   300
           return config
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           (
               (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    
           )							:  Dict 			=   self.prepare_config_and_inputs()

           lowercase							:  List[str] 			=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
           lowercase							:  Tuple 			=   ids_tensor([self.batch_size, self.seq_length]	,vocab_size=2   )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               sequence_labels,
               token_labels,
               choice_labels,
               encoder_hidden_states,
               encoder_attention_mask,
           )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  Optional[Any] 			=   True
           lowercase							:  Dict 			=   RwkvModel(config=snake_case   )
           model.to(snake_case   )
           model.eval()

           lowercase							:  Optional[Any] 			=   model(snake_case   )

           self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size)   )
           self.parent.assertEqual(len(result.hidden_states   )	,config.num_hidden_layers + 1   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  int 			=   RwkvForCausalLM(snake_case   )
           model.to(snake_case   )
           model.eval()

           lowercase							:  Optional[int] 			=   model(snake_case	,labels=snake_case   )
           self.parent.assertEqual(result.loss.shape	,()   )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  str 			=   RwkvModel(config=snake_case   )
           model.to(snake_case   )
           model.eval()

           lowercase							:  int 			=   model(snake_case   )
           lowercase							:  int 			=   outputs.last_hidden_state

           lowercase							:  Dict 			=   model(input_ids[:, :2]   )
           lowercase							:  Optional[int] 			=   outputs.last_hidden_state

           # Using the state computed on the first inputs, we will get the same output
           lowercase							:  int 			=   model(input_ids[:, 2:]	,state=outputs.state   )
           lowercase							:  Union[str, Any] 			=   outputs.last_hidden_state

           self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]	,dim=1   )	,snake_case	,atol=1e-5   )   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case	,snake_case=False   ):



           '''simple docstring'''







           lowercase							:  Dict 			=   RwkvForCausalLM(snake_case   )
           model.to(snake_case   )
           if gradient_checkpointing:
                  model.gradient_checkpointing_enable()

           lowercase							:  List[str] 			=   model(snake_case	,labels=snake_case   )
           self.parent.assertEqual(result.loss.shape	,()   )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)   )
           result.loss.backward()
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[Any] 			=   self.prepare_config_and_inputs()

           (
               (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    
           )							:  Union[str, Any] 			=   config_and_inputs

           lowercase							:  Optional[Any] 			=   {"""input_ids""": input_ids}

           return config, inputs_dict






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0      ,			reason="See https://github.com/huggingface/transformers/pull/24204"  )
@require_torch
class __snake_case	(   lowerCAmelCase      ,			lowerCAmelCase      ,			lowerCAmelCase      ,			unittest.TestCase  ):
    _a	:		str=       (RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
    _a	:		Tuple=       (
        {"feature-extraction": RwkvModel, "text-generation": RwkvForCausalLM} if is_torch_available() else {}
    )
    # all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
    _a	:		List[str]=       False
    _a	:		int=       False
    _a	:		Optional[Any]=       False
    _a	:		List[Any]=       False
    _a	:		int=       False  # Rwkv does not support head masking
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Dict 			=   RwkvModelTester(self   )
           lowercase							:  Any 			=   ConfigTester(
               self	,config_class=snake_case	,n_embd=37	,common_properties=["""hidden_size""", """num_hidden_layers"""]   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case=None   ):



           '''simple docstring'''







           if isinstance(snake_case	,torch.Tensor   ):
                  lowercase			,    lowercase							:  Tuple 			=   member.max().item(), member.min().item()
           elif isinstance(snake_case	,snake_case   ) or isinstance(snake_case	,snake_case   ):
                  lowercase			,    lowercase							:  Dict 			=   max(snake_case   ), min(snake_case   )

           if not isinstance(snake_case	,snake_case   ):
                  raise TypeError("""container should be a list or tuple"""   )
           elif len(snake_case   ) != 2:
                  raise ValueError("""container should have 2 elements"""   )

           lowercase			,    lowercase							:  int 			=   container

           lowercase							:  Union[str, Any] 			=   (min_value >= expected_min) and (max_value <= expected_max)

           if not is_inside_interval:
                  lowercase							:  Optional[int] 			=   """%s not found in %s""" % (safe_repr(snake_case   ), safe_repr(snake_case   ))
                  self.fail(self._formatMessage(snake_case	,snake_case   )   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           self.config_tester.run_common_tests()
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Any 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_rwkv_model(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Any 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_causl_lm(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Any 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_state_equivalency(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase			,    lowercase							:  Union[str, Any] 			=   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  lowercase							:  List[Any] 			=   model_class(config=snake_case   )
                  for name, param in model.named_parameters():
                         if "time_decay" in name:
                                if param.requires_grad:
                                       self.assertTrue(param.data.max().item() == 3.0   )
                                       self.assertTrue(param.data.min().item() == -5.0   )
                         elif "time_first" in name:
                                if param.requires_grad:
                                       # check if it's a ones like
                                       self.assertTrue(torch.allclose(param.data	,torch.ones_like(param.data   )	,atol=1e-5	,rtol=1e-5   )   )
                         elif any(x in name for x in ["""time_mix_key""", """time_mix_receptance"""]   ):
                                if param.requires_grad:
                                       self.assertInterval(
                                           param.data	,[0.0, 1.0]	,msg=f"Parameter {name} of model {model_class} seems not properly initialized"	,)
                         elif "time_mix_value" in name:
                                if param.requires_grad:
                                       self.assertInterval(
                                           param.data	,[0.0, 1.3]	,msg=f"Parameter {name} of model {model_class} seems not properly initialized"	,)
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase			,    lowercase							:  str 			=   self.model_tester.prepare_config_and_inputs_for_common()
           lowercase							:  List[Any] 			=   True

           lowercase							:  int 			=   getattr(self.model_tester	,"""seq_length"""	,snake_case   )

           for model_class in self.all_model_classes:
                  lowercase							:  Union[str, Any] 			=   True
                  lowercase							:  str 			=   False
                  lowercase							:  int 			=   True
                  lowercase							:  List[str] 			=   model_class(snake_case   )
                  model.to(snake_case   )
                  model.eval()

                  lowercase							:  Tuple 			=   self._prepare_for_class(snake_case	,snake_case   )
                  lowercase							:  Optional[Any] 			=   inputs["""input_ids"""].shape[0]
                  with torch.no_grad():
                         lowercase							:  Any 			=   model(**snake_case   )
                  lowercase							:  List[Any] 			=   outputs.attentions
                  self.assertEqual(len(snake_case   )	,self.model_tester.num_hidden_layers   )

                  # check that output_attentions also work using config
                  del inputs_dict["output_attentions"]
                  lowercase							:  str 			=   True
                  lowercase							:  Any 			=   model_class(snake_case   )
                  model.to(snake_case   )
                  model.eval()

                  lowercase							:  Optional[int] 			=   self._prepare_for_class(snake_case	,snake_case   )
                  lowercase							:  Tuple 			=   inputs["""input_ids"""].shape[0]
                  with torch.no_grad():
                         lowercase							:  List[Any] 			=   model(**snake_case   )
                  lowercase							:  str 			=   outputs.attentions
                  self.assertEqual(len(snake_case   )	,self.model_tester.num_hidden_layers   )

                  self.assertListEqual(
                      list(attentions[0].shape[-3:]   )	,[batch_size, seq_len, config.hidden_size]	,)
                  lowercase							:  Dict 			=   len(snake_case   )

                  # Check attention is always last and order is fine
                  lowercase							:  Any 			=   True
                  lowercase							:  List[str] 			=   True
                  lowercase							:  Optional[Any] 			=   model_class(snake_case   )
                  model.to(snake_case   )
                  model.eval()

                  lowercase							:  Optional[int] 			=   self._prepare_for_class(snake_case	,snake_case   )
                  lowercase							:  Optional[Any] 			=   inputs["""input_ids"""].shape[0]
                  with torch.no_grad():
                         lowercase							:  Union[str, Any] 			=   model(**snake_case   )

                  lowercase							:  Tuple 			=   1
                  self.assertEqual(out_len + added_hidden_states	,len(snake_case   )   )

                  lowercase							:  Optional[Any] 			=   outputs.attentions

                  self.assertEqual(len(snake_case   )	,self.model_tester.num_hidden_layers   )
                  self.assertListEqual(
                      list(self_attentions[0].shape[-3:]   )	,[batch_size, seq_len, config.hidden_size]	,)
    @slow
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase							:  Dict 			=   RwkvModel.from_pretrained(snake_case   )
                  self.assertIsNotNone(snake_case   )






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0      ,			reason="See https://github.com/huggingface/transformers/pull/24204"  )
@slow
class __snake_case	(   unittest.TestCase  ):
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[Any] 			=   """RWKV/rwkv-4-169m-pile"""
           lowercase							:  str 			=   AutoTokenizer.from_pretrained(self.model_id   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[Any] 			=   """Hello my name is Jasmine and I am a newbie to the"""
           lowercase							:  List[Any] 			=   RwkvForCausalLM.from_pretrained(self.model_id   ).to(snake_case   )

           lowercase							:  Union[str, Any] 			=   self.tokenizer("""Hello my name is"""	,return_tensors="""pt"""   ).input_ids.to(snake_case   )
           lowercase							:  Optional[Any] 			=   model.generate(snake_case	,max_new_tokens=10   )
           lowercase							:  List[Any] 			=   self.tokenizer.decode(output[0].tolist()   )

           self.assertEqual(snake_case	,snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Any 			=   """Hello my name is Jasmine and I am a newbie to the"""

           lowercase							:  Optional[Any] 			=   self.tokenizer("""Hello my name is"""	,return_tensors="""pt"""   ).input_ids.to(snake_case   )
           lowercase							:  Union[str, Any] 			=   RwkvForCausalLM.from_pretrained(self.model_id	,torch_dtype=torch.bfloataa   ).to(snake_case   )

           lowercase							:  Tuple 			=   model.generate(snake_case	,max_new_tokens=10   )
           lowercase							:  Optional[int] 			=   self.tokenizer.decode(output[0].tolist()   )

           self.assertEqual(snake_case	,snake_case   )
