

import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor


class     lowerCAmelCase				(    unittest.TestCase				):


       '''simple docstring'''







       def   lowerCamelCase__						(	self					:  Optional[Any]						,	__snake_case					:  bool = False     )   -> Optional[Any]:







        '''simple docstring'''

        lowerCamelCase      						=    10

        if only_cross_attention:
         lowerCamelCase      						=    12
        else:
         # when only cross attention is not set, the cross attention dim must be the same as the query dim
         lowerCamelCase      						=    query_dim

        return {
            "query_dim": query_dim,
            "cross_attention_dim": cross_attention_dim,
            "heads": 2,
            "dim_head": 4,
            "added_kv_proj_dim": 6,
            "norm_num_groups": 1,
            "only_cross_attention": only_cross_attention,
            "processor": AttnAddedKVProcessor(),
        }

       def   lowerCamelCase__						(	self					:  str						,	__snake_case					:  List[str]						,	__snake_case					:  Any     )   -> List[str]:







        '''simple docstring'''

        lowerCamelCase      						=    2

        lowerCamelCase      						=    torch.rand(__snake_case						,	__snake_case						,	3						,	2     )
        lowerCamelCase      						=    torch.rand(__snake_case						,	4						,	__snake_case     )
        lowerCamelCase      						=    None

        return {
            "hidden_states": hidden_states,
            "encoder_hidden_states": encoder_hidden_states,
            "attention_mask": attention_mask,
        }





       def   lowerCamelCase__						(	self					:  Dict     )   -> int:







        '''simple docstring'''

        torch.manual_seed(0     )

        lowerCamelCase      						=    self.get_constructor_arguments(only_cross_attention=__snake_case     )
        lowerCamelCase      						=    Attention(**__snake_case     )

        self.assertTrue(attn.to_k is not None     )
        self.assertTrue(attn.to_v is not None     )

        lowerCamelCase      						=    self.get_forward_arguments(
            query_dim=constructor_args['query_dim']						,	added_kv_proj_dim=constructor_args['added_kv_proj_dim']     )

        lowerCamelCase      						=    attn(**__snake_case     )

        # only self attention

        torch.manual_seed(0     )

        lowerCamelCase      						=    self.get_constructor_arguments(only_cross_attention=__snake_case     )
        lowerCamelCase      						=    Attention(**__snake_case     )

        self.assertTrue(attn.to_k is None     )
        self.assertTrue(attn.to_v is None     )

        lowerCamelCase      						=    self.get_forward_arguments(
            query_dim=constructor_args['query_dim']						,	added_kv_proj_dim=constructor_args['added_kv_proj_dim']     )

        lowerCamelCase      						=    attn(**__snake_case     )

        self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()     )


class     lowerCAmelCase				(    unittest.TestCase				):


       '''simple docstring'''







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







        '''simple docstring'''

        lowerCamelCase      						=    DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'						,	safety_checker=__snake_case     )

        lowerCamelCase      						=    pipe(
            'foo'						,	num_inference_steps=2						,	generator=torch.Generator('cpu'     ).manual_seed(0     )						,	output_type='np'						,	).images

        # the initial conversion succeeds
        lowerCamelCase      						=    DiffusionPipeline.from_pretrained(
            'hf-internal-testing/tiny-stable-diffusion-pipe'						,	device_map='sequential'						,	safety_checker=__snake_case     )

        lowerCamelCase      						=    pipe(
            'foo'						,	num_inference_steps=2						,	generator=torch.Generator('cpu'     ).manual_seed(0     )						,	output_type='np'						,	).images

        with tempfile.TemporaryDirectory() as tmpdir:
         # save the converted model
         pipe.save_pretrained(__snake_case     )

         # can also load the converted weights
         lowerCamelCase      						=    DiffusionPipeline.from_pretrained(__snake_case						,	device_map='sequential'						,	safety_checker=__snake_case     )

        lowerCamelCase      						=    pipe(
            'foo'						,	num_inference_steps=2						,	generator=torch.Generator('cpu'     ).manual_seed(0     )						,	output_type='np'						,	).images

        self.assertTrue(np.allclose(__snake_case						,	__snake_case						,	atol=1e-5     )     )
        self.assertTrue(np.allclose(__snake_case						,	__snake_case						,	atol=1e-5     )     )







from __future__ import annotations


class     lowerCAmelCase				:


       '''simple docstring'''







       def __init__(	self					:  Dict						,	__snake_case					:  int     )   -> None:







        '''simple docstring'''

        lowerCamelCase      						=    vertices
        lowerCamelCase      						=    [[0] * vertices for _ in range(__snake_case     )]

       def   lowerCamelCase__						(	self					:  List[str]						,	__snake_case					:  list[int]     )   -> None:







        '''simple docstring'''

        print('Vertex \t Distance from Source'     )
        for vertex in range(self.vertices     ):
         print(__snake_case						,	'\t\t'						,	distances_from_source[vertex]     )

       def   lowerCamelCase__						(	self					:  Optional[int]						,	__snake_case					:  list[int]						,	__snake_case					:  list[bool]     )   -> int:







        '''simple docstring'''

        lowerCamelCase      						=    1e7
        lowerCamelCase      						=    0

        # Search not nearest vertex not in the shortest path tree
        for vertex in range(self.vertices     ):
         if distances_from_source[vertex] < minimum and visited[vertex] is False:
          lowerCamelCase      						=    distances_from_source[vertex]
          lowerCamelCase      						=    vertex
        return min_index





       def   lowerCamelCase__						(	self					:  Optional[Any]						,	__snake_case					:  int     )   -> None:







        '''simple docstring'''

        lowerCamelCase      						=    [int(1e7     )] * self.vertices  # distances from the source
        lowerCamelCase      						=    0
        lowerCamelCase      						=    [False] * self.vertices

        for _ in range(self.vertices     ):
         lowerCamelCase      						=    self.minimum_distance(__snake_case						,	__snake_case     )
         lowerCamelCase      						=    True

         # Update dist value of the adjacent vertices
         # of the picked vertex only if the current
         # distance is greater than new distance and
         # the vertex in not in the shortest path tree
         for v in range(self.vertices     ):
          if (
              self.graph[u][v] > 0
              and visited[v] is False
              and distances[v] > distances[u] + self.graph[u][v]
          ):
           lowerCamelCase      						=    distances[u] + self.graph[u][v]

        self.print_solution(__snake_case     )


if __name__ == "__main__":
     _lowerCAmelCase :	Dict     				=    Graph(9)
     _lowerCAmelCase :	int     				=    [
         [0, 4, 0, 0, 0, 0, 0, 8, 0],
         [4, 0, 8, 0, 0, 0, 0, 1_1, 0],
         [0, 8, 0, 7, 0, 4, 0, 0, 2],
         [0, 0, 7, 0, 9, 1_4, 0, 0, 0],
         [0, 0, 0, 9, 0, 1_0, 0, 0, 0],
         [0, 0, 4, 1_4, 1_0, 0, 2, 0, 0],
         [0, 0, 0, 0, 0, 2, 0, 1, 6],
         [8, 1_1, 0, 0, 0, 0, 1, 0, 7],
         [0, 0, 2, 0, 0, 0, 6, 7, 0],
     ]
     graph.dijkstra(0)




