Skip to content

Commit a3a6d7b

Browse files
authored
Fix non-thread-safe use of Random in tokenizers (dotnet#6695)
1 parent 0278f43 commit a3a6d7b

File tree

1 file changed

+3
-3
lines changed
  • src/Microsoft.ML.Tokenizers/Model

1 file changed

+3
-3
lines changed

src/Microsoft.ML.Tokenizers/Model/Word.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Linq;
76
using System.Collections.Generic;
87
using System.Text;
98

109
namespace Microsoft.ML.Tokenizers
1110
{
1211
internal struct Word
1312
{
14-
private static readonly Random _random = new Random();
13+
[ThreadStatic]
14+
private static Random? _random;
1515
private Vec<Symbol> _symbols;
1616

1717
public Word() => _symbols = new Vec<Symbol>();
@@ -115,7 +115,7 @@ public void MergeAll(Dictionary<Pair<int>, (int, int)> merges, float? dropout)
115115
while (queue.Count > 0)
116116
{
117117
Merge top = queue.Dequeue();
118-
if (dropout.HasValue && _random.NextDouble() < dropout)
118+
if (dropout.HasValue && (_random ??= new()).NextDouble() < dropout)
119119
{
120120
skip.Push(top);
121121
}

0 commit comments

Comments
 (0)