File: 0018-automatically-detect-the-number-of-CPUs-in-batch-pro.patch

package info (click to toggle)
sentencepiece 0.1.97-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 54,944 kB
  • sloc: cpp: 186,945; python: 1,409; xml: 231; perl: 198; pascal: 50; makefile: 23; sh: 13
file content (252 lines) | stat: -rw-r--r-- 11,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
From: Taku Kudo <taku@google.com>
Date: Fri, 5 Aug 2022 14:47:02 +0900
Subject: automatically detect the number of CPUs in batch processing.

Signed-off-by: Kentaro Hayashi <kenhys@gmail.com>
---
 python/src/sentencepiece/__init__.py            | 27 +++++++++++++--------
 python/src/sentencepiece/sentencepiece.i        | 32 ++++++++++++++++---------
 python/src/sentencepiece/sentencepiece_wrap.cxx |  5 +++-
 python/test/sentencepiece_test.py               | 32 +++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/python/src/sentencepiece/__init__.py b/python/src/sentencepiece/__init__.py
index 12dc631..ce9d60d 100644
--- a/python/src/sentencepiece/__init__.py
+++ b/python/src/sentencepiece/__init__.py
@@ -97,6 +97,13 @@ class ImmutableSentencePieceText_ImmutableSentencePiece(object):
               'begin: {}\n'
               'end: {}\n').format(self.piece, self.id, self.surface,
                                   self.begin, self.end)
+
+    def __eq__(self, other):
+      return self.piece == other.piece and self.id == other.id and self.surface == other.surface and self.begin == other.begin and self.end == other.end
+
+    def __hash__(self):
+      return hash(str(self))
+
     __repr__ = __str__
 
 
@@ -395,7 +402,7 @@ class SentencePieceProcessor(object):
              enable_sampling=False,
              nbest_size=-1,
              alpha=0.1,
-             num_threads=1):
+             num_threads=-1):
       """Initialzie sentencepieceProcessor.
 
       Args:
@@ -407,15 +414,15 @@ class SentencePieceProcessor(object):
           reversing (if enabled).
         reverse: Reverses the tokenized sequence (Default = false)
         emit_unk_piece: Emits the unk literal string (Default = false)
-        nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
+        nbest_size: sampling parameters for unigram. Invalid in BPE-Dropout.
                     nbest_size = {0,1}: No sampling is performed.
                     nbest_size > 1: samples from the nbest_size results.
                     nbest_size < 0: assuming that nbest_size is infinite and samples
                       from the all hypothesis (lattice) using
                       forward-filtering-and-backward-sampling algorithm.
         alpha: Soothing parameter for unigram sampling, and dropout probability of
-          merge operations for BPE-dropout.
-        num_threads: number of threads in batch processing.
+               merge operations for BPE-dropout.
+        num_threads: number of threads in batch processing (Default = -1, auto-detected)
       """
 
       _sentencepiece_processor_init_native(self)
@@ -450,18 +457,18 @@ class SentencePieceProcessor(object):
         out_type: output type. int or str.
         add_bos: Add <s> to the result (Default = false)
         add_eos: Add </s> to the result (Default = false) <s>/</s> is added after
-          reversing (if enabled).
+                 reversing (if enabled).
         reverse: Reverses the tokenized sequence (Default = false)
         emit_unk_piece: Emits the unk literal string (Default = false)
-        nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
+        nbest_size: sampling parameters for unigram. Invalid in BPE-Dropout.
                     nbest_size = {0,1}: No sampling is performed.
                     nbest_size > 1: samples from the nbest_size results.
                     nbest_size < 0: assuming that nbest_size is infinite and samples
-                      from the all hypothesis (lattice) using
-                      forward-filtering-and-backward-sampling algorithm.
+                    from the all hypothesis (lattice) using
+                    forward-filtering-and-backward-sampling algorithm.
         alpha: Soothing parameter for unigram sampling, and merge probability for
                BPE-dropout (probablity 'p' in BPE-dropout paper).
