Skip to main content

Model

Struct Model 

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

A loaded model plus its metadata and runtime statistics: what the server serves.

Build one with Model::custom, or with furnace-burn for a Burn model. Requests run through Model::infer / Model::infer_many (named tensors), or the flat Model::predict family for a model of one f32 tensor in and one out. All of them block; call them off the async runtime.

Implementations§

Source§

impl Model

Source

pub fn custom(model: impl TensorModel + 'static) -> Self

Serve your own model (see TensorModel)

Source

pub fn new( inner: Box<dyn TensorModel>, model_type: &'static str, model_size_bytes: u64, ) -> Self

Wrap a model; model_type and model_size_bytes are reported by /model/info

Source

pub fn batches_named(&self) -> bool

Whether concurrent requests of named tensors can be joined into one batch: every input has a fixed per-item shape. (Simple models batch on the flat path instead.)

Source

pub fn is_simple(&self) -> bool

Whether the model takes one f32 tensor and returns one, with fixed per-item shapes. Such models serve /predict and batch concurrent requests together; the others take named tensors through Model::infer.

Source

pub fn inputs(&self) -> &[IoSpec]

The inputs, in the order Model::infer takes them

Source

pub fn outputs(&self) -> &[IoSpec]

The outputs, in the order Model::infer returns them

Source

pub fn infer(&self, inputs: Vec<HostTensor>) -> Result<Vec<HostTensor>>

Run one request of named tensors: inputs in the order of Model::inputs, each with the batch as its first dimension. Returns the outputs in the order of Model::outputs. Blocking, like Model::predict_batch.

Source

pub fn infer_many( &self, requests: Vec<Vec<HostTensor>>, ) -> Vec<Result<Vec<HostTensor>>> ⓘ

Several requests of named tensors as one forward pass, joined along the batch dimension, the outputs split back. Each request is checked on its own, so an invalid one gets its own error and the rest still run. Blocking, like Model::predict_batch.

Source

pub fn predict(&self, input: Vec<f32>) -> Result<Vec<f32>>

Run one item of a simple model (see Model::is_simple): its flattened input in, its flattened output back. Blocking, like Model::predict_batch.

Source

pub fn predict_batch(&self, inputs: Vec<Vec<f32>>) -> Result<Vec<Vec<f32>>>

Run inference on a batch of flattened inputs.

This is CPU/GPU-bound and blocking; call it from a blocking context (e.g. tokio::task::spawn_blocking) inside async code.

Source

pub fn predict_flat( &self, input: Vec<f32>, batch_size: usize, ) -> Result<Vec<f32>>

Run inference on batch_size items stored back to back in one row-major buffer.

Returns the outputs the same way: batch_size * output_size values. This avoids per-item allocations and copies, so it is the fastest entry point for large inputs. Blocking, like Model::predict_batch.

Source

pub fn predict_many(&self, requests: Vec<FlatInput>) -> Vec<Result<Vec<f32>>> ⓘ

Run several independent requests as one forward pass and split the outputs back.

Each request is validated on its own, so an invalid one gets its own error and the rest still run. Results come back in request order, each holding that request’s batch_size * output_size values. The requests together must not exceed MAX_BATCH_SIZE items. Statistics count every request, plus one batch per forward pass. Blocking, like Model::predict_batch.

Source

pub fn adopt_stats(&mut self, previous: &Model)

Continue previous’s statistics, so a reloaded model’s counters do not restart at zero. The two share them from now on: requests still running on previous count as well.

Source

pub fn warm_up(&self) -> Result<Option<Duration>>

Run one all-zero input through the model without touching the statistics, so the first real request does not pay for lazy initialization (weight transforms, buffer pools, page faults on the memory-mapped weights). Returns how long it took.

A model with an input whose shape is only fixed per request cannot be warmed up without guessing it, and is skipped (Ok(None)).

Source

pub fn get_info(&self) -> &ModelInfo

Name, shapes, backend and the rest of what GET /model/info reports

Source

pub fn get_stats(&self) -> ModelStats

A snapshot of the request counters

Source

pub fn validate_input_shape(&self, input: &[f32]) -> Result<()>

Whether input has one item’s worth of values for a simple model’s input

Source

pub fn get_backend_info(&self) -> String

The backend’s name, as in ModelInfo::backend

Source

pub fn model_type(&self) -> &'static str

How the model was defined, as in ModelInfo::model_type

Trait Implementations§

Source§

impl Debug for Model

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Model

§

impl !UnwindSafe for Model

§

impl Freeze for Model

§

impl Send for Model

§

impl Sync for Model

§

impl Unpin for Model

§

impl UnsafeUnpin for Model

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more