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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# Copyright 2026 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Flax linen Quantization modules."""

from collections.abc import Callable, Sequence
import dataclasses
import einops
from flax import linen as nn
from flax.linen import dtypes as flax_dtypes
from flax.typing import Array  # pylint: disable=g-importing-member
from gemma.peft import _quantization_utils
import jax
import jax.numpy as jnp


# (0.5 - 1/32) 2**-22 is the smallest non-zero number in sfp8
_SFP8_ZERO_THRESHOLD = 1.1175882588358173e-07
# 0.625 * 2**-22 is the smallest non-zero number in sfp8
_SFP8_LOWEST_NON_ZERO_BOUND = 1.4901161193847656e-07
_SFP8_UPPER_BOUND = 1.875
_QUANTIZATION_BLOCK_SIZE = 32  # for Q4_0 quantization
_NON_ZERO_DIVISION_EPS = 1e-6


def simulate_quantize(
    x: Array,
    method: _quantization_utils.QuantizationMethod | str,
    axis_to_reduce: int | None = None,
) -> Array:
  """Quantizes the given array.

  In this API, we do not actually quantize tensors as the output is not stored
  using less bits but rather simulate quantization to enable quantization aware
  training.

  NOTE: you can use this implementation to evaluate a checkpoint as if it was
  quantized.

  Args:
    x: The array to simulate_quantize.
    method: The quantization method to use.
    axis_to_reduce: The axis to reduce the array over.

  Returns:
    The simulate_quantized array.
  """
  method = _quantization_utils.QuantizationMethod(method)
  match method:
    case _quantization_utils.QuantizationMethod.NONE:
      return x
    case _quantization_utils.QuantizationMethod.INT4:
      return _simulate_uniform_quantization(
          x,
          bitwidth=4,
          granularity=_quantization_utils.QuantizationGranularity.PER_CHANNEL,
          axis_to_reduce=axis_to_reduce,
      )
    case _quantization_utils.QuantizationMethod.INT8:
      return _simulate_uniform_quantization(
          x,
          bitwidth=8,
          granularity=_quantization_utils.QuantizationGranularity.PER_CHANNEL,
      )
    case _quantization_utils.QuantizationMethod.Q4_0:
      return _simulate_uniform_quantization(
          x,
          bitwidth=4,
          granularity=_quantization_utils.QuantizationGranularity.PER_BLOCK,
      )
    case _quantization_utils.QuantizationMethod.Q4_0_TRANSPOSE:
      return _simulate_uniform_quantization(
          x,
          bitwidth=4,
          granularity=_quantization_utils.QuantizationGranularity.PER_BLOCK,
          transpose=True,
      )
    case _quantization_utils.QuantizationMethod.SFP8:
      return _simulate_sfp8_quantization(x)
    case _:
      raise ValueError(f'Unknown quantization method: {method}')


class SimulateQuantizedDense(nn.Module):
  """Wrapper around `nn.Dense` which adds a Quantized adapter."""

  _: dataclasses.KW_ONLY

  wrapped: nn.Dense
  method: _quantization_utils.QuantizationMethod = (
      _quantization_utils.QuantizationMethod.NONE
  )

  def __post_init__(self):
    super().__post_init__()
    # Share scope, to make the wrapper module transparent with respect to the
    # parameters (instead of nesting `{'params': {'wrapped': params}}`).
    if self.scope is not None:
      nn.share_scope(self, self.wrapped)

  @nn.compact
  def __call__(self, inputs: Array) -> Array:
    kernel = self.param(  # pyrefly: ignore[no-matching-overload]
        'kernel',
        self.wrapped.kernel_init,
        (inputs.shape[-1], self.wrapped.features),
        dtype=self.wrapped.dtype,
    )
    w = simulate_quantize(
        kernel,
        self.method,
        axis_to_reduce=None,
    )
    y = inputs @ w
    if self.wrapped.use_bias:
      b = self.param(  # pyrefly: ignore[no-matching-overload]
          'bias',
          self.wrapped.bias_init,
          (self.wrapped.features,),
          dtype=self.wrapped.dtype,
      )
      y += b
    return y


