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 
 
 
 
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
from typing import Any, Tuple, Callable, Optional, List, Dict, Union
from abc import ABC
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical, Independent, Normal
from ding.torch_utils import get_tensor_data, zeros_like
from ding.rl_utils import create_noise_generator
from ding.utils.data import default_collate


class IModelWrapper(ABC):
    """
    Overview:
        The basic interface class of model wrappers. Model wrapper is a wrapper class of torch.nn.Module model, which \
        is used to add some extra operations for the wrapped model, such as hidden state maintain for RNN-base model, \
        argmax action selection for discrete action space, etc.
    Interfaces:
        ``__init__``, ``__getattr__``, ``info``, ``reset``, ``forward``.
    """

    def __init__(self, model: nn.Module) -> None:
        """
        Overview:
            Initialize model and other necessary member variabls in the model wrapper.
        """
        self._model = model

    def __getattr__(self, key: str) -> Any:
        """
        Overview:
            Get original attrbutes of torch.nn.Module model, such as variables and methods defined in model.
        Arguments:
            - key (:obj:`str`): The string key to query.
        Returns:
            - ret (:obj:`Any`): The queried attribute.
        """
        return getattr(self._model, key)

    def info(self, attr_name: str) -> str:
        """
        Overview:
            Get some string information of the indicated ``attr_name``, which is used for debug wrappers.
            This method will recursively search for the indicated ``attr_name``.
        Arguments:
            - attr_name (:obj:`str`): The string key to query information.
        Returns:
            - info_string (:obj:`str`): The information string of the indicated ``attr_name``.
        """
        if attr_name in dir(self):
            if isinstance(self._model, IModelWrapper):
                return '{} {}'.format(self.__class__.__name__, self._model.info(attr_name))
            else:
                if attr_name in dir(self._model):
                    return '{} {}'.format(self.__class__.__name__, self._model.__class__.__name__)
                else:
                    return '{}'.format(self.__class__.__name__)
        else:
            if isinstance(self._model, IModelWrapper):
                return '{}'.format(self._model.info(attr_name))
            else:
                return '{}'.format(self._model.__class__.__name__)

    def reset(self, data_id: List[int] = None, **kwargs) -> None:
        """
        Overview
            Basic interface, reset some stateful varaibles in the model wrapper, such as hidden state of RNN.
            Here we do nothing and just implement this interface method.
            Other derived model wrappers can override this method to add some extra operations.
        Arguments:
            - data_id (:obj:`List[int]`): The data id list to reset. If None, reset all data. In practice, \
                model wrappers often needs to maintain some stateful variables for each data trajectory, \
                so we leave this ``data_id`` argument to reset the stateful variables of the indicated data.
        """
        pass

    def forward(self, *args, **kwargs) -> Any:
        """
        Overview:
            Basic interface, call the wrapped model's forward method. Other derived model wrappers can override this \
            method to add some extra operations.
        """
        return self._model.forward(*args, **kwargs)


class BaseModelWrapper(IModelWrapper):
    """
    Overview:
        Placeholder class for the model wrapper. This class is used to wrap the model without any extra operations, \
        including a empty ``reset`` method and a ``forward`` method which directly call the wrapped model's forward.
        To keep the consistency of the model wrapper interface, we use this class to wrap the model without specific \
        operations in the implementation of DI-engine's policy.
    """
    pass


