





'''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 						UpperCamelCase    (							_lowerCamelCase : Tuple	):
    return (torch.arange(state.num_processes	) + 1.0 + (state.num_processes * state.process_index)).to(state.device	)





def 						UpperCamelCase    (							_lowerCamelCase : Union[str, Any]	):
    A__    =      create_tensor(_lowerCamelCase	)
    A__    =      gather(_lowerCamelCase	)
    assert gathered_tensor.tolist() == list(range(1						,	state.num_processes**2 + 1	)	)





def 						UpperCamelCase    (							_lowerCamelCase : Optional[Any]	):
    A__    =      [state.process_index]
    A__    =      gather_object(_lowerCamelCase	)
    assert len(_lowerCamelCase	) == state.num_processes, F"{gathered_obj}, {len(_lowerCamelCase	)} != {state.num_processes}"
    assert gathered_obj == list(range(state.num_processes	)	), F"{gathered_obj} != {list(range(state.num_processes	)	)}"





def 						UpperCamelCase    (							_lowerCamelCase : Dict	):
    A__    =      create_tensor(_lowerCamelCase	)
    A__    =      broadcast(_lowerCamelCase	)
    assert broadcasted_tensor.shape == torch.Size([state.num_processes]	)
    assert broadcasted_tensor.tolist() == list(range(1						,	state.num_processes + 1	)	)





def 						UpperCamelCase    (							_lowerCamelCase : Union[str, Any]	):
    # 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:
        A__    =      torch.arange(state.num_processes + 1	).to(state.device	)
    else:
        A__    =      torch.arange(state.num_processes	).to(state.device	)
    A__    =      pad_across_processes(_lowerCamelCase	)
    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    (							_lowerCamelCase : Optional[Any]	):
    # For now runs on only two processes
    if state.num_processes != 2:
        return
    A__    =      create_tensor(_lowerCamelCase	)
    A__    =      reduce(_lowerCamelCase						,	"sum"	)
    A__    =      torch.tensor([4.0, 6]	).to(state.device	)
    assert torch.allclose(_lowerCamelCase						,	_lowerCamelCase	), F"{reduced_tensor} != {truth_tensor}"





def 						UpperCamelCase    (							_lowerCamelCase : str	):
    # For now runs on only two processes
    if state.num_processes != 2:
        return
    A__    =      create_tensor(_lowerCamelCase	)
    A__    =      reduce(_lowerCamelCase						,	"mean"	)
    A__    =      torch.tensor([2.0, 3]	).to(state.device	)
    assert torch.allclose(_lowerCamelCase						,	_lowerCamelCase	), F"{reduced_tensor} != {truth_tensor}"





def 						UpperCamelCase    (							_lowerCamelCase : List[str]	):
    # For xla_spawn (TPUs)
    main()





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


if __name__ == "__main__":
  main()










'''simple docstring'''

import unittest

import numpy as np





def 						UpperCamelCase    (							_lowerCamelCase : np.ndarray						,	_lowerCamelCase : np.ndarray						,	_lowerCamelCase : np.ndarray						,	_lowerCamelCase : np.ndarray | None = None						,	):
    A__    =      np.shape(_lowerCamelCase	)
    A__    =      np.shape(_lowerCamelCase	)
    A__    =      np.shape(_lowerCamelCase	)

    if shape_a[0] != shape_b[0]:
        A__    =      (
            "Expected the same number of rows for A and B. "
            F"Instead found A of size {shape_a} and B of size {shape_b}"
        )
        raise ValueError(_lowerCamelCase	)

    if shape_b[1] != shape_c[1]:
        A__    =      (
            "Expected the same number of columns for B and C. "
            F"Instead found B of size {shape_b} and C of size {shape_c}"
        )
        raise ValueError(_lowerCamelCase	)

    A__    =      pseudo_inv
    if a_inv is None:
        try:
            A__    =      np.linalg.inv(_lowerCamelCase	)
        except np.linalg.LinAlgError:
            raise ValueError(
                "Input matrix A is not invertible. Cannot compute Schur complement."	)

    return mat_c - mat_b.T @ a_inv @ mat_b






class        UpperCAmelCase     (	unittest.TestCase    ):
 def 					UpperCAmelCase_							(    self				:Any							)->							None:
     A__    =      np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]							)
     A__    =      np.array([[0, 3], [3, 0], [2, 3]]							)
     A__    =      np.array([[2, 1], [6, 3]]							)

     A__    =      schur_complement(lowercase_						,	lowercase_						,	lowercase_							)

     A__    =      np.block([[a, b], [b.T, c]]							)

     A__    =      np.linalg.det(lowercase_							)
     A__    =      np.linalg.det(lowercase_							)
     A__    =      np.linalg.det(lowercase_							)

     self.assertAlmostEqual(lowercase_						,	det_a * det_s							)
 def 					UpperCAmelCase_							(    self				:Dict							)->							None:
     A__    =      np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]							)
     A__    =      np.array([[0, 3], [3, 0], [2, 3]]							)
     A__    =      np.array([[2, 1], [6, 3]]							)

     with self.assertRaises(lowercase_							):
         schur_complement(lowercase_						,	lowercase_						,	lowercase_							)



 def 					UpperCAmelCase_							(    self				:Union[str, Any]							)->							None:
     A__    =      np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]							)
     A__    =      np.array([[0, 3], [3, 0], [2, 3]]							)
     A__    =      np.array([[2, 1, 3], [6, 3, 5]]							)

     with self.assertRaises(lowercase_							):
         schur_complement(lowercase_						,	lowercase_						,	lowercase_							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  unittest.main()



