sergeykolychev closed pull request #10460: MXNET-295 [Perl] Added support for 
linalg operators.
URL: https://github.com/apache/incubator-mxnet/pull/10460
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/docs/api/perl/index.md b/docs/api/perl/index.md
index 1669d2283d2..6408b525f00 100644
--- a/docs/api/perl/index.md
+++ b/docs/api/perl/index.md
@@ -60,10 +60,11 @@ pdl> print mx->nd->array(sequence(2,3))->aspdl ## 3 rows, 2 
columns
  [2 3]
  [4 5]
 ]
+```
 
 Export/import to/from sparse MXNet tensors are supported via 
[PDL::CCS](https://metacpan.org/release/PDL-CCS).
 Please check out the examples directory for the examples on how to use the 
sparse matrices.
-```
+
  ## Perl API Reference
  * [Module API is a flexible high-level interface for training neural 
networks.](module.md)
  * [Symbolic API performs operations on NDArrays to assemble neural networks 
from layers.](symbol.md)
diff --git a/perl-package/AI-MXNet/Changes b/perl-package/AI-MXNet/Changes
index ddf6b3ad355..4234ea538d4 100644
--- a/perl-package/AI-MXNet/Changes
+++ b/perl-package/AI-MXNet/Changes
@@ -1,5 +1,8 @@
 Revision history for Perl extension AI::MXNet
 
+1.21    Sun Apr  8 12:08:44 PDT 2018
+        - Support for linear algebra operations on symbols and ndarrays.
+
 1.2     Sun Mar  4 16:29:19 PST 2018
         - Support for sparse tensors
 
diff --git a/perl-package/AI-MXNet/MANIFEST b/perl-package/AI-MXNet/MANIFEST
index 690df1ef241..09e830585e1 100644
--- a/perl-package/AI-MXNet/MANIFEST
+++ b/perl-package/AI-MXNet/MANIFEST
@@ -50,6 +50,9 @@ lib/AI/MXNet/Initializer.pm
 lib/AI/MXNet/IO.pm
 lib/AI/MXNet/KVStore.pm
 lib/AI/MXNet/KVStoreServer.pm
+lib/AI/MXNet/LinAlg.pm
+lib/AI/MXNet/LinAlg/NDArray.pm
+lib/AI/MXNet/LinAlg/Symbol.pm
 lib/AI/MXNet/Logging.pm
 lib/AI/MXNet/LRScheduler.pm
 lib/AI/MXNet/Metric.pm
diff --git a/perl-package/AI-MXNet/META.json b/perl-package/AI-MXNet/META.json
index a37d407f134..d6987cb6fdf 100644
--- a/perl-package/AI-MXNet/META.json
+++ b/perl-package/AI-MXNet/META.json
@@ -45,5 +45,5 @@
       }
    },
    "release_status" : "stable",
-   "version" : "1.2"
+   "version" : "1.21"
 }
diff --git a/perl-package/AI-MXNet/META.yml b/perl-package/AI-MXNet/META.yml
index 8505cefef4a..30192dedb0c 100644
--- a/perl-package/AI-MXNet/META.yml
+++ b/perl-package/AI-MXNet/META.yml
@@ -25,4 +25,4 @@ requires:
   Mouse: v2.1.0
   PDL: '2.007'
   PDL::CCS: '1.23.4'
-version: '1.2'
+version: '1.21'
diff --git a/perl-package/AI-MXNet/Makefile.PL 
b/perl-package/AI-MXNet/Makefile.PL
index 731b6938056..63e0391daf2 100644
--- a/perl-package/AI-MXNet/Makefile.PL
+++ b/perl-package/AI-MXNet/Makefile.PL
@@ -46,7 +46,7 @@ my %WriteMakefileArgs = (
     "GraphViz" => "2.14"
   },
   "TEST_REQUIRES" => {},
-  "VERSION" => "1.2",
+  "VERSION" => "1.21",
   "test" => {
     "TESTS" => "t/*.t"
   }
diff --git a/perl-package/AI-MXNet/README b/perl-package/AI-MXNet/README
index 59d1dd030be..ff757c1d731 100644
--- a/perl-package/AI-MXNet/README
+++ b/perl-package/AI-MXNet/README
@@ -1,5 +1,5 @@
 This archive contains the distribution AI-MXNet,
-version 1.2:
+version 1.21:
 
   Perl interface to MXNet machine learning library
 
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet.pm
index 87d0b6d4974..c7322a9e955 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet.pm
@@ -44,12 +44,13 @@ use AI::MXNet::Visualization;
 use AI::MXNet::RecordIO;
 use AI::MXNet::Image;
 use AI::MXNet::Contrib;
