-
Notifications
You must be signed in to change notification settings - Fork 101
Initial Commit GPT-OSS #485
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c52dbfd
Initial Commit GPT-OSS
hlahkar bc3d704
Update Formatting
hlahkar f3e2553
Update Test Case
hlahkar 1d35ae9
Remove unused variable from test
hlahkar a350ae9
Update model_runner
hlahkar 1928416
Set FUSED_SDPA to 0 for test
hlahkar 596433a
Merge branch 'main' into gpt_oss
hlahkar 2a2968a
Set window_size for fsdpa based on sliding_window
hlahkar 2ecb728
Merge branch 'main' into gpt_oss
hlahkar 051a0c0
Update block calculation for decode
hlahkar d4eee4d
Update pipelined_pa signature
hlahkar 1855f5a
Merge branch 'main' into gpt_oss
hlahkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import os | ||
| import sys | ||
| import vllm | ||
| from vllm.distributed import cleanup_dist_env_and_memory | ||
| from vllm.entrypoints.llm import LLM | ||
| import numpy as np | ||
|
|
||
| RUN_20B_MODEL = True # Set to False to run the 120B model instead | ||
| MODEL_PATH = "lmsys/gpt-oss-20b-BF16" | ||
| MODEL_PATH_120 = "lmsys/gpt-oss-120b-BF16" | ||
| # reference https://github.com/huggingface/transformers/blob/68eb1a9a6353911f491b1c8139eb73d052a8e9b9/tests/models/gpt_oss/test_modeling_gpt_oss.py#L397 | ||
| original_output = "Roses are red, violets are blue, I love you, and I love you too!\n\nRoses are red, vio" | ||
| # reference https://github.com/huggingface/transformers/blob/68eb1a9a6353911f491b1c8139eb73d052a8e9b9/tests/models/gpt_oss/test_modeling_gpt_oss.py#L462 | ||
| original_output_120 = "Roses are red, violets are blue,\nI am a language model, not a human being" | ||
| original_logprobs = [ | ||
| -0.037353515625, | ||
| -0.08154296875, | ||
| -1.21875, | ||
| -1.953125, | ||
| -2.234375, | ||
| -0.96875, | ||
| -1.546875, | ||
| -1.640625, | ||
| -0.93359375, | ||
| -1.609375, | ||
| -1.625, | ||
| -0.85546875, | ||
| -1.7265625, | ||
| ] | ||
| original_logprobs_120 = [ | ||
| -0.90234375, | ||
| -0.66015625, | ||
| -1.546875, | ||
| -2.703125, | ||
| -2.078125, | ||
| -1.21875, | ||
| -2.484375, | ||
| -0.031982421875, | ||
| -0.84765625, | ||
| -1.890625, | ||
| -0.1923828125, | ||
| -2.046875, | ||
| -1.65625, | ||
| ] | ||
|
|
||
|
|
||
| def do_sample(llm: LLM, original_output: str, original_logprobs: list[float], rtol: float, atol: float, max_num_seqs:int) -> list[str]: | ||
| prompts = [ | ||
| "Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?", | ||
| ] * max_num_seqs | ||
|
|
||
| sampling_params = vllm.SamplingParams(temperature=0, | ||
| max_tokens=512, | ||
| logprobs=1 if not PT_PROFILE else None,) | ||
| outputs = llm.generate( | ||
| prompts, | ||
| sampling_params) | ||
|
|
||
| if not PT_PROFILE: | ||
| # Print the outputs. | ||
| generated_texts: list[str] = [] | ||
| logprobs: list[float] = [] | ||
| for output in outputs: | ||
| for probs in output.outputs[0].logprobs: | ||
| logprobs.append(list(probs.values())[0].logprob) | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| generated_texts.append(generated_text) | ||
| print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") | ||
|
|
||
| # assert prompts[0]+generated_texts[0] == original_output, "Generated text does not match the expected output." | ||
| # assert np.allclose(np.array(logprobs[:-1]),np.array(original_logprobs),rtol=rtol, atol=atol), "Logprobs do not match the expected values." | ||
| return generated_texts | ||
| else: | ||
| generated_texts: list[str] = [] | ||
| for output in outputs: | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| generated_texts.append(generated_text) | ||
| print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") | ||
|
|
||
| if __name__ == "__main__": | ||
| DEFAULT_MAX_NUM_SEQS = 1 | ||
| max_num_seqs = int(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_MAX_NUM_SEQS | ||
| # Enable PyTorch profiling when PT_PROFILE env var is set to one of the values (1,true,yes,on) | ||
| _pt_profile_env = os.getenv("PT_PROFILE", "0") | ||
| PT_PROFILE = _pt_profile_env.lower() in ("1", "true", "yes", "on") | ||
|
|
||
| if RUN_20B_MODEL: | ||
| llm = LLM(MODEL_PATH, | ||
| max_num_seqs=8 if not PT_PROFILE else max_num_seqs, | ||
| dtype='bfloat16', | ||
| enforce_eager=True, | ||
| max_model_len=512, | ||
| max_num_batched_tokens=2048, | ||
| tensor_parallel_size=1, | ||
| ) | ||
| if PT_PROFILE: | ||
| import torch | ||
| schedule = torch.profiler.schedule(wait=0, warmup=1, active=1, repeat=1) | ||
| activities = [torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.HPU] | ||
| _profiler = torch.profiler.profile( | ||
| schedule=schedule, | ||
| activities=activities, | ||
| on_trace_ready=torch.profiler.tensorboard_trace_handler("./"), | ||
| record_shapes=False, | ||
| with_stack=False, | ||
| ) | ||
| _profiler.start() | ||
| do_sample(llm, original_output=original_output, | ||
| original_logprobs=original_logprobs, rtol=1e-01, atol=1e-01, max_num_seqs=max_num_seqs) | ||
| _profiler.step() | ||
| do_sample(llm, original_output=original_output, | ||
| original_logprobs=original_logprobs, rtol=1e-01, atol=1e-01, max_num_seqs=max_num_seqs) | ||
| _profiler.step() | ||
| do_sample(llm, original_output=original_output, | ||
| original_logprobs=original_logprobs, rtol=1e-01, atol=1e-01, max_num_seqs=max_num_seqs) | ||
| _profiler.step() | ||
| _profiler.stop() | ||
| else: | ||
| do_sample(llm, original_output=original_output, | ||
| original_logprobs=original_logprobs, rtol=1e-01, atol=1e-01, max_num_seqs=max_num_seqs) | ||
|
|
||
| else: | ||
| llm = LLM(MODEL_PATH_120, | ||
| max_num_seqs=8, | ||
| dtype='bfloat16', | ||
| enforce_eager=False, | ||
| max_model_len=512, | ||
| max_num_batched_tokens=2048, | ||
| tensor_parallel_size=4, | ||
| ) | ||
| do_sample(llm, original_output=original_output_120, | ||
| original_logprobs=original_logprobs_120, rtol=1e-01, atol=3e-01, max_num_seqs=max_num_seqs) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import vllm | ||
| import os | ||
| from vllm.entrypoints.llm import LLM | ||
|
|
||
| RUN_20B_MODEL = True # Set to False to run the 120B model instead | ||
| MODEL_PATH = "lmsys/gpt-oss-20b-BF16" | ||
| MODEL_PATH_120 = "lmsys/gpt-oss-120b-BF16" | ||
| # reference https://github.com/huggingface/transformers/blob/68eb1a9a6353911f491b1c8139eb73d052a8e9b9/tests/models/gpt_oss/test_modeling_gpt_oss.py#L397 | ||
| original_output = "Roses are red, violets are blue, I love you, and I love you too!\n\nRoses are red, vio" | ||
| # reference https://github.com/huggingface/transformers/blob/68eb1a9a6353911f491b1c8139eb73d052a8e9b9/tests/models/gpt_oss/test_modeling_gpt_oss.py#L462 | ||
| original_output_120 = "Roses are red, violets are blue,\nI am a language model, not a human being" | ||
|
|
||
|
|
||
| def do_sample(llm: LLM, original_output: str, rtol: float, atol: float, max_num_seqs: int) -> list[str]: | ||
| prompts = [ | ||
| "Roses are red, violets", | ||
| ] * max_num_seqs | ||
|
|
||
| sampling_params = vllm.SamplingParams( | ||
| temperature=0, | ||
| max_tokens=20, | ||
| ) | ||
| outputs = llm.generate(prompts, sampling_params) | ||
|
|
||
| # Print the outputs. | ||
| generated_texts: list[str] = [] | ||
| for output in outputs: | ||
| prompt = output.prompt | ||
| generated_text = output.outputs[0].text | ||
| generated_texts.append(generated_text) | ||
| print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") | ||
|
|
||
| assert prompts[0] + generated_texts[0] == original_output, "Generated text does not match the expected output." | ||
| return generated_texts | ||
|
|
||
|
|
||
| expected_output = [ | ||
| "are blue, I love you, and I love you too.\n\nRoses are red, vio" # noqa: E501 | ||
| ] | ||
|
|
||
|
|
||
| def _test_gpt_oss(): | ||
| """Main function that sets up and runs the prompt processing.""" | ||
| if RUN_20B_MODEL: | ||
| llm = LLM( | ||
| MODEL_PATH, | ||
| max_num_seqs=8, | ||
| dtype='bfloat16', | ||
| enforce_eager=True, | ||
| max_model_len=512, | ||
| max_num_batched_tokens=2048, | ||
| tensor_parallel_size=1, | ||
| ) | ||
| generated_texts = do_sample(llm, original_output=original_output, rtol=1e-01, atol=1e-01, max_num_seqs=1) | ||
| else: | ||
| llm = LLM( | ||
| MODEL_PATH_120, | ||
| max_num_seqs=8, | ||
| dtype='bfloat16', | ||
| enforce_eager=False, | ||
| max_model_len=512, | ||
| max_num_batched_tokens=2048, | ||
| tensor_parallel_size=4, | ||
| ) | ||
| generated_texts = do_sample(llm, original_output=original_output_120, rtol=1e-01, atol=1e-01, max_num_seqs=1) | ||
| assert generated_texts == expected_output | ||
|
|
||
|
|
||
| def test_gpt_oss_1x(): | ||
| os.environ['PT_HPU_ENABLE_FUSED_SDPA_SINK'] = '1' | ||
| os.environ['PT_HPU_QKV_SLICE_SEQ_LEN_THLD'] = '64' | ||
| os.environ['PT_HPU_SDPA_BR_FACTOR'] = '64' | ||
| os.environ['PT_HPU_SDPA_BC_FACTOR'] = '64' | ||
| os.environ['PT_HPU_SDPA_QKV_SLICE_MODE_FWD'] = '1' | ||
| os.environ['VLLM_FUSEDSDPA_SLIDE_THLD'] = '0' | ||
| _test_gpt_oss() | ||
| os.environ['PT_HPU_ENABLE_FUSED_SDPA_SINK'] = '0' | ||
| os.environ['PT_HPU_QKV_SLICE_SEQ_LEN_THLD'] = '1024' | ||
| os.environ['PT_HPU_SDPA_BR_FACTOR'] = '1024' | ||
| os.environ['PT_HPU_SDPA_BC_FACTOR'] = '1024' | ||
| os.environ['PT_HPU_SDPA_QKV_SLICE_MODE_FWD'] = '0' | ||
| os.environ['VLLM_FUSEDSDPA_SLIDE_THLD'] = '8192' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Assertion compares single generated text with expected output incorrectly. The function returns a list but only validates the first element earlier. This assertion will fail unless
generated_textscontains exactly one element matchingexpected_output[0]. Considerassert generated_texts[0] == expected_output[0]orassert generated_texts == expected_outputafter validating the list length.