forked from kubewharf/katalyst-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.go
More file actions
180 lines (149 loc) · 6.31 KB
/
inference.go
File metadata and controls
180 lines (149 loc) · 6.31 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
/*
Copyright 2022 The Katalyst Authors.
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.
*/
package inference
import (
"context"
"fmt"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/inference/modelinputfetcher"
genericinput "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/inference/modelinputfetcher/generic"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/inference/modelresultfetcher"
borweinresult "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/inference/modelresultfetcher/borwein"
"github.com/kubewharf/katalyst-core/pkg/config"
metricemitter "github.com/kubewharf/katalyst-core/pkg/config/agent/sysadvisor/metric-emitter"
"github.com/kubewharf/katalyst-core/pkg/config/generic"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool"
"github.com/kubewharf/katalyst-core/pkg/util/general"
)
func init() {
modelinputfetcher.RegisterModelInputFetcherInitFunc(genericinput.GenericModelInputFetcherName,
genericinput.NewGenericModelInputFetcher)
modelresultfetcher.RegisterModelResultFetcherInitFunc(borweinresult.BorweinModelResultFetcherName,
borweinresult.NewBorweinModelResultFetcher)
}
type InferencePlugin struct {
name string
// conf config.Configuration
period time.Duration
modelsInputFetchers map[string]modelinputfetcher.ModelInputFetcher
modelsResultFetchers map[string]modelresultfetcher.ModelResultFetcher
metaServer *metaserver.MetaServer
metricEmitter metrics.MetricEmitter
emitterConf *metricemitter.MetricEmitterPluginConfiguration
qosConf *generic.QoSConfiguration
metaReader metacache.MetaReader
metaWriter metacache.MetaWriter
}
func NewInferencePlugin(pluginName string, conf *config.Configuration, extraConf interface{},
emitterPool metricspool.MetricsEmitterPool, metaServer *metaserver.MetaServer,
metaCache metacache.MetaCache,
) (plugin.SysAdvisorPlugin, error) {
if conf == nil || conf.InferencePluginConfiguration == nil {
return nil, fmt.Errorf("nil conf")
} else if metaServer == nil {
return nil, fmt.Errorf("nil metaServer")
} else if metaCache == nil {
return nil, fmt.Errorf("nil metaCache")
} else if emitterPool == nil {
return nil, fmt.Errorf("nil emitterPool")
}
metricEmitter := emitterPool.GetDefaultMetricsEmitter().WithTags("advisor-inference")
inferencePlugin := InferencePlugin{
name: pluginName,
period: conf.InferencePluginConfiguration.SyncPeriod,
modelsInputFetchers: make(map[string]modelinputfetcher.ModelInputFetcher),
modelsResultFetchers: make(map[string]modelresultfetcher.ModelResultFetcher),
metaServer: metaServer,
metricEmitter: metricEmitter,
emitterConf: conf.AgentConfiguration.MetricEmitterPluginConfiguration,
qosConf: conf.GenericConfiguration.QoSConfiguration,
metaReader: metaCache,
metaWriter: metaCache,
}
for fetcherName, initFn := range modelinputfetcher.GetRegisteredModelInputFetcherInitFuncs() {
// todo: support only enabling part of fetchers
general.Infof("try init fetcher: %s", fetcherName)
fetcher, err := initFn(fetcherName, conf, extraConf, emitterPool, metaServer, metaCache)
if err != nil {
return nil, fmt.Errorf("failed to start sysadvisor plugin %v: %v", pluginName, err)
} else if fetcher == nil {
general.Infof("fetcher: %s isn't enabled", fetcherName)
continue
}
inferencePlugin.modelsInputFetchers[fetcherName] = fetcher
}
for fetcherName, initFn := range modelresultfetcher.GetRegisteredModelResultFetcherInitFuncs() {
// todo: support only enabling part of fetchers
general.Infof("try init fetcher: %s", fetcherName)
fetcher, err := initFn(fetcherName, conf, extraConf, emitterPool, metaServer, metaCache)
if err != nil {
return nil, fmt.Errorf("failed to start sysadvisor plugin %v: %v", pluginName, err)
} else if fetcher == nil {
general.Infof("fetcher: %s isn't enabled", fetcherName)
continue
}
inferencePlugin.modelsResultFetchers[fetcherName] = fetcher
}
return &inferencePlugin, nil
}
func (infp *InferencePlugin) Run(ctx context.Context) {
wait.UntilWithContext(ctx, infp.inference, infp.period)
}
// Name returns the name of inference plugin
func (infp *InferencePlugin) Name() string {
return infp.name
}
// Init initializes the inference plugin
func (infp *InferencePlugin) Init() error {
return nil
}
func (infp *InferencePlugin) fetchModelInput(ctx context.Context) {
var wg sync.WaitGroup
for modelName, fetcher := range infp.modelsInputFetchers {
wg.Add(1)
go func(modelName string, fetcher modelinputfetcher.ModelInputFetcher) {
defer wg.Done()
general.Infof("FetchModelInput for model: %s start", modelName)
err := fetcher.FetchModelInput(ctx, infp.metaReader, infp.metaWriter, infp.metaServer)
if err != nil {
general.Errorf("FetchModelInput for model: %s failed with error: %v", modelName, err)
}
}(modelName, fetcher)
}
wg.Wait()
}
func (infp *InferencePlugin) fetchModelResult(ctx context.Context) {
var wg sync.WaitGroup
for modelName, fetcher := range infp.modelsResultFetchers {
wg.Add(1)
go func(modelName string, fetcher modelresultfetcher.ModelResultFetcher) {
defer wg.Done()
general.Infof("FetchModelResult for model: %s start", modelName)
err := fetcher.FetchModelResult(ctx, infp.metaReader, infp.metaWriter, infp.metaServer)
if err != nil {
general.Errorf("FetchModelResult for model: %s failed with error: %v", modelName, err)
}
}(modelName, fetcher)
}
wg.Wait()
}
func (infp *InferencePlugin) inference(ctx context.Context) {
infp.fetchModelInput(ctx)
infp.fetchModelResult(ctx)
}