Pruning Code Mistake #446

Closed
opened 2026-03-22 15:40:13 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @Tess314 on GitHub (Oct 28, 2025).

Originally assigned to: @profvjreddi on GitHub.

In the Model Optimizations chapter, in the Pruning section, there is a mistake in Listing 1:

Image

The results section states that 4 out of 9 weights should remain. This is not the case, due to the mask.
torch.abs(weights) takes the absolute value of each weight, so negative numbers like -0.9 become 0.9.
'>=threshold' then keeps any value whose magnitude is greater than or equal to 0.1, regardless of sign, meaning both the positive and negative large numbers stay. In short, the code leaves 6 out of 9 weights remaining, not 4.

Here is a correction:

Image

The corrected code ensures negative weights are removed. This leaves the user with 3 out of 9 weights remaining (33% sparsity instead of 44%). I am aware the listing states this code example should use magnitude-based pruning, but as the results were incorrect I am providing this correction in which negative signs are accounted for.

Hope this helps!

-Tess

Originally created by @Tess314 on GitHub (Oct 28, 2025). Originally assigned to: @profvjreddi on GitHub. In the Model Optimizations chapter, in the Pruning section, there is a mistake in Listing 1: <img width="400" height="278" alt="Image" src="https://github.com/user-attachments/assets/cd244d0e-8593-4c49-9603-2183bc45208f" /> The results section states that 4 out of 9 weights should remain. This is not the case, due to the mask. torch.abs(weights) takes the absolute value of each weight, so negative numbers like -0.9 become 0.9. '>=threshold' then keeps any value whose magnitude is greater than or equal to 0.1, regardless of sign, meaning both the positive and negative large numbers stay. In short, the code leaves 6 out of 9 weights remaining, not 4. Here is a correction: <img width="362" height="328" alt="Image" src="https://github.com/user-attachments/assets/dde061c0-ac7a-45eb-b890-2914444ba5cd" /> The corrected code ensures negative weights are removed. This leaves the user with 3 out of 9 weights remaining (33% sparsity instead of 44%). I am aware the listing states this code example should use magnitude-based pruning, but as the results were incorrect I am providing this correction in which negative signs are accounted for. Hope this helps! -Tess
GiteaMirror added the area: booktype: erratatype: code labels 2026-03-22 15:40:13 -05:00
Author
Owner

@profvjreddi commented on GitHub (Oct 28, 2025):

Hi @Tess314 --

Thank you so much for catching this and taking the time to explain it so clearly. I love it when the community helps find these kinds of issues. I write these sections over time, and sometimes little mistakes sneak through, so your note is incredibly helpful. 🙏

I’ll take a closer look at your explanation about the mask behavior later this evening and put in a fix today. When I do, I’ll make sure to list you as a co-author on the change so you get credit. I love recognizing people for their help.

And of course, you’re also welcome to open a PR if you’d like to make the fix directly.

Appreciate the sharp eye and contribution!

Cheerios,
VJ

@profvjreddi commented on GitHub (Oct 28, 2025): Hi @Tess314 -- Thank you so much for catching this and taking the time to explain it so clearly. I love it when the community helps find these kinds of issues. I write these sections over time, and sometimes little mistakes sneak through, so your note is incredibly helpful. 🙏 I’ll take a closer look at your explanation about the mask behavior later this evening and put in a fix today. When I do, I’ll make sure to list you as a co-author on the change so you get credit. I love [recognizing people](https://www.mlsysbook.ai/contents/frontmatter/acknowledgements/acknowledgements.html#sec-acknowledgements-contributors-fe00) for their help. And of course, you’re also welcome to open a PR if you’d like to make the fix directly. Appreciate the sharp eye and contribution! Cheerios, VJ
Author
Owner

@Tess314 commented on GitHub (Oct 29, 2025):

Hi @profvjreddi, have you had a chance to check this yet?

I appreciate the recognition :)

Tess

@Tess314 commented on GitHub (Oct 29, 2025): Hi @profvjreddi, have you had a chance to check this yet? I appreciate the recognition :) Tess
Author
Owner

@profvjreddi commented on GitHub (Oct 29, 2025):

Hey @Tess314, I pushed a fix. Could you review it here - dev? This is the dev version. I do a release once every few weeks and push that to the main branch. I might do that later this weekend.

By the way, look who is on the acknowledgments page on the dev branch :)

Image

Your picture doesn't show up because I don't have your GitHub email.

Unless I associate your GitHub account email with your GitHub ID, it won't officially recognize your account. If you want to share your GitHub email, I can update it. You can also email me at vj@eecs.harvard.edu

@profvjreddi commented on GitHub (Oct 29, 2025): Hey @Tess314, I pushed a fix. Could you review it here - [dev](https://harvard-edge.github.io/cs249r_book_dev/)? This is the dev version. I do a release once every few weeks and push that to the main branch. I might do that later this weekend. By the way, look who is on the acknowledgments page on the dev branch :) <img width="1392" height="1521" alt="Image" src="https://github.com/user-attachments/assets/117f1152-8f58-4ebd-8ab5-f9f771cea2d9" /> Your picture doesn't show up because I don't have your GitHub email. Unless I associate your GitHub account email with your GitHub ID, it won't officially recognize your account. If you want to share your GitHub email, I can update it. You can also email me at vj@eecs.harvard.edu
Author
Owner

@Tess314 commented on GitHub (Oct 29, 2025):

Hi @profvjreddi,

The fix looks great! Glad I was able to help.

I appreciated you adding me to the acknowledgements page :)

My GitHub email is tesswattelders@gmail.com

Kind regards,
Tess

@Tess314 commented on GitHub (Oct 29, 2025): Hi @profvjreddi, The fix looks great! Glad I was able to help. I appreciated you adding me to the acknowledgements page :) My GitHub email is tesswattelders@gmail.com Kind regards, Tess
Author
Owner

@profvjreddi commented on GitHub (Oct 30, 2025):

Closing, fixed in c9eaffc9a7

@profvjreddi commented on GitHub (Oct 30, 2025): Closing, fixed in c9eaffc9a7696ffc31912a287178a942d4f082b2
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#446