class HiddenStateWrapper(IModelWrapper):
    """
    Overview:
        Maintain the hidden state for RNN-base model. Each sample in a batch has its own state.
    Interfaces:
        ``__init__``, ``reset``, ``forward``.
    """

    def __init__(
            self,
            model: Any,
            state_num: int,
            save_prev_state: bool = False,
            init_fn: Callable = lambda: None,
    ) -> None:
        """
        Overview:
            Maintain the hidden state for RNN-base model. Each sample in a batch has its own state. \
            Init the maintain state and state function; Then wrap the ``model.forward`` method with auto \
            saved data ['prev_state'] input, and create the ``model.reset`` method.
        Arguments:
            - model(:obj:`Any`): Wrapped model class, should contain forward method.
            - state_num (:obj:`int`): Number of states to process.
            - save_prev_state (:obj:`bool`): Whether to output the prev state in output.
            - init_fn (:obj:`Callable`): The function which is used to init every hidden state when init and reset, \
                default return None for hidden states.

        .. note::
            1. This helper must deal with an actual batch with some parts of samples, e.g: 6 samples of state_num 8.
            2. This helper must deal with the single sample state reset.
        """
        super().__init__(model)
        self._state_num = state_num
        # This is to maintain hidden states (when it comes to this wrapper, \
        # map self._state into data['prev_value] and update next_state, store in self._state)
        self._state = {i: init_fn() for i in range(state_num)}
        self._save_prev_state = save_prev_state
        self._init_fn = init_fn

    def forward(self, data, **kwargs):
        state_id = kwargs.pop('data_id', None)
        valid_id = kwargs.pop('valid_id', None)  # None, not used in any code in DI-engine
        data, state_info = self.before_forward(data, state_id)  # update data['prev_state'] with self._state
        output = self._model.forward(data, **kwargs)
        h = output.pop('next_state', None)
        if h is not None:
            self.after_forward(h, state_info, valid_id)  # this is to store the 'next hidden state' for each time step
        if self._save_prev_state:
            prev_state = get_tensor_data(data['prev_state'])
            # for compatibility, because of the incompatibility between None and torch.Tensor
            for i in range(len(prev_state)):
                if prev_state[i] is None:
                    prev_state[i] = zeros_like(h[0])
            output['prev_state'] = prev_state
        return output

    def reset(self, *args, **kwargs):
        state = kwargs.pop('state', None)
        state_id = kwargs.get('data_id', None)
        self.reset_state(state, state_id)
        if hasattr(self._model, 'reset'):
            return self._model.reset(*args, **kwargs)

    def reset_state(self, state: Optional[list] = None, state_id: Optional[list] = None) -> None:
        if state_id is None:  # train: init all states
            state_id = [i for i in range(self._state_num)]
        if state is None:  # collect: init state that are done
            state = [self._init_fn() for i in range(len(state_id))]
        assert len(state) == len(state_id), '{}/{}'.format(len(state), len(state_id))
        for idx, s in zip(state_id, state):
            self._state[idx] = s

    def before_forward(self, data: dict, state_id: Optional[list]) -> Tuple[dict, dict]:
        if state_id is None:
            state_id = [i for i in range(self._state_num)]

        state_info = {idx: self._state[idx] for idx in state_id}
        data['prev_state'] = list(state_info.values())
        return data, state_info

    def after_forward(self, h: Any, state_info: dict, valid_id: Optional[list] = None) -> None:
        assert len(h) == len(state_info), '{}/{}'.format(len(h), len(state_info))
        for i, idx in enumerate(state_info.keys()):
            if valid_id is None:
                self._state[idx] = h[i]
            else:
                if idx in valid_id:
                    self._state[idx] = h[i]


