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)
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
Returns the device of the BufferValue.
dtypeβ
property dtype: DType
Returns the tensor data type.
from_mlir()β
classmethod from_mlir(value)
Creates a BufferValue from an MLIR buffer value.
-
Parameters:
-
value (Value[BufferType]) β The MLIR buffer value to wrap.
-
Return type:
print()β
print(label='debug_buffer')
Prints detailed information about the buffer.
-
Parameters:
-
label (str)
-
Return type:
-
None
rankβ
property rank: int
Returns the rank (number of dims) of the buffer.
shapeβ
property shape: Shape
Returns the shape of the BufferValue.
typeβ
property type: BufferType
Returns the type of the BufferValue as a BufferType.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!