Skip to content
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
3 changes: 3 additions & 0 deletions Sources/Generation/Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Pedro Cuenca on 7/5/23.
//

#if canImport(CoreML)
import CoreML

import Tokenizers

public enum GenerationMode {
Expand Down Expand Up @@ -106,3 +108,4 @@ public extension Generation {
return logitsWarpers
}
}
#endif // canImport(CoreML)
2 changes: 2 additions & 0 deletions Sources/Generation/LogitsWarper/TopKLogitsWarper.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if canImport(Accelerate)
import Accelerate
import Foundation

Expand Down Expand Up @@ -56,3 +57,4 @@ public struct TopKLogitsWarper: LogitsWarper {
return (indices: topkIndices.map { indices[Int($0)] }, logits: topkLogits)
}
}
#endif // canImport(Accelerate)
2 changes: 2 additions & 0 deletions Sources/Generation/MLMultiArray+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2019 Hugging Face. All rights reserved.
//

#if canImport(CoreML)
import CoreML
import Foundation

Expand Down Expand Up @@ -196,3 +197,4 @@ extension MLMultiArray {
return s + "]"
}
}
#endif // canImport(CoreML)
2 changes: 2 additions & 0 deletions Sources/Generation/MLShapedArray+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Pedro Cuenca on 13/5/23.
//

#if canImport(CoreML)
import CoreML

public extension MLShapedArray<Float> {
Expand Down Expand Up @@ -50,3 +51,4 @@ public extension MLMultiArray {
}
}
}
#endif // canImport(CoreML)
4 changes: 3 additions & 1 deletion Sources/Generation/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2019 Hugging Face. All rights reserved.
//

#if canImport(CoreML) && canImport(Accelerate)
import Accelerate
import CoreML
import Foundation
Expand All @@ -15,7 +16,7 @@ import Foundation
///
/// https://github.com/hollance/CoreMLHelpers
///
public struct Math {
public enum Math {
/**
Returns the index and value of the largest element in the array.

Expand Down Expand Up @@ -168,3 +169,4 @@ public extension Math {
}
}
}
#endif // canImport(CoreML) && canImport(Accelerate)
4 changes: 4 additions & 0 deletions Sources/Models/LanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Pedro Cuenca on 7/5/23.
//

#if canImport(CoreML)
import CoreML

import Generation
import Hub
import Tokenizers
Expand Down Expand Up @@ -226,3 +228,5 @@ public enum TokenizerError: LocalizedError {
}
}
}

#endif // canImport(CoreML)
3 changes: 3 additions & 0 deletions Sources/Models/LanguageModelTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// Created by Pedro Cuenca on 8/5/23.
//

#if canImport(CoreML)
import CoreML

import Generation
import Tokenizers

Expand Down Expand Up @@ -40,3 +42,4 @@ public extension TextGenerationModel {
try await generate(config: config, prompt: prompt, model: callAsFunction, tokenizer: tokenizer, callback: callback)
}
}
#endif // canImport(CoreML)
2 changes: 2 additions & 0 deletions Sources/Models/Weights.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if canImport(CoreML)
import CoreML

public struct Weights {
Expand Down Expand Up @@ -91,3 +92,4 @@ struct Safetensor {
return Weights(dict)
}
}
#endif // canImport(CoreML)
2 changes: 2 additions & 0 deletions Sources/TransformersCLI/main.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if canImport(CoreML)
import ArgumentParser
import CoreML
import Foundation
Expand Down Expand Up @@ -107,3 +108,4 @@ extension Double {
String(format: "\(format)", self)
}
}
#endif // canImport(CoreML)
5 changes: 4 additions & 1 deletion Tests/GenerationTests/LogitsWarperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
// Created by Jan Krukowski on 09/12/2023.
//

#if canImport(CoreML)
import CoreML
@testable import Generation
import XCTest

@testable import Generation

final class LogitsWarperTests: XCTestCase {
private let accuracy: Float = 0.00001

Expand Down Expand Up @@ -150,3 +152,4 @@ final class LogitsWarperTests: XCTestCase {
XCTAssertEqual(result5.logits, [4.5, 3.0, 2.0, 1.0], accuracy: accuracy)
}
}
#endif // canImport(CoreML)
5 changes: 4 additions & 1 deletion Tests/GenerationTests/MathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
// Created by Jan Krukowski on 25/11/2023.
//

#if canImport(CoreML)
import CoreML
@testable import Generation
import XCTest

@testable import Generation

final class MathTests: XCTestCase {
private let accuracy: Float = 0.00001

Expand Down Expand Up @@ -51,3 +53,4 @@ final class MathTests: XCTestCase {
XCTAssertEqual(result1.reduce(0, +), 1.0, accuracy: accuracy)
}
}
#endif // canImport(CoreML)