Skip to content

[Transforms] Transform Args, Scheme, and Config #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 28, 2025

Conversation

kylesayrs
Copy link
Contributor

@kylesayrs kylesayrs commented May 21, 2025

Summary

  • Introduce Transform Args, Scheme, Config and Data as structures to define recipes to apply transforms
  • When compared to [Transforms] Transform Arg, Scheme, and Data support #275, the requirement for ModuleTarget, and TransformData has been removed as well as the transform_creation_args and call_args fields
  • Supports spinquant-like transform configs which target non-linear modules such as q_attn and k_cache

TransformArgs

  • Arguments which define how and where a transform should be applied to a model

TransformScheme

  • Scheme used to parameterize a particular transform type and specify how and where it should be applied to the model
  • Includes a list of TransformationArgs onto which the transformation will be applied

TransformConfig

  • Configuration of transforms to be applied to a model. This config is to be serialized within a model's config.json file
  • The keys can be any arbitrary string and a TransformationScheme should be provided for each new transform type.
  • Include preset configs for QUIP/QUIP# and Llama-SpinQuant

Example:

from compressed_tensors.transform import TransformArgs, TransformScheme, TransformsConfig

forward_args = TransformArgs(targets=["Linear"], location="input")  # non-mergable
inverse_args = TransformArgs(targets=["Linear"], location="weight_input", inverse=True)

scheme = TransformScheme(
    type="hadamard",
    apply=[forward_args, inverse_args],
    randomize_modules=True
)

config = TransformsConfig(transform_groups={"v_transform": scheme}
# [transform locations] and {quant locations}:
# input  <- {input @ [input]}
# weight <- {[weight_output] @ weight @ [weight_input]}
# output <- {input @ weight.T @ [output]}

Signed-off-by: Kyle Sayers <[email protected]>
@kylesayrs kylesayrs marked this pull request as ready for review May 21, 2025 13:41
Signed-off-by: Kyle Sayers <[email protected]>
Copy link
Contributor

@brian-dellabetta brian-dellabetta left a comment

Choose a reason for hiding this comment

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

Looking good! a few comments

Copy link
Collaborator

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

Two questions:

  1. How do we maintain the order of the transforms if multiple are being applied to a given input or weight?
  2. What utility will be inferring the transform size and if its right/left?

@kylesayrs kylesayrs marked this pull request as draft May 21, 2025 20:01
Signed-off-by: Kyle Sayers <[email protected]>
@kylesayrs
Copy link
Contributor Author

kylesayrs commented May 21, 2025

@dsikka

  1. EDIT: order is determined by the order that the schemes are applied in, but the order of application doesn't really matter. The matrix multiplications will still cancel out so long as inputs are prepended
  2. FYI @anmarques suggested replacing side ∈ {"left", "right"} with side ∈ {"input", "output"} to clarify how the weight is applied. This is especially nice for quip, since you can directly see the U and V shapes align in the config

To answer the question, this function uses location and side to determine which of the weight to apply to:
https://github.com/neuralmagic/compressed-tensors/pull/316/files#diff-d70cb4816d5b1e1ff0e5f5ea117da8d2e1e106b7419680ba41228a5064dad890R32

@kylesayrs kylesayrs marked this pull request as ready for review May 21, 2025 21:47
Signed-off-by: Kyle Sayers <[email protected]>
kylesayrs added 3 commits May 27, 2025 21:12
Signed-off-by: Kyle Sayers <[email protected]>
Copy link
Collaborator

@dsikka dsikka left a comment

Choose a reason for hiding this comment

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

this looks great, thank you for improving on the original design
great work!

Copy link
Contributor

@brian-dellabetta brian-dellabetta left a comment

Choose a reason for hiding this comment

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

👍

@dsikka dsikka merged commit 8367985 into main May 28, 2025
1 check passed
@dsikka dsikka deleted the kylesayrs/transform-config branch May 28, 2025 19:42
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.

5 participants