|
| 1 | +use crate::{Result, Status}; |
| 2 | +use std::ffi::CString; |
| 3 | +use tensorflow_sys as tf; |
| 4 | + |
| 5 | +/// PluggableDeviceLibrary handler. |
| 6 | +#[derive(Debug)] |
| 7 | +pub struct PluggableDeviceLibrary { |
| 8 | + inner: *mut tf::TF_Library, |
| 9 | +} |
| 10 | + |
| 11 | +impl PluggableDeviceLibrary { |
| 12 | + /// Load the library specified by library_filename and register the pluggable |
| 13 | + /// device and related kernels present in that library. This function is not |
| 14 | + /// supported on embedded on mobile and embedded platforms and will fail if |
| 15 | + /// called. |
| 16 | + /// |
| 17 | + /// Pass "library_filename" to a platform-specific mechanism for dynamically |
| 18 | + /// loading a library. The rules for determining the exact location of the |
| 19 | + /// library are platform-specific and are not documented here. |
| 20 | + pub fn load(library_filename: &str) -> Result<PluggableDeviceLibrary> { |
| 21 | + let status = Status::new(); |
| 22 | + let library_filename = CString::new(library_filename)?; |
| 23 | + let lib_handle = |
| 24 | + unsafe { tf::TF_LoadPluggableDeviceLibrary(library_filename.as_ptr(), status.inner) }; |
| 25 | + status.into_result()?; |
| 26 | + |
| 27 | + Ok(PluggableDeviceLibrary { inner: lib_handle }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Drop for PluggableDeviceLibrary { |
| 32 | + /// Frees the memory associated with the library handle. |
| 33 | + /// Does NOT unload the library. |
| 34 | + fn drop(&mut self) { |
| 35 | + unsafe { |
| 36 | + tf::TF_DeletePluggableDeviceLibraryHandle(self.inner); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod tests { |
| 43 | + use super::*; |
| 44 | + |
| 45 | + #[ignore] |
| 46 | + #[test] |
| 47 | + fn load_pluggable_device_library() { |
| 48 | + let library_filename = "path-to-library"; |
| 49 | + let pluggable_divice_library = PluggableDeviceLibrary::load(library_filename); |
| 50 | + dbg!(&pluggable_divice_library); |
| 51 | + assert!((pluggable_divice_library.is_ok())); |
| 52 | + } |
| 53 | +} |
0 commit comments