llama-gguf 0.14.0

A high-performance Rust implementation of llama.cpp - LLM inference engine with full GGUF support
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use crate::backend::{BackendError, BackendResult};
use hailort_sys::constants::HAILO_MAX_NETWORK_GROUPS;
use hailort_sys::enums::{HAILO_D2H_STREAM, HAILO_FORMAT_ORDER_AUTO, HAILO_FORMAT_TYPE_AUTO, HAILO_H2D_STREAM};
use hailort_sys::ffi::{
    hailo_activate_network_group, hailo_configure_vdevice, hailo_create_hef_file, hailo_create_input_vstreams,
    hailo_create_output_vstreams, hailo_create_vdevice, hailo_deactivate_network_group, hailo_get_default_vstream_params,
    hailo_get_library_version, hailo_get_status_message, hailo_hef_get_network_group_infos,
    hailo_hef_get_vstream_infos, hailo_init_configure_params_by_vdevice, hailo_init_vdevice_params,
    hailo_input_vstream_write, hailo_output_vstream_read, hailo_release_hef, hailo_release_input_vstream,
    hailo_release_network_group, hailo_release_vdevice, hailo_scan_devices,
};
use hailort_sys::handles::{
    hailo_activated_network_group, hailo_configured_network_group, hailo_hef, hailo_input_vstream,
    hailo_output_vstream, hailo_vdevice,
};
use hailort_sys::status::HAILO_SUCCESS;
use hailort_sys::types::{
    hailo_activate_network_group_params_t, hailo_configure_params_t, hailo_input_vstream_params_by_name_t,
    hailo_network_group_info_t, hailo_output_vstream_params_by_name_t, hailo_vdevice_params_t,
    hailo_version_t, hailo_vstream_info_t,
};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::path::Path;
use tracing;

pub fn check_status(status: hailort_sys::status::hailo_status, context: &str) -> BackendResult<()> {
    if status == HAILO_SUCCESS {
        return Ok(());
    }
    let msg = unsafe {
        let ptr = hailo_get_status_message(status);
        if ptr.is_null() {
            format!("Hailo error {} in {}", status, context)
        } else {
            CStr::from_ptr(ptr).to_string_lossy().into_owned()
        }
    };
    Err(BackendError::OperationFailed(format!("{}: {}", context, msg)))
}

pub fn check_device_available() -> BackendResult<()> {
    let mut count: usize = 0;
    let status = unsafe {
        hailo_scan_devices(
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            &mut count as *mut usize,
        )
    };
    check_status(status, "hailo_scan_devices")?;
    if count == 0 {
        return Err(BackendError::NotAvailable(
            "No Hailo devices found".to_string(),
        ));
    }
    tracing::info!("Hailo device check: {} device(s) available", count);
    Ok(())
}

#[derive(Debug, Clone)]
pub struct HailoDeviceInfo {
    pub num_devices: usize,
    pub library_version: String,
}

pub struct HefHandle {
    hef: hailo_hef,
    configured_network_group: hailo_configured_network_group,
    activated_network_group: hailo_activated_network_group,
    input_vstreams: Vec<hailo_input_vstream>,
    output_vstreams: Vec<hailo_output_vstream>,
}

impl HefHandle {
    pub fn write_input(&self, data: &[u8]) -> BackendResult<()> {
        self.write_input_idx(0, data)
    }

    pub fn read_output(&self, buf: &mut [u8]) -> BackendResult<()> {
        self.read_output_idx(0, buf)
    }

    pub fn write_input_idx(&self, idx: usize, data: &[u8]) -> BackendResult<()> {
        let vstream = self
            .input_vstreams
            .get(idx)
            .copied()
            .ok_or_else(|| BackendError::InvalidArgument(format!("Invalid input vstream index {}", idx)))?;
        let status = unsafe { hailo_input_vstream_write(vstream, data.as_ptr() as *const _, data.len()) };
        check_status(status, "hailo_input_vstream_write")
    }

