



"""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  __snake_case							(	__A      :						str     )     ->       int:






				'''simple docstring'''
				return (torch.arange(state.num_processes     ) + 1.0 + (state.num_processes * state.process_index)).to(state.device     )
def  __snake_case							(	__A      :						List[str]     )     ->       Dict:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							int							=     create_tensor(__A     )
				SCREAMING_SNAKE_CASE						:							Any							=     gather(__A     )
				assert gathered_tensor.tolist() == list(range(1							,		state.num_processes**2 + 1     )     )
def  __snake_case							(	__A      :						Any     )     ->       Union[str, Any]:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							Optional[Any]							=     [state.process_index]
				SCREAMING_SNAKE_CASE						:							int							=     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  __snake_case							(	__A      :						Dict     )     ->       Optional[Any]:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     create_tensor(__A     )
				SCREAMING_SNAKE_CASE						:							List[str]							=     broadcast(__A     )
				assert broadcasted_tensor.shape == torch.Size([state.num_processes]     )
				assert broadcasted_tensor.tolist() == list(range(1							,		state.num_processes + 1     )     )
def  __snake_case							(	__A      :						int     )     ->       List[str]:






				'''simple docstring'''
				# We need to pad the tensor with one more element if we are the main process
				# to ensure that we can pad
				if state.is_main_process:
								SCREAMING_SNAKE_CASE						:							List[Any]							=     torch.arange(state.num_processes + 1     ).to(state.device     )
				else:
								SCREAMING_SNAKE_CASE						:							List[Any]							=     torch.arange(state.num_processes     ).to(state.device     )
				SCREAMING_SNAKE_CASE						:							Optional[Any]							=     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  __snake_case							(	__A      :						Optional[int]     )     ->       Optional[int]:






				'''simple docstring'''
				# For now runs on only two processes
				if state.num_processes != 2:
								return
				SCREAMING_SNAKE_CASE						:							Optional[Any]							=     create_tensor(__A     )
				SCREAMING_SNAKE_CASE						:							List[str]							=     reduce(__A							,		'sum'     )
				SCREAMING_SNAKE_CASE						:							Dict							=     torch.tensor([4.0, 6]     ).to(state.device     )
				assert torch.allclose(__A							,		__A     ), F"""{reduced_tensor} != {truth_tensor}"""
def  __snake_case							(	__A      :						List[Any]     )     ->       Optional[int]:






				'''simple docstring'''
				# For now runs on only two processes
				if state.num_processes != 2:
								return
				SCREAMING_SNAKE_CASE						:							Optional[int]							=     create_tensor(__A     )
				SCREAMING_SNAKE_CASE						:							Optional[Any]							=     reduce(__A							,		'mean'     )
				SCREAMING_SNAKE_CASE						:							List[str]							=     torch.tensor([2.0, 3]     ).to(state.device     )
				assert torch.allclose(__A							,		__A     ), F"""{reduced_tensor} != {truth_tensor}"""
def  __snake_case							(	__A      :						Union[str, Any]     )     ->       Optional[Any]:






				'''simple docstring'''
				# For xla_spawn (TPUs)
				main()
def  __snake_case							(	)     ->       Optional[Any]:






				'''simple docstring'''
				SCREAMING_SNAKE_CASE						:							int							=     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"""

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


A_						:    Optional[Any]				=      {
    'configuration_squeezebert': [
        'SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'SqueezeBertConfig',
        'SqueezeBertOnnxConfig',
    ],
    'tokenization_squeezebert': ['SqueezeBertTokenizer'],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_						:    Tuple				=      ['SqueezeBertTokenizerFast']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_						:    int				=      [
							    'SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'SqueezeBertForMaskedLM',
							    'SqueezeBertForMultipleChoice',
							    'SqueezeBertForQuestionAnswering',
							    'SqueezeBertForSequenceClassification',
							    'SqueezeBertForTokenClassification',
							    'SqueezeBertModel',
							    'SqueezeBertModule',
							    'SqueezeBertPreTrainedModel',
							]


if TYPE_CHECKING:
							from .configuration_squeezebert import (
							    SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    SqueezeBertConfig,
							    SqueezeBertOnnxConfig,
							)
							from .tokenization_squeezebert import SqueezeBertTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_squeezebert import (
														    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    SqueezeBertForMaskedLM,
														    SqueezeBertForMultipleChoice,
														    SqueezeBertForQuestionAnswering,
														    SqueezeBertForSequenceClassification,
														    SqueezeBertForTokenClassification,
														    SqueezeBertModel,
														    SqueezeBertModule,
														    SqueezeBertPreTrainedModel,
														)

else:
							import sys

							A_						:    Optional[Any]				=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





