forked from NVlabs/gnn-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgnn.py
More file actions
1059 lines (848 loc) · 36.5 KB
/
gnn.py
File metadata and controls
1059 lines (848 loc) · 36.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.
##### Utility functions for graph neural networks #####
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer, Dense
import pickle
from sionna.utils.metrics import compute_ber
from time import time
import warnings # ignore some internal TensorFlow warnings
# for e2e model
from sionna.utils import BinarySource, ebnodb2no
from sionna.mapping import Mapper, Demapper
from sionna.channel import AWGN
from sionna.fec.ldpc import LDPC5GDecoder, LDPC5GEncoder
class MLP(Layer):
"""Simple MLP layer.
Parameters
----------
units : List of int
Each element of the list describes the number of units of the
corresponding layer.
activations : List of activations
Each element of the list contains the activation to be used
by the corresponding layer.
use_bias : List of booleans
Each element of the list indicates if the corresponding layer
should use a bias or not.
"""
def __init__(self, units, activations, use_bias):
super().__init__()
self._num_units = units
self._activations = activations
self._use_bias = use_bias
def build(self, input_shape):
self._layers = []
for i, units in enumerate(self._num_units):
self._layers.append(Dense(units,
self._activations[i],
use_bias=self._use_bias[i]))
def call(self, inputs):
outputs = inputs
for layer in self._layers:
outputs = layer(outputs)
return outputs
class GNN_BP(Layer):
"""GNN-based message passing decoder.
Parameters
---------
pcm : [num_nc, num_vn], numpy.array
The parity-check matrix.
num_embed_dims: int
Number of dimensions of the vertex embeddings.
num_msg_dims: int
Number of dimensions of a message.
num_hidden_units: int
Number of hidden units of the MLPs used to compute
messages and to update the vertex embeddings.
num_mlp_layers: int
Number of layers of the MLPs used to compute
messages and to update the vertex embeddings.
num_iter: int
Number of iterations.
reduce_op: str
A string defining the vertex aggregation function.
Currently, "mean", "max", "min" and "sum" is supported.
activation: str
A string defining the activation function of the hidden MLP layers to
be used. Defaults to "relu".
output_all_iter: Bool
Indicates if the LLRs of all iterations should be returned as list
or if only the LLRs of the last iteration should be returned.
clip_llr_to: float or None
If set, the absolute value of the input LLRs will be clipped to this value.
use_attributes: Boolean
Defaults to False. If True, trainable node and edge attributes will be
applied per node/edge, respectively.
node_attribute_dims: int
Number of dimensions of each node attribute.
msg_attribute_dims: int
Number of dimensions of each message attribute.
use_bias: Boolean
Defaults to False. Indicates if the MLPs should use a bias or not.
Input
-----
llr : [batch_size, num_vn], tf.float32
Tensor containing the LLRs of all bits.
Output
------
llr_hat: : [batch_size, num_vn], tf.float32
Tensor containing the LLRs at the decoder output.
If `output_all_iter`==True, a list of such tensors will be returned.
"""
def __init__(self,
pcm,
num_embed_dims,
num_msg_dims,
num_hidden_units,
num_mlp_layers,
num_iter,
reduce_op="mean",
activation="tanh",
output_all_iter=False,
clip_llr_to=None,
use_attributes=False,
node_attribute_dims=0,
msg_attribute_dims=0,
use_bias=False):
super().__init__()
self._pcm = pcm # Parity check matrix
self._num_cn = pcm.shape[0] # Number of check nodes
self._num_vn = pcm.shape[1] # Number of variables nodes
self._num_edges = int(np.sum(pcm)) # Number of edges
# Array of shape [num_edges, 2]
# 1st col = CN id, 2nd col = VN id
# The ith row of this array defines the ith edge.
self._edges = np.stack(np.where(pcm), axis=1)
# Create 2D ragged tensor of shape [num_cn,...]
# cn_edges[i] contains the edge ids for CN i
cn_edges = []
for i in range(self._num_cn):
cn_edges.append(np.where(self._edges[:,0]==i)[0])
self._cn_edges = tf.ragged.constant(cn_edges)
# Create 2D ragged tensor of shape [num_vn,...]
# vn_edges[i] contains the edge ids for VN i
vn_edges = []
for i in range(self._num_vn):
vn_edges.append(np.where(self._edges[:,1]==i)[0])
self._vn_edges = tf.ragged.constant(vn_edges)
# Number of dimensions for vertex embeddings
self._num_embed_dims = num_embed_dims
# Number of dimensions for messages
self._num_msg_dims = num_msg_dims
# Number of hidden units for MLPs computing messages and embeddings
self._num_hidden_units = num_hidden_units
# Number of layers for MLPs computing messages and embeddings
self._num_mlp_layers = num_mlp_layers
# Number of BP iterations, can be modified
self._num_iter = num_iter
# Reduce operation for message aggregation
self._reduce_op = reduce_op
# Activation function of the hidden MLP layers
self._activation = activation
# if True, the model returns intermediate llrs
self._output_all_iter = output_all_iter
# Defines the (internal) LLR clipping value
self._clip_llr_to = clip_llr_to
# Actives (trainable) attributes
self._use_attributes = use_attributes
# Node /Edge attribute dimensions
self._node_attribute_dims = node_attribute_dims
self._msg_attribute_dims = msg_attribute_dims
# Activate bias of MLP layers
self._use_bias = use_bias
# Internal state for initialization
self._is_built=False
@property
def num_iter(self):
return self._num_iter
@num_iter.setter # no retracing of graph (=no effect in graph mode)
def num_iter(self, value):
self._num_iter = value
def build(self, input_shape):
if not self._is_built: # only build once
self._is_built=True
# NN to transform input LLR to VN embedding
self._llr_embed = Dense(self._num_embed_dims,
use_bias=self._use_bias)
# NN to transform VN embedding to output LLR
self._llr_inv_embed = Dense(1,
use_bias=self._use_bias)
# CN embedding update function
self.update_h_cn = UpdateEmbeddings(self._num_msg_dims,
self._num_hidden_units,
self._num_mlp_layers,
# Flip columns: "from VN to CN"
np.flip(self._edges, 1),
self._cn_edges,
self._reduce_op,
self._activation,
self._use_attributes,
self._node_attribute_dims,
self._msg_attribute_dims,
self._use_bias)
# VN embedding update function
self.update_h_vn = UpdateEmbeddings(self._num_msg_dims,
self._num_hidden_units,
self._num_mlp_layers,
self._edges, # "from CN to VN"
self._vn_edges,
self._reduce_op,
self._activation,
self._use_attributes,
self._node_attribute_dims,
self._msg_attribute_dims,
self._use_bias)
def llr_to_embed(self, llr):
"""Transform LLRs to VN embeddings."""
return self._llr_embed(tf.expand_dims(llr, -1))
def embed_to_llr(self, h_vn):
"""Transform VN embeddings to LLRs."""
return tf.squeeze(self._llr_inv_embed(h_vn), axis=-1)
def call(self, llr):
"""Run the decoder."""
batch_size = tf.shape(llr)[0]
# Initialize vertex embeddings
if self._clip_llr_to is not None:
llr = tf.clip_by_value(llr, -self._clip_llr_to, self._clip_llr_to)
h_vn = self.llr_to_embed(llr)
h_cn = tf.zeros([batch_size, self._num_cn, self._num_embed_dims])
# BP iterations
if self._output_all_iter:
llr_hat = []
for i in range(self._num_iter):
# Update CN embeddings
h_cn = self.update_h_cn(h_vn, h_cn)
# Update VNs
h_vn = self.update_h_vn(h_cn, h_vn)
if self._output_all_iter:
llr_hat.append(self.embed_to_llr(h_vn))
if not self._output_all_iter:
llr_hat = self.embed_to_llr(h_vn)
return llr_hat
class UpdateEmbeddings(Layer):
"""Update vertex embeddings of the GNN BP decoder.
This layer computes first the messages that are sent across the edges
of the graph, then sums the incoming messages at each vertex, finally and
updates their embeddings.
Parameters
----------
num_msg_dims: int
Number of dimensions of a message.
num_hidden_units: int
Number of hidden units of MLPs used to compute
messages and to update the vertex embeddings.
num_mlp_layers: int
Number of layers of the MLPs used to compute
messages and to update the vertex embeddings.
from_to_ind: [num_egdes, 2], np.array
Two dimensional array containing in each row the indices of the
originating and receiving vertex for an edge.
gather_ind: [`num_vn` or `num_cn`, None], tf.ragged.constant
Ragged tensor that contains for each receiving vertex the list of
edge indices from which to aggregate the incoming messages. As each
vertex can have a different degree, a ragged tensor is used.
reduce_op: str
A string defining the vertex aggregation function.
Currently, "mean", "max", "min" and "sum" is supported.
activation: str
A string defining the activation function of the hidden MLP layers to
be used. Defaults to "relu".
use_attributes: Boolean
Defaults to False. If True, trainable node and edge attributes will be
applied per node/edge, respectively.
node_attribute_dims: int
Number of dimensions of each node attribute.
msg_attribute_dims: int
Number of dimensions of each message attribute.
use_bias: Boolean
Defaults to False. Indicates if the MLP should use a bias or not.
Input
-----
h_from : [batch_size, num_cn or num_vn, num_embed_dims], tf.float32
Tensor containing the embeddings of the "transmitting" vertices.
h_to : [batch_size, num_vn or num_cn, num_embed_dims], tf.float32
Tensor containing the embeddings of the "receiving" vertices.
Output
------
h_to_new : Same shape and type as `h_to`
Tensor containing the updated embeddings of the "receiving" vertices.
"""
def __init__(self,
num_msg_dims,
num_hidden_units,
num_mlp_layers,
from_to_ind,
gather_ind,
reduce_op="sum",
activation="relu",
use_attributes=False,
node_attribute_dims=0,
msg_attribute_dims=0,
use_bias=False):
super().__init__()
self._num_msg_dims = num_msg_dims
self._num_hidden_units = num_hidden_units
self._num_mlp_layers = num_mlp_layers
self._from_ind = from_to_ind[:,0]
self._to_ind = from_to_ind[:,1]
self._gather_ind = gather_ind
self._reduce_op = reduce_op
self._activation = activation
self._use_attributes = use_attributes
self._node_attribute_dims = node_attribute_dims
self._msg_attribute_dims = msg_attribute_dims
self._use_bias = use_bias
# add node attributes
if self._use_attributes:
num_nodes = self._gather_ind.shape[0]
num_edges = self._from_ind.shape[0]
# node attributes
self._g_node = tf.Variable(tf.zeros((num_nodes,
self._node_attribute_dims),tf.float32),
trainable=True)
# edge attributes
self._g_msg = tf.Variable(tf.zeros((num_edges,
self._msg_attribute_dims), tf.float32),
trainable=True)
def build(self, input_shape):
num_embed_dims = input_shape[-1]
# MLP to compute messages
units = [self._num_hidden_units]*(self._num_mlp_layers-1) + [self._num_msg_dims]
activations = [self._activation]*(self._num_mlp_layers-1) + [None]
use_bias = [self._use_bias]*self._num_mlp_layers
self._msg_mlp = MLP(units, activations, use_bias)
# MLP to update embeddings from accumulated messages
units[-1] = num_embed_dims
self._embed_mlp = MLP(units, activations, use_bias)
def call(self, h_from, h_to):
# Concatenate embeddings of the transmitting (from) and receiving (to) vertex for each edge
features = tf.concat([tf.gather(h_from, self._from_ind, axis=1),
tf.gather(h_to, self._to_ind, axis=1)],
axis=-1)
# Add message attribute
if self._use_attributes:
attr = tf.tile(tf.expand_dims(self._g_msg, axis=0),
[tf.shape(features)[0], 1, 1])
features = tf.concat((features, attr), axis=-1)
# Compute messsages for all edges
messages = self._msg_mlp(features)
# Reduce messages at each receiving (to) vertex
# note: bring batch dim to last dim for improved performance
# with ragged tensors
messages = tf.transpose(messages, (1,2,0))
m_ragged = tf.gather(messages, self._gather_ind, axis=0)
if self._reduce_op=="sum":
m = tf.reduce_sum(m_ragged, axis=1)
elif self._reduce_op=="mean":
m = tf.reduce_mean(m_ragged, axis=1)
elif self._reduce_op=="max":
m = tf.reduce_max(m_ragged, axis=1)
elif self._reduce_op=="min":
m = tf.reduce_min(m_ragged, axis=1)
else:
raise ValueError("unknown reduce operation")
m = tf.transpose(m, (2,0,1)) # batch-dim back to first dim
# Add node attribute
if self._use_attributes:
# tile to bs dim
attr = tf.tile(tf.expand_dims(self._g_node, axis=0),
[tf.shape(m)[0], 1, 1])
m = tf.concat((m, attr), axis=-1)
# Compute new embeddings
h_to_new = self._embed_mlp(tf.concat([m, h_to], axis=-1))
return h_to_new
######### Utility functions #########
def save_weights(system, model_path):
"""Save model weights.
This function saves the weights of a Keras model ``system`` to the
path as provided by ``model_path``.
Parameters
----------
system: Keras model
A model containing the weights to be stored.
model_path: str
Defining the path where the weights are stored.
"""
weights = system.get_weights()
with open(model_path, 'wb') as f:
pickle.dump(weights, f)
def load_weights(system, model_path):
"""Load model weights.
This function loads the weights of a Keras model ``system`` from a file
provided by ``model_path``.
Parameters
----------
system: Keras model
The target model into which the weights are loaded.
model_path: str
Defining the path where the weights are stored.
"""
with open(model_path, 'rb') as f:
weights = pickle.load(f)
system.set_weights(weights)
def train_gnn(model, params):
"""Training function for the GNN decoder model.
This function also generates log files and save the weights every N
iterations.
Parameters
----------
system: Keras model
The system model that should be trained.
params: dict
Defining all required training/model parameters.
"""
# we ignore TF warnings related to sparse indexed slices
# These warnings are related to raggedTensors and cannot be easily fixed.
warnings.filterwarnings('ignore', '.*Converting sparse IndexedSlices.*', )
# We use the BCE loss
loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)
# This function logs the training progress into an external file
log_dir = params["save_dir"] \
+ params["run_name"] \
+ "_log.txt"
# generate log/result folder
try:
os.makedirs(params["save_dir"])
except FileExistsError:
pass
with open(log_dir, 'a') as f:
f.write("\n----Starting Training----\n")
f.write(str(params) + "\n")
# SGD update iteration
@tf.function()
def train_step(batch_size):
# train for random SNRs within a pre-defined interval
ebno_db = tf.random.uniform([batch_size, 1],
minval=params["ebno_db_train_min"],
maxval=params["ebno_db_train_max"])
with tf.GradientTape() as tape:
c, llr_hat = model(batch_size, ebno_db)
loss_value = 0
# we use a multi-loss averaged over all iterations
for _, l in enumerate(llr_hat):
loss_value += loss(c, l)
# and apply the SGD updates
weights = model.trainable_weights
grads = tape.gradient(loss_value, weights)
optimizer.apply_gradients(zip(grads, weights))
return c, llr_hat
# init the optimizer; we use Adam throughout this work
optimizer = tf.keras.optimizers.Adam(
learning_rate=params["learning_rate"][0])
# run the training iterations
iter_total = 0
time_start = time() # measure time per 1000 iters
# for each list-element in the training parameters we run the SGD-updates
for idx,_ in enumerate(params["batch_size"]):
batch_size = tf.constant(params["batch_size"][idx], tf.int32)
lr = params["learning_rate"][idx]
train_iter = params["train_iter"][idx]
# set new learning rate
optimizer.lr.assign(lr)
# and log the training
log_str = f"New training parameters - bs: {batch_size}, "\
f"lr: {lr}, iters: {train_iter}"
print(log_str)
with open(log_dir, 'a') as f:
f.write(log_str + "\n")
# run pre-defined number of training iterations
for it in range(train_iter):
iter_total += 1 # total number of iters to log training progress
train_step(batch_size) # and train
# evaluate intermediate training results
if iter_total%params["eval_train_steps"]==0:
ebno_db = tf.random.uniform([params["batch_size_eval"], 1],
minval=params["ebno_db_eval"],
maxval=params["ebno_db_eval"])
c, llr_hat = model(params["batch_size_eval"], ebno_db)
loss_value = 0
for l in llr_hat:
loss_value += loss(c, l)
# for BER calculations only consider last decoder iterations
# i.e., [-1] axis)
c_hat = tf.cast(tf.greater(llr_hat[-1], 0), tf.float32)
ber = compute_ber(c, c_hat).numpy()
# measure required time since last evaluation
duration = time() - time_start # in s
time_start = time() # reset counter
log_str = f"Iteration {iter_total}, " \
f"loss = {loss_value.numpy():.3f}, " \
f"ber = {ber:.5f}, " \
f"duration: {duration:.2f}s"
print(log_str)
# and write intermediate results in file
with open(log_dir, 'a') as f:
f.write(log_str +"\n")
# save weights of model every X iters
# keep in mind that this may require a log of memory
if iter_total%params["save_weights_iter"]==0:
model_path = params["save_dir"] \
+ params["run_name"] \
+ "_" + str(iter_total) + ".npy"
save_weights(model, model_path)
# and save the final weights
model_path = params["save_dir"] + params["run_name"] + "_final.npy"
save_weights(model, model_path)
class E2EModel(tf.keras.Model):
"""End-to-end model for (GNN-)decoder evaluation.
Parameters
----------
encoder: Layer or None
Encoder layer, no encoding applied if None.
decoder: Layer or None
Decoder layer, no decoding applied if None.
k: int
Number of information bits per codeword.
n: int
Codeword lengths.
return_infobits: Boolean
Defaults to False. If True, only the ``k`` information bits are
returned. Must be supported be the decoder as well.
es_no: Boolean
Defaults to False. If True, the SNR is not rate-adjusted (i.e., Es/N0).
Input
-----
batch_size: int or tf.int
The batch_size used for the simulation.
ebno_db: float or tf.float
A float defining the simulation SNR.
Output
------
(c, llr):
Tuple:
c: tf.float32
A tensor of shape `[batch_size, n] of 0s and 1s containing the
transmitted codeword bits.
llr: tf.float32
A tensor of shape `[batch_size, n] of llrs containing estimated on
the codeword bits.
"""
def __init__(self, encoder, decoder, k, n, return_infobits=False, es_no=False):
super().__init__()
self._n = n
self._k = k
self._binary_source = BinarySource()
self._num_bits_per_symbol = 1 # originally 2, should be 1 if using BPSK
self._mapper = Mapper("pam", self._num_bits_per_symbol) # originally "qam"
self._demapper = Demapper("app", "pam", self._num_bits_per_symbol) # originally "qam"
self._channel = AWGN()
self._decoder = decoder
self._encoder = encoder
self._return_infobits = return_infobits
self._es_no = es_no
self._rng = tf.random.Generator.from_seed(seed=234)
@tf.function(jit_compile=True)
def call(self, batch_size, ebno_db):
# no rate-adjustment for uncoded transmission or es_no scenario
if self._decoder is not None and self._es_no==False:
no = ebnodb2no(ebno_db, self._num_bits_per_symbol, self._k/self._n)
else: #for uncoded transmissions the rate is 1
no = ebnodb2no(ebno_db, self._num_bits_per_symbol, 1)
b = self._binary_source([batch_size, self._k])
if self._encoder is not None:
c = self._encoder(b)
else:
c = b
# check that rate calculations are correct
assert self._n==c.shape[-1], "Invalid value of n."
# zero padding to support odd codeword lengths
if self._n%2==1:
c_pad = tf.concat([c, tf.zeros([batch_size, 1])], axis=1)
else: # no padding
c_pad = c
x = self._mapper(c_pad)
# For channels where each bit has different spectral density (variance)
if len(no.shape) == 0:
no = tf.reshape(no, [1,1])
no_list = tf.tile(no, [1, x.shape[1]])
# # bursty
# no_bernoulli = tf.cast(tf.random.categorical(tf.math.log(tf.tile([[0.5, 0.5]], [batch_size, 1])), x.shape[1]), dtype=no.dtype) # sample [0,1] with probability 0.5 for each bit of each codeword in the batch
# no = no_list + tf.multiply(no_bernoulli, no) # compute the original uniform variance plus the bursty variance
# burst
Q = 0.8
q = 0.5
# no_burst = np.zeros((no_list.shape[0], no_list.shape[1]))
# for i in range(no_burst.shape[0]):
# current_state = 0
# for j in range(no_burst.shape[1]):
# if current_state == 0:
# if not np.random.binomial(1, Q):
# current_state = 1
# else:
# no_burst[i, j] = 1
# if not np.random.binomial(1, q):
# current_state = 0
# no_burst = tf.convert_to_tensor(no_burst, dtype=no.dtype)
no_burst = tf.zeros_like(no_list, dtype=no.dtype)
for i in range(len(no_burst)): # FIXME
current_state = 0
for j in range(no_burst.shape[1]):
if current_state == 0:
if not np.random.binomial(1, Q):
current_state = 1
else:
sets = tf.constant([[i, j]])
no_burst = tf.tensor_scatter_nd_update(no_burst, tf.expand_dims(sets, 1), tf.constant([[1.]]))
if not np.random.binomial(1, q):
current_state = 0
no = no_list + tf.multiply(no_burst, no)
y = self._channel([x, no])
llr = self._demapper([y, no])
# remove zero padded bit at the end
if self._n%2==1:
llr = llr[:,:-1]
# and run the decoder
if self._decoder is not None:
llr = self._decoder(llr)
if self._return_infobits:
return b, llr
else:
return c, llr
def export_pgf(ber_plot, col_names):
"""Export results as table for for pgfplots compatible imports.
Parameters
----------
ber_plot: PlotBER
An object of PlotBER containing the BER simulations to be exported
col_names: list of str
Column names of the exported BER curves
"""
s = "snr, \t"
for idx, var_name in enumerate(col_names):
s += var_name + ", \t"
s += "\n"
for idx_snr,snr in enumerate(ber_plot._snrs[0]):
s += f"{snr:.3f},\t"
for idx_dec, _ in enumerate(col_names):
s += f"{ber_plot._bers[idx_dec][idx_snr].numpy():.6E},\t"
s += "\n"
print(s)
def generate_pruned_pcm_5g(decoder, n, verbose=True):
"""Utility function to get the pruned parity-check matrix of the 5G code.
Identifies the pruned and shortened positions.
Hereby, '0' indicates an pruned codeword position
'1' indicates an codeword position
'2' indicates a shortened position.
Parameters
---------
decoder: LDPC5GDecoder
An instance of the decoder object.
n: int
The codeword lengths including rate-matching.
verbose: Boolean
Defaults to True. If True, status information during pruning is
provided.
"""
enc = decoder._encoder
# transmitted positions
pos_tx = np.ones(n)
# undo puncturing of the first 2*z information bits
pos_punc = np.concatenate([np.zeros([2*enc.z]),pos_tx], axis=0)
# puncturing of the last positions
# total length must be n_ldpc, while pos_tx has length n
# first 2*z positions are already added
# -> add n_ldpc - n - 2Z punctured positions
k_short = enc.k_ldpc - enc.k # number of shortend bits
num_punc_bits = ((enc.n_ldpc - k_short) - enc.n - 2*enc.z)
pos_punc2 = np.concatenate(
[pos_punc, np.zeros([num_punc_bits - decoder._nb_pruned_nodes])])
# shortening (= add 0 positions after k bits, i.e. LLR=LLR_max)
# the first k positions are the systematic bits
pos_info = pos_punc2[0:enc.k]
# parity part
num_par_bits = (enc.n_ldpc-k_short-enc.k-decoder._nb_pruned_nodes)
pos_parity = pos_punc2[enc.k:enc.k+num_par_bits]
pos_short = 2 * np.ones([k_short]) # "2" indicates shortened position
# and concatenate final pattern
rm_pattern = np.concatenate([pos_info, pos_short, pos_parity], axis=0)
# and prune matrix (remove shortend positions from pcm)
pcm_pruned = np.copy(decoder.pcm.todense())
idx_short = np.where(rm_pattern==2)
idx_pruned = np.setdiff1d(np.arange(pcm_pruned.shape[1]), idx_short)
pcm_pruned = pcm_pruned[:,idx_pruned]
num_shortened = np.size(idx_short)
# print information if enabled
if verbose:
print("using bg: ", enc._bg)
print("# information bits:", enc.k)
print("CW length after rate-matching:", n)
print("CW length without rm (incl. first 2*Z info bits):",
pcm_pruned.shape[1])
print("# punctured bits:", num_punc_bits)
print("# pruned nodes:", decoder._nb_pruned_nodes)
print("# parity bits", num_par_bits)
print("# shortened bits", num_shortened)
print("pruned pcm dimension:", pcm_pruned.shape)
return pcm_pruned, rm_pattern[idx_pruned]
class LDPC5GGNN(GNN_BP):
"""GNN-based Decoder for 5G LDPC codes incl. internal rate-matching.
This layer inherits from the GNN_BP decoder and extends its functionality
by the LDPC rate-matching.
Parameters
---------
encoder : LDPC5GEncoder
Instance of LDPC5GEncoder used for encoding.
num_embed_dims: int
Number of dimensions of the vertex embeddings.
num_msg_dims: int
Number of dimensions of a message.
num_hidden_units: int
Number of hidden units of the MLPs used to compute
messages and to update the vertex embeddings.
num_mlp_layers: int
Number of layers of the MLPs used to compute
messages and to update the vertex embeddings.
num_iter: int
Number of iterations.
reduce_op: str
A string defining the vertex aggregation function.
Currently, "mean", "max", "min" and "sum" is supported.
activation: str
A string defining the activation function of the hidden MLP layers to
be used. Defaults to "relu".
output_all_iter: Bool
Indicates if the LLRs of all iterations should be returned as list
or if only the LLRs of the last iteration should be returned.
clip_llr_to: float or None
If set, the absolute value of the input LLRs will be clipped to this value.
use_attributes: Boolean
Defaults to False. If True, trainable node and edge attributes will be
applied per node/edge, respectively.
node_attribute_dims: int
Number of dimensions of each node attribute.
msg_attribute_dims: int
Number of dimensions of each message attribute.
return_infobits: Boolean
Defaults to False. Indicates if only the `k` information bits are
returned.
use_bias: Boolean
Defaults to True. Indicates if the MLPs should use a bias or not.
Input
-----
llr : [batch_size, num_vn], tf.float32
Tensor containing the LLRs of all bits.
Output
------
llr_hat: : [batch_size, num_vn], tf.float32
Tensor containing the LLRs at the decoder output.
If `output_all_iter`==True, a list of such tensors will be returned.
"""
def __init__(self,
encoder,
num_embed_dims,
num_msg_dims,
num_hidden_units,
num_mlp_layers,
num_iter,
reduce_op="mean",
activation="tanh",
output_all_iter=False,
clip_llr_to=None,
use_attributes=False,
node_attribute_dims=0,
msg_attribute_dims=0,
return_infobits=False,
use_bias=True,
**kwargs):
self._encoder = encoder
self._return_infobits = return_infobits
self._llr_max = 20 # internal max value for LLR initialization
# instantiate internal decoder object to access pruned pcm
# Remark: this object is NOT used for decoding!
decoder = LDPC5GDecoder(encoder, prune_pcm=True)
# access pcm and code properties
self._n_pruned = decoder._n_pruned
self._num_pruned_nodes = decoder._nb_pruned_nodes
# prune and remove shortened positions
self._pcm, self._rm_pattern = generate_pruned_pcm_5g(decoder,
encoder.n,
verbose=False)
# precompute pruned positions
gather_ind = encoder.n * np.ones(np.size(self._rm_pattern))
gather_ind_inv = np.zeros(np.size(np.where(self._rm_pattern==1)))
for idx, pos in enumerate(np.where(self._rm_pattern==1)[0]):
gather_ind[pos] = idx
gather_ind_inv[idx] = pos
self._rm_ind = tf.constant(gather_ind, tf.int32)
self._rm_inv_ind = tf.constant(gather_ind_inv, tf.int32)
# init GNN decoder
super().__init__(self._pcm,
num_embed_dims,
num_msg_dims,
num_hidden_units,
num_mlp_layers,
num_iter,
reduce_op,
activation,
output_all_iter,
clip_llr_to,
use_attributes,
node_attribute_dims,
msg_attribute_dims,
use_bias,
**kwargs)
#########################################
# Public methods and properties
#########################################
@property
def llr_max(self):
"""Max LLR value used for rate-matching."""
return self._llr_max