Skip to content

rocblas alt impl during backward pass only #978

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions aten/src/ATen/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ bool NoTF32Guard::should_disable_tf32() {
return override_allow_tf32_flag;
}

thread_local bool BackwardPassGuard::is_backward_pass_;

BackwardPassGuard::BackwardPassGuard() {
is_backward_pass_ = true;
}

BackwardPassGuard::~BackwardPassGuard() {
is_backward_pass_ = false;
}

bool BackwardPassGuard::is_backward_pass() {
return is_backward_pass_;
}

bool Context::areVmapFallbackWarningsEnabled() const {
return display_vmap_fallback_warnings_;
}
Expand Down
8 changes: 8 additions & 0 deletions aten/src/ATen/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,12 @@ struct TORCH_API NoTF32Guard {
bool changed = false;
};

struct TORCH_API BackwardPassGuard {
BackwardPassGuard();
~BackwardPassGuard();
static bool is_backward_pass();
private:
static thread_local bool is_backward_pass_;
};

} // namespace at
17 changes: 15 additions & 2 deletions aten/src/ATen/cuda/CUDABlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include <cublasLt.h>
#endif

#ifdef USE_ROCM
#define PYTORCH_ROCBLAS_VERSION_DECIMAL (ROCBLAS_VERSION_MAJOR * 100 + ROCBLAS_VERSION_MINOR)
#define USE_GEMM_FLAGS_FP16_ALT_IMPL (PYTORCH_ROCBLAS_VERSION_DECIMAL >= 242)
#endif

#define CUDABLAS_POSINT_CHECK(FD, X) \
TORCH_CHECK( \
(X > 0 && X <= INT_MAX), \
Expand Down Expand Up @@ -246,13 +251,17 @@ void bgemm<at::Half>(CUDABLAS_BGEMM_ARGTYPES(at::Half)) {
float falpha = alpha;
float fbeta = beta;
#ifdef USE_ROCM
int flag = 0;
#if USE_GEMM_FLAGS_FP16_ALT_IMPL
flag = at::BackwardPassGuard::is_backward_pass() ? rocblas_gemm_flags_fp16_alt_impl : 0;
#endif
TORCH_CUDABLAS_CHECK(rocblas_gemm_strided_batched_ex(handle, opa, opb, (int)m, (int)n, (int)k,
(void*)&falpha, a, rocblas_datatype_f16_r, (int)lda, stridea,
b, rocblas_datatype_f16_r, (int)ldb, strideb,
(void*)&fbeta, c, rocblas_datatype_f16_r, (int)ldc, stridec,
c, rocblas_datatype_f16_r, (int)ldc, stridec,
(int) num_batches, rocblas_datatype_f32_r, rocblas_gemm_algo_standard,
0, 0));
0, flag));
#else
#if defined(CUDA_VERSION) && CUDA_VERSION < 11000
// On CUDA versions prior to 11, users are required to set the math mode to CUBLAS_TENSOR_OP_MATH
Expand Down Expand Up @@ -392,6 +401,10 @@ void gemm<at::Half>(CUDABLAS_GEMM_ARGTYPES(at::Half)) {
_cublasAdjustLdLevel3(transa, transb, m, n, k, &lda, &ldb, &ldc);
GEMM_CHECK_ARGVALUES(at::Half);
#ifdef USE_ROCM
int flag = 0;
#if USE_GEMM_FLAGS_FP16_ALT_IMPL
flag = at::BackwardPassGuard::is_backward_pass() ? rocblas_gemm_flags_fp16_alt_impl : 0;
#endif
TORCH_CUDABLAS_CHECK(rocblas_gemm_ex(
handle,
opa,
Expand All @@ -416,7 +429,7 @@ void gemm<at::Half>(CUDABLAS_GEMM_ARGTYPES(at::Half)) {
rocblas_datatype_f32_r,
rocblas_gemm_algo_standard,
0,
0));
flag));
#else
cudaDeviceProp* prop = at::cuda::getCurrentDeviceProperties();
if (prop->major >= 5) {
Expand Down
3 changes: 3 additions & 0 deletions torch/csrc/autograd/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ struct TORCH_API Node : std::enable_shared_from_this<Node> {
// probably operate with names.
at::NoNamesGuard no_names_guard;

// Keep track of backward pass for rocblas.
at::BackwardPassGuard in_backward;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required?

Copy link
Collaborator

@pruthvistony pruthvistony Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in the Node object. Please ignore previous comment.


bool pre_sampled = false;
if (at::shouldRunRecordFunction(&pre_sampled)) {
// Using RecordFunction to trigger observers in the backward pass
Expand Down