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

fix past-kv in general LLM exporter #18529

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions onnxruntime/python/tools/transformers/large_model_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ def hook_for_inputs(_, inputs, kwargs):
for idx, (key, value) in enumerate(zip(input_keys, onnx_inputs)):
if type(value) is torch.Tensor:
value.to(model.device)
# Didn't touch past_key_value now, please change it if you want
if "use_cache" in key:
onnx_inputs[idx] = with_past
out = model(sample_inputs[0], attention_mask=sample_inputs[1], use_cache=with_past) if with_past else out

return input_keys, onnx_inputs, out.past_key_values


def move_to_approprate_device(model: nn.Module, sample_inputs_tp: tuple) -> nn.Module:
def move_to_appropriate_device(model: nn.Module, sample_inputs_tp: tuple) -> nn.Module:
"""
According to the model size, we will upload it to
CPU if has no GPU or enough GPU memory,
Expand Down Expand Up @@ -307,7 +307,7 @@ def export_onnx(hf_model: str, cache_dir: Optional[str], onnx_path_str: str, wit
"""
model, sample_inputs_tp = initialize_model_and_sample_inputs(hf_model, cache_dir)

model = move_to_approprate_device(model, sample_inputs_tp)
model = move_to_appropriate_device(model, sample_inputs_tp)

sample_inputs = adapt_inputs_to_device(sample_inputs_tp, next(model.parameters()).device)

Expand Down
Loading