Skip to content

Commit e722358

Browse files
Merge changes from github.
END_PUBLIC --- Commit 6078160 authored by Eugene Brevdo<[email protected]> Committed by TensorFlower Gardener<[email protected]>: Extended ScratchSpace to expose its underlying scratch tensor object. PiperOrigin-RevId: 167649551 --- Commit db43fe6 authored by A. Unique TensorFlower<[email protected]> Committed by TensorFlower Gardener<[email protected]>: Add fast math attributes to all generated methods when fast math enabled. RELNOTES: n/a PiperOrigin-RevId: 167646637 --- Commit aebe8cc authored by A. Unique TensorFlower<[email protected]> Committed by TensorFlower Gardener<[email protected]>: Call HloComputation.Accept instead of HloInstruction.Accept to get all instructions profiled. RELNOTES: n/a PiperOrigin-RevId: 167640259 --- Commit 0ab137c authored by A. Unique TensorFlower<[email protected]> Committed by TensorFlower Gardener<[email protected]>: BEGIN_PUBLIC Automated g4 rollback of changelist 167604306 PiperOrigin-RevId: 167800256
1 parent f8a43f9 commit e722358

65 files changed

Lines changed: 1521 additions & 268 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

configure.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,13 @@ def set_tf_cunn_version(environ_cp):
685685
ldconfig_bin = which('ldconfig') or '/sbin/ldconfig'
686686
cudnn_path_from_ldconfig = run_shell([ldconfig_bin, '-p'])
687687
cudnn_path_from_ldconfig = re.search('.*libcudnn.so .* => (.*)',
688-
cudnn_path_from_ldconfig).group(1)
689-
if os.path.exists('%s.%s' % (cudnn_path_from_ldconfig, tf_cudnn_version)):
690-
cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
691-
break
688+
cudnn_path_from_ldconfig)
689+
if cudnn_path_from_ldconfig:
690+
cudnn_path_from_ldconfig = cudnn_path_from_ldconfig.group(1)
691+
if os.path.exists('%s.%s' % (cudnn_path_from_ldconfig,
692+
tf_cudnn_version)):
693+
cudnn_install_path = os.path.dirname(cudnn_path_from_ldconfig)
694+
break
692695

693696
# Reset and Retry
694697
print(

tensorflow/c/c_api_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class TensorCApi {
146146
}
147147
};
148148

149+
Status TF_TensorToTensor(const TF_Tensor* src, Tensor* dst);
150+
149151
TF_Tensor* TF_TensorFromTensor(const Tensor& src, TF_Status* status);
150152

151153
Status MessageToBuffer(const tensorflow::protobuf::Message& in, TF_Buffer* out);

tensorflow/c/eager/c_api.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ TF_DeviceList* TFE_ContextListDevices(TFE_Context* ctx, TF_Status* status) {
151151
return TF_SessionListDevices(ctx->session, status);
152152
}
153153