class SimulateQuantizedEinsum(nn.Module):
  """Wrapper around `nn.Einsum` which adds a Quantized adapter."""

  _: dataclasses.KW_ONLY

  wrapped: nn.Einsum
  method: _quantization_utils.QuantizationMethod = (
      _quantization_utils.QuantizationMethod.NONE
  )

  def __post_init__(self):
    super().__post_init__()
    # Share scope, to make the wrapper module transparent with respect to the
    # parameters (instead of nesting `{'params': {'wrapped': params}}`).
    if self.scope is not None:
      nn.share_scope(self, self.wrapped)

  def process_einsum_str(self, einsum_str: str) -> str:
    """Processes the einsum string."""

    einsum_str = einsum_str.replace(' ', '')
    if '->' not in einsum_str:
      raise ValueError(
          '`einsum_str` equation must be explicit and include "->".'
      )
    if einsum_str.count(',') != 1:
      raise ValueError(
          '`einsum_str` equation must have exactly two operands and '
          'therefore, exactly one comma character, instead of '
          f'{einsum_str.count(",")}'
      )
    return einsum_str

  @nn.compact
  def __call__(self, inputs: Array, einsum_str: str | None = None) -> Array:
    einsum_str = nn.merge_param(
        'einsum_str', self.wrapped.einsum_str, einsum_str
    )
    einsum_str = self.process_einsum_str(einsum_str)

    kernel = self.param(
        'kernel',
        self.wrapped.kernel_init,
        self.wrapped.shape,
        self.wrapped.param_dtype,
    )

    inputs, kernel, _ = flax_dtypes.promote_dtype(
        inputs, kernel, None, dtype=self.wrapped.dtype
    )

    kernel = simulate_quantize(
        kernel,
        self.method,
        axis_to_reduce=get_axis_to_reduce_from_einsum_str(  # pyrefly: ignore[bad-argument-type]
            einsum_str=self.wrapped.name  # pyrefly: ignore[bad-argument-type]
        ),
    )

    y = jnp.einsum(einsum_str, inputs, kernel, precision=self.wrapped.precision)
    if self.wrapped.use_bias:
      bias_shape, _ = self.wrapped._get_bias_shape(einsum_str, inputs, kernel)
      bias = self.param(
          'bias', self.wrapped.bias_init, bias_shape, self.wrapped.param_dtype
      )
      y += bias
    return y


class IntDense(nn.Module):
  """Wrapper around `nn.Dense` which adds a Quantized adapter."""

  _: dataclasses.KW_ONLY

  wrapped: nn.Dense
  dtype: jnp.dtype = jnp.int4  # pyrefly: ignore[bad-assignment]

  def __post_init__(self):
    super().__post_init__()
    # Share scope, to make the wrapper module transparent with respect to the
    # parameters (instead of nesting `{'params': {'wrapped': params}}`).
    if self.scope is not None:
      nn.share_scope(self, self.wrapped)

  @nn.compact
  def __call__(self, inputs: Array) -> Array:
    kernel = self.param(
        'kernel',
        nn.initializers.ones_init(),
        (inputs.shape[-1], self.wrapped.features),
        self.dtype,
    )
    scale = self.param(
        'scale',
        nn.initializers.ones_init(),
        (1, self.wrapped.features),
        self.wrapped.dtype,
    )
    w = kernel.astype(self.wrapped.dtype) / scale
    y = inputs @ w
    if self.wrapped.use_bias:
      b = self.param(  # pyrefly: ignore[no-matching-overload]
          'bias',
          self.wrapped.bias_init,
          (self.wrapped.features,),
          dtype=self.wrapped.dtype,
      )
      y += b
    return y


class IntEinsum(nn.Module):
  """Wrapper around `nn.Einsum` which adds a Quantized adapter."""

  _: dataclasses.KW_ONLY

  wrapped: nn.Einsum
  dtype: jnp.dtype = jnp.int4  # pyrefly: ignore[bad-assignment]

  def __post_init__(self):
    super().__post_init__()
    # Share scope, to make the wrapper module transparent with respect to the
    # parameters (instead of nesting `{'params': {'wrapped': params}}`).
    if self.scope is not None:
      nn.share_scope(self, self.wrapped)

  def process_einsum_str(self, einsum_str: str) -> str:
    """Processes the einsum string."""

    einsum_str = einsum_str.replace(' ', '')
    if '->' not in einsum_str:
      raise ValueError(
          '`einsum_str` equation must be explicit and include "->".'
      )
    if einsum_str.count(',') != 1:
      raise ValueError(
          '`einsum_str` equation must have exactly two operands and '
          'therefore, exactly one comma character, instead of '
          f'{einsum_str.count(",")}'
      )
    return einsum_str

  @nn.compact
  def __call__(self, inputs: Array, einsum_str: str | None = None) -> Array:
    einsum_str = nn.merge_param(
        'einsum_str', self.wrapped.einsum_str, einsum_str
    )
    einsum_str = self.process_einsum_str(einsum_str)

    kernel = self.param(
        'kernel',
        nn.initializers.ones_init(),
        self.wrapped.shape,
        self.dtype,
    ).astype(self.wrapped.dtype)
    scale = self.param(
        'scale',
        nn.initializers.ones_init(),
        tuple(jnp.ones(len(self.wrapped.shape) - 1, dtype=jnp.int32).tolist())
        + (self.wrapped.shape[-1],),
        self.wrapped.dtype,
    )

    inputs, kernel, _ = flax_dtypes.promote_dtype(
        inputs, kernel, None, dtype=self.wrapped.dtype
    )

    kernel = kernel / scale

    y = jnp.einsum(einsum_str, inputs, kernel, precision=self.wrapped.precision)
    if self.wrapped.use_bias:
      bias_shape, _ = self.wrapped._get_bias_shape(einsum_str, inputs, kernel)
      bias = self.param(
          'bias', self.wrapped.bias_init, bias_shape, self.wrapped.param_dtype
      )
      y += bias
    return y


