MXNet/NDArray/Base.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 180 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package AI::MXNet::NDArray::Base;
19 use strict;
20 use warnings;
21 use AI::MXNet::Base;
22 use AI::MXNet::NDArray::Doc;
23 use Mouse;
24 use AI::MXNet::Function::Parameters;
25  
26 =head1 NAME
27  
28 AI::MXNet::NDArray::Base
29 =cut
30  
31 =head1 DESCRIPTION
32  
33 This module provides a convenient interface to a C++ functions
34 that work with NDArray.
35 Essentially it loads them up during the lib startup into the Perl space.
36 =cut
37  
38 my %function_meta;
39 method function_meta($code)
40 {
41 return $function_meta{$code};
42 }
43  
44 method function_meta_hash()
45 {
46 return \%function_meta;
47 }
48  
49 func _make_ndarray_function($handle, $func_name)
50 {
51 my ($real_name, $desc, $arg_names,
52 $arg_types, $arg_descs, $key_var_num_args,
53 $ret_type) = @{ check_call(AI::MXNetCAPI::SymbolGetAtomicSymbolInfo($handle)) };
54 $ret_type //= '';
55 my $doc_str = build_doc($func_name,
56 $desc,
57 $arg_names,
58 $arg_types,
59 $arg_descs,
60 $key_var_num_args,
61 $ret_type
62 );
63 my %ndarguments;
64 my @arguments;
65 my %arguments = (out => 1, name => 1, ctx => 1, shape => 1);
66 my $j = 0;
67 for my $i (0..(@$arg_names-1))
68 {
69 if(not $arg_types->[$i] =~ /^(?:NDArray|Symbol|ndarray\-or\-symbol)/)
70 {
71 push @arguments, $arg_names->[$i];
72 $arguments{ $arg_names->[$i] } = 1;
73 }
74 else
75 {
76 $ndarguments{ $arg_names->[$i] } = $j++;
77 }
78 }
79 my $generic_ndarray_function = sub
80 {
81 my $class = shift;
82 my (@args, %kwargs, %ndkwargs, @tmp);
83 if(@_ and ref $_[-1] eq 'HASH')
84 {
85 %kwargs = %{ pop(@_) };
86 }
87 else
88 {
89 while(@_ >= 2 and not ref $_[-2])
90 {
91 if(exists $arguments{ $_[-2] })
92 {
93 my $v = pop(@_);
94 my $k = pop(@_);
95 $kwargs{ $k } = $v;
96 }
97 elsif(exists $ndarguments{ $_[-2] })
98 {
99 my $v = pop(@_);
100 my $k = pop(@_);
101 $ndkwargs{ $k } = $v;
102 }
103 else
104 {
105 unshift(@tmp, pop(@_));
106 unshift(@tmp, pop(@_));
107 }
108 }
109 }
110 @args = (@_, @tmp);
111 if(%ndkwargs)
112 {
113 for my $k (keys %ndkwargs)
114 {
115 $args[$ndarguments{$k}] = $ndkwargs{$k};
116 }
117 }
118 my @ndargs;
119 my @pos_args;
120 for my $i (@args)
121 {
122 if(blessed($i) and $i->isa(__PACKAGE__))
123 {
124 push @ndargs, $i->handle;
125 }
126 else
127 {
128 push @pos_args, $i;
129 }
130 if(@pos_args > @arguments)
131 {
132 confess("Too many positional arguments");
133 }
134 }
135 @kwargs{ @arguments[0..$#pos_args] } = @pos_args;
136 my $original_output;
137 my $output_vars;
138 delete $kwargs{name};
139 if(grep { $_ eq 'out' } keys %kwargs)
140 {
141 $output_vars = delete $kwargs{out};
142 $original_output = $output_vars;
143 unless(ref($output_vars) and ref($output_vars) eq 'ARRAY')
144 {
145 $output_vars = [$output_vars];
146 }
147 }
148 else
149 {
150 $output_vars = [];
151 }
152 if(blessed($class) and $class->isa(__PACKAGE__) and not @{ $output_vars })
153 {
154 @ndargs = ($class->handle) if not @ndargs;
155 $class = ref $class;
156 }
157 for my $key (keys %kwargs)
158 {
159 $kwargs{ $key } = "(" .join(", ", map { defined($_) ? $_ : 'None' } @{ $kwargs{ $key } }) .")"
160 if ref $kwargs{ $key } eq 'ARRAY';
161 }
162 my ($out, $stypes) = check_call(AI::MXNetCAPI::ImperativeInvokeEx(
163 $handle,
164 scalar(@ndargs),
165 \@ndargs,
166 [map { $_->handle } @$output_vars],
167 scalar(keys %kwargs),
168 \%kwargs)
169 );
170 return $original_output if $original_output;
171 if(@$out == 1)
172 {
173 return __PACKAGE__->_ndarray_cls($out->[0], 1, $stypes->[0]);
174 }
175 else
176 {
177 my $i = 0;
178 return [map { __PACKAGE__->_ndarray_cls($_, 1, $stypes->[$i++]) } @$out];
179 }
180 };
181 $function_meta{ $generic_ndarray_function }{__name__} = $func_name;
182 $function_meta{ $generic_ndarray_function }{__doc__} = $doc_str;
183 return $generic_ndarray_function;
184 }
185  
186 method _ndarray_cls($handle, $writable=1, $stype=STORAGE_TYPE_UNDEFINED)
187 {
188 if($stype eq STORAGE_TYPE_UNDEFINED)
189 {
190 $stype = __PACKAGE__->_storage_type($handle);
191 }
192 if($stype eq STORAGE_TYPE_DEFAULT)
193 {
194 return AI::MXNet::NDArray->new(handle => $handle, writable => $writable);
195 }
196 elsif($stype eq STORAGE_TYPE_CSR)
197 {
198 return AI::MXNet::NDArray::CSR->new(handle => $handle, writable => $writable);
199 }
200 elsif($stype eq STORAGE_TYPE_ROW_SPARSE)
201 {
202 return AI::MXNet::NDArray::RowSparse->new(handle => $handle, writable => $writable);
203 }
204 else
205 {
206 confess("unknown storage type: $stype");
207 }
208 }
209  
210 method _storage_type($handle)
211 {
212 scalar(check_call(AI::MXNetCAPI::NDArrayGetStorageType($handle)));
213 }
214  
215 method stype()
216 {
217 return STORAGE_TYPE_ID_TO_STR->{ __PACKAGE__->_storage_type($self->handle) };
218 }
219  
220 method _init_ndarray_module()
221 {
222 my $op_names = check_call(AI::MXNetCAPI::ListAllOpNames());
223 for my $name (@$op_names)
224 {
225 my $handle = check_call(AI::NNVMCAPI::GetOpHandle($name));
226 my $function = _make_ndarray_function($handle, $name);
227 {
228 no strict 'refs';
229 *{__PACKAGE__."::$name"} = $function;
230 }
231 }
232 }
233  
234  
235 __PACKAGE__->_init_ndarray_module;
236  
237 1;