-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_intx.py
More file actions
186 lines (159 loc) · 6.76 KB
/
Copy pathtest_intx.py
File metadata and controls
186 lines (159 loc) · 6.76 KB
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
# Copyright 2026 Apple Inc.
#
# Use of this source code is governed by a BSD-3-clause license that can
# be found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause
"""Test uintx tensors."""
import math
import pytest
import torch
from coreai_torch._compression._intx import ( # type: ignore [attr-defined]
IntxTensor,
UintxTensor,
)
@pytest.mark.parametrize(
"tensor_shape",
[(3,), (2, 4), (3, 3, 3), (3, 4, 3, 3), (13, 39, 19, 111)],
)
@pytest.mark.parametrize(
"nbits",
[1, 2, 4, 6],
)
def test_uintx_tensor(tensor_shape: tuple[int, ...], nbits: int) -> None:
"""Test uintx tensor."""
unpacked = torch.randint(0, 1 << nbits, tensor_shape).to(torch.uint8)
uintx_tensor = UintxTensor.from_unpacked(unpacked, nbits)
assert uintx_tensor.shape == torch.Size(tensor_shape)
assert torch.equal(uintx_tensor.to(torch.uint8), unpacked)
assert uintx_tensor.elem.numel() == math.ceil(math.prod(tensor_shape) * nbits / 8)
@pytest.mark.parametrize(
"nbits",
[1, 2, 4, 6],
)
def test_uintx_scalar(nbits: int) -> None:
"""Test uintx tensor for scalar input."""
target_scalar = 2 if nbits > 1 else 1
uintx_tensor = UintxTensor.from_unpacked(unpacked=target_scalar, nbits=nbits)
assert uintx_tensor.shape == torch.Size((1,))
assert torch.equal(uintx_tensor.to(torch.uint8), torch.Tensor([target_scalar]))
# The packed elements could be padded when the number of bits is not aligned with
# byte. For example, for 6-bit, 4 elements take 24 bits (3 bytes), so a single
# 6-bit element will have two more padding values in packed `elem`.
if nbits == 6:
assert uintx_tensor.elem.numel() == 3
else:
assert uintx_tensor.elem.numel() == 1
@pytest.mark.parametrize(
"tensor_shape",
[(3,), (2, 4), (3, 3, 3), (3, 4, 3, 3), (13, 39, 19, 111)],
)
@pytest.mark.parametrize(
"nbits",
[4],
)
def test_intx_tensor(tensor_shape: tuple[int, ...], nbits: int) -> None:
"""Test intx tensor."""
unpacked = torch.randint(
-1 << (nbits - 1),
(1 << (nbits - 1)) - 1,
tensor_shape,
).to(torch.int8)
intx_tensor = IntxTensor.from_unpacked(unpacked, nbits)
assert intx_tensor.shape == torch.Size(tensor_shape)
assert torch.equal(intx_tensor.to(torch.int8), unpacked)
assert intx_tensor.elem.numel() == math.ceil(math.prod(tensor_shape) * nbits / 8)
@pytest.mark.parametrize(
"nbits",
[4],
)
def test_intx_scalar(nbits: int) -> None:
"""Test intx tensor with scalar input."""
intx_tensor = IntxTensor.from_unpacked(unpacked=2, nbits=nbits)
assert intx_tensor.shape == torch.Size((1,))
assert torch.equal(intx_tensor.to(torch.int8), torch.Tensor([2]))
assert intx_tensor.elem.numel() == 1
@pytest.mark.parametrize(
"dim",
[0, 1, -1],
)
def test_uintx_cat(dim: int) -> None:
"""Test uintx tensor concatenation along a given dimension."""
unpacked = [
torch.tensor([[3, 0, 14, 1], [0, 7, 15, 2]], dtype=torch.uint8),
torch.tensor([[1, 4, 5, 6], [2, 3, 6, 1]], dtype=torch.uint8),
]
uintx_tensors = [UintxTensor.from_unpacked(x, 4) for x in unpacked]
concatenated = torch.cat(uintx_tensors, dim=dim)
expected = torch.cat(unpacked, dim=dim)
assert concatenated.shape == expected.shape
assert torch.equal(concatenated.to(torch.uint8), expected)
@pytest.mark.parametrize(
"dim",
[0, 1, -1],
)
def test_intx_cat(dim: int) -> None:
"""Test intx tensor concatenation along a given dimension."""
unpacked = [
torch.tensor([[3, 0, -2, 1], [0, 7, -8, 2]], dtype=torch.int8),
torch.tensor([[-1, 4, 5, -6], [2, -3, 6, 1]], dtype=torch.int8),
]
intx_tensors = [IntxTensor.from_unpacked(x, 4) for x in unpacked]
concatenated = torch.cat(intx_tensors, dim=dim)
expected = torch.cat(unpacked, dim=dim)
assert concatenated.shape == expected.shape
assert torch.equal(concatenated.to(torch.int8), expected)
def test_uint4_packed_value() -> None:
"""Test uint4 packed values."""
unpacked = torch.tensor([3, 0, 2, 0, 9, 14, 15], dtype=torch.uint8)
uintx_tensor = UintxTensor.from_unpacked(unpacked, 4)
# We use little end bitorder: https://numpy.org/doc/stable/reference/generated/numpy.packbits.html
# [9, 14]: [(1, 0, 0, 1), (1, 1, 1, 0)] -> [1, 1, 1, 0, 1, 0, 0, 1] -> 233
assert torch.equal(
uintx_tensor.elem,
torch.tensor([3, 2, 233, 15], dtype=torch.uint8),
)
def test_uint2_packed_value() -> None:
"""Test uint2 packed values."""
unpacked = torch.tensor([3, 0, 0, 0, 2, 0, 0, 0, 1, 0], dtype=torch.uint8)
# Every 4 elements are packed into one byte.
# [3, 0, 0, 0]: [(1, 1), (0, 0), (0, 0), (0, 0)] -> [00 00 00 11] -> 3
# [2, 0, 0, 0]: [(1, 0), (0, 0), (0, 0), (0, 0)] -> [00 00 00 10] -> 2
# [1, 0]: [(0, 1), (0, 0), padded(0, 0), padded(0, 0)] -> [00 00 00 01] -> 1
uintx_tensor = UintxTensor.from_unpacked(unpacked, 2)
assert torch.equal(uintx_tensor.elem, torch.tensor([3, 2, 1], dtype=torch.uint8))
def test_uint6_packed_value() -> None:
"""Test uint6 packed values."""
unpacked = torch.tensor([21, 0, 35], dtype=torch.uint8)
uintx_tensor = UintxTensor.from_unpacked(unpacked, 6)
# We use little end bitorder: https://numpy.org/doc/stable/reference/generated/numpy.packbits.html
# unpacked values are [010101], [0000-00], [10-0011]
# it will gets packed to [00-010101], [0011-0000], [000000-10]
assert torch.equal(
uintx_tensor.elem,
torch.tensor([21, 48, 2], dtype=torch.uint8),
)
unpacked = torch.tensor([21, 0, 35, 8], dtype=torch.uint8)
uintx_tensor = UintxTensor.from_unpacked(unpacked, 6)
# unpacked values are [010101], [0000-00], [10-0011], [001000]
# it will gets packed to [00-010101], [0011-0000], [001000-10]
assert torch.equal(
uintx_tensor.elem,
torch.tensor([21, 48, 34], dtype=torch.uint8),
)
def test_uint1_packed_value() -> None:
"""Test uint1 packed values."""
unpacked = torch.tensor([1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1], dtype=torch.uint8)
# Every 8 elements are packed into one byte.
# [1, 0, 1, 0, 1, 0, 0, 0] -> 00010101 -> 21
# [1, 0, 1] -> padded(00000) 101 -> 5
uintx_tensor = UintxTensor.from_unpacked(unpacked, 1)
assert torch.equal(uintx_tensor.elem, torch.tensor([21, 5], dtype=torch.uint8))
def test_int4_packed_value() -> None:
"""Test int4 packed values."""
unpacked = torch.tensor([[3, 0, -2], [0, 7, -8]], dtype=torch.int8)
intx_tensor = IntxTensor.from_unpacked(unpacked, 4)
# We use little end bitorder: https://numpy.org/doc/stable/reference/generated/numpy.packbits.html
# [7, -8]: [(0, 1, 1, 1), (1, 0, 0, 0)] -> [1, 0, 0, 0, 0, 1, 1, 1] -> 135
assert torch.equal(
intx_tensor.elem,
torch.tensor([3, 14, 135], dtype=torch.uint8),
)