






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 				__snake_case  (     __lowerCamelCase						):


    '''simple docstring'''







    lowerCamelCase__						:		Dict							=  DPRContextEncoderTokenizer
    lowerCamelCase__						:		int							=  DPRContextEncoderTokenizerFast
    lowerCamelCase__						:		str							=  True





@require_tokenizers
class 				__snake_case  (     __lowerCamelCase						):


    '''simple docstring'''







    lowerCamelCase__						:		Union[str, Any]							=  DPRQuestionEncoderTokenizer
    lowerCamelCase__						:		Union[str, Any]							=  DPRQuestionEncoderTokenizerFast
    lowerCamelCase__						:		Any							=  True





@require_tokenizers
class 				__snake_case  (     __lowerCamelCase						):


    '''simple docstring'''







    lowerCamelCase__						:		List[str]							=  DPRReaderTokenizer
    lowerCamelCase__						:		Union[str, Any]							=  DPRReaderTokenizerFast
    lowerCamelCase__						:		Optional[Any]							=  True



    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.tokenizer_class.from_pretrained('''bert-base-uncased'''    )

        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''question sequence'''      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''title sequence'''      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''text sequence ''' * 4      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= [[1_01] + text_a + [1_02] + text_a + [1_02] + text_a]
        SCREAMING_SNAKE_CASE__       							= BatchEncoding({'''input_ids''': input_ids}    )

        SCREAMING_SNAKE_CASE__       							= [[0] * len(input_ids[0]    )]
        SCREAMING_SNAKE_CASE__       							= [[0] * len(input_ids[0]    )]
        SCREAMING_SNAKE_CASE__       							= [0]
        SCREAMING_SNAKE_CASE__       							= DPRReaderOutput(A_      ,							A_      ,							A_    )

        SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= 8, 9
        SCREAMING_SNAKE_CASE__       							= 10
        SCREAMING_SNAKE_CASE__       							= 10
        SCREAMING_SNAKE_CASE__       							= tokenizer.decode_best_spans(A_      ,							A_    )
        self.assertEqual(predicted_spans[0].start_index      ,							A_    )
        self.assertEqual(predicted_spans[0].end_index      ,							A_    )
        self.assertEqual(predicted_spans[0].doc_id      ,							0    )




    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.tokenizer_class.from_pretrained('''bert-base-uncased'''    )

        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''question sequence'''      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''title sequence'''      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= tokenizer.encode('''text sequence'''      ,							add_special_tokens=A_    )
        SCREAMING_SNAKE_CASE__       							= [1_01] + text_a + [1_02] + text_a + [1_02] + text_a
        SCREAMING_SNAKE_CASE__       							= tokenizer(questions=['''question sequence''']      ,							titles=['''title sequence''']      ,							texts=['''text sequence''']    )
        self.assertIn('''input_ids'''      ,							A_    )
        self.assertIn('''attention_mask'''      ,							A_    )
        self.assertListEqual(encoded_input['''input_ids'''][0]      ,							A_    )







import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
  import transformer_engine.pytorch as te


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True  )							->	Tuple:
    if not is_fpa_available():
        raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''  )
    for name, module in model.named_children():
        if isinstance(lowerCAmelCase_   ,       nn.Linear  ) and to_transformer_engine and _convert_linear:
            # Return early if the linear layer weights are not multiples of 16
            if any(p % 1_6 != 0 for p in module.weight.shape  ):
                return
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= te.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       nn.LayerNorm  ) and to_transformer_engine and _convert_ln:
            SCREAMING_SNAKE_CASE__       							= te.LayerNorm(module.normalized_shape[0]   ,       eps=module.eps   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       te.Linear  ) and not to_transformer_engine and _convert_linear:
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= nn.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       te.LayerNorm  ) and not to_transformer_engine and _convert_ln:
            SCREAMING_SNAKE_CASE__       							= nn.LayerNorm(module.normalized_shape[0]   ,       eps=module.eps   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        else:
            convert_model(
                lowerCAmelCase_   ,       to_transformer_engine=lowerCAmelCase_   ,       _convert_linear=_convert_linear   ,       _convert_ln=_convert_ln   ,       )



def        __snake_case       (						lowerCAmelCase_  )							->	Union[str, Any]:
    if not is_fpa_available():
        raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''  )
    for m in model.modules():
        if isinstance(lowerCAmelCase_   ,       (te.LayerNorm, te.Linear)  ):
            return True
    return False
