Skip to content

chore: experiment with swift-format for PR #174

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 3 commits into from
Mar 25, 2025
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
87 changes: 87 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--swiftversion 5.9
--acronyms ID,URL,UUID
--allman false
--anonymousforeach convert
--assetliterals visual-width
--asynccapturing
--beforemarks
--binarygrouping 4,8
--categorymark "MARK: %c"
--classthreshold 0
--closingparen balanced
--closurevoid remove
--commas always
--conflictmarkers reject
--decimalgrouping ignore
--elseposition same-line
--emptybraces spaced
--enumthreshold 0
--exponentcase lowercase
--exponentgrouping disabled
--extensionacl on-extension
--extensionlength 0
--extensionmark "MARK: - %t + %c"
--fractiongrouping disabled
--fragment false
--funcattributes preserve
--generictypes
--groupedextension "MARK: %c"
--guardelse auto
--header ignore
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef no-indent
--importgrouping alpha
--indent 4
--indentcase false
--indentstrings false
--lifecycle
--lineaftermarks true
--linebreaks lf
--markcategories true
--markextensions always
--marktypes always
--maxwidth none
--modifierorder
--nevertrailing
--nospaceoperators
--nowrapoperators
--octalgrouping 4,8
--onelineforeach ignore
--operatorfunc spaced
--organizetypes actor,class,enum,struct
--patternlet hoist
--ranges no-space
--redundanttype infer-locals-only
--self remove
--selfrequired
--semicolons inline
--shortoptionals always
--smarttabs enabled
--someany true
--stripunusedargs unnamed-only
--structthreshold 0
--tabwidth unspecified
--throwcapturing
--trailingclosures
--typeattributes preserve
--typeblanklines remove
--typemark "MARK: - %t"
--varattributes preserve
--voidtype void
--wraparguments preserve
--wrapcollections preserve
--wrapconditions preserve
--wrapeffects preserve
--wrapenumcases always
--wrapparameters preserve
--wrapreturntype preserve
--wrapternary default
--wraptypealiases preserve
--xcodeindentation disabled
--yodaswap always
--disable blankLineAfterImports,unusedArguments
--enable docComments
--disable enumnamespaces
--trimwhitespace nonblank-lines
--disable preferKeyPath
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.4.0")),
.package(url: "https://github.com/johnmai-dev/Jinja", .upToNextMinor(from: "1.1.0"))
.package(url: "https://github.com/johnmai-dev/Jinja", .upToNextMinor(from: "1.1.0")),
],
targets: [
.executableTarget(
name: "TransformersCLI",
dependencies: [
"Models", "Generation", "Tokenizers",
.product(name: "ArgumentParser", package: "swift-argument-parser")]),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.executableTarget(name: "HubCLI", dependencies: ["Hub", .product(name: "ArgumentParser", package: "swift-argument-parser")]),
.target(name: "Hub", resources: [.process("FallbackConfigs")]),
.target(name: "Tokenizers", dependencies: ["Hub", .product(name: "Jinja", package: "Jinja")]),
Expand Down
8 changes: 4 additions & 4 deletions Sources/Generation/Generation.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// Generation.swift
//
//
//
// Created by Pedro Cuenca on 7/5/23.
//

import Tokenizers
import CoreML
import TensorUtils
import Tokenizers

public enum GenerationMode {
case contrastiveSearch
Expand Down Expand Up @@ -57,7 +57,7 @@ public extension Generation {
let logitsProcessor = LogitsProcessor(logitsWarpers: logitsWarpers(config: config))
while outputTokens.count < config.maxLength {
let outputs = model(outputTokens, config)
/// `floats` can be much faster than `scalars` for a vector with stride 1, as it uses `memcpy` in that case
// `floats` can be much faster than `scalars` for a vector with stride 1, as it uses `memcpy` in that case
let logits = (outputs as? MLShapedArraySlice<Float>)?.floats ?? outputs.scalars as! [Float]
let (indexes, processedLogits) = logitsProcessor(logits)
let nextToken = Math.sample(indexes: indexes, probs: Math.softmax(processedLogits))
Expand Down Expand Up @@ -92,7 +92,7 @@ public extension Generation {

private func logitsWarpers(config: GenerationConfig) -> [any LogitsWarper] {
var logitsWarpers = [any LogitsWarper]()
if config.temperature > 0 && config.temperature != 1 {
if config.temperature > 0, config.temperature != 1 {
logitsWarpers.append(TemperatureLogitsWarper(temperature: Float(config.temperature)))
}
if config.topK > 0 {
Expand Down
22 changes: 11 additions & 11 deletions Sources/Generation/GenerationConfig.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GenerationConfig.swift
//
//
//
// Created by Pedro Cuenca on 7/5/23.
//
Expand All @@ -14,15 +14,15 @@ public struct GenerationConfig {
public var doSample = false
public var numBeams = 1
public var numBeamGroups = 1
public var penaltyAlpha: Double? = nil
public var penaltyAlpha: Double?
public var temperature = 1.0
public var topK = 50
public var topP = 1.0
public var repetitionPenalty = 1.0

public var padTokenId: Int? = nil
public var bosTokenId: Int? = nil
public var eosTokenId: Int? = nil
public var padTokenId: Int?
public var bosTokenId: Int?
public var eosTokenId: Int?

public init(maxLength: Int = 20, maxNewTokens: Int, doSample: Bool = false, numBeams: Int = 1, numBeamGroups: Int = 1, penaltyAlpha: Double? = nil, temperature: Double = 1.0, topK: Int = 50, topP: Double = 1.0, repetitionPenalty: Double = 1.0) {
self.maxLength = maxLength
Expand All @@ -41,18 +41,18 @@ public struct GenerationConfig {
public extension GenerationConfig {
var generationMode: GenerationMode {
// Exclude this case from the pattern matching below
if topK > 1 && !doSample && penaltyAlpha != nil && penaltyAlpha! > 0 {
if topK > 1, !doSample, penaltyAlpha != nil, penaltyAlpha! > 0 {
return .contrastiveSearch
}

switch (numBeams, numBeamGroups, doSample) {
case (1, 1, false) : return .greedy
case (1, 1, true) : return .sample
case (1, 1, false): return .greedy
case (1, 1, true): return .sample
case (2..., 1, false): return .beam
case (2..., 2..., _) : return .groupBeam
default : return .unsupported
case (2..., 2..., _): return .groupBeam
default: return .unsupported
}
}
}

extension GenerationConfig: Decodable {}
extension GenerationConfig: Decodable { }
29 changes: 14 additions & 15 deletions Sources/Hub/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
// See LICENSE at https://github.com/huggingface/swift-coreml-diffusers/LICENSE
//

import Foundation
import Combine
import Foundation

class Downloader: NSObject, ObservableObject {
private(set) var destination: URL

private let chunkSize = 10 * 1024 * 1024 // 10MB
private let chunkSize = 10 * 1024 * 1024 // 10MB

enum DownloadState {
case notStarted
Expand Down Expand Up @@ -53,7 +53,7 @@ class Downloader: NSObject, ObservableObject {
config.sessionSendsLaunchEvents = true
}

self.urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)

setupDownload(from: url, with: authToken, resumeSize: resumeSize, headers: headers, expectedSize: expectedSize, timeout: timeout, numRetries: numRetries)
}
Expand Down Expand Up @@ -106,14 +106,13 @@ class Downloader: NSObject, ObservableObject {
var requestHeaders = headers ?? [:]

// Populate header auth and range fields
if let authToken = authToken {
if let authToken {
requestHeaders["Authorization"] = "Bearer \(authToken)"
}
if resumeSize > 0 {
requestHeaders["Range"] = "bytes=\(resumeSize)-"
}


request.timeoutInterval = timeout
request.allHTTPHeaderFields = requestHeaders

Expand Down Expand Up @@ -157,7 +156,7 @@ class Downloader: NSObject, ObservableObject {
numRetries: Int,
expectedSize: Int?
) async throws {
guard let session = self.urlSession else {
guard let session = urlSession else {
throw DownloadError.unexpectedError
}

Expand Down Expand Up @@ -194,7 +193,7 @@ class Downloader: NSObject, ObservableObject {
buffer.removeAll(keepingCapacity: true)
downloadedSize += chunkSize
newNumRetries = 5
guard let expectedSize = expectedSize else { continue }
guard let expectedSize else { continue }
let progress = expectedSize != 0 ? Double(downloadedSize) / Double(expectedSize) : 0
downloadState.value = .downloading(progress)
}
Expand Down Expand Up @@ -227,7 +226,7 @@ class Downloader: NSObject, ObservableObject {

// Verify the downloaded file size matches the expected size
let actualSize = try tempFile.seekToEnd()
if let expectedSize = expectedSize, expectedSize != actualSize {
if let expectedSize, expectedSize != actualSize {
throw DownloadError.unexpectedError
}
}
Expand All @@ -239,16 +238,16 @@ class Downloader: NSObject, ObservableObject {
stateSubscriber = downloadState.sink { state in
switch state {
case .completed: semaphore.signal()
case .failed: semaphore.signal()
default: break
case .failed: semaphore.signal()
default: break
}
}
semaphore.wait()

switch downloadState.value {
case .completed(let url): return url
case .failed(let error): throw error
default: throw DownloadError.unexpectedError
case let .completed(url): return url
case let .failed(error): throw error
default: throw DownloadError.unexpectedError
}
}

Expand All @@ -265,15 +264,15 @@ extension Downloader: URLSessionDownloadDelegate {
func urlSession(_: URLSession, downloadTask _: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
// If the downloaded file already exists on the filesystem, overwrite it
try FileManager.default.moveDownloadedFile(from: location, to: self.destination)
try FileManager.default.moveDownloadedFile(from: location, to: destination)
downloadState.value = .completed(destination)
} catch {
downloadState.value = .failed(error)
}
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
if let error {
downloadState.value = .failed(error)
// } else if let response = task.response as? HTTPURLResponse {
// print("HTTP response status code: \(response.statusCode)")
Expand Down
Loading