Skip to content

Commit

Permalink
new: refactored to use trait object
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Oct 29, 2024
1 parent 4b980c5 commit fb66d76
Show file tree
Hide file tree
Showing 23 changed files with 1,064 additions and 831 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
protobuf_codegen::Codegen::new()
.pure()
.includes(["src"])
.input("src/core/onnx/protos/onnx.proto")
.input("src/core/handlers/onnx/protos/onnx.proto")
.cargo_out_dir("onnx-protos")
.run_from_script();
}
18 changes: 3 additions & 15 deletions src/cli/graph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::FileType;
use crate::core::handlers::Scope;

use super::GraphArgs;

Expand All @@ -9,18 +9,6 @@ pub(crate) fn graph(args: GraphArgs) -> anyhow::Result<()> {
args.output.display()
);

let forced_format = args.format.unwrap_or(FileType::Unknown);
let file_ext = args
.file_path
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or("")
.to_ascii_lowercase();

if !forced_format.is_onnx() && file_ext != "onnx" {
anyhow::bail!("this format does not embed graph information");
}

crate::core::onnx::create_graph(args.file_path, args.output)
crate::core::handlers::handler_for(args.format, &args.file_path, Scope::Inspection)?
.create_graph(&args.file_path, &args.output)
}
23 changes: 7 additions & 16 deletions src/cli/inspect.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::core::FileType;
use crate::core::handlers::Scope;

use super::InspectArgs;

pub(crate) fn inspect(args: InspectArgs) -> anyhow::Result<()> {
let handler =
crate::core::handlers::handler_for(args.format, &args.file_path, Scope::Inspection)?;

if !args.quiet {
println!(
"Inspecting {:?} (detail={:?}{}):\n",
"Inspecting {:?} (format={}, detail={:?}{}):\n",
args.file_path,
handler.file_type(),
args.detail,
args.filter
.as_ref()
Expand All @@ -15,20 +19,7 @@ pub(crate) fn inspect(args: InspectArgs) -> anyhow::Result<()> {
);
}

let forced_format = args.format.unwrap_or(FileType::Unknown);
let inspection = if forced_format.is_safetensors()
|| crate::core::safetensors::is_safetensors(&args.file_path)
{
crate::core::safetensors::inspect(args.file_path, args.detail, args.filter)?
} else if forced_format.is_onnx() || crate::core::onnx::is_onnx(&args.file_path) {
crate::core::onnx::inspect(args.file_path, args.detail, args.filter)?
} else if forced_format.is_gguf() || crate::core::gguf::is_gguf(&args.file_path) {
crate::core::gguf::inspect(args.file_path, args.detail, args.filter)?
} else if forced_format.is_pytorch() || crate::core::pytorch::is_pytorch(&args.file_path) {
crate::core::pytorch::inspect(args.file_path, args.detail, args.filter)?
} else {
anyhow::bail!("unsupported file format")
};
let inspection = handler.inspect(&args.file_path, args.detail, args.filter)?;

if !args.quiet {
println!("file type: {}", inspection.file_type);
Expand Down
34 changes: 7 additions & 27 deletions src/cli/signing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{signing::Manifest, FileType};
use crate::core::{handlers::Scope, signing::Manifest};

use super::{CreateKeyArgs, SignArgs, VerifyArgs};

Expand All @@ -8,19 +8,9 @@ pub(crate) fn create_key(args: CreateKeyArgs) -> anyhow::Result<()> {

pub(crate) fn sign(args: SignArgs) -> anyhow::Result<()> {
let signing_key = crate::core::signing::load_key(&args.key_path)?;

let forced_format = args.format.unwrap_or(FileType::Unknown);
let mut paths_to_sign = if forced_format.is_safetensors()
|| crate::core::safetensors::is_safetensors(&args.file_path)
|| crate::core::safetensors::is_safetensors_index(&args.file_path)
{
crate::core::safetensors::paths_to_sign(&args.file_path)?
} else if forced_format.is_onnx() || crate::core::onnx::is_onnx(&args.file_path) {
crate::core::onnx::paths_to_sign(&args.file_path)?
} else if forced_format.is_gguf() || crate::core::gguf::is_gguf(&args.file_path) {
crate::core::gguf::paths_to_sign(&args.file_path)?
} else if forced_format.is_pytorch() || crate::core::pytorch::is_pytorch(&args.file_path) {
crate::core::pytorch::paths_to_sign(&args.file_path)?
let handler = crate::core::handlers::handler_for(args.format, &args.file_path, Scope::Signing);
let mut paths_to_sign = if let Ok(handler) = handler {
handler.paths_to_sign(&args.file_path)?
} else {
println!("Warning: Unrecognized file format. Signing this file does not ensure that the model data will be signed in its entirety.");
vec![args.file_path.clone()]
Expand Down Expand Up @@ -65,22 +55,12 @@ pub(crate) fn verify(args: VerifyArgs) -> anyhow::Result<()> {
let raw = std::fs::read_to_string(&manifest_path)?;
let ref_manifest: Manifest = serde_json::from_str(&raw)?;

let forced_format = args.format.unwrap_or(FileType::Unknown);

let raw = std::fs::read(&args.key_path)?;
let mut manifest = Manifest::for_verifying(raw);

let mut paths_to_verify = if forced_format.is_safetensors()
|| crate::core::safetensors::is_safetensors(&args.file_path)
|| crate::core::safetensors::is_safetensors_index(&args.file_path)
{
crate::core::safetensors::paths_to_sign(&args.file_path)?
} else if forced_format.is_onnx() || crate::core::onnx::is_onnx(&args.file_path) {
crate::core::onnx::paths_to_sign(&args.file_path)?
} else if forced_format.is_gguf() || crate::core::gguf::is_gguf(&args.file_path) {
crate::core::gguf::paths_to_sign(&args.file_path)?
} else if forced_format.is_pytorch() || crate::core::pytorch::is_pytorch(&args.file_path) {
crate::core::pytorch::paths_to_sign(&args.file_path)?
let handler = crate::core::handlers::handler_for(args.format, &args.file_path, Scope::Signing);
let mut paths_to_verify = if let Ok(handler) = handler {
handler.paths_to_sign(&args.file_path)?
} else {
println!("Warning: Unrecognized file format. Signing this file does not ensure that the model data will be signed in its entirety.");
vec![args.file_path.clone()]
Expand Down
4 changes: 2 additions & 2 deletions src/core/docker/inspection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::Path;

use blake2::{Blake2b512, Digest};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl Inspector {

pub fn run(
&self,
file_path: PathBuf,
file_path: &Path,
additional_files: Vec<String>,
detail: DetailLevel,
filter: Option<String>,
Expand Down
125 changes: 0 additions & 125 deletions src/core/gguf/inspect.rs

This file was deleted.

35 changes: 0 additions & 35 deletions src/core/gguf/mod.rs

This file was deleted.

Loading

0 comments on commit fb66d76

Please sign in to comment.