Skip to content

Add support of drop_negatives for tf.unsorted_segment_sum - #13055

Merged
martinwicke merged 3 commits into
tensorflow:masterfrom
yongtang:478-unsorted_segment_sum-drop_negatives
Nov 7, 2017
Merged

martinwicke merged 3 commits into
tensorflow:masterfrom
yongtang:478-unsorted_segment_sum-drop_negatives

Conversation

@yongtang

Copy link
Copy Markdown
Member

This fix tries to address the issue raised in #478 by adding the support of drop_negatives for tf.unsorted_segment_sum so that it is possible to skip entries (when index = -1)

This fix fixes #478.

Signed-off-by: Yong Tang [email protected]

@tensorflow-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@mention-bot

Copy link
Copy Markdown

@yongtang, thanks for your PR! By analyzing the history of the files in this pull request, we identified @keveman, @tensorflower-gardener and @vrv to be potential reviewers.

drpngx
drpngx previously requested changes Sep 17, 2017
bool drop_negatives = false;
OP_REQUIRES_OK(context, context->GetAttr("drop_negatives", &drop_negatives));
if (drop_negatives) {
this->reduction_functor_ = &this->sum_with_drop_negatives_functor_;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this->? If not, please remove.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @drpngx. Done.

};

// Functor for UnsortedSegmentSumWithDropNegativesOp.
// 'output_rows': the number of output segments (unique segment ids in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove quotes unless they're there for consistency.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

@drpngx

drpngx commented Sep 17, 2017

Copy link
Copy Markdown
Contributor

This will probably require an API review.

At a high level, could we use tf.where with the simple reduce_sum?

@yongtang
yongtang force-pushed the 478-unsorted_segment_sum-drop_negatives branch from 22c3cb9 to b415b8a Compare September 19, 2017 21:38
@yongtang

Copy link
Copy Markdown
Member Author

Thanks @drpngx for the review. I addressed some of the comments. For using tf.where with reduce_sum, I am still playing with it.

However, I noticed that at the moment, the behavior of CPU and GPU is actually different. On CPU, negative indices will through out an error while on GPU negative indices will be ignored silently.

In other words, on GPU the behavior of tf.unsorted_segment_sum is already drop_negatives=True even before this PR.

I am wondering if it make sense to just adjust the behavior on CPU so that negatives will be dropped?

This will bring the behavior of GPU and CPU to the same without introducing another flag of drop_negatives=True.

Below is the output for GPU and CPU without this PR:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print tf.__version__
1.3.0
>>> with tf.device('/gpu:0'):
...   v = tf.unsorted_segment_sum([1,2], [1, -1], 5)
KeyboardInterrupt
>>> 
>>> import numpy as np
>>> with tf.device('/gpu:0'):
... v = tf.unsorted_segment_sum(np.asarray([1,2], dtype=np., [1, -1], 5)
KeyboardInterrupt
>>> 
>>> 
>>> with tf.device('/gpu:0'):
...   v = tf.unsorted_segment_sum([1.0, 2.0], [1, -1], 5)
...   tf.Session().run(v)
... 
....
array([ 0.,  1.,  0.,  0.,  0.], dtype=float32)
>>> with tf.device('/cpu:0'):
...   v = tf.unsorted_segment_sum([1.0, 2.0], [1, -1], 5)
...   tf.Session().run(v)
... 
2017-09-19 21:26:50.961788: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1055] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
Traceback (most recent call last):
...
...
InvalidArgumentError (see above for traceback): segment_ids[1] = -1 is out of range [0, 5)
	 [[Node: UnsortedSegmentSum_1 = UnsortedSegmentSum[T=DT_FLOAT, Tindices=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](UnsortedSegmentSum_1/data, UnsortedSegmentSum_1/segment_ids, UnsortedSegmentSum_1/num_segments)]]

>>> 

@drpngx drpngx added the API review API Review label Sep 20, 2017
@drpngx

drpngx commented Sep 20, 2017

Copy link
Copy Markdown
Contributor

Requesting API review. To rephrase, see @yongtang comment: GPU and CPU behave differently, one dropping negatives and the other one doesn't. We can leave the behavior undefined until we use drop_negatives, or just decide on a common behavior (error out or silently skip).

@martinwicke

Copy link
Copy Markdown
Member

Since the behavior on GPU is drop_negative=True, and since (IIRC) the current behavior on CPU is an error, we are within our right to change the behavior on CPU to match GPU (drop_negative=True). I think this alone would address #478.

I do not know whether we would still want the extra argument. Does anyone require drop_negative=False (which does not result in an error)?

This fix tries to address the issue raised in 478 by adding the
support of `drop_negatives` for `tf.unsorted_segment_sum`
so that it is possible to skip entries (when index = -1)

This fix fixes 478.

Signed-off-by: Yong Tang <[email protected]>

Add test case for `drop_negatives` of `tf.unsorted_segment_sum`

Signed-off-by: Yong Tang <[email protected]>

Remove quote from comments.

Signed-off-by: Yong Tang <[email protected]>
@yongtang
yongtang force-pushed the 478-unsorted_segment_sum-drop_negatives branch from b415b8a to c902776 Compare September 24, 2017 18:35
@yongtang

Copy link
Copy Markdown
Member Author

Thanks @drpngx @martinwicke for the review. I updated the PR so that now negative indices will be dropped silently, bring in behaviors of GPU and CPU closer.

I do notice another discrepancy between GPU and CPU. In GPU the bad index (> num_segments) are ignored as well while in CPU greater than num_segments will results in error. Not sure the best way to handle this situation. Any suggestions about that?

@sb2nov sb2nov added the awaiting review Pull request awaiting review label Sep 25, 2017
@martinwicke

Copy link
Copy Markdown
Member

@yongtang

Copy link
Copy Markdown
Member Author

Thanks @martinwicke. The PR has been updated with the documentation added in math_ops.cc.

@sb2nov

sb2nov commented Sep 28, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@yongtang

Copy link
Copy Markdown
Member Author

I tried to produce locally for the python 3 failure //tensorflow/contrib/learn:dnn_linear_combined_test but it works fine. Maybe it is transient?

@drpngx

drpngx commented Sep 29, 2017 via email

Copy link
Copy Markdown
Contributor

@josh11b

josh11b commented Oct 4, 2017

Copy link
Copy Markdown
Contributor

Approval for API review.

@josh11b josh11b removed the API review API Review label Oct 4, 2017
@yongtang

yongtang commented Oct 5, 2017

Copy link
Copy Markdown
Member Author

Thanks all for the review. The Windows build failure is likely to be transient (didn't failure in the previous runs). I think a rerun might fix it.

@drpngx

drpngx commented Oct 5, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@yongtang

yongtang commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

The Linux CPU Tests Makefile is the only test failing. It might be unrelated (also saw the same failure on other PRs).

@drpngx

drpngx commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

Yes, but I still want to see it run.

Jenkins, test this please.

@martinwicke
martinwicke dismissed drpngx’s stale review November 7, 2017 23:10

comments addressed.

@martinwicke
martinwicke merged commit 64db53f into tensorflow:master Nov 7, 2017
@yongtang
yongtang deleted the 478-unsorted_segment_sum-drop_negatives branch November 7, 2017 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Pull request awaiting review cla: yes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend tf.unsorted_segment_sum to allow 'rejecting' entries

8 participants