-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Fix ComputePropagateScalesMkldnnPass of MKLDNN #47574
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,27 +336,46 @@ void ComputePropagateScalesMkldnnPass::ComputeWeightScales( | |
ComputeLstmWeightScales(graph, scope, "WeightX", "WeightH", var_quant_scales); | ||
} | ||
|
||
void ComputePropagateScalesMkldnnPass::UpdateScaleOpInScale( | ||
void ComputePropagateScalesMkldnnPass::UpdateScaleOpInOutScales( | ||
Node* op_node, | ||
const std::string& input_name, | ||
const std::string& output_name, | ||
StringPairMap* var_quant_scales) const { | ||
auto iter = var_quant_scales->find(output_name); | ||
if (iter != var_quant_scales->end()) { | ||
auto pair = iter->second; | ||
const auto tensor = pair.second; | ||
|
||
const auto scale = PADDLE_GET_CONST(float, op_node->Op()->GetAttr("scale")); | ||
phi::DenseTensor tmp_tensor; | ||
tmp_tensor.Resize(tensor.dims()); | ||
auto* data = tmp_tensor.mutable_data<float>(platform::CPUPlace()); | ||
for (int i = 0; i < tensor.numel(); i++) { | ||
data[i] = data[i] * scale; | ||
} | ||
auto out_iter = var_quant_scales->find(output_name); | ||
auto input_iter = var_quant_scales->find(input_name); | ||
// All the input and output have scales | ||
if (out_iter != var_quant_scales->end() && | ||
input_iter != var_quant_scales->end()) { | ||
return; | ||
} | ||
|
||
auto new_pair = std::make_pair(pair.first, tmp_tensor); | ||
var_quant_scales->insert(std::make_pair(input_name, new_pair)); | ||
const auto scale = PADDLE_GET_CONST(float, op_node->Op()->GetAttr("scale")); | ||
if (std::abs(scale) < 1e-6 && out_iter != var_quant_scales->end()) { | ||
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. Did you find any example where the scale was so small? 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. I didn't find so many cases, but when out_iter != var_quant_scales->end(), we need to divide, so add this judgment to avoid crashes |
||
return; | ||
} | ||
|
||
std::string name = input_name; | ||
auto iter = out_iter; | ||
if (input_iter != var_quant_scales->end()) { | ||
iter = input_iter; | ||
name = output_name; | ||
} | ||
|
||
phi::DenseTensor tmp_tensor; | ||
auto pair = iter->second; | ||
const auto tensor = pair.second; | ||
tmp_tensor.Resize(tensor.dims()); | ||
auto* data = tmp_tensor.mutable_data<float>(platform::CPUPlace()); | ||
auto* src_data = tensor.data<float>(); | ||
for (int i = 0; i < tensor.numel(); i++) { | ||
if (out_iter != var_quant_scales->end()) { | ||
data[i] = src_data[i] / scale; | ||
} else { | ||
data[i] = src_data[i] * scale; | ||
} | ||
} | ||
auto new_pair = std::make_pair(pair.first, tmp_tensor); | ||
var_quant_scales->insert(std::make_pair(name, new_pair)); | ||
} | ||
|
||
std::unordered_set<std::string> ComputePropagateScalesMkldnnPass::UpdateScales( | ||
|
@@ -403,10 +422,12 @@ std::unordered_set<std::string> ComputePropagateScalesMkldnnPass::UpdateScales( | |
} | ||
} else if (op_name == "scale") { | ||
const std::string output_name = op_node->Op()->Output("Out")[0]; | ||
const std::string input_name = op_node->Op()->Input("X")[0]; | ||
auto out_iter = var_quant_scales->find(output_name); | ||
if (out_iter != var_quant_scales->end()) { | ||
const std::string input_name = op_node->Op()->Input("X")[0]; | ||
UpdateScaleOpInScale( | ||
auto input_iter = var_quant_scales->find(input_name); | ||
if (out_iter != var_quant_scales->end() || | ||
input_iter != var_quant_scales->end()) { | ||
UpdateScaleOpInOutScales( | ||
op_node, input_name, output_name, var_quant_scales); | ||
} | ||
} | ||
|
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.
I think that for a new quantization method where there is linear_quantize before and and linear_dequantize after each operator this UpdateScaleOp it is no longer needed so much.