-        num_threads: the number of threads used in the batch processin (Default = 1).
+        num_threads: the number of threads used in the batch processing (Default = -1).
       """
 
       if out_type is None:
@@ -722,7 +729,7 @@ class SentencePieceProcessor(object):
 
       Args:
         out_type: output type. str or 'serialized_proto' or 'immutable_proto' (Default = str)
-        num_threads: the number of threads used in the batch processin (Default = 1).
+        num_threads: the number of threads used in the batch processing (Default = -1).
       """
 
       if num_threads is None:
diff --git a/python/src/sentencepiece/sentencepiece.i b/python/src/sentencepiece/sentencepiece.i
index 8309fc2..e22f763 100644
--- a/python/src/sentencepiece/sentencepiece.i
+++ b/python/src/sentencepiece/sentencepiece.i
@@ -233,9 +233,12 @@ class ThreadPool {
 
 template <typename T>
 inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
+  if (*num_threads < 0) {
+    *num_threads = std::thread::hardware_concurrency();
+  }
   *num_threads = std::max<int>(1,
                                std::min<int>({*num_threads,
-                                   static_cast<int>(ins.size()), 256}));
+                                     static_cast<int>(ins.size()), 256}));
 }
 
 #define DEFINE_ENCODE_BATCH_FUNC_IMPL(FuncName, InType, OutType)        \
@@ -675,7 +678,7 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
            enable_sampling=False,
            nbest_size=-1,
            alpha=0.1,
-           num_threads=1):
+           num_threads=-1):
     """Initialzie sentencepieceProcessor.
 
     Args:
@@ -687,15 +690,15 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
         reversing (if enabled).
       reverse: Reverses the tokenized sequence (Default = false)
       emit_unk_piece: Emits the unk literal string (Default = false)
-      nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
+      nbest_size: sampling parameters for unigram. Invalid in BPE-Dropout.
                   nbest_size = {0,1}: No sampling is performed.
                   nbest_size > 1: samples from the nbest_size results.
                   nbest_size < 0: assuming that nbest_size is infinite and samples
                     from the all hypothesis (lattice) using
                     forward-filtering-and-backward-sampling algorithm.
       alpha: Soothing parameter for unigram sampling, and dropout probability of
-        merge operations for BPE-dropout.
-      num_threads: number of threads in batch processing.
+             merge operations for BPE-dropout.
+      num_threads: number of threads in batch processing (Default = -1, auto-detected)
     """
 
     _sentencepiece_processor_init_native(self)
@@ -730,18 +733,18 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
       out_type: output type. int or str.
       add_bos: Add <s> to the result (Default = false)
       add_eos: Add </s> to the result (Default = false) <s>/</s> is added after
-        reversing (if enabled).
+               reversing (if enabled).
       reverse: Reverses the tokenized sequence (Default = false)
       emit_unk_piece: Emits the unk literal string (Default = false)
-      nbest_size: sampling parameters for unigram. Invalid for BPE-Dropout.
+      nbest_size: sampling parameters for unigram. Invalid in BPE-Dropout.
                   nbest_size = {0,1}: No sampling is performed.
                   nbest_size > 1: samples from the nbest_size results.
                   nbest_size < 0: assuming that nbest_size is infinite and samples
-                    from the all hypothesis (lattice) using
-                    forward-filtering-and-backward-sampling algorithm.
+                  from the all hypothesis (lattice) using
+                  forward-filtering-and-backward-sampling algorithm.
       alpha: Soothing parameter for unigram sampling, and merge probability for
              BPE-dropout (probablity 'p' in BPE-dropout paper).
-      num_threads: the number of threads used in the batch processin (Default = 1).
+      num_threads: the number of threads used in the batch processing (Default = -1).
     """
 
     if out_type is None:
@@ -1002,7 +1005,7 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
 
     Args:
       out_type: output type. str or 'serialized_proto' or 'immutable_proto' (Default = str)
-      num_threads: the number of threads used in the batch processin (Default = 1).
+      num_threads: the number of threads used in the batch processing (Default = -1).
     """
 
     if num_threads is None:
