Skip to content

consistent treatment of HasDType (Float.self) vs dtype (.float32)#222

Merged
davidkoski merged 3 commits intomainfrom
dtypes
Apr 18, 2025
Merged

consistent treatment of HasDType (Float.self) vs dtype (.float32)#222
davidkoski merged 3 commits intomainfrom
dtypes

Conversation

@davidkoski
Copy link
Collaborator

  • fixes Missing functionality around types/dtypes #199
  • there were inconsistent methods for creating MLXArrays with various patterns using Float.self vs .float32
  • add an FInfo struct also in support of dtype
  • encountered when porting VLMs

@davidkoski davidkoski requested review from awni and barronalex April 10, 2025 22:20
/// The difference between 1.0 and the next smallest representable float larger than 1.0
///
/// In Swift this is e.g. ``Double.ulpOfOne``
public var eps: Double {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per suggestion from Awni this exposes everything as Double. we ran into some cases in porting Qwen2.5VL where it would have been convenient to map from DType to these values.

I wonder if we actually want to return an MLXArray here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably better to return it as a Double. Makes it easier to use these in functions which only take scalars as input (like linspace for example). I guess the inconvenience is that you have to specify the type when using them in creation operations like full, but that seems kind of minor.

var result = mlx_array_new()
mlx_zeros(&result, shape.map { Int32($0) }, shape.count, T.dtype.cmlxDtype, stream.ctx)
return MLXArray(result)
MLX.zeros(shape, type: type, stream: stream)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is MLXArray.zeros() -- a natural way to write things in swift. We also have the free function zeros() which does the same thing but is a little less idiomatic swift. Anyway we want:

  • only one implementation -- just have one call the other
  • they should take type (Float.self) or dtype (.float32) consistently -- this was not the case

return MLXArray(result)
}

/// Create a square identity matrix and a given ``DType``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure what you are trying to say here with the Dtype, but it's confusing. If it's about the type of the output I would change to:

/// Create a square identity matrix with a given ``DType``.

Or if it's about the argument I would say:

/// Create a square identity matrix given a ``DType``.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the former. Thanks for the thorough read of these doc comments!

Copy link
Member

@awni awni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few cosmetic comments. Otherwise good to go, thanks for the update!

davidkoski and others added 3 commits April 18, 2025 08:19
- fixes #199
- there were inconsistent methods for creating MLXArrays with various patterns using Float.self vs .float32
- add an FInfo struct also in support of dtype
- encountered when porting VLMs
Co-authored-by: Awni Hannun <awni@apple.com>
@davidkoski davidkoski merged commit 07fdf84 into main Apr 18, 2025
1 check passed
@davidkoski davidkoski deleted the dtypes branch April 18, 2025 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing functionality around types/dtypes

2 participants