Skip to content

Use native int2 types. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ targets (LiteRT).
converter could produce full integer models.
* LoRA/QLoRA: this mode enables LoRA and QLoRA on a model.
* Supported numerics:
* Native: `int4`, `int8`, `fp8`.
* Emulated: `int1` to `int7`, `nf4`.
* Native: `int2`, `int4`, `int8`, `fp8`.
* Emulated: `int3` to `int7`, `nf4`.
* Supported array calibration methods:
* `absmax`: symmetric quantization using maximum absolute value.
* `minmax`: asymmetric quantization using minimum and maximum values.
Expand Down
4 changes: 2 additions & 2 deletions qwix/core/numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_symmetric_bound(qtype: jax.typing.DTypeLike) -> float:
match qtype:
case 'nf4':
return 1.0
case 'int2' | 'int3' | 'int5' | 'int6' | 'int7':
case 'int3' | 'int5' | 'int6' | 'int7':
# The bound is extended to qmax + 0.5 so that we have a better utilization
# of the whole range. This is more important for fewer bits of int.
return 2 ** (int(qtype[3:]) - 1) - 0.5
Expand All @@ -63,7 +63,7 @@ def convert_to(x: jax.Array, qtype: jax.typing.DTypeLike) -> jax.Array:
match qtype:
case 'nf4':
return fp_to_nf4(x)
case 'int2' | 'int3' | 'int5' | 'int6' | 'int7':
case 'int3' | 'int5' | 'int6' | 'int7':
bits = int(qtype[3:])
qmin = -(2 ** (bits - 1))
qmax = 2 ** (bits - 1) - 1
Expand Down
14 changes: 13 additions & 1 deletion tests/core/numerics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_convert_to(self):
numerics.convert_to(jnp.array([1.2, 3.5, 8, -1300]), jnp.int4),
jnp.array([1, 4, 7, -8], jnp.int4),
)
self._assert_equal(
numerics.convert_to(jnp.array([1.2, 3.5, 8, -1300]), jnp.int2),
jnp.array([1, 1, 1, -2], jnp.int2),
)

def test_inf(self):
self._assert_equal(
Expand All @@ -52,7 +56,15 @@ def test_arbitrary_integer_dtype(self):
numerics.convert_to(jnp.array([1.2, 3.5, 129, -1300]), "int6"),
jnp.array([1, 4, 31, -32], jnp.int8),
)
# jnp.int4 and "int4" should be the same.
# jnp.int* and "int*" should be the same.
self._assert_equal(
numerics.get_symmetric_bound("int2"),
numerics.get_symmetric_bound(jnp.int2),
)
self._assert_equal(
numerics.convert_to(jnp.array([1.2, 3.5, 129, -1300]), "int2"),
numerics.convert_to(jnp.array([1.2, 3.5, 129, -1300]), jnp.int2),
)
self._assert_equal(
numerics.get_symmetric_bound("int4"),
numerics.get_symmetric_bound(jnp.int4),
Expand Down