 
             device_internals.cusolver_handle().Call(
                 geqrf_bufferSize,
-                m, n, a_ptr, lda, &lwork_geqrf);
+                m, n, r_ptr, lda, &lwork_geqrf);
 
             device_internals.cusolver_handle().Call(
                 orgqr_bufferSize,
-                m, n, n, a_ptr, lda, tau_ptr, &lwork_orgqr);
+                m, n, n, r_ptr, lda, tau_ptr, &lwork_orgqr);
 
             lwork = (lwork_geqrf > lwork_orgqr) ? lwork_geqrf : lwork_orgqr;
 


        CHAINERX_ASSERT(a.ndim() == 2);

        throw NotImplementedError("QR decomposition is not yet implemented for cuda device");

        if (mode != QRMode::reduced) {
            throw NotImplementedError{"Modes other than reduce are not implemented yet"};
        }

        int m = a.shape()[0];
        int n = a.shape()[1];
        int lda = std::min(m, n);

        Array Q = Empty(Shape({m, n}), dtype, device);
        Array R = Empty(Shape({n, n}), dtype, device);
        Array tau = Empty(Shape({n, 1}), dtype, device);

        auto qr_impl = [&](auto pt, auto geqrf_bufferSize, auto orgqr_bufferSize, auto geqrf, auto orgqr) {
            using T = typename decltype(pt)::type;
            cuda_internal::DeviceInternals& device_internals = cuda_internal::GetDeviceInternals(static_cast<CudaDevice&>(device));

            T* a_ptr = static_cast<T*>(internal::GetRawOffsetData(a));
            T* q_ptr = static_cast<T*>(internal::GetRawOffsetData(Q));
            T* r_ptr = static_cast<T*>(internal::GetRawOffsetData(R));
            T* tau_ptr = static_cast<T*>(internal::GetRawOffsetData(tau));


            device_internals.cusolver_handle().Call(
                geqrf,
                m, n, r_ptr, lda, tau_ptr, work_ptr, lwork, devInfo);

            int devInfo_h = 0;
            CheckCudaError(cudaMemcpy(&devInfo_h, devInfo, sizeof(int), cudaMemcpyDeviceToHost));
