Skip to content

Commit 51b10a7

Browse files
nutsiepullytensorflower-gardener
authored andcommitted
Raise error if quantizing non built model.
PiperOrigin-RevId: 257695954
1 parent 9d0ca18 commit 51b10a7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

tensorflow_model_optimization/python/core/quantization/keras/quantize_emulate.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def quantize_apply(model):
172172
'annotated with `quantize_annotate`. There are no layers '
173173
'to quantize.')
174174

175+
if not model.built:
176+
raise ValueError('quantization cannot be applied to a model which has not'
177+
'been built yet. Please call `model.build(input_shape)`'
178+
'before quantizing your model.')
179+
175180
def _clone_model_with_weights(model_to_clone):
176181
cloned_model = keras.models.clone_model(model_to_clone)
177182
cloned_model.set_weights(model_to_clone.get_weights())

tensorflow_model_optimization/python/core/quantization/keras/quantize_emulate_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def testRaisesErrorNoAnnotatedLayers_Functional(self):
179179
with self.assertRaises(ValueError):
180180
quantize_emulate.quantize_apply(model)
181181

182+
def testRaisesErrorModelNotBuilt(self):
183+
model = keras.Sequential([
184+
quantize_annotate(keras.layers.Dense(10), **self.quant_params1)])
185+
186+
self.assertFalse(model.built)
187+
with self.assertRaises(ValueError):
188+
quantize_emulate.quantize_apply(model)
189+
182190
# Quantization Apply Tests
183191

184192
def _get_annotated_sequential_model(self):

0 commit comments

Comments
 (0)