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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class CoreAIDenoiser: Sendable {
let batchSize = latents.shape[0]

var timestepArray = NDArray(shape: [batchSize], scalarType: .float32)
var timestepView = timestepArray.mutableView(as: Float.self)
let timestepView = timestepArray.mutableView(as: Float.self)
timestepView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<batchSize { ptr[i] = timestep }
}
Expand All @@ -58,7 +58,7 @@ public final class CoreAIDenoiser: Sendable {

let shape = latents.shape
var result = NDArray(shape: shape, scalarType: .float32)
var resultView = result.mutableView(as: Float.self)
let resultView = result.mutableView(as: Float.self)
resultView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<floats.count { ptr[i] = floats[i] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public actor CoreAIDiffusionModelFunction {
switch resolved.scalarType {
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
case .float16:
var view = array.mutableView(as: Float16.self)
let view = array.mutableView(as: Float16.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for j in 0..<data.count { ptr[j] = Float16(data[j]) }
}
#endif
case .float32:
var view = array.mutableView(as: Float.self)
let view = array.mutableView(as: Float.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for j in 0..<data.count { ptr[j] = data[j] }
}
Expand All @@ -83,7 +83,7 @@ public actor CoreAIDiffusionModelFunction {
guard case .ndArray(let nd) = fn.descriptor.inputDescriptor(of: name) else { continue }
let resolved = nd.resolvingDynamicDimensions(shape)
var array = NDArray(descriptor: resolved)
var view = array.mutableView(as: Int32.self)
let view = array.mutableView(as: Int32.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for j in 0..<data.count { ptr[j] = data[j] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class CoreAILatentDecoder: Sendable {
let outW = shape[3] * 8
let outShape = [1, 3, outH, outW]
var result = NDArray(shape: outShape, scalarType: .float32)
var resultView = result.mutableView(as: Float.self)
let resultView = result.mutableView(as: Float.self)
resultView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<outputFloats.count { ptr[i] = outputFloats[i] }
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public final class CoreAILatentEncoder: Sendable {
// Scale and return as NDArray
let outShape = [1, 4, height / 8, width / 8]
var result = NDArray(shape: outShape, scalarType: .float32)
var resultView = result.mutableView(as: Float.self)
let resultView = result.mutableView(as: Float.self)
resultView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<outputFloats.count { ptr[i] = outputFloats[i] * scaleFactor }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public struct Flux2Pipeline: DiffusionPipeline {
let image = try DiffusionUtilities.pixelsToCGImage(pixels, height: outputHeight, width: outputWidth)

var latentsND = NDArray(shape: latentShape, scalarType: .float32)
var latentsView = latentsND.mutableView(as: Float.self)
let latentsView = latentsND.mutableView(as: Float.self)
latentsView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<noise.count { ptr[i] = noise[i] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public struct SD3Pipeline: DiffusionPipeline {
pixels, height: imageSize, width: imageSize)

var latentsND = NDArray(shape: latentShape, scalarType: .float32)
var latentsView = latentsND.mutableView(as: Float.self)
let latentsView = latentsND.mutableView(as: Float.self)
latentsView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<latents.count { ptr[i] = latents[i] }
}
Expand Down Expand Up @@ -226,7 +226,7 @@ public struct SD3Pipeline: DiffusionPipeline {
}

var inputArray = NDArray(shape: [1, Self.clipSeqLen], scalarType: .int32)
var view = inputArray.mutableView(as: Int32.self)
let view = inputArray.mutableView(as: Int32.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<ids.count { ptr[i] = ids[i] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public struct StableDiffusionPipeline: DiffusionPipeline {

// Wrap latents back to NDArray for GenerationResult
var latentsND = NDArray(shape: latentShape, scalarType: .float32)
var latentsView = latentsND.mutableView(as: Float.self)
let latentsView = latentsND.mutableView(as: Float.self)
latentsView.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<latents.count { ptr[i] = latents[i] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public final class CoreAISequentialEngine: InferenceEngine, @unchecked Sendable
let dstBlockStride = dstShape[seqDim...].reduce(1, *) // S_new * D

source.view(as: LogitsScalarType.self).withUnsafePointer { srcPtr, _, _ in
var dstView = destination.mutableView(as: LogitsScalarType.self)
let dstView = destination.mutableView(as: LogitsScalarType.self)
dstView.withUnsafeMutablePointer { dstPtr, _, _ in
for block in 0..<numBlocks {
let srcOff = block * srcBlockStride
Expand All @@ -500,7 +500,7 @@ public final class CoreAISequentialEngine: InferenceEngine, @unchecked Sendable

private func zeroFill(_ array: inout NDArray) {
let count = array.shape.reduce(1, *)
var view = array.mutableView(as: LogitsScalarType.self)
let view = array.mutableView(as: LogitsScalarType.self)
// Inlined constant write — under -Onone, fillNDArray's
// `(Int) -> LogitsScalarType` closure is invoked per element (no inlining),
// which made zeroing the KV cache (~14.7M elements for a 32K-context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public final class CoreAISequentialVLMEngine: MultimodalInferenceEngine, @unchec
"scatterMerge only supports float16 embeddings; got \(imageEmbeddings.scalarType)")
}
imageEmbeddings.view(as: Float16.self).withUnsafePointer { imgPtr, _, _ in
var mutableView = merged.mutableView(as: Float16.self)
let mutableView = merged.mutableView(as: Float16.self)
mutableView.withUnsafeMutablePointer { mergedPtr, _, _ in
for (i, pos) in imagePositions.enumerated() {
let srcOffset = i * hiddenDim
Expand Down Expand Up @@ -916,7 +916,7 @@ public final class CoreAISequentialVLMEngine: MultimodalInferenceEngine, @unchec
let dstBlockStride = dstShape[seqDim...].reduce(1, *)

source.view(as: LogitsScalarType.self).withUnsafePointer { srcPtr, _, _ in
var dstView = destination.mutableView(as: LogitsScalarType.self)
let dstView = destination.mutableView(as: LogitsScalarType.self)
dstView.withUnsafeMutablePointer { dstPtr, _, _ in
for block in 0..<numBlocks {
let srcOff = block * srcBlockStride
Expand All @@ -932,7 +932,7 @@ public final class CoreAISequentialVLMEngine: MultimodalInferenceEngine, @unchec

private func zeroFill(_ array: inout NDArray) {
let count = array.shape.reduce(1, *)
var view = array.mutableView(as: LogitsScalarType.self)
let view = array.mutableView(as: LogitsScalarType.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<count {
ptr[i] = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public final class StaticShapeEngine: InferenceEngine, @unchecked Sendable {
}
if case .ndArray(let nd) = desc.inputDescriptor(of: posName) {
var pos = NDArray(descriptor: nd)
var posView = pos.mutableView(as: UInt16.self)
let posView = pos.mutableView(as: UInt16.self)
guard var posSpan = posView.contiguousElements else {
throw InferenceRuntimeError.invalidState("pos array has non-contiguous layout")
}
Expand All @@ -517,7 +517,7 @@ public final class StaticShapeEngine: InferenceEngine, @unchecked Sendable {
case .ndArray(let nd) = desc.inputDescriptor(of: stepName)
{
var step = NDArray(descriptor: nd)
var stepView = step.mutableView(as: Int32.self)
let stepView = step.mutableView(as: Int32.self)
guard var stepSpan = stepView.contiguousElements else {
throw InferenceRuntimeError.invalidState("step array has non-contiguous layout")
}
Expand All @@ -544,7 +544,7 @@ public final class StaticShapeEngine: InferenceEngine, @unchecked Sendable {
}

var tokenArray = NDArray(descriptor: tokenNDDesc)
var tokenView = tokenArray.mutableView(as: Int32.self)
let tokenView = tokenArray.mutableView(as: Int32.self)
guard var tokenSpan = tokenView.contiguousElements else {
throw InferenceRuntimeError.invalidState("tokenArray has non-contiguous layout")
}
Expand Down
4 changes: 2 additions & 2 deletions swift/Sources/CoreAIObjectDetector/ObjectDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public struct ObjectDetector {

if descriptor.scalarType == .float16 {
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
var view = imageArray.mutableView(as: Float16.self)
let view = imageArray.mutableView(as: Float16.self)
guard var span = view.contiguousElements else {
throw DetectionRuntimeError.invalidConfiguration(
"Image input NDArray is not contiguous")
Expand All @@ -223,7 +223,7 @@ public struct ObjectDetector {
fatalError("Float16 is not supported on this platform")
#endif
} else {
var view = imageArray.mutableView(as: Float.self)
let view = imageArray.mutableView(as: Float.self)
guard var span = view.contiguousElements else {
throw DetectionRuntimeError.invalidConfiguration(
"Image input NDArray is not contiguous")
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/CoreAIShared/Runtime/NDArray+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public func fillNDArray<T: BitwiseCopyable>(
public func fillNDArray<T: BitwiseCopyable>(
_ array: inout NDArray, as type: T.Type, count: Int, using generator: (Int) -> T
) {
var view = array.mutableView(as: type)
let view = array.mutableView(as: type)
view.withUnsafeMutablePointer { ptr, shape, _ in
let capacity = shape.product
precondition(count <= capacity, "fillNDArray: count \(count) exceeds array capacity \(capacity)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,15 +755,15 @@ struct DiffusionRunner: AsyncParsableCommand {
{
if asInt32 {
var array = NDArray(shape: shape, scalarType: .int32)
var view = array.mutableView(as: Int32.self)
let view = array.mutableView(as: Int32.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<floats.count { ptr[i] = Int32(floats[i]) }
}
return array
} else if scalarType == .float16 {
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
var array = NDArray(shape: shape, scalarType: .float16)
var view = array.mutableView(as: Float16.self)
let view = array.mutableView(as: Float16.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<floats.count { ptr[i] = Float16(floats[i]) }
}
Expand All @@ -773,7 +773,7 @@ struct DiffusionRunner: AsyncParsableCommand {
#endif
} else {
var array = NDArray(shape: shape, scalarType: scalarType ?? .float32)
var view = array.mutableView(as: Float.self)
let view = array.mutableView(as: Float.self)
view.withUnsafeMutablePointer { ptr, _, _ in
for i in 0..<floats.count { ptr[i] = floats[i] }
}
Expand Down