-
Notifications
You must be signed in to change notification settings - Fork 74.8k
[determinism] Add softmax/cross-entropy op exceptions for GPU determinism #47925
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
copybara-service
merged 4 commits into
tensorflow:master
from
duncanriach:softmax-crossentropy-nond9m-exceptions
Apr 5, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28b56da
[determinism] Add softmax/cross-entropy op exceptions
duncanriach 9e988e0
[determinism] Add disable switches for softmax/cross-entropy op excep…
duncanriach caa3615
[determinism] Address review, step 1, on PR 47925
duncanriach dec1609
[determinism] Make RequireDeterminism internally-linked (for now)
duncanriach 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
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
68 changes: 68 additions & 0 deletions
68
tensorflow/python/kernel_tests/sparse_xent_op_deterministic_test.py
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,68 @@ | ||
# Copyright 2021 The TensorFlow Authors. 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 deterministic functionality of SparseSoftmaxCrossEntropyWithLogits op.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
|
||
from tensorflow.python.framework import constant_op | ||
from tensorflow.python.framework import dtypes | ||
from tensorflow.python.framework import errors_impl | ||
from tensorflow.python.framework import test_util | ||
from tensorflow.python.ops import nn_ops | ||
from tensorflow.python.platform import test | ||
|
||
|
||
class SparseSoftmaxCrossEntropyWithLogitsDeterminismExceptionsTest( | ||
test.TestCase): | ||
"""Test d9m-unimplemented exceptions from SparseSoftmaxCrossEntropyWithLogits. | ||
|
||
Test that tf.errors.UnimplementedError is thrown or not thrown, as | ||
appropriate, by the GPU code-paths for SparseSoftmaxCrossEntropyWithLogits | ||
when deterministic ops are enabled. | ||
|
||
This test assumes that the base op test runs all the same test cases when | ||
deterministic ops are not enabled and will therefore detect erroneous | ||
exception throwing in those cases. | ||
""" | ||
|
||
@test_util.run_cuda_only | ||
@test_util.run_in_graph_and_eager_modes | ||
def testExceptionThrowing(self): | ||
with self.session(force_gpu=True): | ||
for logits_dtype in [dtypes.float16, dtypes.float32]: | ||
for labels_dtype in [dtypes.int32, dtypes.int64]: | ||
labels = constant_op.constant([1, 0], dtype=labels_dtype) | ||
logits = constant_op.constant( | ||
[[0.3, 0.5], [0.2, 0.6]], dtype=logits_dtype) | ||
with self.assertRaisesRegex( | ||
errors_impl.UnimplementedError, | ||
"Deterministic GPU implementation of " + | ||
"SparseSoftmaxCrossEntropyWithLogits not available."): | ||
result = nn_ops.sparse_softmax_cross_entropy_with_logits( | ||
labels=labels, logits=logits) | ||
self.evaluate(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Note that the effect of setting the following environment variable to | ||
# 'true' is not tested. Unless we can find a simpler pattern for testing these | ||
# environment variables, it would require this file to be made into a base | ||
# and then two more test files to be created. | ||
os.environ["TF_DETERMINISTIC_OPS"] = "1" | ||
test.main() |
65 changes: 65 additions & 0 deletions
65
tensorflow/python/kernel_tests/xent_op_deterministic_test.py
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,65 @@ | ||
# Copyright 2021 The TensorFlow Authors. 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 deterministic functionality of SoftmaxCrossEntropyWithLogits op.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
|
||
from tensorflow.python.framework import constant_op | ||
from tensorflow.python.framework import dtypes | ||
from tensorflow.python.framework import errors_impl | ||
from tensorflow.python.framework import test_util | ||
from tensorflow.python.ops import nn_ops | ||
from tensorflow.python.platform import test | ||
|
||
|
||
class SoftmaxCrossEntropyWithLogitsDeterminismExceptionsTest(test.TestCase): | ||
"""Test d9m-unimplemented exceptions from SoftmaxCrossEntropyWithLogits. | ||
|
||
Test that tf.errors.UnimplementedError is thrown or not thrown, as | ||
appropriate, by the GPU code-paths for SoftmaxCrossEntropyWithLogits when | ||
deterministic ops are enabled. | ||
|
||
This test assumes that the base op test runs all the same test cases when | ||
deterministic ops are not enabled and will therefore detect erroneous | ||
exception throwing in those cases. | ||
""" | ||
|
||
@test_util.run_cuda_only | ||
@test_util.run_in_graph_and_eager_modes | ||
def testExceptionThrowing(self): | ||
with self.session(force_gpu=True): | ||
for dtype in [dtypes.float16, dtypes.float32, dtypes.float64]: | ||
labels = constant_op.constant([[0.2, 0.4], [0.1, 0.2]], dtype=dtype) | ||
logits = constant_op.constant([[0.3, 0.5], [0.5, 0.6]], dtype=dtype) | ||
with self.assertRaisesRegex( | ||
errors_impl.UnimplementedError, | ||
"Deterministic GPU implementation of " + | ||
"SoftmaxCrossEntropyWithLogits not available."): | ||
result = nn_ops.softmax_cross_entropy_with_logits( | ||
labels=labels, logits=logits) | ||
self.evaluate(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Note that the effect of setting the following environment variable to | ||
# 'true' is not tested. Unless we can find a simpler pattern for testing these | ||
# environment variables, it would require this file to be made into a base | ||
# and then two more test files to be created. | ||
os.environ["TF_DETERMINISTIC_OPS"] = "1" | ||
test.main() |
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.
Is the CPU implementation deterministic?
Uh oh!
There was an error while loading. Please reload this page.
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.
We just confirmed that the CPU implementation is deterministic (thanks @wenscarl). I'm considering adding tests to prove/confirm/ensure that in a future PR.