IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Python class

BufferValue

BufferValue​

class max.graph.BufferValue(value)

source

Bases: Value[BufferType]

Represents a mutable semantic tensor within a Graph.

Unlike a TensorValue, which is value semantic and produces a new value from every operation, a BufferValue refers to memory you can update in place. Read it into a tensor with buffer_load() and write a tensor back with buffer_store(). This is how a graph carries state that must persist across executions, such as a key-value cache.

The following example loads a buffer input, updates it, and stores the result back in place:

from max.dtype import DType
from max.graph import BufferType, DeviceRef, Graph, ops

buffer_type = BufferType(DType.float32, shape=[4], device=DeviceRef.CPU())

with Graph("buffer_demo", input_types=[buffer_type]) as graph:
    state = graph.inputs[0].buffer

    # Read the current contents into a value-semantic tensor.
    current = ops.buffer_load(state)

    # Write an updated tensor back into the same memory.
    ops.buffer_store(state, current + 1)
    graph.output(current)

    print(f"dtype: {state.dtype}")  # Output: dtype: DType.float32
    print(f"shape: {state.shape}")  # Output: shape: [Dim(4)]

Initializes a BufferValue from another value.

Parameters:

value (Value[Any] | _Value[mo.BufferType] | HasBufferValue) – The value to wrap, either an MLIR value of buffer type or another BufferValue.

device​

property device: DeviceRef

source

Returns the device of the BufferValue.

dtype​

property dtype: DType

source

Returns the tensor data type.

from_mlir()​

classmethod from_mlir(value)

source

Creates a BufferValue from an MLIR buffer value.

Parameters:

value (Value[BufferType]) – The MLIR buffer value to wrap.

Return type:

BufferValue

print()​

print(label='debug_buffer')

source

Prints detailed information about the buffer.

Parameters:

label (str)

Return type:

None

rank​

property rank: int

source

Returns the rank (number of dims) of the buffer.

shape​

property shape: Shape

source

Returns the shape of the BufferValue.

type​

property type: BufferType

source

Returns the type of the BufferValue as a BufferType.