Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Perl5 interface to MXNet (#5022)
Browse files Browse the repository at this point in the history
* initial checkin.

* Addressed code review issues.
1) Change lincense to Apache 2.0
2) Expanded tests
3) Made perl-package testable via Travis CI.

* removed static swig file in favor of dynamically generating ones.

* Reset changes to submodules.

* small fix in order to get tests pass under Perl 5.22 on Ubuntu 16.04.1 LTS (Xenial Xerus)

* 1) Fixed problems that prevented the interface to work on OSX
2) Removed tests that were dependent on random behavior, they were not reliable across different OS.

* OSX build process cleanup.

* Workaround for
SIP (system integrity protection) that prevents DYLD_LIBRARY_PATH from working

* 1) added visualization
2) simplified symbol interface a bit in order to make it closer to Python usage
3) miscellaneous cleanups

* 1) added bucketing module
2) fixed few pod errors
3) added better Synopsis to main module.
  • Loading branch information
sergeykolychev authored and piiswrong committed Feb 21, 2017
1 parent 2a7d6f2 commit 6a0db69
Show file tree
Hide file tree
Showing 70 changed files with 16,819 additions and 3 deletions.
21 changes: 18 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ env:

# TODO, R test, distributed test, clang, more g++ versions

# matrix:
# exclude:
# - os: osx
matrix:
include:
- os: linux
dist: trusty
env: TASK=perl_test
- os: osx
## sudo is required because
## prexexisting packages conflict
## with new ones.
## would be nice to have macports
## on travis osx, it has all needed perl packages
sudo: required
env: TASK=perl_test
# env: TASK=julia JULIA_VER=0.4
# - os: linux
# env: TASK=build
Expand Down Expand Up @@ -57,6 +67,11 @@ addons:
- python3-dev
- python3-nose
- graphviz
- libmouse-perl
- pdl
- cpanminus
- swig
- libgraphviz-perl

before_install:
- export NVCC_PREFIX=${HOME}
Expand Down
1 change: 1 addition & 0 deletions perl-package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!*
5 changes: 5 additions & 0 deletions perl-package/AI-MXNet/Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Revision history for Perl extension AI::MXNet

0.02 Tue Feb 14 07:28:11 PST 2017
- prepared for inclusion to the mxnet code repository.

46 changes: 46 additions & 0 deletions perl-package/AI-MXNet/MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
MANIFEST
Makefile.PL
README
Changes
lib/AI/MXNet.pm
lib/AI/MXNet/Base.pm
lib/AI/MXNet/Callback.pm
lib/AI/MXNet/Context.pm
lib/AI/MXNet/Executor.pm
lib/AI/MXNet/Executor/Group.pm
lib/AI/MXNet/Function/Parameters.pm
lib/AI/MXNet/IO.pm
lib/AI/MXNet/Initializer.pm
lib/AI/MXNet/KVStore.pm
lib/AI/MXNet/KVStoreServer.pm
lib/AI/MXNet/LRScheduler.pm
lib/AI/MXNet/Logging.pm
lib/AI/MXNet/Metric.pm
lib/AI/MXNet/Module.pm
lib/AI/MXNet/Module/Base.pm
lib/AI/MXNet/Module/Bucketing.pm
lib/AI/MXNet/Monitor.pm
lib/AI/MXNet/NDArray.pm
lib/AI/MXNet/NDArray/Base.pm
lib/AI/MXNet/NDArray/Doc.pm
lib/AI/MXNet/NDArray/Slice.pm
lib/AI/MXNet/Optimizer.pm
lib/AI/MXNet/Profiler.pm
lib/AI/MXNet/Random.pm
lib/AI/MXNet/Rtc.pm
lib/AI/MXNet/Symbol.pm
lib/AI/MXNet/Symbol/AttrScope.pm
lib/AI/MXNet/Symbol/Base.pm
lib/AI/MXNet/Symbol/Doc.pm
lib/AI/MXNet/Symbol/NameManager.pm
lib/AI/MXNet/TestUtils.pm
lib/AI/MXNet/Types.pm
lib/AI/MXNet/Util/Printable.pm
lib/AI/MXNet/Visualization.pm
t/AI-MXNet.t
t/initializers.t
t/io.t
t/models.t
t/optimizers.t
t/test_conv.t
t/test_viz.t
22 changes: 22 additions & 0 deletions perl-package/AI-MXNet/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
abstract: 'Perl interface to MXNet machine learning library'
author:
- 'Sergey Kolychev <sergeykolychev.github@gmail.com>'
build_requires: {}
configure_requires:
ExtUtils::MakeMaker: 6.30
dynamic_config: 0
generated_by: 'Dist::Zilla version 5.008, CPAN::Meta::Converter version 2.143240'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: AI-MXNet
requires:
AI::MXNetCAPI: 0.02
AI::NNVMCAPI: 0.02
Function::Parameters: 1.0705
Mouse: v2.1.0
PDL: 2.007
perl: v5.14.0
version: 0.02
59 changes: 59 additions & 0 deletions perl-package/AI-MXNet/Makefile.PL
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use strict;
use warnings;

