This repository was 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.7k
quantized transpose operator #20817
Merged
Merged
quantized transpose operator #20817
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ad8e0fa
add quantized transpose
RafLit cd126d2
add quantized transpose test
RafLit af31280
add license to new files
RafLit 24bd96f
check support, fix warnings
RafLit 1f5a302
remove inplace quantized transpose
RafLit c2606a1
fix inplace quantized transpose
RafLit 9a7e30e
add quantized transpose test
RafLit ba1eb56
separate transpose ops
RafLit 2638995
review fixes
RafLit 5fb3c73
add fallbacks for NP and ND
RafLit 22279f8
fix formatting
RafLit dbfcc4b
fix formatting
RafLit 87ad087
update np transpose test
RafLit 2b3ad03
add operators to amp lists
RafLit edb940c
add macro operator creation
RafLit 1ec7ba9
add tests
RafLit cef3b62
review fix
RafLit 50a4c12
remove relative includes
RafLit 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
src/operator/quantization/dnnl/dnnl_quantized_transpose.cc
This file contains hidden or 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,102 @@ | ||
|
|
||
| /* | ||
| * 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 dnnl_quantized_transpose.cc | ||
| * \author: Rafal Litka, rafal.litka@intel.com | ||
| */ | ||
| #if MXNET_USE_ONEDNN == 1 | ||
| #include "operator/numpy/np_matrix_op-inl.h" | ||
| #include "operator/tensor/matrix_op-inl.h" | ||
| #include "operator/nn/dnnl/dnnl_transpose-inl.h" | ||
|
|
||
| namespace mxnet { | ||
| namespace op { | ||
|
|
||
| inline static bool QuantizedTransposeStorageType(const nnvm::NodeAttrs& attrs, | ||
| const int dev_mask, | ||
| DispatchMode* dispatch_mode, | ||
| std::vector<int>* in_attrs, | ||
| std::vector<int>* out_attrs) { | ||
| CHECK_EQ(in_attrs->size(), 3U); | ||
| CHECK_EQ(out_attrs->size(), 3U); | ||
| return DNNLStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs, out_attrs); | ||
| } | ||
|
|
||
| bool SupportDNNLQuantizedTranspose(const NDArray& data) { | ||
| auto data_ndim = data.shape().ndim(); | ||
|
|
||
| if (data_ndim > 4 || data_ndim == 0 || data.shape().Size() == 0) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
| typedef void (*TransposeFallbackFunAny)(const nnvm::NodeAttrs&, | ||
| const OpContext&, | ||
| const std::vector<TBlob>&, | ||
| const std::vector<OpReqType>&, | ||
| const std::vector<TBlob>&); | ||
|
|
||
| template <class ParamType, TransposeFallbackFunAny TransposeFallback> | ||
| static void DNNLQuantizedTransposeForward(const nnvm::NodeAttrs& attrs, | ||
| const OpContext& ctx, | ||
| const std::vector<NDArray>& inputs, | ||
| const std::vector<OpReqType>& req, | ||
| const std::vector<NDArray>& outputs) { | ||
| CHECK(inputs[0].dtype() == mshadow::kUint8 || inputs[0].dtype() == mshadow::kInt8) | ||
| << "dnnl_quantized_transpose only supports uint8 and int8 as input type"; | ||
| if (req[0] == kNullOp) { | ||
| return; | ||
| } | ||
| CHECK_EQ(inputs.size(), 3U); | ||
| CHECK_EQ(outputs.size(), 3U); | ||
| if (SupportDNNLQuantizedTranspose(inputs[0])) { | ||
| DNNLRun(DNNLTransposeForward<ParamType>, attrs, ctx, inputs[0], req[0], outputs[0]); | ||
| } else { | ||
| FallBackCompute(TransposeFallback, attrs, ctx, inputs, req, outputs); | ||
| } | ||
| outputs[1].data().dptr<float>()[0] = inputs[1].data().dptr<float>()[0]; | ||
| outputs[2].data().dptr<float>()[0] = inputs[2].data().dptr<float>()[0]; | ||
| } | ||
|
|
||
| NNVM_REGISTER_OP(_npx_quantized_transpose) | ||
| .set_attr<FInferStorageType>("FInferStorageType", QuantizedTransposeStorageType) | ||
| .set_attr<FResourceRequest>("FResourceRequest", | ||
| [](const NodeAttrs& n) { | ||
| return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; | ||
| }) | ||
| .set_attr<FComputeEx>("FComputeEx<cpu>", | ||
| DNNLQuantizedTransposeForward<NumpyTransposeParam, NumpyTranspose<cpu>>) | ||
| .set_attr<bool>("TIsDNNL", true); | ||
|
|
||
| NNVM_REGISTER_OP(_contrib_quantized_transpose) | ||
| .set_attr<FInferStorageType>("FInferStorageType", QuantizedTransposeStorageType) | ||
| .set_attr<FResourceRequest>("FResourceRequest", | ||
| [](const NodeAttrs& n) { | ||
| return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; | ||
| }) | ||
| .set_attr<FComputeEx>("FComputeEx<cpu>", | ||
| DNNLQuantizedTransposeForward<TransposeParam, Transpose<cpu>>) | ||
| .set_attr<bool>("TIsDNNL", true); | ||
|
|
||
| } // namespace op | ||
| } // namespace mxnet | ||
|
|
||
| #endif // MXNET_USE_ONEDNN == 1 |
This file contains hidden or 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,130 @@ | ||
| /* | ||
| * 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 quantized_transpose.cc | ||
| * \author: Rafal Litka, rafal.litka@intel.com | ||
| */ | ||
| #include <mxnet/op_attr_types.h> | ||
| #include "../tensor/matrix_op-inl.h" | ||
| #include "../numpy/np_matrix_op-inl.h" | ||
|
|
||
| namespace mxnet { | ||
| namespace op { | ||
|
|
||
| inline bool QuantizedTransposeType(const nnvm::NodeAttrs& attrs, | ||
| std::vector<int>* in_attrs, | ||
| std::vector<int>* out_attrs) { | ||
| CHECK_EQ(in_attrs->size(), 3U); | ||
| CHECK_EQ(out_attrs->size(), 3U); | ||
| TYPE_ASSIGN_CHECK(*in_attrs, 1, mshadow::kFloat32); | ||
| TYPE_ASSIGN_CHECK(*in_attrs, 2, mshadow::kFloat32); | ||
| TYPE_ASSIGN_CHECK(*out_attrs, 0, (*in_attrs)[0]); | ||
| TYPE_ASSIGN_CHECK(*out_attrs, 1, mshadow::kFloat32); | ||
| TYPE_ASSIGN_CHECK(*out_attrs, 2, mshadow::kFloat32); | ||
| return (*in_attrs)[0] != -1; | ||
| } | ||
|
|
||
| typedef bool (*TransposeShapeFunAny)(const nnvm::NodeAttrs&, | ||
| mxnet::ShapeVector*, | ||
| mxnet::ShapeVector*); | ||
|
|
||
| template <TransposeShapeFunAny TransposeShapeFun> | ||
| inline bool QuantizedTransposeShape(const nnvm::NodeAttrs& attrs, | ||
| mxnet::ShapeVector* in_attrs, | ||
| mxnet::ShapeVector* out_attrs) { | ||
| CHECK_EQ(in_attrs->size(), 3U); | ||
| CHECK_EQ(out_attrs->size(), 3U); | ||
| mxnet::ShapeVector qin_attrs(1); | ||
| mxnet::ShapeVector qout_attrs(1); | ||
| SHAPE_ASSIGN_CHECK(qin_attrs, 0, (*in_attrs)[0]); | ||
| SHAPE_ASSIGN_CHECK(qout_attrs, 0, (*out_attrs)[0]); | ||
| bool ret = TransposeShapeFun(attrs, &qin_attrs, &qout_attrs); | ||
| SHAPE_ASSIGN_CHECK(*in_attrs, 0, qin_attrs[0]); | ||
| SHAPE_ASSIGN_CHECK(*out_attrs, 0, qout_attrs[0]); | ||
| SHAPE_ASSIGN_CHECK(*in_attrs, 1, mxnet::TShape{1}); | ||
| SHAPE_ASSIGN_CHECK(*in_attrs, 2, mxnet::TShape{1}); | ||
| SHAPE_ASSIGN_CHECK(*out_attrs, 1, mxnet::TShape{1}); | ||
| SHAPE_ASSIGN_CHECK(*out_attrs, 2, mxnet::TShape{1}); | ||
| return ret; | ||
| } | ||
|
|
||
| #define MXNET_OPERATOR_REGISTER_QUANTIZED_TRANSPOSE(name) \ | ||
| NNVM_REGISTER_OP(name) \ | ||
| .set_num_inputs(3) \ | ||
| .set_num_outputs(3) \ | ||
| .set_attr<nnvm::FInferType>("FInferType", QuantizedTransposeType) \ | ||
| .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) \ | ||
| .set_attr<nnvm::FListInputNames>( \ | ||
| "FListInputNames", \ | ||
| [](const NodeAttrs& attrs) { \ | ||
| return std::vector<std::string>{"data", "min_data", "max_data"}; \ | ||
| }) \ | ||
| .set_attr<nnvm::FListOutputNames>( \ | ||
| "FListOutputNames", \ | ||
| [](const NodeAttrs& attrs) { \ | ||
| return std::vector<std::string>{"output", "min_output", "max_output"}; \ | ||
| }) \ | ||
| .set_attr<FQuantizable>("FQuantizable", \ | ||
| [](const NodeAttrs& attrs) { return QuantizeType::kSupport; }) \ | ||
| .add_argument("data", "NDArray-or-Symbol", "Array to be transposed.") \ | ||
| .add_argument("min_data", \ | ||
| "NDArray-or-Symbol", \ | ||
| "The minimum scalar value " \ | ||
| "possibly produced for the data") \ | ||
| .add_argument("max_data", \ | ||
| "NDArray-or-Symbol", \ | ||
| "The maximum scalar value " \ | ||
| "possibly produced for the data") | ||
|
|
||
| MXNET_OPERATOR_REGISTER_QUANTIZED_TRANSPOSE(_npx_quantized_transpose) | ||
| .set_attr_parser(ParamParser<NumpyTransposeParam>) | ||
| .set_attr<mxnet::FInferShape>("FInferShape", QuantizedTransposeShape<NumpyTransposeShape>) | ||
| .add_arguments(NumpyTransposeParam::__FIELDS__()); | ||
|
|
||
| MXNET_OPERATOR_REGISTER_QUANTIZED_TRANSPOSE(_contrib_quantized_transpose) | ||
| .add_alias("quantized_transpose") | ||
| .set_attr_parser(ParamParser<TransposeParam>) | ||
| .set_attr<mxnet::FInferShape>("FInferShape", QuantizedTransposeShape<TransposeShape>) | ||
| .add_arguments(TransposeParam::__FIELDS__()); | ||
|
|
||
| NNVM_REGISTER_OP(transpose).set_attr<FQuantizedOp>("FQuantizedOp", [](const NodeAttrs& attrs) { | ||
| nnvm::ObjectPtr node = nnvm::Node::Create(); | ||
| node->attrs.op = Op::Get("_contrib_quantized_transpose"); | ||
| node->attrs.name = "quantized_" + attrs.name; | ||
| node->attrs.dict = attrs.dict; | ||
| if (node->op()->attr_parser != nullptr) { | ||
| node->op()->attr_parser(&(node->attrs)); | ||
| } | ||
| return node; | ||
| }); | ||
|
|
||
| NNVM_REGISTER_OP(_npi_transpose).set_attr<FQuantizedOp>("FQuantizedOp", [](const NodeAttrs& attrs) { | ||
| nnvm::ObjectPtr node = nnvm::Node::Create(); | ||
| node->attrs.op = Op::Get("_npx_quantized_transpose"); | ||
| node->attrs.name = "quantized_" + attrs.name; | ||
| node->attrs.dict = attrs.dict; | ||
| if (node->op()->attr_parser != nullptr) { | ||
| node->op()->attr_parser(&(node->attrs)); | ||
| } | ||
| return node; | ||
| }); | ||
|
|
||
| } // namespace op | ||
| } // namespace mxnet | ||
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.
Uh oh!
There was an error while loading. Please reload this page.