"""simple docstring"""






from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


__lowerCAmelCase					: Tuple        =logging.get_logger(__name__)

__lowerCAmelCase					: Tuple        ={
    """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json""",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}





class 				_A		( lowerCAmelCase			):
      snake_case__    :     int													=			'blenderbot-small'
      snake_case__    :     Optional[Any]													=			['past_key_values']
      snake_case__    :     Optional[Any]													=			{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

      def __init__(		self	, __lowerCAmelCase=5_0265	, __lowerCAmelCase=512	, __lowerCAmelCase=8	, __lowerCAmelCase=2048	, __lowerCAmelCase=16	, __lowerCAmelCase=8	, __lowerCAmelCase=2048	, __lowerCAmelCase=16	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase="gelu"	, __lowerCAmelCase=512	, __lowerCAmelCase=0.1	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0	, __lowerCAmelCase=0.0_2	, __lowerCAmelCase=1	, __lowerCAmelCase=False	, __lowerCAmelCase=0	, __lowerCAmelCase=1	, __lowerCAmelCase=2	, __lowerCAmelCase=2	, **__lowerCAmelCase	, ):







            """simple docstring"""



            lowercase														=			vocab_size
            lowercase														=			max_position_embeddings
            lowercase														=			d_model
            lowercase														=			encoder_ffn_dim
            lowercase														=			encoder_layers
            lowercase														=			encoder_attention_heads
            lowercase														=			decoder_ffn_dim
            lowercase														=			decoder_layers
            lowercase														=			decoder_attention_heads
            lowercase														=			dropout
            lowercase														=			attention_dropout
            lowercase														=			activation_dropout
            lowercase														=			activation_function
            lowercase														=			init_std
            lowercase														=			encoder_layerdrop
            lowercase														=			decoder_layerdrop
            lowercase														=			use_cache
            lowercase														=			encoder_layers
            lowercase														=			scale_embedding  # scale factor will be sqrt(d_model) if True

            super().__init__(
                pad_token_id=__lowerCAmelCase	, bos_token_id=__lowerCAmelCase	, eos_token_id=__lowerCAmelCase	, is_encoder_decoder=__lowerCAmelCase	, decoder_start_token_id=__lowerCAmelCase	, forced_eos_token_id=__lowerCAmelCase	, **__lowerCAmelCase	, )






class 				_A		( lowerCAmelCase			):

      @property
      def        A__ (		self		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                      ]		)

                  if self.use_past:
                        lowercase														=			{0: """batch"""}
                        lowercase														=			{0: """batch""", 1: """past_decoder_sequence + sequence"""}
                  else:
                        lowercase														=			{0: """batch""", 1: """decoder_sequence"""}
                        lowercase														=			{0: """batch""", 1: """decoder_sequence"""}

                  if self.use_past:
                        self.fill_with_past_key_values_(__lowerCAmelCase	, direction="""inputs"""		)
            elif self.task == "causal-lm":
                  # TODO: figure this case out.
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                      ]		)
                  if self.use_past:
                        lowercase							,							lowercase														=			self.num_layers
                        for i in range(__lowerCAmelCase		):
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
            else:
                  lowercase														=			OrderedDict(
                      [
                          ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                          ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}),
                          ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}),
                      ]		)

            return common_inputs

      @property
      def        A__ (		self		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			super().outputs
            else:
                  lowercase														=			super(__lowerCAmelCase	, self		).outputs
                  if self.use_past:
                        lowercase							,							lowercase														=			self.num_layers
                        for i in range(__lowerCAmelCase		):
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
                              lowercase														=			{0: """batch""", 2: """past_sequence + sequence"""}
            return common_outputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)

            # Generate decoder inputs
            lowercase														=			seq_length if not self.use_past else 1
            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)
            lowercase														=			{f'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
            lowercase														=			dict(**__lowerCAmelCase	, **__lowerCAmelCase		)

            if self.use_past:
                  if not is_torch_available():
                        raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""		)
                  else:
                        import torch
                  lowercase							,							lowercase														=			common_inputs["""input_ids"""].shape
                  lowercase														=			common_inputs["""decoder_input_ids"""].shape[1]
                  lowercase							,							lowercase														=			self.num_attention_heads
                  lowercase														=			(
                      batch,
                      num_encoder_attention_heads,
                      encoder_seq_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )
                  lowercase														=			decoder_seq_length + 3
                  lowercase														=			(
                      batch,
                      num_decoder_attention_heads,
                      decoder_past_length,
                      self._config.hidden_size // num_decoder_attention_heads,
                  )

                  lowercase														=			torch.cat(
                      [common_inputs["""decoder_attention_mask"""], torch.ones(__lowerCAmelCase	, __lowerCAmelCase		)]	, dim=1		)

                  lowercase														=			[]
                  # If the number of encoder and decoder layers are present in the model configuration, both are considered
                  lowercase							,							lowercase														=			self.num_layers
                  lowercase														=			min(__lowerCAmelCase	, __lowerCAmelCase		)
                  lowercase														=			max(__lowerCAmelCase	, __lowerCAmelCase		) - min_num_layers
                  lowercase														=			"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

                  for _ in range(__lowerCAmelCase		):
                        common_inputs["past_key_values"].append(
                            (
                                torch.zeros(__lowerCAmelCase		),
                                torch.zeros(__lowerCAmelCase		),
                                torch.zeros(__lowerCAmelCase		),
                                torch.zeros(__lowerCAmelCase		),
                            )		)
                  # TODO: test this.
                  lowercase														=			encoder_shape if remaining_side_name == """encoder""" else decoder_shape
                  for _ in range(__lowerCAmelCase	, __lowerCAmelCase		):
                        common_inputs["past_key_values"].append((torch.zeros(__lowerCAmelCase		), torch.zeros(__lowerCAmelCase		))		)
            return common_inputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)

            if self.use_past:
                  if not is_torch_available():
                        raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""		)
                  else:
                        import torch
                  lowercase							,							lowercase														=			common_inputs["""input_ids"""].shape
                  # Not using the same length for past_key_values
                  lowercase														=			seqlen + 2
                  lowercase							,							lowercase														=			self.num_layers
                  lowercase							,							lowercase														=			self.num_attention_heads
                  lowercase														=			(
                      batch,
                      num_encoder_attention_heads,
                      past_key_values_length,
                      self._config.hidden_size // num_encoder_attention_heads,
                  )

                  lowercase														=			common_inputs["""attention_mask"""].dtype
                  lowercase														=			torch.cat(
                      [common_inputs["""attention_mask"""], torch.ones(__lowerCAmelCase	, __lowerCAmelCase	, dtype=__lowerCAmelCase		)]	, dim=1		)
                  lowercase														=			[
                      (torch.zeros(__lowerCAmelCase		), torch.zeros(__lowerCAmelCase		)) for _ in range(__lowerCAmelCase		)
                  ]
            return common_inputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            lowercase														=			compute_effective_axis_dimension(
                __lowerCAmelCase	, fixed_dimension=OnnxConfig.default_fixed_batch	, num_token_to_add=0		)

            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            lowercase														=			tokenizer.num_special_tokens_to_add(__lowerCAmelCase		)
            lowercase														=			compute_effective_axis_dimension(
                __lowerCAmelCase	, fixed_dimension=OnnxConfig.default_fixed_sequence	, num_token_to_add=__lowerCAmelCase		)

            # Generate dummy inputs according to compute batch and sequence
            lowercase														=			[""" """.join([tokenizer.unk_token]		) * seq_length] * batch_size
            lowercase														=			dict(tokenizer(__lowerCAmelCase	, return_tensors=__lowerCAmelCase		)		)
            return common_inputs

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = -1	, __lowerCAmelCase = -1	, __lowerCAmelCase = False	, __lowerCAmelCase = None	, ):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)

            elif self.task == "causal-lm":
                  lowercase														=			self._generate_dummy_inputs_for_causal_lm(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)
            else:
                  lowercase														=			self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                      __lowerCAmelCase	, batch_size=__lowerCAmelCase	, seq_length=__lowerCAmelCase	, is_pair=__lowerCAmelCase	, framework=__lowerCAmelCase		)

            return common_inputs







      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            if self.task in ["default", "seq2seq-lm"]:
                  lowercase														=			super()._flatten_past_key_values_(__lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)
            else:
                  lowercase														=			super(__lowerCAmelCase	, self		)._flatten_past_key_values_(
                      __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)