use 5.014000;

use ExtUtils::MakeMaker 6.30;



my %WriteMakefileArgs = (
"ABSTRACT" => "Perl interface to MXNet machine learning library",
"AUTHOR" => "Sergey Kolychev <sergeykolychev.github\@gmail.com>",
"BUILD_REQUIRES" => {},
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => "6.30"
},
"DISTNAME" => "AI-MXNet",
"EXE_FILES" => [],
"LICENSE" => "apache_2_0",
"NAME" => "AI::MXNet",
"PREREQ_PM" => {
"AI::MXNetCAPI" => "0.02",
"AI::NNVMCAPI" => "0.02",
"Function::Parameters" => "1.0705",
"Mouse" => "2.1.0",
"PDL" => "2.007",
"GraphViz" => "2.14"
},
"TEST_REQUIRES" => {},
"VERSION" => "0.02",
"test" => {
"TESTS" => "t/*.t"
}
);


my %FallbackPrereqs = (
"AI::MXNetCAPI" => "0.02",
"AI::NNVMCAPI" => "0.02",
"Function::Parameters" => "1.0705",
"Mouse" => "2.1.0",
"PDL" => "2.007",
"GraphViz" => "2.14"
);


unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
delete $WriteMakefileArgs{BUILD_REQUIRES};
$WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs;
}

delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
unless eval { ExtUtils::MakeMaker->VERSION(6.52) };

WriteMakefile(%WriteMakefileArgs);



11 changes: 11 additions & 0 deletions perl-package/AI-MXNet/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


This archive contains the distribution AI-MXNet,
version 0.02:

Perl interface to MXNet machine learning library

Copyright (C) 2017 by Sergey Kolychev <sergeykolychev.github@gmail.com>

This library is licensed under Apache 2.0 license https://www.apache.org/licenses/LICENSE-2.0

154 changes: 154 additions & 0 deletions perl-package/AI-MXNet/lib/AI/MXNet.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package AI::MXNet;
use v5.14.0;
use strict;
use warnings;
use AI::MXNet::Base;
use AI::MXNet::Callback;
use AI::MXNet::NDArray;
use AI::MXNet::Symbol;
use AI::MXNet::Executor;
use AI::MXNet::Executor::Group;
use AI::MXNet::Rtc;
use AI::MXNet::Random;
use AI::MXNet::Initializer;
use AI::MXNet::Optimizer;
use AI::MXNet::KVStore;
use AI::MXNet::KVStoreServer;
use AI::MXNet::IO;
use AI::MXNet::Metric;
use AI::MXNet::LRScheduler;
use AI::MXNet::Monitor;
use AI::MXNet::Profiler;
use AI::MXNet::Module::Base;
use AI::MXNet::Module;
use AI::MXNet::Module::Bucketing;
use AI::MXNet::Visualization;
our $VERSION = '0.02';

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;
sub nd { '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 cpu { AI::MXNet::Context->cpu(\$_[1]//0) }
sub gpu { AI::MXNet::Context->gpu(\$_[1]//0) }
sub kv { 'AI::MXNet::KVStore' }
sub io { 'AI::MXNet::IO' }
sub metric { 'AI::MXNet::Metric' }
sub mod { 'AI::MXNet::Module' }
sub viz { 'AI::MXNet::Visualization' }
1;
EOP
eval $short_name_package;
}
}
}

