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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// granite-docling ONNX Rust Example with ORT crate
|
| 2 |
+
// Demonstrates how to use granite-docling ONNX model in Rust applications
|
| 3 |
+
|
| 4 |
+
use anyhow::Result;
|
| 5 |
+
use ort::{
|
| 6 |
+
execution_providers::ExecutionProvider,
|
| 7 |
+
session::{Session, builder::GraphOptimizationLevel},
|
| 8 |
+
inputs, value::TensorRef,
|
| 9 |
+
};
|
| 10 |
+
use ndarray::{Array1, Array2, Array4};
|
| 11 |
+
|
| 12 |
+
/// granite-docling ONNX inference engine
|
| 13 |
+
pub struct GraniteDoclingONNX {
|
| 14 |
+
session: Session,
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
impl GraniteDoclingONNX {
|
| 18 |
+
/// Load granite-docling ONNX model
|
| 19 |
+
pub fn new(model_path: &str) -> Result<Self> {
|
| 20 |
+
println!("Loading granite-docling ONNX model from: {}", model_path);
|
| 21 |
+
|
| 22 |
+
let session = Session::builder()?
|
| 23 |
+
.with_optimization_level(GraphOptimizationLevel::Level3)?
|
| 24 |
+
.with_execution_providers([
|
| 25 |
+
ExecutionProvider::DirectML, // Windows ML acceleration
|
| 26 |
+
ExecutionProvider::CUDA, // NVIDIA acceleration
|
| 27 |
+
ExecutionProvider::CPU, // Universal fallback
|
| 28 |
+
])?
|
| 29 |
+
.commit_from_file(model_path)?;
|
| 30 |
+
|
| 31 |
+
// Print model information
|
| 32 |
+
println!("Model loaded successfully:");
|
| 33 |
+
for (i, input) in session.inputs()?.iter().enumerate() {
|
| 34 |
+
println!(" Input {}: {} {:?}", i, input.name(), input.input_type());
|
| 35 |
+
}
|
| 36 |
+
for (i, output) in session.outputs()?.iter().enumerate() {
|
| 37 |
+
println!(" Output {}: {} {:?}", i, output.name(), output.output_type());
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
Ok(Self { session })
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/// Process document image to DocTags markup
|
| 44 |
+
pub async fn process_document(
|
| 45 |
+
&self,
|
| 46 |
+
document_image: Array4<f32>, // [batch, channels, height, width]
|
| 47 |
+
prompt: &str,
|
| 48 |
+
) -> Result<String> {
|
| 49 |
+
|
| 50 |
+
println!("Processing document with granite-docling...");
|
| 51 |
+
|
| 52 |
+
// Prepare text inputs (simplified tokenization)
|
| 53 |
+
let input_ids = self.tokenize_prompt(prompt)?;
|
| 54 |
+
let attention_mask = Array2::ones((1, input_ids.len()));
|
| 55 |
+
|
| 56 |
+
// Convert to required input format
|
| 57 |
+
let input_ids_2d = Array2::from_shape_vec(
|
| 58 |
+
(1, input_ids.len()),
|
| 59 |
+
input_ids.iter().map(|&x| x as i64).collect(),
|
| 60 |
+
)?;
|
| 61 |
+
|
| 62 |
+
// Run inference
|
| 63 |
+
let outputs = self.session.run(inputs![
|
| 64 |
+
"pixel_values" => TensorRef::from_array_view(&document_image.view())?,
|
| 65 |
+
"input_ids" => TensorRef::from_array_view(&input_ids_2d.view())?,
|
| 66 |
+
"attention_mask" => TensorRef::from_array_view(&attention_mask.view())?,
|
| 67 |
+
])?;
|
| 68 |
+
|
| 69 |
+
// Extract logits and decode to text
|
| 70 |
+
let logits = outputs["logits"].try_extract_tensor::<f32>()?;
|
| 71 |
+
let tokens = self.decode_logits_to_tokens(&logits)?;
|
| 72 |
+
let doctags = self.detokenize_to_doctags(&tokens)?;
|
| 73 |
+
|
| 74 |
+
println!("✅ Document processing complete");
|
| 75 |
+
Ok(doctags)
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/// Simple tokenization (in practice, use proper tokenizer)
|
| 79 |
+
fn tokenize_prompt(&self, prompt: &str) -> Result<Vec<u32>> {
|
| 80 |
+
// Simplified tokenization - in practice, load tokenizer.json
|
| 81 |
+
// and use proper HuggingFace tokenization
|
| 82 |
+
let tokens: Vec<u32> = prompt
|
| 83 |
+
.split_whitespace()
|
| 84 |
+
.enumerate()
|
| 85 |
+
.map(|(i, _)| (i + 1) as u32)
|
| 86 |
+
.collect();
|
| 87 |
+
|
| 88 |
+
Ok(tokens)
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
/// Decode logits to most likely tokens
|
| 92 |
+
fn decode_logits_to_tokens(&self, logits: &ndarray::ArrayViewD<f32>) -> Result<Vec<u32>> {
|
| 93 |
+
// Find argmax for each position
|
| 94 |
+
let tokens: Vec<u32> = logits
|
| 95 |
+
.axis_iter(ndarray::Axis(2))
|
| 96 |
+
.map(|logit_slice| {
|
| 97 |
+
logit_slice
|
| 98 |
+
.iter()
|
| 99 |
+
.enumerate()
|
| 100 |
+
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
|
| 101 |
+
.map(|(idx, _)| idx as u32)
|
| 102 |
+
.unwrap_or(0)
|
| 103 |
+
})
|
| 104 |
+
.collect();
|
| 105 |
+
|
| 106 |
+
Ok(tokens)
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
/// Convert tokens back to DocTags markup
|
| 110 |
+
fn detokenize_to_doctags(&self, tokens: &[u32]) -> Result<String> {
|
| 111 |
+
// In practice, use granite-docling tokenizer to convert tokens → text
|
| 112 |
+
// Then parse the text as DocTags markup
|
| 113 |
+
|
| 114 |
+
// Simplified example
|
| 115 |
+
let mock_doctags = format!(
|
| 116 |
+
"<doctag>\n <text>Document processed with {} tokens</text>\n</doctag>",
|
| 117 |
+
tokens.len()
|
| 118 |
+
);
|
| 119 |
+
|
| 120 |
+
Ok(mock_doctags)
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/// Preprocess document image for granite-docling inference
|
| 125 |
+
pub fn preprocess_document_image(image_path: &str) -> Result<Array4<f32>> {
|
| 126 |
+
// Load image and resize to 512x512 (SigLIP2 requirement)
|
| 127 |
+
// Normalize with SigLIP2 parameters
|
| 128 |
+
// Convert to [batch, channels, height, width] format
|
| 129 |
+
|
| 130 |
+
// Simplified example - in practice, use image processing library
|
| 131 |
+
let document_image = Array4::zeros((1, 3, 512, 512));
|
| 132 |
+
|
| 133 |
+
Ok(document_image)
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
#[tokio::main]
|
| 137 |
+
async fn main() -> Result<()> {
|
| 138 |
+
println!("granite-docling ONNX Rust Example");
|
| 139 |
+
|
| 140 |
+
// Load granite-docling ONNX model
|
| 141 |
+
let model_path = "granite-docling-258M-onnx/model.onnx";
|
| 142 |
+
let granite_docling = GraniteDoclingONNX::new(model_path)?;
|
| 143 |
+
|
| 144 |
+
// Preprocess document image
|
| 145 |
+
let document_image = preprocess_document_image("example_document.png")?;
|
| 146 |
+
|
| 147 |
+
// Process document
|
| 148 |
+
let prompt = "Convert this document to DocTags:";
|
| 149 |
+
let doctags = granite_docling.process_document(document_image, prompt).await?;
|
| 150 |
+
|
| 151 |
+
println!("Generated DocTags:");
|
| 152 |
+
println!("{}", doctags);
|
| 153 |
+
|
| 154 |
+
Ok(())
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// Cargo.toml dependencies:
|
| 158 |
+
/*
|
| 159 |
+
[dependencies]
|
| 160 |
+
ort = { version = "2.0.0-rc.10", features = ["directml", "cuda", "tensorrt"] }
|
| 161 |
+
ndarray = "0.15"
|
| 162 |
+
anyhow = "1.0"
|
| 163 |
+
tokio = { version = "1.0", features = ["full"] }
|
| 164 |
+
*/
|