

import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, slow, torch_device

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin
class     SCREAMING_SNAKE_CASE__     (						__a ,	__a ,	unittest.TestCase		):





       '''simple docstring'''




       UpperCamelCase__ :     List[Any]				    =       UNetaDModel
       UpperCamelCase__ :     Optional[Any]				    =       '''sample'''

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Optional[Any]:
             snake_case__					=   4
             snake_case__					=   14
             snake_case__					=   16

             snake_case__					=   floats_tensor((batch_size, num_features, seq_len)							).to(lowerCAmelCase__							)
             snake_case__					=   torch.tensor([10] * batch_size							).to(lowerCAmelCase__							)

             return {"sample": noise, "timestep": time_step}

       @property
       def UpperCAmelCase_						(						self       :						Dict							)			->    Union[str, Any]:
             return (4, 14, 16)

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    List[str]:
             return (4, 14, 16)

       def UpperCAmelCase_						(						self       :						str							)			->    Optional[Any]:
             pass

       def UpperCAmelCase_						(						self       :						List[Any]							)			->    int:
             pass

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Tuple:
             super().test_determinism()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Dict							)			->    Union[str, Any]:
             super().test_outputs_equivalence()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Dict							)			->    List[str]:
             super().test_from_save_pretrained()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    int:
             super().test_from_save_pretrained_variant()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Optional[Any]:
             super().test_model_from_pretrained()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Dict:
             super().test_output()

       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Tuple:
             snake_case__					=   {
                 """block_out_channels""": (32, 64, 128, 256),
                 """in_channels""": 14,
                 """out_channels""": 14,
                 """time_embedding_type""": """positional""",
                 """use_timestep_embedding""": True,
                 """flip_sin_to_cos""": False,
                 """freq_shift""": 1.0,
                 """out_block_type""": """OutConv1DBlock""",
                 """mid_block_type""": """MidResTemporalBlock1D""",
                 """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""),
                 """up_block_types""": ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D"""),
                 """act_fn""": """mish""",
             }
             snake_case__					=   self.dummy_input
             return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Tuple							)			->    Tuple:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""unet"""							)
             self.assertIsNotNone(lowerCAmelCase__							)
             self.assertEqual(len(loading_info["""missing_keys"""]							)    ,		0							)

             model.to(lowerCAmelCase__							)
             snake_case__					=   model(**self.dummy_input							)

             assert image is not None, "Make sure output is not None"

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						List[str]							)			->    List[Any]:
             snake_case__					=   UNetaDModel.from_pretrained("""bglick13/hopper-medium-v2-value-function-hor32"""    ,		subfolder="""unet"""							)
             torch.manual_seed(0							)
             if torch.cuda.is_available():
                   torch.cuda.manual_seed_all(0							)

             snake_case__					=   model.config.in_channels
             snake_case__					=   16
             snake_case__					=   torch.randn((1, seq_len, num_features)							).permute(
                 0    ,		2    ,		1							)  # match original, we can update values and remove
             snake_case__					=   torch.full((num_features,)    ,		0							)

             with torch.no_grad():
                   snake_case__					=   model(lowerCAmelCase__    ,		lowerCAmelCase__							).sample.permute(0    ,		2    ,		1							)

             snake_case__					=   output[0, -3:, -3:].flatten()
             # fmt: off
             snake_case__					=   torch.tensor([-2.137_172, 1.1_426_016, 0.3_688_687, -0.766_922, 0.7_303_146, 0.11_038_864, -0.4_760_633, 0.13_270_172, 0.02_591_348]							)
             # fmt: on
             self.assertTrue(torch.allclose(lowerCAmelCase__    ,		lowerCAmelCase__    ,		rtol=1E-3							)							)

       def UpperCAmelCase_						(						self       :						Tuple							)			->    Optional[int]:
             # Not implemented yet for this UNet
             pass

       @slow
       def UpperCAmelCase_						(						self       :						Optional[Any]							)			->    Optional[Any]:
             snake_case__					=   """harmonai/maestro-150k"""
             snake_case__					=   UNetaDModel.from_pretrained(lowerCAmelCase__    ,		subfolder="""unet"""							)
             model.to(lowerCAmelCase__							)

             snake_case__					=   65536
             snake_case__					=   torch.sin(torch.arange(lowerCAmelCase__							)[None, None, :].repeat(1    ,		2    ,		1							)							).to(lowerCAmelCase__							)
             snake_case__					=   torch.tensor([1]							).to(lowerCAmelCase__							)

             with torch.no_grad():
                   snake_case__					=   model(lowerCAmelCase__    ,		lowerCAmelCase__							).sample

             snake_case__					=   output.abs().sum()
             snake_case__					=   output.abs().max()

             assert (output_sum - 224.0_896).abs() < 0.5
             assert (output_max - 0.0_607).abs() < 4E-4



class     SCREAMING_SNAKE_CASE__     (						__a ,	__a ,	unittest.TestCase		):





       '''simple docstring'''




       UpperCamelCase__ :     Optional[int]				    =       UNetaDModel
       UpperCamelCase__ :     Dict				    =       '''sample'''

       @property
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Union[str, Any]:
             snake_case__					=   4
             snake_case__					=   14
             snake_case__					=   16

             snake_case__					=   floats_tensor((batch_size, num_features, seq_len)							).to(lowerCAmelCase__							)
             snake_case__					=   torch.tensor([10] * batch_size							).to(lowerCAmelCase__							)

             return {"sample": noise, "timestep": time_step}

       @property
       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Dict:
             return (4, 14, 16)

       @property
       def UpperCAmelCase_						(						self       :						List[Any]							)			->    Dict:
             return (4, 14, 1)

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[int]							)			->    Optional[Any]:
             super().test_determinism()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						List[Any]							)			->    Dict:
             super().test_outputs_equivalence()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						str							)			->    int:
             super().test_from_save_pretrained()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Tuple							)			->    Union[str, Any]:
             super().test_from_save_pretrained_variant()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						List[Any]							)			->    List[Any]:
             super().test_model_from_pretrained()

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						str							)			->    Optional[int]:
             # UNetRL is a value-function is different output shape
             snake_case__	,			snake_case__					=   self.prepare_init_args_and_inputs_for_common()
             snake_case__					=   self.model_class(**lowerCAmelCase__							)
             model.to(lowerCAmelCase__							)
             model.eval()

             with torch.no_grad():
                   snake_case__					=   model(**lowerCAmelCase__							)

                   if isinstance(lowerCAmelCase__    ,		lowerCAmelCase__							):
                         snake_case__					=   output.sample

             self.assertIsNotNone(lowerCAmelCase__							)
             snake_case__					=   torch.Size((inputs_dict["""sample"""].shape[0], 1)							)
             self.assertEqual(output.shape    ,		lowerCAmelCase__    ,		"""Input and output shapes do not match"""							)

       def UpperCAmelCase_						(						self       :						str							)			->    Optional[Any]:
             pass

       def UpperCAmelCase_						(						self       :						int							)			->    Union[str, Any]:
             pass

       def UpperCAmelCase_						(						self       :						Any							)			->    Optional[int]:
             snake_case__					=   {
                 """in_channels""": 14,
                 """out_channels""": 14,
                 """down_block_types""": ["""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""],
                 """up_block_types""": [],
                 """out_block_type""": """ValueFunction""",
                 """mid_block_type""": """ValueFunctionMidBlock1D""",
                 """block_out_channels""": [32, 64, 128, 256],
                 """layers_per_block""": 1,
                 """downsample_each_block""": True,
                 """use_timestep_embedding""": True,
                 """freq_shift""": 1.0,
                 """flip_sin_to_cos""": False,
                 """time_embedding_type""": """positional""",
                 """act_fn""": """mish""",
             }
             snake_case__					=   self.dummy_input
             return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Optional[Any]							)			->    Dict:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""value_function"""							)
             self.assertIsNotNone(lowerCAmelCase__							)
             self.assertEqual(len(vf_loading_info["""missing_keys"""]							)    ,		0							)

             value_function.to(lowerCAmelCase__							)
             snake_case__					=   value_function(**self.dummy_input							)

             assert image is not None, "Make sure output is not None"

       @unittest.skipIf(torch_device == """mps"""    ,		"""mish op not supported in MPS"""							)
       def UpperCAmelCase_						(						self       :						Dict							)			->    Optional[int]:
             snake_case__	,			snake_case__					=   UNetaDModel.from_pretrained(
                 """bglick13/hopper-medium-v2-value-function-hor32"""    ,		output_loading_info=lowerCAmelCase__    ,		subfolder="""value_function"""							)
             torch.manual_seed(0							)
             if torch.cuda.is_available():
                   torch.cuda.manual_seed_all(0							)

             snake_case__					=   value_function.config.in_channels
             snake_case__					=   14
             snake_case__					=   torch.randn((1, seq_len, num_features)							).permute(
                 0    ,		2    ,		1							)  # match original, we can update values and remove
             snake_case__					=   torch.full((num_features,)    ,		0							)

             with torch.no_grad():
                   snake_case__					=   value_function(lowerCAmelCase__    ,		lowerCAmelCase__							).sample

             # fmt: off
             snake_case__					=   torch.tensor([165.25] * seq_len							)
             # fmt: on
             self.assertTrue(torch.allclose(lowerCAmelCase__    ,		lowerCAmelCase__    ,		rtol=1E-3							)							)

       def UpperCAmelCase_						(						self       :						int							)			->    Union[str, Any]:
             # Not implemented yet for this UNet
             pass









# Copyright 2023 The HuggingFace Inc. 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.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool


lowerCAmelCase						:   List[Any]										=    '''Here is a text containing a lot of information: \'\'\'{text}\'\'\'.

Can you answer this question about the text: \'{question}\''''
class     SCREAMING_SNAKE_CASE__     (						__a		):





       '''simple docstring'''




       UpperCamelCase__ :     int				    =       '''google/flan-t5-base'''
       UpperCamelCase__ :     int				    =       (
           '''This is a tool that answers questions related to a text. It takes two arguments named `text`, which is the '''
           '''text where to find the answer, and `question`, which is the question, and returns the answer to the question.'''
       )
       UpperCamelCase__ :     Tuple				    =       '''text_qa'''
       UpperCamelCase__ :     Tuple				    =       AutoTokenizer
       UpperCamelCase__ :     List[Any]				    =       AutoModelForSeqaSeqLM

       UpperCamelCase__ :     Optional[Any]				    =       ['''text''', '''text''']
       UpperCamelCase__ :     Any				    =       ['''text''']

       def UpperCAmelCase_						(						self       :						Tuple    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						str							)			->    List[Any]:
             snake_case__					=   QA_PROMPT.format(text=lowerCAmelCase__    ,		question=lowerCAmelCase__							)
             return self.pre_processor(lowerCAmelCase__    ,		return_tensors="""pt"""							)

       def UpperCAmelCase_						(						self       :						Optional[Any]    ,		lowerCAmelCase__       :						Dict							)			->    List[Any]:
             snake_case__					=   self.model.generate(**lowerCAmelCase__							)

             snake_case__	,			snake_case__					=   inputs["""input_ids"""].shape
             snake_case__					=   output_ids.shape[0]

             return output_ids.reshape(lowerCAmelCase__    ,		out_b // in_b    ,		*output_ids.shape[1:]							)[0][0]

       def UpperCAmelCase_						(						self       :						Union[str, Any]    ,		lowerCAmelCase__       :						Dict							)			->    List[Any]:
             return self.pre_processor.decode(lowerCAmelCase__    ,		skip_special_tokens=lowerCAmelCase__    ,		clean_up_tokenization_spaces=lowerCAmelCase__							)






