|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Quantized jax.lax.ragged_dot.""" |
| 16 | + |
| 17 | +import jax |
| 18 | +from jax import numpy as jnp |
| 19 | +from qwix._src.core import qarray |
| 20 | + |
| 21 | + |
| 22 | +def _fast_ragged_dot( |
| 23 | + lhs: qarray.MaybeQArray, |
| 24 | + rhs: qarray.MaybeQArray, |
| 25 | + group_sizes: jax.Array, |
| 26 | + precision: jax.lax.PrecisionLike = None, |
| 27 | + preferred_element_type: jax.typing.DTypeLike | None = None, |
| 28 | + group_offset: jax.Array | None = None, |
| 29 | +) -> jax.Array: |
| 30 | + """Quantized jax.lax.ragged_dot.""" |
| 31 | + if isinstance(lhs, qarray.QArray): |
| 32 | + assert lhs.zero_point is None, 'not supported yet' |
| 33 | + lhs_value = lhs.qvalue |
| 34 | + lhs_scale = lhs.scale |
| 35 | + else: |
| 36 | + lhs_value = lhs |
| 37 | + lhs_scale = None |
| 38 | + if isinstance(rhs, qarray.QArray): |
| 39 | + assert rhs.zero_point is None, 'not supported yet' |
| 40 | + rhs_value = rhs.qvalue |
| 41 | + rhs_scale = rhs.scale |
| 42 | + else: |
| 43 | + rhs_value = rhs |
| 44 | + rhs_scale = None |
| 45 | + |
| 46 | + preferred_element_type, result_type = qarray.get_accumulator_and_result_type( |
| 47 | + lhs, rhs, preferred_element_type=preferred_element_type |
| 48 | + ) |
| 49 | + |
| 50 | + out = jax.lax.ragged_dot( |
| 51 | + lhs_value, |
| 52 | + rhs_value, |
| 53 | + group_sizes, |
| 54 | + precision=precision, |
| 55 | + preferred_element_type=preferred_element_type, |
| 56 | + group_offset=group_offset, |
| 57 | + ) |
| 58 | + |
| 59 | + # ragged_dot has fixed dimension numbers which makes implementation a lot |
| 60 | + # easier, i.e., lhs: [m, k], rhs: [g, k, n], res: [m, n]. |
| 61 | + # TODO(dangyi): support arbitrary dimension numbers. |
| 62 | + if lhs_scale is not None: # [m, 1] |
| 63 | + lhs_scale = qarray.transpose_array(lhs_scale, (0, None)) |
| 64 | + out = qarray.call_with_generic_broadcast(jnp.multiply, out, lhs_scale) |
| 65 | + if rhs_scale is not None: # [1, 1, n] or [g, 1, n] |
| 66 | + if rhs_scale.shape[0] == 1: |
| 67 | + # It's possible to apply the scale to the out directly. |
| 68 | + rhs_scale = qarray.transpose_array(rhs_scale, (None, 2)) |
| 69 | + else: |
| 70 | + # We need another ragged_dot to apply the scale to the out. |
| 71 | + rhs_scale = jax.lax.ragged_dot( |
| 72 | + jnp.ones((out.shape[0], 1), rhs_scale.dtype), |
| 73 | + rhs_scale, |
| 74 | + group_sizes, |
| 75 | + group_offset=group_offset, |
| 76 | + ) |
| 77 | + out = qarray.call_with_generic_broadcast(jnp.multiply, out, rhs_scale) |
| 78 | + |
| 79 | + return out.astype(result_type) |
| 80 | + |
| 81 | + |
| 82 | +def _slow_ragged_dot( |
| 83 | + lhs: qarray.MaybeQArray, |
| 84 | + rhs: qarray.MaybeQArray, |
| 85 | + group_sizes: jax.Array, |
| 86 | + **kwargs, |
| 87 | +) -> jax.Array: |
| 88 | + """Quantized jax.lax.ragged_dot which dequantizes first.""" |
| 89 | + if isinstance(lhs, qarray.QArray): |
| 90 | + lhs = qarray.dequantize(lhs) |
| 91 | + if isinstance(rhs, qarray.QArray): |
| 92 | + rhs = qarray.dequantize(rhs) |
| 93 | + return jax.lax.ragged_dot(lhs, rhs, group_sizes, **kwargs) |
| 94 | + |
| 95 | + |
| 96 | +def ragged_dot( |
| 97 | + lhs: qarray.MaybeQArray, |
| 98 | + rhs: qarray.MaybeQArray, |
| 99 | + group_sizes: jax.Array, |
| 100 | + precision: jax.lax.PrecisionLike = None, |
| 101 | + preferred_element_type: jax.typing.DTypeLike | None = None, |
| 102 | + group_offset: jax.Array | None = None, |
| 103 | +) -> jax.Array: |
| 104 | + """Quantized jax.lax.ragged_dot.""" |
| 105 | + use_fast_ragged_dot = True |
| 106 | + |
| 107 | + # fast_ragged_dot does't support channelwise scales on group axis, or tiled |
| 108 | + # scales on contracting axes, or zero_point. |
| 109 | + if isinstance(lhs, qarray.QArray): # [m, k] |
| 110 | + if lhs.zero_point is not None or lhs.scale.shape[1] > 1: |
| 111 | + use_fast_ragged_dot = False |
| 112 | + if isinstance(rhs, qarray.QArray): # [g, k, n] |
| 113 | + if ( |
| 114 | + rhs.zero_point is not None |
| 115 | + or rhs.scale.shape[0] > 1 |
| 116 | + or rhs.scale.shape[1] > 1 |
| 117 | + ): |
| 118 | + use_fast_ragged_dot = False |
| 119 | + |
| 120 | + if use_fast_ragged_dot: |
| 121 | + return _fast_ragged_dot( |
| 122 | + lhs, |
| 123 | + rhs, |
| 124 | + group_sizes, |
| 125 | + precision=precision, |
| 126 | + preferred_element_type=preferred_element_type, |
| 127 | + group_offset=group_offset, |
| 128 | + ) |
| 129 | + else: |
| 130 | + return _slow_ragged_dot( |
| 131 | + lhs, |
| 132 | + rhs, |
| 133 | + group_sizes, |
| 134 | + precision=precision, |
| 135 | + preferred_element_type=preferred_element_type, |
| 136 | + group_offset=group_offset, |
| 137 | + ) |
0 commit comments