






'''simple docstring'''







from __future__ import annotations

from math import pi, pow





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase ) ->			List[Any]:
							if side_length < 0:
														raise ValueError("""vol_cube() only accepts non-negative values""" )
							return pow(_a							,     3 )





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			Optional[int]:
							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(_a							,     2 ) * (3 * radius - height)





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase							,     UpperCamelCase ) ->			Dict:
							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(_a							,     _a ) )

							lowerCamelCase__   :      List[Any]			    =			(
							    (radius_a - radius_a + centers_distance)
							    * (radius_a + radius_a - centers_distance)
							    / (2 * centers_distance)
							)
							lowerCamelCase__   :      Optional[Any]			    =			(
							    (radius_a - radius_a + centers_distance)
							    * (radius_a + radius_a - centers_distance)
							    / (2 * centers_distance)
							)

							return vol_spherical_cap(_a							,     _a ) + vol_spherical_cap(_a							,     _a )





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase							,     UpperCamelCase ) ->			Any:

							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(_a							,     _a ) )

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





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase							,     UpperCamelCase ) ->			List[Any]:
							if width < 0 or height < 0 or length < 0:
														raise ValueError("""vol_cuboid() only accepts non-negative values""" )
							return float(width * height * length )





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			str:
							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 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			Any:
							if height < 0 or radius < 0:
														raise ValueError("""vol_right_circ_cone() only accepts non-negative values""" )
							return pi * pow(_a							,     2 ) * height / 3.0





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			Dict:
							if height < 0 or area_of_base < 0:
														raise ValueError("""vol_prism() only accepts non-negative values""" )
							return float(area_of_base * height )





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			List[str]:
							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 					SCREAMING_SNAKE_CASE_			(UpperCamelCase ) ->			List[str]:
							if radius < 0:
														raise ValueError("""vol_sphere() only accepts non-negative values""" )
							# Volume is 4/3 * pi * radius cubed
							return 4 / 3 * pi * pow(_a							,     3 )





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase ) ->			Optional[int]:
							if radius < 0:
														raise ValueError("""vol_hemisphere() only accepts non-negative values""" )
							# Volume is radius cubed * pi * 2/3
							return pow(_a							,     3 ) * pi * 2 / 3





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			Dict:
							if height < 0 or radius < 0:
														raise ValueError("""vol_circular_cylinder() only accepts non-negative values""" )
							# Volume is radius squared * height * pi
							return pow(_a							,     2 ) * height * pi





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase							,     UpperCamelCase ) ->			int:
							# Volume - (outer_radius squared - inner_radius squared) * pi * height
							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(_a							,     2 ) - pow(_a							,     2 )) * height





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase							,     UpperCamelCase ) ->			Optional[Any]:
							# Volume is 1/3 * pi * height *
							#           (radius_1 squared + radius_2 squared + radius_1 * radius_2)
							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(_a							,     2 ) + pow(_a							,     2 ) + radius_a * radius_a)
							)





def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			int:
							if torus_radius < 0 or tube_radius < 0:
														raise ValueError("""vol_torus() only accepts non-negative values""" )
							return 2 * pow(_a							,     2 ) * torus_radius * pow(_a							,     2 )





def 					SCREAMING_SNAKE_CASE_			() ->			Optional[int]:
							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()







'''simple docstring'''







import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
   from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import PoolFormerImageProcessor


class      _a    (      __a					):
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.config_class(**self.inputs_dict							)
           self.parent.assertTrue(hasattr(lowercase  ,							'''hidden_sizes'''							)							)
           self.parent.assertTrue(hasattr(lowercase  ,							'''num_encoder_blocks'''							)							)






