Part VII: Cluster, Edge, and Reliable Infrastructure
Chapter 34: Edge, Fog, and On-Device Distributed AI

Edge AI as Distribution to the Periphery

"Every frame, I face the same question: solve it here in my own small silicon, or pay the network to ask someone smarter. Most days I decide it is cheaper to think for myself."

A Sensor at the Edge, Deciding Whether to Phone Home
Big Picture

Scale-out has a direction we have not yet used: outward. Every part of this book until now pulled work toward the center, gathering data into a cluster, sharding a model across a datacenter, replicating a service behind a load balancer. Edge AI inverts that gravity. It pushes computation out to where the data is born, onto phones, cameras, vehicles, and sensors, because the round trip to the center costs too much in latency, bandwidth, money, privacy, or autonomy to pay on every observation. This is still distribution, still the same questions of how to split work, move information, and recombine results, but in a new regime: millions of heterogeneous, unreliable, resource-constrained nodes connected by links that may vanish at any moment, instead of the homogeneous, well-fed, well-connected machines of Chapter 33. This section sets that thesis and draws the map, the cloud-to-device continuum, that the rest of the chapter fills in.

The datacenter of Chapter 33 is the natural home of scale-out as we have developed it. Thousands of near-identical accelerators sit in racks, joined by an interconnect engineered for bandwidth, powered and cooled without limit, scheduled by a system that assumes every node is reachable and roughly equal. Data flows in, gradients and predictions flow out, and the whole arrangement behaves as one large coherent machine. That picture is correct, and it underwrites most of modern AI training and a great deal of its serving. It also quietly assumes that the data has already arrived at the center, that moving every input to the cluster and every output back is affordable. For an enormous class of real systems that assumption is false, and the failure of that single assumption is what creates the field of edge AI.

Consider where data actually originates. A self-driving car generates terabytes of sensor readings per hour, on a road, miles from any datacenter, on a cellular link that is metered and intermittent. A factory inspection camera produces a video stream that must trigger a reject decision within milliseconds, faster than light can make the round trip to a regional cloud. A medical wearable records signals whose owner has not consented to streaming them anywhere. A smart speaker must keep working when the home network drops. In each case the data is born at the periphery, and the question is no longer "how do we bring it to the compute" but "how do we bring the compute to it". That inversion, distribution to the periphery rather than concentration at the center, is the subject of this chapter.

Cloud datacenter cluster (Ch 33) Fog regional / on-prem gateways (34.2) Edge base stations, site servers Device phones, cameras, sensors (34.3) More compute, energy, memory, aggregated data volume More nodes, more heterogeneity, more unreliable links Higher latency & bandwidth cost to the data source Closer to where data is born; works when disconnected
Figure 34.1.1: The cloud-fog-edge-device continuum. Tiers are not rivals but a spectrum: moving left concentrates compute, energy, and aggregated data in fewer, richer, well-connected machines; moving right spreads work across more numerous, weaker, more heterogeneous, less reliably connected nodes that sit nearer the data and keep functioning when the link drops. Fog is developed in Section 34.2 and the device tier in Section 34.3.

1. The Center of Gravity Was Always a Choice Beginner

It is worth seeing clearly that pulling data to a central cluster was never a law of nature; it was an economic default that held while bandwidth was cheap relative to compute and while latency to the cloud was tolerable. Under those conditions the simplest correct design is to ship every input to a place with abundant accelerators, run the model there, and ship the answer back. That default produced the architectures of Parts II through V, and it remains right whenever the data is already in the datacenter or is cheap to move there. Edge AI is what you reach for when the default breaks, and it breaks along four fairly distinct seams that are useful to keep separate because, exactly as with the three ceilings of Section 1.1, each one calls for a different response.

The first seam is latency. The round trip from a device to a regional datacenter and back is bounded below by physics and inflated by every queue and protocol hop along the way; tens of milliseconds is a good case, and many control loops (a robot arm, a vehicle, an augmented-reality overlay) need an answer faster than that round trip can possibly deliver. The second is bandwidth and its cost. A fleet of high-resolution cameras or a population of always-listening microphones generates far more raw data than any network can carry to the center, and on a metered cellular or satellite link the bytes are billed. The third is privacy and regulation. Health signals, faces, keystrokes, and location traces are exactly the data that users and law are least willing to see streamed to a third party, so the safest place to process them is the device that already holds them. The fourth is autonomy under intermittent connectivity: a tractor in a field, a drone past the horizon, or a phone in a tunnel must keep making decisions when the link to the center is slow, expensive, or simply gone.

