-
Notifications
You must be signed in to change notification settings - Fork 91
Update flop calculator to work for hybrid models #1616
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
Merged
+112
−23
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61e8d84
draft
gautham-kollu b82e9e4
1
gautham-kollu 54d8646
move to functests
gautham-kollu dfdd449
ruff
gautham-kollu 5b26688
1
gautham-kollu 92a9532
Merge branch 'main' into gk/nemotron_h_flops
gautham-kollu 5bb746e
update tests
gautham-kollu 656bb95
formatting
ko3n1g e00fece
Update tests
gautham-kollu fd8dc21
remove deadcode
gautham-kollu 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
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,23 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| #!/bin/bash | ||
| set -xeuo pipefail # Exit immediately if a command exits with a non-zero status | ||
|
|
||
| export CUDA_VISIBLE_DEVICES="0,1" | ||
|
|
||
| uv run coverage run --data-file=/opt/Megatron-Bridge/.coverage --source=/opt/Megatron-Bridge/ --parallel-mode -m pytest \ | ||
| -o log_cli=true -o log_cli_level=INFO -v -s -x -m "not pleasefixme" --tb=short -rA \ | ||
| tests/functional_tests/utils | ||
| coverage combine -q |
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,62 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for flop_utils module.""" | ||
|
|
||
| import importlib | ||
|
|
||
| import pytest | ||
|
|
||
| from megatron.bridge.training.utils.flop_utils import num_floating_point_operations | ||
| from megatron.bridge.utils.vocab_utils import calculate_padded_vocab_size | ||
|
|
||
|
|
||
| class TestFlops: | ||
| @pytest.mark.parametrize( | ||
| "model_family,model_config_func_name, seq_length, vocab_size, expected_flops", | ||
| [ | ||
| ("llama", "llama3_8b_pretrain_config", 8192, 128256, 4.22e14), | ||
| ("llama", "llama3_70b_pretrain_config", 8192, 128256, 3.68e15), | ||
| ("qwen", "qwen3_30b_a3b_pretrain_config", 4096, 151643, 9.42e13), | ||
| ("qwen", "qwen3_235b_a22b_pretrain_config", 4096, 151643, 6.06e14), | ||
| ], | ||
| ) | ||
| def test_flops(self, model_family, model_config_func_name, seq_length, vocab_size, expected_flops): | ||
| """ | ||
| Test the number of floating point operations for a given model family and configuration. | ||
| For GBS=1 | ||
| """ | ||
| model_family_module = importlib.import_module(f"megatron.bridge.recipes.{model_family}") | ||
| cfg = getattr(model_family_module, model_config_func_name)() | ||
| cfg.model.finalize() | ||
|
|
||
| cfg.model.seq_length = seq_length | ||
| cfg.tokenizer.vocab_size = vocab_size | ||
|
|
||
| # Calculate padded vocab size to ensure it's divisible by tensor parallel size | ||
| cfg.tokenizer.padded_vocab_size = calculate_padded_vocab_size( | ||
| cfg.tokenizer.vocab_size, | ||
| cfg.model.make_vocab_size_divisible_by, | ||
| cfg.model.tensor_model_parallel_size, | ||
| ) | ||
| cfg.model.vocab_size = cfg.tokenizer.padded_vocab_size | ||
|
|
||
| actual_num_flops = num_floating_point_operations(cfg, batch_size=1) | ||
| actual_num_flops_rounded = float(f"{actual_num_flops:.2e}") | ||
|
|
||
| assert actual_num_flops_rounded == expected_flops, ( | ||
| f"Expected TFLops: {expected_flops:.2e} but got {actual_num_flops:.2e} with Padded Vocab Size: {cfg.tokenizer.padded_vocab_size} and Sequence len: {cfg.model.seq_length}" | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.