
'''simple docstring'''







import bsa
import requests







def   lowerCAmelCase(  a__    :  bsa.element.ResultSet      ):







       '''simple docstring'''






       return {
           "name": soup.ha.a.text,
           "genre": soup.find("span"      , class_="genre"      ).text.strip(),
           "rating": soup.strong.text,
           "page_link": f"""https://www.imdb.com{soup.a.get("href"      )}""",
       }







def   lowerCAmelCase(  a__    :  int = 5      ):







       '''simple docstring'''






       lowerCamelCase__				   =      int(float(a__      )      )
       if num_movies < 1:
              return ()
       lowerCamelCase__				   =      (
           "https://www.imdb.com/search/title?title_type="
           f"""feature&sort=num_votes,desc&count={num_movies}"""
       )
       lowerCamelCase__				   =      bsa.BeautifulSoup(requests.get(a__      ).content      , "html.parser"      )
       return tuple(
           get_movie_data_from_soup(a__      )
           for movie in source.find_all("div"      , class_="lister-item mode-advanced"      )      )


if __name__ == "__main__":
    import json

    lowerCAmelCase_      =   int(input("How many movies would you like to see? "))
    print(
        ", ".join(
            json.dumps(movie, indent=4) for movie in get_imdb_top_movies(num_movies)
        )
    )



'''simple docstring'''







import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

lowerCAmelCase_      =   logging.get_logger(__name__)







def   lowerCAmelCase(  a__    :  List[Any]      , a__    :  bool = True      ):







       '''simple docstring'''






       lowerCamelCase__				   =      {}
       for name in model.attn_processors.keys():
              lowerCamelCase__				   =      name.endswith("attn2.processor"      ) and not (
                  name.startswith("transformer_in"      ) or "temp_attentions" in name.split("."      )
              )
              lowerCamelCase__				   =      model.config.cross_attention_dim if has_cross_attention else None
              if name.startswith("mid_block"      ):
                     lowerCamelCase__				   =      model.config.block_out_channels[-1]
              elif name.startswith("up_blocks"      ):
                     lowerCamelCase__				   =      int(name[len("up_blocks."      )]      )
                     lowerCamelCase__				   =      list(reversed(model.config.block_out_channels      )      )[block_id]
              elif name.startswith("down_blocks"      ):
                     lowerCamelCase__				   =      int(name[len("down_blocks."      )]      )
                     lowerCamelCase__				   =      model.config.block_out_channels[block_id]
              elif name.startswith("transformer_in"      ):
                     # Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
                     lowerCamelCase__				   =      8 * model.config.attention_head_dim

              lowerCamelCase__				   =      LoRAAttnProcessor(hidden_size=a__      , cross_attention_dim=a__      )
              lowerCamelCase__				   =      lora_attn_procs[name].to(model.device      )

              if mock_weights:
                     # add 1 to weights to mock trained weights
                     with torch.no_grad():
                            lora_attn_procs[name].to_q_lora.up.weight += 1
                            lora_attn_procs[name].to_k_lora.up.weight += 1
                            lora_attn_procs[name].to_v_lora.up.weight += 1
                            lora_attn_procs[name].to_out_lora.up.weight += 1

       return lora_attn_procs







@skip_mps
class 		snake_case_       (						A__		,	A__		,	unittest.TestCase						):



   """simple docstring"""





   __lowerCAmelCase	:							Optional[int]								=UNetaDConditionModel
   __lowerCAmelCase	:							int								='''sample'''







   @property
   def     __UpperCAmelCase      ( self):
          lowerCamelCase__				   =      4
          lowerCamelCase__				   =      4
          lowerCamelCase__				   =      4
          lowerCamelCase__				   =      (32, 32)

          lowerCamelCase__				   =      floats_tensor((batch_size, num_channels, num_frames) + sizes).to(UpperCamelCase)
          lowerCamelCase__				   =      torch.tensor([10]).to(UpperCamelCase)
          lowerCamelCase__				   =      floats_tensor((batch_size, 4, 32)).to(UpperCamelCase)

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







   @property
   def     __UpperCAmelCase      ( self):
          return (4, 4, 32, 32)







   @property
   def     __UpperCAmelCase      ( self):
          return (4, 4, 32, 32)







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__				   =      {
              "block_out_channels": (32, 64),
              "down_block_types": (
                  "CrossAttnDownBlock3D",
                  "DownBlock3D",
              ),
              "up_block_types": ("UpBlock3D", "CrossAttnUpBlock3D"),
              "cross_attention_dim": 32,
              "attention_head_dim": 8,
              "out_channels": 4,
              "in_channels": 4,
              "layers_per_block": 1,
              "sample_size": 32,
          }
          lowerCamelCase__				   =      self.dummy_input
          return init_dict, inputs_dict







   @unittest.skipIf(
       torch_device != "cuda" or not is_xformers_available()			,   reason="XFormers attention is only available with CUDA and `xformers` installed"			,   )
   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)

          model.enable_xformers_memory_efficient_attention()

          assert (
              model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
              == "XFormersAttnProcessor"
          ), "xformers is not enabled"







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      32

          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)
          model.eval()

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)

                 if isinstance(UpperCamelCase			,   UpperCamelCase):
                        lowerCamelCase__				   =      output.sample

          self.assertIsNotNone(UpperCamelCase)
          lowerCamelCase__				   =      inputs_dict["sample"].shape
          self.assertEqual(output.shape			,   UpperCamelCase			,   "Input and output shapes do not match")







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)
          model.eval()

          with torch.no_grad():
                 # Warmup pass when using mps (see #372)
                 if torch_device == "mps" and isinstance(UpperCamelCase			,   UpperCamelCase):
                        model(**self.dummy_input)

                 lowerCamelCase__				   =      model(**UpperCamelCase)
                 if isinstance(UpperCamelCase			,   UpperCamelCase):
                        lowerCamelCase__				   =      first.sample

                 lowerCamelCase__				   =      model(**UpperCamelCase)
                 if isinstance(UpperCamelCase			,   UpperCamelCase):
                        lowerCamelCase__				   =      second.sample

          lowerCamelCase__				   =      first.cpu().numpy()
          lowerCamelCase__				   =      second.cpu().numpy()
          lowerCamelCase__				   =      out_a[~np.isnan(UpperCamelCase)]
          lowerCamelCase__				   =      out_a[~np.isnan(UpperCamelCase)]
          lowerCamelCase__				   =      np.amax(np.abs(out_a - out_a))
          self.assertLessEqual(UpperCamelCase			,   1E-5)







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)
          model.eval()

          model.set_attention_slice("auto")
          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)
          assert output is not None

          model.set_attention_slice("max")
          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)
          assert output is not None

          model.set_attention_slice(2)
          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)
          assert output is not None







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase)

          # make sure we can set a list of attention processors
          model.set_attn_processor(UpperCamelCase)
          model.to(UpperCamelCase)

          # test that attn processors can be set to itself
          model.set_attn_processor(model.attn_processors)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.0}).sample
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample

          assert (samplea - samplea).abs().max() < 3E-3
          assert (samplea - samplea).abs().max() < 3E-3

          # sample 2 and sample 3 should be different
          assert (samplea - samplea).abs().max() > 3E-3







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample

          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(UpperCamelCase)
                 self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase			,   "pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
                 new_model.to(UpperCamelCase)
                 new_model.load_attn_procs(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      new_model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample

          assert (sample - new_sample).abs().max() < 1E-3

          # LoRA and no LoRA should NOT be the same
          assert (sample - old_sample).abs().max() > 1E-4







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample

          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(UpperCamelCase			,   safe_serialization=UpperCamelCase)
                 self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase			,   "pytorch_lora_weights.safetensors")))
                 torch.manual_seed(0)
                 lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
                 new_model.to(UpperCamelCase)
                 new_model.load_attn_procs(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      new_model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.5}).sample

          assert (sample - new_sample).abs().max() < 3E-3

          # LoRA and no LoRA should NOT be the same
          assert (sample - old_sample).abs().max() > 1E-4







   def     __UpperCAmelCase      ( self):
          # enable deterministic behavior for gradient checkpointing
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase			,   mock_weights=UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)
          # Saving as torch, properly reloads with directly filename
          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(UpperCamelCase)
                 self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase			,   "pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
                 new_model.to(UpperCamelCase)
                 new_model.load_attn_procs(UpperCamelCase			,   weight_name="pytorch_lora_weights.bin")







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase			,   mock_weights=UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)
          # Saving as torch, properly reloads with directly filename
          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_attn_procs(UpperCamelCase)
                 self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase			,   "pytorch_lora_weights.bin")))
                 torch.manual_seed(0)
                 lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
                 new_model.to(UpperCamelCase)
                 with self.assertRaises(UpperCamelCase) as e:
                        new_model.load_attn_procs(UpperCamelCase			,   use_safetensors=UpperCamelCase)
                 self.assertIn("Error no file named pytorch_lora_weights.safetensors"			,   str(e.exception))







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      8

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase			,   cross_attention_kwargs={"scale": 0.0}).sample

          model.set_attn_processor(AttnProcessor())

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          assert (sample - new_sample).abs().max() < 1E-4
          assert (sample - old_sample).abs().max() < 3E-3







   @unittest.skipIf(
       torch_device != "cuda" or not is_xformers_available()			,   reason="XFormers attention is only available with CUDA and `xformers` installed"			,   )
   def     __UpperCAmelCase      ( self):
          # enable deterministic behavior for gradient checkpointing
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()

          lowerCamelCase__				   =      4

          torch.manual_seed(0)
          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)
          lowerCamelCase__				   =      create_lora_layers(UpperCamelCase)
          model.set_attn_processor(UpperCamelCase)

          # default
          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

                 model.enable_xformers_memory_efficient_attention()
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

                 model.disable_xformers_memory_efficient_attention()
                 lowerCamelCase__				   =      model(**UpperCamelCase).sample

          assert (sample - on_sample).abs().max() < 1E-4
          assert (sample - off_sample).abs().max() < 1E-4







   def     __UpperCAmelCase      ( self):
          lowerCamelCase__   ,		lowerCamelCase__				   =      self.prepare_init_args_and_inputs_for_common()
          lowerCamelCase__				   =      32

          lowerCamelCase__				   =      self.model_class(**UpperCamelCase)
          model.to(UpperCamelCase)
          model.eval()

          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)[0]

          model.enable_forward_chunking()
          with torch.no_grad():
                 lowerCamelCase__				   =      model(**UpperCamelCase)[0]

          self.assertEqual(output.shape			,   output_a.shape			,   "Shape doesn't match")
          assert np.abs(output.cpu() - output_a.cpu()).max() < 1E-2


# (todo: sayakpaul) implement SLOW tests.

