Skip to content

Internal prototype #655

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
3 changes: 3 additions & 0 deletions aqt/jax/v2/aqt_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Quantizer:
_calibrator: AbstractAqtCalibration | None = utils.static_field(default=None)
# Round up the calibration to power of 2 (po2).
po2_scale: bool = utils.static_field()
scale_dtype: jnp.dtype | None = utils.static_field(default=None)
# TODO(yichizh): Factor out auxilliary dataclasses into a separate file.
context: utils.Context

Expand Down Expand Up @@ -85,6 +86,8 @@ def calibrate(self, x, *, calibration_axes) -> aqt_tensor.QTensor:
bound = self._calibrator.get_bound(x, shared_axes, self.context)
abs_max_mapped_to = self.numerics.abs_val_mapped_to()
scale = bound / abs_max_mapped_to
if self.scale_dtype:
scale = scale.astype(self.scale_dtype)

if self.po2_scale:
# With floor the biggest value (we are using jnp.max) is in the range of
Expand Down
18 changes: 18 additions & 0 deletions aqt/jax/v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ def get_numerics(bits):
return cfg


def set_scale_dtype(cfg: DotGeneral, scale_dtype: jnp.dtype):
"""Set scale_dtype for dot_general config."""
assert isinstance(
cfg.fwd.dg_quantizer, aqt_dot_general.DefaultDotGeneralQuantizer
)
assert isinstance(
cfg.dlhs.dg_quantizer, aqt_dot_general.DefaultDotGeneralQuantizer
)
assert isinstance(
cfg.drhs.dg_quantizer, aqt_dot_general.DefaultDotGeneralQuantizer
)
cfg.fwd.dg_quantizer.lhs.scale_dtype = scale_dtype
cfg.fwd.dg_quantizer.rhs.scale_dtype = scale_dtype
cfg.dlhs.dg_quantizer.lhs.scale_dtype = scale_dtype
cfg.dlhs.dg_quantizer.rhs.scale_dtype = scale_dtype
cfg.drhs.dg_quantizer.lhs.scale_dtype = scale_dtype
cfg.drhs.dg_quantizer.rhs.scale_dtype = scale_dtype

################################################################################
# Functions below are auxiliary config creators.

Expand Down