Skip to content
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

Add device options to enable tensor core math mode. #3066

Merged
merged 7 commits into from
Mar 4, 2019
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
10 changes: 10 additions & 0 deletions src/cudamatrix/cu-device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ void CuDevice::Initialize() {
// Initialize CUBLAS.
CUBLAS_SAFE_CALL(cublasCreate(&cublas_handle_));
CUBLAS_SAFE_CALL(cublasSetStream(cublas_handle_, cudaStreamPerThread));

if (device_options_.use_tensor_cores) {
// Enable tensor cores in CUBLAS
// Note if the device does not support tensor cores this will fall back to normal math mode
CUBLAS_SAFE_CALL(cublasSetMathMode(cublas_handle_,
CUBLAS_TENSOR_OP_MATH));
}

// Initialize the cuSPARSE library
CUSPARSE_SAFE_CALL(cusparseCreate(&cusparse_handle_));
CUSPARSE_SAFE_CALL(cusparseSetStream(cusparse_handle_, cudaStreamPerThread));
Expand Down Expand Up @@ -525,6 +533,8 @@ CuDevice::~CuDevice() {
// Each thread has its own copy of the CuDevice object.
// Note: this was declared "static".
thread_local CuDevice CuDevice::this_thread_device_;

CuDevice::CuDeviceOptions CuDevice::device_options_;

// define and initialize the static members of the CuDevice object.
int32 CuDevice::device_id_ = -1;
Expand Down
23 changes: 23 additions & 0 deletions src/cudamatrix/cu-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,31 @@ class CuDevice {
/// (i.e. from outside the class), call this only if Enabled() returns true.
bool IsComputeExclusive();

// Register command line options for CUDA device.
// This must be done before calling CuDevice::Initialize()
// Example:
// CuDevice::RegisterDeviceOptions(&po);
// po.Read(argc, argv);
// CuDevice::Initialize();
static void RegisterDeviceOptions(OptionsItf *po) {
CuDevice::device_options_.Register(po);
}
~CuDevice();
private:

struct CuDeviceOptions {
bool use_tensor_cores; // Enable tensor cores
CuDeviceOptions () : use_tensor_cores(false) {};
void Register(OptionsItf *po) {
po->Register("cuda-use-tensor-cores", &use_tensor_cores,
"Enable FP16 tensor math. "
"This is higher performance but less accuracy. "
"This is only recommended for inference.");
}
};

static CuDeviceOptions device_options_;

// Default constructor used to initialize this_thread_device_
CuDevice();
CuDevice(CuDevice&); // Disallow.
Expand Down
4 changes: 4 additions & 0 deletions src/nnet3bin/nnet3-compute-batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ int main(int argc, char *argv[]) {
"priors stored with the model (in this case, "
"a .mdl file is expected as input).");

#if HAVE_CUDA==1
CuDevice::RegisterDeviceOptions(&po);
#endif

po.Read(argc, argv);

if (po.NumArgs() != 3) {
Expand Down
4 changes: 4 additions & 0 deletions src/nnet3bin/nnet3-compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ int main(int argc, char *argv[]) {
"priors stored with the model (in this case, "
"a .mdl file is expected as input).");

#if HAVE_CUDA==1
CuDevice::RegisterDeviceOptions(&po);
#endif

po.Read(argc, argv);

if (po.NumArgs() != 3) {
Expand Down
4 changes: 4 additions & 0 deletions src/nnet3bin/nnet3-latgen-faster-batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ int main(int argc, char *argv[]) {
po.Register("use-gpu", &use_gpu,
"yes|no|optional|wait, only has effect if compiled with CUDA");

#if HAVE_CUDA==1
CuDevice::RegisterDeviceOptions(&po);
#endif

po.Read(argc, argv);

if (po.NumArgs() != 4) {
Expand Down
4 changes: 4 additions & 0 deletions src/nnet3bin/nnet3-xvector-compute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ int main(int argc, char *argv[]) {
po.Register("pad-input", &pad_input, "If true, duplicate the first and "
"last frames of the input features as required to equal min-chunk-size.");

#if HAVE_CUDA==1
CuDevice::RegisterDeviceOptions(&po);
#endif

po.Read(argc, argv);

if (po.NumArgs() != 3) {
Expand Down