Programming Assignment: Optimizing Models for Metro City's Smart Fleet - Exercise 1 edge case errors

I am having errors when the unittest in exercise 1, prune_model, tests the edge cases with amount=0.0 and amount=1.0, my error here is:

Failed test case: Missing pruning reparam attributes after amount=0.0.
Expected: weight_orig and weight_mask present
Got: has weight_orig? False, weight_mask? False

Failed test case: Missing pruning reparam attributes after amount=0.0.
Expected: weight_orig and weight_mask present
Got: has weight_orig? False, weight_mask? False

Failed test case: Missing pruning reparam attributes after amount=1.0.
Expected: weight_orig and weight_mask present
Got: has weight_orig? False, weight_mask? False

Failed test case: Missing pruning reparam attributes after amount=1.0.
Expected: weight_orig and weight_mask present
Got: has weight_orig? False, weight_mask? False

Failed test case: Conv layer missing weight_mask after ln_structured..
Expected: weight_mask present
Got: Absent

Failed test case: Linear layer missing weight_mask after ln_structured..
Expected: weight_mask present
Got: Absent

Failed test case: Expected ValueError for invalid mode, but none was raised..
Expected: Raise ValueError
Got: No error

I didn’t touch the for loop condition, I am using hasattr() to check if the module has the “weight” attribute, and comparing the mode string for “l1_unstructured” or “ln_structured”. Also passing the amount parameter into the prune function calls. The prune functions are being called by themselves not assigned to anything just like just like in the “Introduction to Pruning” practice lab. Any ideas? I’m not sure if I can share the whole code.

These errors usually mean your loop body is being skipped entirely. Since the loop doesn’t execute, no pruning is applied (missing weight_orig/weight_mask attributes) and the invalid mode check inside the loop is never reached (no ValueError is raised).

Check these three common issues in your implementation:

  1. Are you checking hasattr(module, “weight”) or did you accidentally write hasattr(model, “weight”)? The overall model does not have a weight attribute, which would cause the loop to skip every layer.
  2. Did you include the “not” in your check? It should skip the module if it does not have the attribute: if not hasattr(module, “weight”): continue.
  3. Make sure you are unpacking the tuple from the helper generator correctly: for _, module in _iter_prunable_modules(model):. If you only bind one variable like for module in …, it becomes a tuple of (name, module) which lacks the weight attribute.

It was that “not” I was missing, thanks a bunch arman!