ModelRuntime

Struct ModelRuntime 

Source
pub struct ModelRuntime { /* private fields */ }
Expand description

Model runtime for executing inference.

Implementations§

Source§

impl ModelRuntime

Source

pub fn new(config: ModelConfig) -> Self

Create a new model runtime.

Examples found in repository?
examples/full_inference_pipeline.rs (line 67)
55fn build_model() {
56    println!("2. Model Building");
57
58    let model_config = ModelConfig {
59        name: "ResNet18-Lite".to_string(),
60        input_shape: vec![1, 224, 224, 3],
61        output_shape: vec![1, 1000],
62        quant_format: QuantFormat::Int8,
63        optimization_level: OptimizationLevel::O3,
64        use_cache: true,
65    };
66
67    let runtime = ModelRuntime::new(model_config);
68    println!("   Model: {}", runtime.get_config().name);
69    println!("   Input: {:?}", runtime.input_shape());
70    println!("   Output: {:?}", runtime.output_shape());
71
72    let mut network = NeuralNetwork::new(runtime.get_config().name.clone());
73    
74    network.add_layer(Layer::new(
75        "stem_conv".to_string(),
76        LayerType::Convolution,
77        vec![1, 224, 224, 3],
78        vec![1, 112, 112, 64],
79    ));
80    
81    network.add_layer(Layer::new(
82        "residual_block_1".to_string(),
83        LayerType::PointwiseConvolution,
84        vec![1, 112, 112, 64],
85        vec![1, 112, 112, 64],
86    ));
87    
88    network.add_layer(Layer::new(
89        "residual_block_2".to_string(),
90        LayerType::PointwiseConvolution,
91        vec![1, 56, 56, 128],
92        vec![1, 56, 56, 128],
93    ));
94    
95    network.add_layer(Layer::new(
96        "global_avg_pool".to_string(),
97        LayerType::Pooling,
98        vec![1, 7, 7, 512],
99        vec![1, 512],
100    ));
101    
102    network.add_layer(Layer::new(
103        "classifier".to_string(),
104        LayerType::FullyConnected,
105        vec![1, 512],
106        vec![1, 1000],
107    ));
108
109    println!("   Layers: {}", network.layer_count());
110    println!("   Estimated TOPS: {:.6}\n", network.total_tops());
111}
Source

pub fn load_from_path(_path: &str) -> Result<Self>

Load model from path.

Source

pub fn get_config(&self) -> &ModelConfig

Get model configuration.

Examples found in repository?
examples/full_inference_pipeline.rs (line 68)
55fn build_model() {
56    println!("2. Model Building");
57
58    let model_config = ModelConfig {
59        name: "ResNet18-Lite".to_string(),
60        input_shape: vec![1, 224, 224, 3],
61        output_shape: vec![1, 1000],
62        quant_format: QuantFormat::Int8,
63        optimization_level: OptimizationLevel::O3,
64        use_cache: true,
65    };
66
67    let runtime = ModelRuntime::new(model_config);
68    println!("   Model: {}", runtime.get_config().name);
69    println!("   Input: {:?}", runtime.input_shape());
70    println!("   Output: {:?}", runtime.output_shape());
71
72    let mut network = NeuralNetwork::new(runtime.get_config().name.clone());
73    
74    network.add_layer(Layer::new(
75        "stem_conv".to_string(),
76        LayerType::Convolution,
77        vec![1, 224, 224, 3],
78        vec![1, 112, 112, 64],
79    ));
80    
81    network.add_layer(Layer::new(
82        "residual_block_1".to_string(),
83        LayerType::PointwiseConvolution,
84        vec![1, 112, 112, 64],
85        vec![1, 112, 112, 64],
86    ));
87    
88    network.add_layer(Layer::new(
89        "residual_block_2".to_string(),
90        LayerType::PointwiseConvolution,
91        vec![1, 56, 56, 128],
92        vec![1, 56, 56, 128],
93    ));
94    
95    network.add_layer(Layer::new(
96        "global_avg_pool".to_string(),
97        LayerType::Pooling,
98        vec![1, 7, 7, 512],
99        vec![1, 512],
100    ));
101    
102    network.add_layer(Layer::new(
103        "classifier".to_string(),
104        LayerType::FullyConnected,
105        vec![1, 512],
106        vec![1, 1000],
107    ));
108
109    println!("   Layers: {}", network.layer_count());
110    println!("   Estimated TOPS: {:.6}\n", network.total_tops());
111}
Source

