



import math
from collections import defaultdict
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput






def 	_lowerCAmelCase						(				__lowerCamelCase   :     Optional[int]		,	__lowerCamelCase   :     Any=0.999		,	__lowerCamelCase   :     List[Any]="cosine"		,	):



 """simple docstring"""





 if alpha_transform_type == "cosine":

  def alpha_bar_fn(__lowerCamelCase   :     Optional[Any]     ):
   return math.cos((t + 0.008) / 1.008 * math.pi / 2     ) ** 2

 elif alpha_transform_type == "exp":

  def alpha_bar_fn(__lowerCamelCase   :     Union[str, Any]     ):
   return math.exp(t * -12.0     )

 else:
  raise ValueError(F"""Unsupported alpha_tranform_type: {alpha_transform_type}"""     )

 __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	[]
 for i in range(__lowerCamelCase     ):
  __SCREAMING_SNAKE_CASE       :    str       =	i / num_diffusion_timesteps
  __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	(i + 1) / num_diffusion_timesteps
  betas.append(min(1 - alpha_bar_fn(__lowerCamelCase     ) / alpha_bar_fn(__lowerCamelCase     )		,	__lowerCamelCase     )     )
 return torch.tensor(__lowerCamelCase		,	dtype=torch.floataa     )






class       _SCREAMING_SNAKE_CASE	(UpperCamelCase			,    UpperCamelCase ):
     lowerCAmelCase					=			[e.name for e in KarrasDiffusionSchedulers]
     lowerCAmelCase					=			2





     @register_to_config
     def __init__(       self :							Union[str, Any]     ,  UpperCamelCase :							int = 1_0_0_0     ,  UpperCamelCase :							float = 0.0_0_0_8_5     ,  UpperCamelCase :							float = 0.0_1_2     ,  UpperCamelCase :							str = "linear"     ,  UpperCamelCase :							Optional[Union[np.ndarray, List[float]]] = None     ,  UpperCamelCase :							str = "epsilon"     ,  UpperCamelCase :							str = "linspace"     ,  UpperCamelCase :							int = 0     ,  )->Any:
      if trained_betas is not None:
       __SCREAMING_SNAKE_CASE       :    Dict       =	torch.tensor(UpperCamelCase     ,  dtype=torch.floataa				)
      elif beta_schedule == "linear":
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	torch.linspace(UpperCamelCase     ,  UpperCamelCase     ,  UpperCamelCase     ,  dtype=torch.floataa				)
      elif beta_schedule == "scaled_linear":
       # this schedule is very specific to the latent diffusion model.
       __SCREAMING_SNAKE_CASE       :    Dict       =	(
           torch.linspace(beta_start**0.5     ,  beta_end**0.5     ,  UpperCamelCase     ,  dtype=torch.floataa				) ** 2
       )
      elif beta_schedule == "squaredcos_cap_v2":
       # Glide cosine schedule
       __SCREAMING_SNAKE_CASE       :    str       =	betas_for_alpha_bar(UpperCamelCase				)
      else:
       raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}"""				)

      __SCREAMING_SNAKE_CASE       :    Dict       =	1.0 - self.betas
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	torch.cumprod(self.alphas     ,  dim=0				)

      #  set all values
      self.set_timesteps(UpperCamelCase     ,  UpperCamelCase     ,  UpperCamelCase				)





     def 	__snake_case    (       self :							Any     ,  UpperCamelCase :							int     ,  UpperCamelCase :							List[Any]=None				)->List[Any]:
      if schedule_timesteps is None:
       __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	self.timesteps

      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	(schedule_timesteps == timestep).nonzero()

      # The sigma index that is taken for the **very** first `step`
      # is always the second index (or the last index if there is only 1)
      # This way we can ensure we don't accidentally skip a sigma in
      # case we start in the middle of the denoising schedule (e.g. for image-to-image)
      if len(self._index_counter				) == 0:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	1 if len(UpperCamelCase				) > 1 else 0
      else:
       __SCREAMING_SNAKE_CASE       :    Dict       =	timestep.cpu().item() if torch.is_tensor(UpperCamelCase				) else timestep
       __SCREAMING_SNAKE_CASE       :    int       =	self._index_counter[timestep_int]

      return indices[pos].item()





     @property
     def 	__snake_case    (       self :							List[str]				)->Union[str, Any]:
      # standard deviation of the initial noise distribution
      if self.config.timestep_spacing in ["linspace", "trailing"]:
       return self.sigmas.max()

      return (self.sigmas.max() ** 2 + 1) ** 0.5





     def 	__snake_case    (       self :							List[str]     ,  UpperCamelCase :							torch.FloatTensor     ,  UpperCamelCase :							Union[float, torch.FloatTensor]     ,  )->torch.FloatTensor:
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	self.index_for_timestep(UpperCamelCase				)

      if self.state_in_first_order:
       __SCREAMING_SNAKE_CASE       :    str       =	self.sigmas[step_index]
      else:
       __SCREAMING_SNAKE_CASE       :    List[str]       =	self.sigmas_interpol[step_index]

      __SCREAMING_SNAKE_CASE       :    List[Any]       =	sample / ((sigma**2 + 1) ** 0.5)
      return sample





     def 	__snake_case    (       self :							int     ,  UpperCamelCase :							int     ,  UpperCamelCase :							Union[str, torch.device] = None     ,  UpperCamelCase :							Optional[int] = None     ,  )->Any:
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_inference_steps

      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_train_timesteps or self.config.num_train_timesteps

      # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
      if self.config.timestep_spacing == "linspace":
       __SCREAMING_SNAKE_CASE       :    Tuple       =	np.linspace(0     ,  num_train_timesteps - 1     ,  UpperCamelCase     ,  dtype=UpperCamelCase				)[::-1].copy()
      elif self.config.timestep_spacing == "leading":
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	num_train_timesteps // self.num_inference_steps
       # creates integer timesteps by multiplying by ratio
       # casting to int to avoid issues when num_inference_step is power of 3
       __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	(np.arange(0     ,  UpperCamelCase				) * step_ratio).round()[::-1].copy().astype(UpperCamelCase				)
       timesteps += self.config.steps_offset
      elif self.config.timestep_spacing == "trailing":
       __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	num_train_timesteps / self.num_inference_steps
       # creates integer timesteps by multiplying by ratio
       # casting to int to avoid issues when num_inference_step is power of 3
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	(np.arange(UpperCamelCase     ,  0     ,  -step_ratio				)).round().copy().astype(UpperCamelCase				)
       timesteps -= 1
      else:
       raise ValueError(
           F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""				)

      __SCREAMING_SNAKE_CASE       :    Any       =	np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5				)
      __SCREAMING_SNAKE_CASE       :    Any       =	torch.from_numpy(np.log(UpperCamelCase				)				).to(UpperCamelCase				)

      __SCREAMING_SNAKE_CASE       :    Any       =	np.interp(UpperCamelCase     ,  np.arange(0     ,  len(UpperCamelCase				)				)     ,  UpperCamelCase				)
      __SCREAMING_SNAKE_CASE       :    Tuple       =	np.concatenate([sigmas, [0.0]]				).astype(np.floataa				)
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	torch.from_numpy(UpperCamelCase				).to(device=UpperCamelCase				)

      # interpolate sigmas
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	sigmas.log().lerp(sigmas.roll(1				).log()     ,  0.5				).exp()

      __SCREAMING_SNAKE_CASE       :    List[Any]       =	torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2				), sigmas[-1:]]				)
      __SCREAMING_SNAKE_CASE       :    int       =	torch.cat(
          [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2				), sigmas_interpol[-1:]]				)

      if str(UpperCamelCase				).startswith("mps"				):
       # mps does not support float64
       __SCREAMING_SNAKE_CASE       :    List[str]       =	torch.from_numpy(UpperCamelCase				).to(UpperCamelCase     ,  dtype=torch.floataa				)
      else:
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	torch.from_numpy(UpperCamelCase				).to(UpperCamelCase				)

      # interpolate timesteps
      __SCREAMING_SNAKE_CASE       :    str       =	self.sigma_to_t(UpperCamelCase				).to(UpperCamelCase     ,  dtype=timesteps.dtype				)
      __SCREAMING_SNAKE_CASE       :    str       =	torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None])     ,  dim=-1				).flatten()

      __SCREAMING_SNAKE_CASE       :    Tuple       =	torch.cat([timesteps[:1], interleaved_timesteps]				)

      __SCREAMING_SNAKE_CASE       :    str       =	None

      # for exp beta schedules, such as the one for `pipeline_shap_e.py`
      # we need an index counter
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	defaultdict(UpperCamelCase				)





     def 	__snake_case    (       self :							Tuple     ,  UpperCamelCase :							Dict				)->int:
      # get log sigma
      __SCREAMING_SNAKE_CASE       :    List[str]       =	sigma.log()

      # get distribution
      __SCREAMING_SNAKE_CASE       :    Tuple       =	log_sigma - self.log_sigmas[:, None]

      # get sigmas range
      __SCREAMING_SNAKE_CASE       :    Any       =	dists.ge(0				).cumsum(dim=0				).argmax(dim=0				).clamp(max=self.log_sigmas.shape[0] - 2				)
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	low_idx + 1

      __SCREAMING_SNAKE_CASE       :    str       =	self.log_sigmas[low_idx]
      __SCREAMING_SNAKE_CASE       :    Any       =	self.log_sigmas[high_idx]

      # interpolate sigmas
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	(low - log_sigma) / (low - high)
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	w.clamp(0     ,  1				)

      # transform interpolation to time range
      __SCREAMING_SNAKE_CASE       :    Any       =	(1 - w) * low_idx + w * high_idx
      __SCREAMING_SNAKE_CASE       :    Any       =	t.view(sigma.shape				)
      return t





     @property
     def 	__snake_case    (       self :							str				)->Dict:
      return self.sample is None





     def 	__snake_case    (       self :							str     ,  UpperCamelCase :							Union[torch.FloatTensor, np.ndarray]     ,  UpperCamelCase :							Union[float, torch.FloatTensor]     ,  UpperCamelCase :							Union[torch.FloatTensor, np.ndarray]     ,  UpperCamelCase :							bool = True     ,  )->Union[SchedulerOutput, Tuple]:
      __SCREAMING_SNAKE_CASE       :    Dict       =	self.index_for_timestep(UpperCamelCase				)

      # advance index counter by 1
      __SCREAMING_SNAKE_CASE       :    List[str]       =	timestep.cpu().item() if torch.is_tensor(UpperCamelCase				) else timestep
      self._index_counter[timestep_int] += 1

      if self.state_in_first_order:
       __SCREAMING_SNAKE_CASE       :    Tuple       =	self.sigmas[step_index]
       __SCREAMING_SNAKE_CASE       :    str       =	self.sigmas_interpol[step_index + 1]
       __SCREAMING_SNAKE_CASE       :    Dict       =	self.sigmas[step_index + 1]
      else:
       # 2nd order / KDPM2's method
       __SCREAMING_SNAKE_CASE       :    Any       =	self.sigmas[step_index - 1]
       __SCREAMING_SNAKE_CASE       :    int       =	self.sigmas_interpol[step_index]
       __SCREAMING_SNAKE_CASE       :    List[str]       =	self.sigmas[step_index]

      # currently only gamma=0 is supported. This usually works best anyways.
      # We can support gamma in the future but then need to scale the timestep before
      # passing it to the model which requires a change in API
      __SCREAMING_SNAKE_CASE       :    List[str]       =	0
      __SCREAMING_SNAKE_CASE       :    List[str]       =	sigma * (gamma + 1)  # Note: sigma_hat == sigma for now

      # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
      if self.config.prediction_type == "epsilon":
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	sigma_hat if self.state_in_first_order else sigma_interpol
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	sample - sigma_input * model_output
      elif self.config.prediction_type == "v_prediction":
       __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	sigma_hat if self.state_in_first_order else sigma_interpol
       __SCREAMING_SNAKE_CASE       :    Tuple       =	model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + (
           sample / (sigma_input**2 + 1)
       )
      elif self.config.prediction_type == "sample":
       raise NotImplementedError("prediction_type not implemented yet: sample"				)
      else:
       raise ValueError(
           F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""				)

      if self.state_in_first_order:
       # 2. Convert to an ODE derivative for 1st order
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	(sample - pred_original_sample) / sigma_hat
       # 3. delta timestep
       __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	sigma_interpol - sigma_hat

       # store for 2nd order step
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	sample
      else:
       # DPM-Solver-2
       # 2. Convert to an ODE derivative for 2nd order
       __SCREAMING_SNAKE_CASE       :    Tuple       =	(sample - pred_original_sample) / sigma_interpol

       # 3. delta timestep
       __SCREAMING_SNAKE_CASE       :    Dict       =	sigma_next - sigma_hat

       __SCREAMING_SNAKE_CASE       :    List[str]       =	self.sample
       __SCREAMING_SNAKE_CASE       :    int       =	None

      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	sample + derivative * dt

      if not return_dict:
       return (prev_sample,)

      return SchedulerOutput(prev_sample=UpperCamelCase				)





     def 	__snake_case    (       self :							Optional[int]     ,  UpperCamelCase :							torch.FloatTensor     ,  UpperCamelCase :							torch.FloatTensor     ,  UpperCamelCase :							torch.FloatTensor     ,  )->torch.FloatTensor:
      # Make sure sigmas and timesteps have the same device and dtype as original_samples
      __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	self.sigmas.to(device=original_samples.device     ,  dtype=original_samples.dtype				)
      if original_samples.device.type == "mps" and torch.is_floating_point(UpperCamelCase				):
       # mps does not support float64
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	self.timesteps.to(original_samples.device     ,  dtype=torch.floataa				)
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	timesteps.to(original_samples.device     ,  dtype=torch.floataa				)
      else:
       __SCREAMING_SNAKE_CASE       :    Optional[int]       =	self.timesteps.to(original_samples.device				)
       __SCREAMING_SNAKE_CASE       :    Dict       =	timesteps.to(original_samples.device				)

      __SCREAMING_SNAKE_CASE       :    Any       =	[self.index_for_timestep(UpperCamelCase     ,  UpperCamelCase				) for t in timesteps]

      __SCREAMING_SNAKE_CASE       :    str       =	sigmas[step_indices].flatten()
      while len(sigma.shape				) < len(original_samples.shape				):
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	sigma.unsqueeze(-1				)

      __SCREAMING_SNAKE_CASE       :    Tuple       =	original_samples + noise * sigma
      return noisy_samples






     def __len__(       self :							int				)->Optional[Any]:
      return self.config.num_train_timesteps











# Copyright 2021 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 json
import os

from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES
from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType
from ...utils.imports import is_botoa_available
from .config_args import SageMakerConfig
from .config_utils import (
    DYNAMO_BACKENDS,
    _ask_field,
    _ask_options,
    _convert_dynamo_backend,
    _convert_mixed_precision,
    _convert_sagemaker_distributed_mode,
    _convert_yes_no_to_bool,
)


if is_botoa_available():
      import botoa  # noqa: F401






def 	_lowerCAmelCase						(				__lowerCamelCase   :     Optional[int]     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Optional[int]       =	botoa.client("iam"     )

 __SCREAMING_SNAKE_CASE       :    List[Any]       =	{
     "Version": "2012-10-17",
     "Statement": [
         {"Effect": "Allow", "Principal": {"Service": "sagemaker.amazonaws.com"}, "Action": "sts:AssumeRole"}
     ],
 }
 try:
  # create the role, associated with the chosen trust policy
  iam_client.create_role(
      RoleName=__lowerCamelCase		,	AssumeRolePolicyDocument=json.dumps(__lowerCamelCase		,	indent=2     )     )
  __SCREAMING_SNAKE_CASE       :    str       =	{
      "Version": "2012-10-17",
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "sagemaker:*",
                  "ecr:GetDownloadUrlForLayer",
                  "ecr:BatchGetImage",
                  "ecr:BatchCheckLayerAvailability",
                  "ecr:GetAuthorizationToken",
                  "cloudwatch:PutMetricData",
                  "cloudwatch:GetMetricData",
                  "cloudwatch:GetMetricStatistics",
                  "cloudwatch:ListMetrics",
                  "logs:CreateLogGroup",
                  "logs:CreateLogStream",
                  "logs:DescribeLogStreams",
                  "logs:PutLogEvents",
                  "logs:GetLogEvents",
                  "s3:CreateBucket",
                  "s3:ListBucket",
                  "s3:GetBucketLocation",
                  "s3:GetObject",
                  "s3:PutObject",
              ],
              "Resource": "*",
          }
      ],
  }
  # attach policy to role
  iam_client.put_role_policy(
      RoleName=__lowerCamelCase		,	PolicyName=F"""{role_name}_policy_permission"""		,	PolicyDocument=json.dumps(__lowerCamelCase		,	indent=2     )		,	)
 except iam_client.exceptions.EntityAlreadyExistsException:
  print(F"""role {role_name} already exists. Using existing one"""     )






def 	_lowerCAmelCase						(				__lowerCamelCase   :     Dict     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    List[str]       =	botoa.client("iam"     )
 return iam_client.get_role(RoleName=__lowerCamelCase     )["Role"]["Arn"]






def 	_lowerCAmelCase						(				):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Any       =	_ask_options(
     "How do you want to authorize?"		,	["AWS Profile", "Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) "]		,	__lowerCamelCase		,	)
 __SCREAMING_SNAKE_CASE       :    str       =	None
 if credentials_configuration == 0:
  __SCREAMING_SNAKE_CASE       :    List[str]       =	_ask_field("Enter your AWS Profile name: [default] "		,	default="default"     )
  __SCREAMING_SNAKE_CASE       :    Any       =	aws_profile
 else:
  print(
      "Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,"
      "`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`"     )
  __SCREAMING_SNAKE_CASE       :    Dict       =	_ask_field("AWS Access Key ID: "     )
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	aws_access_key_id

  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	_ask_field("AWS Secret Access Key: "     )
  __SCREAMING_SNAKE_CASE       :    Optional[int]       =	aws_secret_access_key

 __SCREAMING_SNAKE_CASE       :    List[Any]       =	_ask_field("Enter your AWS Region: [us-east-1]"		,	default="us-east-1"     )
 __SCREAMING_SNAKE_CASE       :    Any       =	aws_region

 __SCREAMING_SNAKE_CASE       :    int       =	_ask_options(
     "Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?"		,	["Provide IAM Role name", "Create new IAM role using credentials"]		,	__lowerCamelCase		,	)
 if role_management == 0:
  __SCREAMING_SNAKE_CASE       :    List[str]       =	_ask_field("Enter your IAM role name: "     )
 else:
  __SCREAMING_SNAKE_CASE       :    Any       =	"accelerate_sagemaker_execution_role"
  print(F"""Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials"""     )
  _create_iam_role_for_sagemaker(__lowerCamelCase     )

 __SCREAMING_SNAKE_CASE       :    Any       =	_ask_field(
     "Do you want to use custom Docker image? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
 __SCREAMING_SNAKE_CASE       :    Tuple       =	None
 if is_custom_docker_image:
  __SCREAMING_SNAKE_CASE       :    List[Any]       =	_ask_field("Enter your Docker image: "		,	lambda __lowerCamelCase     : str(__lowerCamelCase     ).lower()     )

 __SCREAMING_SNAKE_CASE       :    Dict       =	_ask_field(
     "Do you want to provide SageMaker input channels with data locations? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
 __SCREAMING_SNAKE_CASE       :    List[Any]       =	None
 if is_sagemaker_inputs_enabled:
  __SCREAMING_SNAKE_CASE       :    List[str]       =	_ask_field(
      "Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): "		,	lambda __lowerCamelCase     : str(__lowerCamelCase     ).lower()		,	)

 __SCREAMING_SNAKE_CASE       :    Dict       =	_ask_field(
     "Do you want to enable SageMaker metrics? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
 __SCREAMING_SNAKE_CASE       :    int       =	None
 if is_sagemaker_metrics_enabled:
  __SCREAMING_SNAKE_CASE       :    Dict       =	_ask_field(
      "Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): "		,	lambda __lowerCamelCase     : str(__lowerCamelCase     ).lower()		,	)

 __SCREAMING_SNAKE_CASE       :    Any       =	_ask_options(
     "What is the distributed mode?"		,	["No distributed training", "Data parallelism"]		,	_convert_sagemaker_distributed_mode		,	)
 __SCREAMING_SNAKE_CASE       :    Tuple       =	{}
 __SCREAMING_SNAKE_CASE       :    str       =	_ask_field(
     "Do you wish to optimize your script with torch dynamo?[yes/NO]:"		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
 if use_dynamo:
  __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	"dynamo_"
  __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	_ask_options(
      "Which dynamo backend would you like to use?"		,	[x.lower() for x in DYNAMO_BACKENDS]		,	_convert_dynamo_backend		,	default=2		,	)
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	_ask_field(
      "Do you want to customize the defaults sent to torch.compile? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)

  if use_custom_options:
   __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	_ask_options(
       "Which mode do you want to use?"		,	__lowerCamelCase		,	lambda __lowerCamelCase     : TORCH_DYNAMO_MODES[int(__lowerCamelCase     )]		,	default="default"		,	)
   __SCREAMING_SNAKE_CASE       :    str       =	_ask_field(
       "Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
   __SCREAMING_SNAKE_CASE       :    Any       =	_ask_field(
       "Do you want to enable dynamic shape tracing? [yes/NO]: "		,	_convert_yes_no_to_bool		,	default=__lowerCamelCase		,	error_message="Please enter yes or no."		,	)
 __SCREAMING_SNAKE_CASE       :    Optional[int]       =	"Which EC2 instance type you want to use for your training?"
 if distributed_type != SageMakerDistributedType.NO:
  __SCREAMING_SNAKE_CASE       :    List[Any]       =	_ask_options(
      __lowerCamelCase		,	__lowerCamelCase		,	lambda __lowerCamelCase     : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__lowerCamelCase     )]     )
 else:
  eca_instance_query += "? [ml.p3.2xlarge]:"
  __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	_ask_field(__lowerCamelCase		,	lambda __lowerCamelCase     : str(__lowerCamelCase     ).lower()		,	default="ml.p3.2xlarge"     )

 __SCREAMING_SNAKE_CASE       :    List[Any]       =	1
 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
  __SCREAMING_SNAKE_CASE       :    Any       =	_ask_field(
      "How many machines do you want use? [1]: "		,	__lowerCamelCase		,	default=1		,	)

 __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	_ask_options(
     "Do you wish to use FP16 or BF16 (mixed precision)?"		,	["no", "fp16", "bf16", "fp8"]		,	_convert_mixed_precision		,	)

 if use_dynamo and mixed_precision == "no":
  print(
      "Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts."     )

 return SageMakerConfig(
     image_uri=__lowerCamelCase		,	compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER		,	distributed_type=__lowerCamelCase		,	use_cpu=__lowerCamelCase		,	dynamo_config=__lowerCamelCase		,	eca_instance_type=__lowerCamelCase		,	profile=__lowerCamelCase		,	region=__lowerCamelCase		,	iam_role_name=__lowerCamelCase		,	mixed_precision=__lowerCamelCase		,	num_machines=__lowerCamelCase		,	sagemaker_inputs_file=__lowerCamelCase		,	sagemaker_metrics_file=__lowerCamelCase		,	)






