class_docstr: Samples from multiple sub-datasets according to given sampling ratios.
  Args:
      datasets (
          List[~torch.utils.data.Dataset]
          or OrderedDict[str, ~torch.utils.data.Dataset]
      ): datasets
      sampling_ratios (List[float]): list of probability of each dataset to be sampled
          (default: None, which corresponds to concatenating all dataset together).
      seed (int): RNG seed to use (default: 2).
      epoch (int): starting epoch number (default: 1).
      eval_key (str, optional): a key used at evaluation time that causes
          this instance to pass-through batches from *datasets[eval_key]*.
      collate_format (CollateFormat):  collater output format, either CollateFormat.ordered_dict or
          CollateFormat.single (default: CollateFormat.single) where CollateFormat.single configures
          the collater to output batches of data mixed from all sub-datasets,
          and CollateFormat.ordered_dict configures the collater to output a dictionary of batches indexed by keys
          of sub-datasets.
          Note that not all sub-datasets will present in a single batch in both formats.
      virtual_size (int, or callable): the expected virtual size of the dataset (default: default_virtual_size_func).
      split (str): the split of the data, e.g. 'train', 'valid' or 'test'.
      shared_collater (bool): whether or not to all sub-datasets have the same collater.
      shuffle (bool): whether or not to shuffle data (default: True).
class_docstr_tok: ["Samples","from","multiple","sub-datasets","according","to","given","sampling","ratios",".","Args",":","datasets","(","List","[","~torch.utils.data.Dataset","]","or","OrderedDict","[","str",",","~torch.utils.data.Dataset","]",")",":","datasets","sampling_ratios","(","List","[","float","]",")",":","list","of","probability","of","each","dataset","to","be","sampled","(","default",":","None",",","which","corresponds","to","concatenating","all","dataset","together",")",".","seed","(","int",")",":","RNG","seed","to","use","(","default",":","2",")",".","epoch","(","int",")",":","starting","epoch","number","(","default",":","1",")",".","eval_key","(","str",",","optional",")",":","a","key","used","at","evaluation","time","that","causes","this","instance","to","pass-through","batches","from","*","datasets","[","eval_key","]","*",".","collate_format","(","CollateFormat",")",":","collater","output","format",",","either","CollateFormat.ordered_dict","or","CollateFormat.single","(","default",":","CollateFormat.single",")","where","CollateFormat.single","configures","the","collater","to","output","batches","of","data","mixed","from","all","sub-datasets",",","and","CollateFormat.ordered_dict","configures","the","collater","to","output","a","dictionary","of","batches","indexed","by","keys","of","sub-datasets",".","Note","that","not","all","sub-datasets","will","present","in","a","single","batch","in","both","formats",".","virtual_size","(","int",",","or","callable",")",":","the","expected","virtual","size","of","the","dataset","(","default",":","default_virtual_size_func",")",".","split","(","str",")",":","the","split","of","the","data",",","e.g",".","'train","'",",","'valid","'","or","'test","'",".","shared_collater","(","bool",")",":","whether","or","not","to","all","sub-datasets","have","the","same","collater",".","shuffle","(","bool",")",":","whether","or","not","to","shuffle","data","(","default",":","True",")","."]
docstr: Merge a list of samples to form a mini-batch.
docstr_tok: ["Merge","a","list","of","samples","to","form","a","mini-batch","."]
code: def collater(self, samples, **extra_args):
      """Merge a list of samples to form a mini-batch."""
      if len(samples) == 0:
          return None
      if self.collate_format == "ordered_dict":
          collect_samples = [[] for _ in range(len(self.datasets))]
          for i, sample in samples:
              collect_samples[i].append(sample)
          batch = OrderedDict(
              [
                  (self.keys[i], dataset.collater(collect_samples[i]))
                  for i, (key, dataset) in enumerate(
                      zip(self.keys, self.datasets)
                  )
                  if len(collect_samples[i]) > 0
              ]
          )
      elif self.shared_collater:
          batch = self.datasets[0].collater([s for _, s in samples])
      else:
          samples_dict = defaultdict(list)
          pad_to_length = (
              defaultdict(int)
              if "pad_to_length" not in extra_args
              else extra_args["pad_to_length"]
          )
          for ds_idx, s in samples:
              pad_to_length["source"] = max(
                  pad_to_length["source"], s["source"].size(0)
              )
              if s["target"] is not None:
                  pad_to_length["target"] = max(
                      pad_to_length["target"], s["target"].size(0)
                  )
              samples_dict[ds_idx].append(s)
          batches = [
              self.datasets[i].collater(
                  samples_dict[i], pad_to_length=pad_to_length
              )
              for i in range(len(self.datasets))
              if len(samples_dict[i]) > 0
          ]
  
          def straight_data(tensors):
              batch = torch.cat(tensors, dim=0)
              return batch
  
          src_lengths = straight_data(
              [b["net_input"]["src_lengths"] for b in batches]
          )
          src_lengths, sort_order = src_lengths.sort(descending=True)
  
          def straight_order(tensors):
              batch = straight_data(tensors)
              return batch.index_select(0, sort_order)
  
          batch = {
              "id": straight_order([b["id"] for b in batches]),
              "nsentences": sum(b["nsentences"] for b in batches),
              "ntokens": sum(b["ntokens"] for b in batches),
              "net_input": {
                  "src_tokens": straight_order(
                      [b["net_input"]["src_tokens"] for b in batches]
                  ),
                  "src_lengths": src_lengths,
              },
              "target": straight_order([b["target"] for b in batches])
              if batches[0]["target"] is not None
              else None,
          }
          if "prev_output_tokens" in batches[0]["net_input"]:
              batch["net_input"]["prev_output_tokens"] = straight_order(
                  [
                      b["net_input"]["prev_output_tokens"]
                      for b in batches
                  ]
              )
          if "src_lang_id" in batches[0]["net_input"]:
              batch["net_input"]["src_lang_id"] = straight_order(
                  [b["net_input"]["src_lang_id"] for b in batches]
              )
          if "tgt_lang_id" in batches[0]:
              batch["tgt_lang_id"] = straight_order(
                  [b["tgt_lang_id"] for b in batches]
              )
      return batch
  
imports: import datetime
  import hashlib
  import logging
  import time
  from bisect import bisect_right
  from collections import OrderedDict, defaultdict
  from enum import Enum
  from typing import List
  import numpy
  import torch
  from fairseq.data import FairseqDataset, data_utils
  from fairseq.distributed import utils
label_desc: Use image node_id 15 for calling the SampledMultiDataset obj's underlying member method code with example usage: obj.collater(samples) and returns: batch, None, batch, batch