Skip to content

Commit 3635c74

Browse files
authored
[mlir][gpu][sparse] gracefully accept zero size allocation (#66127)
This cleans up a unnecessary code that changes zero size allocation to avoid the following error message 'cuMemAlloc(&ptr, sizeBytes)' failed with 'CUDA_ERROR_INVALID_VALUE'
1 parent cff72d7 commit 3635c74

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ extern MLIR_CUDA_WRAPPERS_EXPORT "C" void mgpuEventRecord(CUevent event,
212212

213213
extern "C" void *mgpuMemAlloc(uint64_t sizeBytes, CUstream /*stream*/) {
214214
ScopedContext scopedContext;
215-
CUdeviceptr ptr;
216-
CUDA_REPORT_IF_ERROR(cuMemAlloc(&ptr, sizeBytes));
215+
CUdeviceptr ptr = 0;
216+
if (sizeBytes != 0)
217+
CUDA_REPORT_IF_ERROR(cuMemAlloc(&ptr, sizeBytes));
217218
return reinterpret_cast<void *>(ptr);
218219
}
219220

@@ -521,7 +522,7 @@ extern "C" MLIR_CUDA_WRAPPERS_EXPORT intptr_t mgpuSpMVBufferSize(
521522
CUSPARSE_REPORT_IF_ERROR(cusparseSpMV_bufferSize(
522523
cusparse_env, modeA, alphap, matA, vecX, betap, vecY, cTp,
523524
CUSPARSE_SPMV_ALG_DEFAULT, &bufferSize))
524-
return bufferSize == 0 ? 1 : bufferSize; // avoid zero-alloc
525+
return bufferSize;
525526
}
526527

527528
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void mgpuSpMV(int32_t ma, void *a, void *x,
@@ -555,7 +556,7 @@ mgpuSpMMBufferSize(int32_t ma, int32_t mb, void *a, void *b, void *c,
555556
CUSPARSE_REPORT_IF_ERROR(cusparseSpMM_bufferSize(
556557
cusparse_env, modeA, modeB, alphap, matA, matB, betap, matC, cTp,
557558
CUSPARSE_SPMM_ALG_DEFAULT, &bufferSize))
558-
return bufferSize == 0 ? 1 : bufferSize; // avoid zero-alloc
559+
return bufferSize;
559560
}
560561

561562
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void mgpuSpMM(int32_t ma, int32_t mb,
@@ -590,7 +591,7 @@ mgpuSDDMMBufferSize(int32_t ma, int32_t mb, void *a, void *b, void *c,
590591
CUSPARSE_REPORT_IF_ERROR(cusparseSDDMM_bufferSize(
591592
cusparse_env, modeA, modeB, alphap, matA, matB, betap, matC, cTp,
592593
CUSPARSE_SDDMM_ALG_DEFAULT, &bufferSize))
593-
return bufferSize == 0 ? 1 : bufferSize; // avoid zero-alloc
594+
return bufferSize;
594595
}
595596

596597
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void mgpuSDDMM(int32_t ma, int32_t mb,
@@ -638,7 +639,7 @@ extern "C" MLIR_CUDA_WRAPPERS_EXPORT intptr_t mgpuSpGEMMWorkEstimation(
638639
CUSPARSE_REPORT_IF_ERROR(cusparseSpGEMM_workEstimation(
639640
cusparse_env, modeA, modeB, alphap, matA, matB, betap, matC, cTp,
640641
CUSPARSE_SPGEMM_DEFAULT, spgemmDesc, &newBufferSize, buf))
641-
return newBufferSize == 0 ? 1 : newBufferSize; // avoid zero-alloc
642+
return newBufferSize;
642643
}
643644

644645
extern "C" MLIR_CUDA_WRAPPERS_EXPORT intptr_t
@@ -656,7 +657,7 @@ mgpuSpGEMMCompute(void *s, int32_t ma, int32_t mb, void *a, void *b, void *c,
656657
CUSPARSE_REPORT_IF_ERROR(cusparseSpGEMM_compute(
657658
cusparse_env, modeA, modeB, alphap, matA, matB, betap, matC, cTp,
658659
CUSPARSE_SPGEMM_DEFAULT, spgemmDesc, &newBufferSize2, buf2))
659-
return newBufferSize2 == 0 ? 1 : newBufferSize2; // avoid zero-alloc
660+
return newBufferSize2;
660661
}
661662

662663
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void
@@ -794,7 +795,6 @@ mgpuCuSparseLtSpMMBufferSize(void *bs, int32_t ma, int32_t mb, void *a, void *b,
794795
auto workspace_size = reinterpret_cast<int64_t *>(bs);
795796
auto compressed_size = &(reinterpret_cast<int64_t *>(bs)[1]);
796797
auto compressed_buffer_size = &(reinterpret_cast<int64_t *>(bs)[2]);
797-
size_t workspace_size_, compressed_size_, compressed_buffer_size_;
798798
auto cTp = static_cast<cusparseComputeType>(ctp);
799799

800800
cusparseOperation_t modeA = static_cast<cusparseOperation_t>(ma);
@@ -836,16 +836,9 @@ mgpuCuSparseLtSpMMBufferSize(void *bs, int32_t ma, int32_t mb, void *a, void *b,
836836
}
837837

838838
CUSPARSE_REPORT_IF_ERROR(cusparseLtMatmulGetWorkspace(
839-
&cusparseLt_env, &(matA->plan), &workspace_size_))
839+
&cusparseLt_env, &(matA->plan), workspace_size))
840840
CUSPARSE_REPORT_IF_ERROR(cusparseLtSpMMACompressedSize(
841-
&cusparseLt_env, &(matA->plan), &compressed_size_,
842-
&compressed_buffer_size_))
843-
844-
// Avoid zero-allocation.
845-
*workspace_size = (workspace_size_ == 0 ? 1 : workspace_size_);
846-
*compressed_size = (compressed_size_ == 0 ? 1 : compressed_size_);
847-
*compressed_buffer_size =
848-
(compressed_buffer_size_ == 0 ? 1 : compressed_buffer_size_);
841+
&cusparseLt_env, &(matA->plan), compressed_size, compressed_buffer_size))
849842
}
850843

851844
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void

0 commit comments

Comments
 (0)