

'''simple docstring'''







import gc
import unittest

from diffusers import FlaxStableDiffusionInpaintPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
      import jax
      import jax.numpy as jnp
      from flax.jax_utils import replicate
      from flax.training.common_utils import shard


@slow
@require_flax
class 					A__      (      unittest.TestCase			):



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





           '''simple docstring'''
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()







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





           '''simple docstring'''
           A_						=      load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/sd2-inpaint/init_image.png"""       )
           A_						=      load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""       )

           A_						=      """xvjiarui/stable-diffusion-2-inpainting"""
           A_					,			A_						=      FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase__		,				safety_checker=UpperCAmelCase__       )

           A_						=      """Face of a yellow cat, high resolution, sitting on a park bench"""

           A_						=      jax.random.PRNGKey(0       )
           A_						=      50

           A_						=      jax.device_count()
           A_						=      num_samples * [prompt]
           A_						=      num_samples * [init_image]
           A_						=      num_samples * [mask_image]
           A_					,			A_					,			A_						=      pipeline.prepare_inputs(UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__       )

           # shard inputs and rng
           A_						=      replicate(UpperCAmelCase__       )
           A_						=      jax.random.split(UpperCAmelCase__		,				jax.device_count()       )
           A_						=      shard(UpperCAmelCase__       )
           A_						=      shard(UpperCAmelCase__       )
           A_						=      shard(UpperCAmelCase__       )

           A_						=      pipeline(
               UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				jit=UpperCAmelCase__       )

           A_						=      output.images.reshape(UpperCAmelCase__		,				512		,				512		,				3       )

           A_						=      images[0, 253:256, 253:256, -1]

           A_						=      jnp.asarray(jax.device_get(image_slice.flatten()       )       )
           A_						=      jnp.array(
               [0.3611307, 0.37649736, 0.3757408, 0.38213953, 0.39295167, 0.3841631, 0.41554978, 0.4137475, 0.4217084]       )
           print(f'''output_slice: {output_slice}'''       )
           assert jnp.abs(output_slice - expected_slice       ).max() < 1e-2



'''simple docstring'''







__lowerCamelCase		       =    range(2, 20 + 1)
__lowerCamelCase		       =    [10**k for k in range(ks[-1] + 1)]
__lowerCamelCase		       =    {}
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)			->			Tuple:
       A_						=      sum(a_i[j] for j in range(UpperCAmelCase__,       len(UpperCAmelCase__				)				)				)
       A_						=      sum(a_i[j] * base[j] for j in range(min(len(UpperCAmelCase__				),       UpperCAmelCase__				)				)				)

       A_					,			A_						=      0, 0
       A_						=      n - i

       A_						=      memo.get(UpperCAmelCase__				)

       if sub_memo is not None:
              A_						=      sub_memo.get(UpperCAmelCase__				)

              if jumps is not None and len(UpperCAmelCase__				) > 0:
                     # find and make the largest jump without going over
                     A_						=      -1
                     for _k in range(len(UpperCAmelCase__				) - 1,       -1,       -1				):
                            if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
                                   A_						=      _k
                                   break

                     if max_jump >= 0:
                            A_					,			A_					,			A_						=      jumps[max_jump]
                            # since the difference between jumps is cached, add c
                            A_						=      diff + c
                            for j in range(min(UpperCAmelCase__,       len(UpperCAmelCase__				)				)				):
                                   A_					,			A_						=      divmod(UpperCAmelCase__,       10				)
                            if new_c > 0:
                                   add(UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)

              else:
                     A_						=      []
       else:
              A_						=      {c: []}
              A_						=      sub_memo

       if dn >= max_dn or c + diff >= base[k]:
              return diff, dn

       if k > ks[0]:
              while True:
                     # keep doing smaller jumps
                     A_					,			A_						=      next_term(UpperCAmelCase__,       k - 1,       i + dn,       UpperCAmelCase__				)
                     diff += _diff
                     dn += terms_jumped

                     if dn >= max_dn or c + diff >= base[k]:
                            break
       else:
              # would be too small a jump, just compute sequential terms instead
              A_					,			A_						=      compute(UpperCAmelCase__,       UpperCAmelCase__,       i + dn,       UpperCAmelCase__				)
              diff += _diff
              dn += terms_jumped

       A_						=      sub_memo[c]

       # keep jumps sorted by # of terms skipped
       A_						=      0
       while j < len(UpperCAmelCase__				):
              if jumps[j][1] > dn:
                     break
              j += 1

       # cache the jump for this value digitsum(b) and c
       sub_memo[c].insert(UpperCAmelCase__,       (diff, dn, k)				)
       return (diff, dn)
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)			->			int:
       if i >= n:
              return 0, i
       if k > len(UpperCAmelCase__				):
              a_i.extend([0 for _ in range(k - len(UpperCAmelCase__				)				)]				)

       # note: a_i -> b * 10^k + c
       # ds_b -> digitsum(b)
       # ds_c -> digitsum(c)
       A_						=      i
       A_					,			A_					,			A_						=      0, 0, 0
       for j in range(len(UpperCAmelCase__				)				):
              if j >= k:
                     ds_b += a_i[j]
              else:
                     ds_c += a_i[j]

       while i < n:
              i += 1
              A_						=      ds_c + ds_b
              diff += addend
              A_						=      0
              for j in range(UpperCAmelCase__				):
                     A_						=      a_i[j] + addend
                     A_					,			A_						=      divmod(UpperCAmelCase__,       10				)

                     ds_c += a_i[j]

              if addend > 0:
                     break

       if addend > 0:
              add(UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)
       return diff, i - start_i
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__				)			->			str:
       for j in range(UpperCAmelCase__,       len(UpperCAmelCase__				)				):
              A_						=      digits[j] + addend
              if s >= 10:
                     A_					,			A_						=      divmod(UpperCAmelCase__,       10				)
                     A_						=      addend // 10 + quotient
              else:
                     A_						=      s
                     A_						=      addend // 10

              if addend == 0:
                     break

       while addend > 0:
              A_					,			A_						=      divmod(UpperCAmelCase__,       10				)
              digits.append(UpperCAmelCase__				)
def       UpperCAmelCase__    (      UpperCAmelCase__ = 10**15				)			->			int:
       A_						=      [1]
       A_						=      1
       A_						=      0
       while True:
              A_					,			A_						=      next_term(UpperCAmelCase__,       20,       i + dn,       UpperCAmelCase__				)
              dn += terms_jumped
              if dn == n - i:
                     break

       A_						=      0
       for j in range(len(UpperCAmelCase__				)				):
              a_n += digits[j] * 10**j
       return a_n


if __name__ == "__main__":
      print(f"""{solution() = }""")
