forked from apache/mxnet
/
autodiff.jl
178 lines (160 loc) · 6.76 KB
/
autodiff.jl
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
169
170
171
172
173
174
175
176
177
178
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
grad(s::SymbolicNode, wrt::Vector{Symbol})
Get the autodiff gradient of the current `SymbolicNode`. This function can
only be used if the current symbol is a loss function.
# Arguments:
* `s::SymbolicNode`: current node.
* `wrt::Vector{Symbol}`: the names of the arguments to the gradient.
Returns a gradient symbol of the corresponding gradient.
"""
function grad(s::SymbolicNode, wrt::Vector{Symbol})
hdr_ref = Ref{MX_handle}(C_NULL)
keys = string.(key)
@mxcall(:MXSymbolGrad, (MX_handle, MX_uint, char_pp, Ptr{MX_handle}),
self, length(keys), keys, hdr_ref)
return SymbolicNode(MX_SymbolHandle(hdr_ref[]))
end
function _build_shapes(shape_size::MX_uint, shape_ndim::Ptr{MX_uint}, shape_data::Ptr{Ptr{MX_uint}})
shape_ndim = unsafe_wrap(Array, shape_ndim, shape_size)
shape_data = unsafe_wrap(Array, shape_data, shape_size)
shapes = map(1:shape_size) do i
my_shape = unsafe_wrap(Array, shape_data[i], shape_ndim[i])
tuple(reverse(Int[my_shape...], dims = 1)...)
end
convert(Vector{Tuple}, shapes)
end
function _infer_shape(self, keys, indptr, sdata)
ref_arg_shape_size = Ref{MX_uint}(0)
ref_arg_shape_ndim = Ref{Ptr{MX_uint}}(0)
ref_arg_shape_data = Ref{Ptr{Ptr{MX_uint}}}(0)
ref_out_shape_size = Ref{MX_uint}(0)
ref_out_shape_ndim = Ref{Ptr{MX_uint}}(0)
ref_out_shape_data = Ref{Ptr{Ptr{MX_uint}}}(0)
ref_aux_shape_size = Ref{MX_uint}(0)
ref_aux_shape_ndim = Ref{Ptr{MX_uint}}(0)
ref_aux_shape_data = Ref{Ptr{Ptr{MX_uint}}}(0)
ref_complete = Ref{Cint}(0)
@mxcall(:MXSymbolInferShape,
(MX_handle, MX_uint, char_pp, Ptr{MX_uint}, Ptr{MX_uint},
Ref{MX_uint}, Ref{Ptr{MX_uint}}, Ref{Ptr{Ptr{MX_uint}}},
Ref{MX_uint}, Ref{Ptr{MX_uint}}, Ref{Ptr{Ptr{MX_uint}}},
Ref{MX_uint}, Ref{Ptr{MX_uint}}, Ref{Ptr{Ptr{MX_uint}}},
Ref{Cint}),
self, length(indptr)-1, keys, indptr, sdata,
ref_arg_shape_size, ref_arg_shape_ndim, ref_arg_shape_data,
ref_out_shape_size, ref_out_shape_ndim, ref_out_shape_data,
ref_aux_shape_size, ref_aux_shape_ndim, ref_aux_shape_data,
ref_complete)
if ref_complete[] == 0
return (nothing, nothing, nothing)
else
return (
_build_shapes(ref_arg_shape_size[], ref_arg_shape_ndim[], ref_arg_shape_data[]),
_build_shapes(ref_out_shape_size[], ref_out_shape_ndim[], ref_out_shape_data[]),
_build_shapes(ref_aux_shape_size[], ref_aux_shape_ndim[], ref_aux_shape_data[])
)
end
end
"""
infer_shape(self :: SymbolicNode, args...)
infer_shape(self :: SymbolicNode; kwargs...)
Do shape inference according to the input shapes. The input shapes could be provided
as a list of shapes, which should specify the shapes of inputs in the same order as
the arguments returned by [`list_arguments`](@ref). Alternatively, the shape information
could be specified via keyword arguments.
Returns a 3-tuple containing shapes of all the arguments, shapes of all the outputs and
shapes of all the auxiliary variables. If shape inference failed due to incomplete
or incompatible inputs, the return value will be `(nothing, nothing, nothing)`.
"""
function infer_shape(self :: SymbolicNode; kwargs...)
sdata = MX_uint[]
indptr = MX_uint[0]
for (k,v) in kwargs
append!(sdata, reverse([v...], dims = 1))
push!(indptr, length(sdata))
end
keys = AbstractString[string(x[1]) for x in kwargs]
_infer_shape(self, keys, indptr, sdata)
end
function infer_shape(self :: SymbolicNode, args::Union{Tuple, Cvoid}...)
sdata = MX_uint[]
indptr = MX_uint[0]
for arg in args
if isa(arg, Cvoid); continue; end
append!(sdata, reverse([arg...], dims = 1))
push!(indptr, length(sdata))
end
keys = Ptr{char_p}(0)
_infer_shape(self, keys, indptr, sdata)
end
function _infer_type(self, keys, arg_type_data)
ref_in_type_size = Ref{MX_uint}()
ref_in_type_data = Ref{Ptr{Cint}}()
ref_out_type_size = Ref{MX_uint}()
ref_out_type_data = Ref{Ptr{Cint}}()
ref_aux_type_size = Ref{MX_uint}()
ref_aux_type_data = Ref{Ptr{Cint}}()
ref_complete = Ref{Cint}()
@mxcall(:MXSymbolInferType,
(MX_handle, MX_uint, char_pp, Ptr{Cint},
Ref{MX_uint}, Ref{Ptr{Cint}},
Ref{MX_uint}, Ref{Ptr{Cint}},
Ref{MX_uint}, Ref{Ptr{Cint}},
Ref{Cint}),
self, length(arg_type_data)-1, keys, arg_type_data,
ref_in_type_size, ref_in_type_data,
ref_out_type_size, ref_out_type_data,
ref_aux_type_size, ref_aux_type_data,
ref_complete)
if ref_complete[] == 0
return (nothing, nothing, nothing)
else
in_type = unsafe_wrap(Array, ref_in_type_data[], ref_in_type_size[])
out_type = unsafe_wrap(Array, ref_out_type_data[], ref_out_type_size[])
aux_type = unsafe_wrap(Array, ref_aux_type_data[], ref_aux_type_size[])
return ([fromTypeFlag(TypeFlag(t)) for t in in_type],
[fromTypeFlag(TypeFlag(t)) for t in out_type],
[fromTypeFlag(TypeFlag(t)) for t in aux_type])
end
end
"""
infer_type(self :: SymbolicNode; kwargs...)
infer_type(self :: SymbolicNode, args...)
Do type inference according to the input types. The input types could be provided
as a list of types, which should specify the types of inputs in the same order as
the arguments returned by [`list_arguments`](@ref). Alternatively, the type information
could be specified via keyword arguments.
Returns a 3-tuple containing types of all the arguments, types of all the outputs and
types of all the auxiliary variables. If type inference failed due to incomplete
or incompatible inputs, the return value will be `(nothing, nothing, nothing)`.
"""
function infer_type(self :: SymbolicNode; kwargs...)
types = Cint[toTypeFlag(x[2]) for x in kwargs]
keys = AbstractString[string(x[1]) for x in kwargs]
_infer_type(self, keys, types)
end
function infer_type(self :: SymbolicNode, args :: Union{Tuple,Cvoid}...)
types = Cint[]
keys = Ptr{char_p}(0)
for arg in args
if isa(arg, Cvoid); continue; end
push!(types, toTypeFlag(arg))
end
_infer_type(self, keys, types)
end