class      _a    :
       def __init__(      self		:      Any  ,							lowercase		:      Any  ,							lowercase		:      Optional[Any]=13  ,							lowercase		:      List[str]=64  ,							lowercase		:      Optional[Any]=3  ,							lowercase		:      List[str]=4  ,							lowercase		:      Optional[int]=[2, 2, 2, 2]  ,							lowercase		:      Tuple=[8, 4, 2, 1]  ,							lowercase		:      Optional[int]=[16, 32, 64, 128]  ,							lowercase		:      Tuple=[1, 4, 8, 16]  ,							lowercase		:      List[Any]=False  ,							lowercase		:      List[Any]=True  ,							lowercase		:      List[str]="gelu"  ,							lowercase		:      Any=0.1  ,							lowercase		:      Dict=0.02  ,							lowercase		:      str=3  ,							lowercase		:      Optional[Any]=None  ,							):



           '''simple docstring'''

           UpperCAmelCase     			=						parent
           UpperCAmelCase     			=						batch_size
           UpperCAmelCase     			=						image_size
           UpperCAmelCase     			=						num_channels
           UpperCAmelCase     			=						num_encoder_blocks
           UpperCAmelCase     			=						sr_ratios
           UpperCAmelCase     			=						depths
           UpperCAmelCase     			=						hidden_sizes
           UpperCAmelCase     			=						downsampling_rates
           UpperCAmelCase     			=						is_training
           UpperCAmelCase     			=						use_labels
           UpperCAmelCase     			=						hidden_act
           UpperCAmelCase     			=						hidden_dropout_prob
           UpperCAmelCase     			=						initializer_range
           UpperCAmelCase     			=						num_labels
           UpperCAmelCase     			=						scope
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           UpperCAmelCase     			=						None
           if self.use_labels:
               UpperCAmelCase     			=						ids_tensor([self.batch_size, self.image_size, self.image_size]  ,							self.num_labels							)

           UpperCAmelCase     			=						PoolFormerConfig(
               image_size=self.image_size  ,							num_channels=self.num_channels  ,							num_encoder_blocks=self.num_encoder_blocks  ,							depths=self.depths  ,							hidden_sizes=self.hidden_sizes  ,							hidden_act=self.hidden_act  ,							hidden_dropout_prob=self.hidden_dropout_prob  ,							initializer_range=self.initializer_range  ,							)

           return config, pixel_values, labels
       def        A (      self		:      Optional[Any]  ,							lowercase		:      List[Any]  ,							lowercase		:      Dict  ,							lowercase		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModel(config=lowercase							)
           model.to(lowercase							)
           model.eval()
           UpperCAmelCase     			=						model(lowercase							)
           UpperCAmelCase     			=						UpperCAmelCase     			=						self.image_size // 32.0
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,							(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)							)







       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.prepare_config_and_inputs()
           UpperCAmelCase			,	UpperCAmelCase			,	UpperCAmelCase     			=						config_and_inputs
           UpperCAmelCase     			=						{'''pixel_values''': pixel_values}
           return config, inputs_dict






@require_torch
class      _a    (      __a				,				__a				,				unittest.TestCase					):
       __a							:	Optional[int]  							=   (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       __a							:	Dict  							=   (
           {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       __a							:	str  							=   False
       __a							:	Optional[int]  							=   False
       __a							:	Tuple  							=   False
       __a							:	Union[str, Any]  							=   False
       __a							:	Dict  							=   False
       def        A (      self		:      Any							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModelTester(self							)
           UpperCAmelCase     			=						PoolFormerConfigTester(self  ,							config_class=lowercase							)
       def        A (      self		:      Any							):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def        A (      self		:      Optional[int]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase							)
       @unittest.skip('''PoolFormer does not use inputs_embeds'''							)
       def        A (      self		:      Dict							):



           '''simple docstring'''

           pass
       @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''							)
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           pass
       def        A (      self		:      Dict							):



           '''simple docstring'''

           def check_hidden_states_output(lowercase		:      int  ,							lowercase		:      Any  ,							lowercase		:      Union[str, Any]							):
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.eval()

               with torch.no_grad():
                   UpperCAmelCase     			=						model(**self._prepare_for_class(lowercase  ,							lowercase							)							)

               UpperCAmelCase     			=						outputs.hidden_states

               UpperCAmelCase     			=						self.model_tester.num_encoder_blocks
               self.assertEqual(len(lowercase							)  ,							lowercase							)

               # verify the first hidden states (first block)
               self.assertListEqual(
                   list(hidden_states[0].shape[-3:]							)  ,							[
                       self.model_tester.hidden_sizes[0],
                       self.model_tester.image_size // 4,
                       self.model_tester.image_size // 4,
                   ]  ,							)

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						True
               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               UpperCAmelCase     			=						True

               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)
       def        A (      self		:      Optional[Any]							):



           '''simple docstring'''

           if not self.model_tester.is_training:
               return

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase     			=						True

           for model_class in self.all_model_classes:
               if model_class in get_values(lowercase							):
                   continue
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.train()
               UpperCAmelCase     			=						self._prepare_for_class(lowercase  ,							lowercase  ,							return_labels=lowercase							)
               UpperCAmelCase     			=						model(**lowercase							).loss
               loss.backward()
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase     			=						['''pixel_values''']
               self.assertListEqual(arg_names[:1]  ,							lowercase							)







       @slow
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase     			=						PoolFormerModel.from_pretrained(lowercase							)
               self.assertIsNotNone(lowercase							)






def 		snake_case_				():
    UpperCAmelCase     			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image







@require_torch
class      _a    (      unittest.TestCase					):
       @slow
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerImageProcessor()
           UpperCAmelCase     			=						PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''							).to(lowercase							)

           UpperCAmelCase     			=						image_processor(images=prepare_img()  ,							return_tensors='''pt'''							).to(lowercase							)

           # forward pass
           with torch.no_grad():
               UpperCAmelCase     			=						model(**lowercase							)

           # verify the logits
           UpperCAmelCase     			=						torch.Size((1, 1_000)							)
           self.assertEqual(outputs.logits.shape  ,							lowercase							)

           UpperCAmelCase     			=						torch.tensor([-0.6113, 0.1685, -0.0492]							).to(lowercase							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,							lowercase  ,							atol=1E-4							)							)




