






'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import numpy
			import tensorflow as tf

			from transformers import (
			    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
			    BertConfig,
			    DPRConfig,
			    TFDPRContextEncoder,
			    TFDPRQuestionEncoder,
			    TFDPRReader,
			)

class _lowercase	:




							def __init__(							self:				Union[str, Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				Any=13			,							UpperCamelCase__:				str=7			,							UpperCamelCase__:				Optional[Any]=True			,							UpperCamelCase__:				int=True			,							UpperCamelCase__:				List[Any]=True			,							UpperCamelCase__:				int=True			,							UpperCamelCase__:				Union[str, Any]=99			,							UpperCamelCase__:				Optional[Any]=32			,							UpperCamelCase__:				List[Any]=2			,							UpperCamelCase__:				str=4			,							UpperCamelCase__:				str=37			,							UpperCamelCase__:				Tuple="gelu"			,							UpperCamelCase__:				str=0.1			,							UpperCamelCase__:				str=0.1			,							UpperCamelCase__:				Optional[int]=512			,							UpperCamelCase__:				Dict=16			,							UpperCamelCase__:				int=2			,							UpperCamelCase__:				Dict=0.02			,							UpperCamelCase__:				int=3			,							UpperCamelCase__:				List[Any]=4			,							UpperCamelCase__:				Optional[int]=None			,							UpperCamelCase__:				Union[str, Any]=0			,							):
														lowerCamelCase__					:	List[Any]         =       parent
														lowerCamelCase__					:	int         =       batch_size
														lowerCamelCase__					:	Optional[int]         =       seq_length
														lowerCamelCase__					:	int         =       is_training
														lowerCamelCase__					:	Optional[Any]         =       use_input_mask
														lowerCamelCase__					:	Dict         =       use_token_type_ids
														lowerCamelCase__					:	List[str]         =       use_labels
														lowerCamelCase__					:	List[str]         =       vocab_size
														lowerCamelCase__					:	Tuple         =       hidden_size
														lowerCamelCase__					:	Dict         =       num_hidden_layers
														lowerCamelCase__					:	Tuple         =       num_attention_heads
														lowerCamelCase__					:	Dict         =       intermediate_size
														lowerCamelCase__					:	Any         =       hidden_act
														lowerCamelCase__					:	Tuple         =       hidden_dropout_prob
														lowerCamelCase__					:	Optional[Any]         =       attention_probs_dropout_prob
														lowerCamelCase__					:	int         =       max_position_embeddings
														lowerCamelCase__					:	List[Any]         =       type_vocab_size
														lowerCamelCase__					:	List[str]         =       type_sequence_label_size
														lowerCamelCase__					:	str         =       initializer_range
														lowerCamelCase__					:	str         =       num_labels
														lowerCamelCase__					:	str         =       num_choices
														lowerCamelCase__					:	Optional[Any]         =       scope
														lowerCamelCase__					:	int         =       projection_dim




							def 		lowerCamelCase_				(							self:				int     ):
														lowerCamelCase__					:	Optional[int]         =       ids_tensor([self.batch_size, self.seq_length]			,							self.vocab_size     )

														lowerCamelCase__					:	Dict         =       None
														if self.use_input_mask:
																					# follow test_modeling_tf_ctrl.py
																					lowerCamelCase__					:	Dict         =       random_attention_mask([self.batch_size, self.seq_length]     )

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

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

														lowerCamelCase__					:	str         =       BertConfig(
														    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=SCREAMING_SNAKE_CASE__			,							initializer_range=self.initializer_range			,							)
														lowerCamelCase__					:	List[Any]         =       DPRConfig(projection_dim=self.projection_dim			,							**config.to_dict()     )

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




							def 		lowerCamelCase_				(							self:				List[str]			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Tuple			,							UpperCamelCase__:				Optional[int]			,							UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				Dict     ):
														lowerCamelCase__					:	Optional[Any]         =       TFDPRContextEncoder(config=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	List[Any]         =       model(SCREAMING_SNAKE_CASE__			,							attention_mask=SCREAMING_SNAKE_CASE__			,							token_type_ids=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	int         =       model(SCREAMING_SNAKE_CASE__			,							token_type_ids=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	Tuple         =       model(SCREAMING_SNAKE_CASE__     )
														self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size)     )




							def 		lowerCamelCase_				(							self:				List[Any]			,							UpperCamelCase__:				Any			,							UpperCamelCase__:				Any			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				Any			,							UpperCamelCase__:				Tuple			,							UpperCamelCase__:				Dict     ):
														lowerCamelCase__					:	Tuple         =       TFDPRQuestionEncoder(config=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	List[Any]         =       model(SCREAMING_SNAKE_CASE__			,							attention_mask=SCREAMING_SNAKE_CASE__			,							token_type_ids=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	int         =       model(SCREAMING_SNAKE_CASE__			,							token_type_ids=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	Any         =       model(SCREAMING_SNAKE_CASE__     )
														self.parent.assertEqual(result.pooler_output.shape			,							(self.batch_size, self.projection_dim or self.hidden_size)     )




							def 		lowerCamelCase_				(							self:				Optional[int]			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				List[str]			,							UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Tuple			,							UpperCamelCase__:				str			,							UpperCamelCase__:				Optional[Any]     ):
														lowerCamelCase__					:	Any         =       TFDPRReader(config=SCREAMING_SNAKE_CASE__     )
														lowerCamelCase__					:	Dict         =       model(SCREAMING_SNAKE_CASE__			,							attention_mask=SCREAMING_SNAKE_CASE__     )

														self.parent.assertEqual(result.start_logits.shape			,							(self.batch_size, self.seq_length)     )
														self.parent.assertEqual(result.end_logits.shape			,							(self.batch_size, self.seq_length)     )
														self.parent.assertEqual(result.relevance_logits.shape			,							(self.batch_size,)     )




							def 		lowerCamelCase_				(							self:				str     ):
														lowerCamelCase__					:	int         =       self.prepare_config_and_inputs()
														(
														    lowerCamelCase__
														)					:	List[Any]         =       config_and_inputs
														lowerCamelCase__					:	Any         =       {'''input_ids''': input_ids}
														return config, inputs_dict



@require_tf
class _lowercase	(				_lowercase				,      _lowercase				,      unittest.TestCase		):
							a				      =       (
							    (
							        TFDPRContextEncoder,
							        TFDPRQuestionEncoder,
							        TFDPRReader,
							    )
							    if is_tf_available()
							    else ()
							)
							a				      =       {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

							a				      =       False
							a				      =       False
							a				      =       False
							a				      =       False
							a				      =       False




							def 		lowerCamelCase_				(							self:				int     ):
														lowerCamelCase__					:	Dict         =       TFDPRModelTester(self     )
														lowerCamelCase__					:	List[Any]         =       ConfigTester(self			,							config_class=SCREAMING_SNAKE_CASE__			,							hidden_size=37     )




							def 		lowerCamelCase_				(							self:				Dict     ):
														self.config_tester.run_common_tests()




							def 		lowerCamelCase_				(							self:				Any     ):
														lowerCamelCase__					:	List[Any]         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_context_encoder(*SCREAMING_SNAKE_CASE__     )




							def 		lowerCamelCase_				(							self:				Tuple     ):
														lowerCamelCase__					:	Dict         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_question_encoder(*SCREAMING_SNAKE_CASE__     )




							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														lowerCamelCase__					:	Any         =       self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_dpr_reader(*SCREAMING_SNAKE_CASE__     )




							@slow
							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):






														for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	Tuple         =       TFDPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE__     )
																					self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

														for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	str         =       TFDPRContextEncoder.from_pretrained(SCREAMING_SNAKE_CASE__     )
																					self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

														for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	Optional[int]         =       TFDPRQuestionEncoder.from_pretrained(SCREAMING_SNAKE_CASE__     )
																					self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )

														for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__					:	Union[str, Any]         =       TFDPRReader.from_pretrained(SCREAMING_SNAKE_CASE__     )
																					self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )



@require_tf
class _lowercase	(				unittest.TestCase		):




							@slow
							def 		lowerCamelCase_				(							self:				str     ):
														lowerCamelCase__					:	Optional[int]         =       TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""     )

														lowerCamelCase__					:	Tuple         =       tf.constant(
														    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]     )  # [CLS] hello, is my dog cute? [SEP]
														lowerCamelCase__					:	Optional[Any]         =       model(SCREAMING_SNAKE_CASE__     )[0]  # embedding shape = (1, 768)
														# compare the actual values for a slice.
														lowerCamelCase__					:	Optional[Any]         =       tf.constant(
														    [
														        [
														            0.03_236_253,
														            0.12_753_335,
														            0.16_818_509,
														            0.00_279_786,
														            0.3_896_933,
														            0.24_264_945,
														            0.2_178_971,
														            -0.02_335_227,
														            -0.08_481_959,
														            -0.14_324_117,
														        ]
														    ]     )
														self.assertTrue(numpy.allclose(output[:, :10].numpy()			,							expected_slice.numpy()			,							atol=1e-4     )     )








"""simple docstring"""
# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser



def      __lowerCamelCase   (							)			->      Any:
      __SCREAMING_SNAKE_CASE   :Tuple						=  ArgumentParser('''Accelerate CLI tool''' ,   usage='''accelerate <command> [<args>]''' ,   allow_abbrev=a_							)
      __SCREAMING_SNAKE_CASE   :str						=  parser.add_subparsers(help='''accelerate command helpers'''							)

      # Register commands
      get_config_parser(subparsers=a_							)
      env_command_parser(subparsers=a_							)
      launch_command_parser(subparsers=a_							)
      tpu_command_parser(subparsers=a_							)
      test_command_parser(subparsers=a_							)

      # Let's go
      __SCREAMING_SNAKE_CASE   :int						=  parser.parse_args()

      if not hasattr(a_ ,   '''func'''							):
            parser.print_help()
            exit(1							)

      # Run
      args.func(a_							)


if __name__ == "__main__":
  main()