relayrl_algorithms 0.4.1

Independent / Multi-Agent Proximal Policy Optimization (PPO) Algorithms for the RelayRL framework
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
use crate::algorithms::{
    GenericMlp, LayerSpecs, NeuralNetwork, NeuralNetworkError, NeuralNetworkSpec, ValueFunction,
};
use crate::algorithms::{convert_byte_dtype_to_f32, convert_byte_dtype_to_i64};

use burn_tensor::TensorData as BurnTensorData;
use burn_tensor::backend::Backend;
use burn_tensor::{BasicOps, Float, Int, Tensor, TensorKind};
use rand::RngExt;
use rand_distr::Distribution;
use rayon::prelude::*;
use relayrl_types::data::tensor::NdArrayDType;
#[cfg(feature = "tch-backend")]
use relayrl_types::data::tensor::TchDType;
use relayrl_types::data::tensor::{DType, TensorData};
use relayrl_types::prelude::tensor::relayrl::BackendMatcher;
use std::{collections::HashMap, sync::Arc};

// ---- training module  ----

pub(crate) mod training {
    use super::*;

    extern crate burn_core as burn;

    use burn_autodiff::Autodiff;
    use burn_core::module::Initializer;
    use burn_core::module::Module;
    use burn_nn::{Linear, LinearConfig, Relu};
    use burn_optim::adaptor::OptimizerAdaptor;
    use burn_optim::grad_clipping::GradientClipping;
    use burn_optim::{Adam, AdamConfig, GradientsParams, Optimizer};
    use burn_tensor::activation::log_softmax;

    #[cfg(feature = "tch-backend")]
    use burn_tch::LibTorch;
    #[cfg(feature = "tch-backend")]
    pub type TB = Autodiff<LibTorch>;

    #[cfg(not(feature = "tch-backend"))]
    use burn_ndarray::NdArray;
    #[cfg(not(feature = "tch-backend"))]
    pub type TB = Autodiff<NdArray>;

    /// Separate pi and vf layer stacks. Trained with one shared Adam optimizer.
    #[derive(Module, Debug)]
    pub struct ActorCriticMlp<B: burn_tensor::backend::Backend> {
        pub pi_layers: Vec<Linear<B>>,
        pub vf_layers: Vec<Linear<B>>,
        pub relu: Relu,
        pub obs_dim: usize,
        pub act_dim: usize,
    }

    impl<B: burn_tensor::backend::Backend> ActorCriticMlp<B> {
        pub fn new(
            obs_dim: usize,
            hidden_sizes: &[usize],
            act_dim: usize,
            device: &B::Device,
        ) -> Self {
            let mut pi_dims = vec![obs_dim];
            pi_dims.extend_from_slice(hidden_sizes);
            pi_dims.push(act_dim);
            let pi_n = pi_dims.len() - 1;
            let pi_layers = pi_dims
                .windows(2)
                .enumerate()
                .map(|(i, w)| {
                    let gain = if i < pi_n - 1 { 2.0f64.sqrt() } else { 0.01 };
                    let mut layer = LinearConfig::new(w[0], w[1])
                        .with_initializer(Initializer::Zeros)
                        .init(device);
                    layer.weight = Initializer::Orthogonal { gain }.init_with(
                        [w[0], w[1]],
                        Some(w[0]),
                        Some(w[1]),
                        device,
                    );
                    layer
                })
                .collect();

            let mut vf_dims = vec![obs_dim];
            vf_dims.extend_from_slice(hidden_sizes);
            vf_dims.push(1);
            let vf_n = vf_dims.len() - 1;
            let vf_layers = vf_dims
                .windows(2)
                .enumerate()
                .map(|(i, w)| {
                    let gain = if i < vf_n - 1 { 2.0f64.sqrt() } else { 1.0 };
                    let mut layer = LinearConfig::new(w[0], w[1])
                        .with_initializer(Initializer::Zeros)
                        .init(device);
                    layer.weight = Initializer::Orthogonal { gain }.init_with(
                        [w[0], w[1]],
                        Some(w[0]),
                        Some(w[1]),
                        device,
                    );
                    layer
                })
                .collect();

            Self {
                pi_layers,
                vf_layers,
                relu: Relu::new(),
                obs_dim,
                act_dim,
            }
        }

        pub fn pi_forward(&self, input: Tensor<B, 2, Float>) -> Tensor<B, 2, Float> {
            let mut x = input;
            for (i, layer) in self.pi_layers.iter().enumerate() {
                x = layer.forward(x);
                if i < self.pi_layers.len() - 1 {
                    x = self.relu.forward(x);
                }
            }
            x
        }

        pub fn vf_forward(&self, input: Tensor<B, 2, Float>) -> Tensor<B, 2, Float> {
            let mut x = input;
            for (i, layer) in self.vf_layers.iter().enumerate() {
                x = layer.forward(x);
                if i < self.vf_layers.len() - 1 {
                    x = self.relu.forward(x);
                }
            }
            x
        }
    }

    pub struct PPOActorCriticTrainer {
        pub network: Option<ActorCriticMlp<TB>>,
        pub optimizer: OptimizerAdaptor<Adam, ActorCriticMlp<TB>, TB>,
        pub lr: f64,
        pub vf_coef: f32,
        pub lr_schedule_steps: Option<u64>,
        pub grad_step_count: u64,
    }

