class_docstr: A helper class for automagically batching jobs.
class_docstr_tok: ["A","helper","class","for","automagically","batching","jobs","."]
docstr: Determine the optimal batch size
code: def compute_batch_size(self):
      """Determine the optimal batch size"""
      old_batch_size = self._effective_batch_size
      batch_duration = self._smoothed_batch_duration
      if (
          batch_duration > 0
          and batch_duration < self.MIN_IDEAL_BATCH_DURATION
      ):
          # The current batch size is too small: the duration of the
          # processing of a batch of task is not large enough to hide
          # the scheduling overhead.
          ideal_batch_size = int(
              old_batch_size
              * self.MIN_IDEAL_BATCH_DURATION
              / batch_duration
          )
          # Multiply by two to limit oscilations between min and max.
          batch_size = max(2 * ideal_batch_size, 1)
          self._effective_batch_size = batch_size
          if self.parallel.verbose >= 10:
              self.parallel._print(
                  "Batch computation too fast (%.4fs.) "
                  "Setting batch_size=%d.",
                  (batch_duration, batch_size),
              )
      elif (
          batch_duration > self.MAX_IDEAL_BATCH_DURATION
          and old_batch_size >= 2
      ):
          # The current batch size is too big. If we schedule overly long
          # running batches some CPUs might wait with nothing left to do
          # while a couple of CPUs a left processing a few long running
          # batches. Better reduce the batch size a bit to limit the
          # likelihood of scheduling such stragglers.
          batch_size = old_batch_size // 2
          self._effective_batch_size = batch_size
          if self.parallel.verbose >= 10:
              self.parallel._print(
                  "Batch computation too slow (%.4fs.) "
                  "Setting batch_size=%d.",
                  (batch_duration, batch_size),
              )
      else:
          # No batch size adjustment
          batch_size = old_batch_size
  
      if batch_size != old_batch_size:
          # Reset estimation of the smoothed mean batch duration: this
          # estimate is updated in the multiprocessing apply_async
          # CallBack as long as the batch_size is constant. Therefore
          # we need to reset the estimate whenever we re-tune the batch
          # size.
          self._smoothed_batch_duration = 0
  
      return batch_size
  
imports: import gc
  import os
  import sys
  import warnings
  import threading
  from abc import ABCMeta, abstractmethod
  from .format_stack import format_exc
  from .my_exceptions import WorkerInterrupt, TransportableException
  from ._multiprocessing_helpers import mp
  from ._compat import with_metaclass
label_desc: Use image node_id 1 for calling the AutoBatchingMixin obj's underlying member method code with example usage: obj.compute_batch_size() and returns: batch_size