In Distributed Data Parallel (DDP) training, each worker process computes gradients on its own mini-batch shard. Before the optimizer step, gradients must be synchronized across all workers so every replica applies the same update. This is done via an all-reduce operation that averages the gradients element-wise across workers.
Implement a function all_reduce_mean(worker_grads) that simulates this synchronization step. The input worker_grads is a list of length N (number of workers), where each element is itself a list of numpy arrays representing the gradients for each parameter tensor on that worker. All workers have the same parameter shapes.
The function should return a single list of numpy arrays (converted to nested Python lists via .tolist()) where each parameter's gradient is the element-wise mean across all workers. Round every value in the output to 6 decimal places.
If the input list is empty, return an empty list. If any worker has mismatched shapes for a parameter compared to worker 0, raise a ValueError.