


'''simple docstring'''







# Copyright 2023 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 torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce






def       lowercase					(_A       ):







       """simple docstring"""







       return (torch.arange(state.num_processes       ) + 1.0 + (state.num_processes * state.process_index)).to(state.device       )






def       lowercase					(_A       ):







       """simple docstring"""







       _lowerCAmelCase     :				Dict       =							create_tensor(_A       )
       _lowerCAmelCase     :				str       =							gather(_A       )
       assert gathered_tensor.tolist() == list(range(1 ,    state.num_processes**2 + 1       )       )






def       lowercase					(_A       ):







       """simple docstring"""







       _lowerCAmelCase     :				Union[str, Any]       =							[state.process_index]
       _lowerCAmelCase     :				Dict       =							gather_object(_A       )
       assert len(_A       ) == state.num_processes, f'{gathered_obj}, {len(_A       )} != {state.num_processes}'
       assert gathered_obj == list(range(state.num_processes       )       ), f'{gathered_obj} != {list(range(state.num_processes       )       )}'






def       lowercase					(_A       ):







       """simple docstring"""







       _lowerCAmelCase     :				List[str]       =							create_tensor(_A       )
       _lowerCAmelCase     :				Dict       =							broadcast(_A       )
       assert broadcasted_tensor.shape == torch.Size([state.num_processes]       )
       assert broadcasted_tensor.tolist() == list(range(1 ,    state.num_processes + 1       )       )






def       lowercase					(_A       ):







       """simple docstring"""







       if state.is_main_process:
              _lowerCAmelCase     :				Dict       =							torch.arange(state.num_processes + 1       ).to(state.device       )
       else:
              _lowerCAmelCase     :				List[str]       =							torch.arange(state.num_processes       ).to(state.device       )
       _lowerCAmelCase     :				Optional[int]       =							pad_across_processes(_A       )
       assert padded_tensor.shape == torch.Size([state.num_processes + 1]       )
       if not state.is_main_process:
              assert padded_tensor.tolist() == list(range(0 ,    state.num_processes       )       ) + [0]






def       lowercase					(_A       ):







       """simple docstring"""







       if state.num_processes != 2:
              return
       _lowerCAmelCase     :				Any       =							create_tensor(_A       )
       _lowerCAmelCase     :				List[Any]       =							reduce(_A ,    'sum'       )
       _lowerCAmelCase     :				Tuple       =							torch.tensor([4.0, 6]       ).to(state.device       )
       assert torch.allclose(_A ,    _A       ), f'{reduced_tensor} != {truth_tensor}'






def       lowercase					(_A       ):







       """simple docstring"""







       if state.num_processes != 2:
              return
       _lowerCAmelCase     :				str       =							create_tensor(_A       )
       _lowerCAmelCase     :				List[Any]       =							reduce(_A ,    'mean'       )
       _lowerCAmelCase     :				Tuple       =							torch.tensor([2.0, 3]       ).to(state.device       )
       assert torch.allclose(_A ,    _A       ), f'{reduced_tensor} != {truth_tensor}'






def       lowercase					(_A       ):







       """simple docstring"""







       main()






def       lowercase					():







       """simple docstring"""







       _lowerCAmelCase     :				List[str]       =							PartialState()
       state.print(f'State: {state}'       )
       state.print('testing gather'       )
       test_gather(_A       )
       state.print('testing gather_object'       )
       test_gather_object(_A       )
       state.print('testing broadcast'       )
       test_broadcast(_A       )
       state.print('testing pad_across_processes'       )
       test_pad_across_processes(_A       )
       state.print('testing reduce_sum'       )
       test_reduce_sum(_A       )
       state.print('testing reduce_mean'       )
       test_reduce_mean(_A       )


if __name__ == "__main__":
       main()





'''simple docstring'''







import argparse

from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta
from transformers.utils import logging


logging.set_verbosity_info()






def       lowercase					(_A ,    _A ,    _A       ):







       """simple docstring"""







       _lowerCAmelCase     :				Optional[Any]       =							TaConfig.from_json_file(_A       )
       print(f'Building PyTorch model from configuration: {config}'       )
       _lowerCAmelCase     :				Union[str, Any]       =							TaForConditionalGeneration(_A       )

       # Load weights from tf checkpoint
       load_tf_weights_in_ta(_A ,    _A ,    _A       )

       # Save pytorch-model
       print(f'Save PyTorch model to {pytorch_dump_path}'       )
       model.save_pretrained(_A       )


if __name__ == "__main__":
       lowerCAmelCase     :  str											=    argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
       )
       parser.add_argument(
           """--config_file""",
           default=None,
           type=str,
           required=True,
           help=(
               """The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture."""
           ),
       )
       parser.add_argument(
           """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
       )
       lowerCAmelCase     :  int											=    parser.parse_args()
       convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)

