1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| Backend::MemObj* OpenCLBackend::onAcquire(const Tensor* nativeTensor, StorageType storageType) { #ifdef LOG_VERBOSE MNN_PRINT("Start OpenCLBackend::onAcquireBuffer !\n"); #endif
auto tensorShape = OpenCL::tensorShapeFormat(nativeTensor); int N = tensorShape.at(0); int H = tensorShape.at(1); int W = tensorShape.at(2); int C = tensorShape.at(3);
#ifdef LOG_VERBOSE MNN_PRINT("OpenCLBackend::onAcquireBuffer: NHWC:[%d, %d, %d, %d]\n", N, H, W, C); #endif
#ifndef MNN_OPENCL_BUFFER_CLOSED if(mOpenCLRuntime->getGpuMemType() == BUFFER) { size_t size; if (nativeTensor->dimensions() >= 2) { auto alignC = ROUND_UP(C, 8); auto hR = ROUND_UP(H + 3, 4) - H; auto wR = ROUND_UP(W + 3, 4) - W; size = N * alignC * W * H; size = size + hR * W * 4 + wR * 4; } else { size = nativeTensor->elementSize(); size = ROUND_UP(size, 4); }
if (mOpenCLRuntime->isSupportedIntelSubgroup()) { int cPack = TensorUtils::getTensorChannelPack(nativeTensor); auto pads = TensorUtils::getDescribe(nativeTensor)->mPads; size_t imageWidth = (size_t) ROUND_UP(UP_DIV(C, cPack), 2) * ROUND_UP(pads.left + W + pads.right, 4); size_t imageHeight = (size_t)N * H; size = imageWidth*imageHeight*cPack; } cl_channel_type dataType = CL_FLOAT; if (getOpenCLRuntime()->isSupportedFP16()) { dataType = CL_HALF_FLOAT; }
if (storageType == DYNAMIC_SEPERATE) { auto buffer = mBufferPool->alloc(size* (dataType==CL_HALF_FLOAT?sizeof(half_float::half):sizeof(float)), true); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)buffer; return new CLMemReleaseBuffer(buffer, mBufferPool.get()); } if (storageType == DYNAMIC) { auto buffer = mBufferPool->alloc(size* (dataType==CL_HALF_FLOAT?sizeof(half_float::half):sizeof(float))); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)buffer; return new CLMemReleaseBuffer(buffer, mBufferPool.get()); } MNN_ASSERT(storageType == STATIC); #ifdef MNN_LOW_MEMORY if ((nativeTensor->getType().code == halide_type_int) && (nativeTensor->getType().bits == 8 || nativeTensor->getType().bits == 4)) { size_t alloc_size = size; if (nativeTensor->getType().bits == 4) { alloc_size = size / 2; } auto buffer = mStaticBufferPool->alloc(alloc_size); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)buffer; return new CLMemReleaseBuffer(buffer, mStaticBufferPool.get()); } #endif auto buffer = mStaticBufferPool->alloc(size* (dataType == CL_HALF_FLOAT ? sizeof(half_float::half) : sizeof(float))); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)buffer; return new CLMemReleaseBuffer(buffer, mStaticBufferPool.get()); } else #endif { size_t imageWidth = (size_t) (UP_DIV(C, 4) * W); size_t imageHeight = (size_t)N * H; cl_channel_type dataType = CL_HALF_FLOAT; if (mPrecision == BackendConfig::Precision_High) { dataType = CL_FLOAT; }
if (storageType == DYNAMIC_SEPERATE) { auto image = mImagePool->alloc(imageWidth, imageHeight, dataType, true); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)image; return new CLMemReleaseImage(image, mImagePool.get()); } if (storageType == DYNAMIC) { auto image = mImagePool->alloc(imageWidth, imageHeight, dataType); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)image; return new CLMemReleaseImage(image, mImagePool.get()); } MNN_ASSERT(storageType == STATIC); auto image = mStaticImagePool->alloc(imageWidth, imageHeight, dataType); ((Tensor*)nativeTensor)->buffer().device = (uint64_t)image; return new CLMemReleaseImage(image, mStaticImagePool.get()); } }
void* OpenCLBackend::onMapTensor(Tensor::MapType mtype, Tensor::DimensionType dtype, const Tensor* srcTensor) { auto needSize = srcTensor->size(); clearRecord(); #ifdef MNN_OPENCL_SVM_ENABLE auto svm_cap_ = mOpenCLRuntime->getSvmCapabilities(); bool use_svm = (svm_cap_ & CL_DEVICE_SVM_FINE_GRAIN_BUFFER); use_svm |= ((svm_cap_ & CL_DEVICE_SVM_COARSE_GRAIN_BUFFER) && mOpenCLRuntime->getGpuType() == ADRENO);
mUseSvm = (mOpenCLRuntime->getCLVersion() > 1.99f && use_svm); if(mUseSvm) { svmPtr = allocMapTensorMemory(needSize, true, svm_cap_);
if(mtype == Tensor::MAP_TENSOR_READ) { MNN::Tensor tmpTensor(srcTensor, dtype, false); tmpTensor.buffer().device = (uint64_t)svmPtr;
MNN_DATA_FORMAT format_type = MNN_DATA_FORMAT_NCHW; if(dtype == MNN::Tensor::TENSORFLOW) { format_type = MNN_DATA_FORMAT_NHWC; } else if(dtype == MNN::Tensor::CAFFE_C4) { format_type = MNN_DATA_FORMAT_NC4HW4; } mCLRuntime->convertFromDevice(srcTensor, &tmpTensor, format_type, true); }
if(svm_cap_ & CL_DEVICE_SVM_FINE_GRAIN_BUFFER) { mOpenCLRuntime->commandQueue().finish(); return svmPtr; }
auto map_flag = CL_MAP_WRITE; if(mtype == Tensor::MAP_TENSOR_READ) { map_flag = CL_MAP_READ; }
cl_int res = clEnqueueSVMMap(mOpenCLRuntime->commandQueue().get(), true, map_flag, svmPtr, needSize, 0, nullptr, nullptr);
MNN_CHECK_CL_SUCCESS(res, "svm_map") return svmPtr; } #endif
svmPtr = allocMapTensorMemory(needSize, false);
if(mtype == Tensor::MAP_TENSOR_READ) { MNN::Tensor tmpTensor(srcTensor, dtype, false); tmpTensor.buffer().host = (uint8_t *)svmPtr;
onCopyBuffer(srcTensor, &tmpTensor); } return svmPtr; }
bool OpenCLBackend::onUnmapTensor(Tensor::MapType mtype, Tensor::DimensionType dtype, const Tensor* dstTensor, void* mapPtr) { #ifdef MNN_OPENCL_SVM_ENABLE auto svm_cap_ = mOpenCLRuntime->getSvmCapabilities(); if(mUseSvm) {
if(!(svm_cap_ & CL_DEVICE_SVM_FINE_GRAIN_BUFFER)) { cl_int res = clEnqueueSVMUnmap(mOpenCLRuntime->commandQueue().get(), svmPtr, 0, nullptr, nullptr); MNN_CHECK_CL_SUCCESS(res, "svm_unmap") }
if(mtype == Tensor::MAP_TENSOR_WRITE) { MNN::Tensor interTensor(dstTensor, dtype, false); interTensor.buffer().device = (uint64_t)svmPtr;
MNN_DATA_FORMAT format_type = MNN_DATA_FORMAT_NCHW; if(dtype == MNN::Tensor::TENSORFLOW) { format_type = MNN_DATA_FORMAT_NHWC; } else if(dtype == MNN::Tensor::CAFFE_C4) { format_type = MNN_DATA_FORMAT_NC4HW4; } mCLRuntime->convertToDevice(&interTensor, dstTensor, format_type, true); } mOpenCLRuntime->commandQueue().finish();
return true; } #endif
if(mtype == Tensor::MAP_TENSOR_WRITE) { MNN::Tensor srcTensor(dstTensor, dtype, false); srcTensor.buffer().host = (uint8_t *)svmPtr;
onCopyBuffer(&srcTensor, dstTensor); } return true; }
void ConvBufExecution::setConv1x1WeightBuffer(int packCout, int packCin, const float* filterDataPtr) { cl_int res; std::shared_ptr<Tensor> filterBuffer(Tensor::createDevice<float>({ROUND_UP(mOutputChannel, 8), ROUND_UP(mInputChannel, packCin), mKernelWidth, mKernelHeight})); int buffer_size = filterBuffer->elementSize(); if(mOpenCLBackend->getOpenCLRuntime()->isSupportedFP16()) { buffer_size *= sizeof(half_float::half); } else { buffer_size *= sizeof(float); } mKernelBuffer.reset(new cl::Buffer(mOpenCLBackend->getOpenCLRuntime()->context(), CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, buffer_size)); auto kernelBufferPtr = mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(*(mKernelBuffer.get()), true, CL_MAP_WRITE, 0, buffer_size, nullptr, nullptr, &res); if(kernelBufferPtr != nullptr && res == CL_SUCCESS){ ::memset(kernelBufferPtr, 0, buffer_size); for(int o = 0; o < mOutputChannel; o++){ for(int i = 0 ; i < mInputChannel; i++){ int bufferIdx = (o/packCout) * ROUND_UP(mInputChannel, packCin)*packCout + (i/packCin)*packCin*packCout + (o%packCout)*packCin + (i%packCin); int filterIdx = o*mInputChannel + i; if(mOpenCLBackend->getOpenCLRuntime()->isSupportedFP16()){ ((half_float::half*)kernelBufferPtr)[bufferIdx] = (half_float::half)(filterDataPtr[filterIdx]); }else{ ((float*)kernelBufferPtr)[bufferIdx] = (float)(filterDataPtr[filterIdx]); } } } }else{ MNN_ERROR("Map error ptrCL == nullptr \n"); MNN_ASSERT(false); } mOpenCLBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(*(mKernelBuffer.get()), kernelBufferPtr); }
|