+use AI::MXNet::LinAlg;
 use AI::MXNet::CachedOp;
 use AI::MXNet::AutoGrad;
 use AI::MXNet::Gluon;
 use AI::MXNet::NDArray::Sparse;
 use AI::MXNet::Symbol::Sparse;
-our $VERSION = '1.2';
+our $VERSION = '1.21';
 
 sub import
 {
@@ -63,6 +64,7 @@ sub import
             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' }
@@ -88,6 +90,7 @@ sub import
             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 name { '$short_name' }
             sub rtc { '$short_name' }
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm
index 2a6e18e1405..9f6a0ab0160 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/Contrib.pm
@@ -24,5 +24,6 @@ use AI::MXNet::Contrib::NDArray;
 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/Gluon/NN/BasicLayers.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/NN/BasicLayers.pm
index 6d85c9abf71..6ef4714992a 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
@@ -42,7 +42,7 @@ use AI::MXNet::Function::Parameters;
 use AI::MXNet::Gluon::Mouse;
 extends 'AI::MXNet::Gluon::Block';
 
-=head
+=head2
 
     Adds block on top of the stack.
 =cut
@@ -104,7 +104,7 @@ package AI::MXNet::Gluon::NN::HybridSequential;
 use AI::MXNet::Gluon::Mouse;
 extends 'AI::MXNet::Gluon::HybridBlock';
 
-=head
+=head2
 
     Adds block on top of the stack.
 =cut
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 f56f8f5d333..502f5226ae4 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
@@ -15,6 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
+use strict;
+use warnings;
 package AI::MXNet::Gluon::NN::Conv;
 use AI::MXNet::Function::Parameters;
 use AI::MXNet::Symbol;
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 d241aa196a9..131a6ab6723 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/Gluon/Parameter.pm
@@ -665,7 +665,7 @@ method _get_impl($name)
     return undef;
 }
 
-=head get
+=head2 get
 
         Retrieves a 'AI::MXNet::Gluon::Parameter' with name 
'$self->prefix.$name'. If not found,
         'get' will first try to retrieve it from 'shared' dict. If still not
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm
new file mode 100644
index 00000000000..9290e68d456
--- /dev/null
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg.pm
@@ -0,0 +1,29 @@
+# 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::LinAlg;
+use strict;
+use warnings;
+use AI::MXNet::LinAlg::Symbol;
+use AI::MXNet::LinAlg::NDArray;
+
+sub sym    { 'AI::MXNet::LinAlg::Symbol'  }
+sub symbol { 'AI::MXNet::LinAlg::Symbol'  }
+sub nd     { 'AI::MXNet::LinAlg::NDArray' }
+sub ndarray { 'AI::MXNet::LinAlg::NDArray' }
+
+1;
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/NDArray.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/NDArray.pm
new file mode 100644
index 00000000000..0cc7f7f938a
--- /dev/null
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/NDArray.pm
@@ -0,0 +1,30 @@
+# 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::LinAlg::NDArray;
+use strict;
+use warnings;
+
+sub AUTOLOAD {
+    my $sub = $AI::MXNet::LinAlg::NDArray::AUTOLOAD;
+    $sub =~ s/.*:://;
+    $sub = "_linalg_$sub";
+    shift;
+    return AI::MXNet::NDArray->$sub(@_);
+}
+
+1;
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/Symbol.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/Symbol.pm
new file mode 100644
index 00000000000..377f3936a61
--- /dev/null
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/LinAlg/Symbol.pm
@@ -0,0 +1,30 @@
+# 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::LinAlg::Symbol;
+use strict;
+use warnings;
+
+sub AUTOLOAD {
+    my $sub = $AI::MXNet::LinAlg::Symbol::AUTOLOAD;
+    $sub =~ s/.*:://;
+    $sub = "_linalg_$sub";
+    shift;
+    return AI::MXNet::Symbol->$sub(@_);
+}
+
+1;
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm
index 62e64d259a0..d5c874bf08f 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/NDArray.pm
@@ -1599,5 +1599,6 @@ EOV
 sub contrib { 'AI::MXNet::Contrib::NDArray' }
 sub random  { 'AI::MXNet::Random' }
 sub sparse  { 'AI::MXNet::NDArray::Sparse' }
+sub linalg  { 'AI::MXNet::LinAlg::NDArray' }
 
 __PACKAGE__->meta->make_immutable;
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm
index 299aed7492e..61bf2cb5ff0 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/Symbol.pm
@@ -1498,5 +1498,6 @@ sub  _ufunc_helper
 sub contrib { 'AI::MXNet::Contrib::Symbol' }
 sub random  { 'AI::MXNet::Symbol::Random' }
 sub sparse  { 'AI::MXNet::Symbol::Sparse' }