    pub fn read_output_idx(&self, idx: usize, buf: &mut [u8]) -> BackendResult<()> {
        let vstream = self
            .output_vstreams
            .get(idx)
            .copied()
            .ok_or_else(|| BackendError::InvalidArgument(format!("Invalid output vstream index {}", idx)))?;
        let status = unsafe { hailo_output_vstream_read(vstream, buf.as_mut_ptr() as *mut _, buf.len()) };
        check_status(status, "hailo_output_vstream_read")
    }
}

impl Drop for HefHandle {
    fn drop(&mut self) {
        for &vstream in &self.output_vstreams {
            let status = unsafe { hailort_sys::ffi::hailo_release_output_vstream(vstream) };
            if status != HAILO_SUCCESS {
                let msg = unsafe {
                    let ptr = hailo_get_status_message(status);
                    if ptr.is_null() {
                        format!("status {}", status)
                    } else {
                        CStr::from_ptr(ptr).to_string_lossy().into_owned()
                    }
                };
                tracing::warn!("hailo_release_output_vstream failed: {}", msg);
            }
        }
        for &vstream in &self.input_vstreams {
            let status = unsafe { hailo_release_input_vstream(vstream) };
            if status != HAILO_SUCCESS {
                let msg = unsafe {
                    let ptr = hailo_get_status_message(status);
                    if ptr.is_null() {
                        format!("status {}", status)
                    } else {
                        CStr::from_ptr(ptr).to_string_lossy().into_owned()
                    }
                };
                tracing::warn!("hailo_release_input_vstream failed: {}", msg);
            }
        }
        let status = unsafe { hailo_deactivate_network_group(self.activated_network_group) };
        if status != HAILO_SUCCESS {
            let msg = unsafe {
                let ptr = hailo_get_status_message(status);
                if ptr.is_null() {
                    format!("status {}", status)
                } else {
                    CStr::from_ptr(ptr).to_string_lossy().into_owned()
                }
            };
            tracing::warn!("hailo_deactivate_network_group failed: {}", msg);
        }
        let status = unsafe { hailo_release_network_group(self.configured_network_group) };
        if status != HAILO_SUCCESS {
            let msg = unsafe {
                let ptr = hailo_get_status_message(status);
                if ptr.is_null() {
                    format!("status {}", status)
                } else {
                    CStr::from_ptr(ptr).to_string_lossy().into_owned()
                }
            };
            tracing::warn!("hailo_release_network_group failed: {}", msg);
        }
        let status = unsafe { hailo_release_hef(self.hef) };
        if status != HAILO_SUCCESS {
            let msg = unsafe {
                let ptr = hailo_get_status_message(status);
                if ptr.is_null() {
                    format!("status {}", status)
                } else {
                    CStr::from_ptr(ptr).to_string_lossy().into_owned()
                }
            };
            tracing::warn!("hailo_release_hef failed: {}", msg);
        }
    }
}

pub struct HailoContext {
    vdevice: hailo_vdevice,
    device_info: HailoDeviceInfo,
}

impl HailoContext {
    pub fn new() -> BackendResult<Self> {
        check_device_available()?;

        let mut vdevice_params: hailo_vdevice_params_t = unsafe { std::mem::zeroed() };
        unsafe {
            hailo_init_vdevice_params(&mut vdevice_params as *mut _);
        }

        let mut vdevice: hailo_vdevice = std::ptr::null_mut();
        let status = unsafe {
            hailo_create_vdevice(&mut vdevice_params as *mut _, &mut vdevice as *mut _)
        };
        check_status(status, "hailo_create_vdevice")?;

        let mut version: hailo_version_t = unsafe { std::mem::zeroed() };
        let status = unsafe { hailo_get_library_version(&mut version as *mut _) };
        check_status(status, "hailo_get_library_version")?;

        let library_version = format!(
            "{}.{}.{}",
            version.major, version.minor, version.revision
        );

        let mut count: usize = 0;
        let status = unsafe {
            hailo_scan_devices(
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                &mut count as *mut usize,
            )
        };
        check_status(status, "hailo_scan_devices")?;

        let device_info = HailoDeviceInfo {
            num_devices: count,
            library_version,
        };

        Ok(Self {
            vdevice,
            device_info,
        })
    }

