Skip to content

Commit ef489fb

Browse files
Fix for op placements when deduplicating sparse gradients.
Adds a fake GPU kernel for unique, which allows the hard placement of other ops on the GPU. Change: 146432826
1 parent ec1dec8 commit ef489fb

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎tensorflow/core/kernels/unique_op.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,16 @@ class UniqueOp : public OpKernel {
9494
TF_CALL_REAL_NUMBER_TYPES(REGISTER_UNIQUE);
9595
REGISTER_UNIQUE(string)
9696
#undef REGISTER_UNIQUE
97+
98+
// A fake int32 GPU kernel so that the use of Unique in optimizers (to
99+
// de-duplicate sparse gradient indices) does not conflict with gradients being
100+
// located on a GPU.
101+
REGISTER_KERNEL_BUILDER(Name("Unique")
102+
.Device(DEVICE_GPU)
103+
.TypeConstraint<int32>("T")
104+
.TypeConstraint<int32>("out_idx")
105+
.HostMemory("x")
106+
.HostMemory("y")
107+
.HostMemory("idx"),
108+
UniqueOp<int32>);
97109
} // namespace tensorflow

‎tensorflow/python/BUILD‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2598,14 +2598,29 @@ py_test(
25982598
],
25992599
)
26002600

2601+
cuda_py_test(
2602+
name = "adam_test",
2603+
size = "small",
2604+
srcs = ["training/adam_test.py"],
2605+
additional_deps = [
2606+
":array_ops",
2607+
":framework",
2608+
":math_ops",
2609+
":platform",
2610+
":training",
2611+
":platform_test",
2612+
":client_testlib",
2613+
"//third_party/py/numpy",
2614+
],
2615+
)
2616+
26012617
cuda_py_tests(
26022618
name = "training_tests",
26032619
size = "small",
26042620
srcs = [
26052621
"training/adadelta_test.py",
26062622
"training/adagrad_da_test.py",
26072623
"training/adagrad_test.py",
2608-
"training/adam_test.py",
26092624
"training/basic_loops_test.py",
26102625
"training/coordinator_test.py",
26112626
"training/device_setter_test.py",

‎tensorflow/python/training/adam_test.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from tensorflow.python.framework import constant_op
2525
from tensorflow.python.framework import dtypes
2626
from tensorflow.python.framework import ops
27+
from tensorflow.python.ops import array_ops
28+
from tensorflow.python.ops import math_ops
2729
from tensorflow.python.ops import resource_variable_ops
2830
from tensorflow.python.ops import variables
2931
from tensorflow.python.platform import test
@@ -93,6 +95,18 @@ def testSparse(self):
9395
self.assertAllCloseAccordingToType(var0_np, var0.eval())
9496
self.assertAllCloseAccordingToType(var1_np, var1.eval())
9597

98+
def testSparseDevicePlacement(self):
99+
with self.test_session(force_gpu=test.is_gpu_available()):
100+
# If a GPU is available, tests that all optimizer ops can be placed on
101+
# it (i.e. they have GPU kernels).
102+
var = variables.Variable([[1.0], [2.0]])
103+
indices = constant_op.constant([0, 1])
104+
gathered_sum = math_ops.reduce_sum(array_ops.gather(var, indices))
105+
optimizer = adam.AdamOptimizer(3.0)
106+
minimize_op = optimizer.minimize(gathered_sum)
107+
variables.global_variables_initializer().run()
108+
minimize_op.run()
109+
96110
def testSparseRepeatedIndices(self):
97111
for dtype in [dtypes.half, dtypes.float32, dtypes.float64]:
98112
with self.test_session():

0 commit comments

Comments
 (0)