



'''simple docstring'''





# Copyright 2023 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.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCAmelCase										=     {
    '''configuration_vivit''': ['''VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VivitConfig'''],
}
try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase										=     ['''VivitImageProcessor''']


try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase										=     [
     '''VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''VivitModel''',
     '''VivitPreTrainedModel''',
     '''VivitForVideoClassification''',
 ]


if TYPE_CHECKING:
 from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .image_processing_vivit import VivitImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_vivit import (
      VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      VivitForVideoClassification,
      VivitModel,
      VivitPreTrainedModel,
  )


else:
 import sys

 _lowerCAmelCase										=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)







'''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 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_						,			unittest.TestCase					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				VQModel
  __lowercase							:				Tuple           =				'''sample'''




  @property
  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase=(32, 32)				)   ->		List[Any]:
      lowerCAmelCase__			:  Dict											= 4
      lowerCAmelCase__			:  List[str]											= 3

      lowerCAmelCase__			:  Dict											= floats_tensor((batch_size, num_channels) + sizes				).to(__UpperCAmelCase				)

      return {"sample": image}




  @property
  def 							UpperCAmelCase_     (   self				)   ->		List[Any]:
      return (3, 32, 32)




  @property
  def 							UpperCAmelCase_     (   self				)   ->		Optional[Any]:
      return (3, 32, 32)




  def 							UpperCAmelCase_     (   self				)   ->		List[Any]:
      lowerCAmelCase__			:  Dict											= {
          """block_out_channels""": [32, 64],
          """in_channels""": 3,
          """out_channels""": 3,
          """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""],
          """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""],
          """latent_channels""": 3,
      }
      lowerCAmelCase__			:  Any											= self.dummy_input
      return init_dict, inputs_dict




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




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




  def 							UpperCAmelCase_     (   self				)   ->		Tuple:
      lowerCAmelCase__				,       lowerCAmelCase__			:  Union[str, Any]											= VQModel.from_pretrained("""fusing/vqgan-dummy"""  ,output_loading_info=__UpperCAmelCase				)
      self.assertIsNotNone(__UpperCAmelCase				)
      self.assertEqual(len(loading_info["""missing_keys"""]				)  ,0				)

      model.to(__UpperCAmelCase				)
      lowerCAmelCase__			:  List[Any]											= model(**self.dummy_input				)

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






  def 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  Tuple											= VQModel.from_pretrained("""fusing/vqgan-dummy"""				)
      model.to(__UpperCAmelCase				).eval()

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

      lowerCAmelCase__			:  List[str]											= torch.randn(1  ,model.config.in_channels  ,model.config.sample_size  ,model.config.sample_size				)
      lowerCAmelCase__			:  Optional[Any]											= image.to(__UpperCAmelCase				)
      with torch.no_grad():
          lowerCAmelCase__			:  Optional[Any]											= model(__UpperCAmelCase				).sample

      lowerCAmelCase__			:  Tuple											= output[0, -1, -3:, -3:].flatten().cpu()
      # fmt: off
      lowerCAmelCase__			:  Tuple											= torch.tensor([-0.0_1_5_3, -0.4_0_4_4, -0.1_8_8_0, -0.5_1_6_1, -0.2_4_1_8, -0.4_0_7_2, -0.1_6_1_2, -0.0_6_3_3, -0.0_1_4_3]				)
      # fmt: on
      self.assertTrue(torch.allclose(__UpperCAmelCase  ,__UpperCAmelCase  ,atol=1E-3				)				)


