This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Add for option to use tensor hooks for Dynamic Linear #198
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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,20 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def x_fail_activation_hooks(request): | ||
use_activation_hooks = request.getfixturevalue("use_activation_hooks") | ||
if use_activation_hooks: | ||
request.node.add_marker( | ||
pytest.mark.xfail(reason="use_activation_hooks is not supported for AOT") | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def x_fail_activation_hooks_with_delayed(request): | ||
linear_type = request.getfixturevalue("linear_type") | ||
use_activation_hooks = request.getfixturevalue("use_activation_hooks") | ||
if use_activation_hooks and linear_type == linear_type.DELAYED: | ||
request.node.add_marker( | ||
pytest.mark.xfail(reason="use_activation_hooks is not supported for AOT") | ||
) |
This file contains 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 |
---|---|---|
|
@@ -50,8 +50,15 @@ def test_preserves_dtype(self) -> None: | |
|
||
|
||
class TestFloat8Linear: | ||
def _test_linear_impl(self, x, m_ref, linear_type: LinearType, emulate: bool): | ||
m_fp8 = get_float8_linear(linear_type, m_ref, emulate) | ||
def _test_linear_impl( | ||
self, | ||
x, | ||
m_ref, | ||
linear_type: LinearType, | ||
emulate: bool, | ||
use_activation_hooks: bool = False, | ||
): | ||
m_fp8 = get_float8_linear(linear_type, m_ref, emulate, use_activation_hooks) | ||
for _ in range(2): | ||
if linear_requires_sync(linear_type): | ||
sync_float8_amax_and_scale_history(m_fp8) | ||
|
@@ -112,7 +119,15 @@ def _test_linear_impl(self, x, m_ref, linear_type: LinearType, emulate: bool): | |
@pytest.mark.parametrize("emulate", [True, False]) | ||
@pytest.mark.parametrize("x_shape", [(16, 16), (2, 16, 16), (3, 2, 16, 16)]) | ||
@pytest.mark.parametrize("linear_type", [LinearType.DELAYED, LinearType.DYNAMIC]) | ||
def test_linear_nobias(self, x_shape, linear_type: LinearType, emulate: bool): | ||
@pytest.mark.parametrize("use_activation_hooks", [True, False]) | ||
@pytest.mark.usefixtures("x_fail_activation_hooks_with_delayed") | ||
def test_linear_nobias( | ||
self, | ||
x_shape, | ||
linear_type: LinearType, | ||
emulate: bool, | ||
use_activation_hooks: bool, | ||
): | ||
if not emulate: | ||
if not torch.cuda.is_available(): | ||
warnings.warn("CUDA not available") | ||
|
@@ -125,16 +140,23 @@ def test_linear_nobias(self, x_shape, linear_type: LinearType, emulate: bool): | |
|
||
x = torch.randn(*x_shape, device="cuda") | ||
m_ref = nn.Linear(16, 32, bias=False, device="cuda") | ||
self._test_linear_impl(x, m_ref, linear_type, emulate) | ||
self._test_linear_impl(x, m_ref, linear_type, emulate, use_activation_hooks) | ||
|
||
@pytest.mark.parametrize("emulate", [True, False]) | ||
@pytest.mark.parametrize("x_shape", [(16, 16), (2, 16, 16), (3, 2, 16, 16)]) | ||
@pytest.mark.parametrize("linear_type", [LinearType.DELAYED, LinearType.DYNAMIC]) | ||
@pytest.mark.parametrize( | ||
"linear_dtype", [torch.float16, torch.bfloat16, torch.float32] | ||
) | ||
@pytest.mark.parametrize("use_activation_hooks", [True, False]) | ||
@pytest.mark.usefixtures("x_fail_activation_hooks_with_delayed") | ||
def test_linear_bias( | ||
self, x_shape, linear_type: LinearType, emulate: bool, linear_dtype: torch.dtype | ||
self, | ||
x_shape, | ||
linear_type: LinearType, | ||
emulate: bool, | ||
linear_dtype: torch.dtype, | ||
use_activation_hooks: bool, | ||
): | ||
if not emulate: | ||
if not torch.cuda.is_available(): | ||
|
@@ -148,25 +170,52 @@ def test_linear_bias( | |
|
||
x = torch.randn(*x_shape, device="cuda", dtype=linear_dtype) | ||
m_ref = nn.Linear(16, 32, bias=True, device="cuda", dtype=linear_dtype) | ||
self._test_linear_impl(x, m_ref, linear_type, emulate) | ||
self._test_linear_impl(x, m_ref, linear_type, emulate, use_activation_hooks) | ||
|
||
m = nn.Linear(32, 16, device="cuda", dtype=linear_dtype) | ||
m = Float8Linear.from_float(m, emulate) | ||
@pytest.mark.parametrize("emulate", [True, False]) | ||
@pytest.mark.parametrize("linear_type", [LinearType.DELAYED, LinearType.DYNAMIC]) | ||
@pytest.mark.parametrize( | ||
"linear_dtype", [torch.float16, torch.bfloat16, torch.float32] | ||
) | ||
@pytest.mark.parametrize("use_activation_hooks", [True, False]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was some testing that was globbed together before this split the test into two |
||
@pytest.mark.usefixtures("x_fail_activation_hooks_with_delayed") | ||
def test_autocast_outputs( | ||
self, | ||
linear_type: LinearType, | ||
emulate: bool, | ||
linear_dtype: torch.dtype, | ||
use_activation_hooks: bool, | ||
): | ||
if not emulate: | ||
if not torch.cuda.is_available(): | ||
warnings.warn("CUDA not available") | ||
pytest.skip() | ||
elif torch.cuda.get_device_capability() < (9, 0): | ||
warnings.warn( | ||
f"CUDA capability {torch.cuda.get_device_capability()} < (9.0)" | ||
) | ||
pytest.skip() | ||
|
||
m_ref = nn.Linear(32, 16, device="cuda", dtype=linear_dtype) | ||
m = get_float8_linear(linear_type, m_ref, emulate, use_activation_hooks) | ||
|
||
# autocast off | ||
x = torch.randn(16, 32, device="cuda", dtype=linear_dtype) | ||
sync_float8_amax_and_scale_history(m) | ||
if linear_requires_sync(linear_type): | ||
sync_float8_amax_and_scale_history(m) | ||
y = m(x) | ||
assert y.dtype == linear_dtype, f"y.dtype is {y.dtype}, expected {linear_dtype}" | ||
|
||
# autocast on | ||
with torch.autocast("cuda"): | ||
sync_float8_amax_and_scale_history(m) | ||
if linear_requires_sync(linear_type): | ||
sync_float8_amax_and_scale_history(m) | ||
y = m(x) | ||
assert y.dtype == torch.half, f"y.dtype is {y.dtype}, expected {torch.half}" | ||
|
||
with torch.autocast("cuda", dtype=torch.bfloat16): | ||
sync_float8_amax_and_scale_history(m) | ||
if linear_requires_sync(linear_type): | ||
sync_float8_amax_and_scale_history(m) | ||
y = m(x) | ||
assert ( | ||
y.dtype == torch.bfloat16 | ||
|
@@ -180,11 +229,6 @@ def test_type_cast(self, linear_type: LinearType, linear_dtype: torch.dtype): | |
emulate = ( | ||
not torch.cuda.is_available() or torch.cuda.get_device_capability() < (9, 0) | ||
) | ||
x_shape = (16, 16) | ||
|
||
x = torch.randn(*x_shape, device="cuda", dtype=linear_dtype) | ||
m_ref = nn.Linear(16, 32, bias=True, device="cuda", dtype=linear_dtype) | ||
self._test_linear_impl(x, m_ref, linear_type, emulate) | ||
|
||
m = nn.Linear(32, 16, device="cuda", dtype=linear_dtype) | ||
m = Float8Linear.from_float(m, emulate) | ||
|
Oops, something went wrong.
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.
need to figure out if we want this