cublas_utils.h 3.14 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20 21 22 23
/*!
 * \file Use external cudnn utils function
 */

24 25
#ifndef TVM_RUNTIME_CONTRIB_CUBLAS_CUBLAS_UTILS_H_
#define TVM_RUNTIME_CONTRIB_CUBLAS_CUBLAS_UTILS_H_
26 27

#include <dmlc/logging.h>
28
#include <dlpack/dlpack.h>
29
#include <cublas_v2.h>
30 31 32 33 34 35
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <cstdint>
#if CUDART_VERSION >= 10010
#include <cublasLt.h>
#endif  // CUDART_VERSION >= 10010
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

namespace tvm {
namespace contrib {

inline const char* GetCublasErrorString(int error) {
  switch (error) {
  case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  case CUBLAS_STATUS_LICENSE_ERROR: return "CUBLAS_STATUS_LICENSE_ERROR";
  }
  return "Unrecognized error";
}

#ifndef CHECK_CUBLAS_ERROR
#define CHECK_CUBLAS_ERROR(fn)                  \
  do {                                          \
    int error = static_cast<int>(fn);                      \
    CHECK_EQ(error, CUBLAS_STATUS_SUCCESS) << "CUBLAS: " << GetCublasErrorString(error); \
  } while (0)  // ; intentionally left off.
#endif  // CHECK_CUBLAS_ERROR


struct CuBlasThreadEntry {
  CuBlasThreadEntry();
  ~CuBlasThreadEntry();
  cublasHandle_t handle{nullptr};
  static CuBlasThreadEntry* ThreadLocal();
};  // CuBlasThreadEntry

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
inline cudaDataType_t GetCudaDataType(DLDataType type) {
  if (type.code == kDLInt) {
    switch (type.bits) {
      case 8: return CUDA_R_8I;
      case 32: return CUDA_R_32I;
    }
  } else if (type.code == kDLUInt) {
    switch (type.bits) {
      case 8: return CUDA_R_8U;
      case 32: return CUDA_R_32U;
    }
  } else if (type.code == kDLFloat) {
    switch (type.bits) {
      case 16: return CUDA_R_16F;
      case 32: return CUDA_R_32F;
      case 64: return CUDA_R_64F;
    }
  }
  LOG(FATAL) << "Unsupported cuda type";
  return CUDA_R_16F;
}
92 93 94
}  // namespace contrib
}  // namespace tvm

95
#endif  // TVM_RUNTIME_CONTRIB_CUBLAS_CUBLAS_UTILS_H_