This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix fp32 flatten issue #15351
Merged
Merged
fix fp32 flatten issue #15351
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3fcabc
Fix flatten issue before slice op
wuxun-zhang 436ffa4
fix cpplint
wuxun-zhang 2c3472f
address comments
wuxun-zhang f659a72
retrigger CI
wuxun-zhang 233a941
trigger CI
wuxun-zhang 8ae01d4
retrigger CI
wuxun-zhang d0dd24e
use SupportMKLDNNReshape and update operator list
wuxun-zhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file mkldnn_flatten.cc | ||
* \brief Implement flatten operator by using mkldnn reorder primitive | ||
* \author Wuxun Zhang | ||
*/ | ||
|
||
#if MXNET_USE_MKLDNN == 1 | ||
|
||
#include "mkldnn_reshape-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
class MKLDNNFlattenFwd : public MKLDNNReshapeFwd { | ||
public: | ||
explicit MKLDNNFlattenFwd(const OpReqType &req, | ||
const NDArray &input, | ||
const NDArray &output) | ||
: MKLDNNReshapeFwd(req, input, output) {} | ||
}; | ||
|
||
static MKLDNNFlattenFwd &GetFlattenForward(const OpReqType &req, | ||
const NDArray &input, | ||
const NDArray &output) { | ||
#if DMLC_CXX11_THREAD_LOCAL | ||
static thread_local std::unordered_map<OpSignature, | ||
MKLDNNFlattenFwd, OpHash> fwds; | ||
#else | ||
static MX_THREAD_LOCAL std::unordered_map<OpSignature, | ||
MKLDNNFlattenFwd, OpHash> fwds; | ||
#endif | ||
OpSignature key; | ||
key.AddSign(req); | ||
key.AddSign(input); | ||
|
||
auto it = fwds.find(key); | ||
if (it == fwds.end()) { | ||
MKLDNNFlattenFwd fwd(req, input, output); | ||
it = AddToCache(&fwds, key, fwd); | ||
} | ||
return it->second; | ||
} | ||
|
||
void MKLDNNFlattenForward(const nnvm::NodeAttrs &attrs, | ||
const OpContext &ctx, | ||
const NDArray &input, | ||
const OpReqType &req, | ||
const NDArray &output) { | ||
if (req == kNullOp) return; | ||
CHECK_NE(req, kAddTo) << "kAddTo is not supported yet"; | ||
|
||
auto fwd = GetFlattenForward(req, input, output); | ||
auto ws_size = fwd.GetWorkspaceSize(); | ||
void* ws_ptr = nullptr; | ||
if (ws_size) { | ||
mshadow::Stream<cpu> *s = ctx.get_stream<cpu>(); | ||
mshadow::Tensor<cpu, 1, char> ws = ctx.requested[0] | ||
.get_space_typed<cpu, 1, char>(mshadow::Shape1(ws_size), s); | ||
ws_ptr = reinterpret_cast<void*>(ws.dptr_); | ||
} | ||
|
||
fwd.Execute(input, output, ws_ptr); | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,12 +119,17 @@ void MKLDNNTransposeForward(const nnvm::NodeAttrs& attrs, | |
const OpReqType &req, | ||
const NDArray &output); | ||
|
||
void MKLDNNReshapeForward(const nnvm::NodeAttrs &attrs, | ||
void MKLDNNReshapeForward(const nnvm::NodeAttrs& attrs, | ||
const OpContext &ctx, | ||
const NDArray &data, | ||
const NDArray &input, | ||
const OpReqType &req, | ||
const NDArray &output); | ||
|
||
void MKLDNNFlattenForward(const nnvm::NodeAttrs &attrs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to keep both flatten and reshape function declaration here. |
||
const OpContext &ctx, | ||
const NDArray &input, | ||
const OpReqType &req, | ||
const NDArray &output); | ||
} // namespace op | ||
} // namespace mxnet | ||
#endif // MXNET_USE_MKLDNN == 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2019 by Contributors | ||
* \file mkldnn_reshape-inl.h | ||
* \brief Function definition of mkldnn reshape operator | ||
*/ | ||
|
||
#ifndef MXNET_OPERATOR_NN_MKLDNN_MKLDNN_RESHAPE_INL_H_ | ||
#define MXNET_OPERATOR_NN_MKLDNN_MKLDNN_RESHAPE_INL_H_ | ||
|
||
#if MXNET_USE_MKLDNN == 1 | ||
#include <vector> | ||
#include "mkldnn_base-inl.h" | ||
#include "../../tensor/matrix_op-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
inline bool SupportMKLDNNReshape(const ReshapeParam ¶m, | ||
const NDArray &data) { | ||
auto data_ndim = data.shape().ndim(); | ||
|
||
if (data_ndim > 4 || | ||
data.dtype() != mshadow::kFloat32 || | ||
param.shape.ndim() > 4) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
class MKLDNNReshapeFwd { | ||
protected: | ||
std::shared_ptr<mkldnn::memory> data_; | ||
std::shared_ptr<mkldnn::memory> out_; | ||
std::shared_ptr<mkldnn::memory> temp_; | ||
std::vector<mkldnn::primitive> prims_; | ||
bool needInvalidateInput = false; | ||
|
||
public: | ||
MKLDNNReshapeFwd(const OpReqType &req, | ||
const NDArray &input, | ||
const NDArray &output); | ||
int GetWorkspaceSize(); | ||
void SetNewMem(const NDArray &input, | ||
const NDArray &output, | ||
void* workspace = nullptr); | ||
void Execute(const NDArray &input, | ||
const NDArray &output, | ||
void* workspace = nullptr); | ||
}; | ||
|
||
typedef ParamOpSign<ReshapeParam> MKLDNNReshapeSignature; | ||
MKLDNNReshapeFwd &GetReshapeForward(const ReshapeParam& param, | ||
const OpReqType &req, | ||
const NDArray &input, | ||
const NDArray &output); | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_MKLDNN == 1 | ||
#endif // MXNET_OPERATOR_NN_MKLDNN_MKLDNN_RESHAPE_INL_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it possible to combine
GetFlattenForward
andGetRehshapeForward
into one, and call them via passing different template parameter? So that we can still reuse most of the function when implementing other ops likeexpand_dims
?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.
Seems cannot combine these two functions into one. Because
reshape
op have a parameterReshapeParam
whileflatten
op don't, so when we try to createkey
, forreshape
we useMKLDNNReshapeSignature key(ReshapeParam)
, but forflatten
we useOpSignature key
. So, this function should be designed differently.Also,
expand_dims
op also have a parameter, and can reuse this function withreshape
op.