-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8855129
commit c46d0c2
Showing
19 changed files
with
894 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
import SentencepieceTokenizer | ||
@preconcurrency import Tokenizers | ||
|
||
public protocol TextTokenizer: Sendable { | ||
func tokenizeText(_ text: String, maxLength: Int) throws -> [Int32] | ||
func tokenizeTextsPaddingToLongest( | ||
_ texts: [String], padTokenId: Int, maxLength: Int | ||
) throws -> [[Int32]] | ||
} | ||
|
||
extension TextTokenizer { | ||
public func tokenizeTextsPaddingToLongest( | ||
_ texts: [String], | ||
padTokenId: Int, | ||
maxLength: Int | ||
) throws -> [[Int32]] { | ||
var longest = 0 | ||
var result = [[Int32]]() | ||
result.reserveCapacity(texts.count) | ||
for text in texts { | ||
let encoded = try tokenizeText(text, maxLength: maxLength) | ||
longest = max(longest, encoded.count) | ||
result.append(encoded) | ||
} | ||
return result.map { | ||
if $0.count < longest { | ||
return $0 + Array(repeating: Int32(padTokenId), count: longest - $0.count) | ||
} else { | ||
return $0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
public struct TokenizerWrapper { | ||
private let tokenizer: any Tokenizers.Tokenizer | ||
|
||
public init(_ tokenizer: any Tokenizers.Tokenizer) { | ||
self.tokenizer = tokenizer | ||
} | ||
} | ||
|
||
extension TokenizerWrapper: TextTokenizer { | ||
public func tokenizeText(_ text: String, maxLength: Int) throws -> [Int32] { | ||
var encoded = tokenizer.encode(text: text) | ||
if encoded.count > maxLength { | ||
encoded.removeLast(encoded.count - maxLength) | ||
} | ||
return encoded.map { Int32($0) } | ||
} | ||
} |
Oops, something went wrong.