Zero Labs
Industry News / AI

DeepSeek V4 mHC Explained

Source · Towards AI By Shakti Wadekar

From the publisher’s feed, collected in the ZeroLabs newsroom.

This article explains mHC in DeepSeek V4 through visual explanations and short animations to build clear intuition around the mHC.📚 Content🏗️ Model architecture 💡 mHC idea/intuition 🧩 mHC in Transformer block 🎯 Attention with mHC ⚡ MoE with mHC 🧠 Learnable parameters of mHC 🔒 Constraints in mHC🏗️ Model architectureBefore diving into the mHC concepts, let’s first look at the overall architecture to build a more holistic understanding of how everything fits together.Figure 1: DeepSeek V4 full model architectureDeepSeek V4 model consists of Embedding layer, 61 or 43 Transformer blocks, Prediction head and MTP modules. It has two main variants:DeepSeek V4 Pro (61 transformer blocks)DeepSeek V4 Flash (43 transformer blocks)DeepSeek V4 is built as an evolution of the DeepSeek V3 architecture. Most of the architectural components remain similar to V3, with major additions including CSA, HCA, and mHC.CSA : Compressed Sparse AttentionHCA: Heavily compressed AttentionmHC: Manifold-Constrained Hyper-ConnectionsGoing forward, for simplification, we will call CSA/HCA as Attention block.In this article, we will focus specifically on mHC. The concepts behind CSA and HCA will be covered in a separate article in the near future. For full DeepSeek V3/R1 deep dive refer my previous article.💡 mHC Idea/IntuitionWhat is mHC?mHC stands for Manifold-Constrained Hyper Connections. Let’s first understand what is HC (Hyper-Connections).Intuition behind the HC:Figure 2: Residual connection vs Hyper-ConnectionsResidual connections were introduced by ‘Deep Residual Learning for Image Recognition (ResNet)’ paper and became a critical innovation for training very deep neural networks. They helped improve gradient flow across layers, making optimization of deeper models significantly more stable and effective.The diagram on the left (figure a) shows a standard residual connection, where the input of a layer is directly added to the transformed output of the layer to produce the final output.Focus on the residual path.In the left diagram (a), the residual path is just one direct shortcut from input (x_l) to the output.In the HC diagram (b), that single residual path is replaced with multiple parallel residual streams (shown as stacked blocks).Instead of one highway carrying all information, the model now keeps several highways alive across layers.In order to achieve those multiple parallel residual streams, the Res-Mapping, Pre-Mapping and Post-Mapping computations are introduced. We will be understanding them one by one in coming sections.Figure 3: Residual vs HC vs mHCHC and mHC are similar, except mHC applies additional constraints on how the parallel residual streams are mixed, leading to more structured and stable training. This will be more clear in next sections.🧩 mHC in Transformer blockWhere is mHC in DeepSeek V4 architecture?mHC is applied within each transformer block.The residual paths in the transformer blocks are replaced with mHC. It results in changes to the paths of Attention block and MoE block.IMPORTANT: mHC does not change how Attention and MoE blocks are computed. What they change is the input and output of those blocks.Following is how the DeepSeek V4 transformer would look like with and without mHC. This helps clearly identify the computational blocks which contribute to mHC.Figure 4: DeepSeek V4 transformer block with mHC (left) and without mHC (right)Let’s divide the transformer block into block diagrams to simplify and understand what each part is contributing.The transformer block mainly consists of two main parts:1. Attention with mHC2. MoE with mHCFigure 5: Left: Original transformer block. Right: Block covering the Attention + mHC partsFigure 6: Left: Original transformer block. Right: Block covering the MoE + mHC partsSimplified block diagram of a transformer block in DeepSeek v4:Figure 7: Simplified block diagram of DeepSeek V4 transformer blockEach layer receives 4 parallel streams as input and produces 4 parallel streams as output.For the first layer, the input token stream is duplicated to create 4 parallel streams. Only in the first layer, all 4 streams contain identical token embeddings. However, as the model progresses through deeper layers, the streams gradually become different due to independent computations and stream-mixing operations performed by mHC.Each stream has shape sequence_length × d_model.For example, a single stream can have shape 4096 × 1024, where:4096 → sequence length1024 → token embedding dimensionIn DeepSeek V4, 4 streams are processed in parallel, so the overall representation can be viewed as: 4 × 4096 × 1024The real tensor shapes will be (4096 × 4 × 1024). But for simplified understanding and visualization, representing them in (4 × 4096 × 1024) helps.Let’s first understand 🎯 Attention with mHC, and then move to ⚡ MoE with mHC.🎯 Attention with mHC:Animation 1: Layers inside Attention-with-mHCThe Attention-with-mHC block consists of 4 main components:Residual mappingPre-mappingAttention block (CSA/HCA)Post-mappingWhere Residual mapping, pre-mapping and post-mapping are mHC computations.Let’s understand them one by one.Pre-mapping and Post-mapping:Now let’s understand the block with pre-mapping, attention and post-mappingFigure 8: Pre-mapping, Post-mapping and Attention layer in Attention-with-mHC blockThe below animations shows the computations involved in the pre and post mapping blocks. Normalization layers and a few additional computations are also present in the actual implementation, but only the main computations are shown here for clarity and easier understanding. You can refer to the DeepSeek V4 huggingface code for more details.Animation 2: Pre-mapping and Post-mapping computationsPre-mapping layer:The pre-mapping layer combines the 4 parallel streams into a single stream representation before sending it into the Attention block.Each token exists in all 4 streams, meaning every token has 4 different representations. The pre-mapping layer mixes these representations using a weighted sum. For a given token, the representations from the 4 streams are multiplied by corresponding pre-mapping weights and then summed together to produce a single token representation.These weights are referred to here as weight_pre_1, weight_pre_2, weight_pre_3, and weight_pre_4.Example:Input: (4, sequence_length, d_model)Output: (1, sequence_length, d_model)The real tensor shapes will be (4096 × 4 × 1024) → (4096 × 1× 1024). But for simplified understanding and visualization, representing them in (4 × 4096 × 1024) → (1 × 4096 × 1024) helps.The pre-mapping weights are not directly stored as fixed learnable parameters. Instead, they are dynamically generated from the input stream representations using learnable parameters. Their exact computation and tensor shapes will be explained later in the section: Learnable Parameters in mHC.For now, they can simply be viewed as weights that control how much information each stream contributes during the stream-mixing process.Post-mapping layer:The post-mapping layer takes the single output stream produced by the Attention or MoE block and maps it back into 4 parallel streams.After the computation in the sublayer is completed, each token has only one updated representation. The post-mapping layer transforms this single representation into 4 different stream representations using post-mapping weights.For a given token, the single output representation is multiplied by post-mapping weights and distributed across the 4 streams, producing 4 updated token representations.These weights are referred to here as weight_post_1, weight_post_2, weight_post_3, and weight_post_4.Example:Input: (4, sequence_length, d_model)Output: (1, sequence_length, d_model)Similar to the pre-mapping layer, these are not directly stored as fixed learnable weights. Instead, they are dynamically generated from the stream representations using learnable parameters. Their exact computation and tensor shapes will be explained later in the section: Learnable Parameters in mHC.Conceptually, the post-mapping layer controls how information from the single computed representation is redistributed back into the multiple parallel streams.Residual mapping:Figure 9: Residual mapping block inside Attention-with-mHCThe below animations shows the computations involved in the Residual mapping (Res-mapping) blocks. Normalization layers and a few additional computations are also present in the actual implementation, but only the main computations are shown here for clarity and easier understanding. You can refer to the DeepSeek V4 huggingface code for more details.Animation 3: Res-mapping computationsIn the Residual Mapping block, each of the 4 input streams generates 4 different output representations. This results in a total of 16 intermediate streams.In other words, every token representation in a stream is transformed into 4 different embeddings using different weights. Since all 4 input streams do this independently, we obtain 4×4=16 unique intermediate streams.These weights are referred to here as weight_comb_1, weight_comb_2, to weight_post_16.These 16 streams are then recombined back into 4 output streams. Each output stream is formed by combining one transformed representation from each of the 4 input streams. As a result, every output stream contains information mixed from all input streams.The terminology can get confusing very quickly, which is exactly why the animation is useful for visualizing how the streams split, transform, and merge throughout the computation. 🙂Summary of full Attention-with-mHC blockAnimation 4: Attention-with-mHC computations summarized⚡ MoE with mHC:This is mostly similar to Attention-with-mHC. The overall mHC computation remains the same, the only difference is that the Attention block is replaced with an MoE block.Figure 10: MoE-with-mHC block diagramThe below animations shows the computations involved in the MoE-with-mHC blocks. Normalization layers and a few additional computations are also present in the actual implementation, but only the main computations are shown here for clarity and easier understanding. You can refer to the DeepSeek V4 huggingface code for more details.Animation 5: MoE-with-mHC computations summarized🧠 Learnable parameters of mHC:Specifically for mHC, the number of learnable parameters are (24 × n_hc × d_model), where n_hc​ is the number of parallel streams and d_model is the token dimension. In DeepSeek V4, n_hc=4. The animation takes d_model as 1024 and sequence_length as 4096.Computations are visualized below:Animation 6: Computations to generate pre, post and res mapping weightsThese learnable parameters (24, n_hc × d_model) (Green block) are multiplied with the transformer block’s input streams to generate 24 output values for each token. These 24 output values are then used as dynamic weights in the different mHC mapping operations.The 24 outputs of each token are divided as follows:4 weights for the pre-mapping layer4 weights for the post-mapping layer16 weights for the residual mapping layerThe role of these weights becomes much easier to understand visually through the animations shown in the previous sections (Pre-mapping, Post-mapping and Res-mapping), where each mapping block demonstrates how the streams are mixed and redistributed.IMPORTANT: The weights used in mHC, including the pre mapping, post mapping, and residual mapping weights, are not directly stored as learnable parameters.Instead, these weights are dynamically generated by multiplying the input stream representations with learnable parameters. This makes the mHC weights data dependent, meaning the weight values change depending on the input token representations.The data dependent weights/gating idea is similar to the concept of data dependent gating used in SwiGLU activations. In SwiGLU, the input representations are multiplied with learnable parameters to generate gating values, making the activations dynamically depend on the input data. Similarly, in mHC, the input streams are multiplied with learnable parameters to generate stream mixing weights, allowing the model to dynamically control how information is mixed and redistributed for different tokens.Attention-with-mHC block with mHC weight computations:Animation 7: Attention-with-mHC block with mHC weight computations:🔒 Constraints in mHC:One important issue with the original Hyper-Connections (HC) design was training instability caused by unconstrained stream mixing.In HC, the model dynamically generates mixing weights (pre, post and res-mapping weights) that decide how information flows between parallel residual streams. Since these weights are data-dependent and unconstrained, the model can learn highly unbalanced mixing patterns during training.For example, one stream may start dominating while other streams receive very little information. Similarly, some streams may continuously absorb information from all other streams while contributing almost nothing back. As training progresses, this imbalance can make optimization unstable, reduce stream diversity, weaken gradient flow, and partially defeat the purpose of having multiple parallel streams.mHC (manifold-constrained Hyper-Connections) introduces constraints on the mixing operations to solve this problem:Instead of allowing arbitrary mixing values, the generated weights are transformed into a more stable and balanced structure before being used.The three main components involved are:pre-mapping weights (pre) : controls how much of the current stream participates before the layer computationpost-mapping weights (post) : controls how much transformed output is written back into each streamRes-mapping weights (comb) : controls cross-stream residual mixing between all streamsRevisit Animation 2 and 3, to visualize these weights and how they are used in mHC computations.preand postThe pre and post mappings use sigmoid constraints. The sigmoid function bounds the values into a controlled range instead of allowing arbitrarily large positive or negative values.For the pre mapping, this acts like a soft gating mechanism. Each stream receives a controlled participation strength between 0 and 1. This prevents unstable amplification before the transformer sublayer computation.For the post mapping, sigmoid-based scaling similarly controls how strongly the transformed features are injected back into the streams. Instead of aggressively overwriting streams, the model performs smoother residual updates. This helps preserve stable information flow across very deep layers.One important implementation detail for post in DeepSeek-v4:The sigmoid output of the post, are multiplied with factor of 2. Hence the post weights range is 0 to 2. The multiplication by 2 is important because the post stage writes information back into the residual streams after the layer computation. If the values were restricted only to 0–1, the layer output could only attenuate information and would continuously weaken residual updates across many deep layers.comb (Res-mapping weights)The most important constraint is applied to comb, which controls stream-to-stream mixing.mHC applies Sinkhorn normalization to the comb matrix. Sinkhorn normalization repeatedly normalizes rows and columns so the mixing matrix becomes approximately doubly stochastic.The Sinkhorn normalization process works by repeatedly applying two normalization steps:Row normalizationColumn normalizationRow normalization forces every row to sum to 1. Column normalization forces every column to sum to 1.However, these two constraints interfere with each other. When rows are normalized, all rows sum to 1, but the column sums become disturbed. When columns are normalized afterward, all columns sum to 1, but the row sums become disturbed again.Because of this interaction, a single normalization step is not sufficient. Sinkhorn repeatedly alternates between row normalization and column normalization. With each iteration, the matrix gradually moves closer to a state where both conditions are satisfied simultaneously.The final matrix is approximately called doubly stochastic.A doubly stochastic matrix is a matrix where: every row sums to 1 andevery column sums to 1Conceptually, this creates balanced communication between streams:No stream can completely dominate all othersNo stream becomes permanently ignoredInformation exchange stays distributed across streamsGradients propagate more evenly through all residual pathsWithout this constraint, the learned mixing matrix could collapse, where most information flows through only a few streams. Sinkhorn normalization prevents this collapse by encouraging every stream to both send and receive information in a balanced manner.This is why mHC is called manifold-constrained Hyper-Connections. The mixing matrices are constrained onto a structured manifold instead of being completely unconstrained. These constraints improve optimization stability, maintain stream diversity, stabilize deep residual learning, and allow the architecture to scale more reliably to very large models such as DeepSeek V4.I hope these animations and visualizations helped build an intuitive understanding of how mHC works in DeepSeek V4.References:deepseek-ai/DeepSeek-V4-Pro · Hugging Faceinference/model.py · deepseek-ai/DeepSeek-V4-Pro at mainmHC: Manifold-Constrained Hyper-ConnectionsHyper-ConnectionsDeepSeek V4 mHC Explained was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.