/
cached_op.cr
67 lines (58 loc) · 1.38 KB
/
cached_op.cr
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
require "../mxnet"
module MXNet
# Cached operator.
#
class CachedOp
include MXNet::Util
@handle : CachedOpHandle
def initialize(symbol, flags)
flags = flags.size > 0 ?
# stringify
flags.map { |k, v| [output(k), output(v)] }.to_h :
{} of String => String
MXNet::Internal.libcall(
MXCreateCachedOpEx,
symbol.handle,
flags.size,
flags.keys.map(&.to_unsafe),
flags.values.map(&.to_unsafe),
out @handle
)
end
# Invokes the cached operator.
#
def call(args, out _out : NDArray? = nil)
args = args.size > 0 ?
# obtain handles
args.to_a.compact.map { |v| v.handle } :
[] of NDArrayHandle
num_outputs = 0
outputs = Pointer(NDArrayHandle).null
if _out
num_outputs = 1
outputs = Pointer(NDArrayHandle).malloc(1)
outputs[0] = _out.handle
end
MXNet::Internal.libcall(
MXInvokeCachedOpEx,
@handle,
args.size,
args,
pointerof(num_outputs),
pointerof(outputs),
out stypes
)
if _out
[_out]
else
num_outputs.times.reduce([] of NDArray) do |arr, i|
arr << NDArray.new(outputs[i])
end
end
end
# :nodoc:
def finalize
MXNet::Internal.libcall(MXFreeCachedOp, @handle)
end
end
end