

import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

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


if is_torch_available():
       import torch

       from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
           SpeechaTextaDecoder,
           SpeechaTextaForCausalLM,
       )



@require_torch
class 					__snake_case     :

  def __init__(    self  ,						_A  ,						_A=99  ,						_A=13  ,						_A=16  ,						_A=7  ,						_A=True  ,						_A=True  ,						_A=True  ,						_A=False  ,						_A=True  ,						_A=2  ,						_A=32  ,						_A=4  ,						_A=4  ,						_A=30  ,						_A=0  ,						_A=1  ,						_A=2  ,						_A=None  ,						):
      SCREAMING_SNAKE_CASE_													=  parent
      SCREAMING_SNAKE_CASE_													=  batch_size
      SCREAMING_SNAKE_CASE_													=  decoder_seq_length
      # For common tests
      SCREAMING_SNAKE_CASE_													=  self.decoder_seq_length
      SCREAMING_SNAKE_CASE_													=  is_training
      SCREAMING_SNAKE_CASE_													=  use_attention_mask
      SCREAMING_SNAKE_CASE_													=  use_labels

      SCREAMING_SNAKE_CASE_													=  vocab_size
      SCREAMING_SNAKE_CASE_													=  d_model
      SCREAMING_SNAKE_CASE_													=  d_model
      SCREAMING_SNAKE_CASE_													=  decoder_layers
      SCREAMING_SNAKE_CASE_													=  decoder_layers
      SCREAMING_SNAKE_CASE_													=  decoder_ffn_dim
      SCREAMING_SNAKE_CASE_													=  decoder_attention_heads
      SCREAMING_SNAKE_CASE_													=  decoder_attention_heads
      SCREAMING_SNAKE_CASE_													=  eos_token_id
      SCREAMING_SNAKE_CASE_													=  bos_token_id
      SCREAMING_SNAKE_CASE_													=  pad_token_id
      SCREAMING_SNAKE_CASE_													=  decoder_start_token_id
      SCREAMING_SNAKE_CASE_													=  use_cache
      SCREAMING_SNAKE_CASE_													=  max_position_embeddings

      SCREAMING_SNAKE_CASE_													=  None
      SCREAMING_SNAKE_CASE_													=  decoder_seq_length
      SCREAMING_SNAKE_CASE_													=  2
      SCREAMING_SNAKE_CASE_													=  1

  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.decoder_seq_length]  ,						self.vocab_size)

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_attention_mask:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.decoder_seq_length]  ,						vocab_size=2)

      SCREAMING_SNAKE_CASE_													=  None
      if self.use_labels:
          SCREAMING_SNAKE_CASE_													=  ids_tensor([self.batch_size, self.decoder_seq_length]  ,						self.vocab_size)

      SCREAMING_SNAKE_CASE_													=  SpeechaTextaConfig(
          vocab_size=self.vocab_size  ,						d_model=self.d_model  ,						decoder_layers=self.decoder_layers  ,						decoder_ffn_dim=self.decoder_ffn_dim  ,						decoder_attention_heads=self.decoder_attention_heads  ,						eos_token_id=self.eos_token_id  ,						bos_token_id=self.bos_token_id  ,						use_cache=self.use_cache  ,						pad_token_id=self.pad_token_id  ,						decoder_start_token_id=self.decoder_start_token_id  ,						max_position_embeddings=self.max_position_embeddings  ,						)

      return (
          config,
          input_ids,
          attention_mask,
          lm_labels,
      )

  def   lowerCAmelCase__   (    self  ,						_A  ,						_A  ,						_A  ,						_A  ,						):
      SCREAMING_SNAKE_CASE_													=  True
      SCREAMING_SNAKE_CASE_													=  SpeechaTextaDecoder(config=_A).to(_A).eval()
      SCREAMING_SNAKE_CASE_													=  input_ids[:2]

      input_ids[input_ids == 0] += 1
      # first forward pass
      SCREAMING_SNAKE_CASE_													=  model(_A  ,						use_cache=_A)
      SCREAMING_SNAKE_CASE_													=  model(_A)
      SCREAMING_SNAKE_CASE_													=  model(_A  ,						use_cache=_A)

      self.parent.assertTrue(len(_A) == len(_A))
      self.parent.assertTrue(len(_A) == len(_A) + 1)

      SCREAMING_SNAKE_CASE_													=  outputs['past_key_values']

      # create hypothetical next token and extent to next_input_ids
      SCREAMING_SNAKE_CASE_													=  ids_tensor((2, 1)  ,						config.vocab_size - 1) + 1

      # append to next input_ids and
      SCREAMING_SNAKE_CASE_													=  torch.cat([input_ids, next_tokens]  ,						dim=-1)

      print(_A)

      SCREAMING_SNAKE_CASE_													=  model(_A)['last_hidden_state']
      SCREAMING_SNAKE_CASE_													=  model(_A  ,						past_key_values=_A)['last_hidden_state']

      # select random slice
      SCREAMING_SNAKE_CASE_													=  ids_tensor((1,)  ,						output_from_past.shape[-1]).item()
      SCREAMING_SNAKE_CASE_													=  output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
      SCREAMING_SNAKE_CASE_													=  output_from_past[:, 0, random_slice_idx].detach()

      # test that outputs are equal for slice
      assert torch.allclose(_A  ,						_A  ,						atol=1E-3)


  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  self.prepare_config_and_inputs()
      (
          (
          SCREAMING_SNAKE_CASE_
      )							,	(
          SCREAMING_SNAKE_CASE_
      )							,	(
          SCREAMING_SNAKE_CASE_
      )							,	(
          SCREAMING_SNAKE_CASE_
      )							,	
      )													=  config_and_inputs

      SCREAMING_SNAKE_CASE_													=  {
          'input_ids': input_ids,
          'attention_mask': attention_mask,
      }
      return config, inputs_dict