class TransformerInputWrapper(IModelWrapper):

    def __init__(self, model: Any, seq_len: int, init_fn: Callable = lambda: None) -> None:
        """
        Overview:
            Given N the length of the sequences received by a Transformer model, maintain the last N-1 input
            observations. In this way we can provide at each step all the observations needed by Transformer to
            compute its output. We need this because some methods such as 'collect' and 'evaluate' only provide the
            model 1 observation per step and don't have memory of past observations, but Transformer needs a sequence
            of N observations. The wrapper method ``forward`` will save the input observation in a FIFO memory of
            length N and the method ``reset`` will reset the memory. The empty memory spaces will be initialized
            with 'init_fn' or zero by calling the method ``reset_input``. Since different env can terminate at
            different steps, the method ``reset_memory_entry`` only initializes the memory of specific environments in
            the batch size.
        Arguments:
            - model (:obj:`Any`): Wrapped model class, should contain forward method.
            - seq_len (:obj:`int`): Number of past observations to remember.
            - init_fn (:obj:`Callable`): The function which is used to init every memory locations when init and reset.
        """
        super().__init__(model)
        self.seq_len = seq_len
        self._init_fn = init_fn
        self.obs_memory = None  # shape (N, bs, *obs_shape)
        self.init_obs = None  # sample of observation used to initialize the memory
        self.bs = None
        self.memory_idx = []  # len bs, index of where to put the next element in the sequence for each batch

    def forward(self,
                input_obs: torch.Tensor,
                only_last_logit: bool = True,
                data_id: List = None,
                **kwargs) -> Dict[str, torch.Tensor]:
        """
        Arguments:
            - input_obs (:obj:`torch.Tensor`): Input observation without sequence shape: ``(bs, *obs_shape)``.
            - only_last_logit (:obj:`bool`): if True 'logit' only contains the output corresponding to the current \
                observation (shape: bs, embedding_dim), otherwise logit has shape (seq_len, bs, embedding_dim).
            - data_id (:obj:`List`): id of the envs that are currently running. Memory update and logits return has \
                only effect for those environments. If `None` it is considered that all envs are running.
        Returns:
            - Dictionary containing the input_sequence 'input_seq' stored in memory and the transformer output 'logit'.
        """
        if self.obs_memory is None:
            self.reset_input(torch.zeros_like(input_obs))  # init the memory with the size of the input observation
        if data_id is None:
            data_id = list(range(self.bs))
        assert self.obs_memory.shape[0] == self.seq_len
        # implements a fifo queue, self.memory_idx is index where to put the last element
        for i, b in enumerate(data_id):
            if self.memory_idx[b] == self.seq_len:
                # roll back of 1 position along dim 1 (sequence dim)
                self.obs_memory[:, b] = torch.roll(self.obs_memory[:, b], -1, 0)
                self.obs_memory[self.memory_idx[b] - 1, b] = input_obs[i]
            if self.memory_idx[b] < self.seq_len:
                self.obs_memory[self.memory_idx[b], b] = input_obs[i]
                if self.memory_idx != self.seq_len:
                    self.memory_idx[b] += 1
        out = self._model.forward(self.obs_memory, **kwargs)
        out['input_seq'] = self.obs_memory
        if only_last_logit:
            # return only the logits for running environments
            out['logit'] = [out['logit'][self.memory_idx[b] - 1][b] for b in range(self.bs) if b in data_id]
            out['logit'] = default_collate(out['logit'])
        return out

    def reset_input(self, input_obs: torch.Tensor):
        """
        Overview:
            Initialize the whole memory
        """
        init_obs = torch.zeros_like(input_obs)
        self.init_obs = init_obs
        self.obs_memory = []  # List(bs, *obs_shape)
        for i in range(self.seq_len):
            self.obs_memory.append(init_obs.clone() if init_obs is not None else self._init_fn())
        self.obs_memory = default_collate(self.obs_memory)  # shape (N, bs, *obs_shape)
        self.bs = self.init_obs.shape[0]
        self.memory_idx = [0 for _ in range(self.bs)]

    # called before evaluation
    # called after each evaluation iteration for each done env
    # called after each collect iteration for each done env
    def reset(self, *args, **kwargs):
        state_id = kwargs.get('data_id', None)
        input_obs = kwargs.get('input_obs', None)
        if input_obs is not None:
            self.reset_input(input_obs)
        if state_id is not None:
            self.reset_memory_entry(state_id)
        if input_obs is None and state_id is None:
            self.obs_memory = None
        if hasattr(self._model, 'reset'):
            return self._model.reset(*args, **kwargs)

    def reset_memory_entry(self, state_id: Optional[list] = None) -> None:
        """
        Overview:
            Reset specific batch of the memory, batch ids are specified in 'state_id'
        """
        assert self.init_obs is not None, 'Call method "reset_memory" first'
        for _id in state_id:
            self.memory_idx[_id] = 0
            self.obs_memory[:, _id] = self.init_obs[_id]  # init the corresponding sequence with broadcasting


