For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Python class
Embedding
Embeddingβ
class max.nn.Embedding(vocab_size, hidden_dim, dtype, device, quantization_encoding=None, name=None)
Bases: Module
A lookup table for embedding integer indices into dense vectors.
When called, Embedding maps each integer index to a dense vector of
fixed size. It accepts a TensorValueLike of integer
indices with shape (batch, ..., num_indices) and returns a
TensorValue of shape (batch, ..., num_indices, hidden_dim) containing the corresponding embedding vectors.
Embedding weights are stored on the CPU but are moved to the specified device during model initialization.
from max.driver import Accelerator, CPU, accelerator_count
from max.dtype import DType
from max.graph import DeviceRef, Graph, TensorType
from max.nn import Embedding
device = Accelerator() if accelerator_count() > 0 else CPU()
device_ref = DeviceRef.from_device(device)
embedding_layer = Embedding(
vocab_size=1000,
hidden_dim=256,
dtype=DType.float32,
device=device_ref,
name="embeddings",
)
indices_type = TensorType(DType.uint32, [1, 8], device=device_ref)
with Graph("embedding", input_types=[indices_type]) as graph:
embeddings = embedding_layer(graph.inputs[0])
graph.output(embeddings)Initializes the embedding layer with the given arguments.
-
Parameters:
-
- vocab_size (int) β The number of unique items in the vocabulary.
Indices must be in the range
[0, vocab_size). - hidden_dim (int) β The dimensionality of each embedding vector.
- dtype (DType) β The data type of the embedding weights.
- device (DeviceRef) β The device where embedding lookups are executed. Model init transfers the initially CPU-resident weights to this device.
- quantization_encoding (QuantizationEncoding | None) β Optional quantization encoding for the weights.
- name (str | None) β The name identifier for the embedding weight matrix.
- vocab_size (int) β The number of unique items in the vocabulary.
Indices must be in the range
deviceβ
device: DeviceRef
The device on which embedding lookup is performed.
weightβ
weight: Weight
The embedding weight matrix stored on the CPU.
Model init moves weights to the device specified in device.