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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Models and serialization helpers.
//!
//! When the `rkyv` feature is enabled we use rkyv for zero-copy (de)serialization.
//! Otherwise we fall back to serde + bincode. The public API is identical.
#[cfg(feature = "rkyv")]
mod imp {
// prefer the convenience helper when available
use rkyv::rancor::Error as RkyvError;
use rkyv::{Archive, Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Archive, Serialize, Deserialize)]
pub enum StageDef {
Named(String),
Scale { w: f32 },
Bias { b: f32 },
Factory { name: String, params: Vec<f32> },
}
#[derive(Debug, PartialEq, Clone, Archive, Serialize, Deserialize)]
pub struct NeuronDef {
pub version: u32,
pub stages: Vec<StageDef>,
pub output: StageDef,
}
impl NeuronDef {
pub fn new(stages: Vec<StageDef>, output: StageDef) -> Self {
Self {
version: 1,
stages,
output,
}
}
pub fn to_bytes(&self) -> Result<Vec<u8>, String> {
rkyv::to_bytes(self)
.map_err(|e: RkyvError| format!("rkyv serialize error: {}", e))
.map(|v| v.into_vec())
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, String> {
unsafe {
rkyv::from_bytes_unchecked::<Self, rkyv::rancor::Error>(bytes)
.map_err(|e: rkyv::rancor::Error| format!("rkyv deserialize error: {}", e))
}
}
pub fn to_neuron(
&self,
registry: &crate::FunctionRegistry,
) -> Result<crate::Neuron, String> {
use crate::{Stage, bias, scale};
let mut stages = Vec::new();
for s in &self.stages {
match s {
StageDef::Named(name) => {
if let Some(f) = registry.get(name) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown function '{}'", name));
}
}
StageDef::Scale { w } => stages.push(Stage::new(scale(*w))),
StageDef::Bias { b } => stages.push(Stage::new(bias(*b))),
StageDef::Factory { name, params } => {
if let Some(f) = registry.call_factory(name, params) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown factory '{}'", name));
}
}
}
}
let output_stage = match &self.output {
StageDef::Named(name) => {
if let Some(f) = registry.get(name) {
Stage::from_arc(f)
} else {
return Err(format!("unknown function '{}'", name));
}
}
StageDef::Scale { w } => Stage::new(scale(*w)),
StageDef::Bias { b } => Stage::new(bias(*b)),
StageDef::Factory { name, params } => {
if let Some(f) = registry.call_factory(name, params) {
Stage::from_arc(f)
} else {
return Err(format!("unknown factory '{}'", name));
}
}
};
Ok(crate::Neuron::new(stages, output_stage))
}
/// Zero-copy-ish rehydration: validate the buffer and walk the archived
/// view to construct a `Neuron` without allocating owned `NeuronDef` or
/// `StageDef` instances. Primitive numeric values are copied where
/// necessary (small cost); strings and vec headers remain borrowed.
pub fn from_bytes_zero_copy_to_neuron(
bytes: &[u8],
registry: &crate::FunctionRegistry,
) -> Result<crate::Neuron, String> {
use crate::{Stage, bias, scale};
use rkyv::api::high::access;
// Validate and get a borrowed archived view. `access` returns
// a Result<&ArchivedT, Error> when the `bytecheck` feature is
// enabled (we enabled it in Cargo.toml for the git dependency).
let archived = access::<ArchivedNeuronDef, rkyv::rancor::Error>(bytes)
.map_err(|e| format!("rkyv access error: {}", e))?;
let mut stages = Vec::new();
// archived.stages is an Archived<Vec<ArchivedStageDef>> which
// exposes as_slice for borrowing.
for s in archived.stages.as_slice().iter() {
match s {
ArchivedStageDef::Named(name) => {
let name_str: &str = name.as_str();
if let Some(f) = registry.get(name_str) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown function '{}'", name_str));
}
}
ArchivedStageDef::Scale { w } => {
// archived numeric wrappers convert into native types
// via Into::<f32>::into or to_native depending on
// the underlying wrapper. `into()` works for the
// rend endian wrappers.
let w_val: f32 = (*w).into();
stages.push(Stage::new(scale(w_val)));
}
ArchivedStageDef::Bias { b } => {
let b_val: f32 = (*b).into();
stages.push(Stage::new(bias(b_val)));
}
ArchivedStageDef::Factory { name, params } => {
let name_str: &str = name.as_str();
// map archived param slice to owned Vec<f32>
let params_vec: Vec<f32> =
params.as_slice().iter().map(|v| (*v).into()).collect();
if let Some(f) = registry.call_factory(name_str, ¶ms_vec) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown factory '{}'", name_str));
}
}
}
}
let output_stage = match &archived.output {
ArchivedStageDef::Named(name) => {
let name_str: &str = name.as_str();
if let Some(f) = registry.get(name_str) {
Stage::from_arc(f)
} else {
return Err(format!("unknown function '{}'", name_str));
}
}
ArchivedStageDef::Scale { w } => {
let w_val: f32 = (*w).into();
Stage::new(scale(w_val))
}
ArchivedStageDef::Bias { b } => {
let b_val: f32 = (*b).into();
Stage::new(bias(b_val))
}
ArchivedStageDef::Factory { name, params } => {
let name_str: &str = name.as_str();
let params_vec: Vec<f32> =
params.as_slice().iter().map(|v| (*v).into()).collect();
if let Some(f) = registry.call_factory(name_str, ¶ms_vec) {
Stage::from_arc(f)
} else {
return Err(format!("unknown factory '{}'", name_str));
}
}
};
Ok(crate::Neuron::new(stages, output_stage))
}
}
// Unsafe unchecked rehydrate path: deserialize without validation and
// convert to a runtime `Neuron`. This path is behind the
// `rkyv_unchecked` feature and should only be enabled for trusted data.
#[cfg(feature = "rkyv_unchecked")]
impl NeuronDef {
/// Unsafe unchecked rehydrate: deserialize without validation and
/// convert to a runtime `Neuron`. This avoids the bytecheck
/// validation overhead by using `from_bytes_unchecked`. It returns
/// owned `NeuronDef` instances (no zero-copy), but is faster for
/// trusted inputs.
/// # Safety
///
/// Caller must ensure `bytes` contains a valid archived `NeuronDef`.
/// This function uses `rkyv::from_bytes_unchecked` which does not
/// perform validation; providing invalid data is undefined behavior.
pub unsafe fn from_bytes_unchecked_to_neuron(
bytes: &[u8],
registry: &crate::FunctionRegistry,
) -> Result<crate::Neuron, String> {
// SAFETY: caller is responsible for ensuring `bytes` is a valid
// archived representation of `NeuronDef`.
let def: Self = unsafe {
rkyv::from_bytes_unchecked::<Self, rkyv::rancor::Error>(bytes).map_err(
|e: rkyv::rancor::Error| format!("rkyv unchecked deserialize error: {}", e),
)?
};
def.to_neuron(registry)
}
}
}
#[cfg(not(feature = "rkyv"))]
mod imp {
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
pub enum StageDef {
Named(String),
Scale { w: f32 },
Bias { b: f32 },
Factory { name: String, params: Vec<f32> },
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, bincode::Encode, bincode::Decode)]
pub struct NeuronDef {
pub version: u32,
pub stages: Vec<StageDef>,
pub output: StageDef,
}
impl NeuronDef {
pub fn new(stages: Vec<StageDef>, output: StageDef) -> Self {
Self {
version: 1,
stages,
output,
}
}
pub fn to_bytes(&self) -> Result<Vec<u8>, bincode::error::EncodeError> {
bincode::encode_to_vec(self, bincode::config::standard())
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, bincode::error::DecodeError> {
let (result, _len) = bincode::decode_from_slice(bytes, bincode::config::standard())?;
Ok(result)
}
pub fn to_neuron(
&self,
registry: &crate::FunctionRegistry,
) -> Result<crate::Neuron, String> {
use crate::{Stage, bias, scale};
let mut stages = Vec::new();
for s in &self.stages {
match s {
StageDef::Named(name) => {
if let Some(f) = registry.get(name) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown function '{}'", name));
}
}
StageDef::Scale { w } => stages.push(Stage::new(scale(*w))),
StageDef::Bias { b } => stages.push(Stage::new(bias(*b))),
StageDef::Factory { name, params } => {
if let Some(f) = registry.call_factory(name, params) {
stages.push(Stage::from_arc(f));
} else {
return Err(format!("unknown factory '{}'", name));
}
}
}
}
let output_stage = match &self.output {
StageDef::Named(name) => {
if let Some(f) = registry.get(name) {
Stage::from_arc(f)
} else {
return Err(format!("unknown function '{}'", name));
}
}
StageDef::Scale { w } => Stage::new(scale(*w)),
StageDef::Bias { b } => Stage::new(bias(*b)),
StageDef::Factory { name, params } => {
if let Some(f) = registry.call_factory(name, params) {
Stage::from_arc(f)
} else {
return Err(format!("unknown factory '{}'", name));
}
}
};
Ok(crate::Neuron::new(stages, output_stage))
}
}
}
pub use imp::{NeuronDef, StageDef};