This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 83
Gamma family function support #321
Open
xukai92
wants to merge
4
commits into
JuliaGPU:master
Choose a base branch
from
xukai92:kx/cugammafuns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,63 @@ | ||
# This file is heavlily adopted from https://github.com/JuliaMath/SpecialFunctions.jl. | ||
# License is MIT: http://julialang.org/license | ||
|
||
function lgamma(x) | ||
return CUDAnative.lgamma(x) | ||
end | ||
|
||
function digamma(x) | ||
if x <= 0 # reflection formula | ||
ψ = -π / CUDAnative.tan(π * x) | ||
x = 1 - x | ||
else | ||
ψ = zero(x) | ||
end | ||
if x < 7 | ||
# shift using recurrence formula | ||
ν = one(x) | ||
n = 7 - CUDAnative.floor(x) | ||
while ν <= n - 1 | ||
ψ -= inv(x + ν) | ||
ν += one(x) | ||
end | ||
ψ -= inv(x) | ||
x += n | ||
end | ||
t = inv(x) | ||
ψ += CUDAnative.log(x) - 0.5 * t | ||
t *= t # 1/z^2 | ||
# the coefficients here are Float64(bernoulli[2:9] .// (2*(1:8))) | ||
ψ -= t * @evalpoly(t,0.08333333333333333,-0.008333333333333333,0.003968253968253968,-0.004166666666666667,0.007575757575757576,-0.021092796092796094,0.08333333333333333,-0.4432598039215686) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, this will convert into |
||
return ψ | ||
end | ||
|
||
function _trigamma(x) | ||
ψ = zero(x) | ||
if x < 8 | ||
# shift using recurrence formula | ||
n = 8 - CUDAnative.floor(x) | ||
ψ += inv(x)^2 | ||
ν = one(x) | ||
while ν <= n - 1 | ||
ψ += inv(x + ν)^2 | ||
ν += one(x) | ||
end | ||
x += n | ||
end | ||
t = inv(x) | ||
w = t * t # 1/z^2 | ||
ψ += t + 0.5 * w | ||
# the coefficients here are Float64(bernoulli[2:9]) | ||
ψ += t * w * @evalpoly(w,0.16666666666666666,-0.03333333333333333,0.023809523809523808,-0.03333333333333333,0.07575757575757576,-0.2531135531135531,1.1666666666666667,-7.092156862745098) | ||
return ψ | ||
end | ||
|
||
function trigamma(x) | ||
if x <= 0 # reflection formula | ||
return (π / CUDAnative.sin(π * x))^2 - _trigamma(1 - x) | ||
else | ||
return _trigamma(x) | ||
end | ||
end | ||
|
||
lbeta(x, y) = lgamma(x) + lgamma(y) - lgamma(x + y) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using Test | ||
import SpecialFunctions | ||
using Flux: Tracker | ||
using CuArrays | ||
|
||
n = 1000 | ||
|
||
xs_lgamma = randn(Float32, n); xs_lgamma_cu = cu(xs_lgamma) | ||
xs_digamma = randn(Float32, n); xs_digamma_cu = cu(xs_digamma) | ||
xs_trigamma = randn(Float32, n); xs_trigamma_cu = cu(xs_trigamma) | ||
xs_lbeta_tuple = (randn(Float32, n), randn(Float32, n)) | ||
xs_lbeta_tuple = map(xs -> abs.(xs), xs_lbeta_tuple); xs_lbeta_cu_tuple = map(cu, xs_lbeta_tuple) | ||
|
||
catgrads(grads) = cat(map(ta -> ta.data, grads)...; dims=1) | ||
g∑fx(f, xs) = catgrads(Tracker.gradient(_xs -> sum(f.(_xs)), xs)) | ||
g∑fx(f, xs, ys) = catgrads(Tracker.gradient((_xs, _ys) -> sum(f.(_xs, _ys)), xs, ys)) | ||
|
||
results = Dict() | ||
@testset "Forward evaluation" begin | ||
fn = :lgamma | ||
@testset "$fn" begin | ||
lgamma_val_cpu = @time SpecialFunctions.lgamma.(xs_lgamma) | ||
lgamma_val_gpu = @time CuArrays.lgamma.(xs_lgamma_cu) | ||
lgamma_val_gpu = Array(lgamma_val_gpu) | ||
for i = 1:n | ||
@test lgamma_val_cpu[i] ≈ lgamma_val_gpu[i] | ||
end | ||
results[fn] = (lgamma_val_cpu, lgamma_val_gpu) | ||
end | ||
|
||
fn = :digamma | ||
@testset "$fn" begin | ||
digamma_val_cpu = @time SpecialFunctions.digamma.(xs_digamma) | ||
digamma_val_gpu = @time CuArrays.digamma.(xs_digamma_cu) | ||
digamma_val_gpu = Array(digamma_val_gpu) | ||
for i = 1:n | ||
@test digamma_val_cpu[i] ≈ digamma_val_gpu[i] | ||
end | ||
results[fn] = (digamma_val_cpu, digamma_val_gpu) | ||
end | ||
|
||
fn = :trigamma | ||
@testset "$fn" begin | ||
trigamma_val_cpu = @time SpecialFunctions.trigamma.(xs_trigamma) | ||
trigamma_val_gpu = @time CuArrays.trigamma.(xs_trigamma_cu) | ||
trigamma_val_gpu = Array(trigamma_val_gpu) | ||
for i = 1:n | ||
@test trigamma_val_cpu[i] ≈ trigamma_val_gpu[i] | ||
end | ||
results[fn] = (trigamma_val_cpu, trigamma_val_gpu) | ||
end | ||
|
||
fn = :lbeta | ||
@testset "$fn" begin | ||
lbeta_val_cpu = @time SpecialFunctions.lbeta.(xs_lbeta_tuple...) | ||
lbeta_val_gpu = @time CuArrays.lbeta.(xs_lbeta_cu_tuple...) | ||
lbeta_val_gpu = Array(lbeta_val_gpu) | ||
for i = 1:n | ||
@test lbeta_val_cpu[i] ≈ lbeta_val_gpu[i] | ||
end | ||
results[fn] = (lbeta_val_cpu, lbeta_val_gpu) | ||
end | ||
|
||
end | ||
|
||
@testset "Gradient evaluation" begin | ||
fn = :lgamma | ||
@testset "$fn" begin | ||
lgamma_grad_cpu = @time g∑fx(SpecialFunctions.lgamma, xs_lgamma) | ||
lgamma_grad_gpu = @time g∑fx(CuArrays.lgamma, xs_lgamma_cu) | ||
lgamma_grad_gpu = Array(lgamma_grad_gpu) | ||
for i = 1:n | ||
@test lgamma_grad_cpu[i] ≈ lgamma_grad_gpu[i] | ||
end | ||
end | ||
|
||
fn = :digamma | ||
@testset "$fn" begin | ||
digamma_grad_cpu = @time g∑fx(SpecialFunctions.digamma, xs_digamma) | ||
digamma_grad_gpu = @time g∑fx(CuArrays.digamma, xs_digamma_cu) | ||
digamma_grad_gpu = Array(digamma_grad_gpu) | ||
for i = 1:n | ||
@test digamma_grad_cpu[i] ≈ digamma_grad_gpu[i] | ||
end | ||
end | ||
|
||
fn = :lbeta | ||
@testset "$fn" begin | ||
lbeta_grad_cpu = @time g∑fx(SpecialFunctions.lbeta, xs_lbeta_tuple...) | ||
lbeta_grad_gpu = @time g∑fx(CuArrays.lbeta, xs_lbeta_cu_tuple...) | ||
lbeta_grad_gpu = Array(lbeta_grad_gpu) | ||
for i = 1:n | ||
@test lbeta_grad_cpu[i] ≈ lbeta_grad_gpu[i] | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make it
special.jl
, no need for directories with single source files.