-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NDTensors] Fix contracting dense with diag on GPU (#1453)
- Loading branch information
Showing
12 changed files
with
114 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
NDTensors/ext/NDTensorsGPUArraysCoreExt/NDTensorsGPUArraysCoreExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module NDTensorsGPUArraysCoreExt | ||
include("contract.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Adapt: adapt | ||
using GPUArraysCore: AbstractGPUArray | ||
using NDTensors: NDTensors, DenseTensor, DiagTensor, contract!, dense, inds, Tensor | ||
using NDTensors.Expose: Exposed, expose, unexpose | ||
using NDTensors.TypeParameterAccessors: parenttype, set_ndims | ||
|
||
## In this function we convert the DiagTensor to a dense tensor and | ||
## Feed it back into contract | ||
function NDTensors.contract!( | ||
output_tensor::Exposed{<:AbstractGPUArray,<:DenseTensor}, | ||
labelsoutput_tensor, | ||
tensor1::Exposed{<:Number,<:DiagTensor}, | ||
labelstensor1, | ||
tensor2::Exposed{<:AbstractGPUArray,<:DenseTensor}, | ||
labelstensor2, | ||
α::Number=one(Bool), | ||
β::Number=zero(Bool), | ||
) | ||
tensor1 = unexpose(tensor1) | ||
## convert tensor1 to a dense | ||
## TODO this allocates on CPU first then moves over to GPU which could be slow | ||
tensor1 = adapt(set_ndims(parenttype(typeof(tensor2)), 1), dense(tensor1)) | ||
return contract!( | ||
output_tensor, | ||
labelsoutput_tensor, | ||
expose(tensor1), | ||
labelstensor1, | ||
tensor2, | ||
labelstensor2, | ||
α, | ||
β, | ||
) | ||
end | ||
|
||
function NDTensors.contract!( | ||
output_tensor::Exposed{<:AbstractGPUArray,<:DenseTensor}, | ||
labelsoutput_tensor, | ||
tensor1::Exposed{<:AbstractGPUArray,<:DenseTensor}, | ||
labelstensor1, | ||
tensor2::Exposed{<:Number,<:DiagTensor}, | ||
labelstensor2, | ||
α::Number=one(Bool), | ||
β::Number=zero(Bool), | ||
) | ||
return contract!( | ||
output_tensor, labelsoutput_tensor, tensor2, labelstensor2, tensor1, labelstensor1, α, β | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters