Skip to main content

CostModel

Struct CostModel 

Source
pub struct CostModel {
    pub a: f64,
    pub b: f64,
    pub max_workspace_bytes: usize,
}
Expand description

Quadratic-aware workspace cost model for ONNX attention inference.

BGE-M3 uses multi-head attention whose intermediate tensor footprint scales as O(batch * seq^2) (attention score matrix) plus O(batch * seq) (FFN intermediates, projection matrices). The total peak workspace is approximately:

peak ≈ a * (batch * seq) + b * (batch * seq^2)

where a (bytes/token-position) captures the FFN / projection contribution and b (bytes/token-position^2) captures the attention contribution.

At sequence length 512 attention is small relative to FFN, so a linear approximation works. At 8192, b * N^2 dominates by ~16×, so using only a would under-budget by that same factor.

Coefficients are derived at startup by crate::probe or set conservatively from compile-time defaults when measurement is unavailable.

Fields§

§a: f64

Bytes per token-position (linear term: FFN intermediates, projections).

§b: f64

Bytes per token-position-squared (quadratic term: attention scores).

§max_workspace_bytes: usize

Maximum workspace bytes available per worker for a single session.run() call.

Implementations§

Source§

impl CostModel

Source

pub const CONSERVATIVE_A: f64 = 16_384.0

Conservative static defaults calibrated so a (16, 512) chunk lands at ~140 MB workspace — matching the old static budget at the previous default BGE_M3_ONNX_BATCH_SIZE = 16, MAX_SEQ_LENGTH = 512.

These are used when the probe cannot run (no ORT, no model, macOS without cgroup support) or when BGE_M3_DISABLE_AUTO_BUDGET is set.

Formula check: 16 KiB/token × 16 × 512 + 8 B/token² × 16 × 512² = 16384 × 8192 + 8 × 16 × 262144 = 134 217 728 + 33 554 432 = 167 772 160 ≈ 160 MB per chunk (workers run sequentially inside one worker).

Source

pub const CONSERVATIVE_B: f64 = 8.0

Conservative quadratic coefficient (bytes per token-position squared).

Source

pub const DEFAULT_MAX_WORKSPACE: usize

Default maximum workspace per worker when memory cannot be detected.

2 GiB is conservatively safe for the Fargate 28 GiB task with 7 workers (28 GB * 0.7 safety / 7 workers ≈ 2.8 GB); we round down for headroom.

Source

pub fn conservative(max_workspace_bytes: usize) -> Self

Constructs a CostModel with conservative defaults and the given workspace ceiling.

Source

pub fn chunk_cost(&self, count: usize, max_seq: usize) -> u128

Estimated peak workspace (bytes) for a single session.run() call with count texts and max_seq as the padded sequence length.

Uses saturating arithmetic on u128 to avoid overflow at large inputs.

Source

pub fn fits(&self, count: usize, max_seq: usize) -> bool

Returns true if the chunk fits within the workspace budget.

Trait Implementations§

Source§

impl Clone for CostModel

Source§

fn clone(&self) -> CostModel

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CostModel

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Copy for CostModel

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

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

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

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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