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

Commit

Permalink
MXNET-1447 [Perl] Runtime features and large tensor support. (#17610)
Browse files Browse the repository at this point in the history
* [Perl] MXNET-1447 Runtime features and large tensor support.

* MXNET-1447 fixed test, corrected module version.

* miscellaneous cleanups.

* added missing file.
  • Loading branch information
sergeykolychev authored Feb 19, 2020
1 parent c2aff58 commit c215b33
Show file tree
Hide file tree
Showing 29 changed files with 915 additions and 205 deletions.
3 changes: 3 additions & 0 deletions perl-package/AI-MXNet/Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Revision history for Perl extension AI::MXNet
1.5 Sun Feb 16 19:56:17 PST 2020
- Runtime features
- INT64 Tensor support

1.4 Mon Feb 18 11:54:07 PST 2019
- Two more gluon loss classes
Expand Down
3 changes: 3 additions & 0 deletions perl-package/AI-MXNet/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ lib/AI/MXNet/NDArray/Base.pm
lib/AI/MXNet/NDArray/Doc.pm
lib/AI/MXNet/NDArray/Slice.pm
lib/AI/MXNet/NDArray/Sparse.pm
lib/AI/MXNet/NS.pm
lib/AI/MXNet/Optimizer.pm
lib/AI/MXNet/Profiler.pm
lib/AI/MXNet/Random.pm
lib/AI/MXNet/RecordIO.pm
lib/AI/MXNet/RNN.pm
lib/AI/MXNet/RNN/Cell.pm
lib/AI/MXNet/RNN/IO.pm
lib/AI/MXNet/RunTime.pm
lib/AI/MXNet/Symbol.pm
lib/AI/MXNet/Symbol/AttrScope.pm
lib/AI/MXNet/Symbol/Base.pm
Expand Down Expand Up @@ -125,6 +127,7 @@ t/test_optimizers.t
t/test_random.t
t/test_recordio.t
t/test_rnn.t
t/test_runtime.t
t/test_sparse_ndarray.t
t/test_symbol.t
t/test_viz.t
14 changes: 10 additions & 4 deletions perl-package/AI-MXNet/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@
},
"runtime" : {
"requires" : {
"AI::MXNetCAPI" : "1.4",
"AI::NNVMCAPI" : "1.3",
"AI::MXNetCAPI" : "1.5",
"AI::NNVMCAPI" : "1.4",
"Function::Parameters" : "1.0705",
"Hash::Ordered" : "0.012",
"GraphViz" : "2.14",
"Mouse" : "v2.1.0",
"PDL" : "2.007",
"PDL::CCS" : "1.23.4"
"PDL::CCS" : "1.23.4",
"Archive::Tar": "0",
"Digest::SHA": "0",
"HTTP::Tiny": "0",
"IO::Zlib": "0",
"JSON::PP": "0",
"parent": "0"
}
},
"test" : {
"requires" : {}
}
},
"release_status" : "stable",
"version" : "1.4"
"version" : "1.5"
}
12 changes: 9 additions & 3 deletions perl-package/AI-MXNet/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ no_index:
- t
- inc
requires:
AI::MXNetCAPI: '1.4'
AI::NNVMCAPI: '1.3'
AI::MXNetCAPI: '1.5'
AI::NNVMCAPI: '1.4'
Function::Parameters: '1.0705'
Hash::Ordered: '0.012'
GraphViz: '2.14'
Mouse: v2.1.0
PDL: '2.007'
PDL::CCS: '1.23.4'
version: '1.4'
Archive::Tar: '0'
Digest::SHA: '0'
HTTP::Tiny: '0'
IO::Zlib: '0'
JSON::PP: '0'
parent: '0'
version: '1.5'
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ my %WriteMakefileArgs = (
"LICENSE" => "apache_2_0",
"NAME" => "AI::MXNet",
"PREREQ_PM" => {
"AI::MXNetCAPI" => "1.4",
"AI::MXNetCAPI" => "1.5",
"AI::NNVMCAPI" => "1.3",
"Function::Parameters" => "1.0705",
"Hash::Ordered" => "0.012",
Expand All @@ -46,7 +46,7 @@ my %WriteMakefileArgs = (
"GraphViz" => "2.14"
},
"TEST_REQUIRES" => {},
"VERSION" => "1.4",
"VERSION" => "1.5",
"test" => {
"TESTS" => "t/*.t"
}
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This archive contains the distribution AI-MXNet,
version 1.4:
version 1.5:

Perl interface to MXNet machine learning library

Expand Down
3 changes: 2 additions & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use AI::MXNet::Module::Base;
use AI::MXNet::Module qw(mod module);
use AI::MXNet::Module::Bucketing;
use AI::MXNet::RNN 'rnn';
use AI::MXNet::RunTime 'runtime';
use AI::MXNet::Visualization 'viz';
use AI::MXNet::RecordIO 'recordio';
use AI::MXNet::Image qw(img image);
Expand All @@ -53,7 +54,7 @@ use AI::MXNet::Gluon 'gluon';
use AI::MXNet::NDArray::Sparse;
use AI::MXNet::Symbol::Sparse;
use AI::MXNet::Engine 'engine';
our $VERSION = '1.4';
our $VERSION = '1.5';

sub cpu { AI::MXNet::Context->cpu($_[1]//0) }
sub cpu_pinned { AI::MXNet::Context->cpu_pinned($_[1]//0) }
Expand Down
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/lib/AI/MXNet/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use warnings;
use PDL;
use PDL::Types ();
use PDL::CCS::Nd;
use AI::MXNetCAPI 1.32;
use AI::NNVMCAPI 1.3;
use AI::MXNetCAPI 1.5;
use AI::NNVMCAPI 1.4;
use AI::MXNet::Types;
use Time::HiRes;
use Scalar::Util qw(blessed);
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/Executor/Group.pm
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ sub BUILD
$self->bind_exec($self->data_shapes, $self->label_shapes, $self->shared_group);
}

=decide_slices
=head2 decide_slices
Decide the slices for each context according to the workload.
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/Gluon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ sub model_zoo { require AI::MXNet::Gluon::ModelZoo; 'AI::MXNet::Gluon::ModelZoo'
my $children_outputs = [
map { $self->forward($F, $inputs, $_) @{ $tree->children }
];
#Recursively builds the neural network based on each input sentence’s
#Recursively builds the neural network based on each input sentence
#syntactic structure during the model definition and training process
...
}
Expand Down
8 changes: 4 additions & 4 deletions perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Data/Vision.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extends 'AI::MXNet::Gluon::Data::Vision::DownloadedDataSet';
Whether to load the training or testing set.
Defaults to True
transform : function
A user defined callback that transforms each instance. For example
A user defined callback that transforms each instance. For example:
transform => sub { my ($data, $label) = @_; return ($data->astype('float32')/255, $label) }
=cut
Expand Down Expand Up @@ -172,7 +172,7 @@ use Mouse;
Whether to load the training or testing set.
Defaults to True
transform : function
A user defined callback that transforms each instance. For example
A user defined callback that transforms each instance. For example:
transform => sub { my ($data, $label) = @_; return ($data->astype('float32')/255, $label) }
=cut
Expand Down Expand Up @@ -306,7 +306,7 @@ extends 'AI::MXNet::Gluon::Data::RecordFileSet';
If 1, always convert images to colored (RGB).
transform : function
A user defined callback that transforms each instance. For example::
A user defined callback that transforms each instance. For example:
=cut
has 'flag' => (is => 'rw', isa => 'Bool', default => 1);
has 'transform' => (is => 'rw', isa => 'Maybe[CodeRef]');
Expand Down Expand Up @@ -421,7 +421,7 @@ method at(Int $idx)
my $label = $self->items->[$idx][1];
if(defined $self->transform)
{
return [$self->transform($img, $label)];
return [$self->transform->($img, $label)];
}
return [$img, $label];
}
Expand Down
4 changes: 3 additions & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use AI::MXNet::Function::Parameters;
use Scalar::Util qw/blessed/;
use JSON::PP;

=encoding utf-8
=head1 NAME
AI::MXNet::Metric - Evaluation Metric API.
Expand Down Expand Up @@ -295,7 +297,7 @@ method update(ArrayRef[AI::MXNet::NDArray] $labels, ArrayRef[AI::MXNet::NDArray]
If top_k = 1, then TopKAccuracy is identical to Accuracy.
Parameters:
Parameters:
top_k(Int, default 1) – Whether targets are in top k predictions.
name (Str, default 'top_k_accuracy') – Name of this metric instance for display.
Expand Down
27 changes: 22 additions & 5 deletions perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package AI::MXNet::NDArray;
An NDArray represents a multidimensional, fixed-size homogenous array.
If you're familiar with the PDL, you might notice some similarities.
However, NDArray is row-major, unlike the PDL that is column-major.
Like the PDL, MXNets NDArray enables imperative computation.
Like the PDL, MXNet's NDArray enables imperative computation.
Some NDArray advandages compared to PDL:
MXNet's NDArray supports fast execution on a wide range of hardware configurations, including CPU, GPU, and multi-GPU machines.
Expand Down Expand Up @@ -63,6 +63,7 @@ use AI::MXNet::NS;
use AI::MXNet::Base;
use AI::MXNet::NDArray::Slice;
use AI::MXNet::Context;
use AI::MXNet::RunTime;
use Mouse;
use AI::MXNet::Function::Parameters;
use overload (
Expand Down Expand Up @@ -384,8 +385,11 @@ method _slice (
)
{
confess("start $start > stop $stop") if $start > $stop;
my $sub = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE')
? \&AI::MXNetCAPI::NDArraySlice64
: \&AI::MXNetCAPI::NDArraySlice;
my $handle = check_call(
AI::MXNetCAPI::NDArraySlice(
$sub->(
$self->handle,
$start,
$stop
Expand All @@ -407,8 +411,11 @@ method _slice (

method _at(Index $idx)
{
my $sub = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE')
? \&AI::MXNetCAPI::NDArrayAt64
: \&AI::MXNetCAPI::NDArrayAt;
my $handle = check_call(
AI::MXNetCAPI::NDArrayAt(
$sub->(
$self->handle, $idx >=0 ? $idx : $self->shape->[0] + $idx
)
);
Expand Down Expand Up @@ -549,7 +556,14 @@ method wait_to_read()

method shape()
{
return scalar(check_call(AI::MXNetCAPI::NDArrayGetShapeEx($self->handle)));
if(AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE'))
{
return [map { $_ + 0 } @{ scalar(check_call(AI::MXNetCAPI::NDArrayGetShapeEx64($self->handle))) }];
}
else
{
return scalar(check_call(AI::MXNetCAPI::NDArrayGetShapeEx($self->handle)));
}
}

=head2 size
Expand Down Expand Up @@ -1464,8 +1478,11 @@ sub _new_empty_handle

func _new_alloc_handle($shape, $ctx, $delay_alloc, $dtype)
{
my $sub = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE')
? \&AI::MXNetCAPI::NDArrayCreateEx64
: \&AI::MXNetCAPI::NDArrayCreateEx;
my $hdl = check_call(
AI::MXNetCAPI::NDArrayCreateEx(
$sub->(
$shape,
scalar(@$shape),
$ctx->device_type_id,
Expand Down
6 changes: 5 additions & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/NDArray/Sparse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use strict;
use warnings;
use AI::MXNet::Base;
use AI::MXNet::Function::Parameters;
use AI::MXNet::RunTime;
use Mouse;
extends 'AI::MXNet::NDArray';

Expand Down Expand Up @@ -51,8 +52,11 @@ method _new_alloc_handle(
my $aux_shape_lens = [map { scalar(@$_) } @$aux_shapes];
@$aux_shapes = map { @$_ } @$aux_shapes;
my $num_aux = @{ $aux_types };
my $sub = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE')
? \&AI::MXNetCAPI::NDArrayCreateSparseEx64
: \&AI::MXNetCAPI::NDArrayCreateSparseEx;
my $handle = check_call(
AI::MXNetCAPI::NDArrayCreateSparseEx(
$sub->(
STORAGE_TYPE_STR_TO_ID->{$stype},
$shape,
scalar(@$shape),
Expand Down
Loading

0 comments on commit c215b33

Please sign in to comment.