class TransformerSegmentWrapper(IModelWrapper):

    def __init__(self, model: Any, seq_len: int) -> None:
        """
        Overview:
            Given T the length of a trajectory and N the length of the sequences received by a Transformer model,
            split T in sequences of N elements and forward each sequence one by one. If T % N != 0, the last sequence
            will be zero-padded. Usually used during Transformer training phase.
        Arguments:
            - model (:obj:`Any`): Wrapped model class, should contain forward method.
            - seq_len (:obj:`int`): N, length of a sequence.
        """
        super().__init__(model)
        self.seq_len = seq_len

    def forward(self, obs: torch.Tensor, **kwargs) -> Dict[str, torch.Tensor]:
        """
        Arguments:
            - data (:obj:`dict`): Dict type data, including at least \
                ['main_obs', 'target_obs', 'action', 'reward', 'done', 'weight']
        Returns:
            - List containing a dict of the model output for each sequence.
        """
        sequences = list(torch.split(obs, self.seq_len, dim=0))
        if sequences[-1].shape[0] < self.seq_len:
            last = sequences[-1].clone()
            diff = self.seq_len - last.shape[0]
            sequences[-1] = F.pad(input=last, pad=(0, 0, 0, 0, 0, diff), mode='constant', value=0)
        outputs = []
        for i, seq in enumerate(sequences):
            out = self._model.forward(seq, **kwargs)
            outputs.append(out)
        out = {}
        for k in outputs[0].keys():
            out_k = [o[k] for o in outputs]
            out_k = torch.cat(out_k, dim=0)
            out[k] = out_k
        return out


class TransformerMemoryWrapper(IModelWrapper):

    def __init__(
            self,
            model: Any,
            batch_size: int,
    ) -> None:
        """
        Overview:
            Stores a copy of the Transformer memory in order to be reused across different phases. To make it more
             clear, suppose the training pipeline is divided into 3 phases: evaluate, collect, learn. The goal of the
             wrapper is to maintain the content of the memory at the end of each phase and reuse it when the same phase
             is executed again. In this way, it prevents different phases to interferer each other memory.
        Arguments:
            - model (:obj:`Any`): Wrapped model class, should contain forward method.
            - batch_size (:obj:`int`): Memory batch size.
        """
        super().__init__(model)
        # shape (layer_num, memory_len, bs, embedding_dim)
        self._model.reset_memory(batch_size=batch_size)
        self.memory = self._model.get_memory()
        self.mem_shape = self.memory.shape

    def forward(self, *args, **kwargs) -> Dict[str, torch.Tensor]:
        """
        Arguments:
            - data (:obj:`dict`): Dict type data, including at least \
                ['main_obs', 'target_obs', 'action', 'reward', 'done', 'weight']
        Returns:
            - Output of the forward method.
        """
        self._model.reset_memory(state=self.memory)
        out = self._model.forward(*args, **kwargs)
        self.memory = self._model.get_memory()
        return out

    def reset(self, *args, **kwargs):
        state_id = kwargs.get('data_id', None)
        if state_id is None:
            self.memory = torch.zeros(self.mem_shape)
        else:
            self.reset_memory_entry(state_id)
        if hasattr(self._model, 'reset'):
            return self._model.reset(*args, **kwargs)

    def reset_memory_entry(self, state_id: Optional[list] = None) -> None:
        """
        Overview:
            Reset specific batch of the memory, batch ids are specified in 'state_id'
        """
        for _id in state_id:
            self.memory[:, :, _id] = torch.zeros((self.mem_shape[-1]))

    def show_memory_occupancy(self, layer=0) -> None:
        memory = self.memory
        memory_shape = memory.shape
        print('Layer {}-------------------------------------------'.format(layer))
        for b in range(memory_shape[-2]):
            print('b{}: '.format(b), end='')
            for m in range(memory_shape[1]):
                if sum(abs(memory[layer][m][b].flatten())) != 0:
                    print(1, end='')
                else:
                    print(0, end='')
            print()


def sample_action(logit=None, prob=None):
    if prob is None:
        prob = torch.softmax(logit, dim=-1)
    shape = prob.shape
    prob += 1e-8
    prob = prob.view(-1, shape[-1])
    # prob can also be treated as weight in multinomial sample
    action = torch.multinomial(prob, 1).squeeze(-1)
    action = action.view(*shape[:-1])
    return action


