-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split by regexp with capture groups The other split helpers we have don't work for capture groups. We had to resort to raw `NSRegularExpression`s * Build added tokens split regexp, shortcut before pre-tokenization * Update PreTokenizers so Metaspace can conditionally act * Create LlamaPreTrainedTokenizer subclass We need some custom behaviour that's not in the config :( * Rename test * Replace with enum for future extensibility
- Loading branch information
Showing
3 changed files
with
200 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// AddedTokensTests.swift | ||
// | ||
// | ||
// Created by Pedro Cuenca on 20240426. | ||
// | ||
|
||
import XCTest | ||
import Tokenizers | ||
import Hub | ||
|
||
class AddedTokensTests: XCTestCase { | ||
func testPhiAddedTokens() async throws { | ||
let tokenizer = try await AutoTokenizer.from(pretrained: "mlx-community/Phi-3-mini-128k-instruct-4bit") | ||
let inputIds = tokenizer("This is the <|end|>. My only friend, the <|end|>") | ||
XCTAssertEqual(inputIds, [1, 910, 338, 278, 29871, 32007, 29889, 1619, 871, 5121, 29892, 278, 29871, 32007]) | ||
|
||
let decoded = tokenizer.decode(tokens: inputIds) | ||
XCTAssertEqual(decoded, "<s> This is the <|end|>. My only friend, the <|end|>") | ||
} | ||
|
||
func testSplitWithCaptureGroups() { | ||
let addedTokensRegexp = #"(<\|end\|>)\s*|(<\|raw\|>)\s*"# | ||
let captureRegex = try! NSRegularExpression(pattern: addedTokensRegexp, options: []) | ||
|
||
XCTAssertEqual( | ||
"eating <|raw|> meat <|end|> That's all".split(by: captureRegex), | ||
["eating ", "<|raw|>", "meat ", "<|end|>", "That's all"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"<|raw|>".split(by: captureRegex), | ||
["<|raw|>"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"This string doesn't have those separators".split(by: captureRegex), | ||
["This string doesn't have those separators"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"start <|end|>".split(by: captureRegex), | ||
["start ", "<|end|>"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"start <|end|> ".split(by: captureRegex), | ||
["start ", "<|end|>"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"start <|end|> ".split(by: captureRegex), | ||
["start ", "<|end|>"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"start <|end|> for real".split(by: captureRegex), | ||
["start ", "<|end|>", "for real"] | ||
) | ||
|
||
XCTAssertEqual( | ||
"<|raw|><|end|>".split(by: captureRegex), | ||
["<|raw|>", "<|end|>"] | ||
) | ||
|
||
} | ||
} |