1;
__END__
=encoding UTF-8
=head1 NAME
AI::MXNet - Perl interface to MXNet machine learning library
=head1 SYNOPSIS
## Convolutional NN for recognizing hand-written digits in MNIST dataset
## It's considered "Hello, World" for Neural Networks
## For more info about the MNIST problem please refer to http://neuralnetworksanddeeplearning.com/chap1.html
use strict;
use warnings;
use AI::MXNet qw(mx);
use AI::MXNet::TestUtils qw(GetMNIST_ubyte);
use Test::More tests => 1;
# symbol net
my $batch_size = 100;
### model
my $data = mx->symbol->Variable('data');
my $conv1= mx->symbol->Convolution(data => $data, name => 'conv1', num_filter => 32, kernel => [3,3], stride => [2,2]);
my $bn1 = mx->symbol->BatchNorm(data => $conv1, name => "bn1");
my $act1 = mx->symbol->Activation(data => $bn1, name => 'relu1', act_type => "relu");
my $mp1 = mx->symbol->Pooling(data => $act1, name => 'mp1', kernel => [2,2], stride =>[2,2], pool_type=>'max');
my $conv2= mx->symbol->Convolution(data => $mp1, name => 'conv2', num_filter => 32, kernel=>[3,3], stride=>[2,2]);
my $bn2 = mx->symbol->BatchNorm(data => $conv2, name=>"bn2");
my $act2 = mx->symbol->Activation(data => $bn2, name=>'relu2', act_type=>"relu");
my $mp2 = mx->symbol->Pooling(data => $act2, name => 'mp2', kernel=>[2,2], stride=>[2,2], pool_type=>'max');
my $fl = mx->symbol->Flatten(data => $mp2, name=>"flatten");
my $fc1 = mx->symbol->FullyConnected(data => $fl, name=>"fc1", num_hidden=>30);
my $act3 = mx->symbol->Activation(data => $fc1, name=>'relu3', act_type=>"relu");
my $fc2 = mx->symbol->FullyConnected(data => $act3, name=>'fc2', num_hidden=>10);
my $softmax = mx->symbol->SoftmaxOutput(data => $fc2, name => 'softmax');
# check data
GetMNIST_ubyte();
my $train_dataiter = mx->io->MNISTIter({
image=>"data/train-images-idx3-ubyte",
label=>"data/train-labels-idx1-ubyte",
data_shape=>[1, 28, 28],
batch_size=>$batch_size, shuffle=>1, flat=>0, silent=>0, seed=>10});
my $val_dataiter = mx->io->MNISTIter({
image=>"data/t10k-images-idx3-ubyte",
label=>"data/t10k-labels-idx1-ubyte",
data_shape=>[1, 28, 28],
batch_size=>$batch_size, shuffle=>1, flat=>0, silent=>0});
my $n_epoch = 1;
my $mod = mx->mod->new(symbol => $softmax);
$mod->fit(
$train_dataiter,
eval_data => $val_dataiter,
optimizer_params=>{learning_rate=>0.01, momentum=> 0.9},
num_epoch=>$n_epoch
);
my $res = $mod->score($val_dataiter, mx->metric->create('acc'));
ok($res->{accuracy} > 0.8);
=head1 DESCRIPTION
Perl interface to MXNet machine learning library.
=head1 BUGS AND INCOMPATIBILITIES
Parity with Python inteface is not yet achieved.
Pod mostly contains Python documentation taken as is.
This is WIP.
=head1 SEE ALSO
http://mxnet.io/
https://github.com/sergeykolychev/mxnet/tree/master/perl-package
=head1 AUTHOR
Sergey Kolychev, <sergeykolychev.github@gmail.com>
=head1 COPYRIGHT & LICENSE
Copyright (C) 2017 by Sergey Kolychev <sergeykolychev.github@gmail.com>
This library is licensed under Apache 2.0 license https://www.apache.org/licenses/LICENSE-2.0
=cut
Loading

0 comments on commit 6a0db69

Please sign in to comment.