diff --git a/perl-package/AI-MXNet/lib/AI/MXNet.pm b/perl-package/AI-MXNet/lib/AI/MXNet.pm index ffc72f9513d3..b5665c88eddf 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet.pm @@ -19,105 +19,51 @@ package AI::MXNet; use v5.14.0; use strict; use warnings; +use AI::MXNet::NS 'global'; use AI::MXNet::Base; -use AI::MXNet::Callback; -use AI::MXNet::NDArray; -use AI::MXNet::Symbol; +use AI::MXNet::Callback 'callback'; +use AI::MXNet::NDArray qw(nd ndarray); +use AI::MXNet::Context 'context'; +use AI::MXNet::Symbol qw(sym symbol); use AI::MXNet::Executor; use AI::MXNet::Executor::Group; use AI::MXNet::CudaModule; -use AI::MXNet::Random; -use AI::MXNet::Initializer; -use AI::MXNet::Optimizer; -use AI::MXNet::KVStore; +use AI::MXNet::Random qw(rnd random); +use AI::MXNet::Initializer qw(init initializer); +use AI::MXNet::Optimizer qw(optimizer opt); +use AI::MXNet::KVStore 'kv'; use AI::MXNet::KVStoreServer; -use AI::MXNet::IO; -use AI::MXNet::Metric; +use AI::MXNet::IO 'io'; +use AI::MXNet::Metric 'metric'; use AI::MXNet::LRScheduler; -use AI::MXNet::Monitor; +use AI::MXNet::Monitor 'mon'; use AI::MXNet::Profiler; use AI::MXNet::Module::Base; -use AI::MXNet::Module; +use AI::MXNet::Module qw(mod module); use AI::MXNet::Module::Bucketing; -use AI::MXNet::RNN; -use AI::MXNet::Visualization; -use AI::MXNet::RecordIO; -use AI::MXNet::Image; -use AI::MXNet::Contrib; -use AI::MXNet::LinAlg; +use AI::MXNet::RNN 'rnn'; +use AI::MXNet::Visualization 'viz'; +use AI::MXNet::RecordIO 'recordio'; +use AI::MXNet::Image qw(img image); +use AI::MXNet::Contrib 'contrib'; +use AI::MXNet::LinAlg 'linalg'; use AI::MXNet::CachedOp; -use AI::MXNet::AutoGrad; -use AI::MXNet::Gluon; +use AI::MXNet::AutoGrad 'autograd'; +use AI::MXNet::Gluon 'gluon'; use AI::MXNet::NDArray::Sparse; use AI::MXNet::Symbol::Sparse; -use AI::MXNet::Engine; +use AI::MXNet::Engine 'engine'; our $VERSION = '1.4'; -sub import -{ - my ($class, $short_name) = @_; - if($short_name) - { - $short_name =~ s/[^\w:]//g; - if(length $short_name) - { - my $short_name_package =<<"EOP"; - package $short_name; - no warnings 'redefine'; - sub nd { 'AI::MXNet::NDArray' } - sub ndarray { 'AI::MXNet::NDArray' } - sub sym { 'AI::MXNet::Symbol' } - sub symbol { 'AI::MXNet::Symbol' } - sub init { 'AI::MXNet::Initializer' } - sub initializer { 'AI::MXNet::Initializer' } - sub optimizer { 'AI::MXNet::Optimizer' } - sub opt { 'AI::MXNet::Optimizer' } - sub rnd { 'AI::MXNet::Random' } - sub random { 'AI::MXNet::Random' } - sub Context { shift; AI::MXNet::Context->new(\@_) } - sub context { 'AI::MXNet::Context' } - sub cpu { AI::MXNet::Context->cpu(\$_[1]//0) } - sub cpu_pinned { AI::MXNet::Context->cpu_pinned(\$_[1]//0) } - sub gpu { AI::MXNet::Context->gpu(\$_[1]//0) } - sub kv { 'AI::MXNet::KVStore' } - sub recordio { 'AI::MXNet::RecordIO' } - sub io { 'AI::MXNet::IO' } - sub metric { 'AI::MXNet::Metric' } - sub mod { 'AI::MXNet::Module' } - sub module { 'AI::MXNet::Module' } - sub mon { 'AI::MXNet::Monitor' } - sub viz { 'AI::MXNet::Visualization' } - sub rnn { 'AI::MXNet::RNN' } - sub callback { 'AI::MXNet::Callback' } - sub img { 'AI::MXNet::Image' } - sub image { 'AI::MXNet::Image' } - sub contrib { 'AI::MXNet::Contrib' } - sub linalg { 'AI::MXNet::LinAlg' } - sub autograd { 'AI::MXNet::AutoGrad' } - sub engine { 'AI::MXNet::Engine' } - sub name { '$short_name' } - sub rtc { '$short_name' } - sub gluon { 'AI::MXNet::Gluon' } - sub CudaModule { shift; AI::MXNet::CudaModule->new(\@_) } - sub AttrScope { shift; AI::MXNet::Symbol::AttrScope->new(\@_) } - *AI::MXNet::Symbol::AttrScope::current = sub { \$${short_name}::AttrScope; }; - \$${short_name}::AttrScope = AI::MXNet::Symbol::AttrScope->new; - sub Prefix { AI::MXNet::Symbol::Prefix->new(prefix => \$_[1]) } - *AI::MXNet::Symbol::NameManager::current = sub { \$${short_name}::NameManager; }; - *AI::MXNet::Symbol::NameManager::set_current = sub { \$${short_name}::NameManager = \$_[1]; }; - \$${short_name}::NameManager = AI::MXNet::Symbol::NameManager->new; - *AI::MXNet::Context::current_ctx = sub { \$${short_name}::Context; }; - *AI::MXNet::Context::current_context = sub { \$${short_name}::Context; }; - *AI::MXNet::Context::set_current = sub { \$${short_name}::Context = \$_[1]; }; - \$${short_name}::Context = AI::MXNet::Context->new(device_type => 'cpu', device_id => 0); - package nd; - \@nd::ISA = ('AI::MXNet::NDArray'); - 1; -EOP - eval $short_name_package; - } - } -} +sub cpu { AI::MXNet::Context->cpu($_[1]//0) } +sub cpu_pinned { AI::MXNet::Context->cpu_pinned($_[1]//0) } +sub gpu { AI::MXNet::Context->gpu($_[1]//0) } +sub name { __PACKAGE__ } +sub rtc { __PACKAGE__ } +sub Prefix { AI::MXNet::Symbol::Prefix->new(prefix => $_[1]) } +our $AttrScope = AI::MXNet::Symbol::AttrScope->new; +our $NameManager = AI::MXNet::Symbol::NameManager->new; +our $Context = AI::MXNet::Context->new(device_type => 'cpu', device_id => 0); 1; __END__ diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/AutoGrad.pm b/perl-package/AI-MXNet/lib/AI/MXNet/AutoGrad.pm index c1e5f06e12bd..d6272b5a1def 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/AutoGrad.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/AutoGrad.pm @@ -18,29 +18,12 @@ package AI::MXNet::AutoGrad; use strict; use warnings; +use AI::MXNet::NS 'global'; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use Scalar::Util qw(blessed); use Carp qw(confess); -sub import -{ - my ($class, $short_name) = @_; - if($short_name) - { - $short_name =~ s/[^\w:]//g; - if(length $short_name) - { - my $short_name_package =<<"EOP"; - package $short_name; - use parent 'AI::MXNet::AutoGrad'; - 1; -EOP - eval $short_name_package; - } - } -} - =head1 NAME AI::MXNet::AutoGrad - Autograd for NDArray. diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Base.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Base.pm index 3f6bd8341325..13513c577175 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Base.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Base.pm @@ -32,14 +32,14 @@ use base qw(Exporter); use List::Util qw(shuffle); use Data::Dumper; -@AI::MXNet::Base::EXPORT = qw(product enumerate assert zip check_call build_param_doc - pdl cat dog svd bisect_left pdl_shuffle as_array ascsr rand_sparse - DTYPE_STR_TO_MX DTYPE_MX_TO_STR DTYPE_MX_TO_PDL - DTYPE_PDL_TO_MX DTYPE_MX_TO_PERL GRAD_REQ_MAP - STORAGE_TYPE_UNDEFINED STORAGE_TYPE_DEFAULT - STORAGE_TYPE_ROW_SPARSE STORAGE_TYPE_CSR - STORAGE_TYPE_STR_TO_ID STORAGE_TYPE_ID_TO_STR STORAGE_AUX_TYPES); -@AI::MXNet::Base::EXPORT_OK = qw(pzeros pceil pones digitize hash array_index range); +our @EXPORT = qw(product enumerate assert zip check_call build_param_doc + pdl cat dog svd bisect_left pdl_shuffle as_array ascsr rand_sparse + DTYPE_STR_TO_MX DTYPE_MX_TO_STR DTYPE_MX_TO_PDL + DTYPE_PDL_TO_MX DTYPE_MX_TO_PERL GRAD_REQ_MAP + STORAGE_TYPE_UNDEFINED STORAGE_TYPE_DEFAULT + STORAGE_TYPE_ROW_SPARSE STORAGE_TYPE_CSR + STORAGE_TYPE_STR_TO_ID STORAGE_TYPE_ID_TO_STR STORAGE_AUX_TYPES); +our @EXPORT_OK = qw(pzeros pceil pones digitize hash array_index range); use constant DTYPE_STR_TO_MX => { float32 => 0, diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Callback.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Callback.pm index b2a0b2948154..72ea1bd4502c 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Callback.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Callback.pm @@ -19,6 +19,7 @@ package AI::MXNet::Callback; use strict; use warnings; use List::Util qw/max/; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; use Mouse; use overload "&{}" => sub { my $self = shift; sub { $self->call(@_) } }; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Context.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Context.pm index 7ae99be7b99e..2cca47f9ab4d 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Context.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Context.pm @@ -19,6 +19,7 @@ package AI::MXNet::Context; use strict; use warnings; use Mouse; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Types; use AI::MXNet::Function::Parameters; @@ -216,12 +217,12 @@ method gpu_memory_info($device_id=0) method current_ctx() { - return $AI::MXNet::current_ctx; + return $AI::MXNet::Context; } method set_current(AI::MXNet::Context $current) { - $AI::MXNet::current_ctx = $current; + $AI::MXNet::Context = $current; } *current_context = \¤t_ctx; @@ -234,5 +235,6 @@ method deepcopy() ); } -$AI::MXNet::current_ctx = __PACKAGE__->new(device_type => 'cpu', device_id => 0); +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet'); +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm index c470acab60e7..f9a99f706302 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm @@ -18,8 +18,9 @@ package AI::MXNet::Contrib; use strict; use warnings; -use AI::MXNet::Contrib::Symbol; -use AI::MXNet::Contrib::NDArray; +use AI::MXNet::NS; +use AI::MXNet::Contrib::Symbol qw(sym symbol); +use AI::MXNet::Contrib::NDArray qw(nd ndarray); =head1 NAME @@ -47,9 +48,4 @@ use AI::MXNet::Contrib::NDArray; } =cut -sub sym { 'AI::MXNet::Contrib::Symbol' } -sub symbol { 'AI::MXNet::Contrib::Symbol' } -sub nd { 'AI::MXNet::Contrib::NDArray' } -sub ndarray { 'AI::MXNet::Contrib::NDArray' } - 1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/NDArray.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/NDArray.pm index 574ecc443f79..83e7cbabce5a 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/NDArray.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/NDArray.pm @@ -18,6 +18,7 @@ package AI::MXNet::Contrib::NDArray; use strict; use warnings; +use AI::MXNet::NS; use parent 'AI::MXNet::AutoLoad'; sub config { ('contrib', 'AI::MXNet::NDArray') } diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/Symbol.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/Symbol.pm index d5a041a085fe..03f4b90e3373 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/Symbol.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib/Symbol.pm @@ -18,6 +18,7 @@ package AI::MXNet::Contrib::Symbol; use strict; use warnings; +use AI::MXNet::NS; use parent 'AI::MXNet::AutoLoad'; sub config { ('contrib', 'AI::MXNet::Symbol') } diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/CudaModule.pm b/perl-package/AI-MXNet/lib/AI/MXNet/CudaModule.pm index b3272fe8b048..67e6b60a0190 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/CudaModule.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/CudaModule.pm @@ -18,6 +18,7 @@ package AI::MXNet::CudaModule; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use Mouse; use AI::MXNet::Function::Parameters; @@ -195,6 +196,8 @@ method get_kernel(Str $name, Str $signature) return AI::MXNet::CudaKernel->new($handle, $name, \@is_ndarray, \@dtypes); } +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet'); + package AI::MXNet::CudaKernel; use Mouse; use AI::MXNet::Base; @@ -296,4 +299,4 @@ method launch( ); } -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Engine.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Engine.pm index 1d73e5584268..e74be0230d7b 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Engine.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Engine.pm @@ -20,6 +20,7 @@ use strict; use warnings; use AI::MXNet::Function::Parameters; use AI::MXNet::Base; +use AI::MXNet::NS; =head1 NAME @@ -100,4 +101,4 @@ method bulk(Int $size, CodeRef $sub) Carp::confess($err) if $err; } -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon.pm index fde2f6ac5a63..657be74c5a6d 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon.pm @@ -18,42 +18,14 @@ package AI::MXNet::Gluon; use strict; use warnings; -use AI::MXNet::Gluon::Loss; +use AI::MXNet::NS 'global'; +use AI::MXNet::Gluon::Loss 'loss'; use AI::MXNet::Gluon::Trainer; use AI::MXNet::Gluon::Utils; -use AI::MXNet::Gluon::Data; -use AI::MXNet::Gluon::NN; -use AI::MXNet::Gluon::RNN; - -sub import -{ - my ($class, $short_name) = @_; - if($short_name) - { - $short_name =~ s/[^\w:]//g; - if(length $short_name) - { - my $short_name_package =<<"EOP"; - package $short_name; - no warnings 'redefine'; - sub data { 'AI::MXNet::Gluon::Data' } - sub nn { 'AI::MXNet::Gluon::NN_' } - sub rnn { 'AI::MXNet::Gluon::RNN_' } - sub loss { 'AI::MXNet::Gluon::Loss_' } - sub utils { 'AI::MXNet::Gluon::Utils' } - sub model_zoo { require AI::MXNet::Gluon::ModelZoo; 'AI::MXNet::Gluon::ModelZoo' } - sub Trainer { shift; AI::MXNet::Gluon::Trainer->new(\@_); } - sub Parameter { shift; AI::MXNet::Gluon::Parameter->new(\@_); } - sub ParameterDict { shift; AI::MXNet::Gluon::ParameterDict->new(\@_); } - \@${short_name}::ISA = ('AI::MXNet::Gluon_'); - 1; -EOP - eval $short_name_package; - } - } -} +use AI::MXNet::Gluon::Data 'data'; +use AI::MXNet::Gluon::NN 'nn'; +use AI::MXNet::Gluon::RNN 'rnn'; -sub data { 'AI::MXNet::Gluon::Data' } sub utils { 'AI::MXNet::Gluon::Utils' } sub model_zoo { require AI::MXNet::Gluon::ModelZoo; 'AI::MXNet::Gluon::ModelZoo' } diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Block.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Block.pm index 599c3c3bef6e..b58704b5d8a1 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Block.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Block.pm @@ -731,8 +731,13 @@ method forward(@args) method register(Str $container) { my $sub_name = $self->_class_name; + my $dest = $self->can('new'); + my $func = sub { + splice @_, 0, 1, $self; + goto $dest; + }; no strict 'refs'; - *{$container.'_::'.$sub_name} = sub { shift; $self->new(@_) }; + *{"$container\::$sub_name"} = $func; } =head2 summary diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data.pm index e2287c23dafe..95612d9d8cf3 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data.pm @@ -18,11 +18,11 @@ package AI::MXNet::Gluon::Data; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Gluon::Data::Set; use AI::MXNet::Gluon::Data::Sampler; use AI::MXNet::Gluon::Data::Loader; -use AI::MXNet::Gluon::Data::Vision; -sub vision { 'AI::MXNet::Gluon::Data::Vision' } +use AI::MXNet::Gluon::Data::Vision 'vision'; 1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data/Vision.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data/Vision.pm index 5711af350e5f..1c9d551b3d1e 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data/Vision.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data/Vision.pm @@ -15,6 +15,9 @@ # specific language governing permissions and limitations # under the License. +package AI::MXNet::Gluon::Data::Vision; +use AI::MXNet::NS; + package AI::MXNet::Gluon::Data::Vision::DownloadedDataSet; use strict; use warnings; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Loss.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Loss.pm index 3eb62eb5a2ef..a5938595df6f 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Loss.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Loss.pm @@ -18,6 +18,7 @@ use strict; use warnings; package AI::MXNet::Gluon::Loss; +use AI::MXNet::NS; use AI::MXNet::Gluon::Block; use AI::MXNet::Function::Parameters; @@ -995,4 +996,4 @@ method _cosine_similarity($F, $x, $y, $axis=-1) __PACKAGE__->register('AI::MXNet::Gluon::Loss'); -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN.pm index 673ee5de3686..c8b4c7f4ef60 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN.pm @@ -18,27 +18,10 @@ package AI::MXNet::Gluon::NN; use strict; use warnings; +use AI::MXNet::NS 'global'; use AI::MXNet::Gluon::Block; use AI::MXNet::Gluon::NN::Activation; use AI::MXNet::Gluon::NN::BasicLayers; use AI::MXNet::Gluon::NN::ConvLayers; -sub import -{ - my ($class, $short_name) = @_; - if($short_name) - { - $short_name =~ s/[^\w:]//g; - if(length $short_name) - { - my $short_name_package =<<"EOP"; - package $short_name; - \@${short_name}::ISA = ('AI::MXNet::Gluon::NN_'); - 1; -EOP - eval $short_name_package; - } - } -} - -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/Activation.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/Activation.pm index 1d6342f4955b..63fd80d705b8 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/Activation.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/Activation.pm @@ -240,3 +240,5 @@ method hybrid_forward(GluonClass $F, GluonInput $x) } __PACKAGE__->register('AI::MXNet::Gluon::NN'); + +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/BasicLayers.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/BasicLayers.pm index 954179095cdf..6c554bfd0626 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/BasicLayers.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/BasicLayers.pm @@ -272,7 +272,7 @@ sub BUILD if(defined $self->activation) { $self->act( - AI::MXNet::Gluon::NN::Activation->new( + AI::MXNet::Gluon::NN->Activation( activation => $self->activation, prefix => $self->activation.'_' ) @@ -920,4 +920,4 @@ use overload '""' => sub { __PACKAGE__->register('AI::MXNet::Gluon::NN'); -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/ConvLayers.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/ConvLayers.pm index a4bb89b2b2a2..0e11714d4a41 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/ConvLayers.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/ConvLayers.pm @@ -156,7 +156,7 @@ sub BUILD if(defined $self->activation) { $self->act( - AI::MXNet::Gluon::NN::Activation->new( + AI::MXNet::Gluon::NN->Activation( activation => $self->activation, prefix => $self->activation.'_' ) @@ -1415,4 +1415,4 @@ method hybrid_forward(GluonClass $F, GluonInput $x) __PACKAGE__->register('AI::MXNet::Gluon::NN'); -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm index 89cd0cac6229..a914c1ff68ca 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm @@ -19,6 +19,7 @@ use strict; use warnings; use Hash::Ordered; package AI::MXNet::Gluon::Parameter; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; =head1 NAME @@ -799,6 +800,8 @@ method cast(Dtype $dtype) }); } +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet::Gluon'); + package AI::MXNet::Gluon::Constant; use strict; use warnings; @@ -1323,4 +1326,6 @@ method load( } } +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet::Gluon'); + 1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/RNN.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/RNN.pm index cdd9468f228c..bf5736ccbb9e 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/RNN.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/RNN.pm @@ -18,25 +18,8 @@ package AI::MXNet::Gluon::RNN; use strict; use warnings; +use AI::MXNet::NS 'global'; use AI::MXNet::Gluon::RNN::Layer; use AI::MXNet::Gluon::RNN::Cell; -sub import -{ - my ($class, $short_name) = @_; - if($short_name) - { - $short_name =~ s/[^\w:]//g; - if(length $short_name) - { - my $short_name_package =<<"EOP"; - package $short_name; - \@${short_name}::ISA = ('AI::MXNet::Gluon::RNN_'); - 1; -EOP - eval $short_name_package; - } - } -} - -1; \ No newline at end of file +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Trainer.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Trainer.pm index 6117777eed8f..12cd7c2391c3 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Trainer.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Trainer.pm @@ -18,6 +18,7 @@ use strict; use warnings; package AI::MXNet::Gluon::Trainer; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use IO::File; @@ -554,4 +555,6 @@ method load_states(Str $fname) } } +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet::Gluon'); + 1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Utils.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Utils.pm index 497356da89a8..66d8acc6c5b1 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Utils.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Utils.pm @@ -25,7 +25,7 @@ use File::Path qw(make_path); use HTTP::Tiny; use Exporter; use base qw(Exporter); -@AI::MXNet::Gluon::Utils::EXPORT_OK = qw(download check_sha1); +our @EXPORT_OK = qw(download check_sha1); =head1 NAME diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/IO.pm b/perl-package/AI-MXNet/lib/AI/MXNet/IO.pm index 19e7cfdb8fe3..5dd3955b2438 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/IO.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/IO.pm @@ -18,6 +18,7 @@ package AI::MXNet::IO; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use Scalar::Util qw/blessed/; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Image.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Image.pm index 9c7fa120f343..ab4e7964918b 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Image.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Image.pm @@ -19,6 +19,7 @@ package AI::MXNet::Image; use strict; use warnings; use Scalar::Util qw(blessed); +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use AI::MXNet::Image::NDArray; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Initializer.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Initializer.pm index 75c8b1e3dad1..089731f16eee 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Initializer.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Initializer.pm @@ -45,6 +45,7 @@ around BUILDARGS => sub { # Base class for Initializers package AI::MXNet::Initializer; use Mouse; +use AI::MXNet::NS; use AI::MXNet::Base qw(:DEFAULT pzeros pceil); use AI::MXNet::NDArray; use JSON::PP; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/KVStore.pm b/perl-package/AI-MXNet/lib/AI/MXNet/KVStore.pm index 15aad76c7b4a..259ad0df5191 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/KVStore.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/KVStore.pm @@ -18,6 +18,7 @@ package AI::MXNet::KVStore; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::NDArray; use AI::MXNet::Optimizer; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm index be1262fb6a87..3fef1b873cab 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm @@ -18,6 +18,7 @@ package AI::MXNet::LinAlg; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::LinAlg::Symbol; use AI::MXNet::LinAlg::NDArray; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm index 0941316960a9..4c2baa43d1fd 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm @@ -18,6 +18,7 @@ package AI::MXNet::Metric; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; use Scalar::Util qw/blessed/; use JSON::PP; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Module.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Module.pm index 38c2ae645969..097f038ed11a 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Module.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Module.pm @@ -32,6 +32,7 @@ has [qw/_param_names _fixed_param_names ] => (is => 'rw', init_arg => undef); package AI::MXNet::Module; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use List::Util qw(max); diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Monitor.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Monitor.pm index 76fdfd24e7e8..9e4a96849a00 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Monitor.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Monitor.pm @@ -17,6 +17,7 @@ package AI::MXNet::Monitor; use Mouse; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; use AI::MXNet::Base; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm b/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm index f466aaa11a3d..f75cc84b2a8f 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm @@ -59,6 +59,7 @@ package AI::MXNet::NDArray; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::NDArray::Slice; use AI::MXNet::Context; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/NS.pm b/perl-package/AI-MXNet/lib/AI/MXNet/NS.pm new file mode 100644 index 000000000000..03cd5f195182 --- /dev/null +++ b/perl-package/AI-MXNet/lib/AI/MXNet/NS.pm @@ -0,0 +1,78 @@ +# 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. + +package AI::MXNet::NS; +# this class is similar to Exporter, in that it will add an "import" +# method to the calling package. It is to allow a package to emulate +# the python "import mxnet as mx" style aliasing as "use AI::MXNet 'mx'" +use strict; +use warnings; + +sub _sym : lvalue +{ + my ($pkg, $name) = @_; + no strict 'refs'; + *{"$pkg\::$name"}; +} + +sub import +{ + my (undef, $opt) = @_; + my $class = caller(); + my $func = sub { $class }; + _sym($class, 'import') = sub { + my (undef, @names) = @_; + @names = map { s/[^\w:]//sgr } @names; + my $target = caller(); + + _sym($names[0], '') = _sym($class, '') if + @names == 1 and $opt and $opt eq 'global'; + + _sym($target, $_) = $func for @names; + }; +} + +my $autoload_template = q( + sub AUTOLOAD + { + our ($AUTOLOAD, %AUTOLOAD); + my $name = $AUTOLOAD =~ s/.*:://sr; + my $func = $AUTOLOAD{$name}; + Carp::carp(qq(Can't locate object method "$name" via package "${\ __PACKAGE__ }")) + unless $func; + goto $func; + } +); + +# using AUTOLOAD here allows for the addition of an AI::MXNet::SomeClass +# class to coexist with an AI::MXNet->SomeClass() shorthand constructor. +sub register +{ + my ($class, $target) = @_; + my $name = $class =~ s/.*:://sr; + my $dest = $class->can('new'); + ${_sym($target, 'AUTOLOAD')}{$name} = sub { + splice @_, 0, 1, $class; + goto $dest; + }; + return if $target->can('AUTOLOAD'); + eval sprintf 'package %s { %s }', $target, $autoload_template; + die if $@; + return; +} + +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Optimizer.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Optimizer.pm index ad0e45503220..7e78cd384220 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Optimizer.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Optimizer.pm @@ -18,6 +18,7 @@ package AI::MXNet::Optimizer; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::NDArray; use AI::MXNet::Random; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/RNN.pm b/perl-package/AI-MXNet/lib/AI/MXNet/RNN.pm index 07e72a755723..6ba9bf431e08 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/RNN.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/RNN.pm @@ -18,6 +18,7 @@ package AI::MXNet::RNN; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; use AI::MXNet::RNN::IO; use AI::MXNet::RNN::Cell; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Random.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Random.pm index 7a99b1dc7ea9..ddc0edca1ba8 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Random.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Random.pm @@ -19,6 +19,7 @@ package AI::MXNet::Random; use strict; use warnings; use Scalar::Util qw/blessed/; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::NDArray::Base; use AI::MXNet::Function::Parameters; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/RecordIO.pm b/perl-package/AI-MXNet/lib/AI/MXNet/RecordIO.pm index f22e2ce92789..5637260a6b08 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/RecordIO.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/RecordIO.pm @@ -18,6 +18,7 @@ package AI::MXNet::RecordIO; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; use AI::MXNet::Types; use AI::MXNet::Base; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm index e4953f17031a..32e1105210d5 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm @@ -24,6 +24,7 @@ package AI::MXNet::Symbol; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Symbol::Base; use AI::MXNet::Symbol::Random; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/AttrScope.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/AttrScope.pm index c728ed1b6ce8..549939f006aa 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/AttrScope.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/AttrScope.pm @@ -19,6 +19,7 @@ package AI::MXNet::Symbol::AttrScope; use strict; use warnings; use Mouse; +use AI::MXNet::NS; use AI::MXNet::Function::Parameters; around BUILDARGS => sub { my $orig = shift; @@ -58,7 +59,12 @@ has 'attr' => ( method current() { - $AI::MXNet::curr_attr_scope; + $AI::MXNet::AttrScope; +} + +method set_current(AI::MXNet::Symbol::AttrScope $new) +{ + $AI::MXNet::AttrScope = $new; } =head2 get @@ -83,4 +89,6 @@ method get(Maybe[HashRef[Str]] $attr=) return bless (\%ret, 'AI::MXNet::Util::Printable'); } -$AI::MXNet::curr_attr_scope = __PACKAGE__->new; +__PACKAGE__->AI::MXNet::NS::register('AI::MXNet'); + +1; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/NameManager.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/NameManager.pm index 0126655186fd..2238a4366789 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/NameManager.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol/NameManager.pm @@ -79,16 +79,14 @@ method get(Maybe[Str] $name, Str $hint) method current() { - $AI::MXNet::Symbol::NameManager; + $AI::MXNet::NameManager; } method set_current(AI::MXNet::Symbol::NameManager $new) { - $AI::MXNet::Symbol::NameManager = $new; + $AI::MXNet::NameManager = $new; } -$AI::MXNet::Symbol::NameManager = __PACKAGE__->new; - package AI::MXNet::Symbol::Prefix; use Mouse; diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm b/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm index c04b79c5e4d0..3131692fc981 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm @@ -18,6 +18,7 @@ package AI::MXNet::TestUtils; use strict; use warnings; +use AI::MXNet 'mx'; use PDL; use Carp qw(confess); use Scalar::Util qw(blessed); @@ -26,10 +27,10 @@ use AI::MXNet::Function::Parameters; use AI::MXNet::Base; use Exporter; use base qw(Exporter); -@AI::MXNet::TestUtils::EXPORT_OK = qw(same reldiff almost_equal GetMNIST_ubyte - GetCifar10 pdl_maximum pdl_minimum mlp2 conv dies_ok - check_consistency zip assert enumerate same_array dies_like allclose rand_shape_2d - rand_shape_3d rand_sparse_ndarray random_arrays rand_ndarray randint pdl); +our @EXPORT_OK = qw(same reldiff almost_equal GetMNIST_ubyte + GetCifar10 pdl_maximum pdl_minimum mlp2 conv dies_ok + check_consistency zip assert enumerate same_array dies_like allclose rand_shape_2d + rand_shape_3d rand_sparse_ndarray random_arrays rand_ndarray randint pdl); use constant default_numerical_threshold => 1e-6; =head1 NAME diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Visualization.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Visualization.pm index 1574ea58307f..90ec1da4e289 100644 --- a/perl-package/AI-MXNet/lib/AI/MXNet/Visualization.pm +++ b/perl-package/AI-MXNet/lib/AI/MXNet/Visualization.pm @@ -18,6 +18,7 @@ package AI::MXNet::Visualization; use strict; use warnings; +use AI::MXNet::NS; use AI::MXNet::Base; use AI::MXNet::Function::Parameters; use JSON::PP; diff --git a/perl-package/AI-MXNet/t/AI-MXNet.t b/perl-package/AI-MXNet/t/AI-MXNet.t index d876fe3ee12b..ad5a0266e06f 100644 --- a/perl-package/AI-MXNet/t/AI-MXNet.t +++ b/perl-package/AI-MXNet/t/AI-MXNet.t @@ -17,5 +17,8 @@ use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 3; BEGIN { use_ok('AI::MXNet') }; + +isa_ok(AI::MXNet->Context(), 'AI::MXNet::Context'); +isa_ok(AI::MXNet::Context->new(), 'AI::MXNet::Context'); diff --git a/perl-package/AI-MXNet/t/test_multi_device_exec.t b/perl-package/AI-MXNet/t/test_multi_device_exec.t index 6a33a2375b7d..1b37e6ee9981 100644 --- a/perl-package/AI-MXNet/t/test_multi_device_exec.t +++ b/perl-package/AI-MXNet/t/test_multi_device_exec.t @@ -62,11 +62,11 @@ sub test_ctx_group my ($arr, $name) = @$_; if(exists $set_stage1{ $name }) { - ok($arr->context == $group2ctx->{stage1}); + cmp_ok($arr->context, '==', $group2ctx->{stage1}); } else { - ok($arr->context == $group2ctx->{stage2}); + cmp_ok($arr->context, '==', $group2ctx->{stage2}); } } }