






'''simple docstring'''

import unittest

from transformers import BertGenerationConfig, 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 BertGenerationDecoder, BertGenerationEncoder




class     A    :






 """simple docstring"""





 def __init__(							self			,     __lowerCAmelCase			,     __lowerCAmelCase=13			,     __lowerCAmelCase=7			,     __lowerCAmelCase=True			,     __lowerCAmelCase=True			,     __lowerCAmelCase=99			,     __lowerCAmelCase=32			,     __lowerCAmelCase=5			,     __lowerCAmelCase=4			,     __lowerCAmelCase=37			,     __lowerCAmelCase="gelu"			,     __lowerCAmelCase=0.1			,     __lowerCAmelCase=0.1			,     __lowerCAmelCase=50			,     __lowerCAmelCase=0.02			,     __lowerCAmelCase=True			,     __lowerCAmelCase=None			,     ):
       UpperCamelCase_				:   Optional[Any]					    =							parent
       UpperCamelCase_				:   Optional[Any]					    =							batch_size
       UpperCamelCase_				:   Any					    =							seq_length
       UpperCamelCase_				:   Any					    =							is_training
       UpperCamelCase_				:   List[Any]					    =							use_input_mask
       UpperCamelCase_				:   int					    =							vocab_size
       UpperCamelCase_				:   int					    =							hidden_size
       UpperCamelCase_				:   Union[str, Any]					    =							num_hidden_layers
       UpperCamelCase_				:   Dict					    =							num_attention_heads
       UpperCamelCase_				:   Optional[int]					    =							intermediate_size
       UpperCamelCase_				:   Any					    =							hidden_act
       UpperCamelCase_				:   Any					    =							hidden_dropout_prob
       UpperCamelCase_				:   Optional[Any]					    =							attention_probs_dropout_prob
       UpperCamelCase_				:   int					    =							max_position_embeddings
       UpperCamelCase_				:   Optional[int]					    =							initializer_range
       UpperCamelCase_				:   Optional[int]					    =							use_labels
       UpperCamelCase_				:   List[Any]					    =							scope
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Any					    =							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size )

       UpperCamelCase_				:   Union[str, Any]					    =							None
       if self.use_input_mask:
             UpperCamelCase_				:   Optional[Any]					    =							random_attention_mask([self.batch_size, self.seq_length] )

       if self.use_labels:
             UpperCamelCase_				:   List[Any]					    =							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size )

       UpperCamelCase_				:   Any					    =							self.get_config()

       return config, input_ids, input_mask, token_labels
 def   _UpperCAmelCase       (							self ):
       return BertGenerationConfig(
           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			,     is_decoder=A__			,     initializer_range=self.initializer_range			,     )
 def   _UpperCAmelCase       (							self ):
       (
           UpperCamelCase_     
       )				:   List[str]					    =							self.prepare_config_and_inputs()

       UpperCamelCase_				:   List[str]					    =							True
       UpperCamelCase_				:   str					    =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
       UpperCamelCase_				:   Optional[int]					    =							ids_tensor([self.batch_size, self.seq_length]			,     vocab_size=2 )

       return (
           config,
           input_ids,
           input_mask,
           token_labels,
           encoder_hidden_states,
           encoder_attention_mask,
       )
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     **__lowerCAmelCase			,     ):
       UpperCamelCase_				:   List[str]					    =							BertGenerationEncoder(config=A__ )
       model.to(A__ )
       model.eval()
       UpperCamelCase_				:   Optional[int]					    =							model(A__			,     attention_mask=A__ )
       UpperCamelCase_				:   List[str]					    =							model(A__ )
       self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size) )
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     **__lowerCAmelCase			,     ):
       UpperCamelCase_				:   List[str]					    =							True
       UpperCamelCase_				:   List[Any]					    =							BertGenerationEncoder(config=A__ )
       model.to(A__ )
       model.eval()
       UpperCamelCase_				:   List[str]					    =							model(
           A__			,     attention_mask=A__			,     encoder_hidden_states=A__			,     encoder_attention_mask=A__			,     )
       UpperCamelCase_				:   Any					    =							model(
           A__			,     attention_mask=A__			,     encoder_hidden_states=A__			,     )
       self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size) )
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     **__lowerCAmelCase			,     ):
       UpperCamelCase_				:   str					    =							True
       UpperCamelCase_				:   Dict					    =							True
       UpperCamelCase_				:   str					    =							BertGenerationDecoder(config=A__ ).to(A__ ).eval()

       # first forward pass
       UpperCamelCase_				:   Any					    =							model(
           A__			,     attention_mask=A__			,     encoder_hidden_states=A__			,     encoder_attention_mask=A__			,     use_cache=A__			,     )
       UpperCamelCase_				:   List[Any]					    =							outputs.past_key_values

       # create hypothetical multiple next token and extent to next_input_ids
       UpperCamelCase_				:   int					    =							ids_tensor((self.batch_size, 3)			,     config.vocab_size )
       UpperCamelCase_				:   List[Any]					    =							ids_tensor((self.batch_size, 3)			,     vocab_size=2 )

       # append to next input_ids and
       UpperCamelCase_				:   Union[str, Any]					    =							torch.cat([input_ids, next_tokens]			,     dim=-1 )
       UpperCamelCase_				:   Dict					    =							torch.cat([input_mask, next_mask]			,     dim=-1 )

       UpperCamelCase_				:   Optional[int]					    =							model(
           A__			,     attention_mask=A__			,     encoder_hidden_states=A__			,     encoder_attention_mask=A__			,     output_hidden_states=A__			,     )["""hidden_states"""][0]
       UpperCamelCase_				:   Dict					    =							model(
           A__			,     attention_mask=A__			,     encoder_hidden_states=A__			,     encoder_attention_mask=A__			,     past_key_values=A__			,     output_hidden_states=A__			,     )["""hidden_states"""][0]

       # select random slice
       UpperCamelCase_				:   str					    =							ids_tensor((1,)			,     output_from_past.shape[-1] ).item()
       UpperCamelCase_				:   int					    =							output_from_no_past[:, -3:, random_slice_idx].detach()
       UpperCamelCase_				:   Dict					    =							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(A__			,     A__			,     atol=1E-3 ) )
 def   _UpperCAmelCase       (							self			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     __lowerCAmelCase			,     *__lowerCAmelCase			,     ):
       UpperCamelCase_				:   Dict					    =							BertGenerationDecoder(A__ )
       model.to(A__ )
       model.eval()
       UpperCamelCase_				:   Tuple					    =							model(A__			,     attention_mask=A__			,     labels=A__ )
       self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size) )





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   str					    =							self.prepare_config_and_inputs()
       UpperCamelCase_				:   Dict					    =							{"""input_ids""": input_ids, """attention_mask""": input_mask}
       return config, inputs_dict






@require_torch
class     A    (      SCREAMING_SNAKE_CASE__,   SCREAMING_SNAKE_CASE__,   SCREAMING_SNAKE_CASE__,   unittest.TestCase						):






 """simple docstring"""





 __a			:						Union[str, Any]      =						(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
 __a			:						Union[str, Any]      =						(BertGenerationDecoder,) if is_torch_available() else ()
 __a			:						Optional[Any]      =						(
     {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
     if is_torch_available()
     else {}
 )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Union[str, Any]					    =							BertGenerationEncoderTester(self )
       UpperCamelCase_				:   str					    =							ConfigTester(self			,     config_class=A__			,     hidden_size=37 )
 def   _UpperCAmelCase       (							self ):
       self.config_tester.run_common_tests()
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[Any]					    =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A__ )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Dict					    =							self.model_tester.prepare_config_and_inputs()
       UpperCamelCase_				:   int					    =							"""bert"""
       self.model_tester.create_and_check_model(A__			,     A__			,     A__			,     A__ )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Union[str, Any]					    =							self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(*A__ )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Dict					    =							self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(*A__ )
 def   _UpperCAmelCase       (							self ):
       # This regression test was failing with PyTorch < 1.3
       (
           UpperCamelCase_     
       )				:   Union[str, Any]					    =							self.model_tester.prepare_config_and_inputs_for_decoder()

       UpperCamelCase_				:   List[str]					    =							None

       self.model_tester.create_and_check_model_as_decoder(
           A__			,     A__			,     A__			,     A__			,     A__			,     A__			,     )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[Any]					    =							self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_for_causal_lm(*A__ )





 @slow
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   int					    =							BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder""" )
       self.assertIsNotNone(A__ )






@require_torch
class     A    (      unittest.TestCase						):






 """simple docstring"""





 @slow
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Tuple					    =							BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder""" )
       UpperCamelCase_				:   Optional[Any]					    =							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]] )
       with torch.no_grad():
             UpperCamelCase_				:   Union[str, Any]					    =							model(A__ )[0]
       UpperCamelCase_				:   str					    =							torch.Size([1, 8, 10_24] )
       self.assertEqual(output.shape			,     A__ )
       UpperCamelCase_				:   Tuple					    =							torch.tensor(
           [[[0.17_75, 0.00_83, -0.03_21], [1.60_02, 0.12_87, 0.39_12], [2.14_73, 0.57_91, 0.60_66]]] )
       self.assertTrue(torch.allclose(output[:, :3, :3]			,     A__			,     atol=1E-4 ) )






@require_torch
class     A    (      unittest.TestCase						):






 """simple docstring"""





 @slow
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[int]					    =							BertGenerationDecoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder""" )
       UpperCamelCase_				:   Union[str, Any]					    =							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]] )
       with torch.no_grad():
             UpperCamelCase_				:   str					    =							model(A__ )[0]
       UpperCamelCase_				:   str					    =							torch.Size([1, 8, 5_03_58] )
       self.assertEqual(output.shape			,     A__ )
       UpperCamelCase_				:   Optional[Any]					    =							torch.tensor(
           [[[-0.57_88, -2.59_94, -3.70_54], [0.04_38, 4.79_97, 1.87_95], [1.58_62, 6.64_09, 4.46_38]]] )
       self.assertTrue(torch.allclose(output[:, :3, :3]			,     A__			,     atol=1E-4 ) )














'''simple docstring'''

from __future__ import annotations

import os
from typing import Any

import requests

UpperCamelCase							="https://api.github.com"

# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
UpperCamelCase							=BASE_URL + "/user"

# https://github.com/settings/tokens
UpperCamelCase							=os.environ.get("USER_TOKEN", "")



def 							snake_case					(     a_							: str	)       ->				dict[Any, Any]:





      """simple docstring"""

      UpperCamelCase_				:   Tuple					    =							{
          """Authorization""": f"token {auth_token}",
          """Accept""": """application/vnd.github.v3+json""",
      }
      return requests.get(a_    ,   headers=a_	).json()


if __name__ == "__main__":  # pragma: no cover
      if USER_TOKEN:
            for key, value in fetch_github_info(USER_TOKEN).items():
                  print(f"{key}: {value}")
      else:
            raise ValueError("'USER_TOKEN' field cannot be empty.")






