rccl.go 6.95 KB
Newer Older
1
2
package backend

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
import (
	"container/list"
	"errors"
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
)

type RCCL_BINARY string

const (
	RCCL_ALL_GATHER     RCCL_BINARY = "all_gather_perf"
	RCCL_ALL_REDUCE     RCCL_BINARY = "all_reduce_perf"
	RCCL_ALL_TO_ALL     RCCL_BINARY = "alltoall_perf"
	RCCL_BROADCASE      RCCL_BINARY = "broadcast_perf"
	RCCL_GATHER         RCCL_BINARY = "gather_perf"
	RCCL_REDUCE         RCCL_BINARY = "reduce_perf"
	RCCL_REDUCE_SCATTER RCCL_BINARY = "reduce_scatter_perf"
	RCCL_SCATTER        RCCL_BINARY = "scatter_perf"
	RCCL_SEND_RECV      RCCL_BINARY = "send_recv_perf"
)

type RcclTestAllReducePrefResult struct {
28
29
	DTKPath     string          `json:"dtk_path"`     // dtk 库路径
	Args        string          `json:"args"`         // 执行参数
30
31
32
	TestVersion string          `json:"test_version"` // rccl-tests 版本信息
	UseDevice   []string        `json:"use_device"`   // 使用的设备列表
	Results     []*RcclTestItem `json:"results"`
33
	RawOutput   string          `json:"raw_output,omitempty"`
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
}

type RcclTestItem struct {
	Size       uint64  `json:"size"`
	Count      uint64  `json:"count"`
	Type       string  `json:"type"`
	Redop      string  `json:"redop"`
	Root       int     `json:"root"`
	OutOfPlace Metrics `json:"out_of_place"`
	InPlace    Metrics `json:"in_place"`
}

type Metrics struct {
	Time  float64 `json:"time"`
	AlgBW float64 `json:"alg_bw"`
	BusBW float64 `json:"bus_bw"`
	Wrong uint32  `json:"wrong"`
}

func NewRcclTestItem(str []string) *RcclTestItem {
	if len(str) != 13 {
		return nil
	}
	item := &RcclTestItem{}
	i, err := strconv.ParseUint(str[0], 10, 64)
	if err != nil {
		return nil
	}
	item.Size = i
	i, err = strconv.ParseUint(str[1], 10, 64)
	if err != nil {
		return nil
	}
	item.Count = i
	item.Type = str[2]
	item.Redop = str[3]
	r, err := strconv.ParseInt(str[4], 10, 10)
	if err != nil {
		return nil
	}
	item.Root = int(r)
	t, err := strconv.ParseFloat(str[5], 64)
	if err != nil {
		return nil
	}
	item.OutOfPlace.Time = t
	t, err = strconv.ParseFloat(str[6], 64)
	if err != nil {
		return nil
	}
	item.OutOfPlace.AlgBW = t
	t, err = strconv.ParseFloat(str[7], 64)
	if err != nil {
		return nil
	}
	item.OutOfPlace.BusBW = t
	w, err := strconv.ParseUint(str[8], 10, 32)
	if err != nil {
		return nil
	}
	item.OutOfPlace.Wrong = uint32(w)
	t, err = strconv.ParseFloat(str[9], 64)
	if err != nil {
		return nil
	}
	item.InPlace.Time = t
	t, err = strconv.ParseFloat(str[10], 64)
	if err != nil {
		return nil
	}
	item.InPlace.AlgBW = t
	t, err = strconv.ParseFloat(str[11], 64)
	if err != nil {
		return nil
	}
	item.InPlace.BusBW = t
	w, err = strconv.ParseUint(str[12], 10, 32)
	if err != nil {
		return nil
	}
	item.InPlace.Wrong = uint32(w)
	return item
}

var (
	ReUselessLine    = regexp.MustCompile(`^#\s*$`)
	ReSharpLine      = regexp.MustCompile(`^#.*$`)
	ReRcclVersion    = regexp.MustCompile(`(?mi)^rccl-tests:\s+Version\s+(.+)$`)
	ReMetricsLine    = regexp.MustCompile(`(?mi)^\s*(\d+)\s+(\d+)\s+(\w+)\s+(\w+)\s+((?:-|)\d+)\s+([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)\s+(\d+)$`)
	ReDeviceLineFlag = regexp.MustCompile(`(?mi)^#\s*using\s+devices\s*$`)
	ReDeviceLine     = regexp.MustCompile(`(?mi)^#\s*rank\s+(\d+)\s+pid\s+(\d+)\s+on\s+(\w+)\s+device\s+(\d+)\s+\[([0-9a-zA-Z:.]*)\]\s(\w+)$`)
125
	ReDtkPath        = regexp.MustCompile(`^(.*dtk[0-9A-Za-z.-]*).*`)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
)

