
# 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 pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


_snake_case      = '''Create a default config file for Accelerate with only a few flags set.'''

def  __lowerCamelCase					( _lowercase="no"						,     _lowercase = default_json_config_file						,     _lowercase = False			)       ->      List[str]:
     UpperCamelCase						    =					Path(_lowercase			)
     path.parent.mkdir(parents=_lowercase						,     exist_ok=_lowercase			)
     if path.exists():
          print(
              F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'			)
          return False
     UpperCamelCase						    =					mixed_precision.lower()
     if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
          raise ValueError(
              F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'			)
     UpperCamelCase						    =					{
         'compute_environment': 'LOCAL_MACHINE',
         'mixed_precision': mixed_precision,
     }
     if torch.cuda.is_available():
          UpperCamelCase						    =					torch.cuda.device_count()
          UpperCamelCase						    =					num_gpus
          UpperCamelCase						    =					False
          if num_gpus > 1:
               UpperCamelCase						    =					'MULTI_GPU'
          else:
               UpperCamelCase						    =					'NO'
     elif is_xpu_available() and use_xpu:
          UpperCamelCase						    =					torch.xpu.device_count()
          UpperCamelCase						    =					num_xpus
          UpperCamelCase						    =					False
          if num_xpus > 1:
               UpperCamelCase						    =					'MULTI_XPU'
          else:
               UpperCamelCase						    =					'NO'
     elif is_npu_available():
          UpperCamelCase						    =					torch.npu.device_count()
          UpperCamelCase						    =					num_npus
          UpperCamelCase						    =					False
          if num_npus > 1:
               UpperCamelCase						    =					'MULTI_NPU'
          else:
               UpperCamelCase						    =					'NO'
     else:
          UpperCamelCase						    =					0
          UpperCamelCase						    =					True
          UpperCamelCase						    =					1
          UpperCamelCase						    =					'NO'
     UpperCamelCase						    =					ClusterConfig(**_lowercase			)
     config.to_json_file(_lowercase			)
     return path

def  __lowerCamelCase					( _lowercase						,     _lowercase			)       ->      Any:
     UpperCamelCase						    =					parser.add_parser('default'						,     parents=_lowercase						,     help=_lowercase						,     formatter_class=_lowercase			)
     parser.add_argument(
         '--config_file'						,     default=_lowercase						,     help=(
             'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
             'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
             'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
             'with \'huggingface\'.'
         )						,     dest='save_location'						,     )

     parser.add_argument(
         '--mixed_precision'						,     choices=['no', 'fp16', 'bf16']						,     type=_lowercase						,     help='Whether or not to use mixed precision training. '
         'Choose between FP16 and BF16 (bfloat16) training. '
         'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.'						,     default='no'						,     )
     parser.set_defaults(func=_lowercase			)
     return parser







def  __lowerCamelCase					( _lowercase			)       ->      Tuple:
     UpperCamelCase						    =					write_basic_config(args.mixed_precision						,     args.save_location			)
     if config_file:
          print(F'accelerate configuration saved at {config_file}'			)




import argparse
import shutil
from pathlib import Path

from tqdm import tqdm

from transformers import AutoTokenizer

def  __lowerCamelCase					( _lowercase						,     _lowercase						,     _lowercase						,     _lowercase=1024			)       ->      Union[str, Any]:
     UpperCamelCase    ,		UpperCamelCase						    =					[], []

     UpperCamelCase						    =					list(zip(_lowercase						,     _lowercase			)			)
     UpperCamelCase    ,		UpperCamelCase						    =					sorted_examples[0]

     def is_too_big(_lowercase			):
          return tok(_lowercase						,     return_tensors='pt'			).input_ids.shape[1] > max_tokens

     for src, tgt in tqdm(sorted_examples[1:]			):
          UpperCamelCase						    =					new_src + ' ' + src
          UpperCamelCase						    =					new_tgt + ' ' + tgt
          if is_too_big(_lowercase			) or is_too_big(_lowercase			):  # cant fit, finalize example
               finished_src.append(_lowercase			)
               finished_tgt.append(_lowercase			)
               UpperCamelCase    ,		UpperCamelCase						    =					src, tgt
          else:  # can fit, keep adding
               UpperCamelCase    ,		UpperCamelCase						    =					cand_src, cand_tgt

    # cleanup
     if new_src:
          assert new_tgt
          finished_src.append(_lowercase			)
          finished_tgt.append(_lowercase			)
     return finished_src, finished_tgt

def  __lowerCamelCase					( _lowercase						,     _lowercase						,     _lowercase						,     _lowercase			)       ->      Union[str, Any]:
     UpperCamelCase						    =					Path(_lowercase			)
     save_path.mkdir(exist_ok=_lowercase			)
     for split in ["train"]:
          UpperCamelCase    ,		UpperCamelCase						    =					data_dir / F'{split}.source', data_dir / F'{split}.target'
          UpperCamelCase						    =					[x.rstrip() for x in Path(_lowercase			).open().readlines()]
          UpperCamelCase						    =					[x.rstrip() for x in Path(_lowercase			).open().readlines()]
          UpperCamelCase    ,		UpperCamelCase						    =					pack_examples(_lowercase						,     _lowercase						,     _lowercase						,     _lowercase			)
          print(F'packed {split} split from {len(_lowercase			)} examples -> {len(_lowercase			)}.'			)
          Path(save_path / F'{split}.source'			).open('w'			).write('\n'.join(_lowercase			)			)
          Path(save_path / F'{split}.target'			).open('w'			).write('\n'.join(_lowercase			)			)
     for split in ["val", "test"]:
          UpperCamelCase    ,		UpperCamelCase						    =					data_dir / F'{split}.source', data_dir / F'{split}.target'
          shutil.copyfile(_lowercase						,     save_path / F'{split}.source'			)
          shutil.copyfile(_lowercase						,     save_path / F'{split}.target'			)

def  __lowerCamelCase					( )       ->      Union[str, Any]:
     UpperCamelCase						    =					argparse.ArgumentParser()
     parser.add_argument('--tok_name'						,     type=_lowercase						,     help='like facebook/bart-large-cnn,t5-base, etc.'			)
     parser.add_argument('--max_seq_len'						,     type=_lowercase						,     default=128			)
     parser.add_argument('--data_dir'						,     type=_lowercase			)
     parser.add_argument('--save_path'						,     type=_lowercase			)
     UpperCamelCase						    =					parser.parse_args()
     UpperCamelCase						    =					AutoTokenizer.from_pretrained(args.tok_name			)
     return pack_data_dir(_lowercase						,     Path(args.data_dir			)						,     args.max_seq_len						,     args.save_path			)


if __name__ == "__main__":
 packer_cli()


