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

MXNET-1447 [Perl] Runtime features and large tensor support. #17610

Merged
merged 7 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions perl-package/AI-MXNet/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ 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 +126,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
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"runtime" : {
"requires" : {
"AI::MXNetCAPI" : "1.4",
"AI::MXNetCAPI" : "1.5",
"AI::NNVMCAPI" : "1.3",
"Function::Parameters" : "1.0705",
"Hash::Ordered" : "0.012",
Expand All @@ -45,5 +45,5 @@
}
},
"release_status" : "stable",
"version" : "1.4"
"version" : "1.5"
}
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ no_index:
- t
- inc
requires:
AI::MXNetCAPI: '1.4'
AI::MXNetCAPI: '1.5'
AI::NNVMCAPI: '1.3'
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'
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
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/lib/AI/MXNet/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use warnings;
use PDL;
use PDL::Types ();
use PDL::CCS::Nd;
use AI::MXNetCAPI 1.32;
use AI::MXNetCAPI 1.4;
use AI::NNVMCAPI 1.3;
use AI::MXNet::Types;
use Time::HiRes;
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
25 changes: 21 additions & 4 deletions perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm
Original file line number Diff line number Diff line change
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little awkward to put the caller in charge of checking the INT64_TENSOR_SIZE compile setting. Would it make sense to make AI::MXNetCAPI::NDArraySlice() and friends responsible for checking the flag and deferring to the NDArray*64() variants when appropriate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tlby On python side the switch is made in the caller space. The c api has two functions defined for each task, one with (u)?int32_t input/output and another with (u)?int64_t
I don't know how to combine these two functions via the swig space.
Looks like a task for you to complete later the line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's how things are done in Python, then no need to diverge from this pattern. Thanks!

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
116 changes: 116 additions & 0 deletions perl-package/AI-MXNet/lib/AI/MXNet/RunTime.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# 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::RunTime;
use strict;
use warnings;
use AI::MXNet::NS;
use AI::MXNet::Base;
use AI::MXNet::Function::Parameters;
use Mouse;

=head1 NAME

AI::MXNet::RunTime - Runtime querying of compile time features in the native library.
=cut

=head1 DESCRIPTION

With this module you can check at runtime which libraries and features were compiled in the library.

Example usage:

use AI::MXNet qw(mx);
my $features = mx->runtime->Features();

print $features->is_enabled("CUDNN");
0

print $features->is_enabled("CPU_SSE");
1

print Dumper($features->features);
$VAR1 = {
'LAPACK' => 1,
'F16C' => 1,
'CPU_SSE2' => 1,
'BLAS_MKL' => 0,
'CXX14' => 0,
'DIST_KVSTORE' => 0,
'NCCL' => 0,
'OPENMP' => 1,
'CUDNN' => 0,
'CPU_AVX' => 1,
'CUDA_RTC' => 0,
'BLAS_OPEN' => 1,
'CPU_SSE4_2' => 1,
'CPU_SSE4A' => 0,
'TVM_OP' => 0,
'MKLDNN' => 0,
'TENSORRT' => 0,
'JEMALLOC' => 1,
'SSE' => 0,
'PROFILER' => 0,
'DEBUG' => 0,
'BLAS_APPLE' => 0,
'CPU_SSE3' => 1,
'INT64_TENSOR_SIZE' => 0,
'CPU_SSE4_1' => 1,
'CUDA' => 0,
'OPENCV' => 1,
'CPU_SSE' => 1,
'SIGNAL_HANDLER' => 0,
'BLAS_ATLAS' => 0,
'CAFFE' => 0,
'CPU_AVX2' => 0
};

print $features;
[✖ CUDA, ✖ CUDNN, ✖ NCCL, ✖ CUDA_RTC, ✖ TENSORRT, ✔ CPU_SSE, ✔ CPU_SSE2, ✔ CPU_SSE3,
✔ CPU_SSE4_1, ✔ CPU_SSE4_2, ✖ CPU_SSE4A, ✔ CPU_AVX, ✖ CPU_AVX2, ✔ OPENMP, ✖ SSE,
✔ F16C, ✔ JEMALLOC, ✔ BLAS_OPEN, ✖ BLAS_ATLAS, ✖ BLAS_MKL, ✖ BLAS_APPLE, ✔ LAPACK,
✖ MKLDNN, ✔ OPENCV, ✖ CAFFE, ✖ PROFILER, ✖ DIST_KVSTORE, ✖ CXX14, ✖ INT64_TENSOR_SIZE,
✔ SIGNAL_HANDLER, ✔ DEBUG, ✖ TVM_OP]

=cut
use overload '""' => sub {
my $self = shift;
my $s = join(', ', map {
sprintf("%s %s", $self->features->{ $_ } ? '✔' : '✖', $_)
} sort keys %{ $self->features });
return "[$s]";
};

has 'features' => (is => 'rw', init_arg => undef, default => sub {
return scalar(check_call(AI::MXNetCAPI::LibInfoFeatures()));
});

method is_enabled(Str $feature)
{
confess("Feature $feature does not exist")
unless exists $self->features->{ $feature };
return $self->features->{ $feature };
}

my $features;
method Features()
{
$features //= __PACKAGE__->new;
return $features;
}

1;
17 changes: 15 additions & 2 deletions perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use AI::MXNet::NS;
use AI::MXNet::Base;
use AI::MXNet::Symbol::Base;
use AI::MXNet::Symbol::Random;
use AI::MXNet::RunTime;
use AI::MXNet::Types;
use Mouse;
use AI::MXNet::Function::Parameters;
Expand Down Expand Up @@ -663,7 +664,16 @@ method _infer_shape_impl(Maybe[Str|Shape] @args)
push @{ $indptr }, scalar(@{ $sdata });
}
}
my $infer_func = $partial ? \&AI::MXNetCAPI::SymbolInferShapePartialEx : \&AI::MXNetCAPI::SymbolInferShapeEx;
my $is64 = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE');
my $infer_func = $partial
? (
$is64 ? \&AI::MXNetCAPI::SymbolInferShapePartialEx64
: \&AI::MXNetCAPI::SymbolInferShapePartialEx
)
: (
$is64 ? \&AI::MXNetCAPI::SymbolInferShapeEx64
: \&AI::MXNetCAPI::SymbolInferShapeEx
);
my ($arg_shapes, $out_shapes, $aux_shapes, $complete) = check_call(
$infer_func->(
$self->handle,
Expand Down Expand Up @@ -934,11 +944,14 @@ method simple_bind(
$aux_state_handles,
$exe_handle
);
my $sub = AI::MXNet::RunTime->Features()->is_enabled('INT64_TENSOR_SIZE')
? \&AI::MXNetCAPI::ExecutorSimpleBindEx64
: \&AI::MXNetCAPI::ExecutorSimpleBindEx;
eval {
($updated_shared_data, $in_arg_handles, $arg_grad_handles, $aux_state_handles, $exe_handle)
=
check_call(
AI::MXNetCAPI::ExecutorSimpleBindEx(
$sub->(
$self->handle,
$ctx->device_type_id,
$ctx->device_id,
Expand Down
Loading