3
Eg @ sj d Z ddlZddlmZ ddlmZ dd Zdd Zdd
dZ dd
Z
dd Zdd Zdd Z
dd ZdS )aJ muji.py does multi-gpu training for caffe2 with no need to change the c++
side code. Everything is defined on the computation graph level.
We support the following use cases:
- 2 gpus, where peer access is enabled between them.
- 4 gpus, where peer access are enabled between all of them.
- 4 gpus, where peer access are enabled in two groups,
between {1, 2} and {3, 4}
- 8 gpus, where peer access are enabled in two groups,
between {1, 2, 3, 4} and {5, 6, 7, 8}.
If above cases are not satisfied, a fallback function which does not rely on
peer access will be called.
N)
caffe2_pb2) workspacec C s t j }tj|_| |_|S )zVA utility function that returns a device option protobuf of the
specified gpu id.
)r DeviceOptionr Z
GpuDeviceTypedevice_typeZ device_id)Zgpu_id
device_option r >/usr/local/lib64/python3.6/site-packages/caffe2/python/muji.pyOnGPU s r c C s t j } t j| _| S )N)r r ZCPUr )r r r r OnCPU! s r
_reducedc C s |dkrt tt|}t|t|kr@tdt|t|f tj }t|dkr|jd dkrtj|ddddf rt | |||S t|dkr|jd dkrtj|ddddf rt
| |||S t|dkr0|jd dkr0tj|ddddf r0tj|ddddf r0t| |||S t|dkrx|jd dkrxtj|ddddf rxt| |||S t
| |||S dS )zThe general Allreduce interface that reroutes the function calls.
CPUs and AMD GPUs are not supported because
GetGpuPeerAccessPattern is called to get gpu peer access pattern.
Nz6gpu_indices length and blobs length mismatch: %d vs %d r )listrangelenRuntimeErrorr ZGetGpuPeerAccessPatternshapenpall
Allreduce2
Allreduce4Allreduce4Group2
Allreduce8AllreduceFallback)netblobs
reduced_affixgpu_indicespatternr r r Allreduce' s 44V:r c
C sL |\}}|\}}| j ||g|| t|d}|jg || t|d} || fS )zUAllreduce for 2 gpus.
Algorithm: 0r <- 0 + 1, 1r <- 0r, where r means "reduced"
)r )Addr Copy)
r r r r abgpu_agpu_b a_reduced b_reducedr r r r @ s r c C s |\}}}}|\}} }
}| j ||gt|| t|d}| j ||gt|| t|
d}
|j |
|t|d}|jg |
t|
d}
|jg t|| t| d}|
jg t|| t|d}|||
|fS )zAllreduce for 4 gpus.
Algorithm: 2 level reduction.
0r <- 0 + 1, 2r <- 2 + 3
0r <- 0r + 2r
2r <- 0r,
1r <- 0r, 3r <- 2r
)r )r! strr r" )r r r r r# r$ cdr% r&