diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-06-28 03:56:27 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-06-28 03:59:03 -0700 |
commit | ddc01636d01b338e32c6ff07c22b78840cddd56a (patch) | |
tree | 74334f5cea93699476a7f29453088fe20dd5011c | |
parent | 06aac645329c45bb2c6d0fb1539816c3c0fb98e4 (diff) |
Improve the performance of ParseShapeStringInternal
The previous implementation recompiled the shape regex at every call
what is an expensive opertaion. The new implementation improves the hlo
text parsing time for very large models for up to 9x by eliminating this
overhead.
PiperOrigin-RevId: 202454354
-rw-r--r-- | tensorflow/compiler/xla/shape_util.cc | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/tensorflow/compiler/xla/shape_util.cc b/tensorflow/compiler/xla/shape_util.cc index e827ec5a22..9668a09e41 100644 --- a/tensorflow/compiler/xla/shape_util.cc +++ b/tensorflow/compiler/xla/shape_util.cc @@ -592,12 +592,11 @@ StatusOr<Shape> ParseShapeStringInternal(tensorflow::StringPiece* s) { // tensorflow::StringPiece is not compatible with internal RE2 StringPiece, so // we convert in to the RE2-consumable type and then consume the corresponding // amount from our StringPiece type. + static LazyRE2 shape_pattern = { + "^(\\w*\\d*)\\[([\\d,]*)\\](?:\\s*(dense|sparse)?\\s*{([\\d,]+)})?"}; tensorflow::RegexpStringPiece s_consumable(s->data(), s->size()); - if (RE2::Consume( - &s_consumable, - "^(\\w*\\d*)\\[([\\d,]*)\\](?:\\s*(dense|sparse)?\\s*{([\\d,]+)})?", - &element_type_string, &dimensions_string, &format_string, - &layout_string)) { + if (RE2::Consume(&s_consumable, *shape_pattern, &element_type_string, + &dimensions_string, &format_string, &layout_string)) { size_t consumed = s->size() - s_consumable.size(); s->remove_prefix(consumed); auto string_to_int64 = [&s](const string& input) -> StatusOr<int64> { |