static */
| 711 | } |
| 712 | |
| 713 | /* static */ Status ShapeUtil::ValidateShapeSize(const Shape& shape) { |
| 714 | VLOG(3) << "Validating shape size: " << ShapeUtil::HumanString(shape); |
| 715 | |
| 716 | if (!shape.IsArray()) { |
| 717 | return Status::OK(); |
| 718 | } |
| 719 | |
| 720 | int64 shape_size = [&]() { |
| 721 | int64 dense_shape_size = 1; |
| 722 | if (shape.dimensions().empty()) { |
| 723 | return dense_shape_size; |
| 724 | } |
| 725 | |
| 726 | absl::Span<const int64> shape_max_dimensions = |
| 727 | AsInt64Slice(shape.dimensions()); |
| 728 | for (int64 dim : shape_max_dimensions) { |
| 729 | dense_shape_size = MultiplyWithoutOverflow(dense_shape_size, dim); |
| 730 | if (dense_shape_size < 0) { |
| 731 | return dense_shape_size; |
| 732 | } |
| 733 | } |
| 734 | dense_shape_size = MultiplyWithoutOverflow( |
| 735 | dense_shape_size, ByteSizeOfPrimitiveType(shape.element_type())); |
| 736 | return dense_shape_size; |
| 737 | }(); |
| 738 | |
| 739 | if (shape_size < 0) { |
| 740 | return InvalidArgument("Shape %s size may overflow int64.", |
| 741 | ShapeUtil::HumanString(shape)); |
| 742 | } |
| 743 | |
| 744 | VLOG(3) << "Shape size is valid: " << shape_size; |
| 745 | return Status::OK(); |
| 746 | } |
| 747 | |
| 748 | /* static */ Status ShapeUtil::ValidateShapeWithOptionalLayout( |
| 749 | const Shape& shape) { |
nothing calls this directly
no test coverage detected