MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / TestLLMInt8Functional

Class TestLLMInt8Functional

tests/test_functional.py:350–572  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

348
349
350class TestLLMInt8Functional:
351 @staticmethod
352 def vectorwise_mm_dequant(xq, S1, S2, dtype=torch.half):
353 """Reference implementation for the F.int8_mm_dequant function."""
354 C = 127.0
355
356 x = xq.float()
357 if len(S1.shape) == 3 and len(x.shape) == 2:
358 S1 = S1.squeeze(0)
359 if len(S2.shape) == 3 and len(x.shape) == 2:
360 S2 = S2.squeeze(0)
361 if len(S1.shape) == 2:
362 x *= S1 / C
363 else:
364 x *= S1 / C
365 x *= S2 / C
366 return x.to(dtype)
367
368 @staticmethod
369 def vectorwise_quant(x, dim=1):
370 """Reference implementation"""
371 max1 = torch.amax(torch.abs(x), dim=dim, keepdim=True)
372 xq = torch.round(x * (127.0 / max1)).to(torch.int8)
373 return xq, max1
374
375 @pytest.mark.parametrize("device", get_available_devices())
376 @pytest.mark.parametrize("dim1", [128], ids=id_formatter("dim1"))
377 @pytest.mark.parametrize("dim2", [256], ids=id_formatter("dim2"))
378 @pytest.mark.parametrize("dim3", [499, 512], ids=id_formatter("dim3"))
379 @pytest.mark.parametrize("dim4", [512], ids=id_formatter("dim4"))
380 @pytest.mark.parametrize("dims", (2, 3), ids=id_formatter("dims"))
381 @pytest.mark.parametrize("ldb", (0,), ids=id_formatter("ldb"))
382 def test_int8_linear_matmul(self, device, dim1, dim2, dim3, dim4, dims, ldb):
383 for i in range(k):
384 if dims == 2:
385 A = torch.randint(-128, 127, size=(dim1, dim3), dtype=torch.int8, device=device)
386 elif dims == 3:
387 A = torch.randint(-128, 127, size=(dim1, dim2, dim3), dtype=torch.int8, device=device)
388 B = torch.randint(-128, 127, size=(dim4, dim3), dtype=torch.int8, device=device)
389 C1 = torch.matmul(A.float(), B.t().float())
390
391 C2 = F.int8_linear_matmul(A, B)
392 torch.testing.assert_close(C1, C2.float())
393
394 @pytest.mark.parametrize("device", get_available_devices())
395 @pytest.mark.parametrize("dim1", [32], ids=id_formatter("dim1"))
396 @pytest.mark.parametrize("dim2", [32], ids=id_formatter("dim2"))
397 @pytest.mark.parametrize("dim3", [32], ids=id_formatter("dim3"))
398 @pytest.mark.parametrize("dim4", [32], ids=id_formatter("dim4"))
399 @pytest.mark.parametrize("dims", (2,), ids=id_formatter("dims"))
400 def test_int8_linear_matmul_half(self, device, dim1, dim2, dim3, dim4, dims):
401 for i in range(k):
402 if dims == 2:
403 A = torch.normal(0, 0.5, size=(dim1, dim3), device=device).half()
404 elif dims == 3:
405 A = torch.normal(0, 0.5, size=(dim1, dim2, dim3), device=device).half()
406 B = torch.randn((dim4, dim3), device=device).half()
407 torch.nn.init.xavier_uniform_(B)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected