Dynamic Slicing for Deep Neural Networks

Ziqi Zhang, Yuanchun Li, Yao Guo, Xiangqun Chen, Yunxin Liu

Abstract

Program slicing has been widely applied in a variety of software engineering tasks. However, existing program slicing techniques only deal with traditional programs that are constructed with instructions and variables, rather than neural networks that are composed of neurons and synapses. In this paper, we propose NNSlicer, the first approach for slicing deep neural networks based on data flow analysis. Our method understands the reaction of each neuron to an input based on the difference between its behavior activated by the input and the average behavior over the whole dataset. Then we quantify the neuron contributions to the slicing criterion by recursively backtracking from the output neurons, and calculate the slice as the neurons and the synapses with larger contributions. We demonstrate the usefulness and effectiveness of NNSlicer with three applications, including adversarial input detection, model pruning, and selective model protection. In all applications, NNSlicer significantly outperforms other baselines that do not rely on data flow analysis.

Introduction

In traditional software engineering, program slicing is a fundamental technique used to isolate the specific lines of code that influence a particular variable. This is essential for debugging, testing, and maintenance. However, applying this concept to Deep Neural Networks (DNNs) presents a significant challenge. Unlike traditional code, DNNs are dense, "black-box" structures where every output is technically connected to nearly every neuron in preceding layers. Standard data-flow analysis often fails because it cannot distinguish between meaningful signal and background noise in these massive mathematical graphs.

To address this, we introduce NNSlicer, a tool designed to extract dynamic slices—input-dependent sub-networks—that are responsible for specific model predictions. By identifying the exact subset of neurons and synapses driving an output, NNSlicer enables better model interpretation, efficient pruning, and targeted IP protection.

Methodology: The NNSlicer Framework

NNSlicer operates through a three-phase data flow analysis process to identify critical sub-networks for a given input.

The NNSlicer workflow: (1) Profiling to establish baseline activations, (2) Forward analysis to measure input-specific reactions, and (3) Backward analysis to identify contributing neurons and synapses.

1. Profiling Phase

The system first establishes a behavioral baseline by feeding the entire training dataset through the model. This allows NNSlicer to calculate the average activation value for every neuron, creating a "standard" for normal behavior.

2. Forward Analysis Phase

When a specific input is processed, NNSlicer records the resulting activation values and subtracts the baseline averages. This "relative activation" reveals how sensitively each neuron reacts to the specific input compared to its typical behavior, effectively isolating the signal from the noise.

3. Backward Analysis Phase

Starting from a defined "slicing criterion" (the output neurons of interest), NNSlicer backtracks through the network. It calculates the contribution of preceding neurons by multiplying the target neuron's relative activation, the connecting synapse's weight, and the preceding neuron's relative activation. The final slice consists of the synapses and neurons with the highest cumulative contributions.

Experimental Evaluation and Applications

We evaluated NNSlicer using the CIFAR-10 dataset and various ResNet architectures, demonstrating its effectiveness across three key domains:

Adversarial Input Detection

Adversarial attacks often force data down unnatural computational paths. By training a decision tree on the slices generated by NNSlicer, we achieved 100% recall and 83% precision on average across 17 different attack types (including FGSM, PGD, and CW). This significantly outperforms baseline methods that rely on raw feature maps.

Targeted Model Pruning

NNSlicer allows for class-specific pruning. We successfully pruned 55% of a network's weights while maintaining approximately 80% accuracy on targeted classes without any fine-tuning. With just one epoch of fine-tuning, the sliced model recovered nearly all of its original accuracy for those specific classes, facilitating efficient edge deployment.

Selective Model Protection

To protect intellectual property, NNSlicer can identify the critical parameters for specific model capabilities. By hiding or encrypting just 50% of the parameters identified by NNSlicer, an attacker attempting to steal the model via retraining achieves less than 10% accuracy on target classes.

Discussion and Future Work

While NNSlicer currently supports common CNN operations like weighted sums, max pooling, and ReLU, it does not yet support Recurrent Neural Networks (RNNs) or Graph Convolutional Networks (GCNs). Additionally, scalability remains a challenge; while batch processing reduces the time for a ResNet18 model to approximately 45 seconds per sample, further optimization is needed for real-time applications on massive models.

Future research will focus on expanding slicing criteria to intermediate neurons to debug specific discriminatory attributes, such as race or gender bias, thereby improving model fairness and transparency.

Summary updated

Abstract

Program slicing has been widely applied in a variety of software engineering tasks. However, existing program slicing techniques only deal with traditional programs that are constructed with instructions and variables, rather than neural networks that are composed of neurons and synapses. In this paper, we propose NNSlicer, the first approach for slicing deep neural networks based on data flow analysis. Our method understands the reaction of each neuron to an input based on the difference between its behavior activated by the input and the average behavior over the whole dataset. Then we quantify the neuron contributions to the slicing criterion by recursively backtracking from the output neurons, and calculate the slice as the neurons and the synapses with larger contributions. We demonstrate the usefulness and effectiveness of NNSlicer with three applications, including adversarial input detection, model pruning, and selective model protection. In all applications, NNSlicer significantly outperforms other baselines that do not rely on data flow analysis.
BibTeX
@inproceedings{zhang2020dynamic,
  author = {Ziqi Zhang and Yuanchun Li and Yao Guo and Xiangqun Chen and Yunxin Liu},
  title = {{Dynamic Slicing for Deep Neural Networks}},
  booktitle = {ESEC/SIGSOFT FSE},
  year = {2020},
  url = {https://openreview.net/forum?id=KUuSch6CmZ},
}
Figure