According to the TCP/IP network protocol, network packets are composed of multiple traffic bytes, which are further combined to constitute data flows. In this way, the bytes, packets, sessions, and their combinations in the network traffic can be compared to the combination of characters, words, sentences, and texts in natural language. Therefore, traffic data can essentially be regarded as sequential data with strong contextual dependencies. From this perspective, network intrusion detection can be reformulated as a sequence labeling or classification task, where the model must read the flow to identify semantic anomalies. The requirement for efficient sequence modeling arises directly from the hardware limitations of decentralized sensor nodes. In these environments, the window of observation for a flow often needs to be long to detect stealthy multi-stage attacks, yet the peak memory must remain at the low-megabyte level. Efficient sequence modeling, specifically with linear complexity , is the only viable path to enable these resource-constrained nodes to perform real-time deep packet or flow-based analysis without offloading to a centralized cloud. Theoretically, since power consumption in embedded AI is primarily driven by computational density and memory data movement, the reduction from quadratic to linear complexity inherently minimizes the energy budget required per inference. This linear scaling ensures that the energy-per-flow remains stable even as the observation window for detecting stealthy attacks expands. However, current machine learning-based NIDSs mainly focus on classifying individual network flow records, without considering the temporal dependencies and long-term interaction characteristics present in network traffic during communication.
Despite their efficacy in long-term dependency capture, attention-based NIDSs are often impractical for edge session analysis due to the computational overhead previously identified. Furthermore, the Transformer’s global attention mechanism may lead to scattered attention distributions when dealing with highly redundant or noisy network traffic, thereby reducing the model’s ability to focus on critical attack features.
2.3.1. Input Encoder
The input encoder in flow-based network intrusion detection systems is considered to be an essential component in the conversion of raw flow data into feature vectors, which can be further processed by the NIDS-Mamba model. Flow data is usually presented in tabular form, with each flow consisting of fixed fields that can be categorized into two types: numerical fields, such as the number of packets, in which the numerical differences have contextual significance, and categorical fields, such as ports, in which the numerical differences do not have any significance. For instance, ports 25 and 22 have significant differences, as they belong to different protocols, while ports 443 and 8080 have similar uses.
Therefore, the input encoder must process categorical fields such that their representation in the vector space is not influenced by numerical magnitude, ensuring that the generated feature vectors reflect key information related to network traffic for subsequent model analysis. This section encodes categorical fields by converting them into continuous vectors with contextual meaning separately [
10,
31], then concatenating these vectors with numerical vectors to obtain a feature vector available for the subsequent model. Since there is no inherent order among features in tabular data, position encoding is not employed. The process is illustrated in
Figure 3.
Assume that (
xcat,
xnum) represents a feature target pair, where
xcat denotes all categorical features and
xnum represents all numerical features.
xcat = {
x1,
x2, …,
xm} where each
xi represents a categorical field, and
. The value of
xi belongs to a finite set. Through an embedding layer, a
d-dimensional continuous vector
is learned for each category value, as shown in Equation (4):
The embedding layer here is designed as a lookup table, where each
xi corresponds to a vector
. The parameters of the embedding layer consist of an embedding matrix
, where
K denotes the total number of possible category values. The representation is shown in Equation (5):
The parameters of the embedding layer are continuously updated during the training process to ensure that similar categories have closer representations in the embedding space. Given a specific value
k for
xi, its corresponding embedding vector can be obtained by retrieving the
k-th row of the embedding matrix, as expressed in Equation (6):
This denotes the retrieval of the corresponding embedding vector for the categorical value from the embedding matrix.
After concatenation of categorical and numerical embeddings, the input flow record is mapped to a fixed hidden dimension of D = 128. This dimension ensures a sufficiently high-order representation of network features while remaining lightweight for edge gateways.
2.3.2. NIDS-Mamba Model Architecture
As illustrated in
Figure 4, the overall architecture of NIDS-Mamba is a hierarchical stacking of four identical layers, where each layer integrates a Mamba block, a self-attention layer, a Feed-Forward Network (FFN), and a convolutional layer through a synergistic interaction mechanism. Within each layer, these components operate in a sequential and complementary manner to extract multi-level features from network flows.
First, the Mamba block carries out an initial selective scan on the input feature vector to obtain time-varying dependence and exclude irrelevant noises to establish a more refined representation as the basis for further processing. After this step, the result will be fed into the self-attention layer, where the model will apply the attention mechanism to learn long-term temporal dependencies and high-level correlation within the multidimensional feature vectors. The self-attention layer makes up for the possible information loss during the dimension reduction of the SSM layer by enhancing the modeling ability and learning the correlation between different features. Then, the next step would be the feeding of the feature vector to the FFN for non-linear transformation of the aggregated data for mapping to a new feature space for learning complicated relationships necessary for traffic classification. In the end, convolutional layers enable a larger field for dependency learning for both local and global interactions among tokens in the traffic flows. Throughout this four-layer process, residual connections are employed between each module to address gradient vanishing and exploding problems, ensuring that critical information from the original embedding flows smoothly through the deep architecture.
To ensure the reproducibility of the proposed NIDS-Mamba, the detailed architectural configurations and hyperparameter settings are summarized in
Table 1. These parameters were determined through empirical optimization and grid search to balance detection accuracy and computational overhead for IoT edge deployment.
The Mamba-Block within the NIDS-Mamba architecture is illustrated in
Figure 5. This layer selects relevant data from the input vector and learns time-varying dependencies through an SSM. The internal architecture of the Mamba-Block adopts a gated dual-path design to synergistically process network traffic features. Upon entering the block, the input sequence is bifurcated into two symmetric branches through linear projections. The primary branch (left) first utilizes a 1D convolution layer to aggregate local spatial–temporal correlations within the flow, which is then fed into the Selective SSM module. This module acts as the core engine, performing a hardware-aware selective scan to model long-range dependencies by dynamically adjusting its state transition parameters based on the input content. Simultaneously, the auxiliary branch (right) serves as a gating mechanism, where the projected features are activated by a Sigmoid Linear Unit (SiLU) function to generate a modulation signal. The interaction between these two paths is realized through a Hadamard product, allowing the model to selectively amplify critical attack-related features while suppressing background network noise. Finally, the fused information is projected back to the original dimension and integrated with the initial input via a residual connection, ensuring robust gradient flow and information stability across deep layers.
For the
l-th NIDS-Mamba block, the input flow vector is defined as
, where
B is the batch size,
L represents the sequence length of the network flow, and
D denotes the feature dimension. To capture high-order dependencies, the input is first mapped to a high-dimensional space via two parallel linear projection functions:
where
serve as the intermediate feature branch and the gating branch, respectively, with
E being the expansion factor. In the Mamba block, factor
E is set to 2, effectively projecting the input to a 256-dimensional space. The 1D convolution layer employs a kernel size of 4 to capture local dependencies within the expanded flow features. Subsequently, non-linear transformations are applied to these branches to extract local context:
In this formulation,
represents the feature tensor processed by a one-dimensional convolution (Conv1D) and the SiLU activation function. The gated tensor
is used to modulate the information flow. Based on the selective mechanism, the parameter matrices
B,
C, and the step size
of the SSM are dynamically derived from
x′ as follows [
32]:
where
and
represent learnable linear transformations. By employing the computed step size
, the continuous system matrices
A and
B are discretized into
and
:
Following the selective SSM scan, the latent output
is generated:
As illustrated in Equation (12), the output
y is multiplied by the gated branch
z′ via the Hadamard product
and undergoes a final linear projection to revert to the original dimension
D. This result is combined with the initial input via a residual connection to produce the final block output
:
The attention mechanism enables each time step to interact with all information across other time steps, thereby capturing long-term dependencies within the traffic sequence. This process is typically implemented utilizing the Scaled Dot-Product [
33], as detailed in
Figure 6.
Each position of the input
is multiplied by the weight matrices
,
, and
to generate three vectors: Query (Q), Key (K), and Value (V). For each position’s Query, a dot product is computed with the Key of other positions to yield attention scores, which are subsequently normalized via a Softmax function to determine the attention weights for each position. These weights are then multiplied by the corresponding Value to derive the output for each position. Finally, a FFN applies an independent transformation to the output of each position. The specific computational procedure is expressed in Equation (13):
In the equation above, represents the dimension of the Key vector, which is utilized to scale the attention scores.
However, in real-world scenarios, data flows frequently exhibit a substantial amount of homologous communication features, yet traditional self-attention mechanisms compute their similarities regardless. In NIDS-Mamba, not all feature units are required to participate in attention interactions; flow features possessing similar statistical properties should instead be aggregated into a higher-order traffic pattern representation. Directly binding the original input dimensions to the dimensions of the Q, K, and V vectors not only introduces redundant computational overhead but also constrains the model’s learning capacity.
To address these limitations, this section proposes a scalable self-attention mechanism. By introducing two scaling factors,
and
, it decouples the dimensionality of the input vector from that of the Q, K, and V vectors. The selection of scaling factors
and
is determined by the specific resource constraints of the target IoT edge device and the complexity of the network traffic features.
controls the compression ratio of the temporal dimension (sequence length), while
governs the dimensionality reduction of the feature space. We employ a multi-head scalable attention mechanism with eight attention heads. By setting the scaling factors
= 4 and
= 1, the model effectively aggregates temporal patterns across every four tokens, reducing the computational complexity from quadratic to linear
. In our experiments, these values are optimized through a grid search: a higher scaling factor preserves more fine-grained information but increases computational latency, whereas a lower factor enhances throughput at the cost of potential accuracy degradation. This flexibility allows NIDS-Mamba to be reconfigured for different hardware tiers, from high-performance gateways to low-power sensor nodes. Furthermore, three transformation functions—
,
, and
—are employed to reduce the dimensionality of the weight matrices
,
, and
. To implement the transformation functions, we employ a combination of depthwise separable convolution and linear projection. For an input sequence
, the transformation is defined as follows:
where
denotes a depthwise convolution with a stride corresponding to the scaling factor
, effectively aggregating neighboring flow features into a higher-order representation. The
layer then projects the hidden dimension
D to the target scale
. By utilizing depthwise convolution, we maintain a minimal parameter count while capturing local spatial–temporal correlations that standard point-wise projections would miss. The detailed calculation is presented in Equation (15):
where
,
, and
. The dimensional scaling of the three weight matrices is achieved through
,
, and
, a procedure that effectively mitigates unnecessary intermediate multiplications. In practice, the implementation of these three transformation functions relies on the combined synergistic effects of convolution and linear projection. The scalable self-attention mechanism preserves the dimensions of the input matrix, ensuring rigorous dimensional alignment between the input and output.
Unlike the standard self-attention mechanism, which suffers from complexity, the proposed scalable mechanism reduces the complexity to . While existing efficient attention variants, such as Linformer or Performer, often use random projections or low-rank approximations, our approach specifically leverages the redundancy in NetFlow data through learnable convolutional kernels. This ensures that the model does not just mathematically compress the input, but actively filters homologous communication patterns common in IoT botnet attacks, thereby improving the recall of stealthy anomalies.
The function of the convolution layer is to expand the model’s receptive field, enhancing its capability to model the interactive correlations of both local and global information within the flow sequence. Throughout this procedure, the dimensions of the input and output sequences remain consistent. The convolution process is illustrated in
Figure 7.
The dedicated convolution layer following the FFN utilizes a kernel with a stride of 1. This configuration expands the receptive field to integrate global spatio-temporal contexts without altering the sequence dimensionality.