-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathReactantStructArraysExt.jl
More file actions
168 lines (144 loc) · 5.12 KB
/
ReactantStructArraysExt.jl
File metadata and controls
168 lines (144 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
module ReactantStructArraysExt
using Reactant: Reactant
using StructArrays: StructArrays
import StructArrays:
StructArrayStyle,
StructArray,
index_type,
components,
createinstance,
get_ith,
maybe_convert_elt,
foreachfield
import Reactant:
TraceMode,
TracedToTypes,
traced_type_inner,
append_path,
make_tracer,
traced_type,
ReactantPrimitive,
broadcast_to_size,
TracedRNumber,
TracedRArray,
unwrapped_eltype
import Reactant.TracedRArrayOverrides: AbstractReactantArrayStyle, _copy
import Base.Broadcast: Broadcasted
function __init__()
Reactant.@skip_rewrite_func StructArrays.index_type
end
StructArrays.always_struct_broadcast(::AbstractReactantArrayStyle) = true
function Base.copy(
bc::Broadcasted{StructArrays.StructArrayStyle{S,N}}
) where {S<:AbstractReactantArrayStyle,N}
return _copy(bc)
end
function Reactant.broadcast_to_size(arg::StructArray{T}, rsize) where {T}
new = Tuple((broadcast_to_size(c, rsize) for c in components(arg)))
return StructArray{T}(new)
end
function Base.copyto!(
dest::StructArray, bc::Base.Broadcast.Broadcasted{<:AbstractReactantArrayStyle}
)
axes(dest) == axes(bc) || Broadcast.throwdm(axes(dest), axes(bc))
isempty(dest) && return dest
bc = Broadcast.preprocess(dest, bc)
args = (Reactant.broadcast_to_size(Base.materialize(a), size(bc)) for a in bc.args)
res = Reactant.TracedUtils.elem_apply_via_while_loop(bc.f, args...)
copyto!(dest, res)
return dest
end
function Reactant.TracedRArrayOverrides._copyto!(
dest::StructArray, bc::Base.Broadcast.Broadcasted{<:AbstractReactantArrayStyle}
)
return copyto!(dest, bc)
end
function Base.copyto!(
dest::Reactant.TracedRArray, bc::Broadcasted{StructArrayStyle{S,N}}
) where {S<:AbstractReactantArrayStyle,N}
axes(dest) == axes(bc) || Broadcast.throwdm(axes(dest), axes(bc))
isempty(dest) && return dest
bc = Broadcast.preprocess(dest, bc)
args = (Reactant.broadcast_to_size(Base.materialize(a), size(bc)) for a in bc.args)
res = Reactant.TracedUtils.elem_apply_via_while_loop(bc.f, args...)
return copyto!(dest, res)
end
function alloc_sarr(bc, T)
# Short circuit for Complex since in Reactant they are just a regular number
T <: Complex && return similar(bc, T)
if StructArrays.isnonemptystructtype(T)
return StructArrays.buildfromschema(x -> alloc_sarr(bc, x), T)
else
return similar(bc, T)
end
end
function Base.similar(
bc::Broadcasted{StructArrayStyle{S,N}}, ::Type{ElType}
) where {S<:AbstractReactantArrayStyle,N,ElType}
bc′ = convert(Broadcasted{S}, bc)
# It is possible that we have multiple broadcasted arguments
return alloc_sarr(bc′, ElType)
end
Base.@propagate_inbounds function StructArrays._getindex(
x::StructArray{T}, I::Vararg{TracedRNumber{<:Integer}}
) where {T}
cols = components(x)
@boundscheck checkbounds(x, I...)
return createinstance(T, get_ith(cols, I...)...)
end
setstruct(col, val, I) = @inbounds Reactant.@allowscalar col[I] = val
struct SetStruct{T}
I::T
end
(s::SetStruct)(col, val) = setstruct(col, val, s.I)
(s::SetStruct)(vals) = s(vals...)
Base.@propagate_inbounds function Base.setindex!(
s::StructArray{T,<:Any,<:Any,Int}, vals, I::TracedRNumber{TI}
) where {T,TI<:Integer}
valsT = maybe_convert_elt(T, vals)
setter = SetStruct(I)
foreachfield(setter, s, valsT)
return s
end
const MRarr = Union{Reactant.AnyTracedRArray,Reactant.RArray}
getstruct(col, n, I) = @inbounds Reactant.@allowscalar col[n][I...]
struct GetStruct{C,Idx}
cols::C
I::Idx
end
(g::GetStruct)(n) = getstruct(g.cols, n, g.I...)
function StructArrays.get_ith(cols::NamedTuple{N,<:NTuple{K,<:MRarr}}, I...) where {N,K}
getter = GetStruct(cols, I)
ith = ntuple(getter, Val(K))
return ith
end
function StructArrays.get_ith(cols::NTuple{K,<:MRarr}, I...) where {K}
getter = GetStruct(cols, I)
ith = ntuple(getter, Val(K))
return ith
end
Base.@nospecializeinfer function Reactant.traced_type_inner(
@nospecialize(prev::Type{StructArray{ET,N,C,I}}),
seen,
@nospecialize(mode::TraceMode),
@nospecialize(track_numbers::Type),
@nospecialize(sharding),
@nospecialize(runtime)
) where {ET,N,C,I}
ET_traced = traced_type_inner(
ET, seen, mode, Union{ReactantPrimitive,track_numbers}, sharding, runtime
)
C_traced = traced_type_inner(C, seen, mode, track_numbers, sharding, runtime)
return StructArray{ET_traced,N,C_traced,index_type(fieldtypes(C_traced))}
end
@inline function Reactant.traced_getfield(@nospecialize(obj::StructArray), field)
return Base.getfield(obj, field)
end
# This is to tell StructArrays to leave these array types alone.
StructArrays.staticschema(::Type{<:Reactant.AnyTracedRArray}) = NamedTuple{()}
StructArrays.staticschema(::Type{<:Reactant.RArray}) = NamedTuple{()}
StructArrays.staticschema(::Type{<:Reactant.RNumber}) = NamedTuple{()}
# # Even though RNumbers we have fields we want them to be threated as empty structs
StructArrays.isnonemptystructtype(::Type{<:Reactant.RNumber}) = false
StructArrays.isnonemptystructtype(::Type{<:Reactant.TracedRArray}) = false
end