Key Insight: Edge AI Is Scale-Out Run Outward, Not a Retreat From It

It is tempting to read on-device inference as a step back toward the single machine, the very thing Section 1.1 argued against. It is the opposite. A central cluster is one logical machine made of many; an edge deployment is one logical service made of millions of physical machines, each holding a shard of the work, that must still be coordinated, updated, monitored, and (in the federated case of Section 34.6) trained together. The unit of distribution moved from "accelerators in a rack" to "devices in the world", and the homogeneous, reliable, fully connected assumptions of the datacenter went with it. Everything gets harder; nothing about the scale-out thesis is abandoned.

2. The Cloud-to-Device Continuum Beginner

It is a common simplification to speak of "the cloud" and "the edge" as two boxes, but real deployments live on a continuum with at least four named tiers, drawn in Figure 34.1.1 and summarized in Table 34.1.1. At one end is the cloud: the datacenter cluster of Chapter 33, with effectively unlimited compute, energy, and memory, holding the most aggregated data but sitting farthest, in latency and in bytes, from where any single observation is made. At the other end is the device itself, the phone or camera or microcontroller that captures the data, with the least compute and energy but zero network latency to its own sensors and the ability to keep working when fully disconnected. Between them sit fog and edge tiers: regional gateways, on-premises servers, and base-station compute that trade a little proximity for a lot more capability, aggregating many devices without making the full trip to the cloud.

Table 34.1.1: The four tiers of the continuum and how the relevant quantities change as you move outward from the center. No tier is best in isolation; a deployment places each piece of work where its dominant constraint is cheapest to satisfy.
TierTypical hardwareCompute & energyLatency to data sourceNode count & heterogeneity
CloudGPU/TPU datacenter racksEffectively unlimitedHighest (tens of ms or more)Few, homogeneous
FogRegional / on-prem serversLargeModerateMany sites, semi-uniform
EdgeBase stations, site boxesModestLowVery many, varied
DevicePhone NPU, MCU, camera SoCTight (often battery)Near zeroMillions, highly heterogeneous

The design question is therefore not "cloud or edge?" but "which piece of which workload belongs on which tier?". A speech assistant might run wake-word detection on the device (it must always be on and must not stream audio), short-command recognition at the edge, and a full conversational model in the cloud. A camera network might run motion and person detection on each camera, aggregate and re-identify across cameras at a fog gateway, and retain only flagged events centrally. The skill this chapter builds is reading a workload's dominant constraint, latency, bandwidth, privacy, or autonomy, and placing each stage at the tier where that constraint is cheapest to satisfy. We give the fog tier its own treatment in Section 34.2 and the device tier in Section 34.3.

3. The Latency and Bandwidth Budget, Made Explicit Intermediate

The drivers become concrete the moment we write them as a budget. Consider the end-to-end latency of answering one query when the model lives in the cloud. The data must serialize onto the uplink, traverse the network there and back, wait in the server's queue, run through the model, and return:

$$T_{\text{cloud}} = \underbrace{T_{\text{rtt}}}_{\text{round trip}} + \underbrace{\frac{B_{\uparrow}}{R_{\uparrow}} + \frac{B_{\downarrow}}{R_{\downarrow}}}_{\text{serialize in/out}} + \underbrace{T_{\text{queue}} + T_{\text{compute}}^{\text{cloud}}}_{\text{at the server}}.$$

Here $B_{\uparrow}$ and $B_{\downarrow}$ are the bytes sent up and down, $R_{\uparrow}$ and $R_{\downarrow}$ the link rates, and $T_{\text{rtt}}$ the network round-trip time. Running the same query on the device deletes the entire network term, leaving only local compute:

$$T_{\text{edge}} = T_{\text{compute}}^{\text{edge}}, \qquad T_{\text{compute}}^{\text{edge}} > T_{\text{compute}}^{\text{cloud}}.$$

