Skip to content

Commit a9890af

Browse files
author
James R. Barlow
committed
Fix text2image compilation on C++17 compilers
C++17 drops support for `std::random_shuffle`, breaking C++17 compilers that run to compile text2image.cpp. std::shuffle is valid on C++11 through C++17, so use std::shuffle instead. Due to the use `std::random_shuffle`, `text2image --render_ngrams` would not give consistent results for different compilers or platforms. With the current change, the same random number generator is used for all platforms and initialized to the same seed, so training output should be consistent.
1 parent 3c50710 commit a9890af

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/training/text2image.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <algorithm>
3232
#include <iostream>
3333
#include <map>
34+
#include <random>
3435
#include <string>
3536
#include <utility>
3637
#include <vector>
@@ -573,8 +574,11 @@ static int Main() {
573574
offset += step;
574575
offset += SpanUTF8Whitespace(str8 + offset);
575576
}
576-
if (FLAGS_render_ngrams)
577-
std::random_shuffle(offsets.begin(), offsets.end());
577+
if (FLAGS_render_ngrams) {
578+
std::seed_seq seed{kRandomSeed};
579+
std::mt19937 random_gen(seed);
580+
std::shuffle(offsets.begin(), offsets.end(), random_gen);
581+
}
578582

579583
for (size_t i = 0, line = 1; i < offsets.size(); ++i) {
580584
const char *curr_pos = str8 + offsets[i].first;

0 commit comments

Comments
 (0)