|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 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, |
| 10 | +# software 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 | +from compressed_tensors.quantization import ( |
| 16 | + QuantizationConfig, |
| 17 | + apply_quantization_config, |
| 18 | +) |
| 19 | +from compressed_tensors.quantization.observers.helpers import get_observer_token_count |
| 20 | +from transformers import AutoModelForCausalLM, AutoTokenizer |
| 21 | + |
| 22 | + |
| 23 | +def test_get_observer_token_count(): |
| 24 | + model = AutoModelForCausalLM.from_pretrained("Isotonic/TinyMixtral-4x248M-MoE") |
| 25 | + tokenizer = AutoTokenizer.from_pretrained("Isotonic/TinyMixtral-4x248M-MoE") |
| 26 | + model.eval() |
| 27 | + config = QuantizationConfig( |
| 28 | + format="fakequant", |
| 29 | + quantization_status="calibration", |
| 30 | + config_groups={ |
| 31 | + "group_1": { |
| 32 | + "input_activations": { |
| 33 | + "num_bits": 8, |
| 34 | + "type": "int", |
| 35 | + "symmetric": False, |
| 36 | + "strategy": "tensor", |
| 37 | + }, |
| 38 | + "targets": ["Linear"], |
| 39 | + }, |
| 40 | + }, |
| 41 | + ) |
| 42 | + apply_quantization_config(model, config) |
| 43 | + |
| 44 | + # start calibration |
| 45 | + calib_list = [ |
| 46 | + "I am a string that", |
| 47 | + "is used for calibration so", |
| 48 | + "that your model is", |
| 49 | + "quantized properly.", |
| 50 | + ] |
| 51 | + |
| 52 | + total_num_tokens_observed = 0 |
| 53 | + for calib_sample in calib_list: |
| 54 | + calib_tensor = tokenizer(calib_sample, return_tensors="pt") |
| 55 | + _ = model(**calib_tensor) |
| 56 | + total_num_tokens_observed += len(calib_tensor.input_ids.flatten()) |
| 57 | + |
| 58 | + counter = get_observer_token_count(model) |
| 59 | + |
| 60 | + # filter out the None values |
| 61 | + # (tokens, in the appropriate format, that were not observed by the model) |
| 62 | + counter = {k: v for k, v in counter.items() if v is not None} |
| 63 | + |
| 64 | + # iterate over all the layers in the model where the token count in the proper |
| 65 | + # format is has been observed |
| 66 | + for i in range(model.config.num_hidden_layers): |
| 67 | + # fetch the tokens observed by the router |
| 68 | + tokens_observed_by_router = counter.pop( |
| 69 | + f"model.layers.{i}.block_sparse_moe.gate" |
| 70 | + ) |
| 71 | + assert tokens_observed_by_router == total_num_tokens_observed |
| 72 | + |
| 73 | + # fetch the sum of tokens observed by all the experts |
| 74 | + sum_tokens_observed_by_experts = 0 |
| 75 | + keys_for_this_layer = [ |
| 76 | + k |
| 77 | + for k in counter.keys() |
| 78 | + if f"model.layers.{i}.block_sparse_moe.experts" in k |
| 79 | + ] |
| 80 | + for key in keys_for_this_layer: |
| 81 | + sum_tokens_observed_by_experts += counter.pop(key) |
| 82 | + |
| 83 | + # each Mixtral expert is comprised of 3 linear layers, |
| 84 | + # so we need to multiply by 3 |
| 85 | + assert ( |
| 86 | + sum_tokens_observed_by_experts |
| 87 | + == total_num_tokens_observed * model.config.num_experts_per_tok * 3 |
| 88 | + ) |
| 89 | + |
| 90 | + # there are no more information in the counter |
| 91 | + assert len(counter) == 0 |
0 commit comments