test_cross_entropy.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from commons import set_random_seed
  16. from commons import IdentityLayer
  17. from commons import print_separator
  18. from commons import initialize_distributed
  19. from mpu.cross_entropy import vocab_parallel_cross_entropy
  20. import mpu
  21. import torch.nn.functional as F
  22. import torch
  23. import random
  24. import sys
  25. sys.path.append("../..")
  26. def torch_cross_entropy(batch_size, seq_length, vocab_size,
  27. logits_scale, seed):
  28. set_random_seed(seed)
  29. identity = IdentityLayer((batch_size, seq_length, vocab_size),
  30. scale=logits_scale).cuda()
  31. logits = identity()
  32. target = torch.cuda.LongTensor(
  33. size=(batch_size, seq_length)).random_(0, vocab_size)
  34. loss = F.cross_entropy(logits.view(-1, logits.size()[-1]),
  35. target.view(-1),
  36. reduction='none').view_as(target).mean()
  37. loss.backward()
  38. return loss, identity.weight.grad
  39. def mpu_cross_entropy(batch_size, seq_length, vocab_size,
  40. logits_scale, seed):
  41. set_random_seed(seed)
  42. identity = IdentityLayer((batch_size, seq_length, vocab_size),
  43. scale=logits_scale).cuda()
  44. logits = identity()
  45. logits_parallel = mpu.scatter_to_tensor_model_parallel_region(logits)
  46. target = torch.cuda.LongTensor(
  47. size=(batch_size, seq_length)).random_(0, vocab_size)
  48. loss = vocab_parallel_cross_entropy(logits_parallel, target).mean()
  49. loss.backward()
  50. return loss, identity.weight.grad
  51. def test_cross_entropy(tensor_model_parallel_size):
  52. if torch.distributed.get_rank() == 0:
  53. print('> testing cross entropy with model parallel size {} ...'.
  54. format(tensor_model_parallel_size))
  55. mpu.initialize_model_parallel(tensor_model_parallel_size)
  56. tensor_model_parallel_size = mpu.get_tensor_model_parallel_world_size()
  57. batch_size = 13
  58. seq_length = 17
  59. vocab_size_per_partition = 11
  60. logits_scale = 1000.0
  61. vocab_size = vocab_size_per_partition * tensor_model_parallel_size
  62. seed = 1234
  63. loss_torch, grad_torch = torch_cross_entropy(batch_size, seq_length,
  64. vocab_size, logits_scale,
  65. seed)
  66. loss_mpu, grad_mpu = mpu_cross_entropy(batch_size, seq_length,
  67. vocab_size, logits_scale,
  68. seed)
  69. error = loss_torch.sub_(loss_mpu).abs().max()
  70. print(' max error in loss on global rank {}: {}'.format(
  71. torch.distributed.get_rank(), error))
  72. assert error < 1.0e-6
  73. error = grad_torch.sub_(grad_mpu).abs().max()
  74. print(' max error in grad on global rank {}: {}'.format(
  75. torch.distributed.get_rank(), error))
  76. assert error < 1.0e-6
  77. # Reset groups
  78. mpu.destroy_tensor_model_parallel()
  79. torch.distributed.barrier()
  80. if torch.distributed.get_rank() == 0:
  81. print('>> passed the test :-)')
  82. if __name__ == '__main__':
  83. initialize_distributed()
  84. world_size = torch.distributed.get_world_size()
  85. tensor_model_parallel_size = 1
  86. while tensor_model_parallel_size <= world_size:
  87. print_separator('test cross entropy')
  88. test_cross_entropy(tensor_model_parallel_size)
  89. tensor_model_parallel_size *= 2