Skip to content

Commit

Permalink
[pytorch] Make qlinear weight packing thread safe (#63804)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #63804

Adding a lock around weight packing section of qlinear + qlinear_dynamic

Test Plan: automated tests

Reviewed By: kimishpatel

Differential Revision: D30340957

fbshipit-source-id: 1c9faf796c4ffbc74345396188a6f1154a76bea6
  • Loading branch information
John Shen authored and facebook-github-bot committed Sep 9, 2021
1 parent dc53546 commit c3203ef
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aten/src/ATen/native/quantized/cpu/qlinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ at::Tensor PackedLinearWeightsQnnp::apply_impl(
size_t cols_w = input_contig.size(input_contig.dim() - 1);
auto input_scale = input_contig.q_scale();

// QNNPack is not thread safe
std::lock_guard<std::mutex> lock(qnnp_mutex_);
if (!this->input_scale.has_value() ||
this->input_scale.value() != input_scale) {
// Get the original weight and adjust it to uint8 from int8
Expand Down
3 changes: 3 additions & 0 deletions aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ at::Tensor PackedLinearWeightsQnnp::apply_dynamic_impl(at::Tensor input) {
/*qmin=*/0,
/*qmax=*/255);
float* weight_scales_data = w_scales.data_ptr<float>();

// QNNPack is not thread safe
std::lock_guard<std::mutex> lock(qnnp_mutex_);
if (!input_scale.has_value() || input_scale.value() != q_params.scale) {
generate_requantization_scales(
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
Expand Down
1 change: 1 addition & 0 deletions aten/src/ATen/native/quantized/cpu/qnnpack_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct PackedLinearWeightsQnnp : public LinearPackedParamsBase {
c10::optional<at::Tensor> bias);

private:
std::mutex qnnp_mutex_;
template <bool ReluFused>
at::Tensor apply_impl(
at::Tensor input,
Expand Down

0 comments on commit c3203ef

Please sign in to comment.