| 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::Symbol::Base; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Base; | ||
| 22 | use AI::MXNet::Symbol::AttrScope; | ||
| 23 | use AI::MXNet::Symbol::Doc; | ||
| 24 | use AI::MXNet::Symbol::NameManager; | ||
| 25 | use Mouse; | ||
| 26 | use AI::MXNet::Function::Parameters; | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | AI::MXNet::Symbol::Base | ||
| 31 | =cut | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | A convenience class that loads all C++ symbol related functions at runtime. | ||
| 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 | sub _compose | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | my (@args, %kwargs); | ||
| 53 | while(ref $_[0]) | ||
| 54 | { | ||
| 55 | push @args, shift(@_); | ||
| 56 | } | ||
| 57 | %kwargs = @_; | ||
| 58 | my $name = delete $kwargs{'name'}; | ||
| 59 | if(@args and %kwargs) | ||
| 60 | { | ||
| 61 | confess("_compose only accept input Symbols \ | ||
| 62 | either as positional or keyword arguments, not both"); | ||
| 63 | } | ||
| 64 | if(grep { not blessed($_) or not $_->isa(__PACKAGE__) } (@args, values %kwargs)) | ||
| 65 | { | ||
| 66 | confess("_compose expect 'Symbol' as arguments"); | ||
| 67 | } | ||
| 68 | |||
| 69 | my $num_args = scalar(@args) + scalar(keys %kwargs); | ||
| 70 | my $keys = []; | ||
| 71 | my $args = []; | ||
| 72 | for my $key (keys %kwargs) | ||
| 73 | { | ||
| 74 | push @$keys, $key; | ||
| 75 | push @$args, $kwargs{ $key }->handle; | ||
| 76 | } | ||
| 77 | @$args = map { $_->handle } @args if @args; | ||
| 78 | check_call( | ||
| 79 | AI::NNVMCAPI::SymbolCompose( | ||
| 80 | $self->handle, $name, $num_args, $keys, $args | ||
| 81 | ) | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | |||
| 85 | # Create an atomic symbol function by handle and funciton name | ||
| 86 | func _make_atomic_symbol_function($handle, $name) | ||
| 87 | { | ||
| 88 | my ($real_name, $desc, $arg_names, | ||
| 89 | $arg_types, $arg_descs, $key_var_num_args, | ||
| 90 | $ret_type) = @{ check_call(AI::MXNetCAPI::SymbolGetAtomicSymbolInfo($handle)) }; | ||
| 91 | $ret_type //= ''; | ||
| 92 | my $func_name = $name; | ||
| 93 | my @arguments; | ||
| 94 | my %arguments = map { $_ => 1 } qw/name attr lr_mult wd_mult | ||
| 95 | init __layout__ dtype shape/; | ||
| 96 | for my $i (0..@{ $arg_names }-1) | ||
| 97 | { | ||
| 98 | push @arguments, $arg_names->[$i]; | ||
| 99 | $arguments{ $arg_names->[$i] } = 1; | ||
| 100 | } | ||
| 101 | my $doc_str = build_doc($func_name, | ||
| 102 | $desc, | ||
| 103 | $arg_names, | ||
| 104 | $arg_types, | ||
| 105 | $arg_descs, | ||
| 106 | $key_var_num_args, | ||
| 107 | $ret_type | ||
| 108 | ); | ||
| 109 | my $creator = sub { | ||
| 110 | my $class = ref($_[0]) || shift; | ||
| 111 | my (@args, %kwargs); | ||
| 112 | if( | ||
| 113 | @_ | ||
| 114 | and | ||
| 115 | ref $_[-1] eq 'HASH' | ||
| 116 | and | ||
| 117 | not (@_ >= 2 and not blessed $_[-2] and $_[-2] eq 'attr') | ||
| 118 | ) | ||
| 119 | { | ||
| 120 | %kwargs = %{ pop(@_) }; | ||
| 121 | @args = @_; | ||
| 122 | } | ||
| 123 | elsif(blessed $_[0] and $_[0]->isa(__PACKAGE__)) | ||
| 124 | { | ||
| 125 | |||
| 126 | while(blessed $_[0] and $_[0]->isa(__PACKAGE__)) | ||
| 127 | { | ||
| 128 | push @args, shift(@_); | ||
| 129 | } | ||
| 130 | %kwargs = @_; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | while(@_ >= 2 and not ref $_[-2] | ||
| 135 | and (exists $arguments{ $_[-2] } or (blessed $_[-1] and $_[-1]->isa(__PACKAGE__)))) | ||
| 136 | { | ||
| 137 | my $v = pop(@_); | ||
| 138 | my $k = pop(@_); | ||
| 139 | $kwargs{ $k } = $v; | ||
| 140 | } | ||
| 141 | @kwargs{ @arguments[0..@args-1] } = @args; | ||
| 142 | } | ||
| 143 | if(blessed $class and $class->isa(__PACKAGE__)) | ||
| 144 | { | ||
| 145 | $kwargs{data} = $class; | ||
| 146 | } | ||
| 147 | my $params = {}; | ||
| 148 | my $symbol_kwargs = {}; | ||
| 149 | my $attr = delete $kwargs{ 'attr' }; | ||
| 150 | %kwargs = (%kwargs, % { AI::MXNet::Symbol::AttrScope->current->get($attr) }); | ||
| 151 | $name = delete $kwargs{ 'name' }; | ||
| 152 | if($key_var_num_args and not exists $kwargs { $key_var_num_args }) | ||
| 153 | { | ||
| 154 | $params->{ $key_var_num_args } = scalar(@args); | ||
| 155 | } | ||
| 156 | for my $key (keys %kwargs) | ||
| 157 | { | ||
| 158 | $kwargs{ $key } = "(" .join(", ", map { defined($_) ? $_ : 'None' } @{ $kwargs{ $key } }) .")" | ||
| 159 | if ref $kwargs{ $key } eq 'ARRAY'; | ||
| 160 | } | ||
| 161 | while(my ($k, $v) = each %kwargs) | ||
| 162 | { | ||
| 163 | if(blessed($v) and $v->isa(__PACKAGE__)) | ||
| 164 | { | ||
| 165 | $symbol_kwargs->{ $k } = $v; | ||
| 166 | } | ||
| 167 | else | ||
| 168 | { | ||
| 169 | $params->{ $k } = "$v"; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | # create atomic symbol | ||
| 173 | my $sym_handle = check_call( | ||
| 174 | AI::MXNetCAPI::SymbolCreateAtomicSymbol( | ||
| 175 | $handle, | ||
| 176 | scalar(keys %$params), | ||
| 177 | $params | ||
| 178 | ) | ||
| 179 | ); | ||
| 180 | my $s = $class->new(handle => $sym_handle); | ||
| 181 | my $hint = lc($func_name); | ||
| 182 | $name = AI::MXNet::Symbol::NameManager->current->get($name, $hint); | ||
| 183 | $s->_compose(@args, name => $name, %$symbol_kwargs); | ||
| 184 | return $s; | ||
| 185 | }; | ||
| 186 | $function_meta{ $creator }{__name__} = $func_name; | ||
| 187 | $function_meta{ $creator }{__doc__} = $doc_str; | ||
| 188 | return $creator; | ||
| 189 | } | ||
| 190 | |||
| 191 | method _init_symbol_module() | ||
| 192 | { | ||
| 193 | my $op_names = check_call(AI::MXNetCAPI::ListAllOpNames()); | ||
| 194 | for my $name (@$op_names) | ||
| 195 | { | ||
| 196 | my $handle = check_call(AI::NNVMCAPI::GetOpHandle($name)); | ||
| 197 | my $function = _make_atomic_symbol_function($handle, $name); | ||
| 198 | { | ||
| 199 | no strict 'refs'; | ||
| 200 | { | ||
| 201 | *{__PACKAGE__."::$name"} = $function; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | __PACKAGE__->_init_symbol_module; | ||
| 209 | |||
| 210 | 1; |