class_docstr: A torch module that performs Mixture-of-Experts inference with a local gating function and multiple remote experts.
  Natively supports pytorch autograd.
  
  :note: By default, not all experts are guaranteed to perform forward pass. Moreover, not all of those who ran
   forward pass are guaranteed to perform backward pass. In the latter case, gradient will be averaged without
   the missing experts
  
  :param in_features: common input size for experts and gating function
  :param grid_size: dimensions that form expert uid (see below)
  :param uid_prefix: common prefix for all expert uids (must end with '.')
  :note: expert uid follows the pattern {uid_prefix}.{0...grid_size[0]}.{0...grid_size[1]}...{0...grid_size[-1]}
  :param dht: a DHT instance used to search for best experts
  :param k_best: average this many highest-scoring experts to compute activations
  :param k_min: make sure at least this many experts returned output (i.e. didn't fail)
  :param timeout_after_k_min: wait for this many seconds after k_min experts returned results.
   Any expert that didn't manage to return output after that delay is considered unavailable
  :param detect_anomalies: whether to check input/output tensors for NaN and infinity values
  :param allow_zero_outputs: whether to return zeros if no experts respond on forward pass
class_docstr_tok: ["A","torch","module","that","performs","Mixture-of-Experts","inference","with","a","local","gating","function","and","multiple","remote","experts",".","Natively","supports","pytorch","autograd",".",":","note",":","By","default",",","not","all","experts","are","guaranteed","to","perform","forward","pass",".","Moreover",",","not","all","of","those","who","ran","forward","pass","are","guaranteed","to","perform","backward","pass",".","In","the","latter","case",",","gradient","will","be","averaged","without","the","missing","experts",":","param","in_features",":","common","input","size","for","experts","and","gating","function",":","param","grid_size",":","dimensions","that","form","expert","uid","(","see","below",")",":","param","uid_prefix",":","common","prefix","for","all","expert","uids","(","must","end","with","'",".","'",")",":","note",":","expert","uid","follows","the","pattern","{","uid_prefix","}",".","{","0","...","grid_size","[","0","]","}",".","{","0","...","grid_size","[","1","]","}","...","{","0","...","grid_size","[","-1","]","}",":","param","dht",":","a","DHT","instance","used","to","search","for","best","experts",":","param","k_best",":","average","this","many","highest-scoring","experts","to","compute","activations",":","param","k_min",":","make","sure","at","least","this","many","experts","returned","output","(","i.e",".","did","n't","fail",")",":","param","timeout_after_k_min",":","wait","for","this","many","seconds","after","k_min","experts","returned","results",".","Any","expert","that","did","n't","manage","to","return","output","after","that","delay","is","considered","unavailable",":","param","detect_anomalies",":","whether","to","check","input\/output","tensors","for","NaN","and","infinity","values",":","param","allow_zero_outputs",":","whether","to","return","zeros","if","no","experts","respond","on","forward","pass"]
code: def __init__(
      self,
      *,
      in_features,
      grid_size: Tuple[int, ...],
      dht: DHT,
      uid_prefix: str,
      k_best: int,
      k_min: int = 1,
      forward_timeout: Optional[float] = None,
      timeout_after_k_min: Optional[float] = None,
      backward_k_min: int = 1,
      backward_timeout: Optional[float] = None,
      detect_anomalies: bool = False,
      allow_zero_outputs: bool = False,
      **dht_kwargs,
  ):
      super().__init__()
      self.dht = dht
      self.beam_search = MoEBeamSearcher(
          dht, uid_prefix, grid_size, **dht_kwargs
      )
      self.k_best, self.k_min, self.backward_k_min = (
          k_best,
          k_min,
          backward_k_min,
      )
      self.forward_timeout, self.backward_timeout = (
          forward_timeout,
          backward_timeout,
      )
      self.timeout_after_k_min = timeout_after_k_min
      self.detect_anomalies = detect_anomalies
      self.allow_zero_outputs = allow_zero_outputs
  
      # jointly predict logits for all grid dimensions
      self.proj = nn.Linear(
          in_features, self.beam_search.total_grid_size
      )
      self._expert_info = None
  
imports: from __future__ import annotations
  import time
  from concurrent.futures import Future
  from queue import Empty, Queue
  from typing import Any, Dict, List, Optional, Tuple
  import torch
  import torch.nn
  from torch.autograd.function import once_differentiable
  from hivemind.compression import serialize_torch_tensor
  from hivemind.dht import DHT
  from hivemind.moe.client.beam_search import MoEBeamSearcher
  from hivemind.moe.client.expert import DUMMY, RemoteExpert, expert_backward, expert_forward, get_server_stub
  from hivemind.moe.client.remote_expert_worker import RemoteExpertWorker
  from hivemind.moe.expert_uid import UID_DELIMITER
  from hivemind.p2p.p2p_daemon_bindings.control import P2PDaemonError
  from hivemind.utils import nested_flatten, nested_map, nested_pack
  from hivemind.utils.logging import get_logger
label_desc: Use image node_id 1 to create a new RemoteMixtureOfExperts object from inherited base classes: nn with example: obj = RemoteMixtureOfExperts()