Skip to content

Commit 5de9085

Browse files
author
Vijay Vasudevan
committed
TensorFlow: Upstream changes to git.
Change 109738410 Don't crash if an attribute contains an invalid shape Using GetAttr to retrieve a TensorShape caused a process crash if the shape contained negative entries or was too large. Instead, produce useful error messages (and Python exceptions). Fixes tensorflow#449. Change 109737915 TensorFlow: fix build failures and some warnings when built with clang on OS X. Change 109737559 Fix bad paragraphing Change 109735757 Fix OSX installation instructions. Change 109733797 Adds buttons to toggle the display of all runs. Base CL: 109739474
1 parent 2d11635 commit 5de9085

File tree

11 files changed

+109
-31
lines changed

11 files changed

+109
-31
lines changed

tensorflow/core/framework/attr_value_util.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ string SummarizeString(const string& str) {
3030
return strings::StrCat("\"", str_util::CEscape(str), "\"");
3131
}
3232

33-
string SummarizeShape(const TensorShapeProto& proto) {
34-
TensorShape shape(proto);
35-
return shape.ShortDebugString();
36-
}
37-
3833
string SummarizeTensor(const TensorProto& tensor_proto) {
3934
Tensor t;
4035
if (!t.FromProto(tensor_proto)) {
@@ -59,7 +54,7 @@ string SummarizeAttrValue(const AttrValue& attr_value) {
5954
case AttrValue::kType:
6055
return DataType_Name(attr_value.type());
6156
case AttrValue::kShape:
62-
return SummarizeShape(attr_value.shape());
57+
return TensorShape::ShortDebugString(attr_value.shape());
6358
case AttrValue::kTensor:
6459
return SummarizeTensor(attr_value.tensor());
6560
case AttrValue::kList: {
@@ -92,7 +87,8 @@ string SummarizeAttrValue(const AttrValue& attr_value) {
9287
} else if (attr_value.list().shape_size() > 0) {
9388
for (int i = 0; i < attr_value.list().shape_size(); ++i) {
9489
if (i > 0) strings::StrAppend(&ret, ", ");
95-
strings::StrAppend(&ret, SummarizeShape(attr_value.list().shape(i)));
90+
strings::StrAppend(
91+
&ret, TensorShape::ShortDebugString(attr_value.list().shape(i)));
9692
}
9793
} else if (attr_value.list().tensor_size() > 0) {
9894
for (int i = 0; i < attr_value.list().tensor_size(); ++i) {

tensorflow/core/framework/node_def_util.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ DEFINE_GET_ATTR(bool, b, "bool", push_back, v, ;)
129129
DEFINE_GET_ATTR(DataType, type, "type", emplace_back, static_cast<DataType>(v),
130130
;)
131131
DEFINE_GET_ATTR(TensorShapeProto, shape, "shape", emplace_back, v, ;)
132-
DEFINE_GET_ATTR(TensorShape, shape, "shape", emplace_back, TensorShape(v), ;)
132+
DEFINE_GET_ATTR(TensorShape, shape, "shape", emplace_back, TensorShape(v),
133+
TF_RETURN_IF_ERROR(TensorShape::IsValidShape(v));)
133134
DEFINE_GET_ATTR(Tensor, tensor, "tensor", emplace_back, t, Tensor t;
134135
if (!t.FromProto(v)) {
135136
return errors::InvalidArgument(

tensorflow/core/framework/tensor_shape.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515

1616
#include "tensorflow/core/public/tensor_shape.h"
1717

18+
#include "tensorflow/core/lib/core/errors.h"
1819
#include "tensorflow/core/lib/strings/str_util.h"
1920
#include "tensorflow/core/lib/strings/strcat.h"
2021
#include "tensorflow/core/platform/logging.h"
@@ -34,6 +35,23 @@ bool TensorShape::IsValid(const TensorShapeProto& proto) {
3435
return true;
3536
}
3637

38+
Status TensorShape::IsValidShape(const TensorShapeProto& proto) {
39+
int64 num_elements = 1;
40+
for (const auto& d : proto.dim()) {
41+
if (d.size() < 0) {
42+
return errors::InvalidArgument("Shape ", ShortDebugString(proto),
43+
" has negative dimensions");
44+
}
45+
num_elements *= d.size();
46+
if (num_elements > kMaxElements) {
47+
return errors::InvalidArgument("Shape ", ShortDebugString(proto),
48+
" is too large (more than ", kMaxElements,
49+
" entries)");
50+
}
51+
}
52+
return Status::OK();
53+
}
54+
3755
TensorShape::TensorShape(const TensorShapeProto& proto) {
3856
dim_sizes_.reserve(proto.dim_size());
3957
num_elements_ = 1;
@@ -141,6 +159,17 @@ string TensorShape::ShortDebugString() const {
141159
"[", str_util::Join(gtl::ArraySlice<int64>(dim_sizes_), ","), "]");
142160
}
143161

162+
string TensorShape::ShortDebugString(const TensorShapeProto& proto) {
163+
string s = "[";
164+
bool first = true;
165+
for (const auto& d : proto.dim()) {
166+
strings::StrAppend(&s, first ? "" : ",", d.size());
167+
first = false;
168+
}
169+
strings::StrAppend(&s, "]");
170+
return s;
171+
}
172+
144173
bool TensorShapeUtils::StartsWith(const TensorShape& shape,
145174
const TensorShape& prefix) {
146175
if (shape.dims() < prefix.dims()) return false;

tensorflow/core/lib/gtl/edit_distance.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
#ifndef TENSORFLOW_LIB_GTL_EDIT_DISTANCE_H_
1717
#define TENSORFLOW_LIB_GTL_EDIT_DISTANCE_H_
1818

19+
#include <numeric>
20+
1921
#include "tensorflow/core/lib/gtl/array_slice.h"
2022
#include "tensorflow/core/lib/gtl/inlined_vector.h"
2123

tensorflow/core/platform/default/mutex.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ limitations under the License.
2020
#include <condition_variable>
2121
#include <mutex>
2222

23+
#include "tensorflow/core/platform/default/thread_annotations.h"
24+
2325
namespace tensorflow {
2426

2527
enum LinkerInitialized { LINKER_INITIALIZED };
2628

2729
// A class that wraps around the std::mutex implementation, only adding an
2830
// additional LinkerInitialized constructor interface.
29-
class mutex : public std::mutex {
31+
class LOCKABLE mutex : public std::mutex {
3032
public:
3133
mutex() {}
3234
// The default implementation of std::mutex is safe to use after the linker

tensorflow/core/public/env.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace tensorflow {
2828

2929
class RandomAccessFile;
3030
class Thread;
31-
class ThreadOptions;
3231
class WritableFile;
32+
struct ThreadOptions;
3333

3434
/// \brief An interface used by the tensorflow implementation to
3535
/// access operating system functionality like the filesystem etc.

tensorflow/core/public/tensor_shape.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ limitations under the License.
2525
#include "tensorflow/core/lib/gtl/inlined_vector.h"
2626
#include "tensorflow/core/lib/strings/strcat.h"
2727
#include "tensorflow/core/platform/logging.h"
28+
#include "tensorflow/core/public/status.h"
2829

2930
namespace tensorflow {
3031

@@ -49,6 +50,10 @@ class TensorShape {
4950
/// Returns `true` iff `proto` is a valid tensor shape.
5051
static bool IsValid(const TensorShapeProto& proto);
5152

53+
/// Returns `OK` iff `proto` is a valid tensor shape, and a descriptive error
54+
/// status otherwise.
55+
static Status IsValidShape(const TensorShapeProto& proto);
56+
5257
/// Clear a tensor shape
5358
void Clear();
5459

@@ -118,8 +123,13 @@ class TensorShape {
118123

119124
/// For error messages.
120125
string DebugString() const;
121-
// TODO(vrv): Remove this, this is the same as DebugString().
122126
string ShortDebugString() const;
127+
// TODO(vrv): Consolidate DebugString() and ShortDebugString() into one
128+
// function that is not verbose and works for scalars.
129+
130+
/// Same as `TensorShape(proto).ShortDebugString()` but doesn't crash for
131+
/// invalid protos.
132+
static string ShortDebugString(const TensorShapeProto& proto);
123133

124134
private:
125135
// Recalculates the dimensions of this tensor after they are modified.

tensorflow/g3doc/get_started/os_setup.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ $ python /usr/local/lib/python2.7/dist-packages/tensorflow/models/image/mnist/co
251251

252252
## Installing from sources {#source}
253253

254+
When installing from source you will build a pip wheel that you then install
255+
using pip. You'll need pip for that, so install it as described
256+
[above](#pip_install).
257+
254258
### Clone the TensorFlow repository
255259

256260
```bash
@@ -264,7 +268,6 @@ depends on.
264268

265269
#### Install Bazel
266270

267-
268271
Follow instructions [here](http://bazel.io/docs/install.html) to install the
269272
dependencies for Bazel. Then download bazel version 0.1.1 using the
270273
[installer for your system](https://github.com/bazelbuild/bazel/releases) and
@@ -278,8 +281,8 @@ $ ./PATH_TO_INSTALL.SH --user
278281
Remember to replace `PATH_TO_INSTALL.SH` with the location where you
279282
downloaded the installer.
280283

281-
Finally, follow the instructions in that script to place `bazel` into your binary
282-
path.
284+
Finally, follow the instructions in that script to place `bazel` into your
285+
binary path.
283286

284287
#### Install other dependencies
285288

@@ -416,25 +419,33 @@ given necessary bazel new feature support.
416419

417420
### Installation for Mac OS X
418421

419-
Mac needs the same set of dependencies as Linux, but the installation
420-
process for those dependencies is different. Here is a set of useful links
421-
to help with installing the dependencies on Mac OS X :
422+
We recommend using [homebrew](http://brew.sh) to install the bazel and SWIG
423+
dependencies, and installing python dependencies using easy_install or pip.
422424

423-
#### Bazel
425+
#### Dependencies
424426

425-
Look for installation instructions for Mac OS X on
426-
[this](http://bazel.io/docs/install.html) page.
427+
Follow instructions [here](http://bazel.io/docs/install.html) to install the
428+
dependencies for Bazel. You can then use homebrew to install bazel and SWIG:
427429

428-
#### SWIG
430+
```bash
431+
$ brew install bazel swig
432+
```
429433

430-
[Mac OS X installation](http://www.swig.org/Doc3.0/Preface.html#Preface_osx_installation).
434+
You can install the python dependencies using easy_install or pip. Using
435+
easy_install, run
431436

432-
Notes : You need to install
433-
[PCRE](ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/) and *NOT* PCRE2.
437+
```bash
438+
$ sudo easy_install -U six
439+
$ sudo easy_install -U numpy
440+
$ sudo easy_install wheel
441+
```
434442

435-
#### Numpy
443+
We also recommend the [ipython](https://ipython.org) enhanced python shell, so
444+
best install that too:
436445

437-
Follow installation instructions [here](http://docs.scipy.org/doc/numpy/user/install.html).
446+
```bash
447+
$ sudo easy_install ipython
448+
```
438449

439450
#### Configure the installation {#configure_osx}
440451

@@ -514,13 +525,13 @@ If, during `pip install`, you encounter an error like:
514525
IOError: [Errno 2] No such file or directory: '/tmp/pip-o6Tpui-build/setup.py'
515526
```
516527

517-
Solution: upgrade your version of `pip`:
528+
Solution: upgrade your version of pip:
518529

519530
```bash
520531
pip install --upgrade pip
521532
```
522533

523-
This may require `sudo`, depending on how `pip` is installed.
534+
This may require `sudo`, depending on how pip is installed.
524535

525536
#### SSLError: SSL_VERIFY_FAILED
526537

tensorflow/python/kernel_tests/constant_op_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ def testFillNegative(self):
538538
" must be nonnegative"):
539539
tf.fill(shape, 7).eval()
540540

541+
def testBadShape(self):
542+
with self.test_session():
543+
a = tf.placeholder(tf.float32, shape=(-1, 10))
544+
s = tf.shape(a)
545+
with self.assertRaisesOpError(r"Shape \[-1,10\] has negative dimensions"):
546+
s.eval()
547+
541548

542549
if __name__ == "__main__":
543550
tf.test.main()

tensorflow/tensorboard/components/tf-event-dashboard/tf-run-selector.html

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<link rel="import" href="../../bower_components/polymer/polymer.html">
2+
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
23
<link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">
34
<link rel="import" href="../imports/lodash.html">
45
<link rel="import" href="../tf-dashboard-common/scrollbar-style.html">
@@ -47,6 +48,13 @@
4748
class-scale="[[classScale]]"
4849
hide-missing-tooltips
4950
></tf-multi-checkbox>
51+
<paper-button
52+
class="x-button"
53+
id="toggle-all"
54+
on-tap="_toggleAll"
55+
>
56+
Toggle All Runs
57+
</paper-button>
5058
<style>
5159
:host {
5260
display: flex;
@@ -58,7 +66,6 @@
5866
width: 100%;
5967
flex-grow: 0;
6068
flex-shrink: 0;
61-
padding-left: 35px;
6269
padding-right: 16px;
6370
padding-bottom: 6px;
6471
box-sizing: border-box;
@@ -70,6 +77,13 @@
7077
flex-shrink: 1;
7178
height: 0px; /* hackhack So the flex-grow takes over and gives it space */
7279
}
80+
.x-button {
81+
width: calc(50% - .9em);
82+
font-size: 14px;
83+
background-color: var(--paper-grey-500);
84+
margin-top: 5px;
85+
color: white;
86+
}
7387
.x-tooltip {
7488
display: flex;
7589
flex-direction: row;
@@ -96,6 +110,13 @@
96110
classScale: Object, // map from run name to color class (css)
97111
closestRun: {type: String, value: null}, // which run has a value closest to mouse coordinate
98112
},
113+
_toggleAll: function() {
114+
if (this.outSelected.length > 0) {
115+
this.outSelected = [];
116+
} else {
117+
this.outSelected = this.runs.slice();
118+
}
119+
},
99120
_arrayify: function(item) {
100121
return [item];
101122
},

0 commit comments

Comments
 (0)