This repository was archived by the owner on Mar 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 434
Add SavedModelBundle API #90
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ use std::ffi::CString; | |
| use std::marker; | ||
| use std::path::Path; | ||
| use std::ptr; | ||
| use super::{Buffer, BufferTrait}; | ||
| use super::Code; | ||
| use super::DataType; | ||
| use super::Graph; | ||
|
|
@@ -16,6 +17,64 @@ use super::Status; | |
| use super::Tensor; | ||
| use super::TensorType; | ||
|
|
||
| /// Aggregation type for a saved model bundle. | ||
| #[derive(Debug)] | ||
| pub struct SavedModelBundle { | ||
| /// The loaded session. | ||
| pub session: Session, | ||
| /// A meta graph defition as raw protocol buffer. | ||
| pub meta_graph_def: Vec<u8>, | ||
| } | ||
|
|
||
| impl SavedModelBundle { | ||
|
|
||
| /// Loads a session from an exported model, creating a bundle | ||
| pub fn load<P: AsRef<Path>, Tag: AsRef<str>, Tags: IntoIterator<Item = Tag>> | ||
| (options: &SessionOptions, | ||
| tags: Tags, | ||
| graph: &mut Graph, | ||
| export_dir: P) | ||
| -> Result<SavedModelBundle> { | ||
| let mut status = Status::new(); | ||
|
|
||
| let export_dir_cstr = | ||
| try!(export_dir.as_ref() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For new code, ? is preferred over try!. |
||
| .to_str() | ||
| .and_then(|s| CString::new(s.as_bytes()).ok()) | ||
| .ok_or_else(|| invalid_arg!("Invalid export directory path"))); | ||
|
|
||
| let tags_cstr: Vec<_> = try!(tags.into_iter() | ||
| .map(|t| CString::new(t.as_ref())) | ||
| .collect::<::std::result::Result<_, _>>() | ||
| .map_err(|_| invalid_arg!("Invalid tag name"))); | ||
| let tags_ptr: Vec<*const c_char> = tags_cstr.iter().map(|t| t.as_ptr()).collect(); | ||
|
|
||
| // The empty TF_Buffer will be filled by LoadSessionFromSavedModel | ||
| let mut meta = unsafe { Buffer::<u8>::from_ptr(ptr::null_mut(), 0) }; | ||
|
|
||
| let inner = unsafe { | ||
| tf::TF_LoadSessionFromSavedModel(options.inner, | ||
| ptr::null(), | ||
| export_dir_cstr.as_ptr(), | ||
| tags_ptr.as_ptr(), | ||
| tags_ptr.len() as c_int, | ||
| graph.inner(), | ||
| meta.inner_mut(), | ||
| status.inner()) | ||
| }; | ||
| if inner.is_null() { | ||
| Err(status) | ||
| } else { | ||
| let session = Session { inner: inner }; | ||
| Ok(SavedModelBundle { | ||
| session: session, | ||
| meta_graph_def: Vec::from(meta.as_ref()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// Manages a single graph and execution. | ||
| #[derive(Debug)] | ||
| pub struct Session { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: "definition"