test_ds_init.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. import os
  5. import torch
  6. import deepspeed
  7. from deepspeed.accelerator import get_accelerator
  8. class OneLayerNet(torch.nn.Module):
  9. def __init__(self, D_in, D_out):
  10. """
  11. In the constructor we instantiate two nn.Linear modules and assign them as
  12. member variables.
  13. """
  14. super(OneLayerNet, self).__init__()
  15. self.linear1 = torch.nn.Linear(D_in, D_out)
  16. def forward(self, x):
  17. """
  18. In the forward function we accept a Variable of input data and we must return
  19. a Variable of output data. We can use Modules defined in the constructor as
  20. well as arbitrary operators on Variables.
  21. """
  22. h_relu = self.linear1(x).clamp(min=0)
  23. y_pred = self.linear1(h_relu)
  24. return y_pred
  25. def test_literal_device():
  26. model = OneLayerNet(128, 128)
  27. os.environ['RANK'] = '0'
  28. os.environ['WORLD_SIZE'] = '1'
  29. os.environ['MASTER_ADDR'] = '127.0.0.1'
  30. os.environ['MASTER_PORT'] = '8088'
  31. os.environ['LOCAL_RANK'] = '0'
  32. deepspeed.init_distributed(get_accelerator().communication_backend_name())
  33. deepspeed.initialize(model=model, config='ds_config.json')
  34. string = get_accelerator().device_name() #'xpu' or 'cuda'
  35. string0 = get_accelerator().device_name(0) #'xpu:0' or 'cuda:0'
  36. string1 = get_accelerator().device_name(1) #'xpu:1' or 'cuda:1'
  37. assert string == 'xpu' or string == 'cuda'
  38. assert string0 == 'xpu:0' or string0 == 'cuda:0'
  39. assert string1 == 'xpu:1' or string1 == 'cuda:1'