Skip to main content

GgmlContext

Struct GgmlContext 

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

A safe wrapper around ggml_context.

Implementations§

Source§

impl GgmlContext

Source

pub fn new(mem_size: usize, no_alloc: bool) -> Self

Create a new ggml context.

§Parameters
  • mem_size: Memory pool size in bytes for tensor metadata.
  • no_alloc: If true, tensor data is not allocated (use with backend allocation).
§Sizing the pool

Every tensor and graph created from this context is carved out of mem_size, and running out is not a recoverable error:

  • a release build of ggml returns null, which the constructors here turn into a panic;
  • a debug build of ggml calls GGML_ABORT and kills the process before returning, so no amount of Rust-side care helps.

So size it up front rather than guessing. Self::sized_for does the arithmetic; Self::used_mem and Self::mem_size let you check headroom while building a graph.

§Panics

Panics if ggml returns a null pointer.

Source

pub fn sized_for(n_tensors: usize, n_graphs: usize, no_alloc: bool) -> Self

Create a context sized for n_tensors tensors and n_graphs graphs.

Turns “pick a number and hope” into arithmetic: the pool holds metadata, whose per-item cost ggml reports through tensor_overhead and graph_overhead. A little slack is added for ggml’s own bookkeeping.

Views, reshapes and every intermediate an operation produces are tensors too — count graph nodes, not just the tensors you name.

// Room for 64 tensors and one graph.
let ctx = GgmlContext::sized_for(64, 1, true);
assert!(ctx.mem_size() >= 64 * llama_cpp_4::ggml::tensor_overhead());
§Panics

Panics if ggml returns a null pointer.

Source

pub fn used_mem(&self) -> usize

Bytes of the pool used so far.

Compare with Self::mem_size while building a graph to see whether it will fit, rather than finding out by aborting.

Source

pub fn mem_size(&self) -> usize

Total size of the pool, as passed to Self::new.

Source

pub fn free_mem(&self) -> usize

Bytes still available in the pool.

Source

pub fn as_ptr(&self) -> *mut ggml_context

Get the raw context pointer.

Source

pub fn new_tensor_1d(&self, typ: ggml_type, ne0: i64) -> GgmlTensor

Create a 1D tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn new_tensor_2d(&self, typ: ggml_type, ne0: i64, ne1: i64) -> GgmlTensor

Create a 2D tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn new_tensor_3d( &self, typ: ggml_type, ne0: i64, ne1: i64, ne2: i64, ) -> GgmlTensor

Create a 3D tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn new_tensor_4d( &self, typ: ggml_type, ne0: i64, ne1: i64, ne2: i64, ne3: i64, ) -> GgmlTensor

Create a 4D tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn dup_tensor(&self, src: &GgmlTensor) -> GgmlTensor

Create a tensor with the same shape and type as another.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn new_tensor(&self, typ: ggml_type, ne: &[i64]) -> GgmlTensor

Create a new tensor with arbitrary dimensions.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn add(&self, a: &GgmlTensor, b: &GgmlTensor) -> GgmlTensor

Element-wise addition: a + b

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn mul_mat(&self, a: &GgmlTensor, b: &GgmlTensor) -> GgmlTensor

Matrix multiplication: a @ b

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn scale(&self, a: &GgmlTensor, s: f32) -> GgmlTensor

Scale tensor: a * s

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn cast(&self, a: &GgmlTensor, typ: ggml_type) -> GgmlTensor

Cast tensor to a different type.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn cont(&self, a: &GgmlTensor) -> GgmlTensor

Make tensor contiguous in memory.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn transpose(&self, a: &GgmlTensor) -> GgmlTensor

Transpose a tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn reshape_1d(&self, a: &GgmlTensor, ne0: i64) -> GgmlTensor

Reshape to 1D.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn reshape_2d(&self, a: &GgmlTensor, ne0: i64, ne1: i64) -> GgmlTensor

Reshape to 2D.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn view_1d(&self, a: &GgmlTensor, ne0: i64, offset: usize) -> GgmlTensor

Create a 1D view of a tensor.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn new_graph(&self) -> GgmlGraph

Create a new computation graph.

§Panics

Panics if ggml returns null, which in a release build means the context’s memory pool is exhausted. A debug build of ggml aborts before returning — see GgmlContext::new for how to size the pool.

Source

pub fn first_tensor(&self) -> Option<GgmlTensor>

Get the first tensor in this context.

Source

pub fn next_tensor(&self, tensor: &GgmlTensor) -> Option<GgmlTensor>

Get the next tensor after tensor in this context.

Trait Implementations§

Source§

impl Debug for GgmlContext

Source§

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

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

impl Drop for GgmlContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> 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, 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<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