code: def _generate_uids(
      num_experts: int,
      expert_pattern: Optional[str],
      dht: Optional[DHT] = None,
      attempts_per_expert=10,
  ) -> List[str]:
      """
      Sample experts from a given pattern, remove duplicates.
      :param num_experts: sample this many unique expert uids
      :param expert_pattern: a string pattern or a list of expert uids,  example: myprefix.[0:32].[0:256]\
       means "sample random experts between myprefix.0.0 and myprefix.255.255;
      :param dht: if specified, uses this DHT to check that expert uids are not yet occupied by other peers
      :param attempts_per_expert: give up if unable to generate a new expert uid after this many attempts per uid
      :note: this method is not strictly process-safe. If several servers run it concurrently, they have
       a small chance of sampling duplicate expert uids.
      """
      remaining_attempts = attempts_per_expert * num_experts
      found_uids, attempted_uids = list(), set()
  
      def _generate_uid():
          if expert_pattern is None:
              return f"expert{UID_DELIMITER}{attempts_per_expert * num_experts - remaining_attempts}"
  
          uid = []
          for block in expert_pattern.split(UID_DELIMITER):
              try:
                  if "[" not in block and "]" not in block:
                      uid.append(block)
                  elif (
                      block.startswith("[")
                      and block.endswith("]")
                      and ":" in block
                  ):
                      slice_start, slice_end = map(
                          int, block[1:-1].split(":")
                      )
                      uid.append(
                          str(
                              random.randint(slice_start, slice_end - 1)
                          )
                      )
                  else:
                      raise ValueError(
                          "Block must be either fixed or a range [from:to]"
                      )
              except KeyboardInterrupt:
                  raise
              except Exception as e:
                  raise ValueError(
                      f"Expert pattern {expert_pattern} has invalid block {block}, {e}"
                  )
          return UID_DELIMITER.join(uid)
  
      while remaining_attempts > 0 and len(found_uids) < num_experts:
          # 1. sample new expert uids at random
          new_uids = []
          while (
              len(new_uids) + len(found_uids) < num_experts
              and remaining_attempts > 0
          ):
              new_uid = _generate_uid()
              remaining_attempts -= 1
              if new_uid not in attempted_uids:
                  attempted_uids.add(new_uid)
                  new_uids.append(new_uid)
  
          # 2. look into DHT (if given) and remove duplicates
          if dht is not None:
              existing_expert_uids = {
                  found_expert.uid
                  for found_expert in get_experts(dht, new_uids)
                  if found_expert is not None
              }
              new_uids = [
                  new_uid
                  for new_uid in new_uids
                  if new_uid not in existing_expert_uids
              ]
  
          found_uids += new_uids
  
      if len(found_uids) != num_experts:
          logger.warning(
              f"Found only {len(found_uids)} out of {num_experts} free expert uids after "
              f"{attempts_per_expert * num_experts} attempts"
          )
      return found_uids
  
imports: from __future__ import annotations
  import multiprocessing
  import random
  import threading
  from contextlib import contextmanager
  from functools import partial
  from pathlib import Path
  from typing import Dict, List, Optional
  import torch
  from hivemind.dht import DHT
  from hivemind.moe.expert_uid import UID_DELIMITER
  from hivemind.moe.server.checkpoints import CheckpointSaver, is_directory, load_experts
  from hivemind.moe.server.connection_handler import ConnectionHandler
  from hivemind.moe.server.dht_handler import DHTHandlerThread, get_experts
  from hivemind.moe.server.layers import add_custom_models_from_file, name_to_block, name_to_input, schedule_name_to_scheduler
  from hivemind.moe.server.layers.optim import ClippingWrapper
  from hivemind.moe.server.module_backend import ModuleBackend
  from hivemind.moe.server.runtime import Runtime
  from hivemind.p2p import PeerInfo
  from hivemind.proto.runtime_pb2 import CompressionType
  from hivemind.utils.logging import get_logger
  from hivemind.utils.tensor_descr import DUMMY_BATCH_SIZE, BatchTensorDescriptor
label_desc: Use image node_id 3 for calling a global function with example usage: _generate_uids(num_experts, expert_pattern, dht, attempts_per_expert) and returns: found_uids, UID_DELIMITER, str, UID_DELIMITER, unknown