Conversation
It wasn't added because there is an idiomatic swift way of doing it (since let a = MLXArray(0 ..< 12)There are range variants for open, closed and strides -- all of the Do you think |
|
The main motivation would be discoverability, especially since models are now commonly ported with automated tools. This came to mind as I was cleaning up the mlx-audio repo, which contained some workarounds for Perhaps there's also a performance benefit of using the MLX C API directly, which uses lazy computation? The syntax with // Current workarounds
let a = MLXArray(stride(from: 0, to: 12, by: 2))
let b = MLXArray(stride(from: 0.0, to: 5.0, by: 0.5))let a = MLXArray.arange(0, 12, step: 2)
let b = MLXArray.arange(0.0, 5.0, step: 0.5) |
|
That seems reasonable -- we can cross link the documentation between the two styles. Using |
|
Agreed; we did a bit of workaround for mlx-audio swift port |
53b75c6 to
cfa6deb
Compare
cfa6deb to
504a685
Compare
| /// - ``arange(_:_:step:stream:)`` | ||
| static public func arange(_ stop: Int, stream: StreamOrDevice = .default) -> MLXArray { | ||
| MLX.arange(0, stop, stream: stream) | ||
| } |
There was a problem hiding this comment.
I wonder if all of these could be done with one function?
func arange<T: HasDType>(_ start: T? = nil, _ stop: T, step: T? = nil, dtype:DType? = nil, stream: StreamOrDevice = .default) -> MLXArray under the hood the values are doubles (in mlx_arange). It looks like it would require something on either DType or HasDType to produce a Double.
I think this might be cleaner if it works -- the unlabeled optional start might cause trouble. The python side has two for this reason (I think).
See what you think -- I think this looks good overall!
There was a problem hiding this comment.
I went through several iterations of this and landed on this design by trying to follow the patterns of existing methods in this repo as well as the Python API. Feel free to take it in any direction you want if you prefer a different approach.
There was a problem hiding this comment.
OK, sounds good -- I can play around with variants after it merges to see if any make sense.
|
@DePasqualeOrg looks like a swift-format failure. |
|
Sorry about that. I ran the formatting again. |
davidkoski
left a comment
There was a problem hiding this comment.
Looks good, thank you!
Proposed changes
Do you think it would make sense to add
arangeto mlx-swift? This is probably the most common piece of missing syntax that I've encountered when porting models from Python to Swift. There are enough existing examples that in practice it's easy enough to find a workaround, but having a direct equivalent would reduce friction. Is there a reason this was not included in mix-swift?Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes