Skip to content

Commit

Permalink
[XLA] Remove a bogus invalid argument message printed out when --v>=1.
Browse files Browse the repository at this point in the history
When running any trivial XLA program with --v=1, you will see bogus message such
as "Invalid argument: Shape f32[] size may overflow int64". The reason for this
is because in ShapeUtil::ValidateShapeSize, we incorrectly construct an
InvalidArgument object prematurely. This change postpones the construction of
the InvalidArgument object until an invalid argument is actually discovered.

PiperOrigin-RevId: 202959886
  • Loading branch information
bixia1 authored and tensorflower-gardener committed Jul 2, 2018
1 parent 587f118 commit 97a040a
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions tensorflow/compiler/xla/shape_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -891,44 +891,51 @@ StatusOr<Shape> ParseShapeStringInternal(tensorflow::StringPiece* s) {

/* static */ Status ShapeUtil::ValidateShapeSize(const Shape& shape) {
VLOG(3) << "Validating shape size: " << ShapeUtil::HumanString(shape);
auto invalid_argument =
InvalidArgument("Shape %s size may overflow int64.",
ShapeUtil::HumanString(shape).c_str());

if (!IsArray(shape)) {
return Status::OK();
}
int64 shape_size;
if (LayoutUtil::IsSparseArray(shape)) {
shape_size = LayoutUtil::MaxSparseElements(shape.layout());
if (shape_size < 0) {
return invalid_argument;
}
shape_size = MultiplyWithoutOverflow(shape_size, ShapeUtil::Rank(shape));
if (shape_size < 0) {
return invalid_argument;

int64 shape_size = [&shape]() {
int64 shape_size;
if (LayoutUtil::IsSparseArray(shape)) {
shape_size = LayoutUtil::MaxSparseElements(shape.layout());
if (shape_size < 0) {
return shape_size;
}
shape_size = MultiplyWithoutOverflow(shape_size, ShapeUtil::Rank(shape));
if (shape_size < 0) {
return shape_size;
}
shape_size = MultiplyWithoutOverflow(shape_size, sizeof(int64));
if (shape_size < 0) {
return shape_size;
}
}
shape_size = MultiplyWithoutOverflow(shape_size, sizeof(int64));
if (shape_size < 0) {
return invalid_argument;

shape_size = 1;

// This is intentionally unconditional: even if the shape is sparse, we want
// to verify the densified version has a reasonable size.
if (shape.dimensions().empty()) {
return shape_size;
}
}

// This is intentionally unconditional: even if the shape is sparse, we want
// to verify the densified version has a reasonable size.
if (shape.dimensions().empty()) {
return Status::OK();
}
shape_size = 1;
for (int64 dim : shape.dimensions()) {
shape_size = MultiplyWithoutOverflow(shape_size, dim);
if (shape_size < 0) {
return invalid_argument;
for (int64 dim : shape.dimensions()) {
shape_size = MultiplyWithoutOverflow(shape_size, dim);
if (shape_size < 0) {
return shape_size;
}
}
}
shape_size = MultiplyWithoutOverflow(
shape_size, ByteSizeOfPrimitiveType(shape.element_type()));
shape_size = MultiplyWithoutOverflow(
shape_size, ByteSizeOfPrimitiveType(shape.element_type()));

return shape_size;
}();

if (shape_size < 0) {
return invalid_argument;
return InvalidArgument("Shape %s size may overflow int64.",
ShapeUtil::HumanString(shape).c_str());
}

VLOG(3) << "Shape size is valid: " << shape_size;
Expand Down

0 comments on commit 97a040a

Please sign in to comment.