The edge pays for this with a slower compute term, because the on-device model is smaller and the silicon weaker, so placement is a genuine trade rather than a free win: the edge wins precisely when the network terms it removes exceed the extra compute it adds. The bandwidth picture is starker still, because it accumulates. If the device streams every observation to the cloud it sends $B_{\uparrow}$ bytes per query; if it runs the model locally and emits only the decision it sends a few bytes per query (or none, if it acts locally), and over a busy day the ratio of the two is enormous. The demo in Code 34.1.1 plugs representative numbers for a 15-frame-per-second camera into both equations.

def serialize_ms(nbytes, mbps):
    # time to push nbytes onto a link of the given rate, in milliseconds
    return (nbytes * 8) / (mbps * 1e6) * 1e3

# One 224x224 camera frame, JPEG-compressed before upload.
jpeg_bytes, label_bytes = 30_000, 200
rtt_ms, up_mbps, down_mbps = 50.0, 8.0, 30.0      # a typical 4G uplink

# Cloud path: T_rtt + serialize up + compute + serialize down.
cloud_total = (rtt_ms
               + serialize_ms(jpeg_bytes, up_mbps)
               + 8.0                                # big model, fast once data lands
               + serialize_ms(label_bytes, down_mbps))

# Edge path: only local compute, no network term at all.
edge_total = 35.0                                   # smaller model on a mobile NPU

# Bandwidth over a day at 15 fps: stream every frame vs. send only decisions.
frames = 15 * 86_400
cloud_mb = frames * jpeg_bytes / 1e6
edge_mb  = frames * label_bytes / 1e6
print(f"cloud latency {cloud_total:6.1f} ms   edge latency {edge_total:6.1f} ms")
print(f"cloud data {cloud_mb:8.1f} MB/day   edge data {edge_mb:6.1f} MB/day")
print(f"latency speedup {cloud_total/edge_total:4.1f}x   bandwidth cut {cloud_mb/edge_mb:4.0f}x")
Code 34.1.1: The cloud-versus-edge budget for a single camera, evaluating the two latency equations above and accumulating the daily byte count. The constants are deliberately ordinary; the point is the shape of the result, not the exact figures.
cloud latency   88.1 ms   edge latency   35.0 ms
cloud data  38880.0 MB/day   edge data  259.2 MB/day
latency speedup  2.5x   bandwidth cut  150x
Output 34.1.1: For these representative constants the edge answers in 35 ms against the cloud's 88 ms, a 2.5x latency win, while sending 259 MB per day instead of 39 GB, a 150x bandwidth cut. The latency advantage comes entirely from deleting the 50 ms round trip and the 30 ms uplink serialization; the bandwidth advantage compounds with every frame.

