




import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel







class __snake_case			:







   def __init__(      self		:						str					,						_snake_case		:						Tuple					,						_snake_case		:						List[str]=13					,						_snake_case		:						Tuple=7					,						_snake_case		:						Optional[Any]=True					,						_snake_case		:						List[str]=True					,						_snake_case		:						Dict=False					,						_snake_case		:						List[str]=True					,						_snake_case		:						Optional[int]=99					,						_snake_case		:						Optional[int]=32					,						_snake_case		:						Dict=5					,						_snake_case		:						List[Any]=4					,						_snake_case		:						Optional[int]=37					,						_snake_case		:						Optional[Any]="gelu"					,						_snake_case		:						Tuple=0.1					,						_snake_case		:						Tuple=0.1					,						_snake_case		:						int=512					,						_snake_case		:						int=16					,						_snake_case		:						List[Any]=2					,						_snake_case		:						Any=0.0_2					,						_snake_case		:						str=3					,						_snake_case		:						Union[str, Any]=4					,						_snake_case		:						str=None					,						):

        """simple docstring"""


        UpperCAmelCase_          =							parent
        UpperCAmelCase_          =							batch_size
        UpperCAmelCase_          =							seq_length
        UpperCAmelCase_          =							is_training
        UpperCAmelCase_          =							use_input_mask
        UpperCAmelCase_          =							use_token_type_ids
        UpperCAmelCase_          =							use_labels
        UpperCAmelCase_          =							vocab_size
        UpperCAmelCase_          =							hidden_size
        UpperCAmelCase_          =							num_hidden_layers
        UpperCAmelCase_          =							num_attention_heads
        UpperCAmelCase_          =							intermediate_size
        UpperCAmelCase_          =							hidden_act
        UpperCAmelCase_          =							hidden_dropout_prob
        UpperCAmelCase_          =							attention_probs_dropout_prob
        UpperCAmelCase_          =							max_position_embeddings
        UpperCAmelCase_          =							type_vocab_size
        UpperCAmelCase_          =							type_sequence_label_size
        UpperCAmelCase_          =							initializer_range
        UpperCAmelCase_          =							num_labels
        UpperCAmelCase_          =							num_choices
        UpperCAmelCase_          =							scope







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size)

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

        UpperCAmelCase_          =							None
        if self.use_token_type_ids:
             UpperCAmelCase_          =							ids_tensor([self.batch_size, self.seq_length]					,						self.type_vocab_size)

        UpperCAmelCase_          =							None
        UpperCAmelCase_          =							None
        UpperCAmelCase_          =							None
        if self.use_labels:
             UpperCAmelCase_          =							ids_tensor([self.batch_size]					,						self.type_sequence_label_size)
             UpperCAmelCase_          =							ids_tensor([self.batch_size, self.seq_length]					,						self.num_labels)
             UpperCAmelCase_          =							ids_tensor([self.batch_size]					,						self.num_choices)

        UpperCAmelCase_          =							self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        return OpenLlamaConfig(
            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=_snake_case					,						initializer_range=self.initializer_range					,						use_stable_embedding=_snake_case					,						)







   def lowerCamelCase						(      self		:						Optional[Any]					,						_snake_case		:						int					,						_snake_case		:						str					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						Dict					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Any					,						_snake_case		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							OpenLlamaModel(config=_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case)
        UpperCAmelCase_          =							model(_snake_case)
        self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))







   def lowerCamelCase						(      self		:						Dict					,						_snake_case		:						Optional[int]					,						_snake_case		:						Dict					,						_snake_case		:						List[Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						Any					,						_snake_case		:						List[Any]					,						_snake_case		:						str					,						_snake_case		:						Optional[int]					,						_snake_case		:						str					,						):

        """simple docstring"""


        UpperCAmelCase_          =							True
        UpperCAmelCase_          =							OpenLlamaModel(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(
            _snake_case					,						attention_mask=_snake_case					,						encoder_hidden_states=_snake_case					,						encoder_attention_mask=_snake_case					,						)
        UpperCAmelCase_          =							model(
            _snake_case					,						attention_mask=_snake_case					,						encoder_hidden_states=_snake_case					,						)
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case)
        self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))







   def lowerCamelCase						(      self		:						Union[str, Any]					,						_snake_case		:						List[Any]					,						_snake_case		:						Tuple					,						_snake_case		:						int					,						_snake_case		:						List[Any]					,						_snake_case		:						Any					,						_snake_case		:						str					,						_snake_case		:						int					,						_snake_case		:						Optional[int]					,						_snake_case		:						Union[str, Any]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							OpenLlamaForCausalLM(config=_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case					,						labels=_snake_case)
        self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size))







   def lowerCamelCase						(      self		:						List[str]					,						_snake_case		:						Union[str, Any]					,						_snake_case		:						Optional[int]					,						_snake_case		:						Any					,						_snake_case		:						Tuple					,						_snake_case		:						List[Any]					,						_snake_case		:						str					,						_snake_case		:						Tuple					,						_snake_case		:						Tuple					,						_snake_case		:						Dict					,						):

        """simple docstring"""


        UpperCAmelCase_          =							True
        UpperCAmelCase_          =							True
        UpperCAmelCase_          =							OpenLlamaForCausalLM(config=_snake_case)
        model.to(_snake_case)
        model.eval()

        # first forward pass
        UpperCAmelCase_          =							model(
            _snake_case					,						attention_mask=_snake_case					,						encoder_hidden_states=_snake_case					,						encoder_attention_mask=_snake_case					,						use_cache=_snake_case					,						)
        UpperCAmelCase_          =							outputs.past_key_values

        # create hypothetical multiple next token and extent to next_input_ids
        UpperCAmelCase_          =							ids_tensor((self.batch_size, 3)					,						config.vocab_size)
        UpperCAmelCase_          =							ids_tensor((self.batch_size, 3)					,						vocab_size=2)

        # append to next input_ids and
        UpperCAmelCase_          =							torch.cat([input_ids, next_tokens]					,						dim=-1)
        UpperCAmelCase_          =							torch.cat([input_mask, next_mask]					,						dim=-1)

        UpperCAmelCase_          =							model(
            _snake_case					,						attention_mask=_snake_case					,						encoder_hidden_states=_snake_case					,						encoder_attention_mask=_snake_case					,						output_hidden_states=_snake_case					,						)['''hidden_states'''][0]
        UpperCAmelCase_          =							model(
            _snake_case					,						attention_mask=_snake_case					,						encoder_hidden_states=_snake_case					,						encoder_attention_mask=_snake_case					,						past_key_values=_snake_case					,						output_hidden_states=_snake_case					,						)['''hidden_states'''][0]

        # select random slice
        UpperCAmelCase_          =							ids_tensor((1,)					,						output_from_past.shape[-1]).item()
        UpperCAmelCase_          =							output_from_no_past[:, -3:, random_slice_idx].detach()
        UpperCAmelCase_          =							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(_snake_case					,						_snake_case					,						atol=1e-3))







   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        UpperCAmelCase_          =							self.prepare_config_and_inputs()
        (
            (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       (
            UpperCAmelCase_
        )     ,       
        )          =							config_and_inputs
        UpperCAmelCase_          =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
        return config, inputs_dict







@require_torch
class __snake_case			(			a ,  a ,  a ,  unittest.TestCase		):
   UpperCAmelCase__  :    Dict          =  (
       (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
   )
   UpperCAmelCase__  :    int          =  (OpenLlamaForCausalLM,) if is_torch_available() else ()
   UpperCAmelCase__  :    Union[str, Any]          =  (
       {
           '''feature-extraction''': OpenLlamaModel,
           '''text-classification''': OpenLlamaForSequenceClassification,
           '''text-generation''': OpenLlamaForCausalLM,
           '''zero-shot''': OpenLlamaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   UpperCAmelCase__  :    Any          =  False
   UpperCAmelCase__  :    Dict          =  False







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							OpenLlamaModelTester(self)
        UpperCAmelCase_          =							ConfigTester(self					,						config_class=_snake_case					,						hidden_size=37)







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        self.config_tester.run_common_tests()







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_snake_case)







   def lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
             UpperCAmelCase_          =							type
             self.model_tester.create_and_check_model(*_snake_case)







   def lowerCamelCase						(      self		:						Union[str, Any]):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()
        UpperCAmelCase_          =							3
        UpperCAmelCase_          =							input_dict['''input_ids''']
        UpperCAmelCase_          =							input_ids.ne(1).to(_snake_case)
        UpperCAmelCase_          =							ids_tensor([self.model_tester.batch_size]					,						self.model_tester.type_sequence_label_size)
        UpperCAmelCase_          =							OpenLlamaForSequenceClassification(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case					,						labels=_snake_case)
        self.assertEqual(result.logits.shape					,						(self.model_tester.batch_size, self.model_tester.num_labels))







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()
        UpperCAmelCase_          =							3
        UpperCAmelCase_          =							'''single_label_classification'''
        UpperCAmelCase_          =							input_dict['''input_ids''']
        UpperCAmelCase_          =							input_ids.ne(1).to(_snake_case)
        UpperCAmelCase_          =							ids_tensor([self.model_tester.batch_size]					,						self.model_tester.type_sequence_label_size)
        UpperCAmelCase_          =							OpenLlamaForSequenceClassification(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case					,						labels=_snake_case)
        self.assertEqual(result.logits.shape					,						(self.model_tester.batch_size, self.model_tester.num_labels))







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()
        UpperCAmelCase_          =							3
        UpperCAmelCase_          =							'''multi_label_classification'''
        UpperCAmelCase_          =							input_dict['''input_ids''']
        UpperCAmelCase_          =							input_ids.ne(1).to(_snake_case)
        UpperCAmelCase_          =							ids_tensor(
            [self.model_tester.batch_size, config.num_labels]					,						self.model_tester.type_sequence_label_size).to(torch.float)
        UpperCAmelCase_          =							OpenLlamaForSequenceClassification(_snake_case)
        model.to(_snake_case)
        model.eval()
        UpperCAmelCase_          =							model(_snake_case					,						attention_mask=_snake_case					,						labels=_snake_case)
        self.assertEqual(result.logits.shape					,						(self.model_tester.batch_size, self.model_tester.num_labels))







   @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''')
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        pass







   @parameterized.expand([('''linear''',), ('''dynamic''',)])
   def lowerCamelCase						(      self		:						List[Any]					,						_snake_case		:						List[Any]):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()
        UpperCAmelCase_          =							ids_tensor([1, 10]					,						config.vocab_size)
        UpperCAmelCase_          =							ids_tensor([1, int(config.max_position_embeddings * 1.5)]					,						config.vocab_size)

        set_seed(42)  # Fixed seed at init time so the two models get the same random weights
        UpperCAmelCase_          =							OpenLlamaModel(_snake_case)
        original_model.to(_snake_case)
        original_model.eval()
        UpperCAmelCase_          =							original_model(_snake_case).last_hidden_state
        UpperCAmelCase_          =							original_model(_snake_case).last_hidden_state

        set_seed(42)  # Fixed seed at init time so the two models get the same random weights
        UpperCAmelCase_          =							{'''type''': scaling_type, '''factor''': 1_0.0}
        UpperCAmelCase_          =							OpenLlamaModel(_snake_case)
        scaled_model.to(_snake_case)
        scaled_model.eval()
        UpperCAmelCase_          =							scaled_model(_snake_case).last_hidden_state
        UpperCAmelCase_          =							scaled_model(_snake_case).last_hidden_state

        # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
        # maximum sequence length, so the outputs for the short input should match.
        if scaling_type == "dynamic":
             self.assertTrue(torch.allclose(_snake_case					,						_snake_case					,						atol=1e-5))
        else:
             self.assertFalse(torch.allclose(_snake_case					,						_snake_case					,						atol=1e-5))

        # The output should be different for long inputs
        self.assertFalse(torch.allclose(_snake_case					,						_snake_case					,						atol=1e-5))












from datetime import datetime

import requests


def    A						(__A   :    str       )   ->      bytes:







     """simple docstring"""


     UpperCAmelCase_          =							'''https://downloadgram.net/wp-json/wppress/video-downloader/video?url='''
     UpperCAmelCase_          =							requests.get(base_url + url       ).json()[0]['''urls'''][0]['''src''']
     return requests.get(__A       ).content


if __name__ == "__main__":
    snake_case_ : Optional[Any]    		=       input("Enter Video/IGTV url: ").strip()
    snake_case_ : Any    		=       f"{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4"
    with open(file_name, "wb") as fp:
        fp.write(download_video(url))
    print(f"Done. Video saved to disk as {file_name}.")






