forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I9b7530b0a747ecc4217933773520b2d5eb162e69
- Loading branch information
Showing
37 changed files
with
542 additions
and
295 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// RUN: %clangxx_nsan -O0 -g %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
|
||
// RUN: %clangxx_nsan -O3 -g %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
|
||
// RUN: %clangxx_nsan -O0 -g %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1 not %run %t | ||
|
||
#include <cmath> | ||
#include <cstdio> | ||
|
||
// This function returns a NaN value for triggering the NaN detection. | ||
__attribute__((noinline)) float ReturnNaN(float p, float q) { | ||
float ret = p / q; | ||
return ret; | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
|
||
int main() { | ||
float val = ReturnNaN(0., 0.); | ||
printf("%f\n", val); | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
return 0; | ||
} |
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,54 @@ | ||
// RUN: %clangxx_nsan -O0 -g -DSOFTMAX=softmax %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0,log2_max_relative_error=19 %run %t 2>&1 | FileCheck %s | ||
|
||
// RUN: %clangxx_nsan -O3 -g -DSOFTMAX=softmax %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0,log2_max_relative_error=19 %run %t 2>&1 | FileCheck %s | ||
|
||
// RUN: %clangxx_nsan -O0 -g -DSOFTMAX=stable_softmax %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1,log2_max_relative_error=19 %run %t | ||
|
||
// RUN: %clangxx_nsan -O3 -g -DSOFTMAX=stable_softmax %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=1,log2_max_relative_error=19 %run %t | ||
|
||
#include<iostream> | ||
#include<vector> | ||
#include<algorithm> | ||
#include<cmath> | ||
|
||
// unstable softmax | ||
template <typename T> | ||
__attribute__((noinline)) void softmax(std::vector<T> &values) { | ||
T sum_exp = 0.0; | ||
for (auto &i: values) { | ||
i = std::exp(i); | ||
sum_exp += i; | ||
} | ||
for (auto &i: values) { | ||
i /= sum_exp; | ||
} | ||
} | ||
|
||
// use max value to avoid overflow | ||
// \sigma_i exp(x_i) / \sum_j exp(x_j) = \sigma_i exp(x_i - max(x)) / \sum_j exp(x_j - max(x)) | ||
template <typename T> | ||
__attribute__((noinline)) void stable_softmax(std::vector<T> &values) { | ||
T sum_exp = 0.0; | ||
T max_values = *std::max_element(values.begin(), values.end()); | ||
for (auto &i: values) { | ||
i = std::exp(i - max_values); | ||
sum_exp += i; | ||
} | ||
for (auto &i:values) { | ||
i /= sum_exp; | ||
} | ||
} | ||
|
||
int main() { | ||
std::vector<double> data = {1000, 1001, 1002}; | ||
SOFTMAX(data); | ||
for (auto i: data) { | ||
printf("%f", i); | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
return 0; | ||
} |
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,34 @@ | ||
// RUN: %clangxx_nsan -O0 -g -mavx %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
// RUN: %clangxx_nsan -O3 -g -mavx %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
|
||
#include <cmath> | ||
#include <immintrin.h> | ||
#include <iostream> | ||
|
||
void simd_sqrt(const float *input, float *output, size_t size) { | ||
size_t i = 0; | ||
for (; i + 7 < size; i += 8) { | ||
__m256 vec = _mm256_loadu_ps(&input[i]); | ||
__m256 result = _mm256_sqrt_ps(vec); | ||
_mm256_storeu_ps(&output[i], result); | ||
} | ||
for (; i < size; ++i) { | ||
output[i] = std::sqrt(input[i]); | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
} | ||
|
||
int main() { | ||
float input[] = {1.0, 2.0, -3.0, 4.0, 5.0, 6.0, 7.0, | ||
8.0, 9.0, -10.0, 11.0, 12.0, 13.0, 14.0, | ||
15.0, -16.0, 17.0, -18.0, -19.0, -20.0}; | ||
float output[20]; | ||
simd_sqrt(input, output, 20); | ||
for (int i = 0; i < 20; ++i) { | ||
std::cout << output[i] << std::endl; | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
return 0; | ||
} |
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,25 @@ | ||
// RUN: %clangxx_nsan -O0 -g -mavx %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
// RUN: %clangxx_nsan -O3 -g -mavx %s -o %t | ||
// RUN: NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s | ||
#include <iostream> | ||
#include <cmath> | ||
|
||
typedef float v8sf __attribute__ ((vector_size(32))); | ||
|
||
v8sf simd_sqrt(v8sf a) { | ||
return __builtin_elementwise_sqrt(a); | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
|
||
int main() { | ||
v8sf a = {-1.0, -2.0, -3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; | ||
a = simd_sqrt(a); | ||
|
||
// This prevents DCE. | ||
for (size_t i = 0; i < 8; ++i) { | ||
std::cout << a[i] << std::endl; | ||
// CHECK: WARNING: NumericalStabilitySanitizer: NaN detected | ||
} | ||
return 0; | ||
} |
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.