+sub linalg  { 'AI::MXNet::LinAlg::Symbol' }
 
 1;
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm 
b/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm
index 37ef5b675f5..c04b79c5e4d 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/TestUtils.pm
@@ -29,7 +29,7 @@ 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);
+                                      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/t/test_ndarray.t 
b/perl-package/AI-MXNet/t/test_ndarray.t
index a3d7b6e1c10..4e394f53998 100644
--- a/perl-package/AI-MXNet/t/test_ndarray.t
+++ b/perl-package/AI-MXNet/t/test_ndarray.t
@@ -18,8 +18,8 @@
 use strict;
 use warnings;
 use AI::MXNet qw(mx);
-use AI::MXNet::TestUtils qw(almost_equal same);
-use Test::More tests => 17;
+use AI::MXNet::TestUtils qw(almost_equal same pdl);
+use Test::More tests => 19;
 
 sub test_ndarray_reshape
 {
@@ -139,8 +139,29 @@ sub test_ndarray_slice
     ok(($a->slice([mx->nd->array([1, 1, 0]), mx->nd->array([0, 1, 0])])->aspdl 
== mx->nd->array([2, 3, 0])->aspdl)->all);
 }
 
+sub test_linalg_gemm2
+{
+    # Single matrix multiply
+    my $A = mx->nd->array([[1.0, 1.0], [1.0, 1.0]]);
+    my $B = mx->nd->array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]);
+    ok(almost_equal(
+        mx->nd->linalg->gemm2($A, $B, transpose_b=>1, alpha=>2.0)->aspdl,
+        pdl([[4.0, 4.0, 4.0], [4.0, 4.0, 4.0]])
+    ));
+
+    # Batch matrix multiply
+    $A = mx->nd->array([[[1.0, 1.0]], [[0.1, 0.1]]]);
+    $B = mx->nd->array([[[1.0, 1.0]], [[0.1, 0.1]]]);
+    ok(almost_equal(
+        mx->nd->linalg->gemm2($A, $B, transpose_b=>1, alpha=>2.0)->aspdl,
+        pdl([[[4.0]], [[0.04]]])
+    ));
+}
+
 test_ndarray_slice();
 test_ndarray_reshape();
 test_moveaxis();
 test_output();
 test_cached();
+test_linalg_gemm2();
+
diff --git a/perl-package/AI-MXNet/t/test_symbol.t 
b/perl-package/AI-MXNet/t/test_symbol.t
index 0788241adab..db010e843eb 100644
--- a/perl-package/AI-MXNet/t/test_symbol.t
+++ b/perl-package/AI-MXNet/t/test_symbol.t
@@ -17,9 +17,9 @@
 
 use strict;
 use warnings;
-use Test::More tests => 98;
+use Test::More tests => 100;
 use AI::MXNet qw(mx);
-use AI::MXNet::TestUtils qw(mlp2 conv check_consistency zip assert enumerate);
+use AI::MXNet::TestUtils qw(mlp2 conv check_consistency zip assert enumerate 
almost_equal);
 use Storable qw(freeze thaw);
 use PDL;
 
@@ -238,6 +238,33 @@ sub test_load_000800
 
 test_load_000800();
 
+sub test_linalg_gemm2
+{
+    # Single matrix multiply
+    my $sym_gemm2 = mx->sym->linalg->gemm2(
+        mx->sym->var('A'),
+        mx->sym->var('B'),
+        transpose_b => 1,
+        alpha => 2.0
+    );
+    my $A = mx->nd->array([[1.0, 1.0], [1.0, 1.0]]);
+    my $B = mx->nd->array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]);
+    ok(almost_equal(
+        $sym_gemm2->eval(args => { A => $A, B => $B })->[0]->aspdl,
+        pdl([[4.0, 4.0, 4.0], [4.0, 4.0, 4.0]])
+    ));
+
+    # Batch matrix multiply
+    $A = mx->nd->array([[[1.0, 1.0]], [[0.1, 0.1]]]);
+    $B = mx->nd->array([[[1.0, 1.0]], [[0.1, 0.1]]]);
+    ok(almost_equal(
+        $sym_gemm2->eval(args => { A => $A, B => $B })->[0]->aspdl,
+        pdl([[[4.0]], [[0.04]]])
+    ));
+}
+
+test_linalg_gemm2();
+
 __DATA__
 {
   "nodes": [


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to