class ArgmaxSampleWrapper(IModelWrapper):
    """
    Overview:
        Used to help the model to sample argmax action.
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        """
        Overview:
            Employ model forward computation graph, and use the output logit to greedily select max action (argmax).
        """
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        action = [l.argmax(dim=-1) for l in logit]
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output['action'] = action
        return output


class CombinationArgmaxSampleWrapper(IModelWrapper):
    r"""
    Overview:
        Used to help the model to sample combination argmax action.
    Interfaces:
        ``forward``.
    """

    def forward(self, shot_number, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        # Generate actions.
        act = []
        mask = torch.zeros_like(output['logit'])
        for ii in range(shot_number):
            masked_logit = output['logit'] + mask
            actions = masked_logit.argmax(dim=-1)
            act.append(actions)
            for jj in range(actions.shape[0]):
                mask[jj][actions[jj]] = -1e8
        # `act` is shaped: (B, shot_number)
        act = torch.stack(act, dim=1)
        output['action'] = act
        return output


class CombinationMultinomialSampleWrapper(IModelWrapper):
    r"""
    Overview:
        Used to help the model to sample combination multinomial action.
    Interfaces:
        ``forward``.
    """

    def forward(self, shot_number, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        # Generate actions.
        act = []
        mask = torch.zeros_like(output['logit'])
        for ii in range(shot_number):
            dist = torch.distributions.Categorical(logits=output['logit'] + mask)
            actions = dist.sample()
            act.append(actions)
            for jj in range(actions.shape[0]):
                mask[jj][actions[jj]] = -1e8

        # `act` is shaped: (B, shot_number)
        act = torch.stack(act, dim=1)
        output['action'] = act
        return output


class HybridArgmaxSampleWrapper(IModelWrapper):
    r"""
    Overview:
        Used to help the model to sample argmax action in hybrid action space,
        i.e.{'action_type': discrete, 'action_args', continuous}
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        if 'logit' not in output:
            return output
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        action = [l.argmax(dim=-1) for l in logit]
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output = {'action': {'action_type': action, 'action_args': output['action_args']}, 'logit': logit}
        return output


class MultinomialSampleWrapper(IModelWrapper):
    """
    Overview:
        Used to help the model get the corresponding action from the output['logits']self.
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        if 'alpha' in kwargs.keys():
            alpha = kwargs.pop('alpha')
        else:
            alpha = None
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        if alpha is None:
            action = [sample_action(logit=l) for l in logit]
        else:
            # Note that if alpha is passed in here, we will divide logit by alpha.
            action = [sample_action(logit=l / alpha) for l in logit]
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output['action'] = action
        return output


class EpsGreedySampleWrapper(IModelWrapper):
    r"""
    Overview:
        Epsilon greedy sampler used in collector_model to help balance exploratin and exploitation.
        The type of eps can vary from different algorithms, such as:
        - float (i.e. python native scalar): for almost normal case
        - Dict[str, float]: for algorithm NGU
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        eps = kwargs.pop('eps')
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        else:
            mask = None
        action = []
        if isinstance(eps, dict):
            # for NGU policy, eps is a dict, each collect env has a different eps
            for i, l in enumerate(logit[0]):
                eps_tmp = eps[i]
                if np.random.random() > eps_tmp:
                    action.append(l.argmax(dim=-1))
                else:
                    if mask is not None:
                        action.append(
                            sample_action(prob=mask[0][i].float().unsqueeze(0)).to(logit[0].device).squeeze(0)
                        )
                    else:
                        action.append(torch.randint(0, l.shape[-1], size=l.shape[:-1]).to(logit[0].device))
            action = torch.stack(action, dim=-1)  # shape torch.size([env_num])
        else:
            for i, l in enumerate(logit):
                if np.random.random() > eps:
                    action.append(l.argmax(dim=-1))
                else:
                    if mask is not None:
                        action.append(sample_action(prob=mask[i].float()))
                    else:
                        action.append(torch.randint(0, l.shape[-1], size=l.shape[:-1]))
            if len(action) == 1:
                action, logit = action[0], logit[0]
        output['action'] = action
        return output


