-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.go
166 lines (152 loc) · 3.94 KB
/
predictor.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
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// 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 paddle
// #include "pd_predictor.h"
// #include "pd_tensor.h"
// #include "pd_common.h"
// #include "pd_types.h"
// #include "pd_utils.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"runtime"
"unsafe"
)
type Predictor struct {
c *C.PD_Predictor
}
///
/// \brief Create a new Predictor
///
/// \param[in] Config config
/// \return new predicor.
///
func NewPredictor(config *Config) *Predictor {
cPredictor := C.PD_PredictorCreate(config.c)
predictor := &Predictor{c: cPredictor}
runtime.SetFinalizer(predictor, func(predictor *Predictor) {
C.PD_PredictorDestroy(predictor.c)
})
return predictor
}
///
/// \brief Clone a new Predictor
///
/// \return new predictor.
///
func (p *Predictor) Clone() *Predictor {
cPredictor := C.PD_PredictorClone(p.c)
predictor := &Predictor{c: cPredictor}
runtime.SetFinalizer(predictor, func(predictor *Predictor) {
C.PD_PredictorDestroy(predictor.c)
})
return predictor
}
///
/// \brief Get the input number
///
/// \return input number
///
func (p *Predictor) GetInputNum() uint {
return uint(C.PD_PredictorGetInputNum(p.c))
}
///
/// \brief Get the output number
///
/// \return output number
///
func (p *Predictor) GetOutputNum() uint {
return uint(C.PD_PredictorGetOutputNum(p.c))
}
///
/// \brief Get the input names
///
/// \return input names
///
func (p *Predictor) GetInputNames() []string {
cNames := C.PD_PredictorGetInputNames(p.c)
numNames := int(cNames.size)
names := cvtToGoSliceString(numNames, cNames.data)
C.PD_OneDimArrayCstrDestroy(cNames)
return names
}
///
/// \brief Get the output names
///
/// \return output names
///
func (p *Predictor) GetOutputNames() []string {
cNames := C.PD_PredictorGetOutputNames(p.c)
numNames := int(cNames.size)
names := cvtToGoSliceString(numNames, cNames.data)
C.PD_OneDimArrayCstrDestroy(cNames)
return names
}
///
/// \brief Get the Input Tensor object
///
/// \param[in] name input name
/// \return input tensor
///
func (p *Predictor) GetInputHandle(name string) *Tensor {
cName := C.CString(name)
cHandle := C.PD_PredictorGetInputHandle(p.c, cName)
C.free(unsafe.Pointer(cName))
handle := &Tensor{c: cHandle}
runtime.SetFinalizer(handle, func(handle *Tensor) {
C.PD_TensorDestroy(handle.c)
})
return handle
}
///
/// \brief Get the Output Tensor object
///
/// \param[in] name output name
/// \return output tensor
///
func (p *Predictor) GetOutputHandle(name string) *Tensor {
cName := C.CString(name)
cHandle := C.PD_PredictorGetOutputHandle(p.c, cName)
C.free(unsafe.Pointer(cName))
handle := &Tensor{c: cHandle}
runtime.SetFinalizer(handle, func(handle *Tensor) {
C.PD_TensorDestroy(handle.c)
})
return handle
}
///
/// \brief Run the prediction engine
///
func (p *Predictor) Run() {
C.PD_PredictorRun(p.c)
}
///
/// \brief Clear the intermediate tensors of the predictor
///
func (p *Predictor) ClearIntermediateTensor() {
C.PD_PredictorClearIntermediateTensor(p.c)
}
///
/// \brief Release all tmp tensor to compress the size of the memory pool.
/// The memory pool is considered to be composed of a list of chunks, if
/// the chunk is not occupied, it can be released.
///
/// \return Number of bytes released. It may be smaller than the actual
/// released memory, because part of the memory is not managed by the
/// MemoryPool.
///
func (p *Predictor) TryShrinkMemory() {
C.PD_PredictorTryShrinkMemory(p.c)
}