liveBook · Manning

Batching

As explained in Machine Learning Platform Engineering

Microbatching is the processing of a small group of newly arrived inputs together for analysis or computation.

Batching processes multiple inputs together instead of handling each input independently. In production ML, it improves the usefulness of comparisons and can improve serving throughput, but batch size and timing must match the workload. section 6.2.3 section 10.2

Common production roles include:

  • Microbatch API requests before running data-drift tests; comparing a reference dataset with one request produces inaccurate results. section 6.2.3
  • Maintain a moving window of recent requests, trigger a report when the configured size is reached, and evict older rows. section 6.2.3
  • Feed training data through PyTorch DataLoader objects using configured training and testing batch sizes. section 9.3.2
  • Let BentoML adaptively batch incoming serving requests to improve throughput and resource utilization. section 10.2

A 100-row monitoring window

Configure a monitoring service with a window size of 100. Append incoming feature rows until the window reaches 100 samples, generate a drift report against the reference dataset, then keep the most recent 100 samples as later requests arrive. A report can therefore represent a snapshot of recent API traffic rather than a single prediction. section 6.2.3

Listing 6.10 MonitoringService to run drift reports periodically
class MonitoringService:
    def __init__(
        self,
        report: Report,
        reference: pandas.DataFrame,
        workspace: Workspace,
        project_id: str,
        window_size: int,
    ):
        self.window_size = int(window_size)
        self.report = report
        self.new_rows = 0
        self.reference = reference
        self.workspace = workspace
        self.project_id = project_id
        self.current = pandas.DataFrame()

    def iterate(self, new_rows: pandas.DataFrame):
        rows_count = new_rows.shape[0]


        self.current = pandas.concat(
            [self.current, new_rows],
            ignore_index=True
        )  #1
        self.new_rows += rows_count
        current_size = self.current.shape[0]
        if current_size > self.window_size:    #2
            self.current = self.current.iloc[-self.window_size :]
            self.current.reset_index(drop=True, inplace=True)
        if current_size < self.window_size:    #3

            logger.info(
              f"Not enough data for measurement: {current_size} "
              f"of {self.window_size}. Waiting more data"
            )
            return
        self.report.timestamp = datetime.datetime.now()
        logger.info("Running report")
        self.report.run(
            reference_data=self.reference,
            current_data=self.current,
        )    #4

        self.workspace.add_report(
            project_id=self.project_id,
            report=self.report
        )  #5

Inspect the MonitoringService implementation for appending new rows, counting arrivals, enforcing the window limit, and triggering periodic reports. section 6.2.3

How Machine Learning Platform Engineering covers it

How deep it goes
Explains batch windows, report triggers, eviction, training loaders, and adaptive serving, but does not develop batching theory.
Examples
Uses a 100-sample rolling monitoring window, PyTorch DataLoader batch sizes, and BentoML adaptive request batching.
What it assumes
Assumes familiarity with API requests, datasets, and model workflows while demonstrating the batching mechanics directly.
What you can do after
Apply it via BentoML or PyTorch by configuring monitoring windows, DataLoader batch sizes, or adaptive request batching.
How it connects
Connects batching with data drift detection, model training, model serving, throughput, resource utilization, and monitoring.

Also explained in

Compare how 15 books explain batching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.