Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 53d2a8e

Browse files
committed
Address comments
1 parent e1f9433 commit 53d2a8e

4 files changed

Lines changed: 49 additions & 38 deletions

File tree

scripts/word_embeddings/tools/cooccur.cc

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ std::ostream &operator<<(std::ostream &in,
5959
return in << static_cast<int>(context_weight);
6060
}
6161

62+
// Arguments specified via command line options. See ParseArgs for documentation.
6263
struct Arguments {
6364
unsigned int num_threads = 1;
64-
unsigned int windowSize = 5;
65-
bool noSymmetric = false;
65+
unsigned int window_size = 5;
66+
bool no_symmetric = false;
6667
bool subsample = false;
6768
ContextWeight context_weight;
6869
};
6970

70-
auto parseArgs(int argc, char **argv) {
71+
auto ParseArgs(int argc, char **argv) {
7172
// Performance optimizations for writing to stdout
7273
std::ios::sync_with_stdio(false);
7374

@@ -79,9 +80,9 @@ auto parseArgs(int argc, char **argv) {
7980
app.add_option("-o,--output", output,
8081
"Output file name. Co-occurence matrix is saved as "
8182
"scipy.sparse compatible CSR matrix in a numpy .npz archive");
82-
app.add_option("-w,--window-size", args.windowSize,
83+
app.add_option("-w,--window-size", args.window_size,
8384
"Window size in which to count co-occurences.");
84-
app.add_flag("--no-symmetric", args.noSymmetric,
85+
app.add_flag("--no-symmetric", args.no_symmetric,
8586
"If not specified, a symmetric context window is used.");
8687
app.add_flag("--subsample", args.subsample,
8788
"Apply subsampling during co-occurence matrix construction as "
@@ -178,7 +179,7 @@ class CircularBuffer : public ranges::view_facade<CircularBuffer<T>> {
178179
};
179180

180181
// * Input
181-
auto readVocab() {
182+
auto ReadVocab() {
182183
std::string word;
183184
std::string count;
184185
int rank{0};
@@ -194,11 +195,11 @@ auto readVocab() {
194195
std::mutex paths_m;
195196
std::mutex matrices_m;
196197

197-
void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
198+
void ReadMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
198199
const Vocab &vocab, const Arguments &args, uint32_t seed) {
199200
assert(seed > 0);
200201
std::string line;
201-
CircularBuffer<uint32_t> history(args.windowSize);
202+
CircularBuffer<uint32_t> history(args.window_size);
202203
std::unique_ptr<Matrix> m = std::make_unique<Matrix>();
203204

204205
// Prepare subsampling
@@ -255,7 +256,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
255256
uint64_t key; // We merge 32 bit row and col indices to a single 64
256257
// bit key
257258
// For symmetric contexts, only store one direction.
258-
if (!args.noSymmetric) {
259+
if (!args.no_symmetric) {
259260
if (word_rank <= context_word_rank) {
260261
key = (uint64_t)word_rank << 32 | context_word_rank;
261262
} else {
@@ -268,7 +269,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
268269
if (args.context_weight == ContextWeight::Harmonic) {
269270
(*m)[key] += 1.0f / static_cast<count_type>(distance);
270271
} else if (args.context_weight == ContextWeight::DistanceOverSize) {
271-
(*m)[key] += (args.windowSize - distance - 1) / args.windowSize;
272+
(*m)[key] += (args.window_size - distance - 1) / args.window_size;
272273
} else {
273274
(*m)[key]++;
274275
}
@@ -286,7 +287,7 @@ void readMatrix(std::queue<fs::path> &paths, queue<Matrix> &matrices,
286287
}
287288
}
288289

289-
std::unique_ptr<Matrix> combineMatrices(queue<Matrix> &matrices,
290+
std::unique_ptr<Matrix> CombineMatrices(queue<Matrix> &matrices,
290291
int num_threads) {
291292
std::unique_ptr<Matrix> m1 = matrices.pop();
292293
for (int i = 1; i < num_threads; i++) {
@@ -305,24 +306,24 @@ std::unique_ptr<Matrix> combineMatrices(queue<Matrix> &matrices,
305306
return m1;
306307
}
307308

308-
auto computeCooccurrenceMatrix(Vocab &vocab, std::queue<fs::path> &paths,
309+
auto ComputeCooccurrenceMatrix(Vocab &vocab, std::queue<fs::path> &paths,
309310
const Arguments &args) {
310311
std::vector<std::thread> threads;
311312
queue<Matrix> matrices;
312313
for (unsigned int i = 0; i < args.num_threads; i++) {
313314
threads.push_back(std::thread([&paths, &matrices, &vocab, &args, i]() {
314-
readMatrix(std::ref(paths), std::ref(matrices), std::ref(vocab),
315+
ReadMatrix(std::ref(paths), std::ref(matrices), std::ref(vocab),
315316
std::ref(args), i + 1);
316317
}));
317318
}
318-
std::unique_ptr<Matrix> m = combineMatrices(matrices, args.num_threads);
319+
std::unique_ptr<Matrix> m = CombineMatrices(matrices, args.num_threads);
319320
for (unsigned int i = 0; i < args.num_threads; i++) {
320321
threads[i].join();
321322
}
322323
return m;
323324
}
324325

325-
auto toCOO(const Vocab &vocab, std::unique_ptr<Matrix> m) {
326+
auto ToCOO(const Vocab &vocab, std::unique_ptr<Matrix> m) {
326327
size_t num_tokens = vocab.size();
327328
size_t nnz = m->size();
328329
std::cout << "Got " << nnz
@@ -343,7 +344,7 @@ auto toCOO(const Vocab &vocab, std::unique_ptr<Matrix> m) {
343344
}
344345

345346
// * Output
346-
void writeNumpy(const std::string output, const std::vector<uint32_t> &row,
347+
void WriteNumpy(const std::string output, const std::vector<uint32_t> &row,
347348
const std::vector<uint32_t> &col,
348349
const std::vector<count_type> &data, const bool symmetric,
349350
const uint32_t num_tokens) {
@@ -359,10 +360,10 @@ void writeNumpy(const std::string output, const std::vector<uint32_t> &row,
359360

360361
// * Main
361362
int main(int argc, char **argv) {
362-
auto [paths, output, args] = parseArgs(argc, argv);
363-
auto vocab = readVocab();
364-
auto cooccurenceMatrix = computeCooccurrenceMatrix(vocab, paths, args);
365-
auto [row, col, data] = toCOO(vocab, std::move(cooccurenceMatrix));
366-
writeNumpy(output, row, col, data, !args.noSymmetric, vocab.size());
363+
auto [paths, output, args] = ParseArgs(argc, argv);
364+
auto vocab = ReadVocab();
365+
auto cooccurenceMatrix = ComputeCooccurrenceMatrix(vocab, paths, args);
366+
auto [row, col, data] = ToCOO(vocab, std::move(cooccurenceMatrix));
367+
WriteNumpy(output, row, col, data, !args.no_symmetric, vocab.size());
367368
return 0;
368369
}

scripts/word_embeddings/tools/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ template <typename T> class queue {
1414
std::deque<std::unique_ptr<T>> d_queue;
1515

1616
public:
17+
// Add a value to queue in a thread-safe manner.
1718
void push(std::unique_ptr<T> value) {
1819
{
1920
std::unique_lock<std::mutex> lock(this->d_mutex);
2021
d_queue.push_front(std::move(value));
2122
}
2223
this->d_condition.notify_one();
2324
}
25+
26+
// Remove and return a value from the queue in a thread-safe manner (FIFO).
27+
// Blocks if there is no value in the Queue.
2428
std::unique_ptr<T> pop() {
2529
std::unique_lock<std::mutex> lock(this->d_mutex);
2630
this->d_condition.wait(lock, [&] { return !this->d_queue.empty(); });

scripts/word_embeddings/tools/vocab_count.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ using Vocab = spp::sparse_hash_map<std::string, uint32_t>;
4444
std::mutex paths_m;
4545
std::mutex vocabs_m;
4646

47-
void readVocab(std::queue<fs::path> &paths, queue<Vocab> &vocabs) {
47+
void ReadVocab(std::queue<fs::path> &paths, queue<Vocab> &vocabs) {
4848
std::unique_ptr<Vocab> vocab = std::make_unique<Vocab>();
4949
std::string word;
5050
fs::path path;
@@ -73,9 +73,9 @@ void readVocab(std::queue<fs::path> &paths, queue<Vocab> &vocabs) {
7373
}
7474
}
7575

76-
std::unique_ptr<Vocab> combineVocabs(queue<Vocab> &vocabs, int numThreads) {
76+
std::unique_ptr<Vocab> CombineVocabs(queue<Vocab> &vocabs, int num_threads) {
7777
std::unique_ptr<Vocab> vocab1 = vocabs.pop();
78-
for (int i = 1; i < numThreads; i++) {
78+
for (int i = 1; i < num_threads; i++) {
7979
std::unique_ptr<Vocab> vocab2 = vocabs.pop();
8080
if (vocab1->size() < vocab2->size()) {
8181
for (const auto &e : *vocab1) {
@@ -102,9 +102,9 @@ int main(int argc, char **argv) {
102102
app.add_option("-c,--minCount", minCount,
103103
"Minimum number of occurences required for a word to be "
104104
"included in the vocabulary.");
105-
unsigned int numThreads = 1;
105+
unsigned int num_threads = 1;
106106
app.add_option(
107-
"-j,--numThreads", numThreads,
107+
"-j,--num_threads", num_threads,
108108
"Number of threads to use. Each thread constructs an "
109109
"independent vocabulary which are finally merged. Only appropriate "
110110
"when multiple, sufficiently large input files are specified.")
@@ -117,26 +117,26 @@ int main(int argc, char **argv) {
117117
}
118118
std::vector<std::thread> threads;
119119
queue<Vocab> vocabs;
120-
for (unsigned int i = 0; i < numThreads; i++) {
120+
for (unsigned int i = 0; i < num_threads; i++) {
121121
threads.push_back(std::thread(
122-
[&paths, &vocabs]() { readVocab(std::ref(paths), std::ref(vocabs)); }));
122+
[&paths, &vocabs]() { ReadVocab(std::ref(paths), std::ref(vocabs)); }));
123123
}
124-
std::unique_ptr<Vocab> vocab = combineVocabs(vocabs, numThreads);
125-
for (unsigned int i = 0; i < numThreads; i++) {
124+
std::unique_ptr<Vocab> vocab = CombineVocabs(vocabs, num_threads);
125+
for (unsigned int i = 0; i < num_threads; i++) {
126126
threads[i].join();
127127
}
128128

129129
// Sort
130130
typedef std::function<bool(std::pair<std::string, int>,
131131
std::pair<std::string, int>)>
132132
Comparator;
133-
Comparator compFunctor = [](std::pair<std::string, int> elem1,
133+
Comparator CompFunctor = [](std::pair<std::string, int> elem1,
134134
std::pair<std::string, int> elem2) {
135135
return (elem1.second > elem2.second) ||
136136
(elem1.second == elem2.second && elem1.first < elem2.first);
137137
};
138138
std::set<std::pair<std::string, uint32_t>, Comparator> sorted_vocab(
139-
vocab->begin(), vocab->end(), compFunctor);
139+
vocab->begin(), vocab->end(), CompFunctor);
140140
vocab.reset(); // Release ownership
141141

142142
// Output

scripts/word_embeddings/train_glove.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,18 @@ def hybrid_forward(self, F, row, col, counts):
237237
238238
Parameters
239239
----------
240-
row : mx.nd.NDArray
241-
Array of token indices for source words
242-
row : mx.nd.NDArray
243-
Array of token indices for context words
244-
counts : mx.nd.NDArray
245-
Their co-occurrence counts.
240+
row : mxnet.nd.NDArray or mxnet.sym.Symbol
241+
Array of token indices for source words. Shape (batch_size, ).
242+
row : mxnet.nd.NDArray or mxnet.sym.Symbol
243+
Array of token indices for context words. Shape (batch_size, ).
244+
counts : mxnet.nd.NDArray or mxnet.sym.Symbol
245+
Their co-occurrence counts. Shape (batch_size, ).
246+
247+
Returns
248+
-------
249+
mxnet.nd.NDArray or mxnet.sym.Symbol
250+
Loss. Shape (batch_size, ).
251+
246252
"""
247253

248254
emb_in = self.source_embedding(row)

0 commit comments

Comments
 (0)