Toward Relative Positional Encoding in Spiking Transformers

Changze Lv, Yansen Wang, Dongqi Han, Yifei Shen, Xiaoqing Zheng, Xuanjing Huang, Dongsheng Li

Abstract

Spiking Transformers offer significant energy efficiency advantages but struggle to capture relative token distances due to the incompatibility between continuous positional encodings and binary, spike-based dynamics. This paper introduces two hardware-friendly Relative Positional Encoding (RPE) methods—Gray-PE and Log-PE—specifically designed for Spiking Neural Networks (SNNs). By utilizing XNOR operations and Gray Code properties, these methods enable relative positional awareness while maintaining binary constraints, leading to improved performance across time-series, text, and image tasks.

Introduction

Standard Transformers rely on Relative Positional Encoding (RPE) techniques like RoPE or ALiBi to understand sequence structure. However, Spiking Neural Networks (SNNs), which communicate via discrete binary spikes (0s and 1s), face a fundamental challenge: directly applying continuous RPE math disrupts their sparse, event-driven nature. Consequently, existing spiking Transformers often resort to absolute positional encoding or omit positional information entirely, failing to capture relative context and time-translational invariance. This work addresses the need for spike-native relative positional awareness by rethinking the attention mechanism through the lens of binary logic.

Methodology: Spike-Native Relative Encoding

The proposed framework introduces a fundamental shift in how attention is computed in SNNs, followed by two specific encoding strategies designed to operate within binary constraints.

XNOR-Based Spiking Self-Attention

Traditional attention mechanisms use dot products to measure similarity. In the binary domain of SNNs, the Hamming distance (the count of differing bits) is a more natural and efficient metric. The authors replace the dot product with an exclusive-NOR (XNOR) operation, which effectively measures similarity between binary queries ($Q$) and keys ($K$). This approach maintains the computational efficiency of SNNs and natively supports bitwise logic on neuromorphic hardware.

Gray-PE and Log-PE

Gray-PE leverages Gray Code, a binary system where consecutive values differ by only one bit. A key mathematical property of Gray Code is that it maintains a constant Hamming distance for positions separated by powers of two ($2^n$). By concatenating these binary codes to $Q$ and $K$ matrices, the model gains relative positional information without leaving the binary regime. For vision tasks, this is extended to a 2D format to handle spatial relationships across image patches.

Log-PE addresses potential resolution loss in long sequences by adding a non-negative logarithmic relative distance penalty directly to the attention map. This acts as a decaying sensitivity to distance, providing a spike-compatible alternative to methods like ALiBi.

Methodological overview of XNOR-Based SSA, Gray-PE, and Log-PE implementation.

Experimental Evaluation

The methods were rigorously evaluated across three domains: time-series forecasting (e.g., Metr-la, Electricity), text classification (e.g., SST-2, AGNEWS), and image classification (e.g., CIFAR, Tiny-ImageNet).

Key Results

The inclusion of Gray-PE and Log-PE yielded consistent performance improvements over baseline SNNs like Spikformer and QKFormer.

  • Time-Series Forecasting: As shown in the benchmarking results, the proposed RPE methods consistently improved $R^2$ and reduced RSE across multiple datasets and sequence lengths. For instance, Spikformer-XNOR with Log-PE achieved superior $R^2$ scores compared to standard absolute PE versions.
  • Long-Sequence Tasks: Log-PE proved particularly effective for long sequences. On the AGNEWS text dataset, Spikformer with Log-PE achieved 86.77% accuracy, significantly outperforming the absolute PE baseline of 83.84%.
  • Vision Improvements: The 2D variant of Gray-PE improved patch-based image classification, pushing Spikformer accuracy on CIFAR10 to 95.66% from a 94.80% baseline.
Performance comparison of various Spiking Transformer models and PE types on time-series forecasting datasets.

Discussion and Conclusion

A critical insight of this work is that standard RPE transfer to SNNs is a "dead end"; continuous values disrupt binary spike dynamics. By identifying Hamming distance as the SNN equivalent of the dot product and exploiting the geometric properties of Gray Code, the authors provide a path toward efficient, relative-aware spiking models.

While these methods show promise, scalability to extremely long sequences (e.g., 10,000+ tokens) remains a challenge for future optimization. Additionally, future research will explore expanding evaluations to more complex spatial tasks, such as object detection and 3D geometry representation, ensuring that Spiking Transformers can match the versatility of their continuous counterparts while retaining their energy-efficiency edge.

Summary updated

BibTeX
@inproceedings{lv2025toward,
  author = {Changze Lv and Yansen Wang and Dongqi Han and Yifei Shen and Xiaoqing Zheng and Xuanjing Huang and Dongsheng Li},
  title = {{Toward Relative Positional Encoding in Spiking Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=MDWJlTWZHH},
}
Figure