



# 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 	UpperCAmelCase__      (      UpperCAmelCase__ :str					):






	'''simple docstring'''

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


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[int]					):






	'''simple docstring'''

	a						=		create_tensor(UpperCAmelCase__					)
	a						=		gather(UpperCAmelCase__					)
	assert gathered_tensor.tolist() == list(range(1 ,					state.num_processes**2 + 1					)					)


def 	UpperCAmelCase__      (      UpperCAmelCase__ :List[str]					):






	'''simple docstring'''

	a						=		[state.process_index]
	a						=		gather_object(UpperCAmelCase__					)
	assert len(UpperCAmelCase__					) == state.num_processes, F"""{gathered_obj}, {len(UpperCAmelCase__					)} != {state.num_processes}"""
	assert gathered_obj == list(range(state.num_processes					)					), F"""{gathered_obj} != {list(range(state.num_processes					)					)}"""


def 	UpperCAmelCase__      (      UpperCAmelCase__ :str					):






	'''simple docstring'''

	a						=		create_tensor(UpperCAmelCase__					)
	a						=		broadcast(UpperCAmelCase__					)
	assert broadcasted_tensor.shape == torch.Size([state.num_processes]					)
	assert broadcasted_tensor.tolist() == list(range(1 ,					state.num_processes + 1					)					)


def 	UpperCAmelCase__      (      UpperCAmelCase__ :List[Any]					):






	'''simple docstring'''

	if state.is_main_process:
		a						=		torch.arange(state.num_processes + 1					).to(state.device					)
	else:
		a						=		torch.arange(state.num_processes					).to(state.device					)
	a						=		pad_across_processes(UpperCAmelCase__					)
	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 	UpperCAmelCase__      (      UpperCAmelCase__ :List[Any]					):






	'''simple docstring'''

	if state.num_processes != 2:
		return
	a						=		create_tensor(UpperCAmelCase__					)
	a						=		reduce(UpperCAmelCase__ ,					"sum"					)
	a						=		torch.tensor([4.0, 6]					).to(state.device					)
	assert torch.allclose(UpperCAmelCase__ ,					UpperCAmelCase__					), F"""{reduced_tensor} != {truth_tensor}"""


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[Any]					):






	'''simple docstring'''

	if state.num_processes != 2:
		return
	a						=		create_tensor(UpperCAmelCase__					)
	a						=		reduce(UpperCAmelCase__ ,					"mean"					)
	a						=		torch.tensor([2.0, 3]					).to(state.device					)
	assert torch.allclose(UpperCAmelCase__ ,					UpperCAmelCase__					), F"""{reduced_tensor} != {truth_tensor}"""


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Any					):






	'''simple docstring'''

	main()


def 	UpperCAmelCase__      (      ):






	'''simple docstring'''

	a						=		PartialState()
	state.print(F"""State: {state}"""					)
	state.print("testing gather"					)
	test_gather(UpperCAmelCase__					)
	state.print("testing gather_object"					)
	test_gather_object(UpperCAmelCase__					)
	state.print("testing broadcast"					)
	test_broadcast(UpperCAmelCase__					)
	state.print("testing pad_across_processes"					)
	test_pad_across_processes(UpperCAmelCase__					)
	state.print("testing reduce_sum"					)
	test_reduce_sum(UpperCAmelCase__					)
	state.print("testing reduce_mean"					)
	test_reduce_mean(UpperCAmelCase__					)


if __name__ == "__main__":
					main()






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch

     from transformers.generation import DisjunctiveConstraint



@require_torch
class 		_lowercase		(       unittest.TestCase	):

   def 		A							(      self			:       Union[str, Any]							)       ->				int:


    """simple docstring"""






    a						=		[[1, 2, 4], [1, 2, 3, 4]]
    a						=		DisjunctiveConstraint(__lowerCAmelCase							)
    self.assertTrue(isinstance(dc.token_ids							,   __lowerCAmelCase							)							)

    with self.assertRaises(__lowerCAmelCase							):
     DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]							)							)

    with self.assertRaises(__lowerCAmelCase							):
     DisjunctiveConstraint([torch.LongTensor([1, 2, 4]							), torch.LongTensor([1, 2, 3, 4, 5]							)]							)

   def 		A							(      self			:       Tuple							)       ->				Dict:


    """simple docstring"""






    a						=		[[1, 2], [1, 2, 3, 4]]

    with self.assertRaises(__lowerCAmelCase							):
     DisjunctiveConstraint(__lowerCAmelCase							)  # fails here

   def 		A							(      self			:       int							)       ->				Any:


    """simple docstring"""






    a						=		[[1, 2, 3], [1, 2, 4]]

    a						=		DisjunctiveConstraint(__lowerCAmelCase							)

    a		,  a		,  a						=		dc.update(1							)
    a						=		stepped is True and completed is False and reset is False
    self.assertTrue(__lowerCAmelCase							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.current_seq == [1]							)

    a		,  a		,  a						=		dc.update(2							)
    a						=		stepped is True and completed is False and reset is False
    self.assertTrue(__lowerCAmelCase							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.current_seq == [1, 2]							)

    a		,  a		,  a						=		dc.update(3							)
    a						=		stepped is True and completed is True and reset is False
    self.assertTrue(__lowerCAmelCase							)
    self.assertTrue(dc.completed							)  # Completed!
    self.assertTrue(dc.current_seq == [1, 2, 3]							)




   def 		A							(      self			:       List[Any]							)       ->				List[Any]:


    """simple docstring"""






    a						=		[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

    a						=		DisjunctiveConstraint(__lowerCAmelCase							)

    a		,  a		,  a						=		dc.update(1							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.current_seq == [1]							)

    a		,  a		,  a						=		dc.update(2							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.current_seq == [1, 2]							)

    a		,  a		,  a						=		dc.update(4							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.current_seq == [1, 2, 4]							)

    a		,  a		,  a						=		dc.update(5							)
    self.assertTrue(dc.completed							)  # Completed!
    self.assertTrue(dc.current_seq == [1, 2, 4, 5]							)

    dc.reset()

    a		,  a		,  a						=		dc.update(1							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.remaining() == 3							)
    self.assertTrue(dc.current_seq == [1]							)

    a		,  a		,  a						=		dc.update(2							)
    self.assertTrue(not dc.completed							)
    self.assertTrue(dc.remaining() == 2							)
    self.assertTrue(dc.current_seq == [1, 2]							)

    a		,  a		,  a						=		dc.update(5							)
    self.assertTrue(dc.completed							)  # Completed!
    self.assertTrue(dc.remaining() == 0							)
    self.assertTrue(dc.current_seq == [1, 2, 5]							)