154-
TFE_TensorHandle* TFE_NewTensorHandle(TF_Tensor* t) {
155-
return new TFE_TensorHandle(
156-
tensorflow::TensorCApi::MakeTensor(t->dtype, t->shape, t->buffer),
157-
nullptr);
154+
TFE_TensorHandle* TFE_NewTensorHandle(TF_Tensor* t, TF_Status* status) {
155+
tensorflow::Tensor tensor;
156+
status->status = tensorflow::TF_TensorToTensor(t, &tensor);
157+
if (!status->status.ok()) return nullptr;
158+
return new TFE_TensorHandle(tensor, nullptr);
158159
}
159160

160161
void TFE_DeleteTensorHandle(TFE_TensorHandle* h) { delete h; }

tensorflow/c/eager/c_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extern TF_DeviceList* TFE_ContextListDevices(TFE_Context* ctx,
4343
// placed in memory of different devices or remote address spaces.
4444
typedef struct TFE_TensorHandle TFE_TensorHandle;
4545

46-
extern TFE_TensorHandle* TFE_NewTensorHandle(TF_Tensor* t);
46+
extern TFE_TensorHandle* TFE_NewTensorHandle(TF_Tensor* t, TF_Status* status);
4747
extern void TFE_DeleteTensorHandle(TFE_TensorHandle* h);
4848
extern TF_DataType TFE_TensorHandleDataType(TFE_TensorHandle* h);
4949
extern int TFE_TensorHandleNumDims(TFE_TensorHandle* h);

tensorflow/c/eager/c_api_test.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ TFE_TensorHandle* TestMatrixTensorHandle() {
3434
TF_Tensor* t = TF_AllocateTensor(
3535
TF_FLOAT, &dims[0], sizeof(dims) / sizeof(int64_t), sizeof(data));
3636
memcpy(TF_TensorData(t), &data[0], TF_TensorByteSize(t));
37-
TFE_TensorHandle* th = TFE_NewTensorHandle(t);
37+
TF_Status* status = TF_NewStatus();
38+
TFE_TensorHandle* th = TFE_NewTensorHandle(t, status);
39+
CHECK_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
3840
TF_DeleteTensor(t);
41+
TF_DeleteStatus(status);
3942
return th;
4043
}
4144

@@ -383,7 +386,9 @@ TFE_TensorHandle* CreateVariable(TFE_Context* ctx, float value,
383386
memcpy(TF_TensorData(t.get()), &value, TF_TensorByteSize(t.get()));
384387

385388
std::unique_ptr<TFE_TensorHandle, decltype(&TFE_DeleteTensorHandle)>
386-
value_handle(TFE_NewTensorHandle(t.get()), TFE_DeleteTensorHandle);
389+
value_handle(TFE_NewTensorHandle(t.get(), status),
390+
TFE_DeleteTensorHandle);
391+
if (TF_GetCode(status) != TF_OK) return nullptr;
387392

388393
TFE_OpAddInput(op, value_handle.get(), status);
389394
if (TF_GetCode(status) != TF_OK) return nullptr;

tensorflow/cc/framework/gradients.cc

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class SymbolicGradientBuilder {
7878
const std::vector<Output>& grad_inputs,
7979
std::vector<Output>* grad_outputs);
8080

81+
// Returns a list mapping whether each node in the graph is reachable
82+
// from outputs_. Keyed by node id.
83+
std::vector<bool> GetReachableNodes();
84+
8185
const Scope& scope_;
8286
const ops::GradOpRegistry* registry_;
8387
const std::vector<Output>& outputs_;
@@ -143,11 +147,36 @@ Status SymbolicGradientBuilder::BackpropAlongEdge(const Output& dst_grad,
143147
return Status::OK();
144148
}
145149

150+
std::vector<bool> SymbolicGradientBuilder::GetReachableNodes() {
151+
std::vector<bool> reachable_nodes(scope_.graph()->num_node_ids(), false);
152+
std::deque<Node*> queue;
153+
for (const Output& out : outputs_) {
154+
if (!reachable_nodes[out.node()->id()]) {
155+
queue.push_back(out.node());
156+
reachable_nodes[out.node()->id()] = true;
157+
}
158+
}
159+
160+
while (!queue.empty()) {
161+
Node* n = queue.front();
162+
queue.pop_front();
163+
for (const Edge* e : n->in_edges()) {
164+
if (e->IsControlEdge()) continue;
165+
queue.push_back(e->src());
166+
reachable_nodes[e->src()->id()] = true;
167+
}
168+
}
169+
return reachable_nodes;
170+
}
171+
146172
Status SymbolicGradientBuilder::Initialize() {
147173
if (outputs_.size() != grad_inputs_.size()) {
148174
return errors::InvalidArgument(
149175
"Must specify a gradient input for each output.");
150176
}
177+
std::vector<bool> reachable_nodes = GetReachableNodes();
178+
// TODO(theflofly) Check that inputs_ are reachable from
179+
// outputs_ using reachable_nodes
151180
grad_outputs_->clear();
152181
grad_outputs_->resize(inputs_.size());
153182
// Populate `output_nodes_` from node ids in `outputs_`.
@@ -188,12 +217,15 @@ Status SymbolicGradientBuilder::Initialize() {
188217
if (output_nodes_.find(n->id()) == output_nodes_.end()) {
189218
// Internal node: continue BFS along connected outputs.
190219
for (const Edge* e : n->out_edges()) {
191-
if (e->IsControlEdge()) continue;
192-
++num_expected_backprops;
220+
// If a node is not reachable from outputs_,
221+
// we don't expect it to receive a backpropagated gradient.
222+
// It will not be counted in num_expected_backprops.
223+
if (e->IsControlEdge() || !reachable_nodes[e->dst()->id()]) continue;
193224
if (visited.find(e->dst()) == visited.end()) {
194225
queue.push_back(e->dst());
195226
visited.insert(e->dst());
196227
}
228+
++num_expected_backprops;
197229
}
198230
} else {
199231
// Output node: stop BFS and update `num_expected_backprops` for

tensorflow/cc/framework/gradients_test.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,73 @@ TEST_F(GradientsTest, MultipleNodeOutputGrads) {
364364
test::AsTensor<int>({60, 61, 62, 63, 66, 66, 66, 67}, {4, 2}));
365365
}
366366

367+
TEST_F(GradientsTest, UnreachableEdgeGradOneOutput) {
368+
auto x = Variable(scope_test_, {2, 3}, DT_DOUBLE);
369+
auto x_const = Const(scope_test_, {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}});
370+
auto x_assign = Assign(scope_test_, x, x_const);
371+
372+
auto y = Variable(scope_test_, {3, 1}, DT_DOUBLE);
373+
auto y_const = Const(scope_test_, {{1.0}, {2.0}, {3.0}});
374+
auto y_assign = Assign(scope_test_, y, y_const);
375+
376+
auto m1 = MatMul(scope_test_, x, y);
377+
378+
auto z = Variable(scope_test_, {1, 3}, DT_DOUBLE);
379+
auto z_const = Const(scope_test_, {{9.0, 10.0, 11.0}});
380+
auto z_assign = Assign(scope_test_, z, z_const);
381+
382+
auto m2 = MatMul(scope_test_, y, z);
383+
384+
auto dm1 = Const(scope_test_, {{0.5}, {0.5}});
385+
386+
std::vector<Output> grad_outputs;
387+
TF_ASSERT_OK(
388+
AddSymbolicGradients(scope_test_, {m1}, {y}, {dm1}, &grad_outputs));
389+
390+
std::vector<Tensor> outputs;
391+
test::GetTensors(scope_test_, {x_assign, y_assign, z_assign},
392+
{grad_outputs[0]}, &outputs);
393+
// dz/dy = xT * dm1
394+
test::ExpectTensorNear<double>(
395+
outputs[0], test::AsTensor<double>({2.5, 3.5, 4.5}, {3, 1}), 1e-5);
396+
}
397+
398+
TEST_F(GradientsTest, UnreachableEdgeGradTwoOutputs) {
399+
auto x = Variable(scope_test_, {2, 3}, DT_DOUBLE);
400+
auto x_const = Const(scope_test_, {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}});
401+
auto x_assign = Assign(scope_test_, x, x_const);
402+
403+
auto y = Variable(scope_test_, {3, 1}, DT_DOUBLE);
404+
auto y_const = Const(scope_test_, {{1.0}, {2.0}, {3.0}});
405+
auto y_assign = Assign(scope_test_, y, y_const);
406+
407+
auto m1 = MatMul(scope_test_, x, y);
408+
409+
auto z = Variable(scope_test_, {1, 3}, DT_DOUBLE);
410+
auto z_const = Const(scope_test_, {{9.0, 10.0, 11.0}});
411+
auto z_assign = Assign(scope_test_, z, z_const);
412+
413+
auto m2 = MatMul(scope_test_, y, z);
414+
415+
auto dm1 = Const(scope_test_, {{0.5}, {0.5}});
416+
auto dm2 =
417+
Const(scope_test_, {{0.5, 0.5, 0.5}, {0.6, 0.7, 0.8}, {0.6, 0.7, 0.9}});
418+
419+
std::vector<Output> grad_outputs;
420+
TF_ASSERT_OK(AddSymbolicGradients(scope_test_, {m1, m2}, {y}, {dm1, dm2},
421+
&grad_outputs));
422+
423+
std::vector<Tensor> outputs;
424+
test::GetTensors(scope_test_, {x_assign, y_assign, z_assign},
425+
{grad_outputs[0]}, &outputs);
426+
427+
// the gradients from m1 and m2 will be summed to compute the gradient
428+
// w.r.t y
429+
// dz/dy = xT * dm1 + dm2 * zT
430+
test::ExpectTensorNear<double>(
431+
outputs[0], test::AsTensor<double>({17.5, 24.7, 26.8}, {3, 1}), 1e-5);
432+
}
433+
367434
// StopGradientSingleOutputMultiEdgeTest tests combinations of valid and
368435
// 'NoGradient' (induced by StopGradient op) returned along multiple edges from
369436
// a single nodes output.

tensorflow/cc/framework/testutil.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,19 @@ void GetTensor(const Scope& scope, Output tensor, Tensor* out) {
3636
*out = outputs[0];
3737
}
3838

39+
void GetTensors(const Scope& scope, const std::vector<Output>& assign_vars,
40+
const OutputList& tensors, std::vector<Tensor>* out) {
41+
ClientSession session(scope);
42+
TF_CHECK_OK(session.Run(assign_vars, nullptr));
43+
TF_CHECK_OK(session.Run(tensors, out));
44+
}
45+
46+
void GetTensor(const Scope& scope, const std::vector<Output>& assign_vars,
47+
Output tensor, Tensor* out) {
48+
std::vector<Tensor> outputs;
49+
GetTensors(scope, assign_vars, {std::move(tensor)}, &outputs);
50+
*out = outputs[0];
51+
}
52+
3953
} // end namespace test
4054
} // end namespace tensorflow

tensorflow/cc/framework/testutil.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,21 @@ namespace test {
2626
void GetTensors(const Scope& scope, OutputList tensors,
2727
std::vector<Tensor>* out);
2828

29+
// Computes the outputs listed in 'tensors', returns the tensors in 'out'.
30+
// assign_vars are extra outputs that should be run
31+
// e.g. to assign values to variables.
32+
void GetTensors(const Scope& scope, const std::vector<Output>& assign_vars,
33+
const OutputList& tensors, std::vector<Tensor>* out);
34+
2935
/// Computes the output 'tensor', returning the resulting tensor in 'out'.
3036
void GetTensor(const Scope& scope, Output tensor, Tensor* out);
3137

38+
// Computes the output 'tensor', returning the resulting tensor in 'out'.
39+
// assign_vars are extra outputs that should be run
40+
// e.g. to assign values to variables.
41+
void GetTensor(const Scope& scope, const std::vector<Output>& assign_vars,
42+
Output tensor, Tensor* out);
43+
3244
} // namespace test
3345
} // namespace tensorflow
3446

