






"""simple docstring"""





import inspect
import unittest

from transformers import DecisionTransformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import DecisionTransformerModel
  from transformers.models.decision_transformer.modeling_decision_transformer import (
      DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
  )
class    __lowerCamelCase    :

   def __init__(					self ,				snake_case_ ,				snake_case_=13 ,				snake_case_=7 ,				snake_case_=6 ,				snake_case_=17 ,				snake_case_=23 ,				snake_case_=11 ,				snake_case_=True ,				)		->       List[str]:
       UpperCamelCase__         =    parent
       UpperCamelCase__         =    batch_size
       UpperCamelCase__         =    seq_length
       UpperCamelCase__         =    act_dim
       UpperCamelCase__         =    state_dim
       UpperCamelCase__         =    hidden_size
       UpperCamelCase__         =    max_length
       UpperCamelCase__         =    is_training

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[int]:
       UpperCamelCase__         =    floats_tensor((self.batch_size, self.seq_length, self.state_dim)      )
       UpperCamelCase__         =    floats_tensor((self.batch_size, self.seq_length, self.act_dim)      )
       UpperCamelCase__         =    floats_tensor((self.batch_size, self.seq_length, 1)      )
       UpperCamelCase__         =    floats_tensor((self.batch_size, self.seq_length, 1)      )
       UpperCamelCase__         =    ids_tensor((self.batch_size, self.seq_length) ,				vocab_size=1000      )
       UpperCamelCase__         =    random_attention_mask((self.batch_size, self.seq_length)      )

       UpperCamelCase__         =    self.get_config()

       return (
           config,
           states,
           actions,
           rewards,
           returns_to_go,
           timesteps,
           attention_mask,
       )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       return DecisionTransformerConfig(
           batch_size=self.batch_size ,				seq_length=self.seq_length ,				act_dim=self.act_dim ,				state_dim=self.state_dim ,				hidden_size=self.hidden_size ,				max_length=self.max_length ,				)

   def 						SCREAMING_SNAKE_CASE__ (					self ,				snake_case_ ,				snake_case_ ,				snake_case_ ,				snake_case_ ,				snake_case_ ,				snake_case_ ,				snake_case_ ,				)		->       str:
       UpperCamelCase__         =    DecisionTransformerModel(config=_SCREAMING_SNAKE_CASE      )
       model.to(_SCREAMING_SNAKE_CASE      )
       model.eval()
       UpperCamelCase__         =    model(_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE      )

       self.parent.assertEqual(result.state_preds.shape ,				states.shape      )
       self.parent.assertEqual(result.action_preds.shape ,				actions.shape      )
       self.parent.assertEqual(result.return_preds.shape ,				returns_to_go.shape      )
       self.parent.assertEqual(
           result.last_hidden_state.shape ,				(self.batch_size, self.seq_length * 3, self.hidden_size)      )  # seq length *3 as there are 3 modelities: states, returns and actions







   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       UpperCamelCase__         =    self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						(
           UpperCamelCase__      
       )       ,						      
       )         =    config_and_inputs
       UpperCamelCase__         =    {
           'states': states,
           'actions': actions,
           'rewards': rewards,
           'returns_to_go': returns_to_go,
           'timesteps': timesteps,
           'attention_mask': attention_mask,
       }
       return config, inputs_dict







@require_torch
class    __lowerCamelCase    (  lowerCAmelCase__							,	lowerCAmelCase__							,	lowerCAmelCase__							,	unittest.TestCase  ):
   a      :				Tuple 						=(DecisionTransformerModel,) if is_torch_available() else ()
   a      :				Union[str, Any] 						=()
   a      :				List[str] 						={"feature-extraction": DecisionTransformerModel} if is_torch_available() else {}

   # Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids
   a      :				Optional[int] 						=False

   # Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features
   a      :				List[Any] 						=False
   a      :				Optional[Any] 						=False
   a      :				Tuple 						=False
   a      :				str 						=False
   a      :				Dict 						=False
   a      :				List[str] 						=False
   a      :				Any 						=False
   a      :				Any 						=False
   a      :				Dict 						=False

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[str]:
       UpperCamelCase__         =    DecisionTransformerModelTester(self      )
       UpperCamelCase__         =    ConfigTester(self ,				config_class=_SCREAMING_SNAKE_CASE ,				hidden_size=37      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[Any]:
       self.config_tester.run_common_tests()

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Dict:
       UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE      )

   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__         =    DecisionTransformerModel.from_pretrained(_SCREAMING_SNAKE_CASE      )
           self.assertIsNotNone(_SCREAMING_SNAKE_CASE      )







   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[str]:
       UpperCamelCase__       ,						UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__         =    model_class(_SCREAMING_SNAKE_CASE      )
           UpperCamelCase__         =    inspect.signature(model.forward      )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           UpperCamelCase__         =    [*signature.parameters.keys()]

           UpperCamelCase__         =    [
               'states',
               'actions',
               'rewards',
               'returns_to_go',
               'timesteps',
               'attention_mask',
           ]

           self.assertListEqual(arg_names[: len(_SCREAMING_SNAKE_CASE      )] ,				_SCREAMING_SNAKE_CASE      )