@require_torch
class 					__snake_case     (			lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__   ,       unittest.TestCase				):
  __lowerCAmelCase   :		str         =     (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
  __lowerCAmelCase   :		Optional[Any]         =     (SpeechaTextaForCausalLM,) if is_torch_available() else ()
  __lowerCAmelCase   :		Optional[int]         =     {'text-generation': SpeechaTextaForCausalLM} if is_torch_available() else {}
  __lowerCAmelCase   :		List[str]         =     True
  __lowerCAmelCase   :		List[str]         =     False

  def   lowerCAmelCase__   (    self  ,						):
      SCREAMING_SNAKE_CASE_													=  SpeechaTextaStandaloneDecoderModelTester(self  ,						is_training=_A)
      SCREAMING_SNAKE_CASE_													=  ConfigTester(self  ,						config_class=_A)

  def   lowerCAmelCase__   (    self):
      pass

  def   lowerCAmelCase__   (    self):
      pass

  def   lowerCAmelCase__   (    self):
      pass

  def   lowerCAmelCase__   (    self):
      self.config_tester.run_common_tests()

  def   lowerCAmelCase__   (    self):
      SCREAMING_SNAKE_CASE_													=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*_A)


  def   lowerCAmelCase__   (    self):
      return





from __future__ import annotations

