Skip to content
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

Refactor Initialized Model Export #2224

Merged
merged 7 commits into from
Apr 10, 2024
Merged

Refactor Initialized Model Export #2224

merged 7 commits into from
Apr 10, 2024

Conversation

Satrat
Copy link

@Satrat Satrat commented Apr 5, 2024

  • Fixing bug where exporting a model initialized directly from the HuggingFace hub failed
  • Moved config updates for kv-cache injection to the export function, and add support for kv-cache injection export to instantiated models
  • Automatically infer transformers integration based on model properties

Example

from sparseml.transformers import SparseAutoModelForCausalLM, SparseAutoTokenizer
from sparseml.transformers import oneshot
from sparseml.modifiers import SparseGPTModifier
from sparseml import export

ORIG_MODEL_PATH = "roneneldan/TinyStories-1M"
OUTPUT_DIR = "./test_output_here"

model = SparseAutoModelForCausalLM.from_pretrained(ORIG_MODEL_PATH, device_map="auto")
tokenizer = SparseAutoTokenizer.from_pretrained(ORIG_MODEL_PATH)

oneshot(
    model=model,
    tokenizer=tokenizer,
    dataset="open_platypus",
    recipe=SparseGPTModifier(sparsity=0.5),
    output_dir="./test_export_output",
    num_calibration_samples=512,
)

export(
    model=model,
    tokenizer=tokenizer,
    target_path=OUTPUT_DIR
)

Copy link
Contributor

@dbogunowicz dbogunowicz left a comment

Choose a reason for hiding this comment

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

Left a few comments, all in all, great changes!

src/sparseml/export/export.py Show resolved Hide resolved
@@ -129,7 +129,8 @@ def remove_leftover_files(self):
torch_onnx_export_transform, _TorchOnnxExport
), "Expected the first transform from self.transform to be _TorchOnnxExport"
for file in torch_onnx_export_transform.leftover_files:
os.remove(file)
if os.path.exists(file):
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious, why is this change needed now?

Copy link
Author

Choose a reason for hiding this comment

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

Honestly, I am not quite sure. I would occasionally get an error about deleting a file that didn't exist when exporting and this fixed it

:param integration: Optional name of the integration to use. If not provided,
will attempt to infer it from the source_path.
:return: The name of the integration to use for exporting the model.
"""

integration = integration or _infer_integration_from_source_path(source_path)

# attempt to infer transformers based on model attribute
if source_model is not None and hasattr(source_model, "config_class"):
Copy link
Contributor

Choose a reason for hiding this comment

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

why config_class is a deciding attribute here?

Copy link
Author

Choose a reason for hiding this comment

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

This is really just to tell if its a PreTrainedModel, but I didn't want to have to add the transformers dependency to this part of the repo

@bfineran bfineran merged commit 8de19e3 into main Apr 10, 2024
10 of 17 checks passed
@bfineran bfineran deleted the export_instantiated_model branch April 10, 2024 14:41
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.

4 participants