tensorflow/contrib/cmake/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ option(tensorflow_BUILD_MORE_PYTHON_TESTS "Build more python unit tests for cont
3333
option(tensorflow_BUILD_SHARED_LIB "Build TensorFlow as a shared library" OFF)
3434
option(tensorflow_OPTIMIZE_FOR_NATIVE_ARCH "Enable compiler optimizations for the native processor architecture (if available)" ON)
3535
option(tensorflow_WIN_CPU_SIMD_OPTIONS "Enables CPU SIMD instructions")
36+
option(tensorflow_ENABLE_SNAPPY_SUPPORT "Enable SNAPPY compression support" ON)
3637

3738
if (NOT WIN32)
3839
# Threads: defines CMAKE_THREAD_LIBS_INIT and adds -pthread compile option
@@ -204,6 +205,12 @@ if(tensorflow_ENABLE_JEMALLOC_SUPPORT)
204205
list(APPEND tensorflow_EXTERNAL_DEPENDENCIES jemalloc)
205206
include_directories(${jemalloc_INCLUDE_DIRS})
206207
endif()
208+
if(tensorflow_ENABLE_SNAPPY_SUPPORT)
209+
include(snappy)
210+
list(APPEND tensorflow_EXTERNAL_LIBRARIES ${snappy_STATIC_LIBRARIES})
211+
list(APPEND tensorflow_EXTERNAL_DEPENDENCIES snappy)
212+
include_directories(${snappy_INCLUDE_DIR})
213+
endif()
207214
if(WIN32)
208215
list(APPEND tensorflow_EXTERNAL_LIBRARIES wsock32 ws2_32 shlwapi)
209216
endif()

0 commit comments

Comments
 (0)