Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch swift-tokenizers to main, remove some workarounds #26

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 1 addition & 42 deletions Libraries/LLM/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,13 @@ public func loadTokenizer(configuration: ModelConfiguration) async throws -> Tok
tokenizerConfig = Config(dictionary)
}

// workaround: some merges can't be split on space in BPETokenizer
if let tokenizerClass = tokenizerConfig.tokenizerClass?.stringValue {
switch tokenizerClass {
case "T5Tokenizer":
break
default:
tokenizerData = discardUnhandledMerges(tokenizerData: tokenizerData)
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


let impl = try PreTrainedTokenizer(
tokenizerConfig: tokenizerConfig, tokenizerData: tokenizerData)

return Tokenizer(tokenizer: impl, tokenizerConfig: tokenizerConfig)
}

public func discardUnhandledMerges(tokenizerData: Config) -> Config {
// see https://github.com/ml-explore/mlx-swift-examples/issues/1
// and https://github.com/huggingface/swift-transformers/issues/51

if let model = tokenizerData.model {
if let merges = model.dictionary["merges"] as? [String] {
// discard any merges that can't be split on a space
// (required by BPETokenizer)
let newMerges =
merges
.filter {
$0.split(separator: " ").count == 2
}

if newMerges.count != merges.count {
var newModel = model.dictionary
newModel["merges"] = newMerges

var newTokenizerData = tokenizerData.dictionary
newTokenizerData["model"] = newModel

return Config(newTokenizerData)
}
}
}

return tokenizerData
}

/// overrides for TokenizerModel/knownTokenizers
let replacementTokenizers = [
"CodeLlamaTokenizer": "LlamaTokenizer",
"GemmaTokenizer": "PreTrainedTokenizer",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Qwen2Tokenizer": "PreTrainedTokenizer",
"Qwen2Tokenizer": "PreTrainedTokenizer"
]
4 changes: 2 additions & 2 deletions mlx-swift-examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2220,8 +2220,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/huggingface/swift-transformers";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.1.2;
branch = main;
kind = branch;
};
};
C392736E2B60699100368D5D /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/huggingface/swift-transformers",
"state" : {
"revision" : "564442fba36b0b694d730a62d0593e5f54043b55",
"version" : "0.1.2"
"branch" : "main",
"revision" : "24605a8c0cc974bec5b94a6752eb687bae77db31"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch to main of swift-transformers, track a particular commit hash (the current head) -- we can move this forward as changes come in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed tag 0.1.3 in case it helps.

}
}
],
Expand Down