Skip to content

In-place operators - #91

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
kemchenj:in_place_operator
May 24, 2024
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
kemchenj:in_place_operator

Conversation

@kemchenj

@kemchenj kemchenj commented May 21, 2024 •

Copy link
Copy Markdown
Contributor

Fix #86

@kemchenj kemchenj changed the title In-place operator In-place operators May 21, 2024
@kemchenj
kemchenj force-pushed the in_place_operator branch from 7df4b93 to 00ed613 Compare May 21, 2024 15:54
Comment thread Source/MLX/MLXArray+Ops.swift Outdated
let s = StreamOrDevice.default
let result = mlx_add(lhs.ctx, rhs.ctx, s.ctx)!
mlx_free(lhs.ctx)
lhs.ctx = result

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use this function:

    /// Replace the contents with a reference to a new array.
    public func update(_ array: MLXArray) {
        if array.ctx != self.ctx {
            mlx_retain(array.ctx)
            mlx_free(ctx)
            self.ctx = array.ctx
        }
    }

Possibly make a package scoped variant that takes an mlx_array instead of MLXArray. These self-mutates are rare and it is good to have them go through a common method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, I have made the changes and pushed.

@kemchenj
kemchenj force-pushed the in_place_operator branch from 00ed613 to 24d3f5f Compare May 22, 2024 13:30
@kemchenj
kemchenj force-pushed the in_place_operator branch from 24d3f5f to 96f5b7d Compare May 22, 2024 13:39
@kemchenj
kemchenj marked this pull request as ready for review May 22, 2024 13:41
@kemchenj

Copy link
Copy Markdown
Contributor Author

I noticed there is a bug around swift-docc that could not generate symbol links for -/-=////=. This should be fixed in Swift 5.10 based on swiftlang/swift-docc#808.

For this PR, I want to keep it as simple and focused as possible, so I will continue to document in the previous manner. And I will make a follow-up PR to address this problem.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thanks for this! I think it is a good addition.

@davidkoski
davidkoski merged commit e2bfe0d into ml-explore:main May 24, 2024
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.

In-Place operators (+=, -=) for MLXArray

2 participants