Skip to content

Commit

Permalink
using axes across codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmandlik committed Apr 25, 2024
1 parent 6b0aa60 commit 7adec40
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/aggregations/segmented_lse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ end

function segmented_lse_back(Δ, ::Missing, ψ, bags)
= zero(ψ)
@inbounds for (bi, b) in enumerate(bags)
@inbounds for bi in eachindex(bags)
for i in eachindex(ψ)
dψ[i] += Δ[i, bi]
end
Expand Down
13 changes: 5 additions & 8 deletions src/aggregations/segmented_max.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function segmented_max_forw(x::AbstractMatrix, ψ::AbstractVector, bags::Abstrac
end
else
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
y[i, bi] = max(y[i, bi], x[i, j])
end
end
Expand All @@ -67,20 +67,17 @@ function segmented_max_back(Δ, y, x, ψ, bags)
end
else
fi = first(b)
v .= @view x[:,fi]
# for k in axes(v,1)
# v[k] = x[k,fi]
# end
v .= @view x[:, fi]
idxs .= fi
for j in b
for i in axes(x,1)
for i in axes(x, 1)
if v[i] < x[i, j]
idxs[i] = j
v[i] = x[i, j]
end
end
end
for i in axes(x,1)
for i in axes(x, 1)
dx[i, idxs[i]] += Δ[i, bi]
end
end
Expand All @@ -90,7 +87,7 @@ end

function segmented_max_back(Δ, y, x::Missing, ψ, bags)
= zero(ψ)
@inbounds for (bi, b) in enumerate(bags)
@inbounds for bi in eachindex(bags)
for i in eachindex(ψ)
dψ[i] += Δ[i, bi]
end
Expand Down
6 changes: 3 additions & 3 deletions src/aggregations/segmented_mean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function segmented_mean_forw(x::AbstractMatrix, ψ::AbstractVector, bags::Abstra
end
else
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
y[i, bi] += _weight(w, i, j, t) * x[i, j]
end
end
Expand All @@ -68,7 +68,7 @@ function segmented_mean_back(Δ, y, x, ψ, bags, w)
else
ws = _bagnorm(w, b)
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
dx[i, j] += _weight(w, i, j, eltype(x)) * Δ[i, bi] / _weightsum(ws, i)
∇dw_segmented_mean!(dw, Δ, x, y, w, ws, i, j, bi)
end
Expand All @@ -80,7 +80,7 @@ end

function segmented_mean_back(Δ, y, x::Missing, ψ, bags, w)
= zero(ψ)
@inbounds for (bi, b) in enumerate(bags)
@inbounds for bi in eachindex(bags)
for i in eachindex(ψ)
dψ[i] += Δ[i, bi]
end
Expand Down
16 changes: 7 additions & 9 deletions src/aggregations/segmented_pnorm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function _pnorm_precomp(x::AbstractMatrix, bags)
@inbounds for (bi, b) in enumerate(bags)
if !isempty(b)
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
M[i, bi] = max(M[i, bi], abs(x[i, j]))
end
end
Expand All @@ -77,11 +77,11 @@ function _segmented_pnorm_norm(a::AbstractMatrix, ψ::AbstractVector, p::Abstrac
else
ws = _bagnorm(w, b)
for j in b
for i in 1:size(a, 1)
for i in axes(a, 1)
y[i, bi] += _weight(w, i, j, t) * abs(a[i, j] / M[i, bi]) ^ p[i]
end
end
for i in 1:size(a, 1)
for i in axes(a, 1)
y[i, bi] = M[i, bi] * (y[i, bi] / _weightsum(ws, i)) ^ (one(t) / p[i])
end
end
Expand All @@ -97,9 +97,7 @@ end

function segmented_pnorm_back(Δ, y, a, ψ, p, bags, w, M)
da = zero(a)
dp = zero(p)
dps1 = zero(p)
dps2 = zero(p)
dp, dps1, dps2 = zero(p), zero(p), zero(p)
= zero(ψ)
@inbounds for (bi, b) in enumerate(bags)
if isempty(b)
Expand All @@ -111,7 +109,7 @@ function segmented_pnorm_back(Δ, y, a, ψ, p, bags, w, M)
dps1 .= zero(eltype(p))
dps2 .= zero(eltype(p))
for j in b
for i in 1:size(a, 1)
for i in axes(a, 1)
ab = abs(a[i, j])
da[i, j] += Δ[i, bi] * _weight(w, i, j, eltype(p)) *
sign(a[i, j]) / _weightsum(ws, i) * (ab / y[i, bi]) ^ (p[i] - one(eltype(p)))
Expand All @@ -120,7 +118,7 @@ function segmented_pnorm_back(Δ, y, a, ψ, p, bags, w, M)
dps2[i] += ww
end
end
for i in 1:size(a, 1)
for i in axes(a, 1)
t = y[i, bi] / p[i]
t *= dps1[i] / dps2[i] - (p[i] * log(M[i, bi]) + log(dps2[i]) - log(_weightsum(ws, i))) / p[i]
dp[i] += Δ[i, bi] * t
Expand All @@ -132,7 +130,7 @@ end

function segmented_pnorm_back(Δ, y, ψ, bags)
= zero(ψ)
@inbounds for (bi, b) in enumerate(bags)
@inbounds for bi in eachindex(bags)
for i in eachindex(ψ)
dψ[i] += Δ[i, bi]
end
Expand Down
4 changes: 2 additions & 2 deletions src/aggregations/segmented_sum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function segmented_sum_forw(x::AbstractMatrix, ψ::AbstractVector, bags::Abstrac
end
else
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
y[i, bi] += _weight(w, i, j, t) * x[i, j]
end
end
Expand All @@ -66,7 +66,7 @@ function segmented_sum_back(Δ, y, x, ψ, bags, w)
end
else
for j in b
for i in 1:size(x, 1)
for i in axes(x, 1)
dx[i, j] += _weight(w, i, j, eltype(x)) * Δ[i, bi]
∇dw_segmented_sum!(dw, Δ, x, y, w, i, j, bi)
end
Expand Down
18 changes: 9 additions & 9 deletions src/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ _subsetof(bags) = bags[_getrange(length(bags))]
_convshift(n) = (i = div(n, 2); mod(n, 2) == 0 ? (1 - i:i) : (-i : i) )

function _addmatvec!(o::Matrix, i, W::Matrix, x::Matrix, j)
@inbounds for s in 1:size(W, 2)
for r in 1:size(W, 1)
@inbounds for s in axes(W, 2)
for r in axes(W, 1)
o[r, i] += W[r, s] * x[s, j]
end
end
Expand All @@ -25,23 +25,23 @@ function _addmatvec!(o::Matrix, i, W::Matrix, x::SparseMatrixCSC, j)
rowptr = x.rowval[rn]
vals = x.nzval[rn]
@inbounds for (s, v) in zip(rowptr, vals)
for r in 1:size(W, 1)
for r in axes(W, 1)
o[r, i] += W[r, s] * v
end
end
end

function _addmattvec!(o::Matrix, i, W::Matrix, x::AbstractMatrix, j)
@inbounds for s in 1:size(W, 1)
for r in 1:size(W, 2)
@inbounds for s in axes(W, 1)
for r in axes(W, 2)
o[r, i] += W[s, r] * x[s, j]
end
end
end

function _addvecvect!(W::Matrix, Δ::Matrix, i, x::AbstractMatrix, j)
@inbounds for r in 1:size(W, 2)
for s in 1:size(W, 1)
@inbounds for r in axes(W, 2)
for s in axes(W, 1)
W[s, r] += Δ[s, i] * x[r, j]
end
end
Expand All @@ -53,7 +53,7 @@ function _addvecvect!(W::Matrix, Δ::Matrix, i, x::SparseMatrixCSC, j)
rowptr = x.rowval[rn]
vals = x.nzval[rn]
@inbounds for (r, v) in zip(rowptr, vals)
for s in 1:size(W, 1)
for s in axes(W, 1)
W[s, r] += Δ[s, i] * v
end
end
Expand Down Expand Up @@ -257,4 +257,4 @@ function ChainRulesCore.rrule(::typeof(convsum), bags, xs...)
convsum(bags, xs...), Δ -> (NoTangent(), NoTangent(), ∇convsum(Δ, bags, length(xs))...)
end

legacy_bagconv(x, bags, f::AbstractArray{T, 3}) where {T} = convsum(bags, [f[:, :, i]*x for i in 1:size(f, 3)]...)
legacy_bagconv(x, bags, f::AbstractArray{T, 3}) where {T} = convsum(bags, [f[:, :, i]*x for i in axes(f, 3)]...)
8 changes: 4 additions & 4 deletions src/special_arrays/ngram_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ end
SparseArrays.SparseMatrixCSC(x::NGramMatrix) = SparseArrays.SparseMatrixCSC{Int64, UInt}(x)
function SparseArrays.SparseMatrixCSC{Tv, Ti}(x::NGramMatrix) where {Tv, Ti <: Integer}
size(x, 2) == 0 && return sparse(Ti[],Ti[],Tv[], size(x,1), size(x,2))
l = sum(map(i -> length(NGramIterator(x, i)), 1:size(x, 2)))
l = sum(map(i -> length(NGramIterator(x, i)), axes(x, 2)))
I = zeros(Ti, l)
J = zeros(Ti, l)
V = ones(Tv, l)
vid = 1
for j in 1:size(x, 2)
for j in axes(x, 2)
for i in NGramIterator(x, j)
I[vid] = i + 1
J[vid] = j
Expand Down Expand Up @@ -377,7 +377,7 @@ function _mul_ngram_vec!(s, A, B, bn, C, k, z, ψ=nothing)
for l in 1:_len(s, B.n)
z = mod(_next_ngram(z, l, s, B.n, B.b, bn), B.m)
zi = z + 1
for i in 1:size(C, 1)
for i in axes(C, 1)
@inbounds C[i, k] += A[i, zi]
end
end
Expand All @@ -403,7 +403,7 @@ function _∇A_mul_ngram_vec!(Δ, s, B, bn, ∇A, k, z)
for l in 1:_len(s, B.n)
z = mod(_next_ngram(z, l, s, B.n, B.b, bn), B.m)
zi = z + 1
for i in 1:size(∇A, 1)
for i in axes(∇A, 1)
@inbounds ∇A[i, zi] += Δ[i, k]
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/special_arrays/postimputing_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function _postimpute_maybe_hot(A, B)
@inbounds for (j, k) in enumerate(B.I)
if !ismissing(k)
m[j] = false
for i in 1:size(C, 1)
for i in axes(C, 1)
C[i, j] = A.W[i, k]
end
end
Expand All @@ -103,7 +103,7 @@ function ChainRulesCore.rrule(::typeof(_mul_pi_maybe_hot), A, B)
dW = zero(A.W)
@inbounds for (k, j) in enumerate(B.I)
if !ismissing(j)
for i in 1:size(dW, 1)
for i in axes(dW, 1)
dW[i, j] += Δ[i, k]
end
end
Expand Down

0 comments on commit 7adec40

Please sign in to comment.