def _straight_through_estimator(
    func: Callable[..., Array],
) -> Callable[..., Array]:
  """Decorator to make a straight through estimator w.r.t the first argument.

  Args:
    func: The function to make a straight through estimator for

  Returns:
    The straight through estimator
  """

  def wrapper(x: Array, *args, **kwargs):
    zero = x - jax.lax.stop_gradient(x)
    y = jax.lax.stop_gradient(func(x, *args, **kwargs))
    return zero + y

  return wrapper


def _pack(
    x: Array, *, transpose: bool = True
) -> tuple[Array, jax.ShapeDtypeStruct]:
  """Reshape the input array into shape (-1, block_size)."""
  if transpose:
    x = einops.rearrange(x, '... a b -> ... b a')
  orig_shape, orig_dtype = x.shape, x.dtype
  orig_shape_dtype = jax.ShapeDtypeStruct(shape=orig_shape, dtype=orig_dtype)
  assert orig_shape[-1] % _QUANTIZATION_BLOCK_SIZE == 0
  n_blocks = x.size // _QUANTIZATION_BLOCK_SIZE
  blocks = x.reshape((n_blocks, _QUANTIZATION_BLOCK_SIZE)).astype(jnp.float32)
  return blocks, orig_shape_dtype


def _unpack(
    x: Array,
    *,
    transpose: bool,
    orig_shape_dtype: jax.ShapeDtypeStruct,
    cast_to_orig_dtype: bool = True,
) -> Array:
  """Reshape back the input array to its original shape."""
  res = x.reshape(orig_shape_dtype.shape)
  if cast_to_orig_dtype:
    res = res.astype(orig_shape_dtype.dtype)
  if transpose:
    res = einops.rearrange(res, '... b a -> ... a b')
  return res


def _q4_0(
    x: Array, *, upper_bound: int, bitwidth: int, transpose: bool = False
) -> Array:
  """Approximation of x using Q4_0."""

  def _simulate_quantize_block(arr: Array) -> Array:
    """Quantizes a single block of the input array."""
    assert arr.ndim == 1

    amax_idx = jnp.argmax(jnp.abs(arr))
    max_val = arr[amax_idx]

    d = -max_val / upper_bound
    safe_d = jnp.where(d == 0.0, 1.0, d)
    safe_inv_d = jnp.where(d == 0.0, 0.0, 1.0 / safe_d)

    scale = d.astype(jnp.float16)
    qs = arr * safe_inv_d
    qs = jnp.clip(jnp.trunc(qs + upper_bound + 0.5), 0.0, 2**bitwidth - 1)
    d = scale.astype(jnp.float32)
    return d * (qs - upper_bound)

  packed_x, orig_shape_dtype = _pack(x, transpose=transpose)
  vmaped_approx = jax.vmap(_simulate_quantize_block, in_axes=0, out_axes=0)
  packed_approx_x = vmaped_approx(packed_x)
  approx_x = _unpack(
      packed_approx_x, transpose=transpose, orig_shape_dtype=orig_shape_dtype
  )
  return approx_x


@_straight_through_estimator
def _simulate_uniform_quantization(
    x: Array,
    *,
    bitwidth: int,
    granularity: _quantization_utils.QuantizationGranularity,
    transpose: bool = False,
    axis_to_reduce: int | None = None,
) -> Array:
  """Applies uniform quantization to the given array.

  Quantization here is defined as x_hat = round(x * scale) / scale. We wrap this
  op in a straight through estimator for optimization compatibility.

  Args:
    x: The array to simulate_quantize.
    bitwidth: The bitwidth of the quantization.
    granularity: The granularity of the quantization.
    transpose: Whether to transpose the array before quantization.
    axis_to_reduce: The axis to reduce the max over.

  Returns:
    The simulate_quantized array.
  """
  # get bitwidth bounds
  assert bitwidth >= 2  # binary quantization is not supported
  lower_bound = -(2 ** (bitwidth - 1))
  upper_bound = 2 ** (bitwidth - 1) - 1
  # compute scales
  match granularity:
    case _quantization_utils.QuantizationGranularity.PER_TENSOR:
      max_ = jnp.max(jnp.abs(x))
      scales = upper_bound * jnp.squeeze(jnp.array([1.0 / max_]))
    case _quantization_utils.QuantizationGranularity.PER_CHANNEL:
      if axis_to_reduce is None:
        max_ = _quantization_utils.reduce_max_all_but_one_axis(
            jnp.abs(x), axis=-1
        )
      else:
        max_ = jnp.max(jnp.abs(x), keepdims=True, axis=axis_to_reduce)
      scales = upper_bound * jnp.array(1.0 / max_)
    case _quantization_utils.QuantizationGranularity.PER_BLOCK:
      return _q4_0(
          x, upper_bound=upper_bound + 1, bitwidth=bitwidth, transpose=transpose
      )
    case _:
      raise ValueError(f'Unknown granularity: {granularity}')

  # apply scale
  x = x * scales
  # clip and round
  x = jnp.clip(jnp.round(x), lower_bound, upper_bound)
  # unscale
  x = x / jnp.maximum(scales, _NON_ZERO_DIVISION_EPS)
  return x


def _floor_to_closest_power_of_two(x: Array, eps: float = 1e-6) -> Array:
  """Floor x to the closest power of two.

  We compute the log2 of x and floor it to the closest integer. Then we
  re-compute the value of x from the floored log2. To avoid logs of zero, we
  extract the maximum between abs(x) and eps. Furthermore, we handle negative
  values by extracting the sign ahead of computing the log.

  Args:
    x: The array to floor
    eps: A small value to threshold "zero" (choose carefully to avoid
      underflows)

  Returns:
    The floored array
  """
  x_sign = jnp.sign(x)
  return x_sign * jnp.exp2(jnp.floor(jnp.log2(jnp.maximum(jnp.abs(x), eps))))


@_straight_through_estimator
def _simulate_sfp8_quantization(x: Array) -> Array:
  """Applies SFP8 quantization to the given array.

  Args:
    x: The array to simulate_quantize.

  Returns:
    The simulate_quantized array.
  """
  sfp8_scale = _SFP8_UPPER_BOUND / jnp.maximum(
      _quantization_utils.reduce_max_all_but_one_axis(jnp.abs(x), -1),
      _NON_ZERO_DIVISION_EPS,
  )
  x = x * sfp8_scale

  x_sign = jnp.sign(x)
  x = jnp.abs(x)
  zero_mask = x < _SFP8_ZERO_THRESHOLD
  lower_bound_mask = x < _SFP8_LOWEST_NON_ZERO_BOUND
  lower_bound = jnp.maximum(
      _floor_to_closest_power_of_two(x), _SFP8_LOWEST_NON_ZERO_BOUND
  )
  switched_step_mask = jnp.where(lower_bound <= 1.0 / 256.0, 2.0, 1.0)
  step_size = switched_step_mask * lower_bound / 8.0
  x = jnp.round((x - lower_bound) / step_size) * step_size + lower_bound
  x = jnp.where(lower_bound_mask, _SFP8_LOWEST_NON_ZERO_BOUND, x)
  x = jnp.where(zero_mask, 0, x)
  return x_sign * x / sfp8_scale


def get_axis_to_reduce_from_einsum_str(
    einsum_str: str,
) -> Sequence[int] | None:
  """Returns the axis to reduce over."""
  match einsum_str:
    case 'BTD,NDH->BTNH':
      return (1,)
    case 'BSD,CKDH->CBSKH':
      return (2,)
    case 'BTNH,NHD->BTD':
      return (0, 1)
    case '...F,NHF->...NH':
      return (2,)
    case '...H,HF->...F':
      return (0,)
    case _:
      return None