




from __future__ import annotations

from queue import Queue

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int    ,    __UpperCAmelCase		:			int							)      ->				tuple[int, int]:
      a ^= b
      b ^= a
      a ^= b
      return a, b

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int    ,    __UpperCAmelCase		:			list[list[int]]							)      ->				list[list[int]]:
      SCREAMING_SNAKE_CASE_					=    1
      while (1 << j) < max_node:
            for i in range(1    ,    max_node + 1							):
                  SCREAMING_SNAKE_CASE_					=    parent[j - 1][parent[j - 1][i]]
            j += 1
      return parent

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int    ,    __UpperCAmelCase		:			int    ,    __UpperCAmelCase		:			list[int]    ,    __UpperCAmelCase		:			list[list[int]]							)      ->				int:
      # u must be deeper in the tree than v
      if level[u] < level[v]:
            SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    swap(__UpperCAmelCase    ,    __UpperCAmelCase							)
      # making depth of u same as depth of v
      for i in range(18    ,    -1    ,    -1							):
            if level[u] - (1 << i) >= level[v]:
                  SCREAMING_SNAKE_CASE_					=    parent[i][u]
    # at the same depth if u==v that mean lca is found
      if u == v:
            return u
      # moving both nodes upwards till lca in found
      for i in range(18    ,    -1    ,    -1							):
            if parent[i][u] not in [0, parent[i][v]]:
                  SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    parent[i][u], parent[i][v]
    # returning longest common ancestor of u,v
      return parent[0][u]

def       UpperCAmelCase_							(   __UpperCAmelCase		:			list[int]    ,    __UpperCAmelCase		:			list[list[int]]    ,    __UpperCAmelCase		:			int    ,    __UpperCAmelCase		:			dict[int, list[int]]    ,    __UpperCAmelCase		:			int = 1    ,    )      ->				tuple[list[int], list[list[int]]]:
      SCREAMING_SNAKE_CASE_					=    0
      SCREAMING_SNAKE_CASE_					=    Queue(maxsize=__UpperCAmelCase							)
      q.put(__UpperCAmelCase							)
      while q.qsize() != 0:
            SCREAMING_SNAKE_CASE_					=    q.get()
            for v in graph[u]:
                  if level[v] == -1:
                        SCREAMING_SNAKE_CASE_					=    level[u] + 1
                        q.put(__UpperCAmelCase							)
                        SCREAMING_SNAKE_CASE_					=    u
      return level, parent

def       UpperCAmelCase_							(   )      ->				None:
      SCREAMING_SNAKE_CASE_					=    13
      # initializing with 0
      SCREAMING_SNAKE_CASE_					=    [[0 for _ in range(max_node + 10							)] for _ in range(20							)]
      # initializing with -1 which means every node is unvisited
      SCREAMING_SNAKE_CASE_					=    [-1 for _ in range(max_node + 10							)]
      SCREAMING_SNAKE_CASE_					=    {
          1: [2, 3, 4],
          2: [5],
          3: [6, 7],
          4: [8],
          5: [9, 10],
          6: [11],
          7: [],
          8: [12, 13],
          9: [],
          10: [],
          11: [],
          12: [],
          13: [],
      }
      SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    breadth_first_search(__UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    1							)
      SCREAMING_SNAKE_CASE_					=    create_sparse(__UpperCAmelCase    ,    __UpperCAmelCase							)
      print('LCA of node 1 and 3 is: '    ,    lowest_common_ancestor(1    ,    3    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)
      print('LCA of node 5 and 6 is: '    ,    lowest_common_ancestor(5    ,    6    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)
      print('LCA of node 7 and 11 is: '    ,    lowest_common_ancestor(7    ,    11    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)
      print('LCA of node 6 and 7 is: '    ,    lowest_common_ancestor(6    ,    7    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)
      print('LCA of node 4 and 12 is: '    ,    lowest_common_ancestor(4    ,    12    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)
      print('LCA of node 8 and 8 is: '    ,    lowest_common_ancestor(8    ,    8    ,    __UpperCAmelCase    ,    __UpperCAmelCase							)							)


if __name__ == "__main__":
   main()





import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


lowerCamelCase__				:     Optional[Any]			       =							get_logger(__name__)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			str    ,    __UpperCAmelCase		:			str							)      ->				List[str]:
      if text.startswith(__UpperCAmelCase							):
            return text[len(__UpperCAmelCase							) :]
      return text  # or whatever

def       UpperCAmelCase_							(   __UpperCAmelCase		:			Union[str, Any]							)      ->				List[Any]:
      return {remove_prefix(__UpperCAmelCase    ,    'model.'							): v for k, v in sd.items()}

def       UpperCAmelCase_							(   __UpperCAmelCase		:			List[Dict[str, torch.Tensor]]							)      ->				Dict:
      SCREAMING_SNAKE_CASE_					=    {}
      for k in state_dicts[0].keys():
            SCREAMING_SNAKE_CASE_					=    [sd[k] for sd in state_dicts]
            SCREAMING_SNAKE_CASE_					=    sum(__UpperCAmelCase							) / len(__UpperCAmelCase							)
            assert isinstance(__UpperCAmelCase    ,    torch.Tensor							)
            SCREAMING_SNAKE_CASE_					=    new_t
      return new_sd

def       UpperCAmelCase_							(   __UpperCAmelCase		:			str    ,    __UpperCAmelCase		:			str    ,    __UpperCAmelCase		:			str							)      ->				None:
      SCREAMING_SNAKE_CASE_					=    AutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase							)
      if os.path.isfile(__UpperCAmelCase							):
            SCREAMING_SNAKE_CASE_					=    [pl_ckpt_path]
      else:
            assert os.path.isdir(__UpperCAmelCase							)
            SCREAMING_SNAKE_CASE_					=    list(Path(__UpperCAmelCase							).glob('*.ckpt'							)							)
            assert ckpt_files, f"could not find any ckpt files inside the {pl_ckpt_path} directory"

      if len(__UpperCAmelCase							) > 1:
            logger.info(f"averaging the weights of {ckpt_files}"							)

      SCREAMING_SNAKE_CASE_					=    [sanitize(torch.load(__UpperCAmelCase    ,    map_location='cpu'							)['state_dict']							) for x in ckpt_files]
      SCREAMING_SNAKE_CASE_					=    average_state_dicts(__UpperCAmelCase							)

      SCREAMING_SNAKE_CASE_		,				SCREAMING_SNAKE_CASE_					=    hf_model.load_state_dict(__UpperCAmelCase    ,    strict=__UpperCAmelCase							)
      assert not missing, f"missing keys: {missing}"
      hf_model.save_pretrained(__UpperCAmelCase							)
      try:
            SCREAMING_SNAKE_CASE_					=    AutoTokenizer.from_pretrained(__UpperCAmelCase							)
            tok.save_pretrained(__UpperCAmelCase							)
      except Exception:
            pass
            # dont copy tokenizer if cant


if __name__ == "__main__":
   fire.Fire(convert_pl_to_hf)