Image Classification
Keras
LiteRT
LiteRT
computer-vision
efficientnet
quantization
mixed-precision
sub-4-bit
tensorflow
Eval Results (legacy)
Instructions to use meatloafer/efficientnet-b0-sub4bit-mixed-precision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use meatloafer/efficientnet-b0-sub4bit-mixed-precision with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://meatloafer/efficientnet-b0-sub4bit-mixed-precision") - LiteRT
How to use meatloafer/efficientnet-b0-sub4bit-mixed-precision with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Upload verified model weights, LiteRT FlatBuffers, DP profiles, code, and model card
2abfed0 verified Download code/adaptive_mixed_precision_qat.py from meatloafer/efficientnet-b0-sub4bit-mixed-precision: direct link, hf CLI and curl.
- Browser
- Download file 7.4 kB
-
https://huggingface.co/meatloafer/efficientnet-b0-sub4bit-mixed-precision/resolve/main/code/adaptive_mixed_precision_qat.py
- Command line
-
hf download hf://meatloafer/efficientnet-b0-sub4bit-mixed-precision/code/adaptive_mixed_precision_qat.py
-
curl -L -o adaptive_mixed_precision_qat.py https://huggingface.co/meatloafer/efficientnet-b0-sub4bit-mixed-precision/resolve/main/code/adaptive_mixed_precision_qat.py
7.4 kB
| """Per-layer fake-quant QAT model construction for adaptive B0 profiles. | |
| W3/W4/W8 kernels use TensorFlow fake quantization with its straight-through | |
| gradient. W16 means FP16-rounded weights with Float32 activations and | |
| arithmetic; it does not claim an FP16 execution path. | |
| """ | |
| from __future__ import annotations | |
| from collections import Counter | |
| from typing import Mapping | |
| import tensorflow as tf | |
| from imagenet_research.mixed_precision_profiles import eligible_weight_layers | |
| from imagenet_research.quantization import ( | |
| FakeQuantConv2D, | |
| FakeQuantDense, | |
| FakeQuantDepthwiseConv2D, | |
| ) | |
| SUPPORTED_BITS = frozenset((3, 4, 8, 16)) | |
| def _fp16_weight_round_ste(kernel): | |
| rounded = tf.cast(tf.cast(kernel, tf.float16), kernel.dtype) | |
| def grad(upstream): | |
| return tf.cast(upstream, kernel.dtype) | |
| return rounded, grad | |
| class Fp16WeightConv2D(tf.keras.layers.Conv2D): | |
| """Round Conv2D weights to FP16 while retaining Float32 convolution math.""" | |
| def convolution_op(self, inputs, kernel): | |
| return super().convolution_op(inputs, _fp16_weight_round_ste(kernel)) | |
| class Fp16WeightDepthwiseConv2D(tf.keras.layers.DepthwiseConv2D): | |
| """Round depthwise weights to FP16 while retaining Float32 convolution math.""" | |
| def call(self, inputs): | |
| kernel = tf.cast(self.kernel, tf.float32) | |
| input_channels = self._get_input_channel(inputs.shape) | |
| output_channels = input_channels * self.depth_multiplier | |
| kernel = tf.reshape( | |
| kernel, [self.kernel_size[0], self.kernel_size[1], output_channels] | |
| ) | |
| kernel = _fp16_weight_round_ste(kernel) | |
| kernel = tf.reshape(kernel, tf.shape(self.kernel)) | |
| kernel = tf.cast(kernel, self.compute_dtype) | |
| inputs = tf.cast(inputs, self.compute_dtype) | |
| outputs = tf.keras.ops.depthwise_conv( | |
| inputs, | |
| kernel, | |
| strides=self.strides, | |
| padding=self.padding, | |
| dilation_rate=self.dilation_rate, | |
| data_format=self.data_format, | |
| ) | |
| if self.use_bias: | |
| if self.data_format == "channels_last": | |
| bias_shape = (1,) * (self.rank + 1) + (output_channels,) | |
| else: | |
| bias_shape = (1, output_channels) + (1,) * self.rank | |
| outputs = tf.keras.ops.add( | |
| outputs, tf.keras.ops.reshape(tf.cast(self.bias, self.compute_dtype), bias_shape) | |
| ) | |
| if self.activation is not None: | |
| outputs = self.activation(outputs) | |
| return outputs | |
| class Fp16WeightDense(tf.keras.layers.Dense): | |
| """Round Dense weights to FP16 while retaining Float32 matmul math.""" | |
| def call(self, inputs, training=None): | |
| kernel = tf.cast(_fp16_weight_round_ste(tf.cast(self.kernel, tf.float32)), self.compute_dtype) | |
| inputs = tf.cast(inputs, self.compute_dtype) | |
| outputs = tf.keras.ops.matmul(inputs, kernel) | |
| if self.bias is not None: | |
| outputs = tf.keras.ops.add(outputs, tf.cast(self.bias, self.compute_dtype)) | |
| if self.activation is not None: | |
| outputs = self.activation(outputs) | |
| return outputs | |
| def _clone_layer_for_bits(layer: tf.keras.layers.Layer, bits: int): | |
| config = layer.get_config() | |
| if bits == 16: | |
| if isinstance(layer, tf.keras.layers.DepthwiseConv2D): | |
| return Fp16WeightDepthwiseConv2D(**config) | |
| if isinstance(layer, tf.keras.layers.Conv2D): | |
| return Fp16WeightConv2D(**config) | |
| if isinstance(layer, tf.keras.layers.Dense): | |
| return Fp16WeightDense(**config) | |
| elif isinstance(layer, tf.keras.layers.DepthwiseConv2D): | |
| return FakeQuantDepthwiseConv2D(num_bits=bits, **config) | |
| elif isinstance(layer, tf.keras.layers.Conv2D): | |
| return FakeQuantConv2D(num_bits=bits, **config) | |
| elif isinstance(layer, tf.keras.layers.Dense): | |
| return FakeQuantDense(num_bits=bits, **config) | |
| return layer.__class__.from_config(config) | |
| def build_adaptive_qat_model( | |
| source_model: tf.keras.Model, bits_by_layer: Mapping[str, int] | |
| ) -> tuple[tf.keras.Model, dict]: | |
| """Clone a model with one registered weight precision per eligible kernel.""" | |
| layers = eligible_weight_layers(source_model) | |
| normalized = {name: int(bits) for name, bits in bits_by_layer.items()} | |
| if set(normalized) != set(layers): | |
| raise ValueError( | |
| "Adaptive QAT map must cover exactly the materialized weighted layers" | |
| ) | |
| invalid = {name: bits for name, bits in normalized.items() if bits not in SUPPORTED_BITS} | |
| if invalid: | |
| raise ValueError(f"Unsupported adaptive QAT bitwidths: {invalid}") | |
| clone = tf.keras.models.clone_model( | |
| source_model, | |
| clone_function=lambda layer: _clone_layer_for_bits(layer, normalized[layer.name]) | |
| if layer.name in normalized | |
| else layer.__class__.from_config(layer.get_config()), | |
| recursive=True, | |
| ) | |
| for layer in source_model.layers: | |
| if layer.weights: | |
| clone.get_layer(layer.name).set_weights(layer.get_weights()) | |
| source_rng = {} | |
| target_layers = {layer.name: layer for layer in clone.layers} | |
| for layer in source_model.layers: | |
| generator = getattr(layer, "seed_generator", None) | |
| if generator is None: | |
| continue | |
| target = getattr(target_layers.get(layer.name), "seed_generator", None) | |
| if target is None: | |
| raise RuntimeError(f"Adaptive QAT clone lacks RNG state for {layer.name}") | |
| target.state.assign(generator.state) | |
| source_rng[layer.name] = [int(item) for item in generator.state.numpy().tolist()] | |
| if target.state.numpy().tolist() != generator.state.numpy().tolist(): | |
| raise RuntimeError(f"Adaptive QAT RNG-state copy failed for {layer.name}") | |
| if not source_rng: | |
| raise RuntimeError("Expected EfficientNet Dropout/stochastic-depth RNG state") | |
| parameter_counts: Counter[int] = Counter() | |
| layer_counts: Counter[int] = Counter() | |
| for name, bits in normalized.items(): | |
| parameter_counts[bits] += int(layers[name].kernel.shape.num_elements()) | |
| layer_counts[bits] += 1 | |
| total = sum(parameter_counts.values()) | |
| audit = { | |
| "layer_bits": dict(sorted(normalized.items())), | |
| "eligible_kernel_layers": len(normalized), | |
| "eligible_kernel_parameters": total, | |
| "layer_counts_by_bits": {str(k): layer_counts[k] for k in sorted(layer_counts)}, | |
| "parameter_counts_by_bits": { | |
| str(k): parameter_counts[k] for k in sorted(parameter_counts) | |
| }, | |
| "weighted_mean_bits_per_kernel_parameter": sum( | |
| bits * count for bits, count in parameter_counts.items() | |
| ) | |
| / total, | |
| "fp16_semantics": "W16 kernels are rounded to IEEE FP16 with an identity straight-through gradient, then used by Float32 arithmetic.", | |
| "low_bit_semantics": "W3/W4/W8 kernels use per-output-channel TensorFlow fake quantization and Float32 arithmetic.", | |
| "dropout_rng": { | |
| "status": "copied_and_verified_before_fit", | |
| "layer_states": source_rng, | |
| }, | |
| } | |
| return clone, audit | |