

# 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_       (						_A    ):





     '''simple docstring'''




     return (torch.arange(state.num_processes    ) + 1.0 + (state.num_processes * state.process_index)).to(state.device    )
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   create_tensor(_A    )
     SCREAMING_SNAKE_CASE__   =   gather(_A    )
     assert gathered_tensor.tolist() == list(range(1				,		state.num_processes**2 + 1    )    )
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   [state.process_index]
     SCREAMING_SNAKE_CASE__   =   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     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   create_tensor(_A    )
     SCREAMING_SNAKE_CASE__   =   broadcast(_A    )
     assert broadcasted_tensor.shape == torch.Size([state.num_processes]    )
     assert broadcasted_tensor.tolist() == list(range(1				,		state.num_processes + 1    )    )
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     if state.is_main_process:
          SCREAMING_SNAKE_CASE__   =   torch.arange(state.num_processes + 1    ).to(state.device    )
     else:
          SCREAMING_SNAKE_CASE__   =   torch.arange(state.num_processes    ).to(state.device    )
     SCREAMING_SNAKE_CASE__   =   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     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




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





     '''simple docstring'''




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





     '''simple docstring'''




     main()
def     UpperCAmelCase_       (						):





     '''simple docstring'''




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





from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE					:  Any             =      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE					:  int             =      {
    '''edbeeching/decision-transformer-gym-hopper-medium''': (
        '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json'''
    ),
    # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer
}






class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  a									= "decision_transformer"
  a									= ["past_key_values"]
  a									= {
      "max_position_embeddings": "n_positions",
      "num_attention_heads": "n_head",
      "num_hidden_layers": "n_layer",
  }






  def __init__(							self						:		Tuple     ,       __lowerCamelCase						:		Any=17     ,       __lowerCamelCase						:		Any=4     ,       __lowerCamelCase						:		List[Any]=128     ,       __lowerCamelCase						:		Union[str, Any]=4096     ,       __lowerCamelCase						:		Optional[Any]=True     ,       __lowerCamelCase						:		Any=1     ,       __lowerCamelCase						:		List[Any]=1024     ,       __lowerCamelCase						:		Any=3     ,       __lowerCamelCase						:		Dict=1     ,       __lowerCamelCase						:		List[Any]=None     ,       __lowerCamelCase						:		str="relu"     ,       __lowerCamelCase						:		Dict=0.1     ,       __lowerCamelCase						:		int=0.1     ,       __lowerCamelCase						:		List[str]=0.1     ,       __lowerCamelCase						:		str=1e-5     ,       __lowerCamelCase						:		Optional[Any]=0.02     ,       __lowerCamelCase						:		int=True     ,       __lowerCamelCase						:		Tuple=True     ,       __lowerCamelCase						:		Optional[int]=5_0256     ,       __lowerCamelCase						:		Tuple=5_0256     ,       __lowerCamelCase						:		int=False     ,       __lowerCamelCase						:		Optional[int]=False     ,       **__lowerCamelCase						:		Tuple     ,       )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   state_dim
       SCREAMING_SNAKE_CASE__   =   act_dim
       SCREAMING_SNAKE_CASE__   =   hidden_size
       SCREAMING_SNAKE_CASE__   =   max_ep_len
       SCREAMING_SNAKE_CASE__   =   action_tanh
       SCREAMING_SNAKE_CASE__   =   vocab_size
       SCREAMING_SNAKE_CASE__   =   n_positions
       SCREAMING_SNAKE_CASE__   =   n_layer
       SCREAMING_SNAKE_CASE__   =   n_head
       SCREAMING_SNAKE_CASE__   =   n_inner
       SCREAMING_SNAKE_CASE__   =   activation_function
       SCREAMING_SNAKE_CASE__   =   resid_pdrop
       SCREAMING_SNAKE_CASE__   =   embd_pdrop
       SCREAMING_SNAKE_CASE__   =   attn_pdrop
       SCREAMING_SNAKE_CASE__   =   layer_norm_epsilon
       SCREAMING_SNAKE_CASE__   =   initializer_range
       SCREAMING_SNAKE_CASE__   =   scale_attn_weights
       SCREAMING_SNAKE_CASE__   =   use_cache
       SCREAMING_SNAKE_CASE__   =   scale_attn_by_inverse_layer_idx
       SCREAMING_SNAKE_CASE__   =   reorder_and_upcast_attn

       SCREAMING_SNAKE_CASE__   =   bos_token_id
       SCREAMING_SNAKE_CASE__   =   eos_token_id

       super().__init__(bos_token_id=__lowerCamelCase     ,       eos_token_id=__lowerCamelCase     ,       **__lowerCamelCase )


