Model parallelism splits one model across devices. Different devices run different parts of the model while processing a batch together. Its practical purpose is to train a model too large to fit on one device—not to speed up an ordinary model that already fits. section 18.2.1
Contrast this with data parallelism: each device holds a complete model replica and processes a different portion of the batch. The two approaches can be combined. For example, eight GPUs can hold two replicas of a model that is itself split across four GPUs. section 18.2.1
Split the work, then replicate it
Picture a 2 × 4 grid of GPUs. Along the four-device “model” axis, devices share one model’s computation. Along the two-device “data” axis, two copies of that split model process different sub-batches. This arrangement gives a model more room without giving up data parallelism. section 18.2.1 section 18.2.2
device_mesh <- keras$distribution$DeviceMesh(
shape = shape(2, 4), #1
axis_names = c("data", "model") #2
)
In Keras, a DeviceMesh names those two axes and gives them sizes: two for data, four for model. The mesh organizes devices; it does not yet specify which model variables to split. section 18.2.2
layout_map <- keras$distribution$LayoutMap(device_mesh)
layout_map["sequential/dense/kernel"] <- tuple(NULL, "model")
layout_map["sequential/dense/bias"] <- tuple("model")
layout_map["sequential/dense_1/kernel"] <- tuple(NULL, "model")
layout_map["sequential/dense_1/bias"] <- tuple("model")
A LayoutMap assigns variable layouts on that mesh. Here, dense-layer kernels and biases are mapped to the model axis. Match the paths to the actual model variables; they can be printed from model$variables. section 18.2.2
model_parallel <- keras$distribution$ModelParallel(
layout_map=layout_map,
batch_dim_name="data" #1
)
keras$distribution$set_distribution(model_parallel)
ModelParallel combines the layout map with the data-axis name. Set it as the active Keras distribution strategy before using the model under that strategy. section 18.2.2
Choose model parallelism when model size demands it, especially if the architecture has naturally parallel branches. If the entire model fits on each device, data parallelism is the simpler scaling choice. section 18.2.1 section 18.4