pub fn input_shape(&self) -> &[usize]

Get input shape.

Examples found in repository?
examples/full_inference_pipeline.rs (line 69)
55fn build_model() {
56    println!("2. Model Building");
57
58    let model_config = ModelConfig {
59        name: "ResNet18-Lite".to_string(),
60        input_shape: vec![1, 224, 224, 3],
61        output_shape: vec![1, 1000],
62        quant_format: QuantFormat::Int8,
63        optimization_level: OptimizationLevel::O3,
64        use_cache: true,
65    };
66
67    let runtime = ModelRuntime::new(model_config);
68    println!("   Model: {}", runtime.get_config().name);
69    println!("   Input: {:?}", runtime.input_shape());
70    println!("   Output: {:?}", runtime.output_shape());
71
72    let mut network = NeuralNetwork::new(runtime.get_config().name.clone());
73    
74    network.add_layer(Layer::new(
75        "stem_conv".to_string(),
76        LayerType::Convolution,
77        vec![1, 224, 224, 3],
78        vec![1, 112, 112, 64],
79    ));
80    
81    network.add_layer(Layer::new(
82        "residual_block_1".to_string(),
83        LayerType::PointwiseConvolution,
84        vec![1, 112, 112, 64],
85        vec![1, 112, 112, 64],
86    ));
87    
88    network.add_layer(Layer::new(
89        "residual_block_2".to_string(),
90        LayerType::PointwiseConvolution,
91        vec![1, 56, 56, 128],
92        vec![1, 56, 56, 128],
93    ));
94    
95    network.add_layer(Layer::new(
96        "global_avg_pool".to_string(),
97        LayerType::Pooling,
98        vec![1, 7, 7, 512],
99        vec![1, 512],
100    ));
101    
102    network.add_layer(Layer::new(
103        "classifier".to_string(),
104        LayerType::FullyConnected,
105        vec![1, 512],
106        vec![1, 1000],
107    ));
108
109    println!("   Layers: {}", network.layer_count());
110    println!("   Estimated TOPS: {:.6}\n", network.total_tops());
111}
Source

pub fn output_shape(&self) -> &[usize]

Get output shape.

Examples found in repository?
examples/full_inference_pipeline.rs (line 70)
55fn build_model() {
56    println!("2. Model Building");
57
58    let model_config = ModelConfig {
59        name: "ResNet18-Lite".to_string(),
60        input_shape: vec![1, 224, 224, 3],
61        output_shape: vec![1, 1000],
62        quant_format: QuantFormat::Int8,
63        optimization_level: OptimizationLevel::O3,
64        use_cache: true,
65    };
66
67    let runtime = ModelRuntime::new(model_config);
68    println!("   Model: {}", runtime.get_config().name);
69    println!("   Input: {:?}", runtime.input_shape());
70    println!("   Output: {:?}", runtime.output_shape());
71
72    let mut network = NeuralNetwork::new(runtime.get_config().name.clone());
73    
74    network.add_layer(Layer::new(
75        "stem_conv".to_string(),
76        LayerType::Convolution,
77        vec![1, 224, 224, 3],
78        vec![1, 112, 112, 64],
79    ));
80    
81    network.add_layer(Layer::new(
82        "residual_block_1".to_string(),
83        LayerType::PointwiseConvolution,
84        vec![1, 112, 112, 64],
85        vec![1, 112, 112, 64],
86    ));
87    
88    network.add_layer(Layer::new(
89        "residual_block_2".to_string(),
90        LayerType::PointwiseConvolution,
91        vec![1, 56, 56, 128],
92        vec![1, 56, 56, 128],
93    ));
94    
95    network.add_layer(Layer::new(
96        "global_avg_pool".to_string(),
97        LayerType::Pooling,
98        vec![1, 7, 7, 512],
99        vec![1, 512],
100    ));
101    
102    network.add_layer(Layer::new(
103        "classifier".to_string(),
104        LayerType::FullyConnected,
105        vec![1, 512],
106        vec![1, 1000],
107    ));
108
109    println!("   Layers: {}", network.layer_count());
110    println!("   Estimated TOPS: {:.6}\n", network.total_tops());
111}
Source

pub fn validate_input(&self, shape: &[usize]) -> Result<()>

Validate input dimensions.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V