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

Optimize Memory Usage by Using Exported Program Instead of PyTorch Module in Converter Signature #201

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KennethanCeyer
Copy link

@KennethanCeyer KennethanCeyer commented Sep 6, 2024

Related Issue

Overview

This update enhances memory efficiency during model conversion by replacing the use of the PyTorch module with the exported program in the converter signature.

When defining the signature, the PyTorch model is cleaned up before proceeding with the TFLite conversion process, leading to a memory usage improvement of approximately 7-11%. This is achieved by refining the handling of model tensors, as demonstrated below:

pytorch_model = gemma2.build_2b_model(
    checkpoint_path, kv_cache_max_len=kv_cache_max_len
)
# Tensors used to trace the model graph during conversion.
prefill_tokens = torch.full((1, prefill_seq_len), 0, dtype=torch.long)
prefill_input_pos = torch.arange(0, prefill_seq_len)
decode_token = torch.tensor([[0]], dtype=torch.long)
decode_input_pos = torch.tensor([0], dtype=torch.int64)

quant_config = quant_recipes.full_int8_dynamic_recipe() if quantize else None
converter = (
    ai_edge_torch
      .signature(
          "prefill", pytorch_model, (prefill_tokens, prefill_input_pos)
      )
      .signature("decode", pytorch_model, (decode_token, decode_input_pos))
)

del pytorch_model
gc.collect()

edge_model = converter.convert(quant_config=quant_config)
edge_model.export(output_path)

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.

1 participant