Upload apex-master/csrc/update_scale_hysteresis.cu with huggingface_hub
Browse files
apex-master/csrc/update_scale_hysteresis.cu
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <ATen/ATen.h>
|
| 2 |
+
#include <ATen/cuda/Exceptions.h>
|
| 3 |
+
#include <ATen/cuda/CUDAContext.h>
|
| 4 |
+
|
| 5 |
+
__global__ void update_scale_hysteresis_cuda_kernel(float* current_scale,
|
| 6 |
+
int* growth_tracker,
|
| 7 |
+
int* hysteresis_tracker,
|
| 8 |
+
const float* found_inf,
|
| 9 |
+
double growth_factor,
|
| 10 |
+
double backoff_factor,
|
| 11 |
+
int growth_interval,
|
| 12 |
+
int hysteresis)
|
| 13 |
+
{
|
| 14 |
+
if (*found_inf > 0) {
|
| 15 |
+
*hysteresis_tracker -= 1;
|
| 16 |
+
|
| 17 |
+
// Only reset the growth tracker when hysteresis is larger than zero
|
| 18 |
+
if (*hysteresis_tracker > 0) {
|
| 19 |
+
*growth_tracker = 0;
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if (*found_inf) {
|
| 25 |
+
*current_scale = (*current_scale)*backoff_factor;
|
| 26 |
+
*growth_tracker = 0;
|
| 27 |
+
} else {
|
| 28 |
+
// Entering this branch means we just carried out a successful step,
|
| 29 |
+
// so growth_tracker is incremented before comparing to growth_interval.
|
| 30 |
+
auto successful = (*growth_tracker) + 1;
|
| 31 |
+
if (successful == growth_interval) {
|
| 32 |
+
auto new_scale = static_cast<float>((*current_scale)*growth_factor);
|
| 33 |
+
// Do not grow the scale past fp32 bounds to inf.
|
| 34 |
+
if (isfinite(new_scale)) {
|
| 35 |
+
*current_scale = new_scale;
|
| 36 |
+
}
|
| 37 |
+
*growth_tracker = 0;
|
| 38 |
+
} else {
|
| 39 |
+
*growth_tracker = successful;
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Reset the hysteresis tracker if no infs are found
|
| 44 |
+
if (*found_inf <= 0) {
|
| 45 |
+
*hysteresis_tracker = hysteresis;
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
at::Tensor update_scale_hysteresis_cuda(at::Tensor current_scale,
|
| 50 |
+
at::Tensor growth_tracker,
|
| 51 |
+
at::Tensor hysteresis_tracker,
|
| 52 |
+
at::Tensor found_inf,
|
| 53 |
+
const double growth_factor,
|
| 54 |
+
const double backoff_factor,
|
| 55 |
+
const int64_t growth_interval,
|
| 56 |
+
const int hysteresis)
|
| 57 |
+
{
|
| 58 |
+
update_scale_hysteresis_cuda_kernel<<<1, 1, 0, at::cuda::getCurrentCUDAStream()>>>(
|
| 59 |
+
current_scale.mutable_data_ptr<float>(),
|
| 60 |
+
growth_tracker.mutable_data_ptr<int>(),
|
| 61 |
+
hysteresis_tracker.mutable_data_ptr<int>(),
|
| 62 |
+
found_inf.const_data_ptr<float>(),
|
| 63 |
+
growth_factor,
|
| 64 |
+
backoff_factor,
|
| 65 |
+
growth_interval,
|
| 66 |
+
hysteresis);
|
| 67 |
+
|
| 68 |
+
AT_CUDA_CHECK(cudaGetLastError());
|
| 69 |
+
|
| 70 |
+
return current_scale;
|
| 71 |
+
}
|