-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Unit test for bias add kernel #2298
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e1a4f6
added unit test
mrwyattii d9cd509
Merge branch 'master' into add-bias-add-test
mrwyattii f70d958
Update pt_binding.cpp
mrwyattii 1c18a4f
formatting
mrwyattii 30e02d1
Update test_bias_add.py
mrwyattii b206968
Merge branch 'master' into add-bias-add-test
mrwyattii 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
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,48 @@ | ||
| import pytest | ||
| import torch | ||
| import deepspeed | ||
| from deepspeed.ops.op_builder import InferenceBuilder | ||
|
|
||
| if not deepspeed.ops.__compatible_ops__[InferenceBuilder.NAME]: | ||
| pytest.skip("Inference ops are not available on this system", | ||
| allow_module_level=True) | ||
|
|
||
| inference_module = None | ||
| torch_minor_version = None | ||
|
|
||
|
|
||
| def allclose(x, y): | ||
| assert x.dtype == y.dtype | ||
| rtol, atol = {torch.float32: (5e-4, 5e-5), torch.float16: (3e-2, 2e-3)}[x.dtype] | ||
| return torch.allclose(x, y, rtol=rtol, atol=atol) | ||
|
|
||
|
|
||
| def run_bias_add_reference(activations, bias): | ||
| return activations + bias | ||
|
|
||
|
|
||
| def run_bias_add_ds(activations, bias): | ||
| global inference_module | ||
| if inference_module is None: | ||
| inference_module = InferenceBuilder().load() | ||
| if activations.dtype == torch.float16: | ||
| return inference_module.bias_add_fp16(activations, bias) | ||
| else: | ||
| return inference_module.bias_add_fp32(activations, bias) | ||
|
|
||
|
|
||
| @pytest.mark.inference | ||
| @pytest.mark.parametrize("batch", [1, 2]) | ||
| @pytest.mark.parametrize("sequence", [1, 128, 255]) | ||
| @pytest.mark.parametrize("channels", [512, 1232, 4096]) | ||
| @pytest.mark.parametrize("dtype", [torch.float16, torch.float32], ids=["fp16", "fp32"]) | ||
| def test_bias_add(batch, sequence, channels, dtype): | ||
| activations_ds = torch.randn((batch, sequence, channels), dtype=dtype, device='cuda') | ||
| bias_ds = torch.randn((channels), dtype=dtype, device='cuda') | ||
|
|
||
| activations_ref = activations_ds.clone().detach() | ||
| bias_ref = bias_ds.clone().detach() | ||
|
|
||
| ds_out = run_bias_add_ds(activations_ds, bias_ds) | ||
| ref_out = run_bias_add_reference(activations_ref, bias_ref) | ||
| assert allclose(ds_out, ref_out) |
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.