





# Copyright 2022 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.

import argparse
import os
import platform

import numpy as np
import psutil
import torch

from accelerate import __version__ as version
from accelerate.commands.config import default_config_file, load_config_from_file

from ..utils import is_npu_available, is_xpu_available

def   a_   (			lowerCAmelCase_				:      Union[str, Any]=None     ):
   if subparsers is not None:
      __lowerCAmelCase							     =     subparsers.add_parser('env'     )
   else:
      __lowerCAmelCase							     =     argparse.ArgumentParser('Accelerate env command'     )

   parser.add_argument(
       '--config_file',     default=lowerCAmelCase_,     help='The config file to use for the default values in the launching script.'     )

   if subparsers is not None:
      parser.set_defaults(func=lowerCAmelCase_     )
   return parser

def   a_   (			lowerCAmelCase_				:      Optional[Any]     ):
   __lowerCAmelCase							     =     torch.__version__
   __lowerCAmelCase							     =     torch.cuda.is_available()
   __lowerCAmelCase							     =     is_xpu_available()
   __lowerCAmelCase							     =     is_npu_available()

   __lowerCAmelCase							     =     'Not found'
   # Get the default from the config file.
   if args.config_file is not None or os.path.isfile(lowerCAmelCase_     ):
      __lowerCAmelCase							     =     load_config_from_file(args.config_file     ).to_dict()

   __lowerCAmelCase							     =     {
       '`Accelerate` version': version,
       'Platform': platform.platform(),
       'Python version': platform.python_version(),
       'Numpy version': np.__version__,
       'PyTorch version (GPU?)': F"""{pt_version} ({pt_cuda_available})""",
       'PyTorch XPU available': str(lowerCAmelCase_     ),
       'PyTorch NPU available': str(lowerCAmelCase_     ),
       'System RAM': F"""{psutil.virtual_memory().total / 1024 ** 3:.2f} GB""",
   }
   if pt_cuda_available:
      __lowerCAmelCase							     =     torch.cuda.get_device_name()

   print('\nCopy-and-paste the text below in your GitHub issue\n'     )
   print('\n'.join([F"""- {prop}: {val}""" for prop, val in info.items()]     )     )

   print('- `Accelerate` default config:' if args.config_file is None else '- `Accelerate` config passed:'     )
   __lowerCAmelCase							     =     (
       '\n'.join([F"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()]     )
       if isinstance(lowerCAmelCase_,     lowerCAmelCase_     )
       else F"""\t{accelerate_config}"""
   )
   print(lowerCAmelCase_     )

   __lowerCAmelCase							     =     accelerate_config

   return info

def   a_   (			):
   __lowerCAmelCase							     =     env_command_parser()
   __lowerCAmelCase							     =     parser.parse_args()
   env_command(lowerCAmelCase_     )
   return 0


if __name__ == "__main__":
     raise SystemExit(main())








import argparse

from transformers import (
    TapasConfig,
    TapasForMaskedLM,
    TapasForQuestionAnswering,
    TapasForSequenceClassification,
    TapasModel,
    TapasTokenizer,
    load_tf_weights_in_tapas,
)
from transformers.utils import logging


logging.set_verbosity_info()

def   a_   (			lowerCAmelCase_				:      str,     lowerCAmelCase_				:      Optional[int],     lowerCAmelCase_				:      Any,     lowerCAmelCase_				:      List[Any],     lowerCAmelCase_				:      Tuple     ):
   # Initialise PyTorch model.
   # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of
   # TapasConfig to False.

   # initialize configuration from json file
   __lowerCAmelCase							     =     TapasConfig.from_json_file(lowerCAmelCase_     )
   # set absolute/relative position embeddings parameter
   __lowerCAmelCase							     =     reset_position_index_per_cell

   # set remaining parameters of TapasConfig as well as the model based on the task
   if task == "SQA":
      __lowerCAmelCase							     =     TapasForQuestionAnswering(config=lowerCAmelCase_     )
   elif task == "WTQ":
      # run_task_main.py hparams
      __lowerCAmelCase							     =     4
      __lowerCAmelCase							     =     True
      # hparam_utils.py hparams
      __lowerCAmelCase							     =     0.66_4694
      __lowerCAmelCase							     =     0.20_7951
      __lowerCAmelCase							     =     0.12_1194
      __lowerCAmelCase							     =     True
      __lowerCAmelCase							     =     True
      __lowerCAmelCase							     =     False
      __lowerCAmelCase							     =     0.035_2513

      __lowerCAmelCase							     =     TapasForQuestionAnswering(config=lowerCAmelCase_     )
   elif task == "WIKISQL_SUPERVISED":
      # run_task_main.py hparams
      __lowerCAmelCase							     =     4
      __lowerCAmelCase							     =     False
      # hparam_utils.py hparams
      __lowerCAmelCase							     =     36.4519
      __lowerCAmelCase							     =     0.90_3421
      __lowerCAmelCase							     =     222.088
      __lowerCAmelCase							     =     True
      __lowerCAmelCase							     =     True
      __lowerCAmelCase							     =     True
      __lowerCAmelCase							     =     0.76_3141

      __lowerCAmelCase							     =     TapasForQuestionAnswering(config=lowerCAmelCase_     )
   elif task == "TABFACT":
      __lowerCAmelCase							     =     TapasForSequenceClassification(config=lowerCAmelCase_     )
   elif task == "MLM":
      __lowerCAmelCase							     =     TapasForMaskedLM(config=lowerCAmelCase_     )
   elif task == "INTERMEDIATE_PRETRAINING":
      __lowerCAmelCase							     =     TapasModel(config=lowerCAmelCase_     )
   else:
      raise ValueError(F"""Task {task} not supported."""     )

   print(F"""Building PyTorch model from configuration: {config}"""     )
   # Load weights from tf checkpoint
   load_tf_weights_in_tapas(lowerCAmelCase_,     lowerCAmelCase_,     lowerCAmelCase_     )

   # Save pytorch-model (weights and configuration)
   print(F"""Save PyTorch model to {pytorch_dump_path}"""     )
   model.save_pretrained(lowerCAmelCase_     )

   # Save tokenizer files
   print(F"""Save tokenizer files to {pytorch_dump_path}"""     )
   __lowerCAmelCase							     =     TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + 'vocab.txt',     model_max_length=512     )
   tokenizer.save_pretrained(lowerCAmelCase_     )

   print('Used relative position embeddings:',     model.config.reset_position_index_per_cell     )


if __name__ == "__main__":
     _snake_case  :   Union[str, Any]						   =  argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         '--task', default='SQA', type=str, help='Model task for which to convert a checkpoint. Defaults to SQA.'
     )
     parser.add_argument(
         '--reset_position_index_per_cell',
         default=False,
         action='store_true',
         help='Whether to use relative position embeddings or not. Defaults to True.',
     )
     parser.add_argument(
         '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
     )
     parser.add_argument(
         '--tapas_config_file',
         default=None,
         type=str,
         required=True,
         help=(
             'The config json file corresponding to the pre-trained TAPAS model. \n'
             'This specifies the model architecture.'
         ),
     )
     parser.add_argument(
         '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
     )
     _snake_case  :   Optional[Any]						   =  parser.parse_args()
     convert_tf_checkpoint_to_pytorch(
         args.task,
         args.reset_position_index_per_cell,
         args.tf_checkpoint_path,
         args.tapas_config_file,
         args.pytorch_dump_path,
     )

