-
Notifications
You must be signed in to change notification settings - Fork 9
/
hccl_demo.cpp
2390 lines (2166 loc) · 100 KB
/
hccl_demo.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
# Copyright (c) 2022 Habana Labs, Ltd.
# SPDX-License-Identifier: Apache-2.0
******************************************************************************/
// C++ Standard Libraries
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <exception>
#include <vector>
#include <cstdlib> // for getenv, mkstemp
#include <chrono> // for Bandwidth calculation
#include <iomanip> // for setprecision
#include <unistd.h>
#include <functional>
#include <algorithm>
#include <sstream>
#include <numeric>
#include <fstream>
#include <cmath> // for pow
#include <cstring> // for std::memcopy
// HCCL :: Habana Collective Communications Library
#include <hccl.h>
// Synapse :: Habana Synapse training API
#include <synapse_api.h>
#if AFFINITY_ENABLED
#include "affinity.h"
#endif
#define DATA_ELEMENTS_MAX 13
#define DEFAULT_TEST_SIZE 33554432
#define DEFAULT_TEST_SIZE_RANGE_MIN 0
#define DEFAULT_TEST_SIZE_RANGE_MAX 0
#define DEFAULT_TEST_SIZE_RANGE_INC 1
#define DEFAULT_TEST_LOOP 10
#define DEFAULT_BOX_SIZE 8
#define ALLOCATED_HBM_SIZE (2UL * 1024 * 1024 * 1024) // 2G
#define AMOUNT_JUMBO_BUFFERS (2)
#define MAX_BUFFER_COUNT (33UL)
constexpr int INVALID_RANK = -1;
constexpr int master_mpi_rank = 0;
// scale_test specific constants
constexpr float SCALE_VALIDATION_MARGIN = 0.05; // fraction of expected BW
enum class CONTROL_TYPE : uint8_t
{
SEND = 1,
RECEIVE,
END,
};
#if MPI_ENABLED
// Open MPI (v4.0.2)
#include <mpi.h>
#define CHECK_MPI_STATUS(x) \
{ \
const auto _res = (x); \
if (_res != MPI_SUCCESS) \
throw std::runtime_error {"In function " + std::string {__FUNCTION__} + \
"(): " #x " failed with code: " + std::to_string(_res)}; \
}
#endif //MPI_ENABLED
using namespace std;
using Clock = chrono::high_resolution_clock;
#define CHECK_HCCL_STATUS(x) \
{ \
const auto _res = (x); \
if (_res != hcclSuccess) \
throw runtime_error {"In function " + string {__FUNCTION__} + \
"(): " #x " failed: " + hcclGetErrorString(_res)}; \
}
#define CHECK_SYNAPSE_STATUS(x) \
{ \
const auto _res = (x); \
if (_res != synSuccess) \
throw runtime_error {"In function " + string {__FUNCTION__} + \
"(): " #x " failed with synapse error: " + to_string((_res))}; \
}
#define ASSERT(x) \
do \
{ \
if (!(x)) throw runtime_error {"In function " + string {__FUNCTION__} + " assertion failed"}; \
} while (false)
inline uint16_t floatToBf16(const float f)
{
return ((*(const uint32_t*) &f) >> 16) & 0xffff;
}
inline float bf16ToFloat(const uint16_t a)
{
float val_fp32;
const uint32_t val_32b = ((uint32_t) a) << 16;
static_assert(sizeof(float) == sizeof(val_32b), "`float` size is incompatible!");
std::memcpy(&val_fp32, &val_32b, sizeof(float));
return val_fp32;
}
inline float bf16AccuracyCoefficient(unsigned numberOfRanks)
{
numberOfRanks = (numberOfRanks > 8) ? 8 : numberOfRanks;
return (numberOfRanks > 1) ? (float)numberOfRanks / 256.0 : 0.0; // For 1 rank, tolerance should be 0
}
template<class T>
float get_float(T value);
template<>
float get_float<float>(float value){
return value;
}
template<>
float get_float<uint16_t>(uint16_t value){
return bf16ToFloat(value);
}
struct hccl_demo_data
{
synDeviceId device_handle;
synStreamHandle device_to_host_stream;
synStreamHandle host_to_device_stream;
synStreamHandle collective_stream;
size_t nranks;
hcclComm_t hccl_comm;
hcclDataType_t hccl_data_type;
std::string str_data_type;
size_t num_iters;
std::string ranks_list;
int hccl_rank;
int mpi_root_rank;
uint64_t usable_memory;
};
struct hccl_demo_stats
{
float rank_duration_in_sec;
size_t num_iters;
};
struct hccl_demo_report_entry
{
uint64_t data_size;
uint64_t count;
float time;
double algo_bw;
double avg_bw;
string data_type;
string reduction_op;
};
vector<hccl_demo_report_entry> report_entry_vec;
ostream& log()
{
return cout;
}
bool correctness_check_function(hccl_demo_data demo_data, float expected, float out_value, int i)
{
const float accuracyCoefficient = bf16AccuracyCoefficient(demo_data.nranks);
const float tolerance = fabs(out_value) * accuracyCoefficient;
const float difference = fabs(out_value - expected);
if (difference > tolerance)
{
log() << "index=" << i << ", expectedValue=" << expected << ", value=" << out_value
<< ", thisRankId=" << demo_data.hccl_rank << ", tolerance=" << tolerance << ", difference=" << difference
<< ", accuracyCoefficient=" << accuracyCoefficient << ", m_numberOfRanks=" << demo_data.nranks
<< std::endl;
return false;
}
return true;
}
uint64_t get_data_type_size(const string& data_type)
{
uint64_t data_size = sizeof(float);
if (data_type == "float")
{
data_size = sizeof(float);
}
else if (data_type == "bfloat16")
{
data_size = sizeof(uint16_t);
}
return data_size;
}
hccl_demo_stats benchmark(hccl_demo_data& demo_data, const function<void(uint64_t)>& fn, const std::function<void()>& fn_correctness){
hccl_demo_stats stat;
// Run a single iteration for warmup to sync all the gaudis on the device.
fn(0);
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
// Actual iterations
auto start_time = Clock::now();
for (size_t iter = 1; iter < demo_data.num_iters; ++iter)
{
fn(iter);
}
// Correctness run on separate device output buffer
fn_correctness();
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
auto duration = Clock::now() - start_time;
stat.rank_duration_in_sec = chrono::duration_cast<chrono::duration<double>>(duration).count();
stat.rank_duration_in_sec = stat.rank_duration_in_sec / demo_data.num_iters;
return stat;
}
bool check_correctness()
{
static const auto default_hccl_check_correctness = true;
const char* env_value = getenv("HCCL_DEMO_CHECK_CORRECTNESS");
return (env_value != nullptr) ? atoi(env_value) : default_hccl_check_correctness;
}
bool should_report_stat(int rank)
{
return rank == 0;
}
inline string format_bw(const double bytes_per_sec)
{
stringstream ss;
ss << fixed << setprecision(6) << bytes_per_sec / 1e9 << " GB/s";
return ss.str();
}
string get_print_delimiter(size_t length, char delimiter)
{
stringstream ss;
for (size_t i = 0; i < length; i++)
{
ss << delimiter;
}
return ss.str();
}
string get_demo_test_type()
{
static bool is_cached = false;
static auto test_type = string {"broadcast"};
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST");
test_type = (env_value != nullptr) ? string(env_value) : test_type;
is_cached = true;
}
return test_type;
}
hcclDataType_t get_demo_hccl_data_type()
{
static bool is_cached = false;
static hcclDataType_t hccl_data_type = hcclFloat32;
if (!is_cached)
{
char* env_value = getenv("HCCL_DATA_TYPE");
if (env_value == nullptr || string(env_value) == "float")
{
hccl_data_type = hcclFloat32;
}
else if (string(env_value) == "bfloat16")
{
hccl_data_type = hcclBfloat16;
}
is_cached = true;
}
return hccl_data_type;
}
std::string get_demo_str_data_type()
{
static const string default_data_type = "float";
char* env_value = getenv("HCCL_DATA_TYPE");
return (env_value != nullptr) ? string(env_value) : default_data_type;
}
int get_demo_ranks_per_node()
{
static bool is_cached = false;
static auto ranks_per_node = DEFAULT_BOX_SIZE;
if (!is_cached)
{
#if MPI_ENABLED
char* env_value = getenv("OMPI_COMM_WORLD_LOCAL_SIZE");
#else
char* env_value = getenv("HCCL_RANKS_PER_NODE");
#endif
ranks_per_node = (env_value != nullptr) ? atoi(env_value) : ranks_per_node;
is_cached = true;
}
return ranks_per_node;
}
int get_demo_scaleup_group_size()
{
static bool is_cached = false;
static auto scaleup_group_size = 0;
if (!is_cached)
{
char* env_value = getenv("HCCL_SCALEUP_GROUP_SIZE"); // Allow override for both MPI and non-MPI
if (env_value == nullptr)
{
// get default value from MPI if possible or use ranks_per_node as default
env_value = getenv("OMPI_COMM_WORLD_LOCAL_SIZE");
}
scaleup_group_size = (env_value != nullptr) ? atoi(env_value) : get_demo_ranks_per_node();
is_cached = true;
}
return scaleup_group_size;
}
int get_demo_test_root()
{
static bool is_cached = false;
static auto test_root = 0;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_ROOT");
test_root = (env_value != nullptr) ? atoi(env_value) : test_root;
is_cached = true;
}
return test_root;
}
uint64_t get_demo_test_size()
{
static bool is_cached = false;
static uint64_t test_size = DEFAULT_TEST_SIZE;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_SIZE");
test_size = (env_value != nullptr) ? strtoull(env_value, NULL, 0) : test_size;
is_cached = true;
}
return test_size;
}
uint64_t get_demo_size_min()
{
static bool is_cached = false;
static uint64_t test_size_min = DEFAULT_TEST_SIZE_RANGE_MIN;
if (!is_cached)
{
char* env_value = getenv("HCCL_SIZE_RANGE_MIN");
test_size_min = (env_value != nullptr) ? strtoull(env_value, NULL, 0) : test_size_min;
is_cached = true;
}
return test_size_min;
}
uint64_t get_demo_size_max()
{
static bool is_cached = false;
static uint64_t test_size_max = DEFAULT_TEST_SIZE_RANGE_MAX;
if (!is_cached)
{
char* env_value = getenv("HCCL_SIZE_RANGE_MAX");
test_size_max = (env_value != nullptr) ? strtoull(env_value, NULL, 0) : test_size_max;
is_cached = true;
}
return test_size_max;
}
uint64_t get_demo_size_inc()
{
static bool is_cached = false;
static uint64_t test_size_inc = DEFAULT_TEST_SIZE_RANGE_INC;
if (!is_cached)
{
char* env_value = getenv("HCCL_SIZE_RANGE_INC");
test_size_inc = (env_value != nullptr) ? strtoull(env_value, NULL, 0) : test_size_inc;
is_cached = true;
}
return test_size_inc;
}
bool is_master_rank(const int hccl_rank)
{
return hccl_rank == master_mpi_rank;
}
bool is_master_rank_unique_id(hccl_demo_data demo_data, const int hccl_rank)
{
return hccl_rank == demo_data.mpi_root_rank;
}
bool should_write_report(const int hccl_rank)
{
static bool is_cached = false;
static bool should_write_report = false;
if (!is_cached)
{
should_write_report = is_master_rank(hccl_rank) && get_demo_size_min() > 0 && get_demo_size_max() > 0;
is_cached = true;
}
return should_write_report;
}
int get_demo_test_loop()
{
static bool is_cached = false;
static auto test_loop = DEFAULT_TEST_LOOP;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_LOOP");
test_loop = (env_value != nullptr) ? atoi(env_value) : test_loop;
is_cached = true;
}
return test_loop;
}
string get_demo_csv_path()
{
static bool is_cached = false;
static auto csv_path = string {""};
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_CSV_PATH");
csv_path = (env_value != nullptr) ? string(env_value) : csv_path;
is_cached = true;
}
return csv_path;
}
string get_demo_custom_comm()
{
static string custom_comm = string {""};
char* env_value = getenv("HCCL_DEMO_CUSTOM_COMM");
custom_comm = (env_value != nullptr) ? string(env_value) : custom_comm;
return custom_comm;
}
synDeviceType get_device_type(const synDeviceId deviceId)
{
synDeviceInfoV2 deviceInfo;
CHECK_SYNAPSE_STATUS(synDeviceGetInfoV2(deviceId, &deviceInfo));
return deviceInfo.deviceType;
}
uint64_t get_demo_expected_scaleup_bw(const synDeviceId deviceId)
{
synDeviceType deviceType = get_device_type(deviceId);
switch (deviceType)
{
case synDeviceGaudi: return 12.5e9;
case synDeviceGaudi2: return 37.5e9;
case synDeviceGaudi3: return 75e9;
default:
log() << "Unknown device, setting expected scaleup bandwidth to 37.5GB" << endl;
return 37.5e9;
}
}
uint64_t get_demo_expected_scaleout_bw()
{
static bool is_cached = false;
static uint64_t test_expected_bw = 0;
if (!is_cached)
{
const char* const env_value = getenv("HCCL_EXPECTED_SCALEOUT_BW");
if (env_value == nullptr)
{
throw std::runtime_error {"missing mandatory argument for scale validation: --scaleout_bw"};
}
test_expected_bw = strtoull(env_value, NULL, 0);
is_cached = true;
}
return test_expected_bw;
}
inline void ParseCustomCommEnv(string rank_list, vector<int>& parsed_rank_list)
{
string delimiter = ",";
size_t pos = 0;
while ((pos = rank_list.find(delimiter)) != string::npos)
{
parsed_rank_list.push_back(stoi(rank_list.substr(0, pos)));
rank_list.erase(0, pos + delimiter.length());
}
if (!rank_list.empty())
{
parsed_rank_list.push_back(stoi(rank_list));
}
sort(parsed_rank_list.begin(), parsed_rank_list.end());
return;
}
bool buildCustomComm(hccl_demo_data* demo_data)
{
string rank_list = get_demo_custom_comm();
if (rank_list.size() == 0)
{
// Generate HCCL comm world
return true;
}
vector<int> peers;
ParseCustomCommEnv(rank_list, peers);
demo_data->mpi_root_rank = *peers.begin();
if (find(peers.begin(), peers.end(), demo_data->hccl_rank) != peers.end())
{
vector<int>::iterator it = find(peers.begin(), peers.end(), demo_data->hccl_rank);
// Override params to match new custom comm
demo_data->hccl_rank = distance(peers.begin(), it);
demo_data->nranks = peers.size();
return true;
}
return false;
}
int get_nranks()
{
#if MPI_ENABLED
int mpi_size {};
CHECK_MPI_STATUS(MPI_Comm_size(MPI_COMM_WORLD, &mpi_size));
return mpi_size;
#endif // MPI_ENABLED
static bool is_cached = false;
static auto test_nranks = 0;
if (!is_cached)
{
char* env_value = getenv("HCCL_NRANKS");
test_nranks = (env_value != nullptr) ? atoi(env_value) : test_nranks;
is_cached = true;
}
return test_nranks;
}
int verify_mpi_configuration()
{
bool mpi_enabled = false;
#if MPI_ENABLED
mpi_enabled = true;
#endif // MPI_ENABLED
static auto mpi_requested = false;
char* env_value = getenv("HCCL_DEMO_MPI_REQUESTED");
mpi_requested = (env_value != nullptr) ? atoi(env_value) : mpi_requested;
return mpi_requested ^ mpi_enabled;
}
int get_hccl_rank()
{
#if MPI_ENABLED
int mpi_rank {};
CHECK_MPI_STATUS(MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank));
return mpi_rank;
#endif // MPI_ENABLED
static bool is_cached = false;
static auto test_rank = -1;
if (!is_cached)
{
char* env_value = getenv("HCCL_RANK");
test_rank = (env_value != nullptr) ? atoi(env_value) : test_rank;
is_cached = true;
}
return test_rank;
}
std::string get_ranks_list()
{
static const string default_ranks_list = string {""};
const char* env_value = getenv("HCCL_RANKS_LIST");
return (env_value != nullptr) ? string(env_value) : default_ranks_list;
}
std::string get_reduction_op_str()
{
const char* env_value = getenv("HCCL_REDUCTION_OP");
if (env_value != nullptr) return string(env_value);
throw std::runtime_error {" Unknown reduction op."};
}
hcclRedOp_t get_reduction_op()
{
const char* env_value = getenv("HCCL_REDUCTION_OP");
if (0 == strcmp(env_value, "sum")) return hcclSum;
if (0 == strcmp(env_value, "min")) return hcclMin;
if (0 == strcmp(env_value, "max")) return hcclMax;
throw std::runtime_error {" Unknown reduction op."};
}
uint64_t get_usable_memory(const synDeviceId device_id)
{
uint64_t free_memory = 0;
uint64_t total_memory = 0; // Value is required by synDeviceGetMemoryInfo but not used
CHECK_SYNAPSE_STATUS(synDeviceGetMemoryInfo(device_id, &free_memory, &total_memory));
return free_memory;
}
void print_report(const string& collective_op, const size_t num_iters)
{
constexpr size_t column_width = 14;
const bool is_reduction_op =
collective_op.find("reduce") != std::string::npos; // reduce, all_reduce, reduce_scatter
const static vector<string> header = {"size", "count", "type", "redop", "time", "algo_bw", "nw_bw"};
const static vector<string> units = {"(B)", "(elements)", "", "", "(us)", "(GB/s)", "(GB/s)"};
stringstream ss;
const string summary = "[SUMMARY REPORT]";
const string stat_name = "(src!=dst, collective=" + collective_op + ", iterations=" + to_string(num_iters) + ")";
size_t delimiter_size = stat_name.length() + 1;
ss << '\n' << get_print_delimiter(delimiter_size, '#') << endl;
ss << summary << '\n' << stat_name << '\n' << endl;
ss << left;
// print header
for (size_t i = 0; i < header.size(); ++i)
{
if (!is_reduction_op && header[i] == "redop") continue;
ss << setw(column_width) << header[i];
}
ss << endl;
// print units
for (size_t i = 0; i < units.size(); ++i)
{
if (!is_reduction_op && header[i] == "redop") continue;
ss << setw(column_width) << units[i];
}
ss << endl;
// print stats for each data size
for (const auto& entry : report_entry_vec)
{
ss << setw(column_width) << to_string(entry.data_size) << setw(column_width) << to_string(entry.count)
<< setw(column_width) << entry.data_type;
if (is_reduction_op )
{
ss << setw(column_width) << entry.reduction_op;
}
ss << setw(column_width) << fixed << setprecision(3) << entry.time * 1000 << setw(column_width) << fixed
<< setprecision(6) << entry.algo_bw / 1e9 << setw(column_width) << fixed << setprecision(6)
<< entry.avg_bw / 1e9 << endl;
}
log() << ss.str();
}
string create_data_csv_file(hccl_demo_data demo_data,string test_type, string type){
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream datetime;
datetime << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d_%H%M");
string file_name = "HCCL_demo_" + test_type + "_" + type + "_" + to_string(demo_data.hccl_rank) + "_" + datetime.str() + ".csv";
fstream fout;
fout.open(file_name, ios::out | ios::app);
fout.close();
return file_name;
}
template<class T>
void copy_data_to_csv_report(vector<T> host_data,int count, string data_csv_path, bool bf16_convert){
fstream fout;
fout.open(data_csv_path, ios::out | ios::app);
for (int i = 0; i < count; i++)
{
if (bf16_convert) fout << bf16ToFloat(host_data[i]) << "\n";
else
fout << host_data[i] << "\n";
}
fout.close();
}
void describe_stat(const string& stat_name,
const hccl_demo_stats& stats,
size_t data_size,
double factor,
int hccl_rank,
int loop,
const string& test_type,
const string& data_type,
const string& reduction_op,
const bool reportingRank)
{
auto algo_bandwidth = (double) data_size / stats.rank_duration_in_sec;
auto nw_bandwidth = algo_bandwidth * factor;
bool write_report = should_write_report(hccl_rank);
if (write_report)
{
log() << "Processing data_size " << data_size << endl;
}
else if (reportingRank)
{
stringstream ss;
sleep(1);
size_t delimiter_size = stat_name.length() + string {"[BENCHMARK]"}.length() + 1;
ss << get_print_delimiter(delimiter_size, '#') << '\n';
ss << "[BENCHMARK] " << stat_name << '\n';
ss << "[BENCHMARK] NW Bandwidth : " << format_bw(nw_bandwidth) << '\n';
ss << "[BENCHMARK] Algo Bandwidth : " << format_bw(algo_bandwidth);
ss << '\n' << get_print_delimiter(delimiter_size, '#') << '\n';
log() << ss.str();
}
// Write results to csv file
auto csv_path = get_demo_csv_path();
if (!csv_path.empty())
{
ofstream output;
output.open(csv_path, ofstream::out | ofstream::app);
output << test_type << "," << hccl_rank << "," << data_type << "," << data_size << "," << loop << ","
<< format_bw(nw_bandwidth) << endl;
output.close();
}
// keep the entry for the report
if (write_report)
{
hccl_demo_report_entry report_entry = {data_size,
(uint64_t) (data_size / get_data_type_size(data_type)),
stats.rank_duration_in_sec,
algo_bandwidth,
nw_bandwidth,
data_type,
reduction_op};
report_entry_vec.push_back(report_entry);
}
}
hcclResult_t send_recv_test(void* out_dev_ptr,
const void* input_dev_ptr,
const size_t count,
hcclComm_t comm,
const synStreamHandle stream,
const int recvFromRank,
const int sendToRank,
hcclDataType_t hccl_data_type)
{
hcclGroupStart();
CHECK_HCCL_STATUS(hcclSend((const void*) input_dev_ptr, count, hccl_data_type, sendToRank, comm, stream));
CHECK_HCCL_STATUS(hcclRecv((void*) out_dev_ptr, count, hccl_data_type, recvFromRank, comm, stream));
hcclGroupEnd();
return hcclSuccess;
}
using RanksVector = std::vector<int>;
static hcclResult_t send_recv_ranks_test(uint64_t iter,
const std::vector<uint64_t>& output_dev_ptrs,
const void* input_dev_ptr,
const size_t count,
hcclComm_t comm,
const synStreamHandle stream,
const RanksVector& recvRanks,
const RanksVector& sendRanks,
hcclDataType_t hccl_data_type)
{
hcclGroupStart();
for (const int sendRank : sendRanks)
{
CHECK_HCCL_STATUS(hcclSend((const void*) input_dev_ptr, count, hccl_data_type, sendRank, comm, stream));
}
uint64_t outputBufferIndex = (recvRanks.size() * iter) % output_dev_ptrs.size();
for (const int recvRank : recvRanks)
{
CHECK_HCCL_STATUS(
hcclRecv((void*) output_dev_ptrs[outputBufferIndex], count, hccl_data_type, recvRank, comm, stream));
outputBufferIndex = (outputBufferIndex + 1 == output_dev_ptrs.size()) ? 0 : outputBufferIndex + 1;
}
hcclGroupEnd();
return hcclSuccess;
}
template<class T>
static bool send_recv_test_driver(hccl_demo_data& demo_data,
const std::string& test_type,
const int hccl_rank,
const uint64_t data_size,
const uint64_t count,
const std::vector<T>& input_host_data,
const std::vector<uint64_t> input_dev_ptrs,
const std::vector<uint64_t> output_dev_ptrs,
uint64_t correctness_dev_ptr,
bool bf16_convert,
bool data_csv_enabled,
string data_csv_path_output)
{
//
// This test does the following whether it's a single box or scale-out.
// For single box, exchange buffer with adjacent rank. If odd number of ranks then last rank does self send/recv.
// For scale-out test, exchange buffer with next peer rank in ring manner.
//
// Example:
// 4 boxes: R0 -> R8 & R0 <- R24, R8 <- R0 & R8 -> R16, R16 <- R8 & R16 -> R24, R24 <- R16 & R24 ->R0 etc.
// 2 boxes: R0 <> R8, R1 <> R9, etc.
//
// In both cases, each rank does 1 send and 1 recv from another (same) rank.
bool is_ok = true;
const double send_recv_factor = 1;
// const unsigned int boxSize = static_cast<unsigned>(get_demo_ranks_per_node());
const unsigned int scaleupGroupSize = get_demo_scaleup_group_size();
const unsigned int numOfRanks = demo_data.nranks;
unsigned int numOfBoxes = numOfRanks / scaleupGroupSize;
if (numOfRanks % scaleupGroupSize > 0)
{
numOfBoxes++;
}
const unsigned int ranksPerBox = numOfRanks / numOfBoxes;
const unsigned myRank = static_cast<unsigned>(hccl_rank);
const unsigned myBoxNum = myRank / scaleupGroupSize;
int sendToRank = INVALID_RANK;
int recvFromRank = INVALID_RANK;
if (numOfBoxes > 1)
// scaleout
{
// Do ring with adjacent boxes
const unsigned targetSendBox = myBoxNum == numOfBoxes - 1 ? 0 : myBoxNum + 1;
sendToRank = targetSendBox * ranksPerBox + (myRank % ranksPerBox);
const unsigned targetRecvBox = myBoxNum == 0 ? numOfBoxes - 1 : myBoxNum - 1;
recvFromRank = targetRecvBox * ranksPerBox + (myRank % ranksPerBox);
}
else
// single box
{
// send / recv from adjacent even/odd pairs ranks, i.e. R0 <>R1, R2<>R3.
// in case of odd number of ranks - last rank will do send/recv with self.
sendToRank = (myRank % 2) != 0 ? myRank - 1
: ((numOfRanks % 2) && (myRank == numOfRanks - 1)) != 0 ? myRank
: myRank + 1;
recvFromRank = sendToRank;
}
const void* input_host_data_ptr = reinterpret_cast<const void*>(input_host_data.data());
auto stat = benchmark(
demo_data,
[&](uint64_t iter) {
uint64_t index = iter % input_dev_ptrs.size();
CHECK_HCCL_STATUS(send_recv_test((void*) output_dev_ptrs[index],
(const void*) input_dev_ptrs[index],
(uint64_t) count,
demo_data.hccl_comm,
demo_data.collective_stream,
recvFromRank,
sendToRank,
demo_data.hccl_data_type));
},
[&]() {
CHECK_HCCL_STATUS(send_recv_test((void*) correctness_dev_ptr,
(const void*) input_dev_ptrs[0],
count,
demo_data.hccl_comm,
demo_data.collective_stream,
recvFromRank,
sendToRank,
demo_data.hccl_data_type));
});
// Correctness check
auto output_host_data = vector<T>(input_host_data.size());
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
correctness_dev_ptr,
data_size,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
if (check_correctness())
{
size_t loop_size = data_size / get_demo_test_size();
if (bf16_convert)
{
for (size_t i = 0; i < loop_size; ++i)
{
is_ok = correctness_check_function(demo_data, recvFromRank + 1, bf16ToFloat(output_host_data[i]), i);
}
}
else
{
for (size_t i = 0; i < loop_size; ++i)
{
if (abs(output_host_data[i] - (float) (recvFromRank + 1)) != 0)
{
is_ok = false;
}
}
}
log() << "SendRecv hccl_rank=" << demo_data.hccl_rank << " size=" << data_size << " <"
<< demo_data.str_data_type << ">"
<< " Input Buffer [" << get_float(input_host_data[0]) << " " << get_float(input_host_data[1]) << " "
<< get_float(input_host_data[2]) << " " << get_float(input_host_data[3]) << " ...]"
<< " reduced to Output Buffer [" << get_float(output_host_data[0]) << " "
<< get_float(output_host_data[1]) << " " << get_float(output_host_data[2]) << " "
<< get_float(output_host_data[3]) << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
}
if (data_csv_enabled)
{
copy_data_to_csv_report(output_host_data, count, data_csv_path_output, bf16_convert);
}
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness check
describe_stat("hcclSendRecv(src!=dst, data_size=" + to_string(data_size) + ", count=" + to_string(input_host_data.size()) +
", dtype=" + demo_data.str_data_type + ", iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
send_recv_factor,
hccl_rank,
demo_data.num_iters,
test_type,
demo_data.str_data_type,
"",
should_report_stat(hccl_rank));
return is_ok;
}
struct RanksPairSendRecv
{
int sendFromRank;
int recvInRank;
};
static std::vector<RanksPairSendRecv> parseRanksList(const std::string& ranksListSt, const int maxRankNumber)
{
std::vector<RanksPairSendRecv> ranksList;
std::stringstream ss(ranksListSt);
std::vector<int> tempRanksVector;
std::string token;
while (std::getline(ss, token, ','))
{
const int rankNum = std::stoi(token);
if ((rankNum >= 0) && (rankNum <= (int) (maxRankNumber)))
{
tempRanksVector.push_back(rankNum);
}
else
{
throw std::runtime_error {" Invalid rank number " + std::to_string(rankNum) + ", maxRankNumber=" +
std::to_string(maxRankNumber) + ", ranksListSt=" + ranksListSt};
}
}
if (tempRanksVector.size() % 2 != 0)
{
throw std::runtime_error {" Invalid ranks pairs, ranksListSt=" + ranksListSt};
}
if ((tempRanksVector.size() > 0) && (tempRanksVector.size() % 2 == 0))
{
const size_t pairsNum = tempRanksVector.size() / 2;
for (size_t count = 0; count < pairsNum; count++)
{
const int sendFromRank = tempRanksVector[count * 2];
const int recvInRank = tempRanksVector[count * 2 + 1];
ranksList.push_back({sendFromRank, recvInRank});