C3M4 Assignment Exercise 4 Test Failure

I am encountering a strange error in the test for Exercise 4 in the Module 4 assignment of Course 3. In particular, the error is the following. I am able to receive 9/10 points for this portion of the assignment, and all other functionality passes the tests and receives a perfect score from the grader.

Failed test case: No FakeQuantize modules found; prepare_qat likely not applied..
Expected: ≥ 1 FakeQuantize modules in the graph
Got: 0

The output from the validation cell is:
Base Model loaded and wrapped
Model prepared for qat
Epoch 1/1 - Loss: 0.0198 - Val Acc: 1.0000: 100%
1/1 [07:10<00:00, 430.84s/it]
New best accuracy: 1.0000, saved model to best_model.pt

Training completed:
Best accuracy: 1.0000
Final accuracy: 1.0000
Final model saved to final_model.pt
Model converted to int8
Saved quantized model checkpoint to quantized_int8_model.pt
Testing model on cpu
Test accuracy in base model: 0.99%

Int8 model test accuracy: 0.98%

Inference time comparison:
Base model: 0.9327 seconds per batch
Int8 model: 0.3946 seconds per batch
Speed improvement: 57.7%

Model size comparison:
Base model: 42.72 MB
Int8 model: 10.79 MB
Size reduction: 74.7%

Which is comparable but also differs in important ways from the expected output:
*Base Model loaded and wrapped
Model prepared for qat

New best accuracy: 0.9519, saved model to best_model.pt

New best accuracy: 0.9667, saved model to best_model.pt

Training completed:
Best accuracy: 0.9667
Final accuracy: 0.9667
Final model saved to final_model.pt
Model converted to int8
Saved quantized model checkpoint to quantized_int8_model.pt
Testing model on cpu

Test accuracy in base model: 0.97%

Int8 model test accuracy: 0.96%

Inference time comparison:
Base model: 0.0346 seconds per batch
Int8 model: 0.0193 seconds per batch
Speed improvement: 44.2%

Model size comparison:
Base model: 512.22 MB
Int8 model: 128.31 MB
Size reduction: 75.0%
*
Would anyone be able to guide me in understanding what might be wrong with my implementation? Thank you in advance!

Hi. Welcome to the community.
Did your code pass the exercise 4 unit tests?

I suggest you try out the 4 parts again:

“”"
### START CODE HERE ###
# 1) Work on a copy; do not mutate the original

# 2) Fuse eligible modules (best-effort; safe no-op if unsupported)

# 3) Attach default QAT qconfig

# 4) Prepare for QAT (insert observers/fake-quant)

### END CODE HERE ###
return qat

Use the provided instructions and the added code hints :backhand_index_pointing_down::

Note: The original model must remain unmodified; the returned module must be in train() mode.

The function has the following stages:

  1. Clone & Switch to Train Mode (No Mutation)
  • Create a deep copy of the input model so the original remains intact.
  • Put the copy in training mode: .train() (QAT requires training mode).
  1. Select Quantized Backend
  • Set torch.backends.quantized.engine to the requested backend if available.
  • Defaults: “fbgemm” (x86) or “qnnpack” (ARM).
  • If unsupported, keep the runtime’s current engine (best-effort).
  1. Fuse Eligible Modules (Best-Effort)
  • Call the created helper fuse_model_inplace(qat) to fuse common patterns like (Conv, BN, ReLU), (Conv, BN), (Conv, ReLU), (Linear, ReLU).
  1. Insert Observers & Fake-Quant (Eager QAT Prepare)
  • Call aoq.prepare_qat(qat, inplace=True) to add observers and fake-quant modules throughout the network.
  • These modules simulate quantization effects during training.
  • Ensure the returned module is in train() mode and ready for QAT fine-tuning.

Return the new (deep-copied) QAT-ready model.

The output of this function—a QAT-ready model—will then be fine-tuned for a few epochs. During training, the inserted observers and fake-quantization modules learn appropriate scales/zero-points, enabling a high-accuracy post-training convert to INT8 later.

If you still cant pass the tests, click on my name and send me a copy of your notebook so I can help you troubleshoot.

1 Like

As seen in your notebook, it was because you added some extra lines not in the placeholder code. When attempting the assignments, only replace the Nones and make sure to not add any extra code. You may be correct, but the grader is programmed to expect certain outputs.

2 Likes