-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathcgemm_helper.h
More file actions
190 lines (154 loc) · 5.94 KB
/
cgemm_helper.h
File metadata and controls
190 lines (154 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*************************************************************************
* Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* See LICENSE for license information.
************************************************************************/
#ifndef TRANSFORMER_ENGINE_JAX_CGEMM_HELPER_H_
#define TRANSFORMER_ENGINE_JAX_CGEMM_HELPER_H_
#include <nccl.h>
#include <unistd.h>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <functional>
#include <memory>
#include <thread>
#include <unordered_map>
#include "../extensions.h"
#include "common/comm_gemm_overlap/userbuffers/userbuffers.h"
#include "common/util/cuda_runtime.h"
#include "common/util/logging.h"
#include "transformer_engine/comm_gemm_overlap.h"
namespace transformer_engine {
namespace jax {
// Configuration singleton for CGEMM parameters
class CgemmConfig {
public:
int num_max_streams;
int gemm_priority;
int comm_priority;
int num_comm_sm;
bool use_ce;
bool aggregate_ag;
static void init(int _num_max_streams, int _gemm_priority, int _comm_priority, int _num_comm_sm,
bool _use_ce, bool _aggregate_ag) {
auto &config = get(false);
config._initialized = true;
config.num_max_streams = _num_max_streams;
config.gemm_priority = _gemm_priority;
config.comm_priority = _comm_priority;
config.num_comm_sm = _num_comm_sm;
config.use_ce = _use_ce;
config.aggregate_ag = _aggregate_ag;
}
static CgemmConfig &get(bool is_initialized = true) {
static thread_local CgemmConfig instance;
NVTE_CHECK(
instance._initialized == is_initialized,
"CgemmConfig must be initialized before using it, got is_initialized=", is_initialized);
return instance;
}
CgemmConfig(const CgemmConfig &) = delete;
CgemmConfig &operator=(const CgemmConfig &) = delete;
private:
CgemmConfig() = default;
~CgemmConfig() = default;
bool _initialized = false;
};
// Forward declaration
class CollectiveGemmPlanRegistry;
// NCCL communicator handler for collective GEMM operations
// Support both single process single device AND single process multi device
// Two scenarios:
// 1. Single process multiple devices: TP domain = process (num_devices_per_process == tp_size)
// 2. Single process single device: TP domain spans processes (num_devices_per_process == 1)
class CommunicatorHandler {
public:
int num_total_devices = -1;
int num_devices_per_process = -1;
int process_id = -1;
int num_processes = -1;
int tp_size = -1;
int tp_num_domains = -1;
std::vector<int> local_device_ids_within_tp_domain;
std::vector<int> tp_domain_ids;
std::vector<ncclComm_t> tp_comms;
std::vector<int> local_device_ids_within_process;
std::vector<int> global_device_ids;
int get_global_rank() const {
int device_idx = get_local_device_idx_for_current_device();
return global_device_ids[device_idx];
}
void nccl_device_barrier_impl(ExtComm);
void nccl_allgather_impl(void *output_buf, size_t output_bytes, void *input_buf,
size_t input_bytes, ExtComm);
ncclComm_t get_comm_for_current_device() const {
int device_idx = get_local_device_idx_for_current_device();
return tp_comms[device_idx];
}
int get_local_device_idx_for_current_device() const {
int current_device;
NVTE_CHECK_CUDA(cudaGetDevice(¤t_device));
for (int i = 0; i < num_devices_per_process; i++) {
if (local_device_ids_within_process[i] == current_device) {
return i;
}
}
NVTE_ERROR("Current CUDA device ", current_device,
" not found in local_device_ids_within_process");
}
int get_local_device_id_within_tp_domain() const {
int device_idx = get_local_device_idx_for_current_device();
return local_device_ids_within_tp_domain[device_idx];
}
int get_tp_domain_id() const {
int device_idx = get_local_device_idx_for_current_device();
return tp_domain_ids[device_idx];
}
int get_tp_num_domains() const { return tp_num_domains; }
static void init(int num_total_devices, int num_devices_per_process, int process_id, int tp_size);
private:
ncclUniqueId coordinate_nccl_unique_id(const std::string &id_type);
public:
static CommunicatorHandler &get(bool is_initialized = true) {
static CommunicatorHandler instance;
NVTE_CHECK(instance._initialize == is_initialized,
"CommunicatorHandler._initialize=", instance._initialize,
", is_initialized=", is_initialized);
return instance;
}
ExtAllgatherOp allgather_func;
ExtBarrierOp barrier_func;
CommunicatorHandler(const CommunicatorHandler &) = delete;
CommunicatorHandler &operator=(const CommunicatorHandler &) = delete;
private:
CommunicatorHandler();
~CommunicatorHandler();
bool _initialize = false;
int *_device_barrier = nullptr;
std::vector<std::string> _nccl_id_file_name;
};
// Plan registry for caching collective GEMM executors
class CollectiveGemmPlanRegistry {
public:
static CollectiveGemmPlanRegistry &getInstance() {
static thread_local CollectiveGemmPlanRegistry instance;
return instance;
}
CommOverlapCore *get_executor(std::vector<size_t> buffer_shape, DType dtype,
JAXX_Collective_Op collective_op);
private:
CollectiveGemmPlanRegistry() {}
CollectiveGemmPlanRegistry(const CollectiveGemmPlanRegistry &) = delete;
CollectiveGemmPlanRegistry &operator=(const CollectiveGemmPlanRegistry &) = delete;
std::unordered_map<int64_t, std::unique_ptr<CommOverlapCore>> plan_map;
};
// Function declarations
void InitializeCgemmCommunicator(int num_total_devices, int num_devices_per_process, int process_id,
int tp_size, int num_max_streams, int gemm_priority,
int comm_priority, int num_comm_sm, bool use_ce,
bool aggregate_ag);
int GetCgemmNumMaxStreams();
} // namespace jax
} // namespace transformer_engine
#endif // TRANSFORMER_ENGINE_JAX_CGEMM_HELPER_H_