


# 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__:		Dict	)  ->		str:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		Union[str, Any]	)  ->		Any:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		Dict	)  ->		Tuple:


							'''simple docstring'''





							_UpperCAmelCase					=      [state.process_index]
							_UpperCAmelCase					=      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__:		Any	)  ->		int:


							'''simple docstring'''





							_UpperCAmelCase					=      create_tensor(a__	)
							_UpperCAmelCase					=      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__:		List[str]	)  ->		Dict:


							'''simple docstring'''





							if state.is_main_process:
														_UpperCAmelCase					=      torch.arange(state.num_processes + 1	).to(state.device	)
							else:
														_UpperCAmelCase					=      torch.arange(state.num_processes	).to(state.device	)
							_UpperCAmelCase					=      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__:		Optional[Any]	)  ->		str:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		Any	)  ->		Any:


							'''simple docstring'''





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

def 	lowerCAmelCase__			(      a__:		str	)  ->		Optional[int]:


							'''simple docstring'''





							main()

def 	lowerCAmelCase__			(      )  ->		Dict:


							'''simple docstring'''





							_UpperCAmelCase					=      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()




import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.models import Sequential

if __name__ == "__main__":
		lowerCAmelCase__  :Dict             =							pd.read_csv('''sample_data.csv''', header=None)
		lowerCAmelCase__  :int             =							df.shape[:1][0]
		# If you're using some other dataset input the target column
		lowerCAmelCase__  :Union[str, Any]             =							df.iloc[:, 1:2]
		lowerCAmelCase__  :Optional[int]             =							actual_data.values.reshape(len_data, 1)
		lowerCAmelCase__  :Tuple             =							MinMaxScaler().fit_transform(actual_data)
		lowerCAmelCase__  :str             =							1_0
		lowerCAmelCase__  :Optional[Any]             =							5
		lowerCAmelCase__  :List[str]             =							2_0
		lowerCAmelCase__  :Any             =							len_data - periods * look_back
		lowerCAmelCase__  :Union[str, Any]             =							actual_data[:division]
		lowerCAmelCase__  :Tuple             =							actual_data[division - look_back :]
		lowerCAmelCase__      ,  lowerCAmelCase__  :Optional[int]             =							[], []
		lowerCAmelCase__      ,  lowerCAmelCase__  :str             =							[], []

		for i in range(0, len(train_data) - forward_days - look_back + 1):
				train_x.append(train_data[i : i + look_back])
				train_y.append(train_data[i + look_back : i + look_back + forward_days])
		for i in range(0, len(test_data) - forward_days - look_back + 1):
				test_x.append(test_data[i : i + look_back])
				test_y.append(test_data[i + look_back : i + look_back + forward_days])
		lowerCAmelCase__  :Optional[Any]             =							np.array(train_x)
		lowerCAmelCase__  :Any             =							np.array(test_x)
		lowerCAmelCase__  :Dict             =							np.array([list(i.ravel()) for i in train_y])
		lowerCAmelCase__  :Tuple             =							np.array([list(i.ravel()) for i in test_y])

		lowerCAmelCase__  :Optional[int]             =							Sequential()
		model.add(LSTM(1_2_8, input_shape=(look_back, 1), return_sequences=True))
		model.add(LSTM(6_4, input_shape=(1_2_8, 1)))
		model.add(Dense(forward_days))
		model.compile(loss='''mean_squared_error''', optimizer='''adam''')
		lowerCAmelCase__  :List[Any]             =							model.fit(
		    x_train, y_train, epochs=1_5_0, verbose=1, shuffle=True, batch_size=4
		)
		lowerCAmelCase__  :Optional[Any]             =							model.predict(x_test)
