NCCLTools.cc 5.5 KB
Newer Older
L
lilong12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/distributed/collective/NCCLTools.h"
16

L
lilong12 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
#include "paddle/fluid/distributed/collective/Types.h"

namespace paddle {
namespace distributed {

ncclRedOp_t ToNCCLRedType(ReduceOp reduction) {
  static const std::map<ReduceOp, ncclRedOp_t> red_type = {
      {ReduceOp::MIN, ncclMin},
      {ReduceOp::MAX, ncclMax},
      {ReduceOp::SUM, ncclSum},
      {ReduceOp::PRODUCT, ncclProd},
  };
  auto it = red_type.find(reduction);
30 31
  PADDLE_ENFORCE_EQ(it != red_type.end(),
                    true,
L
lilong12 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
                    platform::errors::InvalidArgument(
                        "Invalid nccl reduction. Must be ncclMin | ncclMax | "
                        "ncclProd | ncclSum"));
  return it->second;
}

std::string SerializeNCCLUniqueId(const ncclUniqueId& ncclID) {
  const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&ncclID);
  std::ostringstream oss;
  for (auto i = 0; i < NCCL_UNIQUE_ID_BYTES; ++i) {
    oss << std::hex << static_cast<int>(bytes[i]);
  }
  return oss.str();
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void StaticCheckTensor(const phi::DenseTensor& tensor,
                       int rank,
                       int world_size) {
  // place check
  PADDLE_ENFORCE_EQ(
      platform::is_gpu_place(tensor.place()),
      true,
      platform::errors::InvalidArgument("Tensor should be in GPU place."));
  // rank check
  PADDLE_ENFORCE_GE(rank,
                    0,
                    platform::errors::InvalidArgument(
                        "Rank should be greater than or equal to 0."));
  PADDLE_ENFORCE_LT(
      rank,
      world_size,
      platform::errors::InvalidArgument("Rank is out of the process group."));
}

// static check for collective
void StaticCheckTensors(const phi::DenseTensor& out_tensor,
                        const phi::DenseTensor& in_tensor,
                        int rank,
                        int world_size,
                        int out_size_factor,
                        int in_size_factor) {
  // place check
  PADDLE_ENFORCE_EQ(platform::is_gpu_place(out_tensor.place()),
                    true,
                    platform::errors::InvalidArgument(
                        "Output tensor should be in GPU place."));
  PADDLE_ENFORCE_EQ(platform::is_gpu_place(in_tensor.place()),
                    true,
                    platform::errors::InvalidArgument(
                        "Input tensor should be in GPU place."));
  // rank check
  PADDLE_ENFORCE_GE(rank,
                    0,
                    platform::errors::InvalidArgument(
                        "Rank should be greater than or equal to 0."));
  PADDLE_ENFORCE_LT(
      rank,
      world_size,
      platform::errors::InvalidArgument("Rank is out of the process group."));
  // shape check
  int64_t out_size = out_tensor.numel();
  PADDLE_ENFORCE_GT(out_size,
                    0,
                    platform::errors::InvalidArgument(
                        "Size of output tensor should be greater than 0."));
  int64_t in_size = in_tensor.numel();
  PADDLE_ENFORCE_GT(in_size,
                    0,
                    platform::errors::InvalidArgument(
                        "Size of input tensor should be greater than 0."));
  PADDLE_ENFORCE_EQ(
      out_size * out_size_factor,
      in_size * in_size_factor,
      platform::errors::InvalidArgument(
          "Input and output tensors should have matching sizes."));
  // dtype check
  PADDLE_ENFORCE_EQ(
      out_tensor.dtype(),
      in_tensor.dtype(),
      platform::errors::InvalidArgument(
          "Input and output tensors should have the same data type."));
}

void StaticCheckTensorsSameShape(const phi::DenseTensor& out_tensor,
                                 const phi::DenseTensor& in_tensor,
                                 int rank,
                                 int world_size) {
  StaticCheckTensors(out_tensor,
                     in_tensor,
                     rank,
                     world_size,
                     /*out_size_factor*/ 1,
                     /*in_size_factor*/ 1);
}

void StaticCheckTensorsScatterLikeShape(const phi::DenseTensor& out_tensor,
                                        const phi::DenseTensor& in_tensor,
                                        int rank,
                                        int world_size) {
  StaticCheckTensors(out_tensor,
                     in_tensor,
                     rank,
                     world_size,
                     /*out_size_factor*/ world_size,
                     /*in_size_factor*/ 1);
}

void StaticCheckTensorsGatherLikeShape(const phi::DenseTensor& out_tensor,
                                       const phi::DenseTensor& in_tensor,
                                       int rank,
                                       int world_size) {
  StaticCheckTensors(out_tensor,
                     in_tensor,
                     rank,
                     world_size,
                     /*out_size_factor*/ 1,
                     /*in_size_factor*/ world_size);
}

L
lilong12 已提交
151 152
}  //  namespace distributed
}  //  namespace paddle