2 ######################################################################
4 2024 Jan 07 21:37:48 (from mygpt.py)
7 # This is one order of magnitude more complicated than I expected, not
8 # elegant, slow, hopefully not buggy
11 def flash_back_time_src(N, H, t0, t1, CL, CH, proba, device):
12 # starting flash backs
13 fb_start = (torch.rand(N, CH, t1 - t0, device=device) <= proba).long()
14 fb_start[:, :, -CL:] = 0
15 fb_start[:, :, :CL] = 0
17 # Remove series longer than CL
18 fb_body = fb_start.clone()
19 fb_body[:, :, CL + 1 :] -= fb_start[:, :, : -(CL + 1)]
20 fb_body = fb_body.cumsum(dim=2)
21 fb_start = fb_start * (fb_body == 1)
23 # Set a origin source time (starting time of the chunck to copy
24 # here) We set it as the current time minus a multiple of CL to be
25 # consistent with the "rolling" caterpillar
26 t = torch.arange(fb_start.size(2), device=fb_start.device)[None, None, :]
27 src_time = fb_start * (
33 torch.rand(fb_start.size(), device=fb_start.device) * (t // CL - 1)
37 src_time[:, :, CL:] -= src_time.clone()[:, :, :-CL]
38 src_time = src_time.cumsum(dim=2)
40 src_head = fb_start * torch.randint(H, fb_start.size(), device=fb_start.device)
41 src_head[:, :, CL:] -= src_head.clone()[:, :, :-CL]
42 src_head = src_head.cumsum(dim=2)
45 src_delta = fb_start.clone()
46 src_delta[:, :, CL:] -= fb_start[:, :, :-CL]
47 src_delta = src_delta.cumsum(dim=2)
48 src_delta[:, :, CL:] -= CL * fb_start[:, :, :-CL]
49 src_time += src_delta.cumsum(dim=2) - 1
51 return src_time, src_head
54 def insert_flash_back(rec_V, V, rec_K, K, t0, t1, CL, proba):
55 N, H, CH = V.size(0), V.size(1), rec_V.size(1)
57 fbt, fbh = flash_back_time_src(N, H, t0, t1, CL, CH, proba, rec_V.device)
59 fbt_V = fbt[:, :, :, None]
60 fbh_V = fbh[:, :, :, None]
61 t = fbt_V.clamp(min=0)
62 n = torch.arange(V.size(0), device=V.device)[:, None, None, None]
63 d = torch.arange(V.size(3), device=V.device)[None, None, None, :]
64 q = V[:, :, t0:t1][n, fbh_V, t, d]
65 rec_V[:, :, t0:t1] = q * (fbt_V >= 0) + rec_V[:, :, t0:t1] * (fbt_V < 0)
67 fbt_K = fbt[:, :, :, None]
68 fbh_K = fbh[:, :, :, None]
69 t = fbt_K.clamp(min=0)
70 n = torch.arange(K.size(0), device=K.device)[:, None, None, None]
71 d = torch.arange(K.size(3), device=K.device)[None, None, None, :]
72 q = K[:, :, t0:t1][n, fbh_K, t, d]
73 rec_K[:, :, t0:t1] = q * (fbt_K >= 0) + rec_K[:, :, t0:t1] * (fbt_K < 0)
76 ######################################################################
78 ######################################################################
80 2024 Jan 07 21:38:11 (from mygpt.py)
82 # insert_flash_back(self.rec_V,V,self.rec_K,K,t0,t1,CL,proba=self.proba_flashback / CL,)