    impl PPOActorCriticTrainer {
        pub fn new(
            obs_dim: usize,
            hidden_sizes: &[usize],
            act_dim: usize,
            lr: f64,
            vf_coef: f32,
            lr_schedule_steps: Option<u64>,
        ) -> Self {
            let device = <TB as burn_tensor::backend::Backend>::Device::default();
            let network = ActorCriticMlp::new(obs_dim, hidden_sizes, act_dim, &device);
            let optimizer = AdamConfig::new()
                .init::<TB, ActorCriticMlp<TB>>()
                .with_grad_clipping(GradientClipping::Norm(4.0));
            Self {
                network: Some(network),
                optimizer,
                lr,
                vf_coef,
                lr_schedule_steps,
                grad_step_count: 0,
            }
        }

        pub fn effective_lr(&self) -> f64 {
            match self.lr_schedule_steps {
                Some(total) if total > 0 => {
                    let frac = 1.0 - (self.grad_step_count as f64 / total as f64).min(1.0);
                    self.lr * frac.max(0.0)
                }
                _ => self.lr,
            }
        }

        /// Combined pi+vf forward+backward. `act_flat` is i64 action indices. Returns (pi_loss, vf_loss, stats).
        #[allow(clippy::too_many_arguments)]
        pub fn train_step_discrete(
            &mut self,
            obs_flat: &[f32],
            obs_dim: usize,
            act_flat: &[i64],
            adv: &[f32],
            logp_old: &[f32],
            ret: &[f32],
            clip_ratio: f32,
            ent_coef: f32,
            compute_stats: bool,
        ) -> (f32, f32, HashMap<String, f32>) {
            let n = (obs_flat.len() / obs_dim.max(1))
                .min(act_flat.len())
                .min(adv.len())
                .min(logp_old.len())
                .min(ret.len());
            if n == 0 {
                return (0.0, 0.0, zero_pi_info().1);
            }
            let net = match self.network.take() {
                Some(net) => net,
                None => return (0.0, 0.0, zero_pi_info().1),
            };
            let device = <TB as burn_tensor::backend::Backend>::Device::default();

            let obs = Tensor::<TB, 2, Float>::from_data(
                BurnTensorData::new(obs_flat[..n * obs_dim].to_vec(), [n, obs_dim]),
                &device,
            );

            // ── Policy head ──────────────────────────────────────────────
            let logits = net.pi_forward(obs.clone());
            let log_probs_full = log_softmax(logits, 1);
            let act = Tensor::<TB, 2, Int>::from_data(
                BurnTensorData::new(act_flat[..n].to_vec(), [n, 1]),
                &device,
            );
            let logp = log_probs_full.clone().gather(1, act).reshape([n]);
            let adv_tensor = Tensor::<TB, 1, Float>::from_data(
                BurnTensorData::new(adv[..n].to_vec(), [n]),
                &device,
            );
            let logp_old_tensor = Tensor::<TB, 1, Float>::from_data(
                BurnTensorData::new(logp_old[..n].to_vec(), [n]),
                &device,
            );
            let ratio = (logp.clone() - logp_old_tensor).exp();
            let clipped_ratio = ratio.clone().clamp(1.0 - clip_ratio, 1.0 + clip_ratio);
            let clip_obj = (ratio.clone() * adv_tensor.clone())
                .min_pair(clipped_ratio * adv_tensor)
                .mean();
            let entropy_t = (log_probs_full.clone().exp() * log_probs_full)
                .neg()
                .sum_dim(1)
                .reshape([n])
                .mean();
            let pi_loss_t = -(clip_obj + ent_coef * entropy_t.clone());

            // ── Value head ────────────────────────────────────────────────
            let v_pred = net.vf_forward(obs).reshape([n]);
            let ret_tensor = Tensor::<TB, 1, Float>::from_data(
                BurnTensorData::new(ret[..n].to_vec(), [n]),
                &device,
            );
            let vf_loss_t = (v_pred - ret_tensor).powf_scalar(2.0).mean();

            // ── Combined loss → single backward pass ──────────────────────
            let vf_coef_t = self.vf_coef;
            let total_loss = pi_loss_t.clone() + vf_loss_t.clone() * vf_coef_t;

            let pi_loss_val = scalar_from_tensor(&pi_loss_t);
            let vf_loss_val = scalar_from_tensor(&vf_loss_t);

            let grads = total_loss.backward();
            let grads_params = GradientsParams::from_grads(grads, &net);
            let lr = self.effective_lr();
            let net = self.optimizer.step(lr, net, grads_params);
            self.network = Some(net);
            self.grad_step_count += 1;

            if !compute_stats {
                return (pi_loss_val, vf_loss_val, HashMap::new());
            }

            let entropy_val = entropy_t.into_scalar();
            let approx_kl = ((ratio.clone() - 1.0) - ratio.clone().log())
                .mean()
                .into_scalar();
            let ratio_values = ratio
                .into_data()
                .to_vec::<f32>()
                .unwrap_or_else(|_| vec![1.0; n]);
            let clipfrac = ratio_values
                .iter()
                .filter(|r| (**r - 1.0).abs() > clip_ratio)
                .count() as f32
                / n as f32;

            let mut info = HashMap::new();
            info.insert("kl".to_string(), approx_kl);
            info.insert("entropy".to_string(), entropy_val);
            info.insert("clipfrac".to_string(), clipfrac);
            (pi_loss_val, vf_loss_val, info)
        }

        /// Value-only forward (no grad). Used for deferred GAE.
        pub fn value_forward_flat(&self, obs_flat: &[f32], obs_dim: usize) -> Vec<f32> {
            #[allow(clippy::manual_checked_ops)]
            let n = if obs_dim > 0 {
                obs_flat.len() / obs_dim
            } else {
                0
            };
            if n == 0 {
                return Vec::new();
            }
            let net = match self.network.as_ref() {
                Some(net) => net,
                None => return vec![0.0; n],
            };
            let device = <TB as burn_tensor::backend::Backend>::Device::default();
            let obs = Tensor::<TB, 2, Float>::from_data(
                BurnTensorData::new(obs_flat[..n * obs_dim].to_vec(), [n, obs_dim]),
                &device,
            );
            let v = net.vf_forward(obs);
            v.into_data()
                .to_vec::<f32>()
                .unwrap_or_else(|_| vec![0.0; n])
        }

