pykeio / ort

Fast ML inference & training for Rust with ONNX Runtime

Home Page:https://ort.pyke.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SessionInputs::ValueMap should have a String, not a &'static str.

alicewith opened this issue · comments

Hi, all. I'm happy to use this nice library thanks to the author and contributors.
I'm using ort@2.0.0-alpha.4 upon Rust 1.75.0, but I encountered some problem when I use inputs! {} macro; it can't extend dynamically due to &'static str.
I guessed that since inputs! {} uses SessionInputs::ValueMap, and the ValueMap has &'static str, not String, it occurs some problem including what I encountered.

For example, see:

use std::error::Error;
use ort::{inputs, Value};
use ndarray::Array1;

fn main() -> Result<(), Box<dyn Error>>{
    let mut input = inputs! {
        "foo" => Value::from_array(Array1::from_vec(vec![42.0]))?,
    }?;

    for i in 1..=5 {
        let (key, value) = (format!("bar{i}"), i * 3);

        // each of the below line is problematic.
        input.insert(&key, Value::from_array(Array1::from_vec(vec![value]))?);
        input.insert(key, Value::from_array(Array1::from_vec(vec![value]))?);
    }

    println!("{:#?}", input);

    Ok(())
}

In above code, it is impossible to extend inputs! {...} at runtime due to &'static str.

I suggest that to change ValueMap(HashMap<&'static str, Value>) to ValueMap(HashMap<String, Value>) is fine. Thanks.

I'm against just using String as I want to minimize the number of allocations in hot paths. Not entirely sure what to do about this scenario, maybe I'll pull in compact-str.

Not entirely sure what to do about this scenario

The scenario is that the keys to pass to the model are many, and each of the key has a string-formed index from 0 to N, as shown in my code.
Due to simplicity, I want to iterate and insert into inputs! {...}.

maybe I'll pull in compact-str.

I'll check that. Thanks.