// RcclTestCheck 检查 rccl-tests 目录及指定二进制文件是否存在,返回二进制文件的完整路径
func RcclTestCheck(rccl_test_path string, binary RCCL_BINARY) (string, error) {
	// 检查 rccl-tests 目录是否存在
	stat, err := os.Stat(rccl_test_path)
	if err != nil {
		return "", err
	}
	if !stat.IsDir() {
		return "", os.ErrNotExist
	}

	// 检查可执行文件是否存在
140
	exePath := strings.TrimSuffix(rccl_test_path, "/") + "/" + string(binary)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	exeStat, err := os.Stat(exePath)
	if err != nil {
		return "", err
	}
	if exeStat.IsDir() {
		return "", errors.New("it's a dir, not file")
	}
	if exeStat.Mode().Perm()&0111 == 0 {
		return "", errors.New("file is not executable")
	}
	return exePath, nil
}

func GetRcclDtkPath(rccl_test_path string, binary RCCL_BINARY) (string, error) {
155
	path, err := RcclTestCheck(rccl_test_path, binary)
156
157
158
159
160
161
162
163
164
165
166
167
168
	if err != nil {
		return "", err
	}
	output, err := exec.Command("ldd", path).CombinedOutput()
	if err != nil {
		return "", err
	}
	str := string(output)
	lines := strings.Split(str, "\n")
	for _, v := range lines {
		if strings.Contains(v, "librccl.so") {
			parts := strings.Fields(v)
			for _, part := range parts {
169
170
171
172
173
				if strings.Contains(part, "dtk") && ReDtkPath.MatchString(part) {
					m := ReDtkPath.FindStringSubmatch(part)
					if len(m) == 2 {
						return m[1], nil
					}
174
175
176
177
178
179
180
				}
			}
		}
	}
	return "", errors.New("librccl.so not found in ldd output")
}

181
func AllReducePerf(rccl_test_path string, args string) (*RcclTestAllReducePrefResult, error) {
182
183
	path, err := RcclTestCheck(rccl_test_path, RCCL_ALL_REDUCE)
	if err != nil {
184
185
186
187
188
189
190
191
		return nil, err
	}
	args = strings.Trim(args, " ")
	var output []byte
	if args == "" {
		output, err = exec.Command(path).CombinedOutput()
	} else {
		output, err = exec.Command(path, strings.Fields(args)...).CombinedOutput()
192
193
	}
	if err != nil {
194
		return nil, err
195
196
197
198
	}
	str := string(output)
	res := ParseRcclOutput(str)
	res.Args = args
199
200
201
202
203
204
	dp, err := GetRcclDtkPath(rccl_test_path, RCCL_ALL_REDUCE)
	if err == nil {
		res.DTKPath = dp
	}

	return res, nil
205
206
207
}

func ParseRcclOutput(output string) *RcclTestAllReducePrefResult {
208
209
	result := RcclTestAllReducePrefResult{}
	result.UseDevice = make([]string, 0, 8)
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

	str := strings.Trim(output, "\n")
	lines := strings.Split(str, "\n")

	sharpLines := list.New()
	nosharpLines := list.New()

	testItems := make([]*RcclTestItem, 0, 16)

	for _, v := range lines {
		if ReUselessLine.MatchString(v) {
			continue
		}
		if ReSharpLine.MatchString(v) {
			sharpLines.PushBack(v)
		} else {
			nosharpLines.PushBack(v)
		}
	}
	cache := make([]string, 0, 16)
	for e := nosharpLines.Front(); e != nil; e = e.Next() {
		line, ok := e.Value.(string)
		if !ok {
			continue
		}
		if ReMetricsLine.MatchString(line) {
236
237
238
			match := ReMetricsLine.FindStringSubmatch(line)
			if len(match) == 14 {
				item := NewRcclTestItem(match[1:])
239
240
241
242
243
244
245
246
				if item != nil {
					testItems = append(testItems, item)
				}
			}
		} else {
			cache = append(cache, line)
		}
	}
247
	result.Results = testItems
248
249
	for _, v := range cache {
		if ReRcclVersion.MatchString(v) {
250
251
252
			match := ReRcclVersion.FindStringSubmatch(v)
			if len(match) == 2 {
				result.TestVersion = match[1]
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
			}
			break
		}
	}

	findFlag := false
	for e := sharpLines.Front(); e != nil; e = e.Next() {
		line, ok := e.Value.(string)
		if !ok {
			continue
		}
		if ReDeviceLineFlag.MatchString(line) {
			findFlag = true
			continue
		}
		if findFlag && ReDeviceLine.MatchString(line) {
269
270
271
			match := ReDeviceLine.FindStringSubmatch(line)
			if len(match) == 7 {
				result.UseDevice = append(result.UseDevice, match[6])
272
273
274
275
276
277
			}
		} else if findFlag {
			findFlag = false
			break
		}
	}
278
279
	result.RawOutput = output
	return &result
280
}