TensorFlow
Model Parallelism
Distributed Computing
Deep Learning
Neural Networks

Implementation of model parallelism in tensorflow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Model parallelism means splitting one model across multiple devices so that different layers or blocks live on different GPUs or workers. This is different from data parallelism, where the whole model is copied to every device. In TensorFlow, the practical implementation is usually explicit device placement.

Manual Device Placement Is the Core Technique

For many TensorFlow workloads, model parallelism is not a one-line strategy object. The simplest working approach is to place different parts of the model on different devices with tf.device.

python
1import tensorflow as tf
2
3with tf.device('/GPU:0'):
4    input_layer = tf.keras.Input(shape=(1024,))
5    x = tf.keras.layers.Dense(2048, activation='relu')(input_layer)
6    x = tf.keras.layers.Dense(2048, activation='relu')(x)
7
8with tf.device('/GPU:1'):
9    x = tf.keras.layers.Dense(2048, activation='relu')(x)
10    output_layer = tf.keras.layers.Dense(10)(x)
11
12model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
13model.summary()

This places the early layers on one GPU and later layers on another. TensorFlow handles the tensor transfers between devices automatically.

Why Model Parallelism Is Used

Use model parallelism when:

  • the model is too large to fit on one device
  • one stage of the network needs a device with more memory
  • you want to split very large embeddings, blocks, or decoders across devices

It is not automatically faster than data parallelism. Cross-device communication can easily become the bottleneck if the split point is poorly chosen.

Training Still Looks Familiar

Once the model is built, training with model.fit can still work if the graph placement is valid.

python
1import numpy as np
2
3x_train = np.random.randn(32, 1024).astype('float32')
4y_train = np.random.randn(32, 10).astype('float32')
5
6model.compile(optimizer='adam', loss='mse')
7model.fit(x_train, y_train, epochs=2, batch_size=8)

The main difference is where the layers execute, not how Keras training is invoked.

Choose Split Points Carefully

The best split point is usually where activations are relatively small compared with the compute saved. If you split a model at a layer that produces huge tensors, device-to-device transfer can cancel out the benefit of parallelism.

A good design tries to balance:

  • memory per device
  • compute per device
  • communication cost between devices

This is why model parallelism is often more of a system-design problem than a syntax problem.

Once the model is split across devices, some systems also pipeline microbatches so one device can process the next microbatch while another finishes the previous one. That can improve utilization, but it adds scheduling complexity and is not the same as basic layer placement.

So the first implementation goal should be simple model partitioning. Pipeline optimization comes later.

Do Not Confuse It with Data Parallelism

tf.distribute strategies are often associated with data parallelism, where each replica sees a different batch shard. That is useful, but it does not automatically implement the kind of layer-by-layer partitioning people usually mean by model parallelism.

In practice, TensorFlow model parallelism often starts with explicit placement, then grows into more specialized sharding or distributed tooling only if the workload justifies it.

Common Pitfalls

  • Expecting data-parallel distribution APIs to automatically split one model across devices.
  • Splitting the model at a point that creates excessive cross-device tensor transfer.
  • Assuming model parallelism always improves speed instead of sometimes only solving memory pressure.
  • Ignoring device memory balance and overloading one GPU while the other stays underused.
  • Adding pipeline complexity before the basic multi-device partition works correctly.

Summary

  • Model parallelism places different parts of one model on different devices.
  • In TensorFlow, explicit tf.device placement is the usual starting point.
  • The goal is often memory fit first, speed second.
  • Good split points minimize communication while balancing compute and memory.
  • Data parallelism and model parallelism solve different scaling problems and should not be confused.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.