-
Notifications
You must be signed in to change notification settings - Fork 6
/
algorithm.go
154 lines (139 loc) · 3.97 KB
/
algorithm.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
package algorithm
import (
"encoding/json"
"errors"
"fmt"
"sync"
"github.com/alibaba/pairec/v2/algorithm/eas"
"github.com/alibaba/pairec/v2/algorithm/faiss"
"github.com/alibaba/pairec/v2/algorithm/seldon"
"github.com/alibaba/pairec/v2/algorithm/tfserving"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/recconf"
"github.com/alibaba/pairec/v2/utils"
)
var algoFactory *AlgorithmFactory
func init() {
algoFactory = NewAlgorithmFactory()
}
// type AlgoData struct {
// Data interface{}
// }
type IAlgorithm interface {
Init(conf *recconf.AlgoConfig) error
Run(algoData interface{}) (interface{}, error)
}
type AlgorithmFactory struct {
algorithms map[string]IAlgorithm
requestDataFuncs map[string]RequestDataFunc
mutex sync.RWMutex
algorithmSigns map[string]string
}
func NewAlgorithmFactory() *AlgorithmFactory {
factory := AlgorithmFactory{
algorithmSigns: make(map[string]string, 2),
}
factory.algorithms = make(map[string]IAlgorithm)
factory.requestDataFuncs = make(map[string]RequestDataFunc)
return &factory
}
func (a *AlgorithmFactory) Init(algoConfs []recconf.AlgoConfig) {
a.mutex.Lock()
defer a.mutex.Unlock()
for _, conf := range algoConfs {
sign, _ := json.Marshal(conf)
if _, ok := a.algorithms[conf.Name]; ok {
if utils.Md5(string(sign)) == a.algorithmSigns[conf.Name] {
continue
}
}
algo, err := a.initAlgo(conf)
if err != nil {
log.Error(err.Error())
continue
}
a.algorithms[conf.Name] = algo
a.algorithmSigns[conf.Name] = utils.Md5(string(sign))
}
}
func (a *AlgorithmFactory) initAlgo(conf recconf.AlgoConfig) (IAlgorithm, error) {
var algo IAlgorithm
if conf.Type == "EAS" {
algo = eas.NewEasModel(conf.Name)
err := algo.Init(&conf)
if err != nil {
return nil, fmt.Errorf("init algorithm error, name:%s, err:%v", conf.Name, err)
}
} else if conf.Type == "FAISS" {
algo = faiss.NewFaissModel(conf.Name)
err := algo.Init(&conf)
if err != nil {
return nil, fmt.Errorf("init algorithm error, name:%s, err:%v", conf.Name, err)
}
} else if conf.Type == "LOOKUP" {
algo = NewLookupPolicy()
err := algo.Init(&conf)
if err != nil {
return nil, fmt.Errorf("init algorithm error, name:%s, err:%v", conf.Name, err)
}
} else if conf.Type == "SELDON" {
algo = new(seldon.Model)
err := algo.Init(&conf)
if err != nil {
return nil, fmt.Errorf("init algorithm error, name:%s, err:%v", conf.Name, err)
}
} else if conf.Type == "TFSERVING" {
algo = tfserving.NewTFservingModel(conf.Name)
err := algo.Init(&conf)
if err != nil {
return nil, fmt.Errorf("init algorithm error, name:%s, err:%v", conf.Name, err)
}
} else {
return nil, fmt.Errorf("algorithm type not support , type:%s", conf.Type)
}
return algo, nil
}
func (a *AlgorithmFactory) Run(name string, algoData interface{}) (interface{}, error) {
a.mutex.RLock()
algo, found := a.algorithms[name]
f, funcFound := a.requestDataFuncs[name]
a.mutex.RUnlock()
if !found {
return nil, errors.New("not found algorithm, name:" + name)
}
// if find request data func
if funcFound {
return algo.Run(f(name, algoData))
}
return algo.Run(algoData)
}
// init algorithm from the config, and add to the algoFactory
func Load(config *recconf.RecommendConfig) {
algoFactory.Init(config.AlgoConfs)
}
func Run(name string, algoData interface{}) (interface{}, error) {
return algoFactory.Run(name, algoData)
}
func AddAlgo(conf recconf.AlgoConfig) {
algoFactory.mutex.Lock()
defer algoFactory.mutex.Unlock()
_, found := algoFactory.algorithms[conf.Name]
if found {
return
}
algo, err := algoFactory.initAlgo(conf)
if err != nil {
log.Error(err.Error())
return
}
algoFactory.algorithms[conf.Name] = algo
}
func RegisterAlgorithm(name string, algo IAlgorithm) {
algoFactory.mutex.Lock()
defer algoFactory.mutex.Unlock()
algoFactory.algorithms[name] = algo
}
type RequestDataFunc func(string, interface{}) interface{}
func RegistRequestDataFunc(name string, f RequestDataFunc) {
algoFactory.requestDataFuncs[name] = f
}