Nekochu commited on
Commit
0aed5d8
路
verified 路
1 Parent(s): 752dc7d

Document dflow-identical seeds

Browse files
Files changed (2) hide show
  1. README.md +4 -0
  2. scripts/torch_randn.py +55 -0
README.md CHANGED
@@ -65,6 +65,10 @@ shift `s = min(0, -0.25 - ln(W路H / 256虏))`, self-attention gain -0.03 up to ab
65
  above), as the LoRA was trained. The JS schedule reproduces the LoRA's recorded grids exactly
66
  (`[1.0, 0.920527, 0.816588, 0.631176, 0.0]` at 832脳1216, `[1.0, 0.940256, 0.858143, 0.699269, 0.0]` at 1824脳1024).
67
 
 
 
 
 
68
  The model and the LoRA were trained at about 1 MP and above. Detail scales with the token count
69
  (one token per 16脳16 pixels), so 512脳512 is a quick preview, not a fair look at the model.
70
 
 
65
  above), as the LoRA was trained. The JS schedule reproduces the LoRA's recorded grids exactly
66
  (`[1.0, 0.920527, 0.816588, 0.631176, 0.0]` at 832脳1216, `[1.0, 0.940256, 0.858143, 0.699269, 0.0]` at 1824脳1024).
67
 
68
+ The page draws its starting noise exactly as dflow does (`torch.randn` on a CPU generator: mt19937,
69
+ then Box-Muller in blocks of 16; the JS port matches PyTorch to about 2e-6), so a seed gives the same
70
+ picture as in dflow or ComfyUI. Seed 4242 at 1824脳1024 with a card prompt reproduces that LoRA card image.
71
+
72
  The model and the LoRA were trained at about 1 MP and above. Detail scales with the token count
73
  (one token per 16脳16 pixels), so 512脳512 is a quick preview, not a fair look at the model.
74
 
scripts/torch_randn.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Reimplementation of torch.randn(..., generator=torch.Generator('cpu').manual_seed(seed)) for float32.
2
+
3
+ mt19937 (32-bit outputs) -> uniform floats u = (r & 0xFFFFFF) / 2^24 for every element, then
4
+ Box-Muller over blocks of 16 (first 8 values pair with the next 8); if the size is not a
5
+ multiple of 16 the last 16 values are redrawn and transformed again (ATen normal_fill).
6
+ """
7
+ import numpy as np
8
+ import torch
9
+
10
+
11
+ class MT19937:
12
+ def __init__(self, seed):
13
+ self.mt = [0] * 624
14
+ self.mt[0] = seed & 0xFFFFFFFF
15
+ for i in range(1, 624):
16
+ self.mt[i] = (1812433253 * (self.mt[i - 1] ^ (self.mt[i - 1] >> 30)) + i) & 0xFFFFFFFF
17
+ self.idx = 624
18
+
19
+ def next(self):
20
+ if self.idx >= 624:
21
+ mt = self.mt
22
+ for i in range(624):
23
+ y = (mt[i] & 0x80000000) | (mt[(i + 1) % 624] & 0x7FFFFFFF)
24
+ mt[i] = mt[(i + 397) % 624] ^ (y >> 1) ^ (0x9908B0DF if y & 1 else 0)
25
+ self.idx = 0
26
+ y = self.mt[self.idx]; self.idx += 1
27
+ y ^= y >> 11; y ^= (y << 7) & 0x9D2C5680; y ^= (y << 15) & 0xEFC60000; y ^= y >> 18
28
+ return y & 0xFFFFFFFF
29
+
30
+
31
+ def randn(n, seed):
32
+ g = MT19937(seed)
33
+ u = lambda: np.float32((g.next() & 0xFFFFFF) / 16777216.0)
34
+ d = np.array([u() for _ in range(n)], dtype=np.float32)
35
+
36
+ def fill16(o):
37
+ a, b = d[o:o + 8].copy(), d[o + 8:o + 16].copy()
38
+ r = np.sqrt(np.float32(-2.0) * np.log(np.float32(1.0) - a)).astype(np.float32)
39
+ th = (np.float32(2.0 * np.pi) * b).astype(np.float32)
40
+ d[o:o + 8] = r * np.cos(th); d[o + 8:o + 16] = r * np.sin(th)
41
+
42
+ for o in range(0, n - 15, 16):
43
+ fill16(o)
44
+ if n % 16:
45
+ o = n - 16
46
+ d[o:] = [u() for _ in range(16)]
47
+ fill16(o)
48
+ return d
49
+
50
+
51
+ if __name__ == "__main__":
52
+ for shape, seed in [((1, 96, 8, 8), 11), ((1, 96, 32, 32), 7), ((1, 96, 3, 5), 1), ((1, 96, 64, 114), 4242)]:
53
+ t = torch.randn(shape, generator=torch.Generator("cpu").manual_seed(seed), dtype=torch.float32).numpy().ravel()
54
+ m = randn(t.size, seed)
55
+ print(shape, seed, "max abs diff", float(np.abs(m - t).max()), "exact-equal fraction", float((m == t).mean()))