


'''simple docstring'''






import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()






class  __magic_name__							(		lowerCAmelCase ,lowerCAmelCase ,unittest.TestCase    ):
       UpperCAmelCase										=VQModel
       UpperCAmelCase										="sample"






       @property
       def       lowerCAmelCase		(      self  ,							snake_case=(3_2, 3_2))						->  int:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =4
          _UpperCAmelCase       :       Optional[int]							   =3

          _UpperCAmelCase       :       Dict							   =floats_tensor((batch_size, num_channels) + sizes).to(snake_case)

          return {"sample": image}






       @property
       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          return (3, 3_2, 3_2)






       @property
       def       lowerCAmelCase		(      self)						->  Dict:




          '''simple docstring'''
          return (3, 3_2, 3_2)






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       List[Any]							   ={
              'block_out_channels': [3_2, 6_4],
              'in_channels': 3,
              'out_channels': 3,
              'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
              'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
              'latent_channels': 3,
          }
          _UpperCAmelCase       :       Optional[Any]							   =self.dummy_input
          return init_dict, inputs_dict






       def       lowerCAmelCase		(      self)						->  List[str]:




          '''simple docstring'''
          pass






       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          pass






       def       lowerCAmelCase		(      self)						->  Any:




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =VQModel.from_pretrained('fusing/vqgan-dummy'  ,							output_loading_info=snake_case)
          self.assertIsNotNone(snake_case)
          self.assertEqual(len(loading_info['missing_keys'])  ,							0)

          model.to(snake_case)
          _UpperCAmelCase       :       Union[str, Any]							   =model(**self.dummy_input)

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






       def       lowerCAmelCase		(      self)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =VQModel.from_pretrained('fusing/vqgan-dummy')
          model.to(snake_case).eval()

          torch.manual_seed(0)
          if torch.cuda.is_available():
             torch.cuda.manual_seed_all(0)

          _UpperCAmelCase       :       Optional[int]							   =torch.randn(1  ,							model.config.in_channels  ,							model.config.sample_size  ,							model.config.sample_size)
          _UpperCAmelCase       :       str							   =image.to(snake_case)
          with torch.no_grad():
             _UpperCAmelCase       :       Optional[int]							   =model(snake_case).sample

          _UpperCAmelCase       :       int							   =output[0, -1, -3:, -3:].flatten().cpu()
          # fmt: off
          _UpperCAmelCase       :       Any							   =torch.tensor([-0.01_53, -0.40_44, -0.18_80, -0.51_61, -0.24_18, -0.40_72, -0.16_12, -0.06_33, -0.01_43])
          # fmt: on
          self.assertTrue(torch.allclose(snake_case  ,							snake_case  ,							atol=1E-3))






'''simple docstring'''






# 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.
import re

from ..utils import cached_file


# docstyle-ignore
lowercase							='\nHuman: <<task>>\n\nAssistant: '


lowercase							='huggingface-tools/default-prompts'
lowercase							={'chat': 'chat_prompt_template.txt', 'run': 'run_prompt_template.txt'}





def lowerCamelCase__      (						__lowerCamelCase	:							Union[str, Any]	,   __lowerCamelCase	:							Union[str, Any]	,   __lowerCamelCase	:							int="run"							):







   '''simple docstring'''




   if prompt_or_repo_id is None:
      _UpperCAmelCase       :       List[str]							   =DEFAULT_PROMPTS_REPO

   # prompt is considered a repo ID when it does not contain any kind of space
   if re.search('\\s'	,   __lowerCamelCase							) is not None:
      return prompt_or_repo_id

   _UpperCAmelCase       :       Dict							   =cached_file(
       __lowerCamelCase	,   PROMPT_FILES[mode]	,   repo_type='dataset'	,   user_agent={'agent': agent_name}							)
   with open(__lowerCamelCase	,   'r'	,   encoding='utf-8'							) as f:
      return f.read()