@require_torch
class    __lowerCamelCase    (  unittest.TestCase  ):

   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[Any]:
       UpperCamelCase__         =    2  # number of steps of autoregressive prediction we will perform
       UpperCamelCase__         =    10  # defined by the RL environment, may be normalized
       UpperCamelCase__         =    DecisionTransformerModel.from_pretrained('edbeeching/decision-transformer-gym-hopper-expert'      )
       UpperCamelCase__         =    model.to(_SCREAMING_SNAKE_CASE      )
       UpperCamelCase__         =    model.config
       torch.manual_seed(0      )
       UpperCamelCase__         =    torch.randn(1 ,				1 ,				config.state_dim      ).to(device=_SCREAMING_SNAKE_CASE ,				dtype=torch.floataa      )  # env.reset()

       UpperCamelCase__         =    torch.tensor(
           [[0.242_793, -0.28_693_074, 0.8_742_613], [0.67_815_274, -0.08_101_085, -0.12_952_147]] ,				device=_SCREAMING_SNAKE_CASE      )

       UpperCamelCase__         =    torch.tensor(_SCREAMING_SNAKE_CASE ,				device=_SCREAMING_SNAKE_CASE ,				dtype=torch.floataa      ).reshape(1 ,				1 ,				1      )
       UpperCamelCase__         =    state
       UpperCamelCase__         =    torch.zeros(1 ,				0 ,				config.act_dim ,				device=_SCREAMING_SNAKE_CASE ,				dtype=torch.floataa      )
       UpperCamelCase__         =    torch.zeros(1 ,				0 ,				device=_SCREAMING_SNAKE_CASE ,				dtype=torch.floataa      )
       UpperCamelCase__         =    torch.tensor(0 ,				device=_SCREAMING_SNAKE_CASE ,				dtype=torch.long      ).reshape(1 ,				1      )

       for step in range(_SCREAMING_SNAKE_CASE      ):
           UpperCamelCase__         =    torch.cat([actions, torch.zeros(1 ,				1 ,				config.act_dim ,				device=_SCREAMING_SNAKE_CASE      )] ,				dim=1      )
           UpperCamelCase__         =    torch.cat([rewards, torch.zeros(1 ,				1 ,				device=_SCREAMING_SNAKE_CASE      )] ,				dim=1      )

           UpperCamelCase__         =    torch.ones(1 ,				states.shape[1]      ).to(dtype=torch.long ,				device=states.device      )

           with torch.no_grad():
               UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__         =    model(
                   states=_SCREAMING_SNAKE_CASE ,				actions=_SCREAMING_SNAKE_CASE ,				rewards=_SCREAMING_SNAKE_CASE ,				returns_to_go=_SCREAMING_SNAKE_CASE ,				timesteps=_SCREAMING_SNAKE_CASE ,				attention_mask=_SCREAMING_SNAKE_CASE ,				return_dict=_SCREAMING_SNAKE_CASE ,				)

           self.assertEqual(action_pred.shape ,				actions.shape      )
           self.assertTrue(torch.allclose(action_pred[0, -1] ,				expected_outputs[step] ,				atol=1E-4      )      )
           UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__       ,						UpperCamelCase__         =    (  # env.step(action)
               torch.randn(1 ,				1 ,				config.state_dim      ).to(device=_SCREAMING_SNAKE_CASE ,				dtype=torch.floataa      ),
               1.0,
               False,
               {},
           )

           UpperCamelCase__         =    action_pred[0, -1]
           UpperCamelCase__         =    torch.cat([states, state] ,				dim=1      )
           UpperCamelCase__         =    returns_to_go[0, -1] - reward
           UpperCamelCase__         =    torch.cat([returns_to_go, pred_return.reshape(1 ,				1 ,				1      )] ,				dim=1      )
           UpperCamelCase__         =    torch.cat(
               [timesteps, torch.ones((1, 1) ,				device=_SCREAMING_SNAKE_CASE ,				dtype=torch.long      ) * (step + 1)] ,				dim=1      )












"""simple docstring"""





# Copyright 2022 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 argparse
import os
import subprocess

from packaging.version import Version, parse

from accelerate.commands.config.config_args import default_config_file, load_config_from_file


A__					: Optional[Any]=	"""Run commands across TPU VMs for initial setup before running `accelerate launch`."""







