Robot Training on a Mac

Robot learning is usually done on an NVIDIA graphics card. This article measures how close a Mac can get on a real training job, using the MuJoCo simulator and two robot dogs learning to wrestle. After a few changes, a MacBook Air reached 69% of a laptop RTX 3060 and a Mac mini reached 61%. Together the two Macs were faster than the graphics card.

Test task

The test was a sumo match between two copies of a 7.8 kg robot dog with eight leg motors. Each robot tries to push the other out of a ring 2.0 m across, or to tip it over. The robots learned by reinforcement learning through self-play, where each new version trains against earlier versions of itself. Over a day this produced 13 fighting styles, from a charging bully to a robot that sweeps the other's paws.

The simulation is detailed, which makes it slow. Physics runs in 1 ms steps, and the robot's controller (its policy) picks new motor targets 50 times a second. Each motor follows a model of a real brushless actuator, with its torque curve, heating, control delay and battery sag. The contact shapes come from the printed body, head, tail, legs and paws.

Speed is counted in policy steps per second. One policy step is 20 ms of simulated time for both robots, with 20 physics steps inside it.

GPU reference

The reference machine is a gaming laptop with an RTX 3060 (6 GB). It runs MuJoCo's GPU version, MJX, with NVIDIA's Warp backend, and trains with PPO on 1,024 copies of the match at once. It reached about 3,500 policy steps per second.

The first idea was to run the same GPU code on the Mac. That did not work. The GPU version needs a long one-time compile of the simulation, and on the Mac's processor this took more than 10 minutes without finishing. Apple's GPU plugin for the same toolkit does not support MJX at all. So the Mac version uses the regular MuJoCo library, which runs on the processor, with one simulation per processor core.

Speed per core

The first step was to measure the physics alone. With the two robots locked together and 23 contact points active, one core of a MacBook Air (M3) ran 9,700 physics steps per second. That is about 485 policy steps per second per core, which is the ceiling for this task.

The first full version reached only 281 policy steps per second per core. A profile, which measures where a program spends its time, showed why. The physics took 39% of the time, but the motor model took 54%. The motor model runs 1,000 times per simulated second for each of 16 motors, and each run looked up several torque curves one at a time.

Two changes fixed most of this:

Training speed

A faster simulation does not speed up training on its own. Eight simulations on eight cores could produce about 2,500 steps per second, but training first reached only about half of that. The rest of the time went to passing data between the simulation processes and the trainer, and to the trainer's update step, during which the simulations sat idle.

Two training settings were changed.

Most of the gain came from the compiled motors. With all three changes, training on the MacBook Air went from 1,219 to 2,416 steps per second (+98%).

  1. Both Macs together4,540 steps/sabout 130% of the RTX 3060
  2. RTX 3060 laptop (reference)3,500 steps/sMJX with Warp, 1,024 matches
  3. MacBook Air M3, final2,416 steps/s69% of the RTX 3060
  4. Mac mini M2, final2,124 steps/s61% of the RTX 3060
  5. MacBook Air M3, first version1,219 steps/s35% of the RTX 3060

Mac, final setupMac, first versionDashed bar: the RTX 3060 reference

SetupPolicy steps per secondShare of the RTX 3060
RTX 3060 laptop, MJX with Warp3,500100%
MacBook Air M3, first version1,21935%
MacBook Air M3, compiled motors, 8 processes2,41669%
Mac mini M2, same setup2,12461%
Both Macs togetherabout 4,500about 130%

The MacBook Air numbers come from a clean test with nothing else running. Each run trained the same robot for 60,000 steps. The Mac mini was measured while it trained.

Matching results

The Mac version is a separate copy of the simulation, so it had to give the same results as the GPU version. Two checks confirmed this.

Trained controllers also move both ways between the two versions. A robot trained on the GPU can keep training on a Mac, and the result can fight in a GPU tournament. A round trip gives identical motor commands.

Batch size trap

The biggest problem had nothing to do with speed. With the trainer's standard settings, continued training on a Mac made a strong GPU-trained robot weaker. Its win rate against a fixed opponent fell from 64% to 52% after half a million steps (−12 points) and to 46% after a million (−18 points).

The cause was the amount of data per update. The GPU run collects about 32,000 policy steps from its 1,024 parallel matches before each update. The standard settings with eight Mac processes collected about 8,000, or roughly 64 bouts. Sumo rewards are mostly a win or a loss at the end of a bout, so 64 bouts give a noisy estimate of what helps. The trainer kept stepping in random directions.

Matching the GPU's data per update fixed it. Each update now uses about 32,000 policy steps, or roughly 260 bouts, with a lower learning rate. The same robot then improved from 64% to 69% after 2 million steps (+5 points).

The training statistics did not show the problem clearly, since they are noisy on their own. Separate test fights against fixed opponents, with no training rewards counted, showed it at once. Every Mac run since then has been checked that way.

Other lessons

Mac or GPU

For a detailed simulation of one or two robots, a recent Mac reaches between half and two thirds of the speed of a laptop graphics card, once the slow parts are compiled and the training settings match. Two Macs together are faster than the card. GPU training pulls further ahead for simpler robots and much larger batches. There, thousands of copies run at once on a single card. For a hobby project that already owns a Mac, the Mac is a useful second or third training machine, and it can be the only one.

Conclusion

At first, this kind of training looked impossible on a Mac, or at best extremely slow. The GPU tools would not run, and the first Mac version reached only a third of the graphics card's speed. Patience, measurement, and some trial and error changed that. A few targeted fixes doubled the speed, and careful checks showed the results match the GPU version. Fast, correct robot training does not require an NVIDIA graphics card.