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

swift: Add generation of the documentation #175

Merged
merged 5 commits into from
Nov 4, 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
19 changes: 18 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,22 @@ task generate_java_doc: OUT_DOC_DIR do
sh 'doxygen wrapper/java/Doxyfile.java-api'
end

task generate_swift_doc: OUT_DOC_DIR do
docc = ''
docc = 'xcrun ' if RUBY_PLATFORM =~ /darwin/
docc += 'docc'

sh %W(
#{docc} convert
--emit-lmdb-index
--fallback-display-name AlpacaCoreSwift
--fallback-bundle-identifier AlpacaCoreSwift
--fallback-bundle-version 0
--output-dir #{OUT_DOC_DIR}/swift/AlpacaCoreSwift.doccarchive
--diagnostics-file #{OUT_DOC_DIR}/swift/AlpacaCoreSwift-diagnostics.json
--additional-symbol-graph-dir #{OUT_DOC_DIR}/swift/symbol-graph
).join(' ')
end

desc 'Generate all documentation'
task generate_doc: %i[generate_cpp_doc generate_c_doc generate_java_doc]
task generate_doc: %i[generate_cpp_doc generate_c_doc generate_java_doc generate_swift_doc]
2 changes: 1 addition & 1 deletion wrapper/swift/code/CxxAlpacaCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set_target_properties(AlpacaCoreSwift PROPERTIES COMPILE_OPTIONS "")

target_compile_options(AlpacaCoreSwift
PUBLIC
$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default -Xcc -std=c++20>
$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default -Xcc -std=c++20 -emit-symbol-graph -emit-symbol-graph-dir "${CMAKE_BINARY_DIR}/../../doc/swift/symbol-graph">
)

target_include_directories(AlpacaCoreSwift PUBLIC .)
Expand Down
10 changes: 10 additions & 0 deletions wrapper/swift/code/CxxAlpacaCore/DictHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public enum DictConvertError: Error, Equatable {
case invalidType(String)
}

/// Convert a Swift dictionary to an `AC.DictRoot` format, suitable for the native C++ code.
///
/// - Parameter dictionary: The Swift dictionary to convert.
/// - Throws: `DictConvertError.invalidType` if an unsupported type is encountered in the dictionary.
/// - Returns: An `AC.DictRoot` object representing the converted dictionary.
public func translateDictionaryToDict(_ dictionary: Dictionary<String, Any>) throws -> AC.DictRoot {
// Create the root dictionary object
// var dictRoot = AC.DictRoot.create()
Expand Down Expand Up @@ -53,6 +58,11 @@ public func translateDictionaryToDict(_ dictionary: Dictionary<String, Any>) thr
return dictRoot
}

/// Convert an `AC.DictRef` dictionary format back into a Swift dictionary.
///
/// - Parameter dict: The `AC.DictRef` dictionary to convert.
/// - Throws: `DictConvertError.invalidType` if an unsupported type is encountered in the dictionary.
/// - Returns: A Swift dictionary representing the converted data.
public func translateDictToDictionary(_ dict: AC.DictRef) throws -> Dictionary<String, Any> {
var dictionary: Dictionary<String, Any> = Dictionary<String, Any>()

Expand Down
13 changes: 11 additions & 2 deletions wrapper/swift/code/CxxAlpacaCore/ModelDesc.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
/// Represents the information of an asset.
public class AssetInfo {
/// The path to the asset.
public var path: String = ""

/// The tag of the asset. Some native model loaders may require it.
public var tag: String = ""

public init() {
}
public init() {}

public init(_ path:String, _ tag:String) {
self.path = path
self.tag = tag
}
}

/// The description of the model used by the native `ModelLoader`.
public class ModelDesc {
/// The type of the inference used to select the appropriate model loader.
public var inferenceType: String = ""

/// Name tag. Not used by the library in any way besides logs and may be helpful for debugging
public var name: String = ""

/// The list of assets required by the model.
public var assets: [AssetInfo]

public init() {
Expand Down
42 changes: 40 additions & 2 deletions wrapper/swift/code/CxxAlpacaCore/SwiftApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ACError: Error, Equatable {
case invalidRunOp(String)
}

/// Initialize the AlpacaCore native SDK
public func initSDK() {
AC.initSDK()
}
Expand Down Expand Up @@ -43,6 +44,19 @@ func callObserver(observer: UnsafeMutableRawPointer,
}
}

/// Create a model with the specified description and parameters, with optional progress tracking.
///
/// - Parameters:
/// - desc: A model description.
/// - params: A dictionary of parameters.
/// - progress: An optional closure for progress updates during model creation.
/// The closure parameters are:
/// - `String`: A description of the current progress step.
/// - `Float`: A value from 0.0 to 1.0 indicating the completion percentage.
///
/// - Throws: `ACError.invalidModelCreation` if the model creation process encounters an error.
///
/// - Returns: A `Model` instance representing the newly created model.
public func createModel(_ desc: inout ModelDesc, _ params: Dictionary<String, Any>,
_ progress: Optional<(String, Float) -> Void> = nil) throws -> Model {
let paramsAsDict = try translateDictionaryToDict(params)
Expand All @@ -60,30 +74,54 @@ public func createModel(_ desc: inout ModelDesc, _ params: Dictionary<String, An
return Model(result.consumeValue())
}

/// A wrapper class for the native `AC.Model` object, providing functionality to create new `Instance`.
public class Model {
var model: AC.Model

init(_ model: AC.Model) {
self.model = model
}

public func createInstance(_ name: String, _ params: Dictionary<String, Any>) throws -> Instance {
/// Create an `Instance` from the specified name and parameters.
///
/// - Parameters:
/// - type: The type of the instance to create.
/// - params: A dictionary of parameters to pass for the instance creation.
///
/// - Throws: `ACError.invalidInstanceCreation` if the instance creation fails.
///
/// - Returns: An `Instance` representing the newly created instance.
public func createInstance(_ type: String, _ params: Dictionary<String, Any>) throws -> Instance {
let paramsAsDict = try translateDictionaryToDict(params)
var result = model.createInstance(std.string(name), paramsAsDict.getRef())
var result = model.createInstance(std.string(type), paramsAsDict.getRef())
if result.hasError() {
throw ACError.invalidInstanceCreation(String(result.error()))
}
return Instance(result.consumeValue())
}
}

/// A wrapper class for native `AC.Instance` object, providing functionality to run operations with progress tracking.
public class Instance {
var instance: AC.Instance

init(_ instance: AC.Instance) {
self.instance = instance
}

/// Run an operation on the `instance` with the provided operation name, parameters, and optional progress callback.
///
/// - Parameters:
/// - op: The name of the operation to execute.
/// - params: A dictionary of parameters to pass to the operation.
/// - progress: An optional closure that is called with updates on the operation's progress.
/// The closure parameters are:
/// - `String`: A description of the current progress step.
/// - `Float`: A value from 0.0 to 1.0 indicating the completion percentage.
///
/// - Throws: An `ACError.invalidRunOp` error if the operation fails.
///
/// - Returns: A dictionary containing the results of the operation.
public func runOp(_ op: String, _ params: Dictionary<String, Any>,
_ progress: Optional<(String, Float) -> Void> = nil) throws -> Dictionary<String, Any> {
let paramsAsDict = try translateDictionaryToDict(params)
Expand Down
Loading