Skip to content

Commit

Permalink
Fix tests on macOS 13/14
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Dec 16, 2024
1 parent 9572995 commit 2c44624
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
61 changes: 39 additions & 22 deletions lib/mps/ndarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export MPSNDArrayDescriptor
@objcproperties MPSNDArrayDescriptor begin
@autoproperty dataType::MPSDataType setter=setDataType
@autoproperty numberOfDimensions::NSUInteger setter=setNumberOfDimensions
@autoproperty preferPackedRows::Bool setter=setPreferPackedRows # macOS 15+

# Both are officially available starting macOS 15, but they work in macOS 13/14
@autoproperty preferPackedRows::Bool setter=setPreferPackedRows # macOS 15+
@autoproperty getShape::id{NSArray} # macOS 15+
end

Expand Down Expand Up @@ -49,18 +50,29 @@ export MPSNDArray

@objcwrapper immutable=false MPSNDArray <: NSObject

@objcproperties MPSNDArray begin
@autoproperty dataType::MPSDataType
@autoproperty dataTypeSize::Csize_t
@autoproperty device::id{MTLDevice}
@autoproperty label::id{NSString} setter=setLabel
@autoproperty numberOfDimensions::NSUInteger
@autoproperty parent::id{MPSNDArray}

#Instance methods that act like properties
@autoproperty descriptor::id{MPSNDArrayDescriptor}
@autoproperty resourceSize::NSUInteger
@autoproperty userBuffer::id{MTLBuffer}
@static if Metal.macos_version() >= v"15"
@objcproperties MPSNDArray begin
@autoproperty dataType::MPSDataType
@autoproperty dataTypeSize::Csize_t
@autoproperty device::id{MTLDevice}
@autoproperty label::id{NSString} setter=setLabel
@autoproperty numberOfDimensions::NSUInteger
@autoproperty parent::id{MPSNDArray}

#Instance methods that act like properties
@autoproperty descriptor::id{MPSNDArrayDescriptor}
@autoproperty resourceSize::NSUInteger
@autoproperty userBuffer::id{MTLBuffer}
end
else
@objcproperties MPSNDArray begin
@autoproperty dataType::MPSDataType
@autoproperty dataTypeSize::Csize_t
@autoproperty device::id{MTLDevice}
@autoproperty label::id{NSString} setter=setLabel
@autoproperty numberOfDimensions::NSUInteger
@autoproperty parent::id{MPSNDArray}
end
end

@objcwrapper immutable=false MPSTemporaryNDArray <: MPSNDArray
Expand Down Expand Up @@ -100,15 +112,20 @@ function MPSNDArray(device::MTLDevice, scalar)
return obj
end

# macOS 15+
function MPSNDArray(buffer::MTLBuffer, offset::UInt, descriptor::MPSNDArrayDescriptor)
arrayaddr = @objc [MPSNDArray alloc]::id{MPSNDArray}
obj = MPSNDArray(arrayaddr)
finalizer(release, obj)
@objc [obj::MPSNDArray initWithBuffer:buffer::id{MTLBuffer}
offset:offset::NSUInteger
descriptor:descriptor::id{MPSNDArrayDescriptor}]::id{MPSNDArray}
return obj
@static if Metal.macos_version() >= v"15"
function MPSNDArray(buffer::MTLBuffer, offset::UInt, descriptor::MPSNDArrayDescriptor)
arrayaddr = @objc [MPSNDArray alloc]::id{MPSNDArray}
obj = MPSNDArray(arrayaddr)
finalizer(release, obj)
@objc [obj::MPSNDArray initWithBuffer:buffer::id{MTLBuffer}
offset:offset::NSUInteger
descriptor:descriptor::id{MPSNDArrayDescriptor}]::id{MPSNDArray}
return obj
end
else
function MPSNDArray(buffer::MTLBuffer, offset::UInt, descriptor::MPSNDArrayDescriptor)
@assert false "Creating an MPSNDArray that shares data with user-provided MTLBuffer is only supported in macOS v15+"
end
end

function MPSNDArray(arr::MtlArray{T,N}) where {T,N}
Expand Down
35 changes: 19 additions & 16 deletions test/mps/ndarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ using .MPS: MPSNDArrayDescriptor, MPSDataType, lengthOfDimension
@testset "MPSNDArrayDescriptor" begin
T = Float32
DT = convert(MPSDataType, T)
rows = 2
cols = 3
rowBytes = sizeof(T) * cols
mats = 4

desc1 = MPSNDArrayDescriptor(T, 5,4,3,2,1)
@test desc1 isa MPSNDArrayDescriptor
Expand Down Expand Up @@ -55,9 +51,6 @@ using .MPS: MPSNDArray
@test ndarr1.label == "Test1"
@test ndarr1.numberOfDimensions == 5
@test ndarr1.parent === nothing
@test ndarr1.descriptor isa MPSNDArrayDescriptor
@test ndarr1.resourceSize isa UInt
@test ndarr1.userBuffer === nothing

ndarr2 = MPSNDArray(dev, 4)
@test ndarr2 isa MPSNDArray
Expand All @@ -68,19 +61,29 @@ using .MPS: MPSNDArray
@test ndarr2.label == "Test2"
@test ndarr2.numberOfDimensions == 1
@test ndarr2.parent === nothing
@test ndarr2.descriptor isa MPSNDArrayDescriptor
@test ndarr2.resourceSize isa UInt
@test ndarr2.userBuffer === nothing

arr3 = MtlArray(ones(Float16, 2,3,8))
ndarr3 = MPSNDArray(arr3)
arr3 = MtlArray(ones(Float16, 2,3,4))
@test_throws "Final dimension of arr must have a byte size divisible by 16" MPSNDArray(arr3)

arr4 = MtlArray(arr3)
@test arr3 == arr4
arr4 = MtlArray(ones(Float16, 2,3,8))

arr5 = MtlArray(ones(Float16, 2,3,4))
@test_throws AssertionError MPSNDArray(arr5)
@static if Metal.macos_version() >= v"15"
@test ndarr1.descriptor isa MPSNDArrayDescriptor
@test ndarr1.resourceSize isa UInt
@test ndarr1.userBuffer === nothing

@test ndarr2.descriptor isa MPSNDArrayDescriptor
@test ndarr2.resourceSize isa UInt
@test ndarr2.userBuffer === nothing

ndarr4 = MPSNDArray(arr4)

arr4 = MtlArray(arr4)
@test arr4 == arr5

else
@test_throws "Creating an MPSNDArray that shares data with user-provided MTLBuffer is only supported in macOS v15+" MPSNDArray(arr4)
end
end


Expand Down

0 comments on commit 2c44624

Please sign in to comment.