Contents

Sionna PHY Source Overview: Package Structure and the Object/Block/Config Mechanisms

Sionna Source Code
#Sionna#PHY#source-code-reading#PyTorch

This time I plan to go through Sionna’s physical-layer implementation systematically, starting from src/sionna/phy/. It splits the entire PHY chain into seven functional blocks; here we look at the skeleton first, and the following posts will dive into each one in turn.

1. Directory Structure

phy/
├── __init__.py            Package entry: aggregates config, base classes and submodules
├── block.py               Block abstract base class (base of all processing blocks)
├── object.py              Object base class (precision/device/RNG management)
├── config.py              Global configuration and dtype mapping
├── constants.py           Physical constants
├── mapping.py             Modulation/demapping, constellation classes
├── channel/               Channel models (incl. tr38901, optical)
├── fec/                   Forward error correction (LDPC/Polar/Turbo/convolutional/linear)
├── mimo/                  MIMO detection, equalization, precoding, stream management
├── ofdm/                  OFDM modulation/demodulation, resource grid, channel estimation/detection/equalization
├── nr/                    5G NR physical layer (PUSCH transceiver chain, TB coding, MCS)
├── signal/                Signal processing (filtering, windows, up/downsampling)
└── utils/                 Common utilities (tensors, linear algebra, metrics, plotting, RNG)

The design is clean: channel / fec / mimo / ofdm / nr each own one segment of the chain, signal and utils are the reused low-level infrastructure, and mapping.py sits at the top level instead of being tucked into a submodule, because the modulation mapping is used by both ofdm and nr.

The responsibilities of the submodules can be summed up in one sentence each:

SubmoduleResponsibility
channelWireless/optical channel modeling: AWGN, block fading, time-domain/frequency-domain channels, and the full 3GPP TR 38.901 model (TDL/CDL/UMi/UMa/RMa)
fecChannel coding and decoding: LDPC, Polar, Turbo, convolutional and linear codes, incl. CRC, interleaving, scrambling and EXIT analysis
mimoMIMO detection, equalization, precoding and multi-cell stream management
ofdmOFDM waveform-level processing: resource grid, modulation/demodulation, pilots, channel estimation, detection and equalization
nr5G NR end-to-end PUSCH transceiver chain, transport block (TB) coding and MCS/resource mapping
signalGeneral signal processing: filtering, window functions, up/downsampling and spectral tools
utilsCross-module infrastructure: tensor operations, linear algebra, metrics, random numbers and plotting

2. Top-Level Files

__init__.py only does aggregation, exporting constants (wildcard), config, dtypes, Precision, Object, Block, and mounting all submodules.

The files that really need to be understood are object.py, block.py and config.py — they are the foundation of the entire framework.

object.py: Precision and Device

Object inherits from torch.nn.Module and converges the two global concerns — “precision” and “device” — into a single base class.

  • Attributes: dtype, cdtype, np_dtype, np_cdtype, precision, device, torch_rng, np_rng, py_rng
  • Methods: _convert (recursively converts tensors according to the current precision), _get_shape, __setattr__

The most interesting part here is why it overrides __setattr__: precision is a global configuration, and constant tensors cached inside a model (e.g., filter coefficients, constellation points) must be converted in sync whenever the precision changes, otherwise mixing single/double will immediately raise a dtype mismatch error.

block.py: Lazy Building

Block(Object) is the abstract base class of all processing blocks.

  • built (property): whether the block has finished building
  • build(*arg_shapes, **kwarg_shapes): lazily builds from the input shapes
  • __call__ / forward / call: subclasses implement call, while __call__ converts the inputs according to the block’s precision and stays compatible with torch.compile

The “lazy building” design deserves a note: things like the Tanner-graph neighbor indices of an LDPC code or the phase-compensation factors of an OFDM demodulator depend on the input shape (codeword length, FFT size), and these parameters may not be known at instantiation time. Sionna’s approach is to build the cache once on the first forward pass from arg_shapes, and reuse it directly on subsequent calls — far friendlier than making the user hand-write every dimension parameter in __init__.

config.py: Singleton Configuration

Config is a singleton configuration class, accessed via sionna.phy.config.<property>; the instance is the module-level config.

  • Attributes/methods: py_rng, np_rng, torch_rng(device), seed, device, np_dtype, np_cdtype, dtype, cdtype, precision, available_devices
  • dtypes: dictionary mapping precision to dtype (single/double → the real/complex dtypes of torch/np)
  • Precision: type alias Literal["single", "double"]

Note that the three random number generators provided by config — py_rng, np_rng, torch_rng — are meant to replace np.random / torch.randn. This is done so that config.seed takes effect in one place and results are globally reproducible.

constants.py

Physical constants, taken from scipy.constants:

ConstantMeaning
ALPHA_MAX = 32Maximum value
BOLTZMANN_CONSTANTBoltzmann constant (J/K)
DIELECTRIC_PERMITTIVITY_VACUUMVacuum permittivity (F/m)
HPlanck constant (J/Hz)
PIPi
SPEED_OF_LIGHTSpeed of light (m/s)

3. mapping.py: Modulation and Demodulation

The reason it sits at the top level was stated above: it is a shared dependency of ofdm and nr. About 20 public symbols, in three groups.

Utility functions:

  • pam_gray(b): Gray-labeled PAM constellation
  • qam(num_bits_per_symbol, ...): M-ary QAM complex constellation
  • pam(num_bits_per_symbol, ...): M-ary PAM real constellation

Core classes:

  • Constellation(Block): the constellation (complex-valued vectors) used by (de)mapping
  • Mapper(Block): binary tensor → constellation points
  • Demapper(Block): received symbols → bit-level LLRs or hard decisions
  • SymbolDemapper(Block): received symbols → normalized logits or hard decisions (symbol-level)
  • SymbolLogits2LLRs(Block) / LLRs2SymbolLogits(Block): symbol logits ⇄ bit LLRs
  • SymbolLogits2Moments(Block): compute mean and variance from symbol logits
  • SymbolInds2Bits(Block): symbol indices → binary
  • QAM2PAM(Object) / PAM2QAM(Object): QAM symbol indices ⇄ two PAM symbol indices

Random sources:

  • BinarySource(Block) / SymbolSource(Block) / QAMSource(SymbolSource) / PAMSource(SymbolSource)

The LLR definition at the demodulator output follows the standard communications convention:

L(b)=ln⁡P(b=1∣y)P(b=0∣y)L(b) = \ln \frac{P(b = 1 \mid y)}{P(b = 0 \mid y)}

When Demapper’s demapping_method is "app", the posterior probabilities are computed exactly as above; with "maxlog", the max-log approximation is used to reduce complexity.

4. Usage Examples

Global configuration and random numbers:

from sionna.phy import config

config.seed = 42                 # Set the global random seed for reproducibility
config.precision = "single"      # Computation precision: 'single' or 'double'
print(config.available_devices)  # List of available devices

# Use the built-in random number generators (instead of np.random / torch.randn)
val = config.py_rng.randint(0, 10)
noise = config.np_rng.normal(size=[4])

Modulation mapping and demodulation:

import torch
from sionna.phy.mapping import Constellation, Mapper, Demapper, QAMSource

const = Constellation("qam", 4)          # 16-QAM constellation

mapper = Mapper("qam", 4)
demapper = Demapper("app", "qam", 4)     # app = exact posterior probabilities
bits = torch.randint(0, 2, (10, 100), dtype=torch.float32)
symbols = mapper(bits)                   # [10, 25]

y = symbols + 0.1 * torch.randn_like(symbols)
llr = demapper(y, no=0.01)               # Bit-level LLRs, [10, 100]

qam = QAMSource(4)
s = qam([10, 100])                       # Generate random QAM symbols directly

16-QAM maps 4 bits onto one complex symbol, so a [10, 100] bit tensor comes out as [10, 25]; LLRs are computed per bit, so it goes back to [10, 100].

5. Summary

Remember the three key conventions of the framework first:

  1. Precision is global, switched via config.precision; Object is responsible for keeping the tensors cached inside models in sync.
  2. Building is lazy, Block.build(*arg_shapes) builds the cache from the input shapes on the first forward pass.
  3. Random numbers go through config — don’t use np.random / torch.randn directly, or the seed won’t be under control.

The following posts will unfold in the order channel → fec → mimo → ofdm → nr → signal/utils.

Comments

Loading comments…

    ← Back to home