        /// Log-probs for (obs, discrete act). Used for logp_old recompute.
        pub fn logprobs_flat(
            &self,
            obs_flat: &[f32],
            obs_dim: usize,
            act_flat: &[i64],
        ) -> Vec<f32> {
            let n = (obs_flat.len() / obs_dim.max(1)).min(act_flat.len());
            if n == 0 {
                return Vec::new();
            }
            let net = match self.network.as_ref() {
                Some(net) => net,
                None => return vec![0.0; n],
            };
            let device = <TB as burn_tensor::backend::Backend>::Device::default();
            let obs = Tensor::<TB, 2, Float>::from_data(
                BurnTensorData::new(obs_flat[..n * obs_dim].to_vec(), [n, obs_dim]),
                &device,
            );
            let logits = net.pi_forward(obs);
            let log_probs = log_softmax(logits, 1);
            let act = Tensor::<TB, 2, Int>::from_data(
                BurnTensorData::new(act_flat[..n].to_vec(), [n, 1]),
                &device,
            );
            let logp = log_probs.gather(1, act).reshape([n]);
            logp.into_data()
                .to_vec::<f32>()
                .unwrap_or_else(|_| vec![0.0; n])
        }

        pub fn get_pi_layer_specs(&self) -> Option<LayerSpecs> {
            let network = self.network.as_ref()?;
            let mut specs = Vec::new();
            for layer in &network.pi_layers {
                let w = layer.weight.val();
                let dims = w.dims();
                let weights: Vec<f32> = w.into_data().to_vec::<f32>().unwrap_or_default();
                let biases: Vec<f32> = if let Some(bias_param) = &layer.bias {
                    bias_param
                        .val()
                        .into_data()
                        .to_vec::<f32>()
                        .unwrap_or_default()
                } else {
                    vec![0.0; dims[1]]
                };
                specs.push((dims[0], dims[1], weights, biases));
            }
            Some(specs)
        }

        pub fn get_vf_layer_specs(&self) -> Option<LayerSpecs> {
            let network = self.network.as_ref()?;
            let mut specs = Vec::new();
            for layer in &network.vf_layers {
                let w = layer.weight.val();
                let dims = w.dims();
                let weights: Vec<f32> = w.into_data().to_vec::<f32>().unwrap_or_default();
                let biases: Vec<f32> = if let Some(bias_param) = &layer.bias {
                    bias_param
                        .val()
                        .into_data()
                        .to_vec::<f32>()
                        .unwrap_or_default()
                } else {
                    vec![0.0; dims[1]]
                };
                specs.push((dims[0], dims[1], weights, biases));
            }
            Some(specs)
        }
    }

    pub fn zero_pi_info() -> (f32, HashMap<String, f32>) {
        let mut info = HashMap::new();
        info.insert("kl".to_string(), 0.0);
        info.insert("entropy".to_string(), 0.0);
        info.insert("clipfrac".to_string(), 0.0);
        (0.0, info)
    }

    fn scalar_from_tensor(t: &Tensor<TB, 1, Float>) -> f32 {
        t.clone()
            .into_data()
            .to_vec::<f32>()
            .unwrap_or_else(|_| vec![0.0])[0]
    }

    /// Convert a slice of obs TensorData to a flat Vec<f32>.
    pub fn obs_flat_from_tdata(obs: &[TensorData]) -> Result<Vec<f32>, NeuralNetworkError> {
        let mut out = Vec::new();
        for td in obs {
            let vals = convert_byte_dtype_to_f32(td.data.clone(), td.dtype.clone())?;
            out.extend_from_slice(&vals);
        }
        Ok(out)
    }

    /// Convert a slice of act TensorData to i64 action indices.
    pub fn action_indices_from_tdata(act: &[TensorData]) -> Vec<i64> {
        act.iter()
            .map(|td| {
                convert_byte_dtype_to_i64(&td.data, &td.dtype)
                    .ok()
                    .and_then(|v| v.first().copied())
                    .unwrap_or(0)
            })
            .collect()
    }
}

// ---- policy network head definitions ----

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
/// Wraps a policy network as either discrete (categorical) or continuous (Gaussian) for PPO inference.
pub enum PPOPolicyHead<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    Discrete(DiscretePPOPolicyHead<B, KindIn, KindOut, Pi>),
    Continuous(ContinuousPPOPolicyHead<B, KindIn, KindOut, Pi>),
}

#[derive(Clone, Debug)]
/// A policy head that samples discrete actions from a categorical distribution over network logits.
pub struct DiscretePPOPolicyHead<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    pub pi: Pi,
    _phantom: std::marker::PhantomData<(B, KindIn, KindOut)>,
}

