-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebNN EP] Add quantize Ops (#18011)
### Description <!-- Describe your changes. --> Add four quantize Ops: MatmulInteger, ConvInteger, DynamicQuantizeLinear and DequantizeLinear. Add datatype TensorProto_DataType_INT8 and TensorProto_DataType_UINT8. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Support quantized models.
- Loading branch information
zesongw
authored
Jan 12, 2024
1 parent
acba63c
commit e1db44b
Showing
13 changed files
with
232 additions
and
4 deletions.
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
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
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
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
70 changes: 70 additions & 0 deletions
70
onnxruntime/core/providers/webnn/builders/impl/dequantizeLinear_op_builder.cc
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,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Copyright (c) Intel Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/common/safeint.h" | ||
#include "core/optimizer/initializer.h" | ||
#include "core/providers/common.h" | ||
#include "core/providers/shared/utils/utils.h" | ||
#include "core/providers/webnn/builders/helper.h" | ||
#include "core/providers/webnn/builders/model_builder.h" | ||
#include "core/providers/webnn/builders/op_builder_factory.h" | ||
|
||
#include "core/providers/webnn/builders/impl/base_op_builder.h" | ||
|
||
namespace onnxruntime { | ||
namespace webnn { | ||
|
||
class DequantizeLinearOpBuilder : public BaseOpBuilder { | ||
// Add operator related. | ||
private: | ||
Status AddToModelBuilderImpl(ModelBuilder& model_builder, const Node& node, | ||
const logging::Logger& logger) const override ORT_MUST_USE_RESULT; | ||
}; | ||
|
||
Status DequantizeLinearOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, | ||
const Node& node, | ||
const logging::Logger& logger) const { | ||
const auto& input_defs = node.InputDefs(); | ||
emscripten::val input = model_builder.GetOperand(input_defs[0]->Name()); | ||
emscripten::val scale = model_builder.GetOperand(input_defs[1]->Name()); | ||
emscripten::val zero_point = emscripten::val::null(); | ||
if (input_defs.size() == 3) { | ||
zero_point = model_builder.GetOperand(node.InputDefs()[2]->Name()); | ||
} else { | ||
zero_point = model_builder.GetZeroConstant("uint8"); | ||
} | ||
emscripten::val output; | ||
std::vector<int64_t> input_shape; | ||
std::vector<int64_t> scale_shape; | ||
ORT_RETURN_IF_NOT(GetShape(*input_defs[0], input_shape, logger), "Cannot get input shape"); | ||
ORT_RETURN_IF_NOT(GetShape(*input_defs[1], scale_shape, logger), "Cannot get scale shape"); | ||
NodeAttrHelper helper(node); | ||
int32_t axis = helper.Get("axis", 1); | ||
// axis is valid for input shape greater than 1D. | ||
if (input_shape.size() > 1) { | ||
axis = static_cast<int32_t>(HandleNegativeAxis(axis, input_shape.size())); | ||
} | ||
// Insert ones before and after the axis dimension for broadcasting of 1D scale tensor. | ||
if (1 == scale_shape.size() && 1 < input_shape.size()) { | ||
std::vector<int32_t> target_shape{static_cast<int>(input_shape[axis])}; | ||
target_shape.insert(target_shape.begin(), axis, 1); | ||
target_shape.insert(target_shape.end(), input_shape.size() - axis - 1, 1); | ||
scale = model_builder.GetBuilder().call<emscripten::val>("reshape", scale, emscripten::val::array(target_shape)); | ||
zero_point = model_builder.GetBuilder().call<emscripten::val>("reshape", | ||
zero_point, emscripten::val::array(target_shape)); | ||
} | ||
output = model_builder.GetBuilder().call<emscripten::val>("dequantizeLinear", input, scale, zero_point); | ||
|
||
model_builder.AddOperand(node.OutputDefs()[0]->Name(), std::move(output)); | ||
|
||
return Status::OK(); | ||
} | ||
|
||
void CreateDequantizeLinearOpBuilder(const std::string& op_type, OpBuilderRegistrations& op_registrations) { | ||
op_registrations.builders.push_back(std::make_unique<DequantizeLinearOpBuilder>()); | ||
op_registrations.op_builder_map.emplace(op_type, op_registrations.builders.back().get()); | ||
} | ||
|
||
} // namespace webnn | ||
} // namespace onnxruntime |
49 changes: 49 additions & 0 deletions
49
onnxruntime/core/providers/webnn/builders/impl/dynamicQuantizeLinear_op_builder.cc
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,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Copyright (c) Intel Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/common/safeint.h" | ||
#include "core/optimizer/initializer.h" | ||
#include "core/providers/common.h" | ||
#include "core/providers/shared/utils/utils.h" | ||
#include "core/providers/webnn/builders/helper.h" | ||
#include "core/providers/webnn/builders/model_builder.h" | ||
#include "core/providers/webnn/builders/op_builder_factory.h" | ||
|
||
#include "core/providers/webnn/builders/impl/base_op_builder.h" | ||
|
||
namespace onnxruntime { | ||
namespace webnn { | ||
|
||
class DynamicQuantizaLinearOpBuilder : public BaseOpBuilder { | ||
// Add operator related. | ||
private: | ||
Status AddToModelBuilderImpl(ModelBuilder& model_builder, const Node& node, | ||
const logging::Logger& logger) const override ORT_MUST_USE_RESULT; | ||
}; | ||
|
||
Status DynamicQuantizaLinearOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, | ||
const Node& node, | ||
const logging::Logger& logger) const { | ||
const auto& input_defs = node.InputDefs(); | ||
emscripten::val input = model_builder.GetOperand(input_defs[0]->Name()); | ||
emscripten::val output_array; | ||
std::vector<int64_t> input_shape; | ||
ORT_RETURN_IF_NOT(GetShape(*input_defs[0], input_shape, logger), "Cannot get shape"); | ||
emscripten::val options = emscripten::val::object(); | ||
|
||
output_array = model_builder.GetBuilder().call<emscripten::val>("dynamicQuantizeLinear", input); | ||
|
||
for (size_t i = 0, count = output_array["length"].as<size_t>(); i < count; i++) { | ||
model_builder.AddOperand(node.OutputDefs()[i]->Name(), std::move(output_array[i])); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
void CreateDynamicQuantizeLinearOpBuilder(const std::string& op_type, OpBuilderRegistrations& op_registrations) { | ||
op_registrations.builders.push_back(std::make_unique<DynamicQuantizaLinearOpBuilder>()); | ||
op_registrations.op_builder_map.emplace(op_type, op_registrations.builders.back().get()); | ||
} | ||
|
||
} // namespace webnn | ||
} // namespace onnxruntime |
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
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
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
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
Oops, something went wrong.