"""simple docstring"""






import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy


def     UpperCAmelCase__ (       lowerCAmelCase__  :int      ,	lowerCAmelCase__  :Tuple	)				->					Union[str, Any]:
      '''simple docstring'''


      assert isinstance(lowerCAmelCase__      ,	lowerCAmelCase__	)
      assert dataset.num_rows == 4
      assert dataset.num_columns == 3
      assert dataset.column_names == ["col_1", "col_2", "col_3"]
      for feature, expected_dtype in expected_features.items():
            assert dataset.features[feature].dtype == expected_dtype


@require_sqlalchemy
@pytest.mark.parametrize("""keep_in_memory"""      ,	[False, True]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :str      ,	lowerCAmelCase__  :List[str]	)				->					int:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            lowercase														=			SqlDatasetReader(
                """dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__      ,	keep_in_memory=lowerCAmelCase__	).read()
      _check_sql_dataset(lowerCAmelCase__      ,	lowerCAmelCase__	)


@require_sqlalchemy
@pytest.mark.parametrize(
    """features"""      ,	[
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]      ,	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :int      ,	lowerCAmelCase__  :Union[str, Any]      ,	lowerCAmelCase__  :List[Any]      ,	lowerCAmelCase__  :Union[str, Any]	)				->					Optional[int]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
      lowercase														=			features.copy() if features else default_expected_features
      lowercase														=			(
          Features({feature: Value(lowerCAmelCase__	) for feature, dtype in features.items()}	) if features is not None else None
      )
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	features=lowerCAmelCase__      ,	cache_dir=lowerCAmelCase__	).read()
      _check_sql_dataset(lowerCAmelCase__      ,	lowerCAmelCase__	)


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Optional[int]:
      '''simple docstring'''


      with contextlib.closing(sqlitea.connect(lowerCAmelCase__	)	) as con:
            lowercase														=			con.cursor()
            cur.execute("""SELECT * FROM dataset"""	)
            for row in cur:
                  yield row


@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :str      ,	lowerCAmelCase__  :int	)				->					List[Any]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=1	).write()

      lowercase														=			iter_sql_file(lowerCAmelCase__	)
      lowercase														=			iter_sql_file(lowerCAmelCase__	)

      for rowa, rowa in zip(lowerCAmelCase__      ,	lowerCAmelCase__	):
            assert rowa == rowa


@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :str      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :Optional[int]	)				->					Optional[int]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=2	).write()

      lowercase														=			iter_sql_file(lowerCAmelCase__	)
      lowercase														=			iter_sql_file(lowerCAmelCase__	)

      for rowa, rowa in zip(lowerCAmelCase__      ,	lowerCAmelCase__	):
            assert rowa == rowa






@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Tuple	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      with pytest.raises(lowerCAmelCase__	):
            SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=0	).write()



