Skip to content

Commit

Permalink
Remove usage of _mm_maskmoveu_si128 (pytorch#421)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#421

asan doesn't like this instruction and it may be causing segfault. Also _mm_maskmoveu_si128 uses non-temporal store and in fact may be slow.
https://stackoverflow.com/questions/62183557/how-to-most-efficiently-store-a-part-of-m128i-m256i-while-ignoring-some-num

Differential Revision: D23400226

fbshipit-source-id: 92c657e44e4c5259ecab322165e9c509189b73f7
  • Loading branch information
dskhudia authored and facebook-github-bot committed Aug 29, 2020
1 parent 59558e6 commit 20d994f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/QuantUtilsAvx2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cmath> //for nearbyint
#include <limits> //for numeric_limits
#include <cassert> //for assert
#include <cstring> //for memcpy
#include "./MaskAvx2.h"

namespace fbgemm {
Expand All @@ -19,9 +20,8 @@ using namespace std;
////////////////////////////////////////////////////////////////////////////////
// Utility functions

// ASAN seems to have a false-positive for _mm_maskmoveu_si128
template <typename T, bool LEGACY>
void NO_SANITIZE("address") QuantizeAvx2(
void QuantizeAvx2(
const float* src,
T* dst,
int len,
Expand Down Expand Up @@ -87,8 +87,8 @@ void NO_SANITIZE("address") QuantizeAvx2(
if (rem > 0) {
__m256i mask_v = _mm256_load_si256(reinterpret_cast<const __m256i*>(
internal::avx2_ps_or_epi32_masks[rem]));
__m128i store_mask_v = _mm_load_si128(
reinterpret_cast<const __m128i*>(internal::sse_epi8_masks[rem]));
// __m128i store_mask_v = _mm_load_si128(
// reinterpret_cast<const __m128i*>(internal::sse_epi8_masks[rem]));
__m256 src_v = _mm256_maskload_ps(src + i, mask_v);
__m256 transformed_v;
if (LEGACY) {
Expand All @@ -113,10 +113,14 @@ void NO_SANITIZE("address") QuantizeAvx2(
// as "rem" number of 8-bit integers
clipped_v = _mm256_shuffle_epi8(clipped_v, shuffle_mask_v);
clipped_v = _mm256_permutevar8x32_epi32(clipped_v, permute_mask_v);
_mm_maskmoveu_si128(
_mm256_castsi256_si128(clipped_v),
store_mask_v,
reinterpret_cast<char*>(dst + i));
// do not use _mm_maskmoveu_si128 instead of memcpy.
// asan has false positives for _mm_maskmoveu_si128 and this instruction
// sometimes causes segfault (root cause is unknown).
memcpy(dst + i, reinterpret_cast<void*>(&clipped_v), rem * sizeof(T));
// _mm_maskmoveu_si128(
// _mm256_castsi256_si128(clipped_v),
// store_mask_v,
// reinterpret_cast<char*>(dst + i));
}
#endif
}
Expand Down

0 comments on commit 20d994f

Please sign in to comment.