class EpsGreedyMultinomialSampleWrapper(IModelWrapper):
    r"""
    Overview:
        Epsilon greedy sampler coupled with multinomial sample used in collector_model
        to help balance exploration and exploitation.
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        eps = kwargs.pop('eps')
        if 'alpha' in kwargs.keys():
            alpha = kwargs.pop('alpha')
        else:
            alpha = None
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        else:
            mask = None
        action = []
        for i, l in enumerate(logit):
            if np.random.random() > eps:
                if alpha is None:
                    action = [sample_action(logit=l) for l in logit]
                else:
                    # Note that if alpha is passed in here, we will divide logit by alpha.
                    action = [sample_action(logit=l / alpha) for l in logit]
            else:
                if mask:
                    action.append(sample_action(prob=mask[i].float()))
                else:
                    action.append(torch.randint(0, l.shape[-1], size=l.shape[:-1]))
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output['action'] = action
        return output


class HybridEpsGreedySampleWrapper(IModelWrapper):
    r"""
    Overview:
        Epsilon greedy sampler used in collector_model to help balance exploration and exploitation.
        In hybrid action space, i.e.{'action_type': discrete, 'action_args', continuous}
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        eps = kwargs.pop('eps')
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        else:
            mask = None
        action = []
        for i, l in enumerate(logit):
            if np.random.random() > eps:
                action.append(l.argmax(dim=-1))
            else:
                if mask:
                    action.append(sample_action(prob=mask[i].float()))
                else:
                    action.append(torch.randint(0, l.shape[-1], size=l.shape[:-1]))
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output = {'action': {'action_type': action, 'action_args': output['action_args']}, 'logit': logit}
        return output


class HybridEpsGreedyMultinomialSampleWrapper(IModelWrapper):
    """
    Overview:
        Epsilon greedy sampler coupled with multinomial sample used in collector_model
        to help balance exploration and exploitation.
        In hybrid action space, i.e.{'action_type': discrete, 'action_args', continuous}
    Interfaces:
        ``forward``.
    """

    def forward(self, *args, **kwargs):
        eps = kwargs.pop('eps')
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        if 'logit' not in output:
            return output

        logit = output['logit']
        assert isinstance(logit, torch.Tensor) or isinstance(logit, list)
        if isinstance(logit, torch.Tensor):
            logit = [logit]
        if 'action_mask' in output:
            mask = output['action_mask']
            if isinstance(mask, torch.Tensor):
                mask = [mask]
            logit = [l.sub_(1e8 * (1 - m)) for l, m in zip(logit, mask)]
        else:
            mask = None
        action = []
        for i, l in enumerate(logit):
            if np.random.random() > eps:
                action = [sample_action(logit=l) for l in logit]
            else:
                if mask:
                    action.append(sample_action(prob=mask[i].float()))
                else:
                    action.append(torch.randint(0, l.shape[-1], size=l.shape[:-1]))
        if len(action) == 1:
            action, logit = action[0], logit[0]
        output = {'action': {'action_type': action, 'action_args': output['action_args']}, 'logit': logit}
        return output


class HybridReparamMultinomialSampleWrapper(IModelWrapper):
    """
    Overview:
        Reparameterization sampler coupled with multinomial sample used in collector_model
        to help balance exploration and exploitation.
        In hybrid action space, i.e.{'action_type': discrete, 'action_args', continuous}
    Interfaces:
        forward
    """

    def forward(self, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))

        logit = output['logit']  # logit: {'action_type': action_type_logit, 'action_args': action_args_logit}
        # discrete part
        action_type_logit = logit['action_type']
        prob = torch.softmax(action_type_logit, dim=-1)
        pi_action = Categorical(prob)
        action_type = pi_action.sample()
        # continuous part
        mu, sigma = logit['action_args']['mu'], logit['action_args']['sigma']
        dist = Independent(Normal(mu, sigma), 1)
        action_args = dist.sample()
        action = {'action_type': action_type, 'action_args': action_args}
        output['action'] = action
        return output


