Skip to content

CUTENSOR: axpy! and axpby! not mutating fixed - #1416

Merged
maleadt merged 3 commits into
JuliaGPU:masterfrom
yapanuwan:master
Mar 15, 2022
Merged

maleadt merged 3 commits into
JuliaGPU:masterfrom
yapanuwan:master

Conversation

@yapanuwan

@yapanuwan yapanuwan commented Feb 27, 2022 •

Copy link
Copy Markdown
Contributor

axpy! and axpby! routines are supposed to overwrite the input array (mutating / in-place operation) without allocating a new array in memory. From the official LinearAlgebra documentation,

axpy!(a, X, Y)
Overwrite Y with X*a + Y, where a is a scalar. Return Y.

This quick fix corrects the behavior as well as improving performance.

Luckily, according to NVIDIA's cuTENSOR documentation for cutensorElementwiseBinary(), the output tensor could be aliasing an input tensor.

Notice that D may alias any input tensor if they share the same memory layout (i.e., same tensor descriptor).

So the fix turned out be really easy.

Please be warned that I'm a newbie when it comes to open-source collaborations. An expert's opinion is highly appreciated. I cross-checked the outputs in my own application. It worked correctly, and the performance had improved dramatically!

axpy! and axpby! are supposed to overwrite the input (in-place multiplication) without allocating a new CuTensor in memory. This fixes the incorrect behavior as well as improving performance.
Creates a copy of input tensor before calling axpy! and axpby! and checks if the returned tensor points to the overwritten input.
@codecov

codecov Bot commented Feb 27, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #1416 (581256d) into master (7de4c45) will decrease coverage by 0.71%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1416      +/-   ##
==========================================
- Coverage   76.66%   75.94%   -0.72%     
==========================================
  Files         124      124              
  Lines        9246     9247       +1     
==========================================
- Hits         7088     7023      -65     
- Misses       2158     2224      +66     
Impacted Files Coverage Δ
lib/cutensor/interfaces.jl 100.00% <100.00%> (ø)
lib/cutensor/tensor.jl 73.33% <100.00%> (+1.90%) ⬆️
lib/cudnn/CUDNN.jl 38.46% <0.00%> (-35.39%) ⬇️
lib/cublas/CUBLAS.jl 49.56% <0.00%> (-25.22%) ⬇️
src/utilities.jl 67.12% <0.00%> (-4.11%) ⬇️
lib/cudadrv/CUDAdrv.jl 61.66% <0.00%> (-3.34%) ⬇️
lib/cudadrv/module/linker.jl 68.75% <0.00%> (-3.13%) ⬇️
lib/cudadrv/state.jl 79.09% <0.00%> (-2.26%) ⬇️
lib/cudadrv/memory.jl 78.59% <0.00%> (-1.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7de4c45...581256d. Read the comment docs.

@yapanuwan

Copy link
Copy Markdown
Contributor Author

UPDATE: Unit test cutensor/elementwise_binary passes with modifications to check for correct behavior.

@yapanuwan
yapanuwan marked this pull request as ready for review February 27, 2022 03:09
@yapanuwan yapanuwan changed the title In-place multiplication in CUTENSOR CUTENSOR: axpy! and axpby! not mutating fixed Mar 2, 2022
@maleadt maleadt added bugfix This gets something working again. cuda libraries Stuff about CUDA library wrappers. labels Mar 7, 2022

@maleadt maleadt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution, and sorry for the delayed response! LGTM, but I'd change the test to prevent needless copying and comparison.

Comment thread test/cutensor/elementwise_binary.jl Outdated
Comment thread test/cutensor/elementwise_binary.jl Outdated
Implement Base.copy() for CuTensors which duplicates the object with all its contents.
@yapanuwan
yapanuwan requested a review from maleadt March 15, 2022 03:41
@maleadt
maleadt merged commit f37805e into JuliaGPU:master Mar 15, 2022
simonbyrne pushed a commit to simonbyrne/CUDA.jl that referenced this pull request Nov 13, 2023
* In-place multiplication

axpy! and axpby! are supposed to overwrite the input (in-place multiplication) without allocating a new CuTensor in memory. This fixes the incorrect behavior as well as improving performance.

* Tests for in-place behavior

Creates a copy of input tensor before calling axpy! and axpby! and checks if the returned tensor points to the overwritten input.

* Refractor by implementing CuTensor copying

Implement Base.copy() for CuTensors which duplicates the object with all its contents.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This gets something working again. cuda libraries Stuff about CUDA library wrappers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants