






from transformers import (
    DPRContextEncoderTokenizer,
    DPRContextEncoderTokenizerFast,
    DPRQuestionEncoderTokenizer,
    DPRQuestionEncoderTokenizerFast,
    DPRReaderOutput,
    DPRReaderTokenizer,
    DPRReaderTokenizerFast,
)
from transformers.testing_utils import require_tokenizers, slow
from transformers.tokenization_utils_base import BatchEncoding

from ..bert.test_tokenization_bert import BertTokenizationTest

@require_tokenizers
class        _SCREAMING_SNAKE_CASE			(     __UpperCamelCase ):
	snake_case__				:	int	      =    DPRContextEncoderTokenizer
	snake_case__				:	Any	      =    DPRContextEncoderTokenizerFast
	snake_case__				:	Dict	      =    True

@require_tokenizers
class        _SCREAMING_SNAKE_CASE			(     __UpperCamelCase ):
	snake_case__				:	Optional[int]	      =    DPRQuestionEncoderTokenizer
	snake_case__				:	int	      =    DPRQuestionEncoderTokenizerFast
	snake_case__				:	Tuple	      =    True

@require_tokenizers
class        _SCREAMING_SNAKE_CASE			(     __UpperCamelCase ):
	snake_case__				:	Tuple	      =    DPRReaderTokenizer
	snake_case__				:	List[str]	      =    DPRReaderTokenizerFast
	snake_case__				:	str	      =    True





	@slow
	def 		_A					(						self							:   List[str]      ):
		UpperCamelCase	:Any		=      self.tokenizer_class.from_pretrained("""bert-base-uncased"""      )

		UpperCamelCase	:Optional[Any]		=      tokenizer.encode("""question sequence"""					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:Any		=      tokenizer.encode("""title sequence"""					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:Optional[int]		=      tokenizer.encode("""text sequence """ * 4					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:str		=      [[101] + text_a + [102] + text_a + [102] + text_a]
		UpperCamelCase	:Optional[Any]		=      BatchEncoding({"""input_ids""": input_ids}      )

		UpperCamelCase	:Any		=      [[0] * len(input_ids[0]      )]
		UpperCamelCase	:Optional[Any]		=      [[0] * len(input_ids[0]      )]
		UpperCamelCase	:int		=      [0]
		UpperCamelCase	:Optional[Any]		=      DPRReaderOutput(__lowercase					,    __lowercase					,    __lowercase      )

		UpperCamelCase	, UpperCamelCase	:Optional[int]		=      8, 9
		UpperCamelCase	:Dict		=      10
		UpperCamelCase	:str		=      10
		UpperCamelCase	:Tuple		=      tokenizer.decode_best_spans(__lowercase					,    __lowercase      )
		self.assertEqual(predicted_spans[0].start_index					,    __lowercase      )
		self.assertEqual(predicted_spans[0].end_index					,    __lowercase      )
		self.assertEqual(predicted_spans[0].doc_id					,    0      )





	@slow
	def 		_A					(						self							:   int      ):
		UpperCamelCase	:Tuple		=      self.tokenizer_class.from_pretrained("""bert-base-uncased"""      )

		UpperCamelCase	:Any		=      tokenizer.encode("""question sequence"""					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:int		=      tokenizer.encode("""title sequence"""					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:Dict		=      tokenizer.encode("""text sequence"""					,    add_special_tokens=__lowercase      )
		UpperCamelCase	:List[str]		=      [101] + text_a + [102] + text_a + [102] + text_a
		UpperCamelCase	:Optional[int]		=      tokenizer(questions=["""question sequence"""]					,    titles=["""title sequence"""]					,    texts=["""text sequence"""]      )
		self.assertIn("""input_ids"""					,    __lowercase      )
		self.assertIn("""attention_mask"""					,    __lowercase      )
		self.assertListEqual(encoded_input["""input_ids"""][0]					,    __lowercase      )








"""simple docstring"""



import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
 import torch

 from transformers import GPTaLMHeadModel


@require_torch
@require_sentencepiece
@require_tokenizers
class 	_UpperCamelCase ( unittest.TestCase							):



    '''simple docstring'''


    @slow
    @unittest.skip("""Model is not available."""			)
    def 		A__      (      self			):
     UpperCAmelCase__ 						=				"""nvidia/megatron-gpt2-345m/"""
     if "MYDIR" in os.environ:
      UpperCAmelCase__ 						=				os.path.join(os.environ["""MYDIR"""]   ,			__lowercase			)
     UpperCAmelCase__ 						=				GPTaLMHeadModel.from_pretrained(__lowercase			)
     model.to(__lowercase			)
     model.half()

     UpperCAmelCase__ 						=				torch.tensor(
         [[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]   ,			device=__lowercase   ,			dtype=torch.long   ,			)

     with torch.no_grad():
      UpperCAmelCase__ 						=				model(__lowercase			).logits

     UpperCAmelCase__ 						=				torch.Size((1, 9, 50257)			)
     self.assertEqual(output.shape   ,			__lowercase			)

     UpperCAmelCase__ 						=				torch.tensor(
         [
             4.9_414,
             -0.2_920,
             -1.2_148,
             -4.0_273,
             -0.5_161,
             -5.2_109,
             -1.2_412,
             -1.8_301,
             -1.7_734,
             -4.7_148,
             -0.2_317,
             -1.0_811,
             -2.1_777,
             0.4_141,
             -3.7_969,
             -4.0_586,
             -2.5_332,
             -3.3_809,
             4.3_867,
         ]   ,			device=__lowercase   ,			dtype=torch.half   ,			)

     for i in range(19			):
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				8 * i // 17, 2792 * i  # along the diagonal
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				output[0, r, c], expected_diag[i]
      UpperCAmelCase__ 						=				F'''row={r} col={c} computed={computed} expected={expected}'''
      self.assertAlmostEqual(__lowercase   ,			__lowercase   ,			delta=1e-4   ,			msg=__lowercase			)



