Using THCudaBlas_Sgemm with torch::Tensor?

cmauceri: <p>I’m updating an old cuda extention. The old code used THCudaTensor and THCudaBlas_Sgemm. The updated code uses torch::Tensor, but I’m not sure how to correspondingly update THCudaBlas_Sgemm.</p>
<p>Original call</p>
<pre><code class="lang-auto">THCudaBlas_Sgemm(state, 'n', 'n', n, m, k, 1.0f,
                     THCudaTensor_data(state, columns), n,
                     THCudaTensor_data(state, weight), k, 1.0f,
                     THCudaTensor_data(state, output_n), n);
</code></pre>
<p>I’ve tried</p>
<pre><code class="lang-auto">THCudaBlas_Sgemm(state, 'n', 'n', n, m, k, 1.0f,
                     columns.data(), n,
                     weight.data(), k, 1.0f,
                     output_n.data(), n);
</code></pre>
<p>But I get the error</p>
<blockquote>
<p>error: cannot convert ‘at::Tensor’ to ‘float*’ for argument ‘8’ to ‘void THCudaBlas_Sgemm(THCState*, char, char, int64_t, int64_t, int64_t, float, float*, int64_t, float*, int64_t, float, float*, int64_t)’</p>
</blockquote>
<p>I’ve also thought about how to convert this call to torch::addmm, but the leading dimension arguments in sgemm make this a complicated conversion since there isn’t an equivalent argument to addmm.</p>

ptrblck: <p><code>THCTensor_</code> might work, but wouldn’t it be easier to use <code>torch.matmul</code> directly?</p>

cmauceri: <p>Does torch.matmul have arguments similar to LDA (leading dimension A) and LDB (leading dimension B)?</p>
<p>I’ve been working out some slicing solutions to get the same result, but this old code is poorly documented and I’d like to stay as close as possible to the original to avoid introducing bugs where I don’t understand the original intent.</p>

ptrblck: <p>No, you would have to pass the transposed matrix if you would like to change the leading dimensions.</p>

JamesDickens: <p><a class="mention" href="/u/cmauceri">@cmauceri</a> <a class="mention" href="/u/ptrblck">@ptrblck</a> do you have a link to any documentation for THCudaBlas_Sgemm, I cannot find it. I notice this is code from the Depth Aware Segementation for RGBD images paper, I too am trying to port this to pytorch 1.7 and write my own c++ extension and it is difficult to find documentation for the older functions used.</p>

ptrblck: <p>This method dispatches to <a href="https://github.com/pytorch/pytorch/blob/1cc1da541158b53c24a579fd316d190d5c27bfd1/aten/src/ATen/cuda/CUDABlas.cpp"><code>at::cuda::blas::gemm</code></a> and calls cublas routines such as <a href="https://github.com/pytorch/pytorch/blob/1cc1da541158b53c24a579fd316d190d5c27bfd1/aten/src/ATen/cuda/CUDABlas.cpp#L251-L270"><code>cublasGemmEx</code></a> (<a href="https://docs.nvidia.com/cuda/cublas/index.html#cublas-GemmEx">docs</a>). I think it might be easier and cleaner to use <code>torch::</code> methods instead of calling into these backends directly, but I’m also unfamiliar with the implementation.</p>

Looottch: <p>I’m also doing that part now, I directy use torch:: methods but I’m not sure will that effect the final results. May I ask have you get the exact same result with older version.</p>

cmauceri: <p><a class="mention" href="/u/jamesdickens">@JamesDickens</a> <a class="mention" href="/u/looottch">@Looottch</a> I have a completed port of DepthAwareCNN at <a href="https://github.com/crmauceri/DepthAwareCNN-pytorch1.5" class="inline-onebox" rel="noopener nofollow ugc">GitHub - crmauceri/DepthAwareCNN-pytorch1.5: Depth-aware CNN for RGB-D Segmentation, ECCV 2018</a></p>

Looottch: <p>Thanks, I go through <strong>depthconv_cuda.cpp</strong> and not find function like “depthconv_backward_parameters_cuda”, you not convert that func or I miss it somewhere.</p>

cmauceri: <p>I tried to do a one-to-one conversion, but in the end, I did a re-implementation instead based on the paper. Therefore some of the functions might be different.</p>