-
Notifications
You must be signed in to change notification settings - Fork 218
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
Tensor data preparation #752
Comments
The problem is on the input_ids. From the look of it it is an Array of [i64;8], and you need an Array of i64. I thing from_shape_vec does not do what you think it does :) look at arr2 maybe (or even tract |
Thank you for the quick feedback! I was originally trying to use
|
This is because 34 is tool long for the trait initializer. OK. from_shape_vec assumes that all your items are in a single vector, so you would need to flatten the nested structure you have been using (giving the function a single flat Alternatively, you can also try from_shape_fn() |
Thank you @kali , flattening the vector helps getting past the compilation error. I am now unfortunately faced with a runtime error. The updated code I am attempting to run is: Rust versionuse std::path::Path;
use tract_onnx::prelude::*;
fn main() -> anyhow::Result<()> {
let model_path = Path::new("path/to/model_optimized.onnx");
let batch_symbol = Symbol::new('N');
let sequence_symbol = Symbol::new('S');
let model = onnx()
.model_for_path(model_path)?
.with_input_fact(
0,
InferenceFact::dt_shape(
i64::datum_type(),
tvec!(batch_symbol.to_dim(), sequence_symbol.to_dim()),
),
)?
.with_input_fact(
1,
InferenceFact::dt_shape(
i64::datum_type(),
tvec!(batch_symbol.to_dim(), sequence_symbol.to_dim()),
),
)?
.into_optimized()?
.into_runnable()?;
let input_ids = tract_ndarray::Array2::from_shape_vec(
(4, 34),
vec![
0i64, 10285i64, 186i64, 38i64, 7084i64, 127i64, 6894i64, 1732i64, 8i64, 655i64, 187i64,
172i64, 127i64, 1028i64, 34i64, 57, 27169i64, 1295i64, 8378i64, 38i64, 304i64, 110i64,
1553i64, 4i64, 2i64, 2i64, 713i64, 2788i64, 16i64, 59i64, 1830i64, 4i64, 2i64, 1i64,
0i64, 10285i64, 186i64, 38i64, 7084i64, 127i64, 6894i64, 1732i64, 8i64, 655i64, 187i64,
172i64, 127i64, 1028i64, 34i64, 57, 27169i64, 1295i64, 8378i64, 38i64, 304i64, 110i64,
1553i64, 4i64, 2i64, 2i64, 713i64, 2788i64, 16i64, 59i64, 998i64, 4i64, 2i64, 1i64,
0i64, 10285i64, 186i64, 38i64, 7084i64, 127i64, 6894i64, 1732i64, 8i64, 655i64, 187i64,
172i64, 127i64, 1028i64, 34i64, 57, 27169i64, 1295i64, 8378i64, 38i64, 304i64, 110i64,
1553i64, 4i64, 2i64, 2i64, 713i64, 2788i64, 16i64, 59i64, 19892i64, 4i64, 2i64, 1i64,
0i64, 10285i64, 186i64, 38i64, 7084i64, 127i64, 6894i64, 1732i64, 8i64, 655i64, 187i64,
172i64, 127i64, 1028i64, 34i64, 57, 27169i64, 1295i64, 8378i64, 38i64, 304i64, 110i64,
1553i64, 4i64, 2i64, 2i64, 713i64, 2788i64, 16i64, 59i64, 1316i64, 899i64, 4i64, 2i64,
],
)?
.into();
let attention_mask = tract_ndarray::Array2::from_shape_vec(
(4, 34),
vec![
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1, 1i64,
1i64, 1i64, 1i64, 1i64, 0i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1, 1i64, 1i64, 1i64, 1i64, 1i64, 0i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1, 1i64, 1i64, 1i64, 1i64, 1i64,
0i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1i64, 1,
1i64, 1i64, 1i64, 1i64, 1i64, 1i64,
],
)?
.into();
let result = model.run(tvec!(input_ids, attention_mask))?;
println!("{:?}", result);
Ok(())
} with error:
The model works fine in Python/ONNX. I am uploading it for reference at https://drive.google.com/file/d/19Rnzp6hT477T307tEX6TPdJliJi1ZJhT/view?usp=sharing (shared under the same Apache 2.0 license as the original: https://huggingface.co/cross-encoder/nli-distilroberta-base) Python versionfrom onnxruntime import GraphOptimizationLevel, InferenceSession, SessionOptions, get_all_providers
from onnxruntime.transformers import optimizer
from pathlib import Path
from transformers import RobertaTokenizerFast
model_dir = Path("path/to/")
optimized_model_name = model_dir / "model_optimized.onnx"
tokenizer = RobertaTokenizerFast.from_pretrained(""cross-encoder/nli-distilroberta-base")
options = SessionOptions()
options.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL
session = InferenceSession(str(optimized_model_name), options, providers=["CPUExecutionProvider"])
input_texts = [
"This is a pair of sentences",
"This is sentence 2"
]
encoded_input = tokenizer(input_texts , padding=True, truncation=True, return_tensors="pt")
encoded_input = {key: np.array(value, dtype=np.int64) for key, value in encoded_input.items()}
output = session.run(None, encoded_input)
output Note that I am directly including the tokens in the Rust version to avoid importing tokenization dependencies. |
Thanks for the detailed report, I will have a look. |
I think #757 should fix the last issue. I've just cut a 0.17.1 with the fix. |
Thank you @kali , Is there a change with compiler dependencies requirements?? I am faced with
|
I don't know :) This is in |
This is because of my update in #748. They (prost) added (unfortunately) a dependency on source-protobuf, and thus require cmake to build it. If you install |
@guillaume-be are you ok with closing this ? |
Thank you @kali - yes this can be closed. I don't want to impose |
Hello,
I am trying to load a model and am unsure how to prepare the input in order to pass it to a model. I tried referring to the batched example provided in https://github.com/sonos/tract/blob/main/examples/onnx-mobilenet-v2-batch/src/main.rs but was not able to have a solution running. Below is a minimal example causing issues:
I am then facing the error:
Is there an additional conversion step required when using
tract_ndarray::Array2::from_shape_vec
to prepare the input?The text was updated successfully, but these errors were encountered: