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:
- Batched lookups. All the torque curves went into one table, read for every motor at once. This raised the speed from 281 to 357 steps per second (+27%).
- Compiled inner loop. The motor model and the 20 physics steps of each policy step moved into one small compiled routine, called once per policy step. The slow scripting layer now runs 50 times per simulated second, down from 1,000. This raised the speed to 470 steps per second (+32%). Its results match the original to about 15 decimal places.
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.
- Eight simulation processes. The first version ran seven, one per core with a core left for the trainer. Eight keeps every core busy while the trainer waits for data.
- Six trainer threads. The trainer's update step used one thread at first. With six, the update finishes sooner, so the simulations sit idle for less time. This setting alone added about 2%.
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%).
- Both Macs together4,540 steps/sabout 130% of the RTX 3060
- RTX 3060 laptop (reference)3,500 steps/sMJX with Warp, 1,024 matches
- MacBook Air M3, final2,416 steps/s69% of the RTX 3060
- Mac mini M2, final2,124 steps/s61% of the RTX 3060
- MacBook Air M3, first version1,219 steps/s35% of the RTX 3060
Mac, final setupMac, first versionDashed bar: the RTX 3060 reference
| Setup | Policy steps per second | Share of the RTX 3060 |
|---|---|---|
| RTX 3060 laptop, MJX with Warp | 3,500 | 100% |
| MacBook Air M3, first version | 1,219 | 35% |
| MacBook Air M3, compiled motors, 8 processes | 2,416 | 69% |
| Mac mini M2, same setup | 2,124 | 61% |
| Both Macs together | about 4,500 | about 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.
- Same robot, same inputs. A trained robot's controller gave the same motor commands in both versions, to within 0.0003%.
- Same fights. The same two trained robots fought 48 bouts in each version. On the GPU the newer robot won 55%, lost 43% and drew 2%. On the Mac it won 54%, lost 38% and drew 8%. Small differences like these are expected, because two robots pushing against each other form a chaotic system. In a chaotic system, a tiny change in the starting state grows quickly until the outcome is different. The GPU version stores numbers with about 7 significant digits and the Mac version with about 16, so the two start to differ around the seventh digit. Within a second of contact, that is enough to change which paw slips first, and the bout can end another way. Single bouts can differ, while the win rates over many bouts stay close.
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
- Data files and worker processes. A data file opened once and then shared by all simulation processes gave corrupted reads. Each process now loads its own full copy at startup.
- Bonus farming. Small rewards for single moves, such as a hit or a hop, got repeated many times a second until they outweighed winning. Paying most of the bonus only when the move leads to a win within a second fixed this. The same problem appears on the GPU.
- Several machines. Each training job is independent, so the RTX 3060 laptop and both Macs trained different fighting styles at the same time. The Macs trained five of the 13 styles in the final tournament, including its top three seeds.
- Memory. Eight simulation processes and the trainer fit in the 8 GB of the Mac mini.
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.