class HybridDeterministicArgmaxSampleWrapper(IModelWrapper):
    """
    Overview:
        Deterministic sampler coupled with argmax sample used in eval_model.
        In hybrid action space, i.e.{'action_type': discrete, 'action_args', continuous}
    Interfaces:
        forward
    """

    def forward(self, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        logit = output['logit']  # logit: {'action_type': action_type_logit, 'action_args': action_args_logit}
        # discrete part
        action_type_logit = logit['action_type']
        action_type = action_type_logit.argmax(dim=-1)
        # continuous part
        mu = logit['action_args']['mu']
        action_args = mu
        action = {'action_type': action_type, 'action_args': action_args}
        output['action'] = action
        return output


class DeterministicSampleWrapper(IModelWrapper):
    """
    Overview:
        Deterministic sampler (just use mu directly) used in eval_model.
    Interfaces:
        forward
    """

    def forward(self, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        output['action'] = output['logit']['mu']
        return output


class ReparamSampleWrapper(IModelWrapper):
    """
    Overview:
        Reparameterization gaussian sampler used in collector_model.
    Interfaces:
        forward
    """

    def forward(self, *args, **kwargs):
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        mu, sigma = output['logit']['mu'], output['logit']['sigma']
        dist = Independent(Normal(mu, sigma), 1)
        output['action'] = dist.sample()
        return output


class ActionNoiseWrapper(IModelWrapper):
    r"""
    Overview:
        Add noise to collector's action output; Do clips on both generated noise and action after adding noise.
    Interfaces:
        ``__init__``, ``forward``.
    Arguments:
        - model (:obj:`Any`): Wrapped model class. Should contain ``forward`` method.
        - noise_type (:obj:`str`): The type of noise that should be generated, support ['gauss', 'ou'].
        - noise_kwargs (:obj:`dict`): Keyword args that should be used in noise init. Depends on ``noise_type``.
        - noise_range (:obj:`Optional[dict]`): Range of noise, used for clipping.
        - action_range (:obj:`Optional[dict]`): Range of action + noise, used for clip, default clip to [-1, 1].
    """

    def __init__(
            self,
            model: Any,
            noise_type: str = 'gauss',
            noise_kwargs: dict = {},
            noise_range: Optional[dict] = None,
            action_range: Optional[dict] = {
                'min': -1,
                'max': 1
            }
    ) -> None:
        super().__init__(model)
        self.noise_generator = create_noise_generator(noise_type, noise_kwargs)
        self.noise_range = noise_range
        self.action_range = action_range

    def forward(self, *args, **kwargs):
        # if noise sigma need decay, update noise kwargs.
        if 'sigma' in kwargs:
            sigma = kwargs.pop('sigma')
            if sigma is not None:
                self.noise_generator.sigma = sigma
        output = self._model.forward(*args, **kwargs)
        assert isinstance(output, dict), "model output must be dict, but find {}".format(type(output))
        if 'action' in output or 'action_args' in output:
            key = 'action' if 'action' in output else 'action_args'
            action = output[key]
            assert isinstance(action, torch.Tensor)
            action = self.add_noise(action)
            output[key] = action
        return output

    def add_noise(self, action: torch.Tensor) -> torch.Tensor:
        r"""
        Overview:
            Generate noise and clip noise if needed. Add noise to action and clip action if needed.
        Arguments:
            - action (:obj:`torch.Tensor`): Model's action output.
        Returns:
            - noised_action (:obj:`torch.Tensor`): Action processed after adding noise and clipping.
        """
        noise = self.noise_generator(action.shape, action.device)
        if self.noise_range is not None:
            noise = noise.clamp(self.noise_range['min'], self.noise_range['max'])
        action += noise
        if self.action_range is not None:
            action = action.clamp(self.action_range['min'], self.action_range['max'])
        return action


class TargetNetworkWrapper(IModelWrapper):
    r"""
    Overview:
        Maintain and update the target network
    Interfaces:
        update, reset
    """

    def __init__(self, model: Any, update_type: str, update_kwargs: dict):
        super().__init__(model)
        assert update_type in ['momentum', 'assign']
        self._update_type = update_type
        self._update_kwargs = update_kwargs
        self._update_count = 0

    def reset(self, *args, **kwargs):
        target_update_count = kwargs.pop('target_update_count', None)
        self.reset_state(target_update_count)
        if hasattr(self._model, 'reset'):
            return self._model.reset(*args, **kwargs)

    def update(self, state_dict: dict, direct: bool = False) -> None:
        r"""
        Overview:
            Update the target network state dict

        Arguments:
            - state_dict (:obj:`dict`): the state_dict from learner model
            - direct (:obj:`bool`): whether to update the target network directly, \
                if true then will simply call the load_state_dict method of the model
        """
        if direct:
            self._model.load_state_dict(state_dict, strict=True)
            self._update_count = 0
        else:
            if self._update_type == 'assign':
                if (self._update_count + 1) % self._update_kwargs['freq'] == 0:
                    self._model.load_state_dict(state_dict, strict=True)
                self._update_count += 1
            elif self._update_type == 'momentum':
                theta = self._update_kwargs['theta']
                for name, p in self._model.named_parameters():
                    # default theta = 0.001
                    p.data = (1 - theta) * p.data + theta * state_dict[name]

    def reset_state(self, target_update_count: int = None) -> None:
        r"""
        Overview:
            Reset the update_count
        Arguments:
            target_update_count (:obj:`int`): reset target update count value.
        """
        if target_update_count is not None:
            self._update_count = target_update_count


class TeacherNetworkWrapper(IModelWrapper):
    """
    Overview:
        Set the teacher Network. Set the model's model.teacher_cfg to the input teacher_cfg
    """

    def __init__(self, model, teacher_cfg):
        super().__init__(model)
        self._model._teacher_cfg = teacher_cfg
        raise NotImplementedError


wrapper_name_map = {
    'base': BaseModelWrapper,
    'hidden_state': HiddenStateWrapper,
    'argmax_sample': ArgmaxSampleWrapper,
    'hybrid_argmax_sample': HybridArgmaxSampleWrapper,
    'eps_greedy_sample': EpsGreedySampleWrapper,
    'eps_greedy_multinomial_sample': EpsGreedyMultinomialSampleWrapper,
    'deterministic_sample': DeterministicSampleWrapper,
    'reparam_sample': ReparamSampleWrapper,
    'hybrid_eps_greedy_sample': HybridEpsGreedySampleWrapper,
    'hybrid_eps_greedy_multinomial_sample': HybridEpsGreedyMultinomialSampleWrapper,
    'hybrid_reparam_multinomial_sample': HybridReparamMultinomialSampleWrapper,
    'hybrid_deterministic_argmax_sample': HybridDeterministicArgmaxSampleWrapper,
    'multinomial_sample': MultinomialSampleWrapper,
    'action_noise': ActionNoiseWrapper,
    'transformer_input': TransformerInputWrapper,
    'transformer_segment': TransformerSegmentWrapper,
    'transformer_memory': TransformerMemoryWrapper,
    # model wrapper
    'target': TargetNetworkWrapper,
    'teacher': TeacherNetworkWrapper,
    'combination_argmax_sample': CombinationArgmaxSampleWrapper,
    'combination_multinomial_sample': CombinationMultinomialSampleWrapper,
}


def model_wrap(model: Union[nn.Module, IModelWrapper], wrapper_name: str = None, **kwargs):
    """
    Overview:
        Wrap the model with the specified wrapper and return the wrappered model.
    Arguments:
        - model (:obj:`Any`): The model to be wrapped.
        - wrapper_name (:obj:`str`): The name of the wrapper to be used.

    .. note::
        The arguments of the wrapper should be passed in as kwargs.
    """
    if wrapper_name in wrapper_name_map:
        # TODO test whether to remove this if branch
        if not isinstance(model, IModelWrapper):
            model = wrapper_name_map['base'](model)
        model = wrapper_name_map[wrapper_name](model, **kwargs)
    else:
        raise TypeError("not support model_wrapper type: {}".format(wrapper_name))
    return model


def register_wrapper(name: str, wrapper_type: type) -> None:
    """
    Overview:
        Register new wrapper to ``wrapper_name_map``. When user implements a new wrapper, they must call this function \
        to complete the registration. Then the wrapper can be called by ``model_wrap``.
    Arguments:
        - name (:obj:`str`): The name of the new wrapper to be registered.
        - wrapper_type (:obj:`type`): The wrapper class needs to be added in ``wrapper_name_map``. This argument \
            should be the subclass of ``IModelWrapper``.
    """
    assert isinstance(name, str)
    assert issubclass(wrapper_type, IModelWrapper)
    wrapper_name_map[name] = wrapper_type