Unified Multimodal Embedding Spaces: Design and On-Device Retrieval Practices

    This article examines how retrieval systems can move from separate single-modality indexes to a shared semantic vector space for text, code, images, video, and audio. It discusses modular encoder loading for on-device use, the role and cost of a 768-dimensional shared space, Matryoshka representation learning and vector truncation, a typical on-device retrieval architecture, code search and local knowledge base scenarios, evaluation and privacy considerations, and deployment choices. Readers will learn the main design trade-offs and engineering practices for building cross-media retrieval on resource-constrained devices.

    1. From Single-Modality Retrieval to a Unified Semantic Space

    Traditional retrieval systems are often built separately by media type. Text uses one encoding model, images use another visual model, and audio and video require separate handling. The vectors produced by different models differ in dimension, value distribution, and semantic expression, so they can usually perform similarity search only within their own indexes.

    This architecture was practical in early applications, but as enterprise knowledge bases increasingly contain documents, screenshots, recordings, video, code, and structured data, maintaining multiple independent vector spaces creates clear problems:

    1. Inconsistent retrieval entry points: Users need to know whether they are searching for documents, images, or recordings so that the system can select the corresponding index.
    2. Difficult cross-media recall: Text queries are hard to compare directly with screenshots, video clips, or speech content.
    3. Duplicated infrastructure: Different modalities require separate encoding services, vector indexes, and evaluation systems.
    4. High on-device deployment cost: Mobile devices and laptops have limited compute, memory, and storage, so running multiple models simultaneously is not ideal.

    The core idea of unified multimodal embedding is to use a shared semantic representation space to encode text, code, images, video, and audio into vectors of the same dimension. As long as these vectors are trained and aligned appropriately, they can be compared using a unified similarity function, such as cosine similarity or normalized dot product.

    This means that after a user enters a natural language description, the system can search not only relevant documents but also matching screenshots, recordings, video clips, and code files, enabling true cross-media retrieval.

    2. Model Capabilities and Modular Loading

    A multimodal embedding model designed for on-device scenarios usually needs to balance expressive capability and resource consumption. A reasonable design does not keep the full model loaded for every application. Instead, it selects the encoder corresponding to the modalities required by the business.

    For example:

    • For text search only, only the text encoding module may need to be loaded.
    • Code repository retrieval can use text and code-related encoding capabilities.
    • Document screenshot search requires a visual encoding module.
    • Meeting material retrieval may involve text, images, and audio at the same time.
    • Video search needs to handle information such as video frames, subtitles, or audio tracks.

    Modular loading can reduce startup time, memory usage, and power consumption. This is especially important for resource-constrained devices. The parameter count of a full multimodal model is usually significantly higher than that of a pure text model, but if the system uses only part of its capabilities, there is no need to keep all modules resident in memory.

    In engineering implementation, the following strategies can also be used:

    1. On-demand loading: Load the corresponding encoder when a retrieval task of a certain type is first triggered.
    2. Combining resident and unloaded models: Keep high-frequency modules resident and release low-frequency modules after the task ends.
    3. Offline pre-encoding: Pre-generate vectors for documents, images, and videos that do not change often.
    4. Incremental encoding: Process only new or changed data to avoid repeated computation.
    5. Tiered model configurations: Use lightweight encoders on low-end devices and full versions on high-performance devices.

    It is important to note that multimodal unification does not mean all modalities have identical semantic capability. Text and code usually have clearer symbolic structure, and their training and evaluation are relatively stable. Images, video, and audio are affected by content complexity, temporal context, and noise, so actual retrieval quality may fluctuate more.

    3. The Meaning of a 768-Dimensional Shared Vector Space

    The key to a unified vector space is not merely that the dimensions are the same, but that comparable semantic relationships are established across modalities. Suppose a user enters “screenshots showing the device configuration interface.” The system should be able to make this text close to image vectors containing the corresponding interface. If the user enters “find recordings about network connection anomalies,” the system should also be able to retrieve semantically related audio clips.

    Higher-dimensional vectors can usually carry more semantic information, but higher is not always better. Vector dimension directly affects:

    • Vector storage space;
    • Index build time;
    • Similarity computation overhead;
    • Memory bandwidth pressure;
    • On-device retrieval response speed.

    For example, with 768-dimensional vectors and 16-bit floating-point storage, a single vector occupies about 1,536 bytes. One million vectors require about 1.5 GB, and actual deployment must also account for index structures, metadata, and system overhead, so total occupancy is often higher.

    Another value of a unified space is that it simplifies the interface design of the retrieval system. The application layer can use a unified data structure to describe different media:

    1. {
    2. "id": "asset-001",
    3. "modality": "image",
    4. "embedding": [ ... ],
    5. "metadata": {
    6. "title": "Device configuration screenshot",
    7. "source": "Local knowledge base",
    8. "timestamp": 1710000000
    9. }
    10. }

    On the query side, the system only needs to generate one vector and then filter by conditions such as permissions, media type, and time range. This enables all-media search while also limiting the result scope through filters.

    4. Matryoshka Representation Learning and Vector Truncation

    To adapt to different hardware and business scenarios, some embedding models support Matryoshka representation learning. The basic idea is that the first several dimensions of the full vector also have relatively complete semantic expression capability. Therefore, a 768-dimensional vector can be truncated to 512, 256, or even 128 dimensions, and retrieval can be performed with shorter vectors.

    The advantage of this approach is that multiple models do not need to be retrained. An application can first generate the full vector and then choose the dimensions actually used according to storage and performance requirements.

    For example, when one million vectors are stored using 16-bit floating-point values:

    • 768 dimensions require about 1.5 GB;
    • 256 dimensions require about 500 MB;
    • 128 dimensions require about 250 MB.

    If quantized storage is used further, space can be reduced even more. However, vector compression inevitably involves information loss. Experience indicates that at 128 dimensions, text tasks can retain a relatively high proportion of their effectiveness, while image, video, and audio retrieval quality usually degrades more noticeably.

    The reason is that different modalities have different semantic complexity. Text queries often depend on keywords, topics, and conceptual relationships, while visual and audio content contains more fine-grained information, such as layout, color, motion, acoustic features, temporal order, and background noise. Shorter vectors may not fully preserve these differences.

    Therefore, dimension selection should not consider storage cost alone. It should also be combined with business goals:

    • For coarse recall over large text collections, 128 or 256 dimensions can be prioritized.
    • For code search, offline evaluation should be used to determine the truncation dimension.
    • For image, video, and audio retrieval, overly short vectors should be used with caution.
    • For high-value results, low-dimensional vectors can be used for initial screening, followed by high-dimensional vectors or a re-ranking model for review.

    5. A Typical Architecture for On-Device Multimodal Retrieval

    A complete on-device retrieval system usually includes five stages: data preprocessing, vector generation, index management, query encoding, and result ranking.

    1. Data Preprocessing

    First, different media need to be converted into content suitable for encoding. Documents can be split by paragraph, code can be split by file, class, function, or semantic block, video can extract keyframes by time window and combine them with subtitles, and audio can be segmented by speaker or time segment.

    The splitting strategy directly affects retrieval results. If a text chunk is too long, its vector may mix multiple topics; if it is too short, the context may be insufficient. Video and audio must also retain timestamps; otherwise, even if the correct content is recalled, it may be difficult to locate the specific segment.

    2. Vector Generation

    The system selects the corresponding encoder based on content type and maps each data chunk to a vector of unified dimension. After generation, normalization is usually required so that cosine similarity or dot product can be used for comparison.

    For static materials, vectors can be generated in the background while the device is idle. For real-time recordings or newly created screenshots, streaming or incremental processing is needed.

    3. Local Indexing

    On-device indexes need to consider memory, write speed, and query latency. For small data volumes, simple brute-force similarity computation can be used. For larger data volumes, approximate nearest neighbor indexes are needed.

    In addition to the index, necessary metadata should be stored, including original file location, media type, creation time, permission labels, and time range. Vectors are only responsible for similarity computation and cannot replace permission control and business filtering.

    4. Query Encoding

    User input can be text, an image, speech, or a video clip. The system first uses the corresponding encoder to generate a query vector and then searches for candidate results in the shared space.

    For example, the text query “find the code implementation related to cache invalidation” can retrieve both technical documents and source code. The query “find screenshots containing abnormal login prompts” can search images or video keyframes.

    5. Result Ranking and Presentation

    Vector similarity is suitable for recall but not necessarily as the final ranking criterion. A real system usually also needs to re-rank results based on time, permissions, source reliability, content freshness, and user behavior.

    For video and audio, the matched time ranges should also be shown to users, so they do not need to open an entire long video or recording and search for the result themselves.

    6. Application Value in Code Search and Local Knowledge Bases

    A unified embedding space is particularly valuable for code search. Developers can use natural language to describe a problem, such as “find the logic that handles connection retries” or “locate the exception handling after an upload failure,” and the system can recall relevant files, functions, and comments from the local codebase.

    To improve code retrieval quality, consider the following:

    1. Do not generate vectors only by file. Use functions, classes, or semantic code blocks as units where possible.
    2. Add metadata such as path, language type, symbol name, and dependencies to each code block.
    3. Associate comments with code appropriately, avoiding syntax-only structures that lack business semantics.
    4. Apply permission filtering to query results to prevent returning code content that the user is not authorized to access.
    5. Add human confirmation for high-risk operations. Do not automatically modify code based only on vector similarity.

    In local knowledge base scenarios, unified multimodal embedding can also connect meeting recordings, design screenshots, requirements documents, and implementation code. After a user enters a natural language question, the system can return different types of evidence, providing more complete context for local intelligent assistants and retrieval-augmented generation systems.

    7. Evaluation and Engineering Considerations

    Before deployment, evaluation sets should be built by modality and task rather than relying on a single text metric. At minimum, the following scenarios should be covered:

    • Text-to-text retrieval;
    • Text-to-code retrieval;
    • Text-to-image retrieval;
    • Text-to-video-clip retrieval;
    • Text-to-audio-clip retrieval;
    • Image-to-text or image-to-image retrieval;
    • Recall and latency under different vector dimensions.

    Evaluation metrics can include Recall@K, MRR, end-to-end latency, memory footprint, index build time, and device power consumption. For multimodal tasks, temporal localization accuracy, cross-media recall ratio, and error types should also be checked separately.

    Privacy and security also require attention in real deployments. On-device deployment can reduce data upload, but local vectors are not inherently irreversible and should not be treated as an absolutely secure replacement. Sensitive content still requires encrypted storage, access control, and lifecycle management. For data such as recordings, screenshots, and code, the collection scope, retention period, and deletion mechanism should be clearly defined.

    8. How to Choose a Deployment Approach

    Overall, unified multimodal embedding is suitable for the following scenarios:

    • Unified search across text, images, audio, and video is required;
    • Low-latency retrieval is desired on mobile devices or personal computers;
    • Local codebase search and knowledge base question answering need to be built;
    • The maintenance cost of multiple encoders and index systems should be reduced;
    • Data privacy requirements are high, and raw content should not frequently leave the device.

    However, if an application handles only a single modality, or requires extremely high quality for one specialized type of content, a full multimodal model may not be necessary. Pure text scenarios can prioritize lightweight text encoding solutions. Tasks such as visual industrial inspection and specialized speech recognition may also need dedicated models.

    A more reliable engineering path is to first establish a baseline at full dimensionality, then gradually test the impact of vector truncation, quantization, and module unloading. Only when storage, latency, or power consumption become clear bottlenecks should the representation be compressed further. This avoids sacrificing retrieval quality too early to save resources.

    The real value of a unified multimodal vector space is not simply encoding five content types into arrays of the same length, but enabling applications to understand different media through the same retrieval logic. As on-device computing capability improves, this technology is expected to become important infrastructure for local search, knowledge management, code assistance, and multimedia question answering.