Skip to content

Add section on copy-view behaviour and mutability #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2020
Merged

Conversation

rgommers
Copy link
Member

Closes gh-24

@amueller
Copy link

two comments from sklearn:
We use "out" a lot for inplace operations, for example cliping :
https://github.com/scikit-learn/scikit-learn/blob/8471c8389d794309b0b62a353e3af903acd48223/sklearn/ensemble/_weight_boosting.py#L278

or np.dot:
https://github.com/scikit-learn/scikit-learn/blob/8471c8389d794309b0b62a353e3af903acd48223/sklearn/linear_model/_base.py#L632

or np.log:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/neural_network/_multilayer_perceptron.py#L1099

Basically we have a lot of iterative algorithms and we can often reuse arrays from previous algorithms, and sometimes we have temporaries that we can overwrite when applying unary transformations.

Actually, looking into the repo, there's less places than I expected. We could probably use it in some other places as well. I wouldn't say it's a good pattern, but I don't know a way to replace it.

The other thing that was mentioned by @rgommers was that it might be possible to have pools of memory to reuse.
We considered doing that manually in scikit-learn/scikit-learn#18242
though then the gain in the end wasn't as big. I'm pretty sure there are other places where reusing temporaries could be beneficial an where we don't do it.

Copy link
Member

@apaszke apaszke left a comment

Choose a reason for hiding this comment

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

It might be worth adding that there are libraries that do some form of tracing or abstract interpretation over a language that doesn't support mutation (to make analysis easier) and that in those cases implementing out= with correct handling of views might even be impossible to do.

@rgommers
Copy link
Member Author

rgommers commented Nov 3, 2020

It might be worth adding that there are libraries that do some form of tracing or abstract interpretation over a language that doesn't support mutation (to make analysis easier) and that in those cases implementing out= with correct handling of views might even be impossible to do.

Thanks @apaszke, I've added that sentence. I assume these libraries include both JAX and TensorFlow today?

@rgommers
Copy link
Member Author

rgommers commented Nov 3, 2020

Actually, looking into the repo, there's less places than I expected.

Glad to hear that:)

I wouldn't say it's a good pattern, but I don't know a way to replace it.

I think the "reusable buffers" feature could be used for the same thing in many cases, see example in data-apis/consortium-feedback#5. Where that's not the case is probably if you supply say a single row or column of an n-D array as the argument to out=. All your scikit-learn examples are supplying a complete array to out=, which should be expressable with "may use this buffer" rather than "must place result in this buffer".

@rgommers rgommers merged commit 39e0d62 into master Nov 3, 2020
@rgommers rgommers deleted the mutability branch November 3, 2020 17:56
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.

Copy-view behaviour and mutating arrays
3 participants