test_replicated_split.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """CPU check for adapter/replicated_split.py and adapter/draft_main_proj.py: tile ranges, the weight
  2. and scale slices a proxy layer gets, the Mxfp8SwizzledInput row count, and the draft main_proj fp8
  3. shard being an exact re-encoding of on-grid weights. The bit-identity gate itself runs at boot."""
  4. import collections
  5. import os
  6. os.environ["DSV41_REPLICATED_SPLIT"] = "wqkv_a,engram.wkv"
  7. os.environ["DSV41_DRAFT_MAIN_PROJ_SPLIT"] = "1"
  8. import torch # noqa: E402
  9. import draft_main_proj as dmp # noqa: E402
  10. import replicated_split as rs # noqa: E402
  11. class Toy(torch.nn.Module):
  12. def __init__(self, n, k):
  13. super().__init__()
  14. self.weight = torch.nn.Parameter(torch.arange(n * k).view(n, k).float().to(torch.float8_e4m3fn),
  15. requires_grad=False)
  16. self.weight_scale_inv_swizzled = torch.nn.Parameter(torch.arange(n * k // 32, dtype=torch.int32),
  17. requires_grad=False)
  18. self.output_size = n
  19. def main():
  20. assert rs._ranges(1792, 4) == ([(0, 512), (512, 1024), (1024, 1408), (1408, 1792)], 512)
  21. assert rs._ranges(25600, 4) == ([(0, 6400), (6400, 12800), (12800, 19200), (19200, 25600)], 6400)
  22. toy = Toy(512, 64)
  23. assert rs._scale_params(toy) == [("weight_scale_inv_swizzled", "flat")]
  24. p = rs._proxy(toy, 128, 384)
  25. assert torch.equal(p.weight.float(), toy.weight.float()[128:384])
  26. assert torch.equal(p.weight_scale_inv_swizzled, toy.weight_scale_inv_swizzled[128 * 2:384 * 2])
  27. assert p.output_size == 256 and toy.output_size == 512 and toy.weight.shape[0] == 512
  28. Mx = collections.namedtuple("Mxfp8SwizzledInput", "data scales")
  29. assert rs._rows(Mx(torch.zeros(6, 64), torch.zeros(1))) == 6 and rs._rows(torch.zeros(3, 8)) == 3
  30. assert rs._rows(torch.zeros(2, 3, 4)) == -1
  31. # draft_main_proj: fp8 values times a power of two per (row, 32 columns) re-encode exactly
  32. q = (torch.randn(64, 256) * 40).to(torch.float8_e4m3fn).float()
  33. e = torch.randint(-12, -2, (64, 8)).float()
  34. w = (q.view(64, 8, 32) * torch.exp2(e)[..., None]).view(64, 256).to(torch.bfloat16)
  35. w8, s = dmp.quant_rowscale(w)
  36. back = (w8.float().view(64, 8, 32) * torch.exp2(s.float() - 127)[..., None]).view(64, 256)
  37. assert torch.equal(back.to(torch.bfloat16), w)
  38. print("test_replicated_split: ok")
  39. if __name__ == "__main__":
  40. main()