code: def _merge_metadata(
      merged: Dict[str, Any],
      rank_metadata: Dict[str, Any],
      rank: int,
      key_ranks: Dict[str, Any],
      key_conflicts: Dict[str, List[int]],
      prev_key: str,
  ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
      for key, metadata in rank_metadata.items():
          # First, update key_ranks. We will use it later
          if key not in key_ranks:
              key_ranks[key] = {"_ranks_": []}
          key_ranks[key]["_ranks_"].append(rank)
  
          full_key = f"{prev_key}/{key}"
  
          # Merge metadata. We consider two possibilities:
          # 1. metadata is dictionary AND :
          #   * if key is not in merged, then recursively unroll and merge metadata; OR
          #   * if key is in merged, and metadata and merged[key] are dictionaries, merge recursively.
          # 2. otherwise:
          #   * key is in merged, then updated key_conflicts and don't merge,
          #   * key is not in merged, then add key and metadata to merged.
          if (key not in merged and isinstance(metadata, dict)) or (
              key in merged
              and isinstance(metadata, dict)
              and isinstance(merged[key], dict)
          ):
              merged[key], key_ranks[key] = _merge_metadata(
                  {} if key not in merged else merged[key],
                  metadata,
                  rank,
                  key_ranks[key],
                  key_conflicts,
                  full_key,
              )
          else:
              if key in merged and merged[key] != metadata:
                  if full_key not in key_conflicts:
                      key_conflicts[full_key] = key_ranks[key][
                          "_ranks_"
                      ].copy()
                  else:
                      key_conflicts[full_key].append(rank)
              else:
                  merged[key] = metadata
  
      return merged, key_ranks
  
imports: import contextlib
  import datetime
  import enum
  import hashlib
  import json
  import logging
  import os
  import pathlib
  import uuid
  from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Tuple, Union
  from determined import core, tensorboard
  from determined.common import api, storage
  from determined.common.api import bindings
label_desc: Use image node_id 1 for calling a global function with example usage: _merge_metadata(merged, rank_metadata, rank, key_ranks, key_conflicts, prev_key) and returns: merged,  key_ranks