|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +#include <curand.h> |
| 20 | +#include <tvm/runtime/c_runtime_api.h> |
| 21 | +#include <tvm/runtime/registry.h> |
| 22 | + |
| 23 | +#include "../../cuda/cuda_common.h" |
| 24 | +#include "./helper_cuda_kernels.h" |
| 25 | + |
| 26 | +namespace tvm { |
| 27 | +namespace runtime { |
| 28 | +namespace curand { |
| 29 | + |
| 30 | +#define TVM_CURAND_CALL(func) \ |
| 31 | + { \ |
| 32 | + curandStatus_t e = (func); \ |
| 33 | + ICHECK(e == CURAND_STATUS_SUCCESS) << "cuRAND error: " << e; \ |
| 34 | + } |
| 35 | + |
| 36 | +class CURandGenerator { |
| 37 | + public: |
| 38 | + CURandGenerator() { TVM_CURAND_CALL(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT)); } |
| 39 | + ~CURandGenerator() { TVM_CURAND_CALL(curandDestroyGenerator(gen)); } |
| 40 | + |
| 41 | + void Generate32bit(void* ptr, int64_t n) { |
| 42 | + TVM_CURAND_CALL(curandGenerateNormal(gen, static_cast<float*>(ptr), n, 0.0f, 5.0f)); |
| 43 | + cudaDeviceSynchronize(); |
| 44 | + } |
| 45 | + |
| 46 | + void Generate64bit(void* ptr, int64_t n) { |
| 47 | + TVM_CURAND_CALL(curandGenerateNormalDouble(gen, static_cast<double*>(ptr), n, 0.0f, 5.0f)); |
| 48 | + } |
| 49 | + |
| 50 | + curandGenerator_t gen; |
| 51 | +}; |
| 52 | + |
| 53 | +DeviceAPI* GetCUDADeviceAPI() { |
| 54 | + const PackedFunc* get_cuda_api = runtime::Registry::Get("device_api.cuda"); |
| 55 | + ICHECK(get_cuda_api) << "ValueError: TVM is not built with USE_CUDA=ON"; |
| 56 | + void* ret = (*get_cuda_api)(); |
| 57 | + runtime::DeviceAPI* cuda_api = static_cast<runtime::DeviceAPI*>(ret); |
| 58 | + return cuda_api; |
| 59 | +} |
| 60 | + |
| 61 | +int64_t GetTensorSize(DLTensor* tensor) { |
| 62 | + int64_t tensor_size = 1; |
| 63 | + for (int i = 0; i < tensor->ndim; ++i) { |
| 64 | + tensor_size *= tensor->shape[i]; |
| 65 | + } |
| 66 | + return tensor_size; |
| 67 | +} |
| 68 | + |
| 69 | +struct DeferredFunc { |
| 70 | + public: |
| 71 | + DeferredFunc(std::function<void()> func) : func_(func) {} |
| 72 | + ~DeferredFunc() { func_(); } |
| 73 | + |
| 74 | + private: |
| 75 | + std::function<void()> func_; |
| 76 | +}; |
| 77 | + |
| 78 | +void RandomFill(DLTensor* tensor) { |
| 79 | + static DeviceAPI* cuda_api = GetCUDADeviceAPI(); |
| 80 | + CHECK(tensor->device.device_type == DLDeviceType::kDLCUDA) |
| 81 | + << "ValueError: cuRAND only works on CUDA devices"; |
| 82 | + if (tensor->dtype.code == DLDataTypeCode::kDLFloat && tensor->dtype.bits == 16) { |
| 83 | + int64_t tensor_size = GetTensorSize(tensor); |
| 84 | + void* data = cuda_api->AllocWorkspace(tensor->device, tensor_size * sizeof(float)); |
| 85 | + { |
| 86 | + DeferredFunc defer([data, tensor]() { cuda_api->FreeWorkspace(tensor->device, data); }); |
| 87 | + CURandGenerator().Generate32bit(data, GetTensorSize(tensor)); |
| 88 | + ConvertFp32toFp16(/*src=*/data, /*dst=*/tensor->data, /*num=*/tensor_size); |
| 89 | + } |
| 90 | + } else if (tensor->dtype.code == DLDataTypeCode::kDLFloat && tensor->dtype.bits == 32) { |
| 91 | + CURandGenerator().Generate32bit(tensor->data, GetTensorSize(tensor)); |
| 92 | + } else if (tensor->dtype.code == DLDataTypeCode::kDLFloat && tensor->dtype.bits == 64) { |
| 93 | + CURandGenerator().Generate64bit(tensor->data, GetTensorSize(tensor)); |
| 94 | + } else { |
| 95 | + LOG(FATAL) << "ValueError: Unsupported dtype: " << tensor->dtype; |
| 96 | + } |
| 97 | + TVMSynchronize(tensor->device.device_type, tensor->device.device_type, nullptr); |
| 98 | +} |
| 99 | + |
| 100 | +TVM_REGISTER_GLOBAL("runtime.contrib.curand.RandomFill").set_body_typed(RandomFill); |
| 101 | + |
| 102 | +} // namespace curand |
| 103 | +} // namespace runtime |
| 104 | +} // namespace tvm |
0 commit comments