Two lessons fall out of Output 34.1.1. First, the edge wins on latency only because the network terms it removes (here 80 of the cloud's 88 milliseconds) dwarf the extra on-device compute; if the model were heavy enough to make $T_{\text{compute}}^{\text{edge}}$ exceed the whole cloud round trip, the trade would flip, which is why model compression and small-model design are inseparable from edge deployment and occupy much of Section 34.3. Second, the bandwidth saving does not depend on any latency assumption at all; it follows purely from sending decisions instead of raw observations, and it is what makes large camera and sensor fleets economically possible. The full machinery for measuring these quantities rigorously, tail latency, energy per inference, accuracy under compression, lives in Chapter 5; here we use them only to motivate placement.

Practical Example: The Camera Network That Stopped Streaming Video

Who: A platform engineer at a logistics company running 4,000 dock-and-warehouse cameras for safety monitoring.

Situation: Every camera streamed full-resolution video to a central cloud that ran person-and-forklift detection and raised alerts.

Problem: The aggregate upload saturated each site's internet link, the monthly bandwidth bill was the largest line item, and alerts arrived one to two seconds late because of upload and queueing delay.

Dilemma: Buy fatter internet links at every site and a bigger cloud cluster, the scale-up-the-pipe move, or push the detector onto a small box at each site and send only events, the scale-out-to-the-edge move that needs a compressed model and a fleet update path.

Decision: They moved detection to an inexpensive edge accelerator per site, streaming only flagged clips and structured events to the cloud, keeping central compute for cross-site analytics.

How: A quantized detector ran on each site box; the cloud received a few kilobytes of JSON per event instead of a continuous video feed, with raw footage retained locally for a short window.

Result: Upload volume fell by more than two orders of magnitude, mirroring the 150x in Output 34.1.1; alert latency dropped below 100 ms; and the bandwidth bill collapsed, paying back the edge hardware within a quarter.

Lesson: When the dominant cost is moving raw observations, the highest-leverage move is to compute the decision where the observation is made and ship only the decision.

4. A Different Regime of Distribution Intermediate

The datacenter cluster of Chapter 33 and an edge fleet are both distributed systems, but almost every assumption that made the cluster tractable is reversed at the periphery, and naming the reversals is the fastest way to see why edge AI needs its own chapter. In the cluster the nodes are homogeneous; at the edge they span a decade of hardware generations, operating systems, and accelerators, so the same model must run acceptably on a flagship phone and a five-year-old budget device. In the cluster the network is fast, private, and engineered for collective communication; at the edge it is slow, metered, shared with the public internet, and frequently absent, so the tight synchronous all-reduce of Chapter 15 is simply not available. In the cluster a node either works or is quickly replaced by a scheduler that sees the whole fleet; at the edge a node may be powered off by its owner, run out of battery, or roam out of coverage, and no central scheduler controls it.

The scale itself is different in kind. A large training cluster has thousands of nodes; a consumer edge deployment has tens or hundreds of millions, each contributing a sliver of data the center may never even see. Resources are tight and non-negotiable: a phone budgets compute against battery and thermal limits, a microcontroller against kilobytes of RAM, and exceeding either budget is not a slowdown but a dead device or a drained battery. These properties, heterogeneity, unreliable and intermittent connectivity, no central control, extreme node count, and hard resource limits, are not nuisances layered on top of ordinary distribution; they define a distinct regime in which familiar primitives must be rebuilt. Federated learning, the subject of Section 34.6, is precisely what data-parallel training becomes once you accept all of these constraints at once, and it is the direct descendant of the FedAvg and gossip methods built in Chapter 14.

Thesis Thread: The Unit of Distribution Moves to the World

The spine of this book is that intelligent systems are distributed across many machines (Section 1.1). Every part so far chose those machines to be cooperative: racks in a datacenter, picked, placed, and connected for the job. Edge AI keeps the thesis and changes the machines. The nodes are now the phones, cameras, vehicles, and sensors of the physical world, unchosen, unequal, and unreliable, yet the goal is unchanged: split the work, move only what must move, and recombine into one coherent behavior. The collective all-reduce of Chapter 4 becomes the periodic, lossy, privacy-preserving aggregation of Section 34.6; the gang scheduler of Chapter 33 becomes a best-effort over-the-air update that some fraction of devices will miss. Scale-out, pushed to its geographic extreme, is the most demanding regime in the entire book, and it is the one closest to where most AI actually touches the world.

Library Shortcut: One Trained Model, Every Edge Runtime

You do not hand-port a model to each device class. A trained network is exported once to a portable graph (ONNX, or a framework's mobile format) and then compiled by a per-target runtime that knows the device's accelerator. The same artifact runs on a phone NPU, a microcontroller, and a browser:

# Export once from your training framework, then deploy per target runtime.
import torch
torch.onnx.export(model, example_input, "detector.onnx",   # portable graph
                  opset_version=17, dynamic_axes={"input": {0: "batch"}})

# On a server / edge box: ONNX Runtime picks the best local provider.
import onnxruntime as ort
sess = ort.InferenceSession("detector.onnx",
                            providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
labels = sess.run(None, {"input": frame})            # one call, runtime handles the hardware

# On a phone you would instead convert to TensorFlow Lite / Core ML; on a
# microcontroller, to TFLite Micro; in a browser, to ONNX Runtime Web or WebNN.
Code 34.1.2: A single exported graph fans out to many runtimes (ONNX Runtime, TensorFlow Lite, Core ML, TFLite Micro, WebNN). The runtime, not your code, absorbs the device heterogeneity that Section 4 identified as the defining edge constraint, turning "millions of different devices" into "one model, many compiled backends".

5. What This Chapter Builds, and Where It Connects Beginner

With the thesis set and the continuum mapped, the rest of the chapter develops each tier and the learning problem that spans them. Section 34.2 studies the fog tier, the regional gateways and on-premises servers that aggregate many devices without the full trip to the cloud, and the placement problem of deciding which stage of a pipeline runs where. Section 34.3 descends to the device itself, where model compression, quantization, and small-model design make inference fit inside a battery and a few milliwatts. Later sections build the fleet machinery (over-the-air model delivery, monitoring devices you do not control) and culminate in Section 34.6, federated learning at the edge, where the devices stop merely running a model and start collectively training one, carrying the FedAvg and decentralized methods of Chapter 14 into the harsh connectivity and heterogeneity regime named in Section 4.

Throughout, the evaluation discipline of Chapter 5 is what keeps placement decisions honest. "Run it on the edge" is a hypothesis about latency, energy, accuracy, and cost that must be measured, not asserted, because the on-device compute term can quietly erase the network saving, because quantization can quietly erase accuracy, and because the tail latency that matters to a user is not the average that a quick benchmark reports. Edge AI is where the trade-offs of the whole book, the communication tax of Section 1.1, the cost models of Chapter 3, the reliability concerns that thread through Part VII, become most visible, because here the machines are the ones in people's pockets and on the roads, and the constraints are real, physical, and unforgiving.

Research Frontier: On-Device Foundation Models (2024 to 2026)

The sharpest current push is moving genuinely capable foundation models onto the device. Small but strong open models, in the lineage of Microsoft's Phi family, Google's Gemma and on-device Gemini Nano, Apple's on-device foundation models, and Meta's quantized small Llama variants, target single-digit-billion or sub-billion parameter counts that fit a phone's memory and thermal envelope. The enabling techniques are aggressive post-training quantization (4-bit and lower), quantization-aware training, structured sparsity, and speculative or hybrid decoding that runs a small draft model on-device and defers only hard tokens to a fog or cloud tier. A parallel line studies the placement question itself as model routing: deciding per-query whether a request can be answered locally or must escalate, which is exactly the "phone home or not" decision of this section's epigraph, now made by a learned policy. We meet the on-device side of these methods in Section 34.3 and the collaborative-training side in Section 34.6.

Fun Note: The Smartest Computer in the Room Is in Your Pocket

The phone in your pocket already does this continuum every day without telling you. The wake word for its assistant is recognized by a tiny always-on model that never leaves the chip; your photos are sorted into faces and places by a model that runs while it charges overnight; predictive text learns your habits without uploading your messages. Each is a deliberate placement decision, made so that the common case never has to phone home. The device in the epigraph that decides whether to ask someone smarter is not a metaphor; it is the routing logic shipping in the hardware most people are holding right now.

Exercise 34.1.1: Place the Pipeline Conceptual

For each system, name the dominant driver from Section 1 (latency, bandwidth, privacy, or autonomy) and assign each stage of its pipeline to a tier of the continuum in Figure 34.1.1, justifying the placement: (a) a voice assistant that must respond to "stop" instantly but answer open questions richly; (b) a continuous glucose monitor that alerts a wearer and, with consent, contributes to research; (c) a fleet of delivery drones that must avoid obstacles past the range of any network. Explain why placing one named stage at the wrong tier would defeat its dominant driver.

Exercise 34.1.2: Find the Break-Even Model Size Coding

Extend Code 34.1.1 so the on-device compute term $T_{\text{compute}}^{\text{edge}}$ scales with a model-size factor you sweep from 1x to 20x of its base value, while the cloud compute term scales more slowly (the cloud accelerator is far faster). Plot or print cloud and edge end-to-end latency against the factor and find the crossover where the edge stops being faster. Then vary the network RTT from 5 ms (a nearby edge server) to 200 ms (a distant or congested link) and report how the crossover moves. State in one sentence what the crossover tells a deployment engineer about which models belong on the device.

Exercise 34.1.3: When Does the Edge Lose? Analysis

Output 34.1.1 made the edge look unambiguously better. Construct, with numbers, three realistic workloads where the cloud is the correct placement despite the network round trip: one where the per-query model is too large for any device, one where outputs (not inputs) are the bulk of the bytes so local computation does not reduce traffic, and one where cross-device aggregation (correlating signals from many sensors) is the actual task. For each, show which term in the latency budget or which property from Section 4 forces the work back toward the center, and connect your reasoning to the cost models of Chapter 3.