from math import pi, pow


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						int | float				):





    """simple docstring"""


    if side_length < 0:
        raise ValueError('vol_cube() only accepts non-negative values'				)
    return pow(_SCREAMING_SNAKE_CASE       ,      3				)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or radius < 0:
        raise ValueError('vol_spherical_cap() only accepts non-negative values'				)
    # Volume is 1/3 pi * height squared * (3 * radius - height)
    return 1 / 3 * pi * pow(_SCREAMING_SNAKE_CASE       ,      2				) * (3 * radius - height)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if radius_a < 0 or radius_a < 0 or centers_distance < 0:
        raise ValueError('vol_spheres_intersect() only accepts non-negative values'				)
    if centers_distance == 0:
        return vol_sphere(min(_SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE				)				)

    SCREAMING_SNAKE_CASE_													=  (
        (radius_a - radius_a + centers_distance)
        * (radius_a + radius_a - centers_distance)
        / (2 * centers_distance)
    )
    SCREAMING_SNAKE_CASE_													=  (
        (radius_a - radius_a + centers_distance)
        * (radius_a + radius_a - centers_distance)
        / (2 * centers_distance)
    )

    return vol_spherical_cap(_SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE				) + vol_spherical_cap(_SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE				)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if radius_a <= 0 or radius_a <= 0 or centers_distance < 0:
        raise ValueError(
            'vol_spheres_union() only accepts non-negative values, non-zero radius'				)

    if centers_distance == 0:
        return vol_sphere(max(_SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE				)				)

    return (
        vol_sphere(_SCREAMING_SNAKE_CASE				)
        + vol_sphere(_SCREAMING_SNAKE_CASE				)
        - vol_spheres_intersect(_SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE       ,      _SCREAMING_SNAKE_CASE				)
    )


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if width < 0 or height < 0 or length < 0:
        raise ValueError('vol_cuboid() only accepts non-negative values'				)
    return float(width * height * length				)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or area_of_base < 0:
        raise ValueError('vol_cone() only accepts non-negative values'				)
    return area_of_base * height / 3.0


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or radius < 0:
        raise ValueError('vol_right_circ_cone() only accepts non-negative values'				)
    return pi * pow(_SCREAMING_SNAKE_CASE       ,      2				) * height / 3.0


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or area_of_base < 0:
        raise ValueError('vol_prism() only accepts non-negative values'				)
    return float(area_of_base * height				)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or area_of_base < 0:
        raise ValueError('vol_pyramid() only accepts non-negative values'				)
    return area_of_base * height / 3.0


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if radius < 0:
        raise ValueError('vol_sphere() only accepts non-negative values'				)
    # Volume is 4/3 * pi * radius cubed
    return 4 / 3 * pi * pow(_SCREAMING_SNAKE_CASE       ,      3				)


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if radius < 0:
        raise ValueError('vol_hemisphere() only accepts non-negative values'				)
    # Volume is radius cubed * pi * 2/3
    return pow(_SCREAMING_SNAKE_CASE       ,      3				) * pi * 2 / 3


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if height < 0 or radius < 0:
        raise ValueError('vol_circular_cylinder() only accepts non-negative values'				)
    # Volume is radius squared * height * pi
    return pow(_SCREAMING_SNAKE_CASE       ,      2				) * height * pi


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if inner_radius < 0 or outer_radius < 0 or height < 0:
        raise ValueError(
            'vol_hollow_circular_cylinder() only accepts non-negative values'				)
    if outer_radius <= inner_radius:
        raise ValueError('outer_radius must be greater than inner_radius'				)
    return pi * (pow(_SCREAMING_SNAKE_CASE       ,      2				) - pow(_SCREAMING_SNAKE_CASE       ,      2				)) * height


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if radius_a < 0 or radius_a < 0 or height < 0:
        raise ValueError('vol_conical_frustum() only accepts non-negative values'				)
    return (
        1
        / 3
        * pi
        * height
        * (pow(_SCREAMING_SNAKE_CASE       ,      2				) + pow(_SCREAMING_SNAKE_CASE       ,      2				) + radius_a * radius_a)
    )


def     _UpperCAmelCase				(     _SCREAMING_SNAKE_CASE    :						float       ,      _SCREAMING_SNAKE_CASE    :						float				):





    """simple docstring"""


    if torus_radius < 0 or tube_radius < 0:
        raise ValueError('vol_torus() only accepts non-negative values'				)
    return 2 * pow(_SCREAMING_SNAKE_CASE       ,      2				) * torus_radius * pow(_SCREAMING_SNAKE_CASE       ,      2				)


def     _UpperCAmelCase				(     ):





    """simple docstring"""


    print('Volumes:'				)
    print(f"""Cube: {vol_cube(2				) = }"""				)  # = 8
    print(f"""Cuboid: {vol_cuboid(2       ,      2       ,      2				) = }"""				)  # = 8
    print(f"""Cone: {vol_cone(2       ,      2				) = }"""				)  # ~= 1.33
    print(f"""Right Circular Cone: {vol_right_circ_cone(2       ,      2				) = }"""				)  # ~= 8.38
    print(f"""Prism: {vol_prism(2       ,      2				) = }"""				)  # = 4
    print(f"""Pyramid: {vol_pyramid(2       ,      2				) = }"""				)  # ~= 1.33
    print(f"""Sphere: {vol_sphere(2				) = }"""				)  # ~= 33.5
    print(f"""Hemisphere: {vol_hemisphere(2				) = }"""				)  # ~= 16.75
    print(f"""Circular Cylinder: {vol_circular_cylinder(2       ,      2				) = }"""				)  # ~= 25.1
    print(f"""Torus: {vol_torus(2       ,      2				) = }"""				)  # ~= 157.9
    print(f"""Conical Frustum: {vol_conical_frustum(2       ,      2       ,      4				) = }"""				)  # ~= 58.6
    print(f"""Spherical cap: {vol_spherical_cap(1       ,      2				) = }"""				)  # ~= 5.24
    print(f"""Spheres intersetion: {vol_spheres_intersect(2       ,      2       ,      1				) = }"""				)  # ~= 21.21
    print(f"""Spheres union: {vol_spheres_union(2       ,      2       ,      1				) = }"""				)  # ~= 45.81
    print(
        f"""Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1       ,      2       ,      3				) = }"""				)  # ~= 28.3


if __name__ == "__main__":
       main()


