Skip to content

feat: add pre-smash-hook for model preparation#309

Merged
simlang merged 5 commits intomainfrom
feat/introduce-pre-smash-setup
Aug 29, 2025
Merged

feat: add pre-smash-hook for model preparation#309
simlang merged 5 commits intomainfrom
feat/introduce-pre-smash-setup

Conversation

@simlang
Copy link
Copy Markdown
Member

@simlang simlang commented Aug 20, 2025

Description

This PR introduces new functionality, such that any algorithm can implement a setup function which is called before any smashing algorithm is applied.

To do pre-smash-setup an algorithm has to override _pre_smash_setup to apply in-place operations on the model based on information provided in the SmashConfig for that specific algorithm.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

If no algorithm overrides _pre_smash_setup there should be no change of functionality compared to the current version.
Therefore to test, I successfully ran the existing tests

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Copy link
Copy Markdown
Collaborator

@gsprochette gsprochette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks almost good to me, I left a few comments:

  • some docstring and name related stuff
  • a discussion about undoing the setup after the smash.

It would be nice to have a unit test to make sure that the pre_smash_setup is executed when the aglo is activated and not executed otherwise, this can be done with a monkey patch of an existing method.

for current_group in ALGORITHM_GROUPS:
algorithm = smash_config[current_group]
if algorithm is not None:
check_algorithm_availability(algorithm, current_group, algorithm_dict)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call is repeated many times: 1. in this function, 2. in the smash loop and 3. in check_model_compatibility. Should we take this opportunity to define a check_active_algorithm_availabilities function and running it at the beginning of smash?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure if this PR is the correct place for it? but generally I agree - @johannaSommer thoughts on this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's not really the PR for it could we clean this up while we're working on this part of the code? Meaning in a follow up PR (my favorite option) or directly here. Would you be ok with that? Johanna do you have a strong opinion about this?

"""
return []

def pre_smash_setup(self, model: Any, smash_config: SmashConfig) -> None:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an argument for a post_smash_? as well? For example if the pre_smash_setup computes something based on the pre-smashed model and stores it in smash_config, a post_smash could be the opportunity to destroy it and restore the smash_config to its original state before the smash function was applied. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there could be an argument for it.
For e.g. recovery it could make sense to create the dataset in the pre-smash, replace the current data and then in a potential post_smash insert the original one again.
What do you think @johannaSommer?

check_model_compatibility(model, smash_config)

# perform any necessary setup steps before the smashing process begins
pre_smash_setup(model, smash_config)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now this only allows inplace operation. After discussion with Simon we can leave it like that for now but should add documentation/justification as to why

# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would slightly prefer not putting this into a separate file and putting it into the "compatibility checks" file as there we have all the pre-smash checks and setup (e.g. device casting). If you feel the naming of the file is a problem feel free to change it to pre_smash_setup or so

Copy link
Copy Markdown
Collaborator

@gsprochette gsprochette Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either options. The point is to keep both the engine dir and the pre_smash_setup file manageable. Each direction is a problem only if we have too many of those setup functions, in which case we can split them into multiple files in a pres_smash_setup directory instead. As long as we don't have that sort of problem I don't have a strong opinion :)

algorithm = smash_config[current_group]
if algorithm is not None:
check_algorithm_availability(algorithm, current_group, algorithm_dict)
algorithm_dict[current_group][algorithm].pre_smash_setup(model, smash_config)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge with existing function in compatibility check and possible adjust naming? -> pre_smash_hook?


def _pre_smash_hook(self, model: Any, smash_config: SmashConfigPrefixWrapper) -> None:
"""
Function to be overridden by an algorithm to perform a pre-smash setup.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-smash-hook instead of setup in the doc?

model, smash_config = model_fixture

pre_smash_hook_called = False
def mock_pre_smash_hook(self: LLMInt8Quantizer, model: Any, smash_config: SmashConfigPrefixWrapper) -> None:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it!

@gsprochette gsprochette self-requested a review August 28, 2025 14:07
Copy link
Copy Markdown
Collaborator

@gsprochette gsprochette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adressing every comment, this looks super good :) I may have found a typo in the _pre_smash_hook docstring but other than that it's ready to merge 🤩

The post_smash_hook thing, we can probably wait until we have a case where we need it, or simply add it in a follow-up PR.

Copy link
Copy Markdown
Member

@johannaSommer johannaSommer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Simon! Agree with @gsprochette that we should keep the post smash hook in mind but since no algorithm needs it at the moment let's keep it for later :)

@simlang simlang changed the title Add pre-smash-setup for model preparation feat: add pre-smash-setup for model preparation Aug 29, 2025
@simlang simlang changed the title feat: add pre-smash-setup for model preparation feat: add pre-smash-hook for model preparation Aug 29, 2025
@simlang simlang merged commit 309be37 into main Aug 29, 2025
7 checks passed
@simlang simlang deleted the feat/introduce-pre-smash-setup branch August 29, 2025 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants