Skip to content

Commit

Permalink
[Perl] Updates mapping between PDL and MX types
Browse files Browse the repository at this point in the history
This removes the hardcoded values and uses PDL::Type instead.

Upgrades minimum PDL dependency to PDL v2.064 which provides
the int8 (PDL: sbyte) type.

Fix tests by allowing some PDL data alongside Perl scalars.

Fixes <apache#20851>.
  • Loading branch information
zmughal committed Jan 28, 2022
1 parent ac4c8dc commit 8afffae
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ci/docker/install/ubuntu_perl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ set -ex
# install libraries for mxnet's perl package on ubuntu
apt-get update || true
apt-get install -y libmouse-perl pdl cpanminus swig libgraphviz-perl
cpanm -q Function::Parameters Hash::Ordered CHM/PDL-2.019.tar.gz PDL::CCS
cpanm -q Function::Parameters Hash::Ordered PDL~2.064 PDL::CCS
2 changes: 1 addition & 1 deletion docker/install/perl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

# install libraries for mxnet's perl package on ubuntu
apt-get update && apt-get install -y libmouse-perl pdl cpanminus swig libgraphviz-perl
cpanm -q Function::Parameters Hash::Ordered PDL::CCS
cpanm -q Function::Parameters Hash::Ordered PDL~2.064 PDL::CCS
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/META.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"Hash::Ordered" : "0.012",
"GraphViz" : "2.14",
"Mouse" : "v2.1.0",
"PDL" : "2.007",
"PDL" : "2.064",
"PDL::CCS" : "1.23.4",
"Archive::Tar": "0",
"Digest::SHA": "0",
Expand Down
2 changes: 1 addition & 1 deletion perl-package/AI-MXNet/META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ requires:
Hash::Ordered: '0.012'
GraphViz: '2.14'
Mouse: v2.1.0
PDL: '2.007'
PDL: '2.064'
PDL::CCS: '1.23.4'
Archive::Tar: '0'
Digest::SHA: '0'
Expand Down
4 changes: 2 additions & 2 deletions perl-package/AI-MXNet/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ my %WriteMakefileArgs = (
"Function::Parameters" => "1.0705",
"Hash::Ordered" => "0.012",
"Mouse" => "v2.1.0",
"PDL" => "2.007",
"PDL" => "2.064",
"PDL::CCS" => "1.23.4",
"GraphViz" => "2.14"
},
Expand All @@ -59,7 +59,7 @@ my %FallbackPrereqs = (
"Function::Parameters" => "1.0705",
"Hash::Ordered" => "0.012",
"Mouse" => "v2.1.0",
"PDL" => "2.007",
"PDL" => "2.064",
"PDL::CCS" => "1.23.4",
"GraphViz" => "2.14"
);
Expand Down
39 changes: 20 additions & 19 deletions perl-package/AI-MXNet/lib/AI/MXNet/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,28 @@ use constant DTYPE_MX_TO_STR => {
6 => 'int64'
};
use constant DTYPE_MX_TO_PDL => {
0 => 6,
1 => 7,
2 => 6,
3 => 0,
4 => 3,
5 => 0,
6 => 5,
float32 => 6,
float64 => 7,
float16 => 6,
uint8 => 0,
int32 => 3,
int8 => 0,
int64 => 5
0 => PDL::Type->new('float')->enum,
1 => PDL::Type->new('double')->enum,
2 => PDL::Type->new('float')->enum,
3 => PDL::Type->new('byte')->enum,
4 => PDL::Type->new('long')->enum,
5 => PDL::Type->new('sbyte')->enum,
6 => PDL::Type->new('longlong')->enum,
float32 => PDL::Type->new('float')->enum,
float64 => PDL::Type->new('double')->enum,
float16 => PDL::Type->new('float')->enum,
uint8 => PDL::Type->new('byte')->enum,
int32 => PDL::Type->new('long')->enum,
int8 => PDL::Type->new('sbyte')->enum,
int64 => PDL::Type->new('longlong')->enum,
};
use constant DTYPE_PDL_TO_MX => {
6 => 0,
7 => 1,
0 => 3,
3 => 4,
5 => 6
PDL::Type->new('float')->enum => 0,
PDL::Type->new('double')->enum => 1,
PDL::Type->new('byte')->enum => 3,
PDL::Type->new('long')->enum => 4,
PDL::Type->new('sbyte')->enum => 5,
PDL::Type->new('longlong')->enum => 6,
};
use constant DTYPE_MX_TO_PERL => {
0 => 'f',
Expand Down
10 changes: 5 additions & 5 deletions perl-package/AI-MXNet/lib/AI/MXNet/Metric.pm
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use overload '""' => sub {
has 'name' => (is => 'rw', isa => 'Str');
has 'num' => (is => 'rw', isa => 'Int');
has 'num_inst' => (is => 'rw', isa => 'Maybe[Int|ArrayRef[Int]]');
has 'sum_metric' => (is => 'rw', isa => 'Maybe[Num|ArrayRef[Num]]');
has 'sum_metric' => (is => 'rw', isa => 'Maybe[Num|ArrayRef[Num]|PDL]');
has '_kwargs' => (is => 'rw', init_arg => undef);
around BUILDARGS => \&AI::MXNet::Base::process_arguments;

Expand Down Expand Up @@ -168,8 +168,8 @@ method get()
method get_name_value()
{
my ($name, $value) = $self->get;
$name = [$name] unless ref $name;
$value = [$value] unless ref $value;
$name = [$name] unless ref $name eq 'ARRAY';
$value = [$value] unless ref $value eq 'ARRAY';
my %ret;
@ret{ @$name } = @$value;
return \%ret;
Expand Down Expand Up @@ -221,8 +221,8 @@ method get()
for my $metric (@{ $self->metrics })
{
my ($name, $result) = $metric->get;
$name = [$name] unless ref $name;
$result = [$result] unless ref $result;
$name = [$name] unless ref $name eq 'ARRAY';
$result = [$result] unless ref $result eq 'ARRAY';
push @$names, @$name;
push @$results, @$result;
}
Expand Down

0 comments on commit 8afffae

Please sign in to comment.