Skip to content

cublasZgemm{S/C}tridedBatched produces incorrect result for some non-contiguous cases when trans='c' #64103

Closed

Description

@anjali411

🐛 Bug

A=torch.ones(1,2,1, device="cuda", dtype=torch.complex128)
B=torch.ones(1,3,1, device="cuda", dtype=torch.complex128).transpose(1,2).conj()
print(torch.bmm(A,B))

expected to produce tensor of size (1,2,3) filled with ones (note, imaginary part of inputs is 0), yet produces

tensor([[[1.+0.j, 0.+0.j, 0.+0.j],
         [1.+0.j, 0.+0.j, 0.+0.j]]], device='cuda:0', dtype=torch.complex128)

when cublasZgemm{S/C}tridedBatched is called with trans='c' (i.e. un-materialized conjugation) (PR: #59380)

cc @ngimel @ezyang @anjali411 @dylanbespalko @mruberry @lezcano @nikitaved @csarofeen @ptrblck @xwang233 @jianyuh @pearu @heitorschueroff @walterddr @IvanYashchuk

Activity

  1. added
    module: complex
    module: cuda
    module: dependency bug
    on Aug 27, 2021
  2. anjali411 commented on Aug 27, 2021

    @anjali411
    ContributorAuthor
  3. added
    module: linear algebra
    triaged
    on Aug 27, 2021
  4. added
    module: cublas
    on Aug 27, 2021
  5. xwang233 commented on Aug 28, 2021

    @xwang233
    Collaborator

    I wrote a short C++ standalone program to work on this, but I can't reproduce. I looked into PR #59380.

    at::cuda::blas::bgemm calls function here

    template <>
    void bgemm<c10::complex<double>>(CUDABLAS_BGEMM_ARGTYPES(c10::complex<double>)) {
    // See Note [Writing Nondeterministic Operations]
    globalContext().alertCuBLASConfigNotDeterministic();
    cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
    cublasOperation_t opa = _cublasOpFromChar(transa);
    cublasOperation_t opb = _cublasOpFromChar(transb);
    _cublasAdjustLdLevel3(transa, transb, m, n, k, &lda, &ldb, &ldc);
    BGEMM_CHECK_ARGVALUES(c10::complex<double>);
    TORCH_CUDABLAS_CHECK(cublasZgemmStridedBatched(
    handle, opa, opb, m, n, k, reinterpret_cast<const cuDoubleComplex*>(&alpha), reinterpret_cast<const cuDoubleComplex*>(a),
    lda, stridea, reinterpret_cast<const cuDoubleComplex*>(b), ldb, strideb, reinterpret_cast<const cuDoubleComplex*>(&beta),
    reinterpret_cast<cuDoubleComplex*>(c), ldc, stridec, num_batches));
    }

    where line 240 calls

    static void _cublasAdjustLdLevel3(
    char transa,
    char transb,
    int64_t m,
    int64_t n,
    int64_t k,
    int64_t* lda,
    int64_t* ldb,
    int64_t* ldc) {
    bool transa_ = ((transa == 't') || (transa == 'T'));
    bool transb_ = ((transb == 't') || (transb == 'T'));

    which also needs to be transposed for 'c' case.

    Note that lda, ldb needs to be changed if their matrices are transposed or conjugate transposed

  6. ngimel commented on Aug 28, 2021

    @ngimel
    Collaborator

    Ah, thanks for checking! I didn't think to look into _cublasAdjustLdLevel3, and the rest of the params seemed to be set up all right. Interesting how it's not failing with mm (even though it also calls into _cublasAdjustLdLevel3, and also has test cases with k=1)

  7. anjali411 commented on Aug 28, 2021

    @anjali411
    ContributorAuthor

    Ah, thanks for checking! I didn't think to look into _cublasAdjustLdLevel3, and the rest of the params seemed to be set up all right. Interesting how it's not failing with mm (even though it also calls into _cublasAdjustLdLevel3, and also has test cases with k=1)

    yeah same

  8. 7 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: complexmodule: cublasmodule: cudamodule: dependency bugmodule: linear algebratriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions