code: 
  
  
  
  
  """simple docstring"""
  
  import os
  from typing import Optional, Union
  
  import numpy as np
  from huggingface_hub import hf_hub_download
  
  from ... import AutoTokenizer
  from ...utils import logging
  
  
  __A 		=			"block_records.npy"
  
  
  __A 		=			logging.get_logger(__name__)
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     str       ,     lowercase__  :     int   ):
  		import tensorflow.compat.va as tf
  
  		snake_case		:						Union[str, Any]								=				tf.data.TFRecordDataset(lowercase__       ,     buffer_size=512 * 1024 * 1024   )
  		snake_case		:						Any								=				blocks_dataset.batch(lowercase__       ,     drop_remainder=lowercase__   )
  		snake_case		:						Optional[int]								=				next(blocks_dataset.take(1   ).as_numpy_iterator()   )
  
  		return np_record
  class 			lowerCamelCase__    :
  
  
  			def __init__(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=2				, SCREAMING_SNAKE_CASE=1_000				, SCREAMING_SNAKE_CASE=100				, SCREAMING_SNAKE_CASE=100_000				, ):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					from scann.scann_ops.py.scann_ops_pybind import builder as Builder
  
  					snake_case		:						Any								=				Builder(db=SCREAMING_SNAKE_CASE				, num_neighbors=SCREAMING_SNAKE_CASE				, distance_measure="dot_product"							)
  					snake_case		:						Any								=				builder.tree(
  					    num_leaves=SCREAMING_SNAKE_CASE				, num_leaves_to_search=SCREAMING_SNAKE_CASE				, training_sample_size=SCREAMING_SNAKE_CASE							)
  					snake_case		:						Dict								=				builder.score_ah(dimensions_per_block=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Union[str, Any]								=				builder.build()
  
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						int								=				self.searcher.search_batched(question_projection.detach().cpu()							)
  					return retrieved_block_ids.astype("int64"							)
  
  
  
  
  
  
  
  class 			lowerCamelCase__    :
  
  
  			def __init__(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					super().__init__()
  					snake_case		:						Any								=				block_records
  					snake_case		:						str								=				tokenizer
  
  
  			def __call__(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=None				, SCREAMING_SNAKE_CASE="pt"							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[int]								=				np.take(self.block_records				, indices=SCREAMING_SNAKE_CASE				, axis=0							)
  
  					snake_case		:						Dict								=				self.tokenizer.decode(question_input_ids[0]				, skip_special_tokens=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						List[str]								=				[]
  					snake_case		:						Optional[Any]								=				[]
  					for retrieved_block in retrieved_blocks:
  							text.append(SCREAMING_SNAKE_CASE							)
  							text_pair.append(retrieved_block.decode()							)
  
  					snake_case		:						Union[str, Any]								=				self.tokenizer(
  					    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, padding=SCREAMING_SNAKE_CASE				, truncation=SCREAMING_SNAKE_CASE				, return_special_tokens_mask=SCREAMING_SNAKE_CASE				, max_length=SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[int]								=				concat_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE							)
  
  					if answer_ids is not None:
  							return self.block_has_answer(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							) + (concat_inputs_tensors,)
  					else:
  							return (None, None, None, concat_inputs_tensors)
  
  
  			@classmethod
  			def 							lowerCamelCase_				(     cls				, SCREAMING_SNAKE_CASE				, *SCREAMING_SNAKE_CASE				, **SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if os.path.isdir(SCREAMING_SNAKE_CASE							):
  							snake_case		:						Optional[Any]								=				os.path.join(SCREAMING_SNAKE_CASE				, _REALM_BLOCK_RECORDS_FILENAME							)
  					else:
  							snake_case		:						str								=				hf_hub_download(
  							    repo_id=SCREAMING_SNAKE_CASE				, filename=_REALM_BLOCK_RECORDS_FILENAME				, **SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[int]								=				np.load(SCREAMING_SNAKE_CASE				, allow_pickle=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Union[str, Any]								=				AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE				, *SCREAMING_SNAKE_CASE				, **SCREAMING_SNAKE_CASE							)
  
  					return cls(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					np.save(os.path.join(SCREAMING_SNAKE_CASE				, _REALM_BLOCK_RECORDS_FILENAME							)				, self.block_records							)
  					# save tokenizer
  					self.tokenizer.save_pretrained(SCREAMING_SNAKE_CASE							)
  
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Any								=				[]
  					snake_case		:						str								=				[]
  					snake_case		:						Any								=				[]
  					snake_case		:						List[Any]								=				0
  
  					for input_id in concat_inputs.input_ids:
  							snake_case		:						Union[str, Any]								=				input_id.tolist()
  							# Check answers between two [SEP] tokens
  							snake_case		:						Union[str, Any]								=				input_id_list.index(self.tokenizer.sep_token_id							)
  							snake_case		:						Optional[int]								=				first_sep_idx + 1 + input_id_list[first_sep_idx + 1 :].index(self.tokenizer.sep_token_id							)
  
  							start_pos.append([]							)
  							end_pos.append([]							)
  							for answer in answer_ids:
  									for idx in range(first_sep_idx + 1				, SCREAMING_SNAKE_CASE							):
  											if answer[0] == input_id_list[idx]:
  													if input_id_list[idx : idx + len(SCREAMING_SNAKE_CASE							)] == answer:
  															start_pos[-1].append(SCREAMING_SNAKE_CASE							)
  															end_pos[-1].append(idx + len(SCREAMING_SNAKE_CASE							) - 1							)
  
  							if len(start_pos[-1]							) == 0:
  									has_answers.append(SCREAMING_SNAKE_CASE							)
  							else:
  									has_answers.append(SCREAMING_SNAKE_CASE							)
  									if len(start_pos[-1]							) > max_answers:
  											snake_case		:						List[str]								=				len(start_pos[-1]							)
  
          # Pad -1 to max_answers
  					for start_pos_, end_pos_ in zip(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							):
  							if len(start_pos_							) < max_answers:
  									snake_case		:						List[str]								=				[-1] * (max_answers - len(start_pos_							))
  									start_pos_ += padded
  									end_pos_ += padded
  					return has_answers, start_pos, end_pos
  
style_context: 
  
  
  
  
  """simple docstring"""
  
  import gc
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import RePaintPipeline, RePaintScheduler, UNetaDModel
  from diffusers.utils.testing_utils import (
      enable_full_determinism,
      load_image,
      load_numpy,
      nightly,
      require_torch_gpu,
      skip_mps,
      torch_device,
  )
  
  from ..pipeline_params import IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_INPAINTING_PARAMS
  from ..test_pipelines_common import PipelineTesterMixin
  
  
  enable_full_determinism()
  class 			lowerCamelCase__    (  lowerCamelCase_				,  unittest.TestCase			):
  			a__ :      Optional[Any]								=     RePaintPipeline
  			a__ :      List[Any]								=     IMAGE_INPAINTING_PARAMS - {"""width""", """height""", """guidance_scale"""}
  			a__ :      List[Any]								=     PipelineTesterMixin.required_optional_params - {
  			    """latents""",
  			    """num_images_per_prompt""",
  			    """callback""",
  			    """callback_steps""",
  			}
  			a__ :      List[Any]								=     IMAGE_INPAINTING_BATCH_PARAMS
  			a__ :      List[str]								=     False
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					torch.manual_seed(0							)
  					torch.manual_seed(0							)
  					snake_case		:						Union[str, Any]								=				UNetaDModel(
  					    block_out_channels=(32, 64)				, layers_per_block=2				, sample_size=32				, in_channels=3				, out_channels=3				, down_block_types=("DownBlock2D", "AttnDownBlock2D")				, up_block_types=("AttnUpBlock2D", "UpBlock2D")				, )
  					snake_case		:						Union[str, Any]								=				RePaintScheduler()
  					snake_case		:						Optional[Any]								=				{"unet": unet, "scheduler": scheduler}
  					return components
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE=0							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if str(SCREAMING_SNAKE_CASE							).startswith("mps"							):
  							snake_case		:						str								=				torch.manual_seed(SCREAMING_SNAKE_CASE							)
  					else:
  							snake_case		:						List[Any]								=				torch.Generator(device=SCREAMING_SNAKE_CASE							).manual_seed(SCREAMING_SNAKE_CASE							)
  					snake_case		:						Any								=				np.random.RandomState(SCREAMING_SNAKE_CASE							).standard_normal((1, 3, 32, 32)							)
  					snake_case		:						Any								=				torch.from_numpy(SCREAMING_SNAKE_CASE							).to(device=SCREAMING_SNAKE_CASE				, dtype=torch.floataa							)
  					snake_case		:						List[str]								=				(image > 0).to(device=SCREAMING_SNAKE_CASE				, dtype=torch.floataa							)
  					snake_case		:						int								=				{
  					    "image": image,
  					    "mask_image": mask,
  					    "generator": generator,
  					    "num_inference_steps": 5,
  					    "eta": 0.0,
  					    "jump_length": 2,
  					    "jump_n_sample": 2,
  					    "output_type": "numpy",
  					}
  					return inputs
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						List[Any]								=				"cpu"  # ensure determinism for the device-dependent torch.Generator
  					snake_case		:						Any								=				self.get_dummy_components()
  					snake_case		:						Optional[int]								=				RePaintPipeline(**SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[Any]								=				sd_pipe.to(SCREAMING_SNAKE_CASE							)
  					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Any								=				self.get_dummy_inputs(SCREAMING_SNAKE_CASE							)
  					snake_case		:						Dict								=				sd_pipe(**SCREAMING_SNAKE_CASE							).images
  					snake_case		:						Optional[int]								=				image[0, -3:, -3:, -1]
  
  					assert image.shape == (1, 32, 32, 3)
  					snake_case		:						List[str]								=				np.array([1.00_00, 0.54_26, 0.54_97, 0.22_00, 1.00_00, 1.00_00, 0.56_23, 1.00_00, 0.62_74]							)
  
  					assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-3
  
  
  			@skip_mps
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return super().test_save_load_local()
  
  
  			@unittest.skip("non-deterministic pipeline"							)
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return super().test_inference_batch_single_identical()
  
  
  			@skip_mps
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return super().test_dict_tuple_outputs_equivalent()
  
  
  			@skip_mps
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return super().test_save_load_optional_components()
  
  
  
  			@skip_mps
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return super().test_attention_slicing_forward_pass()
  
  
  
  
  
  
  
  @nightly
  @require_torch_gpu
  class 			lowerCamelCase__    (  unittest.TestCase			):
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					super().tearDown()
  					gc.collect()
  					torch.cuda.empty_cache()
  
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[int]								=				load_image(
  					    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/"
  					    "repaint/celeba_hq_256.png"							)
  					snake_case		:						Any								=				load_image(
  					    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png"							)
  					snake_case		:						List[Any]								=				load_numpy(
  					    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/"
  					    "repaint/celeba_hq_256_result.npy"							)
  
  					snake_case		:						str								=				"google/ddpm-ema-celebahq-256"
  					snake_case		:						Optional[Any]								=				UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[int]								=				RePaintScheduler.from_pretrained(SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						str								=				RePaintPipeline(unet=SCREAMING_SNAKE_CASE				, scheduler=SCREAMING_SNAKE_CASE							).to(SCREAMING_SNAKE_CASE							)
  					repaint.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE							)
  					repaint.enable_attention_slicing()
  
  					snake_case		:						Union[str, Any]								=				torch.manual_seed(0							)
  					snake_case		:						int								=				repaint(
  					    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, num_inference_steps=250				, eta=0.0				, jump_length=10				, jump_n_sample=10				, generator=SCREAMING_SNAKE_CASE				, output_type="np"				, )
  					snake_case		:						List[Any]								=				output.images[0]
  
  					assert image.shape == (256, 256, 3)
  					assert np.abs(expected_image - image							).mean() < 1E-2
  
  