def   lowerCAmelCase_( SCREAMING_SNAKE_CASE=None					)					->	Dict:
    """simple docstring"""

    if subparsers is not None:
        UpperCamelCase__         =    subparsers.add_parser('tpu-config' ,	description=_description					)
    else:
        UpperCamelCase__         =    argparse.ArgumentParser('Accelerate tpu-config command' ,	description=_description					)
    # Core arguments
    UpperCamelCase__         =    parser.add_argument_group(
        'Config Arguments' ,	'Arguments that can be configured through `accelerate config`.'					)
    config_args.add_argument(
        '--config_file' ,	type=SCREAMING_SNAKE_CASE ,	default=SCREAMING_SNAKE_CASE ,	help='Path to the config file to use for accelerate.' ,	)
    config_args.add_argument(
        '--tpu_name' ,	default=SCREAMING_SNAKE_CASE ,	help='The name of the TPU to use. If not specified, will use the TPU specified in the config file.' ,	)
    config_args.add_argument(
        '--tpu_zone' ,	default=SCREAMING_SNAKE_CASE ,	help='The zone of the TPU to use. If not specified, will use the zone specified in the config file.' ,	)
    UpperCamelCase__         =    parser.add_argument_group('TPU Arguments' ,	'Arguments for options ran inside the TPU.'					)
    pod_args.add_argument(
        '--use_alpha' ,	action='store_true' ,	help='Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.' ,	)
    pod_args.add_argument(
        '--command_file' ,	default=SCREAMING_SNAKE_CASE ,	help='The path to the file containing the commands to run on the pod on startup.' ,	)
    pod_args.add_argument(
        '--command' ,	action='append' ,	nargs='+' ,	help='A command to run on the pod. Can be passed multiple times.' ,	)
    pod_args.add_argument(
        '--install_accelerate' ,	action='store_true' ,	help='Whether to install accelerate on the pod. Defaults to False.' ,	)
    pod_args.add_argument(
        '--accelerate_version' ,	default='latest' ,	help='The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.' ,	)
    pod_args.add_argument(
        '--debug' ,	action='store_true' ,	help='If set, will print the command that would be run instead of running it.'					)

    if subparsers is not None:
        parser.set_defaults(func=SCREAMING_SNAKE_CASE					)
    return parser







def   lowerCAmelCase_( SCREAMING_SNAKE_CASE					)					->	Dict:
    """simple docstring"""

    UpperCamelCase__         =    None

    # Get the default from the config file if it exists.
    if args.config_file is not None or os.path.isfile(SCREAMING_SNAKE_CASE					):
        UpperCamelCase__         =    load_config_from_file(args.config_file					)
        if not args.command_file and defaults.command_file is not None and not args.command:
            UpperCamelCase__         =    defaults.command_file
        if not args.command and defaults.commands is not None:
            UpperCamelCase__         =    defaults.commands
        if not args.tpu_name:
            UpperCamelCase__         =    defaults.tpu_name
        if not args.tpu_zone:
            UpperCamelCase__         =    defaults.tpu_zone
    if args.accelerate_version == "dev":
        UpperCamelCase__         =    'git+https://github.com/huggingface/accelerate.git'
    elif args.accelerate_version == "latest":
        UpperCamelCase__         =    'accelerate -U'
    elif isinstance(parse(args.accelerate_version					) ,	SCREAMING_SNAKE_CASE					):
        UpperCamelCase__         =    F'accelerate=={args.accelerate_version}'

    if not args.command_file and not args.command:
        raise ValueError('You must specify either a command file or a command to run on the pod.'					)

    if args.command_file:
        with open(args.command_file ,	'r'					) as f:
            UpperCamelCase__         =    [f.read().splitlines()]

    # To turn list of lists into list of strings
    if isinstance(args.command[0] ,	SCREAMING_SNAKE_CASE					):
        UpperCamelCase__         =    [line for cmd in args.command for line in cmd]
    # Default to the shared folder and install accelerate
    UpperCamelCase__         =    ['cd /usr/share']
    if args.install_accelerate:
        new_cmd += [F'pip install {args.accelerate_version}']
    new_cmd += args.command
    UpperCamelCase__         =    '; '.join(SCREAMING_SNAKE_CASE					)

    # Then send it to gcloud
    # Eventually try to use google-api-core to do this instead of subprocess
    UpperCamelCase__         =    ['gcloud']
    if args.use_alpha:
        cmd += ["alpha"]
    cmd += [
        "compute",
        "tpus",
        "tpu-vm",
        "ssh",
        args.tpu_name,
        "--zone",
        args.tpu_zone,
        "--command",
        args.command,
        "--worker",
        "all",
    ]
    if args.debug:
        print(F'Running {" ".join(SCREAMING_SNAKE_CASE					)}'					)
        return
    subprocess.run(SCREAMING_SNAKE_CASE					)
    print('Successfully setup pod.'					)







def   lowerCAmelCase_( )					->	int:
    """simple docstring"""

    UpperCamelCase__         =    tpu_command_parser()
    UpperCamelCase__         =    parser.parse_args()

    tpu_command_launcher(SCREAMING_SNAKE_CASE					)




