-
Notifications
You must be signed in to change notification settings - Fork 14
Add inspect command #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
javiermtorres
wants to merge
4
commits into
main
Choose a base branch
from
201-inspect-encoderfile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add inspect command #253
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use std::fs::File; | ||
| use std::io::BufReader; | ||
| use std::path::Path; | ||
|
|
||
| use serde::Serialize; | ||
| use serde_json::to_string_pretty; | ||
|
|
||
| use anyhow::Result; | ||
|
|
||
| use crate::{ | ||
| common::{Config, ModelConfig}, | ||
| runtime::load_assets, | ||
| }; | ||
|
|
||
| // inspect struct with info | ||
|
|
||
| #[derive(Debug, Serialize)] | ||
| pub struct InspectInfo { | ||
| pub model_config: ModelConfig, | ||
| pub encoderfile_config: Config, | ||
| } | ||
|
|
||
| pub fn inspect_encoderfile(path_str: &String) -> Result<String> { | ||
| let file = File::open(Path::new(&path_str))?; | ||
| let mut file = BufReader::new(file); | ||
| let mut loader = load_assets(&mut file)?; | ||
|
|
||
| let config = loader.encoderfile_config()?; | ||
| let model_config = loader.model_config()?; | ||
|
|
||
| Ok(to_string_pretty(&InspectInfo { | ||
| model_config, | ||
| encoderfile_config: config, | ||
| })?) | ||
| } |
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| use anyhow::{Context, Result, bail}; | ||
|
|
||
| use encoderfile::build_cli::cli::{GlobalArguments, inspect_encoderfile}; | ||
| use std::{ | ||
| fs, | ||
| path::Path, | ||
| process::{Command, Output}, | ||
| }; | ||
| use tempfile::tempdir; | ||
|
|
||
| const BINARY_NAME: &str = "test.encoderfile"; | ||
|
|
||
| fn config(model_name: &String, model_path: &Path, output_path: &Path) -> String { | ||
| format!( | ||
| r##" | ||
| encoderfile: | ||
| name: {:?} | ||
| path: {:?} | ||
| model_type: token_classification | ||
| output_path: {:?} | ||
| transform: | | ||
| --- Applies a softmax across token classification logits. | ||
| --- Each token classification is normalized independently. | ||
| --- | ||
| --- Args: | ||
| --- arr (Tensor): A tensor of shape [batch_size, n_tokens, n_labels]. | ||
| --- The softmax is applied along the third axis (n_labels). | ||
| --- | ||
| --- Returns: | ||
| --- Tensor: The input tensor with softmax-normalized embeddings. | ||
| ---@param arr Tensor | ||
| ---@return Tensor | ||
| function Postprocess(arr) | ||
| return arr:softmax(3) | ||
| end | ||
| "##, | ||
| model_name, model_path, output_path | ||
| ) | ||
| } | ||
|
|
||
| const MODEL_ASSETS_PATH: &str = "../models/token_classification"; | ||
|
|
||
| #[test] | ||
| fn test_inspect_encoderfile() -> Result<()> { | ||
| let dir = tempdir()?; | ||
| let path = dir | ||
| .path() | ||
| .canonicalize() | ||
| .expect("Failed to canonicalize temp path"); | ||
|
|
||
| let tmp_model_path = path.join("models").join("token_classification"); | ||
|
|
||
| let ef_config_path = path.join("encoderfile.yml"); | ||
| let encoderfile_path = path.join(BINARY_NAME); | ||
| let model_name = String::from("some-custom-name"); | ||
|
|
||
| // copy model assets to temp dir | ||
| copy_dir_all(MODEL_ASSETS_PATH, tmp_model_path.as_path()) | ||
| .expect("Failed to copy model assets to temp directory"); | ||
|
|
||
| if !tmp_model_path.join("model.onnx").exists() { | ||
| bail!( | ||
| "Path {:?} does not exist", | ||
| tmp_model_path.join("model.onnx") | ||
| ); | ||
| } | ||
|
|
||
| // compile base binary and copy to temp dir | ||
| let _ = Command::new("cargo") | ||
| .args(["build"]) | ||
| .status() | ||
| .expect("Failed to build encoderfile-runtime"); | ||
|
|
||
| let base_binary_path = fs::canonicalize("../target/debug/encoderfile-runtime") | ||
| .expect("Failed to canonicalize base binary path"); | ||
|
|
||
| let ef_binary_path = fs::canonicalize("../target/debug/encoderfile") | ||
| .expect("Failed to canonicalize base binary path"); | ||
|
|
||
| // write encoderfile config | ||
| let config = config( | ||
| &model_name, | ||
| tmp_model_path.as_path(), | ||
| encoderfile_path.as_path(), | ||
| ); | ||
|
|
||
| fs::write(ef_config_path.as_path(), config.as_bytes()) | ||
| .expect("Failed to write encoderfile config"); | ||
|
|
||
| let build_args = | ||
| encoderfile::build_cli::cli::test_build_args(ef_config_path.as_path(), base_binary_path); | ||
|
|
||
| // build encoderfile | ||
| let global_args = GlobalArguments::default(); | ||
|
|
||
| build_args | ||
| .run(&global_args) | ||
| .context("Failed to build encoderfile")?; | ||
|
|
||
| let ef_path_str = String::from( | ||
| encoderfile_path | ||
| .to_str() | ||
| .expect("Encoderfile path name failed to convert to string"), | ||
| ); | ||
|
|
||
| let _inspect_output = inspect_encoderfile(&ef_path_str)?; | ||
|
|
||
| let output = run_inspect_encoderfile( | ||
| ef_binary_path | ||
| .to_str() | ||
| .expect("Failed to create encoderfile binary path"), | ||
| &ef_path_str, | ||
| )?; | ||
|
|
||
| let stdout = String::from_utf8(output.stdout)?; | ||
| let stderr = String::from_utf8(output.stderr)?; | ||
|
|
||
| println!("STDOUT: {}", stdout); | ||
| println!("STDERR: {}", stderr); | ||
|
|
||
| let inspect_output_json = serde_json::from_str::<serde_json::Value>(&stdout) | ||
| .context("Failed to parse inspect output as JSON")?; | ||
| inspect_output_json | ||
| .get("encoderfile_config") | ||
| .and_then(|efc| efc.get("name")) | ||
| .and_then(|name| name.as_str()) | ||
| .filter(|name_str| *name_str == model_name.as_str()) | ||
| .ok_or_else(|| anyhow::anyhow!("Model name in inspect output does not match expected"))?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> { | ||
| let src = src.as_ref(); | ||
| let dst = dst.as_ref(); | ||
|
|
||
| fs::create_dir_all(dst).context(format!("Failed to create directory {:?}", &dst))?; | ||
|
|
||
| for entry in fs::read_dir(src)? { | ||
| let entry = entry?; | ||
| let ty = entry.file_type()?; | ||
| let dest_path = dst.join(entry.file_name()); | ||
|
|
||
| if ty.is_dir() { | ||
| copy_dir_all(entry.path(), dest_path.as_path()).context(format!( | ||
| "Failed to copy {:?} to {:?}", | ||
| entry.path(), | ||
| dest_path.as_path() | ||
| ))?; | ||
| } else { | ||
| fs::copy(entry.path(), dest_path.as_path()).context(format!( | ||
| "Failed to copy {:?} to {:?}", | ||
| entry.path(), | ||
| dest_path.as_path() | ||
| ))?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn run_inspect_encoderfile(path: &str, ef_path: &str) -> Result<Output> { | ||
| let mut cmd = Command::new(path); | ||
| cmd.arg("inspect").arg(ef_path); | ||
| println!("{:?}", cmd); | ||
| cmd.output().context("Failed inspect command") | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.