Harden vLLM and patch utilities for edge cases#454
Harden vLLM and patch utilities for edge cases#454danielhanchen wants to merge 1 commit intomainfrom
Conversation
Summary of ChangesHello @danielhanchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness and stability of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2149cc333
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _write_fallback(output_path, format_type, storage_format): | ||
| qa_pairs = _fallback_pairs() | ||
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | ||
| if storage_format == "hf": |
There was a problem hiding this comment.
Avoid os.makedirs on empty output dir in fallback
When output_path is just a filename in the current working directory (e.g., "out.jsonl"), os.path.dirname(output_path) returns an empty string. Calling os.makedirs("") raises FileNotFoundError, so the new empty-input fallback path crashes before writing any data. This means the intended fallback fails for the common case of writing to CWD. Guard the mkdir with a non-empty dirname or default to "." before creating the directory.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Code Review
This pull request introduces a series of hardening measures and patches for edge cases, primarily focusing on vLLM and related utilities. The changes are well-implemented and significantly improve the robustness and user-friendliness of the library. Key improvements include relaxing vLLM standby checks, adding a fallback to Hugging Face's generate method, fixing FP8Linear construction, and providing fallbacks for missing quant_state keys. The code is more resilient to various edge cases, preventing potential crashes. I have one suggestion to refactor a function for better maintainability.
| def _write_fallback(output_path, format_type, storage_format): | ||
| qa_pairs = _fallback_pairs() | ||
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | ||
| if storage_format == "hf": | ||
| if format_type == "jsonl": | ||
| formatted_pairs = qa_pairs | ||
| elif format_type == "alpaca": | ||
| formatted_pairs = [ | ||
| {"instruction": p["question"], "input": "", "output": p["answer"]} | ||
| for p in qa_pairs | ||
| ] | ||
| elif format_type == "ft": | ||
| formatted_pairs = [ | ||
| { | ||
| "messages": [ | ||
| {"role": "system", "content": "You are a helpful assistant."}, | ||
| {"role": "user", "content": p["question"]}, | ||
| {"role": "assistant", "content": p["answer"]}, | ||
| ] | ||
| } | ||
| for p in qa_pairs | ||
| ] | ||
| elif format_type == "chatml": | ||
| formatted_pairs = [ | ||
| { | ||
| "messages": [ | ||
| {"role": "system", "content": "You are a helpful AI assistant."}, | ||
| {"role": "user", "content": p["question"]}, | ||
| {"role": "assistant", "content": p["answer"]}, | ||
| ] | ||
| } | ||
| for p in qa_pairs | ||
| ] | ||
| else: | ||
| raise ValueError(f"Unknown format type: {format_type}") | ||
| return to_hf_dataset(formatted_pairs, output_path) | ||
|
|
||
| if format_type == "jsonl": | ||
| return to_jsonl(qa_pairs, output_path) | ||
| if format_type == "alpaca": | ||
| return to_alpaca(qa_pairs, output_path) | ||
| if format_type == "ft": | ||
| return to_fine_tuning(qa_pairs, output_path) | ||
| if format_type == "chatml": | ||
| return to_chatml(qa_pairs, output_path) | ||
| raise ValueError(f"Unknown format type: {format_type}") |
There was a problem hiding this comment.
The _write_fallback function uses a long if/elif chain to handle different format types. This can be refactored to use dictionaries for mapping format types to their respective handler functions. This would make the code more readable, maintainable, and easier to extend with new formats.
def _write_fallback(output_path, format_type, storage_format):
qa_pairs = _fallback_pairs()
os.makedirs(os.path.dirname(output_path), exist_ok=True)
if storage_format == "hf":
formatters = {
"jsonl": lambda pairs: pairs,
"alpaca": lambda pairs: [
{"instruction": p["question"], "input": "", "output": p["answer"]} for p in pairs
],
"ft": lambda pairs: [
{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": p["question"]},
{"role": "assistant", "content": p["answer"]},
]
}
for p in pairs
],
"chatml": lambda pairs: [
{
"messages": [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": p["question"]},
{"role": "assistant", "content": p["answer"]},
]
}
for p in pairs
],
}
formatter = formatters.get(format_type)
if formatter is None:
raise ValueError(f"Unknown format type for hf storage: {format_type}")
return to_hf_dataset(formatter(qa_pairs), output_path)
savers = {
"jsonl": to_jsonl,
"alpaca": to_alpaca,
"ft": to_fine_tuning,
"chatml": to_chatml,
}
saver = savers.get(format_type)
if saver is None:
raise ValueError(f"Unknown format type: {format_type}")
return saver(qa_pairs, output_path)| if sig_match: | ||
| sig_text = sig_match.group(1) | ||
| replaced_sig = re.sub( | ||
| r"(?<!\\*)\\bkwargs\\b(\\s*=\\s*[^,\\n\\)]*)?(?=\\s*,\\s*\\*\\*kwargs)", |
| if def_match: | ||
| insert_at = def_match.end() | ||
| new_source = new_source[:insert_at] + "\\n kwargs = kwargs_\\n" + new_source[insert_at:] | ||
| pass |
| raise | ||
|
|
||
| patch_function(save_as, "convert_format", convert_format) | ||
| pass |
| TEMPORARY_PATCHES.append(patch_CsmDepthDecoderForCausalLM_forward) | ||
|
|
||
|
|
||
| def patch_CsmModel_forward(): |
| TEMPORARY_PATCHES.append(patch_CsmForConditionalGeneration_forward) | ||
|
|
||
|
|
||
| def patch_synthetic_data_kit_convert_format(): |
| TEMPORARY_PATCHES.append(patch_CsmForConditionalGeneration_merge) | ||
|
|
||
|
|
||
| def patch_GptOss_init_weights(): |
|
|
||
| original = gpt_oss.GptOssPreTrainedModel._init_weights | ||
|
|
||
| def _init_weights(self, module): |
| TEMPORARY_PATCHES.append(patch_GptOss_init_weights) | ||
|
|
||
|
|
||
| def patch_peft_bnb_dispatch(): |
| fix_4bit_weight_quant_state_from_module(self) | ||
| try: | ||
| fix_4bit_weight_quant_state_from_module(self) | ||
| except AssertionError: |
Summary
Testing