@@ -1260,6 +1263,13 @@ inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
               'begin: {}\n'
               'end: {}\n').format(self.piece, self.id, self.surface,
                                   self.begin, self.end)
+
+    def __eq__(self, other):
+      return self.piece == other.piece and self.id == other.id and self.surface == other.surface and self.begin == other.begin and self.end == other.end
+
+    def __hash__(self):
+      return hash(str(self))
+
     __repr__ = __str__
   %}
 }
diff --git a/python/src/sentencepiece/sentencepiece_wrap.cxx b/python/src/sentencepiece/sentencepiece_wrap.cxx
index 0a8df5f..1eac211 100644
--- a/python/src/sentencepiece/sentencepiece_wrap.cxx
+++ b/python/src/sentencepiece/sentencepiece_wrap.cxx
@@ -3042,9 +3042,12 @@ class ThreadPool {
 
 template <typename T>
 inline void InitNumThreads(const std::vector<T> &ins, int *num_threads) {
+  if (*num_threads < 0) {
+    *num_threads = std::thread::hardware_concurrency();
+  }
   *num_threads = std::max<int>(1,
                                std::min<int>({*num_threads,
-                                   static_cast<int>(ins.size()), 256}));
+                                     static_cast<int>(ins.size()), 256}));
 }
 
 #define DEFINE_ENCODE_BATCH_FUNC_IMPL(FuncName, InType, OutType)        \
diff --git a/python/test/sentencepiece_test.py b/python/test/sentencepiece_test.py
index ed792bd..6cbe077 100755
--- a/python/test/sentencepiece_test.py
+++ b/python/test/sentencepiece_test.py
@@ -332,6 +332,29 @@ class TestSentencepieceProcessor(unittest.TestCase):
     self.assertEqual(s4, y4)
     self.assertEqual(s5, y5)
 
+    hset_piece = defaultdict(int)
+
+    # eq test
+    for i in range(len(s1.pieces)):
+      self.assertEqual(s1.pieces[i], t1.pieces[i])
+      hset_piece[s1.pieces[i]] += 1
+      hset_piece[t1.pieces[i]] += 1
+
+    self.assertEqual(len(hset_piece), len(s1.pieces))
+
+    # has test
+    hset = defaultdict(int)
+    hset[s1] += 1
+    hset[t1] += 1
+    hset[s3] += 1
+    hset[t3] += 1
+
+    self.assertEqual(len(hset), 2)
+    self.assertEqual(hset[s1], 2)
+    self.assertEqual(hset[s3], 2)
+    self.assertEqual(hset[t1], 2)
+    self.assertEqual(hset[t3], 2)
+
     x1 = self.sp_.encode_as_serialized_proto(text)
     x2 = self.sp_.sample_encode_as_serialized_proto(text, 10, 0.2)
     x3 = self.sp_.nbest_encode_as_serialized_proto(text, 10)
@@ -363,6 +386,15 @@ class TestSentencepieceProcessor(unittest.TestCase):
       pieces.append(s1.pieces[i].piece)
     self.assertEqual(pieces, v2)
 
+    for v in s3.nbests:
+      self.assertEqual(text, v.text)
+      self.assertEqual(self.sp_.Decode([x.id for x in v.pieces]), text)
+
+    for i in range(len(s3.nbests)):
+      self.assertEqual(text, s3.nbests[i].text)
+      self.assertEqual(
+          self.sp_.Decode([x.id for x in s3.nbests[i].pieces]), text)
+
     # Japanese offset
     s1 = self.jasp_.EncodeAsImmutableProto('吾輩は猫である。Hello world. ABC 123')
     surfaces1 = [s1.text[x.begin:x.end] for x in s1.pieces]