-
Notifications
You must be signed in to change notification settings - Fork 996
LLM support: improve VGF export and calibration pipeline #19157
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
base: main
Are you sure you want to change the base?
Changes from all commits
2947953
93c91b6
c288ead
e09cffa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||||||
| # All rights reserved. | ||||||
| # Copyright 2026 Arm Limited and/or its affiliates. | ||||||
| # | ||||||
| # This source code is licensed under the BSD-style license found in the | ||||||
| # LICENSE file in the root directory of this source tree. | ||||||
|
|
@@ -285,11 +286,11 @@ def get_example_inputs(self): | |||||
| if self.use_kv_cache: | ||||||
| return self.get_example_inputs_kvcache_sdpa() | ||||||
| else: | ||||||
| return ( | ||||||
| torch.tensor( | ||||||
| [[1, 2, 3]], dtype=torch.long | ||||||
| ), # tokens, with kv cache our input token length is always just 1 token. | ||||||
| ) | ||||||
| max_len = getattr(self.llm_config.export, "max_seq_length", 3) | ||||||
| max_len = max(3, int(max_len)) | ||||||
| example_tokens = torch.arange(max_len, dtype=torch.int32).unsqueeze(0) | ||||||
|
||||||
| example_tokens = torch.arange(max_len, dtype=torch.int32).unsqueeze(0) | |
| example_tokens = torch.arange(max_len, dtype=torch.long).unsqueeze(0) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -282,23 +282,39 @@ def calibrate_template( | |
| module: torch.fx.GraphModule, tokenizer, prompts: str, max_len: int | ||
| ): | ||
| # TODO: change criteria & support batch inputs if necessary | ||
| pos = torch.tensor(0, dtype=torch.int64) | ||
| pos = 0 | ||
| token_list = tokenizer.encode(prompts, bos=True, eos=False) | ||
|
|
||
| pad_token = getattr(tokenizer, "pad_id", tokenizer.eos_id) | ||
|
|
||
| with torch.no_grad(): | ||
| while token_list[-1] != tokenizer.eos_id and pos < max_len: | ||
| logits = module( | ||
| torch.full((1, 1), token_list[pos]), | ||
| {"input_pos": torch.tensor((pos,))}, | ||
| ) | ||
| if self.use_kv_cache: | ||
| logits = module( | ||
| torch.full((1, 1), token_list[pos]), | ||
| {"input_pos": torch.tensor((pos,))}, | ||
| ) | ||
| else: | ||
| prefix_tokens = list(token_list[: pos + 1]) | ||
| if len(prefix_tokens) < max_len: | ||
| prefix_tokens.extend( | ||
| [pad_token] * (max_len - len(prefix_tokens)) | ||
| ) | ||
| else: | ||
| prefix_tokens = prefix_tokens[:max_len] | ||
|
|
||
| prefix = torch.tensor( | ||
| prefix_tokens, dtype=torch.long | ||
| ).unsqueeze(0) | ||
| logits = module(prefix) | ||
|
|
||
| pos += 1 | ||
| if pos >= len(token_list): | ||
| if self.generate_full_logits: | ||
| token_list.append( | ||
| torch.argmax(logits[:, -1], dim=-1).item() | ||
| ) | ||
| next_token = torch.argmax(logits[:, -1], dim=-1).item() | ||
| else: | ||
| token_list.append(torch.argmax(logits[:], dim=-1).item()) | ||
| next_token = torch.argmax(logits[:], dim=-1).item() | ||
| token_list.append(next_token) | ||
|
|
||
| calibrate_template( | ||
| module=prepared_module, | ||
|
|
@@ -307,26 +323,31 @@ def calibrate_template( | |
| max_len=calibration_seq_length, | ||
| ) | ||
|
|
||
| eval_wrapper = GraphModuleEvalWrapper( | ||
| model=prepared_module, | ||
| tokenizer=tokenizer, | ||
| max_seq_length=calibration_seq_length, | ||
| use_kv_cache=self.use_kv_cache, | ||
| generate_full_logits=self.generate_full_logits, | ||
| enable_dynamic_shape=self.enable_dynamic_shape, | ||
| ) | ||
|
|
||
| # Evaluate the model | ||
| with torch.no_grad(): | ||
| eval_results = simple_evaluate( | ||
| model=eval_wrapper, | ||
| tasks=calibration_tasks, | ||
| limit=calibration_limit, | ||
| if calibration_tasks: | ||
| eval_wrapper = GraphModuleEvalWrapper( | ||
| model=prepared_module, | ||
| tokenizer=tokenizer, | ||
| max_seq_length=calibration_seq_length, | ||
| use_kv_cache=self.use_kv_cache, | ||
| generate_full_logits=self.generate_full_logits, | ||
| enable_dynamic_shape=self.enable_dynamic_shape, | ||
| # The exported graph can contain ops like aten.full.default | ||
| # without explicit device, which default to CPU and can | ||
| # trigger device-mismatch errors when lm_eval runs on CUDA. | ||
| # Calibrate on CPU for stability. | ||
| device="cpu", | ||
| ) | ||
|
|
||
| for task, res in eval_results["results"].items(): | ||
| print(f"{task}: {res}") | ||
| logging.info("Calibration finish...") | ||
| with torch.no_grad(): | ||
| eval_results = simple_evaluate( | ||
| model=eval_wrapper, | ||
| tasks=calibration_tasks, | ||
| limit=calibration_limit, | ||
| ) | ||
|
Comment on lines
+326
to
+346
|
||
|
|
||
| for task, res in eval_results["results"].items(): | ||
| print(f"{task}: {res}") | ||
| logging.info("Calibration finish...") | ||
|
|
||
| def pt2e_quantize(self, quantizers: Optional[List[Quantizer]]) -> "LLMEdgeManager": | ||
| """ | ||
|
|
@@ -360,9 +381,7 @@ def pt2e_quantize(self, quantizers: Optional[List[Quantizer]]) -> "LLMEdgeManage | |
| ) | ||
| # Calibrate | ||
| if ( | ||
| self.calibration_tasks is not None | ||
| and self.calibration_limit is not None | ||
| and self.calibration_seq_length is not None | ||
| self.calibration_seq_length is not None | ||
| and self.calibration_data is not None | ||
| and self.tokenizer_path is not None | ||
| ): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the non-KV-cache +
generate_full_logits=Falsecase, logits are reconstructed by running the model once per prefix position (for pos in range(...)). This makes lm-eval O(seq_len) forward passes per sample, which can become extremely slow at larger seq lengths. If possible, prefer exporting withgenerate_full_logits=Truefor evaluation/calibration, or add a fast path/guard (e.g., only reconstruct up to the required positions or raise with guidance when seq_len is large).