Skip to content

Commit c968e5f

Browse files
aaroeytensorflower-gardener
authored andcommitted
Improve BundleWriter performance for some file system implementations: use raw char array as the buffer type in FileOutputBuffer, and wrap it in a Cord for file writing.
PiperOrigin-RevId: 362396766 Change-Id: Id4566040a4bfa1f94acd1db961b5fd3486854355
1 parent 5239f2b commit c968e5f

4 files changed

Lines changed: 78 additions & 42 deletions

File tree

‎tensorflow/core/platform/ram_file_system.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class RamRandomAccessFile : public RandomAccessFile, public WritableFile {
7979
return Status::OK();
8080
}
8181

82-
#if defined(PLATFORM_GOOGLE)
82+
#if defined(TF_CORD_SUPPORT)
8383
Status Append(const absl::Cord& cord) override {
8484
data_->append(cord.char_begin(), cord.char_end());
8585
return Status::OK();

‎tensorflow/core/util/tensor_bundle/tensor_bundle.cc‎

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ limitations under the License.
4141
#include "tensorflow/core/lib/strings/str_util.h"
4242
#include "tensorflow/core/lib/strings/stringprintf.h"
4343
#include "tensorflow/core/platform/bfloat16.h"
44+
#include "tensorflow/core/platform/cord.h"
4445
#include "tensorflow/core/platform/errors.h"
46+
#include "tensorflow/core/platform/mem.h"
4547
#include "tensorflow/core/util/env_var.h"
4648
#include "tensorflow/core/util/saved_tensor_slice_util.h"
4749
#include "tensorflow/core/util/tensor_bundle/byte_swap.h"
@@ -406,11 +408,7 @@ Status PadAlignment(FileOutputBuffer* out, int alignment, int64* size) {
406408
} // namespace
407409

408410
BundleWriter::BundleWriter(Env* env, StringPiece prefix, const Options& options)
409-
: env_(env),
410-
options_(options),
411-
prefix_(prefix),
412-
out_(nullptr),
413-
size_(0) {
411+
: env_(env), options_(options), prefix_(prefix), out_(nullptr), size_(0) {
414412
status_ = env_->HasAtomicMove(prefix_, &use_temp_file_);
415413
if (!status_.ok()) return;
416414

@@ -1138,31 +1136,48 @@ string BundleReader::DebugString() {
11381136
return shape_str;
11391137
}
11401138

1141-
FileOutputBuffer::~FileOutputBuffer() { delete file_; }
1139+
namespace {
1140+
inline char* AlignedMalloc(size_t size) {
1141+
char* buffer = static_cast<char*>(port::AlignedMalloc(size, 64));
1142+
DCHECK(buffer);
1143+
return buffer;
1144+
}
1145+
} // namespace
1146+
1147+
FileOutputBuffer::FileOutputBuffer(WritableFile* file, size_t buffer_size)
1148+
: file_(file), position_(0), buffer_size_(buffer_size) {
1149+
DCHECK_GT(buffer_size, 0);
1150+
buffer_ptr_ = AlignedMalloc(buffer_size);
1151+
}
1152+
1153+
FileOutputBuffer::~FileOutputBuffer() {
1154+
if (buffer_ptr_) port::AlignedFree(buffer_ptr_);
1155+
delete file_;
1156+
}
11421157

11431158
Status FileOutputBuffer::Append(StringPiece data) {
11441159
// In the below, it is critical to calculate the checksum on the actually
11451160
// copied bytes, not the source bytes. This is because "data" typically
11461161
// points to tensor buffers, which may be concurrently written.
11471162
if (data.size() + position_ <= buffer_size_) {
11481163
// Can fit into the current buffer.
1149-
memcpy(&buffer_[position_], data.data(), data.size());
1150-
crc32c_ = crc32c::Extend(crc32c_, &buffer_[position_], data.size());
1164+
memcpy(buffer_ptr_ + position_, data.data(), data.size());
1165+
crc32c_ = crc32c::Extend(crc32c_, buffer_ptr_ + position_, data.size());
11511166
} else if (data.size() <= buffer_size_) {
11521167
// Cannot fit, but can fit after flushing.
1153-
TF_RETURN_IF_ERROR(FlushBuffer());
1154-
memcpy(&buffer_[0], data.data(), data.size());
1155-
crc32c_ = crc32c::Extend(crc32c_, &buffer_[0], data.size());
1168+
TF_RETURN_IF_ERROR(FlushBuffer(false));
1169+
memcpy(buffer_ptr_, data.data(), data.size());
1170+
crc32c_ = crc32c::Extend(crc32c_, buffer_ptr_, data.size());
11561171
} else {
11571172
// Cannot fit even after flushing. So we break down "data" by chunk, and
11581173
// flush/checksum each chunk.
1159-
TF_RETURN_IF_ERROR(FlushBuffer());
1174+
TF_RETURN_IF_ERROR(FlushBuffer(false));
11601175
for (size_t i = 0; i < data.size(); i += buffer_size_) {
11611176
const size_t nbytes = std::min(data.size() - i, buffer_size_);
1162-
memcpy(&buffer_[0], data.data() + i, nbytes);
1163-
crc32c_ = crc32c::Extend(crc32c_, &buffer_[0], nbytes);
1177+
memcpy(buffer_ptr_, data.data() + i, nbytes);
1178+
crc32c_ = crc32c::Extend(crc32c_, buffer_ptr_, nbytes);
11641179
position_ = nbytes;
1165-
TF_RETURN_IF_ERROR(FlushBuffer());
1180+
TF_RETURN_IF_ERROR(FlushBuffer(false));
11661181
}
11671182
return Status::OK();
11681183
}
@@ -1171,13 +1186,18 @@ Status FileOutputBuffer::Append(StringPiece data) {
11711186
}
11721187

11731188
Status FileOutputBuffer::Close() {
1174-
TF_RETURN_IF_ERROR(FlushBuffer());
1189+
TF_RETURN_IF_ERROR(FlushBuffer(true));
11751190
return file_->Close();
11761191
}
11771192

1178-
Status FileOutputBuffer::FlushBuffer() {
1193+
Status FileOutputBuffer::FlushBuffer(bool closing) {
11791194
if (position_ > 0) {
1180-
TF_RETURN_IF_ERROR(file_->Append(StringPiece(&buffer_[0], position_)));
1195+
// Use Cord to avoid extra data copy for some WritableFile implementations.
1196+
absl::Cord buffer = absl::MakeCordFromExternal(
1197+
StringPiece(buffer_ptr_, position_),
1198+
[ptr = buffer_ptr_](StringPiece) { port::AlignedFree(ptr); });
1199+
buffer_ptr_ = closing ? nullptr : AlignedMalloc(buffer_size_);
1200+
TF_RETURN_IF_ERROR(file_->Append(buffer));
11811201
position_ = 0;
11821202
}
11831203
return Status::OK();

‎tensorflow/core/util/tensor_bundle/tensor_bundle.h‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ limitations under the License.
7373
#include "tensorflow/core/lib/io/cache.h"
7474
#include "tensorflow/core/lib/io/inputbuffer.h"
7575
#include "tensorflow/core/lib/io/table.h"
76+
#include "tensorflow/core/platform/cord.h"
7677
#include "tensorflow/core/platform/env.h"
7778
#include "tensorflow/core/platform/file_system.h"
7879
#include "tensorflow/core/platform/macros.h"
@@ -316,11 +317,7 @@ class BundleReader {
316317
// External synchronization must be used in the presence of concurrent callers.
317318
class FileOutputBuffer {
318319
public:
319-
FileOutputBuffer(WritableFile* file, size_t buffer_size)
320-
: file_(file), position_(0), buffer_size_(buffer_size) {
321-
DCHECK_GT(buffer_size, 0);
322-
buffer_.resize(buffer_size);
323-
}
320+
FileOutputBuffer(WritableFile* file, size_t buffer_size);
324321
~FileOutputBuffer();
325322

326323
// Buffered append.
@@ -336,15 +333,15 @@ class FileOutputBuffer {
336333

337334
private:
338335
// Appends the buffered data to the underlying file. Does NOT flush the file.
339-
Status FlushBuffer();
336+
Status FlushBuffer(bool closing);
340337

341338
WritableFile* file_; // Owned.
342339

343-
// buffer_[0, position_) holds the buffered data not yet appended to the
340+
// buffer_ptr_[0, position_) holds the buffered data not yet appended to the
344341
// underlying file.
345342
size_t position_;
346343
const size_t buffer_size_;
347-
std::vector<char> buffer_;
344+
char* buffer_ptr_;
348345

349346
// Checksum of all appended bytes since construction or last clear_crc32c().
350347
uint32 crc32c_ = 0;

‎tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,10 @@ TEST_F(TensorBundleAlignmentTest, AlignmentTest) {
11121112
}
11131113
}
11141114

1115-
static void BM_BundleAlignmentByteOff(::testing::benchmark::State& state,
1116-
int alignment, int tensor_size) {
1115+
static void BM_BundleAlignment(::testing::benchmark::State& state) {
11171116
{
1117+
const int alignment = state.range(0);
1118+
const int tensor_size = state.range(1);
11181119
BundleWriter::Options opts;
11191120
opts.data_alignment = alignment;
11201121
BundleWriter writer(Env::Default(), Prefix("foo"), opts);
@@ -1130,18 +1131,36 @@ static void BM_BundleAlignmentByteOff(::testing::benchmark::State& state,
11301131
}
11311132
}
11321133

1133-
#define BM_BundleAlignment(ALIGN, SIZE) \
1134-
static void BM_BundleAlignment_##ALIGN##_##SIZE( \
1135-
::testing::benchmark::State& state) { \
1136-
BM_BundleAlignmentByteOff(state, ALIGN, SIZE); \
1137-
} \
1138-
BENCHMARK(BM_BundleAlignment_##ALIGN##_##SIZE)
1139-
1140-
BM_BundleAlignment(1, 512);
1141-
BM_BundleAlignment(1, 4096);
1142-
BM_BundleAlignment(1, 1048576);
1143-
BM_BundleAlignment(4096, 512);
1144-
BM_BundleAlignment(4096, 4096);
1145-
BM_BundleAlignment(4096, 1048576);
1134+
BENCHMARK(BM_BundleAlignment)->ArgPair(1, 512);
1135+
BENCHMARK(BM_BundleAlignment)->ArgPair(1, 4096);
1136+
BENCHMARK(BM_BundleAlignment)->ArgPair(1, 1048576);
1137+
BENCHMARK(BM_BundleAlignment)->ArgPair(4096, 512);
1138+
BENCHMARK(BM_BundleAlignment)->ArgPair(4096, 4096);
1139+
BENCHMARK(BM_BundleAlignment)->ArgPair(4096, 1048576);
1140+
1141+
static void BM_BundleWriterSmallTensor(::testing::benchmark::State& state) {
1142+
const int64 bytes = state.range(0);
1143+
Tensor t = Constant(static_cast<int8>('a'), TensorShape{bytes});
1144+
BundleWriter writer(Env::Default(), Prefix("foo"));
1145+
int suffix = 0;
1146+
for (auto s : state) {
1147+
TF_CHECK_OK(writer.Add(strings::StrCat("small", suffix++), t));
1148+
}
1149+
}
1150+
1151+
BENCHMARK(BM_BundleWriterSmallTensor)->Range(1, 1 << 20);
1152+
1153+
static void BM_BundleWriterLargeTensor(::testing::benchmark::State& state) {
1154+
const int mb = state.range(0);
1155+
const int64 bytes = static_cast<int64>(mb) * (1 << 20);
1156+
Tensor t = Constant(static_cast<int8>('a'), TensorShape{bytes});
1157+
for (auto s : state) {
1158+
BundleWriter writer(Env::Default(), Prefix("foo"));
1159+
TF_CHECK_OK(writer.Add("big", t));
1160+
}
1161+
}
1162+
1163+
BENCHMARK(BM_BundleWriterLargeTensor)->Arg(1 << 10);
1164+
BENCHMARK(BM_BundleWriterLargeTensor)->Arg(4 << 10);
11461165

11471166
} // namespace tensorflow

0 commit comments

Comments
 (0)