-
Notifications
You must be signed in to change notification settings - Fork 18
/
lib.rs
252 lines (231 loc) · 8.19 KB
/
lib.rs
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
209
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
pub mod block_ffm;
pub mod block_helpers;
pub mod block_loss_functions;
pub mod block_lr;
pub mod block_misc;
pub mod block_neural;
pub mod block_normalize;
pub mod block_relu;
pub mod buffer_handler;
pub mod cache;
pub mod cmdline;
pub mod feature_buffer;
pub mod feature_transform_executor;
pub mod feature_transform_implementations;
pub mod feature_transform_parser;
pub mod graph;
pub mod hogwild;
pub mod logging_layer;
pub mod model_instance;
pub mod multithread_helpers;
pub mod optimizer;
pub mod parser;
pub mod persistence;
pub mod port_buffer;
pub mod quantization;
pub mod radix_tree;
pub mod regressor;
pub mod serving;
pub mod version;
pub mod vwmap;
extern crate blas;
extern crate half;
extern crate intel_mkl_src;
use crate::feature_buffer::FeatureBufferTranslator;
use crate::multithread_helpers::BoxedRegressorTrait;
use crate::parser::VowpalParser;
use crate::port_buffer::PortBuffer;
use crate::regressor::BlockCache;
use crate::vwmap::NamespaceType;
use shellwords;
use std::ffi::CStr;
use std::io::Cursor;
use std::os::raw::c_char;
const EOF_ERROR_CODE: f32 = -1.0;
const EXCEPTION_ERROR_CODE: f32 = -1.0;
#[repr(C)]
pub struct FfiPredictor {
_marker: core::marker::PhantomData<Predictor>,
}
pub struct Predictor {
feature_buffer_translator: FeatureBufferTranslator,
vw_parser: VowpalParser,
regressor: BoxedRegressorTrait,
pb: PortBuffer,
cache: PredictorCache,
}
pub struct PredictorCache {
blocks: Vec<BlockCache>,
input_buffer_size: usize,
}
impl Predictor {
unsafe fn predict(&mut self, input_buffer: &str) -> f32 {
let mut buffered_input = Cursor::new(input_buffer);
let reading_result = self.vw_parser.next_vowpal(&mut buffered_input);
let buffer = match reading_result {
Ok([]) => {
log::error!("Reading result for prediction returns EOF");
return EOF_ERROR_CODE;
} // EOF
Ok(buffer2) => buffer2,
Err(e) => {
log::error!("Reading result for prediction returns error {}", e);
return EXCEPTION_ERROR_CODE;
}
};
self.feature_buffer_translator.translate(buffer, 0);
self.regressor
.predict(&self.feature_buffer_translator.feature_buffer, &mut self.pb)
}
unsafe fn predict_with_cache(&mut self, input_buffer: &str) -> f32 {
let mut buffered_input = Cursor::new(&input_buffer);
let reading_result = self
.vw_parser
.next_vowpal_with_cache(&mut buffered_input, self.cache.input_buffer_size);
let buffer = match reading_result {
Ok([]) => {
log::error!("Reading result for prediction with cache returns EOF");
return EOF_ERROR_CODE;
} // EOF
Ok(buffer2) => buffer2,
Err(e) => {
log::error!(
"Reading result for prediction with cache returns error {}",
e
);
return EXCEPTION_ERROR_CODE;
}
};
self.feature_buffer_translator.translate(buffer, 0);
self.regressor.predict_with_cache(
&self.feature_buffer_translator.feature_buffer,
&mut self.pb,
self.cache.blocks.as_slice(),
)
}
unsafe fn setup_cache(&mut self, input_buffer: &str) -> f32 {
let mut buffered_input = Cursor::new(input_buffer);
let reading_result = self.vw_parser.next_vowpal_with_size(&mut buffered_input);
let (buffer, input_buffer_size) = match reading_result {
Ok(([], _)) => {
log::error!("Reading result for prediction with cache returns EOF");
return EOF_ERROR_CODE;
} // EOF
Ok(buffer2) => buffer2,
Err(e) => {
log::error!(
"Reading result for prediction with cache returns error {}",
e
);
return EXCEPTION_ERROR_CODE;
}
};
// ignore last newline byte
self.cache.input_buffer_size = input_buffer_size;
self.feature_buffer_translator.translate_and_filter(
buffer,
0,
Some(NamespaceType::Primitive),
);
let is_empty = self.cache.blocks.is_empty();
self.regressor.setup_cache(
&self.feature_buffer_translator.feature_buffer,
&mut self.cache.blocks,
is_empty,
);
0.0
}
}
#[no_mangle]
pub extern "C" fn new_fw_predictor_prototype(command: *const c_char) -> *mut FfiPredictor {
// create a "prototype" predictor that loads the weights file. This predictor is expensive, and is intended
// to only be created once. If additional predictors are needed (e.g. for concurrent work), please
// use this "prototype" with the clone_lite function, which will create cheap copies
logging_layer::initialize_logging_layer();
let str_command = c_char_to_str(command);
let words = shellwords::split(str_command).unwrap();
let cmd_matches = cmdline::create_expected_args().get_matches_from(words);
let weights_filename = match cmd_matches.value_of("initial_regressor") {
Some(filename) => filename,
None => panic!("Cannot resolve input weights file name"),
};
let (model_instance, vw_namespace_map, regressor) =
persistence::new_regressor_from_filename(weights_filename, true, Some(&cmd_matches))
.unwrap();
let feature_buffer_translator = FeatureBufferTranslator::new(&model_instance);
let vw_parser = VowpalParser::new(&vw_namespace_map);
let sharable_regressor = BoxedRegressorTrait::new(Box::new(regressor));
let pb = sharable_regressor.new_portbuffer();
let predictor = Predictor {
feature_buffer_translator,
vw_parser,
regressor: sharable_regressor,
pb,
cache: PredictorCache {
blocks: Vec::default(),
input_buffer_size: 0,
},
};
Box::into_raw(Box::new(predictor)).cast()
}
#[no_mangle]
pub unsafe extern "C" fn clone_lite(prototype: *mut FfiPredictor) -> *mut FfiPredictor {
// given an expensive "prototype" predictor, this function creates cheap copies of it
// that can be used in different threads concurrently. Note that individually, these predictors
// are not thread safe, but it is safe to use multiple threads, each accessing only one predictor.
let prototype: &mut Predictor = from_ptr(prototype);
let lite_predictor = Predictor {
feature_buffer_translator: prototype.feature_buffer_translator.clone(),
vw_parser: prototype.vw_parser.clone(),
regressor: prototype.regressor.clone(),
pb: prototype.pb.clone(),
cache: PredictorCache {
blocks: Vec::new(),
input_buffer_size: 0,
},
};
Box::into_raw(Box::new(lite_predictor)).cast()
}
#[no_mangle]
pub unsafe extern "C" fn fw_predict(ptr: *mut FfiPredictor, input_buffer: *const c_char) -> f32 {
let str_buffer = c_char_to_str(input_buffer);
let predictor: &mut Predictor = from_ptr(ptr);
predictor.predict(str_buffer)
}
#[no_mangle]
pub unsafe extern "C" fn fw_predict_with_cache(
ptr: *mut FfiPredictor,
input_buffer: *const c_char,
) -> f32 {
let str_buffer = c_char_to_str(input_buffer);
let predictor: &mut Predictor = from_ptr(ptr);
predictor.predict_with_cache(str_buffer)
}
#[no_mangle]
pub unsafe extern "C" fn fw_setup_cache(
ptr: *mut FfiPredictor,
input_buffer: *const c_char,
) -> f32 {
let str_buffer = c_char_to_str(input_buffer);
let predictor: &mut Predictor = from_ptr(ptr);
predictor.setup_cache(str_buffer)
}
#[no_mangle]
pub unsafe extern "C" fn free_predictor(ptr: *mut FfiPredictor) {
drop::<Box<Predictor>>(Box::from_raw(from_ptr(ptr)));
}
unsafe fn from_ptr<'a>(ptr: *mut FfiPredictor) -> &'a mut Predictor {
if ptr.is_null() {
log::error!("Fatal error, got NULL `Context` pointer");
std::process::abort();
}
&mut *(ptr.cast())
}
fn c_char_to_str<'a>(input_buffer: *const c_char) -> &'a str {
let c_str = unsafe {
assert!(!input_buffer.is_null());
CStr::from_ptr(input_buffer)
};
let str_buffer = c_str.to_str().unwrap();
str_buffer
}