|
| 1 | +# Copyright 2020 - 2021 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import horovod.torch as hvd |
| 13 | +import torch |
| 14 | + |
| 15 | +from monai.utils import evenly_divisible_all_gather |
| 16 | + |
| 17 | + |
| 18 | +class HvdEvenlyDivisibleAllGather: |
| 19 | + def test_data(self): |
| 20 | + # initialize Horovod |
| 21 | + hvd.init() |
| 22 | + if torch.cuda.is_available(): |
| 23 | + torch.cuda.set_device(hvd.local_rank()) |
| 24 | + self._run() |
| 25 | + |
| 26 | + def _run(self): |
| 27 | + if hvd.rank() == 0: |
| 28 | + data1 = torch.tensor([[1, 2], [3, 4]]) |
| 29 | + data2 = torch.tensor([[1.0, 2.0]]) |
| 30 | + data3 = torch.tensor(7) |
| 31 | + |
| 32 | + if hvd.rank() == 1: |
| 33 | + data1 = torch.tensor([[5, 6]]) |
| 34 | + data2 = torch.tensor([[3.0, 4.0], [5.0, 6.0]]) |
| 35 | + data3 = torch.tensor(8) |
| 36 | + |
| 37 | + result1 = evenly_divisible_all_gather(data=data1, concat=True) |
| 38 | + torch.testing.assert_allclose(result1, torch.tensor([[1, 2], [3, 4], [5, 6]])) |
| 39 | + result2 = evenly_divisible_all_gather(data=data2, concat=False) |
| 40 | + for r, e in zip(result2, [torch.tensor([[1.0, 2.0]]), torch.tensor([[3.0, 4.0], [5.0, 6.0]])]): |
| 41 | + torch.testing.assert_allclose(r, e) |
| 42 | + result3 = evenly_divisible_all_gather(data=data3, concat=False) |
| 43 | + for r in result3: |
| 44 | + torch.testing.assert_allclose(r.ndimension(), 0) |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + """ |
| 49 | + 1. Install Horovod: |
| 50 | + `HOROVOD_NCCL_INCLUDE=/usr/include HOROVOD_NCCL_LIB=/usr/lib/x86_64-linux-gnu HOROVOD_GPU_OPERATIONS=NCCL \ |
| 51 | + HOROVOD_NCCL_LINK=SHARED pip install --no-cache-dir horovod` |
| 52 | +
|
| 53 | + 2. Execute on 2 GPUs in a single machine: |
| 54 | + `horovodrun -np 2 python test_evenly_divisible_all_gather_hvd.py` |
| 55 | +
|
| 56 | + """ |
| 57 | + HvdEvenlyDivisibleAllGather().test_data() |
0 commit comments