Concepts / Distributed Computing Infrastructure for Machine Learning

Distributed Computing Infrastructure for Machine Learning

The central trick of out-of-core learning is to avoid holding everything at once.

  • Programming

When a Dataset Outgrows the Laptop

Many machine learning projects begin with a dataset that comfortably fits on a laptop. Over time the data keeps growing, and eventually a single file becomes too large to load into main memory in one piece. At that point the usual approach of reading the whole dataset and then training on it stops working. This article examines two strategies for scaling past that limit: out-of-core learning, which keeps the work on one computer but streams the data through it, and parallelization, which splits the work across several computers working together.

What do you think happens?

If a file is far larger than RAM, what do you think a learner must do differently from loading the whole file at once?

  • Load the file in pieces, learn from each piece, and let it go before reading the next

  • Compress the whole file so it fits in memory

  • Train on a random sample and ignore the rest of the file

Reveal answer
Answer:

Load the file in pieces, learn from each piece, and let it go before reading the next

The source describes avoiding the need to hold everything at once: the learner pulls in a sample or a small group of samples sized to fit in RAM, updates the model with it, throws the piece away, and reads the next one. The model is the only thing that carries information forward.

Tracing the Model Across Chunks

The central trick of out-of-core learning is to avoid holding everything at once. The learner reads one sample at a time or a small group of samples, sized so that each group fits in RAM. That piece is used to update the model so that the model reflects what the piece taught it. Then the piece is discarded, and the next piece is read in. The data itself never accumulates in memory. Only the model carries information from one piece to the next.

The easiest way to see this is to follow the model's state step by step. The example below is a generated illustration, not a real learning algorithm: suppose the model's state is just two numbers, a running count of values seen and a running total of those values. Each chunk updates those two numbers and is then gone. The model stands in for whatever a real learner keeps, such as the weights it has learned so far.

Worked example

Updating a running state over three chunks

A dataset of nine values arrives as three chunks of three values each: chunk 1 is [4, 8, 6], chunk 2 is [10, 2, 6], chunk 3 is [1, 9, 5]. The file is never loaded whole. Trace the model state after each chunk.

  1. 1
    Start

    Before any data is read, the model state is count 0 and total 0. Nothing is in memory except this state.

  2. 2
    Process chunk 1

    Read [4, 8, 6] into RAM. Add 3 to the count and 18 to the total. The state becomes count 3, total 18. Discard chunk 1.

  3. 3
    Process chunk 2

    Read [10, 2, 6] into RAM. Add 3 to the count and 18 to the total. The state becomes count 6, total 36. Chunk 1 is no longer anywhere in memory, yet its information survives in the state. Discard chunk 2.

  4. 4
    Process chunk 3

    Read [1, 9, 5] into RAM. Add 3 to the count and 15 to the total. The state becomes count 9, total 51. Discard chunk 3.

  5. 5
    Read the result

    The model can report the mean of all nine values as 51 divided by 9, which is about 5.67, even though no moment existed when all nine values were in memory together.

Result

After every chunk the model holds a compact summary, the count 3, then 6, then 9, with totals 18, 36, then 51. Each chunk was used once and discarded, and only the state moved forward.

Model: count 0,total 0Chunk 1 readdiscarded after useModel: count 3, total18Chunk 2 readdiscarded after useModel: count 6, total36Chunk 3 readdiscarded after useModel: count 9, total51readsupdatesreadsupdatesreadsupdates
After each chunk is processed and discarded, what does the model hold, and how does its state change from one step to the next?

Notice what the example depends on. The model must be something that can be updated incrementally, meaning it can absorb one chunk and then absorb another without needing to see the earlier chunks again. Our running count and total have this property. Not every algorithm does. An algorithm that needs to see the whole dataset at once cannot be trained this way.

Keeping Only One Chunk in Memory

Definition

Out-of-core learning trains a model on data too large for main memory by reading it in chunks, updating the model on each chunk, and discarding the chunk before reading the next one.

The memory picture is simple. At any moment, RAM holds the chunk currently being processed and the model. The full dataset remains on disk. Earlier chunks have already left memory, and later chunks have not yet arrived. The diagram below shows this layout. The source describes the process as a loop rather than a one-way pipeline: data flows from storage into memory, the model absorbs what the chunk contains, the chunk leaves memory, and the cycle begins again.

Full datasetstored on diskCurrent chunkthe only data in RAMModelcarries information forwardEarlier chunksalready removed from memorystreams inupdatesdiscarded
When the dataset is much larger than RAM, what lives in main memory at one moment, and what remains on disk?

Scikit-learn implements out-of-core learning for some of its models, and its online user guide describes which ones and how to use them. Whether a given model is available this way is something to check for the specific model you plan to use.

Spreading Data Across a Cluster

When one computer working through chunks one after another is too slow, or the data outgrows what one machine can handle, the scaling picture changes. Instead of one computer processing every chunk, many computers each hold and process a share of the dataset. The source states that for some models this can be much faster, and that the amount of data that can be handled is limited only by the size of the cluster. Adding machines is therefore the main way to grow capacity.

