



'''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    lowerCamelCase__ (		_A       ):
  return (torch.arange(state.num_processes       ) + 1.0 + (state.num_processes * state.process_index)).to(state.device       )
def    lowerCamelCase__ (		_A       ):
  a		:      int       =  create_tensor(_A       )
  a		:      Dict       =  gather(_A       )
  assert gathered_tensor.tolist() == list(range(1				,    state.num_processes**2 + 1       )       )
def    lowerCamelCase__ (		_A       ):
  a		:      List[str]       =  [state.process_index]
  a		:      Union[str, Any]       =  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    lowerCamelCase__ (		_A       ):
  a		:      Any       =  create_tensor(_A       )
  a		:      str       =  broadcast(_A       )
  assert broadcasted_tensor.shape == torch.Size([state.num_processes]       )
  assert broadcasted_tensor.tolist() == list(range(1				,    state.num_processes + 1       )       )
def    lowerCamelCase__ (		_A       ):
  # 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		:      str       =  torch.arange(state.num_processes + 1       ).to(state.device       )
  else:
    a		:      str       =  torch.arange(state.num_processes       ).to(state.device       )
  a		:      List[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    lowerCamelCase__ (		_A       ):
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  a		:      Dict       =  create_tensor(_A       )
  a		:      Any       =  reduce(_A				,    'sum'       )
  a		:      Optional[Any]       =  torch.tensor([4.0, 6]       ).to(state.device       )
  assert torch.allclose(_A				,    _A       ), f"""{reduced_tensor} != {truth_tensor}"""
def    lowerCamelCase__ (		_A       ):
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  a		:      int       =  create_tensor(_A       )
  a		:      List[str]       =  reduce(_A				,    'mean'       )
  a		:      Union[str, Any]       =  torch.tensor([2.0, 3]       ).to(state.device       )
  assert torch.allclose(_A				,    _A       ), f"""{reduced_tensor} != {truth_tensor}"""
def    lowerCamelCase__ (		_A       ):
  # For xla_spawn (TPUs)
  main()
def    lowerCamelCase__ (		):
  a		:      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 numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
def    lowerCamelCase__ (		_A       ):
  return (data["data"], data["target"])
def    lowerCamelCase__ (		_A				,    _A				,    _A       ):
  a		:      Any       =  XGBRegressor(verbosity=0				,    random_state=42       )
  xgb.fit(_A				,    _A       )
  # Predict target for test data
  a		:      Any       =  xgb.predict(_A       )
  a		:      Any       =  predictions.reshape(len(_A       )				,    1       )
  return predictions
def    lowerCamelCase__ (		):
  a		:      Optional[int]       =  fetch_california_housing()
  a     ,			a		:      Optional[Any]       =  data_handling(_A       )
  a     ,			a     ,			a     ,			a		:      Dict       =  train_test_split(
      _A				,    _A				,    test_size=0.25				,    random_state=1       )
  a		:      List[str]       =  xgboost(_A				,    _A				,    _A       )
  # Error printing
  print(f"""Mean Absolute Error : {mean_absolute_error(_A				,    _A       )}"""       )
  print(f"""Mean Square Error  : {mean_squared_error(_A				,    _A       )}"""       )


if __name__ == "__main__":
     import doctest

     doctest.testmod(verbose=True)
     main()