






"""simple docstring"""




import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Value
from datasets.packaged_modules.json.json import Json






@pytest.fixture
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Tuple    ):
  A__											=							tmp_path / """file.jsonl"""
  A__											=							textwrap.dedent(
      """\
        {\"col_1\": -1}
        {\"col_1\": 1, \"col_2\": 2}
        {\"col_1\": 10, \"col_2\": 20}
        """    )
  with open(UpperCamelCase		,				"""w"""    ) as f:
    f.write(UpperCamelCase    )
  return str(UpperCamelCase    )






@pytest.fixture
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Tuple    ):
  A__											=							tmp_path / """file_utf16_encoded.jsonl"""
  A__											=							textwrap.dedent(
      """\
        {\"col_1\": -1}
        {\"col_1\": 1, \"col_2\": 2}
        {\"col_1\": 10, \"col_2\": 20}
        """    )
  with open(UpperCamelCase		,				"""w"""		,				encoding="""utf-16"""    ) as f:
    f.write(UpperCamelCase    )
  return str(UpperCamelCase    )






@pytest.fixture
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Union[str, Any]    ):
  A__											=							tmp_path / """file_with_list_of_dicts.json"""
  A__											=							textwrap.dedent(
      """\
        [
            {\"col_1\": -1},
            {\"col_1\": 1, \"col_2\": 2},
            {\"col_1\": 10, \"col_2\": 20}
        ]
        """    )
  with open(UpperCamelCase		,				"""w"""    ) as f:
    f.write(UpperCamelCase    )
  return str(UpperCamelCase    )






@pytest.fixture
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Union[str, Any]    ):
  A__											=							tmp_path / """file_with_list_of_dicts_field.json"""
  A__											=							textwrap.dedent(
      """\
        {
            \"field1\": 1,
            \"field2\": \"aabb\",
            \"field3\": [
                {\"col_1\": -1},
                {\"col_1\": 1, \"col_2\": 2},
                {\"col_1\": 10, \"col_2\": 20}
            ]
        }
        """    )
  with open(UpperCamelCase		,				"""w"""    ) as f:
    f.write(UpperCamelCase    )
  return str(UpperCamelCase    )






@pytest.mark.parametrize(
    """file_fixture, config_kwargs"""		,				[
        ("""jsonl_file""", {}),
        ("""jsonl_file_utf16_encoded""", {"""encoding""": """utf-16"""}),
        ("""json_file_with_list_of_dicts""", {}),
        ("""json_file_with_list_of_dicts_field""", {"""field""": """field3"""}),
    ]		,				)
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	int		,				UpperCamelCase     :	Tuple		,				UpperCamelCase     :	Union[str, Any]    ):
  A__											=							Json(**UpperCamelCase    )
  A__											=							json._generate_tables([[request.getfixturevalue(UpperCamelCase    )]]    )
  A__											=							pa.concat_tables([table for _, table in generator]    )
  assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20]}






@pytest.mark.parametrize(
    """file_fixture, config_kwargs"""		,				[
        (
            """jsonl_file""",
            {"""features""": Features({"""col_1""": Value("""int64"""    ), """col_2""": Value("""int64"""    ), """missing_col""": Value("""string"""    )}    )},
        ),
        (
            """json_file_with_list_of_dicts""",
            {"""features""": Features({"""col_1""": Value("""int64"""    ), """col_2""": Value("""int64"""    ), """missing_col""": Value("""string"""    )}    )},
        ),
        (
            """json_file_with_list_of_dicts_field""",
            {
                """field""": """field3""",
                """features""": Features(
                    {"""col_1""": Value("""int64"""    ), """col_2""": Value("""int64"""    ), """missing_col""": Value("""string"""    )}    ),
            },
        ),
    ]		,				)
def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Dict		,				UpperCamelCase     :	List[str]		,				UpperCamelCase     :	Tuple    ):
  A__											=							Json(**UpperCamelCase    )
  A__											=							json._generate_tables([[request.getfixturevalue(UpperCamelCase    )]]    )
  A__											=							pa.concat_tables([table for _, table in generator]    )
  assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20], "missing_col": [None, None, None]}












"""simple docstring"""




import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	List[str]		,				UpperCamelCase     :	Tuple		,				UpperCamelCase     :	Optional[int]		,				UpperCamelCase     :	Union[str, Any]		,				UpperCamelCase     :	Optional[int]    ):
  # Initialise PyTorch model
  A__											=							BertConfig.from_pretrained(
      """bert-large-cased"""		,				vocab_size=UpperCamelCase		,				max_position_embeddings=512		,				is_decoder=UpperCamelCase		,				add_cross_attention=UpperCamelCase		,				)
  A__											=							bert_config.to_dict()
  del bert_config_dict["type_vocab_size"]
  A__											=							BertGenerationConfig(**UpperCamelCase    )
  if is_encoder:
    A__											=							BertGenerationEncoder(UpperCamelCase    )
  else:
    A__											=							BertGenerationDecoder(UpperCamelCase    )
  print(F"""Building PyTorch model from configuration: {config}"""    )

  # Load weights from tf checkpoint
  load_tf_weights_in_bert_generation(
      UpperCamelCase		,				UpperCamelCase		,				model_class="""bert"""		,				is_encoder_named_decoder=UpperCamelCase		,				is_encoder=UpperCamelCase		,				)

  # Save pytorch-model
  print(F"""Save PyTorch model and config to {pytorch_dump_path}"""    )
  model.save_pretrained(UpperCamelCase    )


if __name__ == "__main__":
      lowerCamelCase__			=   argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          "--tf_hub_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
      )
      parser.add_argument(
          "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
      )
      parser.add_argument(
          "--is_encoder_named_decoder",
          action="store_true",
          help="If decoder has to be renamed to encoder in PyTorch model.",
      )
      parser.add_argument("--is_encoder", action="store_true", help="If model is an encoder.")
      parser.add_argument("--vocab_size", default=50_358, type=int, help="Vocab size of model")
      lowerCamelCase__			=   parser.parse_args()
      convert_tf_checkpoint_to_pytorch(
          args.tf_hub_path,
          args.pytorch_dump_path,
          args.is_encoder_named_decoder,
          args.vocab_size,
          is_encoder=args.is_encoder,
      )




