-
Notifications
You must be signed in to change notification settings - Fork 0
다운로드 로직을 리펙토링 합니다. #268
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
Merged
Merged
다운로드 로직을 리펙토링 합니다. #268
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dda425d
refactor(app): whisper load 타이밍 변경
Kimyonhae e187a0b
refactor(data): whisper 영속성 버그
Kimyonhae de65557
refactor(all): OnDeviceStatus runTime 삭제
Kimyonhae eae62c9
refactor(domain): download 중복 방지 코드 적용
Kimyonhae 040814a
refactor(doamin): swiftformat 스타일 적용
Kimyonhae 904e0c6
refactor(data): Downloader , Tokenizer 리펙토링
Kimyonhae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
Data/Sources/Infrastructure/OnDevice/MLXSupport/MLXHubDownloader.swift
This file contains hidden or 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,36 @@ | ||
| import Foundation | ||
| import HuggingFace | ||
| import MLXHuggingFace | ||
| import MLXLMCommon | ||
|
|
||
| /// Hugging Face Hub에서 모델 파일을 다운로드하기 위한 Downloader 구현체. | ||
| /// 컴파일 타임 매크로(#hubDownloader) 대신 사용되는 수동 구현체입니다. | ||
| public struct MLXHubDownloader: MLXLMCommon.Downloader { | ||
| private let upstream: HuggingFace.HubClient | ||
|
|
||
| public init(hubClient: HuggingFace.HubClient = HuggingFace.HubClient()) { | ||
| upstream = hubClient | ||
| } | ||
|
|
||
| public func download( | ||
| id: String, | ||
| revision: String?, | ||
| matching patterns: [String], | ||
| useLatest: Bool, | ||
| progressHandler: @Sendable @escaping (Foundation.Progress) -> Void | ||
| ) async throws -> URL { | ||
| guard let repoID = HuggingFace.Repo.ID(rawValue: id) else { | ||
| throw HuggingFaceDownloaderError.invalidRepositoryID(id) | ||
| } | ||
| let revision = revision ?? "main" | ||
|
|
||
| return try await upstream.downloadSnapshot( | ||
| of: repoID, | ||
| revision: revision, | ||
| matching: patterns, | ||
| progressHandler: { @MainActor progress in | ||
| progressHandler(progress) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
Data/Sources/Infrastructure/OnDevice/MLXSupport/MLXTokenizerLoader.swift
This file contains hidden or 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,64 @@ | ||
| import Foundation | ||
| import MLXLMCommon | ||
| import Tokenizers | ||
|
|
||
| /// Hugging Face 기반 토크나이저를 로드하기 위한 TokenizerLoader 구현체. | ||
| /// 컴파일 타임 매크로(#huggingFaceTokenizerLoader) 대신 사용되는 수동 구현체입니다. | ||
| public struct MLXTokenizerLoader: MLXLMCommon.TokenizerLoader { | ||
| public init() {} | ||
|
|
||
| public func load(from directory: URL) async throws -> any MLXLMCommon.Tokenizer { | ||
| let upstream = try await Tokenizers.AutoTokenizer.from(modelFolder: directory) | ||
| return TokenizerBridge(upstream) | ||
| } | ||
| } | ||
|
|
||
| private struct TokenizerBridge: MLXLMCommon.Tokenizer { | ||
| private let upstream: any Tokenizers.Tokenizer | ||
|
|
||
| init(_ upstream: any Tokenizers.Tokenizer) { | ||
| self.upstream = upstream | ||
| } | ||
|
|
||
| func encode(text: String, addSpecialTokens: Bool) -> [Int] { | ||
| upstream.encode(text: text, addSpecialTokens: addSpecialTokens) | ||
| } | ||
|
|
||
| func decode(tokenIds: [Int], skipSpecialTokens: Bool) -> String { | ||
| upstream.decode(tokens: tokenIds, skipSpecialTokens: skipSpecialTokens) | ||
| } | ||
|
|
||
| func convertTokenToId(_ token: String) -> Int? { | ||
| upstream.convertTokenToId(token) | ||
| } | ||
|
|
||
| func convertIdToToken(_ id: Int) -> String? { | ||
| upstream.convertIdToToken(id) | ||
| } | ||
|
|
||
| var bosToken: String? { | ||
| upstream.bosToken | ||
| } | ||
|
|
||
| var eosToken: String? { | ||
| upstream.eosToken | ||
| } | ||
|
|
||
| var unknownToken: String? { | ||
| upstream.unknownToken | ||
| } | ||
|
|
||
| func applyChatTemplate( | ||
| messages: [[String: any Sendable]], | ||
| tools: [[String: any Sendable]]?, | ||
| additionalContext: [String: any Sendable]? | ||
| ) throws -> [Int] { | ||
| do { | ||
| return try upstream.applyChatTemplate( | ||
| messages: messages, tools: tools, additionalContext: additionalContext | ||
| ) | ||
| } catch Tokenizers.TokenizerError.missingChatTemplate { | ||
| throw MLXLMCommon.TokenizerError.missingChatTemplate | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.