impl<B, KindIn, KindOut, Pi> DiscretePPOPolicyHead<B, KindIn, KindOut, Pi>
where
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
{
    /// Wraps a policy network as a discrete policy head.
    pub fn new(pi: Pi) -> Result<Self, NeuralNetworkError> {
        Ok(Self {
            pi,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Runs the policy network forward pass on an observation, producing action logits.
    pub fn forward<const IN_D: usize, const OUT_D: usize>(
        &self,
        obs: Tensor<B, IN_D, KindIn>,
    ) -> Tensor<B, OUT_D, KindOut> {
        self.pi.forward(obs)
    }

    /// Returns the policy network's per-layer weight specs for model export.
    pub fn get_pi_layer_specs(&self) -> LayerSpecs {
        self.pi.get_layer_specs()
    }
}

#[derive(Clone, Debug)]
/// A policy head that samples continuous actions from a Gaussian distribution.
pub struct ContinuousPPOPolicyHead<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    pub pi: Pi,
    _phantom: std::marker::PhantomData<(B, KindIn, KindOut)>,
}

impl<B, KindIn, KindOut, Pi> ContinuousPPOPolicyHead<B, KindIn, KindOut, Pi>
where
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
{
    /// Wraps a policy network as a continuous policy head.
    pub fn new(pi: Pi) -> Result<Self, NeuralNetworkError> {
        Ok(Self {
            pi,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Runs the policy network forward pass on an observation, producing distribution parameters.
    pub fn forward<const IN_D: usize, const OUT_D: usize>(
        &self,
        obs: Tensor<B, IN_D, KindIn>,
    ) -> Tensor<B, OUT_D, KindOut> {
        self.pi.forward(obs)
    }

    /// Returns the policy network's per-layer weight specs for model export.
    pub fn get_pi_layer_specs(&self) -> LayerSpecs {
        self.pi.get_layer_specs()
    }
}

// ---- kernel interfaces ----

/// Policy loss value from a PPO gradient step.
pub type PiLoss = f32;
/// Value function loss from a PPO gradient step.
pub type VfLoss = f32;
/// Diagnostic key-value pairs from a training step.
pub type Info = HashMap<String, f32>;

/// Provides the gradient update step for a PPO policy/value kernel.
pub trait PPOKernelTraining<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
>
{
    #[allow(clippy::too_many_arguments)]
    fn train_step(
        &mut self,
        obs: &[TensorData],
        obs_dim: usize,
        act: &[TensorData],
        adv: &[f32],
        logp_old: &[f32],
        ret: &[f32],
        clip_ratio: f32,
        ent_coef: f32,
        compute_stats: bool,
    ) -> (PiLoss, VfLoss, Info);
}

/// Serialized action bytes produced by a PPO policy forward pass.
pub type ActBytes = Vec<u8>;
/// Serialized log-probability bytes corresponding to sampled actions.
pub type LogpBytes = Vec<u8>;

/// Provides inference-side operations for a PPO kernel: action sampling and log-probability computation.
pub trait PPOKernelOps<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
>
{
    fn policy_forward_bytes(
        &self,
        raw_model_output: &TensorData,
        mask_bytes: Option<&[u8]>,
        n_envs: usize,
        act_dtype: &DType,
    ) -> Result<(ActBytes, LogpBytes), NeuralNetworkError>;
    fn get_pi_logprobs(&self, obs: &[TensorData], obs_dim: usize, act: &[TensorData]) -> Vec<f32>;
    fn value_forward(&self, obs: &[TensorData], obs_dim: usize) -> Vec<f32>;
    fn normalize_persistent_returns(&mut self, ret: &[f32]) -> Vec<f32>;
    fn set_return_denorm_stats(&mut self, mean: f32, std: f32);
}

/// Factory for constructing continuous or discrete PPO kernels.
/// Constructs a `PPOKernel` from separate policy head and value function, validating dimension consistency.
pub struct PPOKernelFactory<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    _phantom: std::marker::PhantomData<(B, KindIn, KindOut, Pi)>,
}

/// Concrete discrete-action PPO kernel: policy head, value function, and optional trainer with return statistics.
pub struct DiscretePPOKernel<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    pub pi: DiscretePPOPolicyHead<B, KindIn, KindOut, Pi>,
    pub vf: ValueFunction<B, KindIn>,
    pub trainer: Option<training::PPOActorCriticTrainer>,
    pub returns_mean: f64,
    pub returns_variance: f64,
    pub returns_count: u64,
    pub ret_denorm_mean: f32,
    pub ret_denorm_std: f32,
}

/// Concrete continuous-action PPO kernel: policy head, value function, and optional trainer with return statistics.
pub struct ContinuousPPOKernel<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    pub pi: ContinuousPPOPolicyHead<B, KindIn, KindOut, Pi>,
    pub vf: ValueFunction<B, KindIn>,
    pub trainer: Option<training::PPOActorCriticTrainer>,
    pub returns_mean: f64,
    pub returns_variance: f64,
    pub returns_count: u64,
    pub ret_denorm_mean: f32,
    pub ret_denorm_std: f32,
}

/// Active PPO kernel holding the policy head and value function along with their training state.
pub enum PPOKernel<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    Discrete(DiscretePPOKernel<B, KindIn, KindOut, Pi>),
    Continuous(ContinuousPPOKernel<B, KindIn, KindOut, Pi>),
}

/// A read-only reference snapshot of a `PPOKernel` used for inference without holding the full kernel.
pub struct PPOKernelSnapshot<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> {
    kernel: Arc<PPOKernel<B, KindIn, KindOut, Pi>>,
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> Clone for PPOKernelSnapshot<B, KindIn, KindOut, Pi>
{
    fn clone(&self) -> Self {
        Self {
            kernel: Arc::clone(&self.kernel),
        }
    }
}

/// Training parameters for the actor-critic kernel.
/// Learning-rate, value-function coefficient, and optional LR schedule for a `PPOKernel`.
pub struct PPOKernelTrainingArgs {
    pub pi_lr: f64,
    pub vf_coef: f32,
    pub lr_schedule_steps: Option<u64>,
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> PPOKernelFactory<B, KindIn, KindOut, Pi>
{
    /// Builds a `PPOKernel` from a policy head, value MLP, and training args, validating matching dimensions.
    #[allow(clippy::new_ret_no_self)]
    pub fn new(
        pi_head: PPOPolicyHead<B, KindIn, KindOut, Pi>,
        vf_mlp: GenericMlp<B, KindIn, Float>,
        training_args: PPOKernelTrainingArgs,
    ) -> Result<PPOKernel<B, KindIn, KindOut, Pi>, NeuralNetworkError> {
        #[inline]
        fn check_input_dim<
            B2: Backend + BackendMatcher<Backend = B2>,
            KindIn2: TensorKind<B2> + BasicOps<B2>,
            KindOut2: TensorKind<B2> + BasicOps<B2>,
            Pi2: NeuralNetwork<B2, KindIn2, KindOut2>,
        >(
            pi_nn: &Pi2,
            vf_nn: &ValueFunction<B2, KindIn2>,
        ) -> Result<(), NeuralNetworkError> {
            if *pi_nn.input_dim() != *<ValueFunction<B2, KindIn2> as NeuralNetworkSpec<B2, KindIn2, KindOut2>>::input_dim(vf_nn) {
                return Err(NeuralNetworkError::InputDimMismatch(
                    *pi_nn.input_dim(),
                    *<ValueFunction<B2, KindIn2> as NeuralNetworkSpec<B2, KindIn2, KindOut2>>::input_dim(vf_nn),
                ));
            }
            Ok(())
        }

        let vf: ValueFunction<B, KindIn> = ValueFunction::new(vf_mlp)?;

        match pi_head {
            PPOPolicyHead::Discrete(discrete_pi) => {
                check_input_dim::<B, KindIn, KindOut, Pi>(&discrete_pi.pi, &vf)?;
                let obs_dim = *discrete_pi.pi.input_dim();
                let act_dim = *discrete_pi.pi.output_dim();
                // Derive hidden sizes from layer specs (all out-dims except last)
                let hidden_sizes: Vec<usize> = discrete_pi
                    .pi
                    .get_layer_specs()
                    .iter()
                    .rev()
                    .skip(1) // skip last layer (hidden → act_dim)
                    .rev()
                    .map(|(_, out, _, _)| *out)
                    .collect();
                let trainer = Some(training::PPOActorCriticTrainer::new(
                    obs_dim,
                    &hidden_sizes,
                    act_dim,
                    training_args.pi_lr,
                    training_args.vf_coef,
                    training_args.lr_schedule_steps,
                ));
                Ok(PPOKernel::<B, KindIn, KindOut, Pi>::Discrete(
                    DiscretePPOKernel {
                        pi: discrete_pi,
                        vf,
                        trainer,
                        returns_mean: 0.0,
                        returns_variance: 1.0,
                        returns_count: 0,
                        ret_denorm_mean: 0.0,
                        ret_denorm_std: 1.0,
                    },
                ))
            }
            PPOPolicyHead::Continuous(continuous_pi) => {
                check_input_dim::<B, KindIn, KindOut, Pi>(&continuous_pi.pi, &vf)?;
                let obs_dim = *continuous_pi.pi.input_dim();
                let act_dim = *continuous_pi.pi.output_dim();
                let hidden_sizes: Vec<usize> = continuous_pi
                    .pi
                    .get_layer_specs()
                    .iter()
                    .rev()
                    .skip(1)
                    .rev()
                    .map(|(_, out, _, _)| *out)
                    .collect();
                let trainer = Some(training::PPOActorCriticTrainer::new(
                    obs_dim,
                    &hidden_sizes,
                    act_dim,
                    training_args.pi_lr,
                    training_args.vf_coef,
                    training_args.lr_schedule_steps,
                ));
                Ok(PPOKernel::<B, KindIn, KindOut, Pi>::Continuous(
                    ContinuousPPOKernel {
                        pi: continuous_pi,
                        vf,
                        trainer,
                        returns_mean: 0.0,
                        returns_variance: 1.0,
                        returns_count: 0,
                        ret_denorm_mean: 0.0,
                        ret_denorm_std: 1.0,
                    },
                ))
            }
        }
    }
}

const MIN_RAYON_PARALLEL_ENVS: usize = 8;

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut> + Clone,
> PPOKernel<B, KindIn, KindOut, Pi>
{
    /// Clones the kernel for inference only, dropping the trainer state.
    pub fn clone_for_inference(&self) -> Self {
        match self {
            PPOKernel::Discrete(kernel) => PPOKernel::Discrete(DiscretePPOKernel {
                pi: kernel.pi.clone(),
                vf: kernel.vf.clone(),
                trainer: None,
                returns_mean: kernel.returns_mean,
                returns_variance: kernel.returns_variance,
                returns_count: kernel.returns_count,
                ret_denorm_mean: kernel.ret_denorm_mean,
                ret_denorm_std: kernel.ret_denorm_std,
            }),
            PPOKernel::Continuous(kernel) => PPOKernel::Continuous(ContinuousPPOKernel {
                pi: kernel.pi.clone(),
                vf: kernel.vf.clone(),
                trainer: None,
                returns_mean: kernel.returns_mean,
                returns_variance: kernel.returns_variance,
                returns_count: kernel.returns_count,
                ret_denorm_mean: kernel.ret_denorm_mean,
                ret_denorm_std: kernel.ret_denorm_std,
            }),
        }
    }

    /// Produces a shareable, inference-only `PPOKernelSnapshot` backed by an `Arc`.
    pub fn to_arc_snapshot(&self) -> PPOKernelSnapshot<B, KindIn, KindOut, Pi> {
        PPOKernelSnapshot {
            kernel: Arc::new(self.clone_for_inference()),
        }
    }
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> PPOKernelSnapshot<B, KindIn, KindOut, Pi>
{
    /// Samples actions from raw policy output bytes, returning serialized action and log-probability bytes for `n_envs`.
    pub fn policy_forward_bytes(
        &self,
        raw_model_output: &TensorData,
        mask_bytes: Option<&[u8]>,
        n_envs: usize,
        act_dtype: &DType,
    ) -> Result<(ActBytes, LogpBytes), NeuralNetworkError> {
        self.kernel
            .policy_forward_bytes(raw_model_output, mask_bytes, n_envs, act_dtype)
    }
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> PPOKernel<B, KindIn, KindOut, Pi>
{
    /// Extract pi layer specs from the trainer (after training); falls back to inference pi.
    pub fn get_pi_layer_specs(&self) -> Option<LayerSpecs> {
        match self {
            PPOKernel::Discrete(kernel) => {
                if let Some(t) = &kernel.trainer
                    && let Some(specs) = t.get_pi_layer_specs()
                {
                    return Some(specs);
                }

                Some(kernel.pi.get_pi_layer_specs())
            }
            PPOKernel::Continuous(kernel) => {
                if let Some(t) = &kernel.trainer
                    && let Some(specs) = t.get_pi_layer_specs()
                {
                    return Some(specs);
                }

                Some(kernel.pi.get_pi_layer_specs())
            }
        }
    }

    /// Extracts value-function layer specs from the trainer (after training); falls back to the inference vf.
    pub fn get_vf_layer_specs(&self) -> Option<LayerSpecs> {
        match self {
            PPOKernel::Discrete(kernel) => {
                if let Some(t) = &kernel.trainer
                    && let Some(specs) = t.get_vf_layer_specs()
                {
                    return Some(specs);
                }

                Some(kernel.vf.get_vf_layer_specs())
            }
            PPOKernel::Continuous(kernel) => {
                if let Some(t) = &kernel.trainer
                    && let Some(specs) = t.get_vf_layer_specs()
                {
                    return Some(specs);
                }

                Some(kernel.vf.get_vf_layer_specs())
            }
        }
    }
}

// ---- action byte encoding helpers ----

fn encode_action_i64_as_dtype(act: i64, dtype: &DType) -> Vec<u8> {
    match dtype {
        DType::NdArray(nd) => match nd {
            NdArrayDType::I8 => (act as i8).to_le_bytes().to_vec(),
            NdArrayDType::I16 => (act as i16).to_le_bytes().to_vec(),
            NdArrayDType::I32 => (act as i32).to_le_bytes().to_vec(),
            NdArrayDType::I64 => act.to_le_bytes().to_vec(),
            NdArrayDType::F16 => half::f16::from_f32(act as f32).to_le_bytes().to_vec(),
            NdArrayDType::F32 => (act as f32).to_le_bytes().to_vec(),
            NdArrayDType::F64 => (act as f64).to_le_bytes().to_vec(),
            NdArrayDType::Bool => vec![if act != 0 { 1u8 } else { 0u8 }],
        },
        #[cfg(feature = "tch-backend")]
        DType::Tch(tch) => match tch {
            TchDType::I8 => (act as i8).to_le_bytes().to_vec(),
            TchDType::I16 => (act as i16).to_le_bytes().to_vec(),
            TchDType::I32 => (act as i32).to_le_bytes().to_vec(),
            TchDType::I64 => act.to_le_bytes().to_vec(),
            TchDType::F16 => half::f16::from_f32(act as f32).to_le_bytes().to_vec(),
            TchDType::Bf16 => half::bf16::from_f32(act as f32).to_le_bytes().to_vec(),
            TchDType::F32 => (act as f32).to_le_bytes().to_vec(),
            TchDType::F64 => (act as f64).to_le_bytes().to_vec(),
            TchDType::U8 => (act as u8).to_le_bytes().to_vec(),
            TchDType::Bool => vec![if act != 0 { 1u8 } else { 0u8 }],
        },
    }
}

fn encode_action_f32_as_dtype(act: f32, dtype: &DType) -> Vec<u8> {
    match dtype {
        DType::NdArray(nd) => match nd {
            NdArrayDType::F16 => half::f16::from_f32(act).to_le_bytes().to_vec(),
            NdArrayDType::F32 => act.to_le_bytes().to_vec(),
            NdArrayDType::F64 => (act as f64).to_le_bytes().to_vec(),
            NdArrayDType::I8 => (act as i8).to_le_bytes().to_vec(),
            NdArrayDType::I16 => (act as i16).to_le_bytes().to_vec(),
            NdArrayDType::I32 => (act as i32).to_le_bytes().to_vec(),
            NdArrayDType::I64 => (act as i64).to_le_bytes().to_vec(),
            NdArrayDType::Bool => vec![if act != 0.0 { 1u8 } else { 0u8 }],
        },
        #[cfg(feature = "tch-backend")]
        DType::Tch(tch) => match tch {
            TchDType::F16 => half::f16::from_f32(act).to_le_bytes().to_vec(),
            TchDType::Bf16 => half::bf16::from_f32(act).to_le_bytes().to_vec(),
            TchDType::F32 => act.to_le_bytes().to_vec(),
            TchDType::F64 => (act as f64).to_le_bytes().to_vec(),
            TchDType::I8 => (act as i8).to_le_bytes().to_vec(),
            TchDType::I16 => (act as i16).to_le_bytes().to_vec(),
            TchDType::I32 => (act as i32).to_le_bytes().to_vec(),
            TchDType::I64 => (act as i64).to_le_bytes().to_vec(),
            TchDType::U8 => (act as u8).to_le_bytes().to_vec(),
            TchDType::Bool => vec![if act != 0.0 { 1u8 } else { 0u8 }],
        },
    }
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> PPOKernelOps<B, KindIn, KindOut, Pi> for PPOKernel<B, KindIn, KindOut, Pi>
{
    fn policy_forward_bytes(
        &self,
        raw_model_output: &TensorData,
        mask_bytes: Option<&[u8]>,
        n_envs: usize,
        act_dtype: &DType,
    ) -> Result<(ActBytes, LogpBytes), NeuralNetworkError> {
        let logits = convert_byte_dtype_to_f32(
            raw_model_output.data.clone(),
            raw_model_output.dtype.clone(),
        )?;

        let act_dim = match self {
            PPOKernel::Discrete(kernel) => *kernel.pi.pi.output_dim(),
            PPOKernel::Continuous(kernel) => *kernel.pi.pi.output_dim(),
        };

        let mut action_bytes = Vec::<u8>::new();
        let mut logp_bytes = Vec::<u8>::with_capacity(n_envs * 4);

        match self {
            PPOKernel::Discrete(_) => {
                let pairs: Vec<(i64, f32)> = if n_envs < MIN_RAYON_PARALLEL_ENVS {
                    (0..n_envs)
                        .map(|i| {
                            DiscretePPOKernel::<B, KindIn, KindOut, Pi>::get_env_byte_action(
                                i, &logits, mask_bytes, act_dim,
                            )
                        })
                        .collect()
                } else {
                    (0..n_envs)
                        .into_par_iter()
                        .map(|i| {
                            DiscretePPOKernel::<B, KindIn, KindOut, Pi>::get_env_byte_action(
                                i, &logits, mask_bytes, act_dim,
                            )
                        })
                        .collect()
                };
                for (act_idx, logp) in &pairs {
                    action_bytes
                        .extend_from_slice(&encode_action_i64_as_dtype(*act_idx, act_dtype));
                    logp_bytes.extend_from_slice(&logp.to_le_bytes());
                }
            }
            PPOKernel::Continuous(_) => {
                let results: Vec<Result<(Vec<f32>, f32), NeuralNetworkError>> =
                    if n_envs < MIN_RAYON_PARALLEL_ENVS {
                        (0..n_envs)
                            .map(|i| {
                                ContinuousPPOKernel::<B, KindIn, KindOut, Pi>::get_env_byte_action(
                                    i, &logits, act_dim,
                                )
                            })
                            .collect()
                    } else {
                        (0..n_envs)
                            .into_par_iter()
                            .map(|i| {
                                ContinuousPPOKernel::<B, KindIn, KindOut, Pi>::get_env_byte_action(
                                    i, &logits, act_dim,
                                )
                            })
                            .collect()
                    };
                for res in results {
                    let (act_vec, logp) = res?;
                    for &a in &act_vec {
                        action_bytes.extend_from_slice(&encode_action_f32_as_dtype(a, act_dtype));
                    }
                    logp_bytes.extend_from_slice(&logp.to_le_bytes());
                }
            }
        }

        Ok((action_bytes, logp_bytes))
    }

    fn get_pi_logprobs(&self, obs: &[TensorData], obs_dim: usize, act: &[TensorData]) -> Vec<f32> {
        {
            let trainer = match self {
                PPOKernel::Discrete(k) => k.trainer.as_ref(),
                PPOKernel::Continuous(k) => k.trainer.as_ref(),
            };
            if let Some(t) = trainer {
                let obs_flat = match training::obs_flat_from_tdata(obs) {
                    Ok(f) => f,
                    Err(_) => return vec![0.0; act.len()],
                };
                let act_flat = training::action_indices_from_tdata(act);
                return t.logprobs_flat(&obs_flat, obs_dim, &act_flat);
            }
        }
        vec![0.0; act.len()]
    }

    fn value_forward(&self, obs: &[TensorData], obs_dim: usize) -> Vec<f32> {
        if obs.is_empty() {
            return Vec::new();
        }

        let (trainer, returns_mean, returns_var, returns_count, ret_denorm_mean, ret_denorm_std) = match self {
            PPOKernel::Discrete(k) => (k.trainer.as_ref(), k.returns_mean, k.returns_variance, k.returns_count, k.ret_denorm_mean, k.ret_denorm_std),
            PPOKernel::Continuous(k) => (k.trainer.as_ref(), k.returns_mean, k.returns_variance, k.returns_count, k.ret_denorm_mean, k.ret_denorm_std),
        };
        if let Some(t) = trainer {
            let obs_flat = match training::obs_flat_from_tdata(obs) {
                Ok(f) => f,
                Err(_) => return vec![0.0; obs.len()],
            };
            let v = t.value_forward_flat(&obs_flat, obs_dim);

            let persistent_std = if returns_count > 1 {
                (returns_var / (returns_count - 1) as f64).sqrt().max(1e-8)
            } else {
                1.0
            };
            let persistent_mean = if returns_count > 0 { returns_mean } else { 0.0 };

            return v.into_iter().map(|v| ((v as f64 * persistent_std + persistent_mean) * ret_denorm_std as f64 + ret_denorm_mean as f64) as f32).collect();
        }
        vec![0.0; obs.len()]
    }

    fn normalize_persistent_returns(&mut self, ret: &[f32]) -> Vec<f32> {
        let (mean, variance, count) = match self {
            PPOKernel::Discrete(k) => (
                &mut k.returns_mean,
                &mut k.returns_variance,
                &mut k.returns_count,
            ),
            PPOKernel::Continuous(k) => (
                &mut k.returns_mean,
                &mut k.returns_variance,
                &mut k.returns_count,
            ),
        };
        for &r in ret {
            *count += 1;
            let r64 = r as f64;
            let delta = r64 - *mean;
            *mean += delta / *count as f64;
            let delta2 = r64 - *mean;
            *variance += delta * delta2;
        }
        let std = if *count > 1 {
            (*variance / (*count - 1) as f64).sqrt().max(1e-8)
        } else {
            1.0
        };
        ret.iter()
            .map(|&r| ((r as f64 - *mean) / std).clamp(-5.0, 5.0) as f32)
            .collect()
    }

    fn set_return_denorm_stats(&mut self, mean: f32, std: f32) {
        match self {
            PPOKernel::Discrete(k) => {
                k.ret_denorm_mean = mean;
                k.ret_denorm_std = std;
            },
            PPOKernel::Continuous(k) => {
                k.ret_denorm_mean = mean;
                k.ret_denorm_std = std;
            },
        }
    }
}

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> PPOKernelTraining<B, KindIn, KindOut, Pi> for PPOKernel<B, KindIn, KindOut, Pi>
{
    fn train_step(
        &mut self,
        obs: &[TensorData],
        obs_dim: usize,
        act: &[TensorData],
        adv: &[f32],
        logp_old: &[f32],
        ret: &[f32],
        clip_ratio: f32,
        ent_coef: f32,
        compute_stats: bool,
    ) -> (PiLoss, VfLoss, Info) {
        {
            match self {
                PPOKernel::Discrete(kernel) => {
                    if let Some(trainer) = kernel.trainer.as_mut() {
                        let obs_flat = match training::obs_flat_from_tdata(obs) {
                            Ok(f) => f,
                            Err(_) => return (0.0, 0.0, HashMap::new()),
                        };
                        let act_flat = training::action_indices_from_tdata(act);
                        return trainer.train_step_discrete(
                            &obs_flat,
                            obs_dim,
                            &act_flat,
                            adv,
                            logp_old,
                            ret,
                            clip_ratio,
                            ent_coef,
                            compute_stats,
                        );
                    }
                }
                PPOKernel::Continuous(_kernel) => {
                    // Continuous training deferred; return zeros
                }
            }
        }
        (0.0, 0.0, HashMap::new())
    }
}

// ---- discrete kernel implementation ----

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> DiscretePPOKernel<B, KindIn, KindOut, Pi>
{
    #[inline(always)]
    pub(super) fn get_env_byte_action(
        env_id: usize,
        logits: &[f32],
        mask_bytes: Option<&[u8]>,
        act_dim: usize,
    ) -> (i64, f32) {
        let mut rng = rand::rng();
        let start = env_id * act_dim;
        let env_logits = &logits[start..start + act_dim];

        let mut masked_logits = env_logits.to_vec();
        if let Some(mask) = mask_bytes {
            for j in 0..act_dim {
                if mask[env_id * act_dim + j] == 0 {
                    masked_logits[j] = f32::NEG_INFINITY
                }
            }
        }

        let max_length = masked_logits
            .iter()
            .cloned()
            .fold(f32::NEG_INFINITY, f32::max);
        let exponentials = masked_logits
            .iter()
            .map(|&x| ((x - max_length) as f64).exp())
            .collect::<Vec<f64>>();
        let exp_sum = exponentials.iter().sum::<f64>();
        let probabilities = exponentials
            .iter()
            .map(|&x| x / exp_sum)
            .collect::<Vec<f64>>();

        let rand_selected_prob = rng.random::<f64>();
        let mut cumulative_prob = 0.0;
        let act_idx = probabilities
            .iter()
            .enumerate()
            .find(|(_, p)| {
                cumulative_prob += *p;
                cumulative_prob >= rand_selected_prob
            })
            .map(|(idx, _)| idx as i64)
            .unwrap_or((act_dim - 1) as i64);

        let log_prob = (probabilities[act_idx as usize] as f32).ln();

        (act_idx, log_prob)
    }
}

// ---- continuous kernel implementation ----

impl<
    B: Backend + BackendMatcher<Backend = B>,
    KindIn: TensorKind<B> + BasicOps<B>,
    KindOut: TensorKind<B> + BasicOps<B>,
    Pi: NeuralNetwork<B, KindIn, KindOut>,
> ContinuousPPOKernel<B, KindIn, KindOut, Pi>
{
    #[inline(always)]
    pub(super) fn get_env_byte_action(
        env_id: usize,
        logits: &[f32],
        act_dim: usize,
    ) -> Result<(Vec<f32>, f32), NeuralNetworkError> {
        use rand_distr::Normal;
        let mut rng = rand::rng();

        let stride = act_dim.saturating_mul(2);
        let start = env_id * stride;
        let env_logits = &logits[start..start + stride];

        let mean = &env_logits[..act_dim];
        let log_std = &env_logits[act_dim..stride];

        let mut act_vec = Vec::<f32>::with_capacity(act_dim);
        let mut total_log_prob = 0.0f32;

        for j in 0..act_dim {
            let std = log_std[j].exp();
            let distribution =
                Normal::new(mean[j], std).map_err(|_| NeuralNetworkError::InvalidDistribution)?;

            let action = distribution.sample(&mut rng);

            total_log_prob += -0.5 * (((action - mean[j]) / std).powi(2))
                - log_std[j]
                - (0.5 * (2.0 * std::f32::consts::PI).ln());

            act_vec.push(action);
        }

        Ok((act_vec, total_log_prob))
    }
}