|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Buffers; |
| 7 | + |
| 8 | +namespace Microsoft.ML.Tokenizers |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Normalize the string to lowercase form before processing it with the tokenizer. |
| 12 | + /// </summary> |
| 13 | + public sealed class LlamaNormalizer : Normalizer |
| 14 | + { |
| 15 | + internal const char DummyPrefix = '\u2581'; // '▁' (LOWER ONE EIGHT BLOCK) |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Creates a LowerCaseNormalizer object. |
| 19 | + /// </summary> |
| 20 | + public LlamaNormalizer(bool removeExtraWhiteSpaces, bool addDummyPrefix, bool escapeWhiteSpaces, bool treatWhitespaceAsSuffix) |
| 21 | + { |
| 22 | + RemoveExtraWhiteSpaces = removeExtraWhiteSpaces; |
| 23 | + AddDummyPrefix = addDummyPrefix; |
| 24 | + EscapeWhiteSpaces = escapeWhiteSpaces; |
| 25 | + TreatWhitespaceAsSuffix = treatWhitespaceAsSuffix; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Indicate removing extra white spaces from the original string during the normalization. |
| 30 | + /// </summary> |
| 31 | + public bool RemoveExtraWhiteSpaces { get; } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Indicate emitting the dummy prefix character U+2581 at the beginning of sentence token during the encoding. |
| 35 | + /// </summary> |
| 36 | + public bool AddDummyPrefix { get; } |
| 37 | + |
| 38 | + public bool EscapeWhiteSpaces { get; } |
| 39 | + |
| 40 | + public bool TreatWhitespaceAsSuffix { get; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Normalize the original string according to SentencePiece normalization with Llama model. |
| 44 | + /// </summary> |
| 45 | + /// <param name="original">The original string to normalize.</param> |
| 46 | + /// <returns>The normalized string.</returns> |
| 47 | + public override string Normalize(string original) |
| 48 | + { |
| 49 | + if (string.IsNullOrEmpty(original)) |
| 50 | + { |
| 51 | + return string.Empty; |
| 52 | + } |
| 53 | + |
| 54 | + int startIndex = 0; |
| 55 | + int endIndex = original.Length - 1; |
| 56 | + |
| 57 | + if (RemoveExtraWhiteSpaces) |
| 58 | + { |
| 59 | + while (startIndex <= endIndex && original[startIndex] == ' ') |
| 60 | + { |
| 61 | + startIndex++; |
| 62 | + } |
| 63 | + |
| 64 | + while (endIndex >= startIndex && original[endIndex] == ' ') |
| 65 | + { |
| 66 | + endIndex--; |
| 67 | + } |
| 68 | + |
| 69 | + if (startIndex == endIndex) |
| 70 | + { |
| 71 | + return string.Empty; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + int length = endIndex - startIndex + 1; |
| 76 | + |
| 77 | + Span<char> span = stackalloc char[512]; |
| 78 | + char[]? buffer = null; |
| 79 | + |
| 80 | + if (span.Length < length + 1) |
| 81 | + { |
| 82 | + // Add dummy prefix if needed |
| 83 | + buffer = ArrayPool<char>.Shared.Rent(AddDummyPrefix ? length + 1 : length); |
| 84 | + span = buffer; |
| 85 | + } |
| 86 | + |
| 87 | + int bufferIndex = 0; |
| 88 | + if (AddDummyPrefix && !TreatWhitespaceAsSuffix) |
| 89 | + { |
| 90 | + span[bufferIndex++] = EscapeWhiteSpaces ? DummyPrefix : ' '; |
| 91 | + } |
| 92 | + |
| 93 | + while (startIndex <= endIndex) |
| 94 | + { |
| 95 | + char c = original[startIndex++]; |
| 96 | + if (c == ' ') |
| 97 | + { |
| 98 | + span[bufferIndex++] = EscapeWhiteSpaces ? DummyPrefix : c; |
| 99 | + |
| 100 | + if (RemoveExtraWhiteSpaces) |
| 101 | + { |
| 102 | + while (startIndex <= endIndex && original[startIndex] == ' ') |
| 103 | + { |
| 104 | + startIndex++; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + span[bufferIndex++] = c; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (AddDummyPrefix && TreatWhitespaceAsSuffix) |
| 115 | + { |
| 116 | + span[bufferIndex++] = EscapeWhiteSpaces ? DummyPrefix : ' '; |
| 117 | + } |
| 118 | + |
| 119 | + string result = span.Slice(0, bufferIndex).ToString(); |
| 120 | + |
| 121 | + if (buffer is not null) |
| 122 | + { |
| 123 | + ArrayPool<char>.Shared.Return(buffer); |
| 124 | + } |
| 125 | + return result; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments