-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Conversation
58e9144
to
0f36d95
Compare
Have you tried this with mkldnn enabled? E.g reshape the output of mkldnn convolution? |
@ZhennanQin I didn't test that specifically based on the assumption that the backend implementation should not cause existing APIs such as |
@szha -- Thanks for putting in this patch! I have a couple questions about the operator (1) I originally discovered this because I was using the C API to call MXImperativeInvoke() using the expand_dims operator, and I noticed it was causing a copy. This fix only effects the Python version when operating on ndarrays, not users of the expand_dims operator. I see that there is a path in the operator that checks if it is an in-place operation. Presumably it uses that path if I pass an output array that is the same NDArrayHandle as the input array? But what if I still need the original input array handle, and I want to create a new output array handle with the expanded dim but still not make a copy? (2) In the issue I created you commented that: "For symbol (and thus the hybridized version), since in-place identity is possible it should not matter". Can you talk a little more about that? I assume you mean that in this case:
The engine can figure out that x is not needed again, and can thus turn the expand_dims(1) into an in-place operation that doesn't make a copy? I'm not very familiar with how this part of the code works, so what happens if you had code that looked like this?
i.e. the code still makes a reference to the original x, and thus presumably the engine can't decide to use the in-place version of expand_dims in that case, right? So I guess my question is -- Does the ability for the Syblolic / hybridized engine to elide the copy depend on the code not referencing the un-expanded version of the array after calling expand_dims()? If so, it seems like there will still be some use cases where an unexpected copy is happening. |
Right. The reshape operators always return a copy. This is because in imperative mode there is no complete computation graph to be analyzed and see whether a copy is needed or not.
Right, it is done through
As long as it's not used in the context of autograd, you can save the original handle before in-place operation. Though the output of in-place op shares the space with the input, it's still a separate handle.
To elaborate, in symbolic mode, the backend can see the complete computation graph, and as a result it can analyze the graph (through node coloring) and figure out whether nodes can share the same space. Note that such memory plan happens before the engine/scheduler work happens, and it happens for both symbolic executor and hybridized Gluon HybridBlock. |
@szha -- Thanks for the detailed explanation, that helps a lot. So just for clarity, I see that in the operator definition for expand_dims, it sets the FInPlaceIdentity attribute to true: I didn't see anything in the plan_memory.cc file that depended on autograd / training. So should I take it that the code is smart enough to not allocate new memory for the expand_dims operator in symbolic mode, i.e. not to make a copy, even during training when using autograd? If so, then I think I understand it, and I see what you mean about only needing to fix this for the Python ndarray use case. Thanks again! |
@mxnet-label-bot add [pr-awaiting-review, NDArray] |
@stephenrawls actually, autograd comes into play in pure imperative mode. MXNet's autograd is trace based and does not support in-place operation such as |
Does it mean that with these changes python API will have different behavior compared with other frontend languages? |
@TaoLv Other language bindings don't seem to have the fluent methods for reshaping NDArray. |
The current implementation would break backward compatibility for in-place update. |
I plan on adding an |
@reminisce Could you please help review the PR? |
I haven't finished this PR yet as I haven't got around to add the in-place option. |
@mxnet-label-bot add [pr-work-in-progress] |
@mxnet-label-bot remove [pr-awaiting-review] |
b2963d0
to
81536a5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add unit tests.
@reminisce good catch. I was relying on existing tests before adding the inplace option but forgot to add tests for them afterwards. |
Added tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM : )
I have a question.
It seems that in-place flag does not support Symbol.
In gluon, F.expand_dim(a, inplace=True) will raise inplace not found when hybridizing the model.
Could we add warnings in mxnet/symbol.py ?
@wkcn good point on symbol and HybridBlock. I'm not sure if a warning should be added as it can get very verbose. |
Nonetheless it should not throw an error when using inplace with hybridize. Let me see how to best deal with it. |
@szha Thank you for your contributions! Is this PR work in progress? |
@szha is this PR ready to merge? |
@szha Is this good to merge ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for all the reviews. I plan to add a dummy option in the symbol version of the reshape ops so that it won't throw error when people use this option in the context of Gluon |
@mxnet-label-bot add[pr-work-in-progress] |
@mxnet-label-bot remove [pr-awaiting-response] |
@szha Did you get a chance to add a dummy option in the symbol version? Thanks! |
eb1da49
to
d58bd77
Compare
@szha @wkcn This PR is ready to be merged I guess ? @mxnet-label-bot Update [NDArray, pr-awaiting-merge] |
@piyushghai thanks for pinging. I wanted to hold onto this for the 1.5 release code freeze. |
@szha is this good to go now? |
* in-place reshape ops * add inplace option * add dummy arguments to symbol
Description
make NDArray fluent methods for expand_dims/flatten/squeeze in-place reshapes.
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.
Changes
Comments