File size: 3,768 Bytes
2f86d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

from etils import etree
from etils.enp.typing import ArrayAliasMeta  # pylint: disable=g-importing-member
from etils.enp.typing import f32
from flax import linen as nn
from gemma import peft
import jax
from jax import numpy as jnp
import numpy as np

i4 = ArrayAliasMeta(shape=None, dtype=jnp.int4)  # pyrefly: ignore[bad-argument-type]


def _dense_to_quantized(module):
  if isinstance(module, nn.Dense):
    return peft.SimulateQuantizedDense(
        wrapped=module, method=peft.QuantizationMethod.INT4
    )
  if isinstance(module, nn.Einsum):
    return peft.SimulateQuantizedEinsum(
        wrapped=module, method=peft.QuantizationMethod.INT4
    )
  else:
    return module


def _dense_to_int4(module):
  if isinstance(module, nn.Dense):
    return peft.IntDense(wrapped=module, dtype=jnp.int4)  # pyrefly: ignore[bad-argument-type]
  if isinstance(module, nn.Einsum):
    return peft.IntEinsum(wrapped=module, dtype=jnp.int4)  # pyrefly: ignore[bad-argument-type]
  else:
    return module


class MyModule(nn.Module):

  @nn.compact
  def __call__(self, x):
    # Call a model twice
    model = nn.Dense(2)
    y0 = model(x)
    y1 = model(x)

    # Call another model
    y2 = nn.Dense(3)(x)

    # Test Einsum
    y3 = nn.Einsum(
        shape=(4, 2, 3),
        einsum_str='bi,imn->bmn',
    )(x)
    return {
        'y0': y0,
        'y1': y1,
        'y2': y2,
        'y3': y3,
    }


def test_quantization_simulation():
  model = MyModule()
  with peft.ModuleInterceptor(_dense_to_quantized):
    out, params = model.init_with_output(
        jax.random.key(0),
        jnp.zeros((1, 4)),
    )

  assert etree.spec_like(params) == etree.spec_like({
      'params': {
          'Dense_0': {
              'kernel': f32[4, 2],
              'bias': f32[2],
          },
          'Dense_1': {
              'kernel': f32[4, 3],
              'bias': f32[3],
          },
          'Einsum_0': {
              'kernel': f32[4, 2, 3],
              'bias': f32[2, 3],
          },
      },
  })
  assert etree.spec_like(out) == etree.spec_like({
      'y0': f32[1, 2],
      'y1': f32[1, 2],
      'y2': f32[1, 3],
      'y3': f32[1, 2, 3],
  })


def test_quantization():
  model = MyModule()
  params = model.init(jax.random.key(0), jnp.zeros((1, 4)))
  params_q = peft.quantize(
      params,
      method=peft.QuantizationMethod.INT4,
      in_place_keys=True,
      checkpoint_kernel_key='kernel',
  )
  with peft.ModuleInterceptor(_dense_to_int4):
    out = model.apply(params_q, jnp.zeros((1, 4)))
  assert etree.spec_like(params_q) == etree.spec_like({
      'params': {
          'Dense_0': {
              'kernel': i4[4, 2],
              'bias': f32[2],
              'scale': f32[1, 2],
          },
          'Dense_1': {
              'kernel': i4[4, 3],
              'bias': f32[3],
              'scale': f32[1, 3],
          },
          'Einsum_0': {
              'kernel': i4[4, 2, 3],
              'bias': f32[2, 3],
              'scale': f32[1, 1, 3],
          },
      },
  })
  assert etree.spec_like(out) == etree.spec_like({
      'y0': f32[1, 2],
      'y1': f32[1, 2],
      'y2': f32[1, 3],
      'y3': f32[1, 2, 3],
  })