Full datasetPartition 1Machine APartition 2Machine BCombined resultPartition 3Machine Csplitsplitsplitplaced onplaced onplaced onresultsresultsresults
How does one large dataset get split into partitions, where does each partition go, and how do the results flow back together?

The diagram captures the core idea. One dataset is split so that each machine receives a portion, and each machine works on its portion independently. The number of machines in the cluster sets the ceiling on how much data the setup can handle. The diagram's final step, where results come back together, is a generated summary of the flow; the source itself focuses on the split and the independent work on each portion.

How Hadoop, Spark, and MLlib Nest

One of the most popular distributed computing platforms in use is Spark, which is built on top of Hadoop. Spark includes machine learning functionality inside a package called MLlib. The layers are nested, and each one provides something to the layer above it. Hadoop provides the storage and infrastructure underneath. Spark provides the distributed computing engine on top of that. MLlib supplies the learning algorithms within Spark.

Hadoopstorage and infrastructureSparkdistributed computingengineMLliblearning algorithmsbuilt onlives inside
Which component sits inside which, and what does each layer provide to the layer above it?

The source gives two situations where Spark is the natural choice. If your data already lives on a Hadoop filesystem, Spark can work with it directly. The same holds if you already use Spark to preprocess your data. In those cases, adopting Spark for model training may be the easiest path. If you do not have this infrastructure, however, setting up and integrating a Spark cluster can be too large an effort. In that case the source points to the vw package as a better fit, since it offers some distributed features without requiring the whole stack.

Choosing Between Chunking and Distribution

The two strategies differ in where the computation happens, how much data each machine handles, and what each one costs in complexity and speed. The comparison below summarizes these differences as the source describes them.

AspectSingle-machine chunking (out-of-core)Cluster distribution (parallelization)
Where computation happensOn one computer, which processes every chunkAcross many machines, each working on its own share
Data handled per machineOne chunk at a time in RAMA portion of the dataset per machine
Speed on very large dataRuntime can grow very longCan be much faster for some models
Capacity limitLimited by the one machine's running timeLimited by the size of the cluster
Infrastructure and complexitySimpler; no cluster to buildComplex; machines must be set up, connected, and coordinated
Algorithm requirementModel must be updatable chunk by chunkDepends on the algorithm and platform used

Single-machine chunking versus cluster-based distribution

One machineMany machinesChunks in sequenceShares in parallelSimpler setupCluster overheadprocessescostsprocessescosts
How do the two approaches differ in where computation happens, how much data each machine handles, and what each costs?

The source offers a useful way to decide. Ask whether the algorithm can be trained incrementally on chunks. If so, out-of-core learning may be enough, and it avoids cluster overhead. Then ask whether the single machine's runtime is acceptable, and whether the data still outgrows what one machine can handle. If either answer points the other way, distributing the work across a cluster becomes the more reasonable option.

Mistakes Learners Make with Scaling

  • Common mistake

    Assuming any model can be trained out-of-core

    What it looks like

    Trying to chunk a dataset and train an algorithm that needs to see the whole dataset at once.

    Why it fails

    Out-of-core learning requires a model that can absorb one chunk and then another without revisiting earlier chunks. Not every algorithm has this property.

    Do this instead

    Check whether the specific model supports incremental updates before planning a chunked workflow.

  • Common mistake

    Keeping every chunk in memory to be safe

    What it looks like

    Appending each chunk to a growing list so that all earlier data stays in RAM.

    Why it fails

    The approach depends on discarding each chunk after it updates the model. Accumulating chunks defeats the memory goal.

    Do this instead

    Let the model carry the information forward and release the chunk before reading the next one.

  • Common mistake

    Building a cluster for a modest dataset

    Why it fails

    Distributed computation brings complex infrastructure requirements. For a dataset that a simpler strategy can handle, the cluster overhead can outweigh the benefit.

    Do this instead

    Try single-machine chunking first, and move to a cluster only when runtime or data size requires it.

  • Common mistake

    Treating Spark as a replacement for MLlib, when MLlib is the package of learning algorithms that runs inside Spark

Deciding on a Scaling Strategy

MEDIUM

A team has a model that can be updated one chunk at a time. Their dataset is 40 gigabytes, and a single machine finishes a full pass in a runtime the team considers acceptable. They already store their data on a Hadoop filesystem. Which strategy should they consider first, and what would change the answer?

Hints
  • Start with the two questions the source suggests: can the algorithm be trained incrementally, and is the single machine's runtime acceptable?
  • Think about whether the existing Hadoop setup changes how easy a Spark-based approach would be.
  • Consider what would happen if the runtime became unacceptable or the data grew well beyond what one machine can handle.

Key Takeaways

  • Out-of-core learning trains a model on data too large for RAM by reading one chunk at a time, updating the model, and discarding the chunk, so the model is the only thing carried forward.
  • Parallelization splits a dataset across the machines of a cluster, each working on its own portion, and the size of data it can handle is limited by the cluster's size.
  • Both strategies have costs: single-machine chunking can run very long and requires an incrementally updatable algorithm, while cluster distribution requires complex infrastructure that can be excessive for modest datasets.
  • The Spark stack is nested: Hadoop provides storage and infrastructure, Spark provides the distributed engine, and MLlib supplies the learning algorithms within Spark.

Related concepts

More concepts starting with D