    pub fn load_hef(&self, path: &Path) -> BackendResult<HefHandle> {
        let path_cstr = CString::new(path.to_string_lossy().as_bytes())
            .map_err(|e| BackendError::InvalidArgument(format!("Invalid path: {}", e)))?;

        let mut hef: hailo_hef = std::ptr::null_mut();
        let status = unsafe { hailo_create_hef_file(&mut hef as *mut _, path_cstr.as_ptr()) };
        check_status(status, "hailo_create_hef_file")?;

        let mut ng_count: usize = 0;
        let status = unsafe {
            hailo_hef_get_network_group_infos(
                hef,
                std::ptr::null_mut(),
                &mut ng_count as *mut usize,
            )
        };
        check_status(status, "hailo_hef_get_network_group_infos (count)")?;

        if ng_count == 0 {
            unsafe { hailo_release_hef(hef) };
            return Err(BackendError::OperationFailed(
                "HEF has no network groups".to_string(),
            ));
        }

        let mut ng_infos: Vec<hailo_network_group_info_t> =
            vec![unsafe { std::mem::zeroed() }; ng_count];
        let status = unsafe {
            hailo_hef_get_network_group_infos(
                hef,
                ng_infos.as_mut_ptr(),
                &mut ng_count as *mut usize,
            )
        };
        check_status(status, "hailo_hef_get_network_group_infos")?;

        let ng_name = ng_infos[0].name;
        let ng_name_cstr = c_char_array_to_cstr(&ng_name);

        let mut configure_params: hailo_configure_params_t = unsafe { std::mem::zeroed() };
        let status = unsafe {
            hailo_init_configure_params_by_vdevice(self.vdevice, hef, &mut configure_params as *mut _)
        };
        check_status(status, "hailo_init_configure_params_by_vdevice")?;

        let mut network_groups: [hailo_configured_network_group; 1] =
            [std::ptr::null_mut(); 1];
        let mut ng_out_count: usize = HAILO_MAX_NETWORK_GROUPS;
        let status = unsafe {
            hailo_configure_vdevice(
                self.vdevice,
                hef,
                &mut configure_params as *mut _,
                network_groups.as_mut_ptr(),
                &mut ng_out_count as *mut usize,
            )
        };
        check_status(status, "hailo_configure_vdevice")?;

        let configured_network_group = network_groups[0];
        if configured_network_group.is_null() {
            unsafe { hailo_release_hef(hef) };
            return Err(BackendError::OperationFailed(
                "hailo_configure_vdevice returned null network group".to_string(),
            ));
        }

        let mut activate_params: hailo_activate_network_group_params_t = unsafe { std::mem::zeroed() };
        let mut activated: hailo_activated_network_group = std::ptr::null_mut();
        let status = unsafe {
            hailo_activate_network_group(
                configured_network_group,
                &mut activate_params as *mut _,
                &mut activated as *mut _,
            )
        };
        check_status(status, "hailo_activate_network_group")?;

        if activated.is_null() {
            unsafe { hailo_release_network_group(configured_network_group) };
            unsafe { hailo_release_hef(hef) };
            return Err(BackendError::OperationFailed(
                "hailo_activate_network_group returned null".to_string(),
            ));
        }

        let mut input_count: usize = 0;
        let status = unsafe {
            hailo_hef_get_vstream_infos(
                hef,
                ng_name_cstr.as_ptr(),
                std::ptr::null_mut(),
                &mut input_count as *mut usize,
            )
        };
        check_status(status, "hailo_hef_get_vstream_infos (input count)")?;

        let mut all_infos: Vec<hailo_vstream_info_t> = (0..input_count)
            .map(|_| unsafe { std::mem::zeroed() })
            .collect();
        let status = unsafe {
            hailo_hef_get_vstream_infos(
                hef,
                ng_name_cstr.as_ptr(),
                all_infos.as_mut_ptr(),
                &mut input_count as *mut usize,
            )
        };
        check_status(status, "hailo_hef_get_vstream_infos")?;

        let input_indices: Vec<usize> = all_infos
            .iter()
            .enumerate()
            .filter(|(_, i)| i.direction == HAILO_H2D_STREAM)
            .map(|(idx, _)| idx)
            .collect();
        let output_indices: Vec<usize> = all_infos
            .iter()
            .enumerate()
            .filter(|(_, i)| i.direction == HAILO_D2H_STREAM)
            .map(|(idx, _)| idx)
            .collect();

        let user_format = hailort_sys::types::hailo_format_t {
            type_: HAILO_FORMAT_TYPE_AUTO,
            order: HAILO_FORMAT_ORDER_AUTO,
            flags: hailort_sys::enums::HAILO_FORMAT_FLAGS_NONE,
        };

        let mut input_params: Vec<hailo_input_vstream_params_by_name_t> =
            Vec::with_capacity(input_indices.len());
        for &idx in &input_indices {
            let info = &all_infos[idx];
            let mut params: hailort_sys::types::hailo_vstream_params_t = unsafe { std::mem::zeroed() };
            let status = unsafe {
                hailo_get_default_vstream_params(
                    info as *const _,
                    user_format,
                    HAILO_H2D_STREAM,
                    &mut params as *mut _,
                )
            };
            check_status(status, "hailo_get_default_vstream_params (input)")?;
            let mut name_buf = [0i8; 128];
            let copy_len = info.name.len().min(name_buf.len());
            name_buf[..copy_len].copy_from_slice(&info.name[..copy_len]);
            input_params.push(hailo_input_vstream_params_by_name_t {
                name: name_buf,
                params,
            });
        }

        let mut output_params: Vec<hailo_output_vstream_params_by_name_t> =
            Vec::with_capacity(output_indices.len());
        for &idx in &output_indices {
            let info = &all_infos[idx];
            let mut params: hailort_sys::types::hailo_vstream_params_t = unsafe { std::mem::zeroed() };
            let status = unsafe {
                hailo_get_default_vstream_params(
                    info as *const _,
                    user_format,
                    HAILO_D2H_STREAM,
                    &mut params as *mut _,
                )
            };
            check_status(status, "hailo_get_default_vstream_params (output)")?;
            let mut name_buf = [0i8; 128];
            let copy_len = info.name.len().min(name_buf.len());
            name_buf[..copy_len].copy_from_slice(&info.name[..copy_len]);
            output_params.push(hailo_output_vstream_params_by_name_t {
                name: name_buf,
                params,
            });
        }

        let mut input_vstreams: Vec<hailo_input_vstream> =
            vec![std::ptr::null_mut(); input_params.len()];
        if !input_params.is_empty() {
            let status = unsafe {
                hailo_create_input_vstreams(
                    configured_network_group,
                    input_params.as_ptr(),
                    input_params.len(),
                    input_vstreams.as_mut_ptr(),
                )
            };
            check_status(status, "hailo_create_input_vstreams")?;
        }

        let mut output_vstreams: Vec<hailo_output_vstream> =
            vec![std::ptr::null_mut(); output_params.len()];
        if !output_params.is_empty() {
            let status = unsafe {
                hailo_create_output_vstreams(
                    configured_network_group,
                    output_params.as_ptr(),
                    output_params.len(),
                    output_vstreams.as_mut_ptr(),
                )
            };
            check_status(status, "hailo_create_output_vstreams")?;
        }

        Ok(HefHandle {
            hef,
            configured_network_group,
            activated_network_group: activated,
            input_vstreams,
            output_vstreams,
        })
    }

    pub fn device_info(&self) -> &HailoDeviceInfo {
        &self.device_info
    }
}

fn c_char_array_to_cstr(arr: &[c_char; 128]) -> CString {
    let len = arr.iter().position(|&c| c == 0).unwrap_or(arr.len());
    let slice = &arr[..len];
    let bytes: Vec<u8> = slice.iter().map(|&c| c as u8).collect();
    CString::new(bytes).unwrap_or_else(|_| CString::new("").unwrap())
}

impl Drop for HailoContext {
    fn drop(&mut self) {
        let status = unsafe { hailo_release_vdevice(self.vdevice) };
        if status != HAILO_SUCCESS {
            let msg = unsafe {
                let ptr = hailo_get_status_message(status);
                if ptr.is_null() {
                    format!("status {}", status)
                } else {
                    CStr::from_ptr(ptr).to_string_lossy().into_owned()
                }
            };
            tracing::warn!("hailo_release_vdevice failed: {}", msg);
        }
    }
}