MXNet
GPU
deep learning
machine learning
hardware acceleration

Is there a way to check if mxnet uses my gpu?

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

Yes, you can check whether MXNet is using your GPU, and you should verify it instead of assuming that a GPU-enabled installation guarantees acceleration. In practice, you want to confirm three things: MXNet sees a GPU, your arrays or model are actually placed on that GPU, and the device shows real activity while the program runs.

Check whether MXNet sees any GPU devices

The first question is whether MXNet can detect a GPU at all.

python
1import mxnet as mx
2
3print("Number of GPUs:", mx.context.num_gpus())
4print("CPU context:", mx.cpu())
5print("GPU context:", mx.gpu(0))

If mx.context.num_gpus() returns 0, MXNet does not currently see a usable GPU. That usually means one of these problems:

  • A CPU-only MXNet package is installed
  • CUDA or cuDNN is missing or incompatible
  • The NVIDIA driver is not available
  • You are running in an environment without GPU access

Verify array placement directly

Even if a GPU is available, MXNet will not use it unless the data or model is placed on a GPU context.

python
1import mxnet as mx
2from mxnet import nd
3
4ctx = mx.gpu(0)
5x = nd.ones((2, 3), ctx=ctx)
6
7print(x)
8print("Context:", x.context)

The key line is x.context. If it prints something like gpu(0), that array lives on the GPU. If it prints cpu(0), your code is still using the CPU.

Check a Gluon model context

With Gluon, the model parameters must also be moved to the GPU.

python
1import mxnet as mx
2from mxnet.gluon import nn
3
4ctx = mx.gpu(0)
5
6net = nn.Sequential()
7with net.name_scope():
8    net.add(nn.Dense(16, activation="relu"))
9    net.add(nn.Dense(1))
10
11net.initialize(ctx=ctx)
12
13for name, param in net.collect_params().items():
14    print(name, param.list_ctx())

If the parameters report gpu(0), the model is on the GPU. If not, MXNet will execute on the CPU even if a GPU exists.

Watch the GPU externally

The most convincing check is to run your code while observing the device from outside the process. On NVIDIA systems, nvidia-smi is the standard tool.

bash
nvidia-smi

When a training step or large tensor operation is running, you should see memory usage and often utilization increase for the Python process. This is a practical cross-check because it confirms not just placement, but real device activity.

Data and model must be on the same device

A common source of confusion is moving only part of the workload. For example, putting the model on the GPU but leaving batches on the CPU creates device mismatch problems or silent CPU-heavy behavior in surrounding code.

python
1import mxnet as mx
2from mxnet import nd
3
4ctx = mx.gpu(0)
5data = nd.random.uniform(shape=(4, 4), ctx=ctx)
6weights = nd.random.uniform(shape=(4, 4), ctx=ctx)
7
8result = nd.dot(data, weights)
9print(result.context)

Both operands and the result stay on the GPU here. That is the pattern you want for accelerated execution.

Common Pitfalls

The most common mistake is installing a CPU-only MXNet build and assuming GPU support is automatic. If num_gpus() is zero, check the package and CUDA stack first.

Another issue is creating arrays on the CPU by default and forgetting to move them with as_in_context or by constructing them directly on mx.gpu(0).

Developers also verify only one side of the workload. A GPU-resident model with CPU-resident input data is still a broken setup.

Finally, do not rely on speed alone. Small workloads can run so quickly on CPU that the difference is not obvious. Check the context explicitly and confirm with nvidia-smi.

Summary

  • Use mx.context.num_gpus() to see whether MXNet detects GPU devices.
  • Check array.context and parameter contexts to verify actual placement.
  • Make sure both model parameters and input data live on the GPU.
  • Use nvidia-smi as an external confirmation of real GPU activity.
  • If MXNet reports zero GPUs, inspect the package, driver, CUDA, and environment setup.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.