forked from rai-project/evaluation
/
summary_model_memory.go
208 lines (185 loc) · 6.08 KB
/
summary_model_memory.go
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
package evaluation
import (
"regexp"
"strings"
"github.com/c3sr/tracer"
"github.com/k0kubun/pp"
"github.com/spf13/cast"
model "github.com/uber/jaeger/model/json"
)
//easyjson:json
type GPUMemInformation struct {
GPUID int `json:"gpuid,omitempty"`
StartUsed int64 `json:"start_used,omitempty"`
StartFree int64 `json:"start_free,omitempty"`
StartTotal int64 `json:"start_total,omitempty"`
FinishUsed int64 `json:"finish_used,omitempty"`
FinishFree int64 `json:"finish_free,omitempty"`
FinishTotal int64 `json:"finish_total,omitempty"`
}
//easyjson:json
type SystemMemoryInformation struct {
StartAvailable int64 `json:"start_available,omitempty"`
StartFree int64 `json:"start_free,omitempty"`
StartTotal int64 `json:"start_total,omitempty"`
FinishAvailable int64 `json:"finish_available,omitempty"`
FinishFree int64 `json:"finish_free,omitempty"`
FinishTotal int64 `json:"finish_total,omitempty"`
}
//easyjson:json
type RuntimeMemoryInformation struct {
StartAlloc int64 `json:"start_alloc,omitempty"`
StartHeapAlloc int64 `json:"start_heap_alloc,omitempty"`
StartHeapSys int64 `json:"start_heap_sys,omitempty"`
StartTotalAlloc int64 `json:"start_total_alloc,omitempty"`
FinishAlloc int64 `json:"finish_alloc,omitempty"`
FinishHeapAlloc int64 `json:"finish_heap_alloc,omitempty"`
FinishHeapSys int64 `json:"finish_heap_sys,omitempty"`
FinishTotalAlloc int64 `json:"finish_total_alloc,omitempty"`
}
//easyjson:json
type MemoryInformation struct {
GPU []GPUMemInformation `json:"gpu,omitempty"`
Runtime RuntimeMemoryInformation `json:"runtime,omitempty"`
System SystemMemoryInformation `json:"system,omitempty"`
}
//easyjson:json
type SummaryMemoryInformation struct {
SummaryBase `json:",inline"`
MemoryInformations []MemoryInformation `json:"memory_informations,omitempty"`
}
func (p Performance) MemoryInformationSummary(e Evaluation) (*SummaryMemoryInformation, error) {
perfSpans, err := p.Spans()
if err != nil {
return nil, err
}
cPredictSpans := perfSpans.FilterByOperationNameAndEvalTraceLevel("c_predict", tracer.FRAMEWORK_TRACE.String())
return &SummaryMemoryInformation{
SummaryBase: e.summaryBase(),
MemoryInformations: cPredictSpans.MemoryInformation(),
}, nil
}
func (spns Spans) MemoryInformation() []MemoryInformation {
res := []MemoryInformation{}
for _, s := range spns {
info := memoryInformationFromSpan(s)
if info == nil {
continue
}
res = append(res, *info)
}
return res
}
var gpuIdSelectorRe = regexp.MustCompile(`^start_gpu\[(\d+)\]_.*`)
func memoryInformationFromSpan(span model.Span) *MemoryInformation {
if len(span.Logs) == 0 {
return nil
}
logs := span.Logs
memInfo := MemoryInformation{}
getGPUId := func(str string) int {
for _, match := range gpuIdSelectorRe.FindAllString(str, -1) {
idx := cast.ToInt(match)
tbl := memInfo.GPU
if len(memInfo.GPU) < idx {
tbl = make([]GPUMemInformation, idx+1)
}
if len(memInfo.GPU) != 0 {
copy(tbl, memInfo.GPU)
}
memInfo.GPU[idx].GPUID = idx
memInfo.GPU = tbl
return idx
}
pp.Println("not found...")
return 0
}
for _, lg := range logs {
for _, f := range lg.Fields {
if strings.HasPrefix(f.Key, "start_mem_alloc") {
memInfo.Runtime.StartAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_heap_alloc") {
memInfo.Runtime.StartHeapAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_heap_sys") {
memInfo.Runtime.StartHeapSys = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_total_alloc") {
memInfo.Runtime.StartTotalAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_sys_available") {
memInfo.System.StartAvailable = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_sys_free") {
memInfo.System.StartFree = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_mem_sys_total") {
memInfo.System.StartTotal = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "start_gpu[") && strings.HasSuffix(f.Key, "]_mem_free") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].StartFree = cast.ToInt64(f.Value)
}
if strings.HasPrefix(f.Key, "start_gpu[") && strings.HasSuffix(f.Key, "]_mem_total") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].StartTotal = cast.ToInt64(f.Value)
}
if strings.HasPrefix(f.Key, "start_gpu[") && strings.HasSuffix(f.Key, "]_mem_used") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].StartUsed = cast.ToInt64(f.Value)
}
if strings.HasPrefix(f.Key, "finish_mem_alloc") {
memInfo.Runtime.FinishAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_heap_alloc") {
memInfo.Runtime.FinishHeapAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_heap_sys") {
memInfo.Runtime.FinishHeapSys = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_total_alloc") {
memInfo.Runtime.FinishTotalAlloc = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_sys_available") {
memInfo.System.FinishAvailable = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_sys_free") {
memInfo.System.FinishFree = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_mem_sys_total") {
memInfo.System.FinishTotal = cast.ToInt64(f.Value)
continue
}
if strings.HasPrefix(f.Key, "finish_gpu[") && strings.HasSuffix(f.Key, "]_mem_free") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].FinishFree = cast.ToInt64(f.Value)
}
if strings.HasPrefix(f.Key, "finish_gpu[") && strings.HasSuffix(f.Key, "]_mem_total") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].FinishTotal = cast.ToInt64(f.Value)
}
if strings.HasPrefix(f.Key, "finish_gpu[") && strings.HasSuffix(f.Key, "]_mem_used") {
gpuId := getGPUId(f.Key)
memInfo.GPU[gpuId].FinishUsed = cast.ToInt64(f.Value)
}
}
}
if isZero(memInfo) {
return nil
}
return &memInfo
}