Unified Multimodal Vector Space: From Principles to Retrieval System Implementation
This article explains why multimodal retrieval benefits from a unified vector space, how unified encoders and similarity scoring work, and how to implement retrieval with on-demand modality branches, Matryoshka dimension truncation, task templates, evaluation sets, common use cases, and a deployment checklist.
1. Why Put Multimodal Content into the Same Vector Space
Traditional multimodal retrieval often prepares a separate encoder for each media type. Text uses one encoder, images use another, and audio uses a third. The resulting vectors live in independent spaces, so their values are not comparable. The system therefore needs a separate index for each type, retrieves from each index separately, and then merges and ranks the results. This approach creates three problems. First, indexing and maintenance costs grow with the number of modalities. Second, score distributions differ across indexes, making merged ranking results difficult to explain. Third, cross-modal queries are nearly impossible to implement directly, such as finding a video clip using a sentence description.
A unified vector space is designed to solve these three problems. Once text, code, images, video, and audio are all mapped into a space of the same dimensionality, a query vector can directly compute similarity against candidate vectors of any type. The system only needs to maintain one index and complete one ranking pass.
2. How a Unified Space Works
The encoder for each modality converts input into a fixed-length floating-point vector. During training, the model learns to place semantically similar content closer together in the space, such as a description and its corresponding image, or a function and its documentation. Cross-modal similarity therefore comes from the semantic alignment learned during training.
Common similarity measures are cosine similarity and inner product. When using cosine similarity, vectors are usually normalized to unit length first, so the score reflects only direction and is not affected by vector magnitude.
# Normalize both query and candidate vectorsq = normalize(encode(query_text, role="query"))d = normalize(encode(image_file, role="document"))score = dot(q, d) # cosine similarity
The same dimensionality does not mean vectors generated by different models can be mixed. Even if two models output vectors of the same dimensionality, their spaces are independent of each other, and scores between them are meaningless. The index and queries must be generated by the same model version. When the model is upgraded, vectors need to be rebuilt entirely.
3. Load Modality Branches on Demand
A unified model usually consists of multiple encoding branches. If the business only needs text and code retrieval, only the corresponding branches need to be loaded, which can reduce memory usage and startup time.
encoder = load_encoder(modalities=["text", "code"])
This on-demand loading approach is suitable for on-device scenarios. Mobile devices and laptops have limited memory, and loading only the necessary branches can keep the model within an acceptable resource range. In server-side scenarios, the full set of branches can be loaded so that the same service supports multiple query types.
When selecting a model, consider the following dimensions:
- Modality needs: whether the current business really needs images, video, or audio.
- Deployment location: on-device, edge node, or central service.
- Latency budget: whether the time required for a single encoding pass meets interactive requirements.
- License terms: whether the rights and obligations of the open-source license meet enterprise compliance requirements.
4. Trade Storage for Dimension Truncation
Matryoshka Representation Learning is a training method. During training, the model learns to make the first several dimensions of a vector carry the main semantics, so the first k dimensions can be used directly without re-encoding.
After truncation, the vector needs to be re-normalized:
def truncate(vec, k):v = vec[:k]return v / norm(v)
Storage cost can be calculated directly. Taking 1 million vectors as an example, with each component stored as bfloat16 using 2 bytes:
- 768 dimensions: 1,000,000 × 768 × 2 bytes, approximately 1.54 GB.
- 128 dimensions: 1,000,000 × 128 × 2 bytes, approximately 256 MB.
Storage usage drops to one sixth of the original. Truncation loses information, and retrieval quality declines accordingly. The decline may also differ across modalities. Therefore, truncation should be adopted only when there is real pressure on storage or latency. The recommended approach is to establish a quality baseline with full dimensions first, then gradually reduce dimensions to find the smallest dimension that meets business requirements.
5. Task Templates and Input Formats
Many embedding models use task-related input templates during training, such as labeling “document” and “query” separately. Inputting data according to the templates specified in the model documentation, consistent with training, is a prerequisite for achieving expected results.
Template errors do not raise errors. They only silently reduce recall, making them one of the most easily overlooked sources of failure. Two practical recommendations are:
- Encapsulate templates in a unified encoding function and specify the role through parameters, avoiding string concatenation in each part of the business code.
- Before launch, run a smoke test with a small set of known relevant sample pairs to confirm that relevant content ranks near the top.
6. Build Your Own Evaluation Set
General leaderboards can only reflect the average performance of a model and cannot represent a specific business. The basic steps for building an evaluation set are:
- Collect real queries and label relevant documents, images, code snippets, or video clips for each query.
- Group by modality and query type, and calculate metrics separately.
- Use recall, mean reciprocal rank, and normalized discounted cumulative gain to measure performance.
- Compare full dimensions, truncated dimensions, and the previous model version, while recording changes in both quality and cost.
Grouped statistics are especially important. Overall metrics may hide obvious degradation in a particular modality.
7. Typical Application Scenarios
- Local codebase retrieval: developers describe functionality in natural language, and the system returns relevant functions or files. This can serve as a retrieval step before an agent reads code.
- Personal device content search: documents, screenshots, and recordings are searched in a unified way, so users do not need to remember file types or storage locations.
- Video content retrieval: text descriptions are used to find video clips. Actual effectiveness depends on frame sampling and segmentation strategy, and needs separate evaluation.
- Retrieval-augmented generation: unified vectors are used as a knowledge base index, with the same retrieval logic covering multiple material forms.
- Classification and clustering: vectors are reused as features, reducing the work of training separate representations for different tasks.
8. Deployment Checklist
- Fix the model version, and use the same version for both indexing and queries.
- Standardize input templates and verify them with smoke tests.
- Evaluate dimensions against the storage budget and record quality changes after truncation.
