eric-haibin-lin commented on a change in pull request #9988: [Perl] Sparse 
feature.
URL: https://github.com/apache/incubator-mxnet/pull/9988#discussion_r173079707
 
 

 ##########
 File path: perl-package/AI-MXNet/lib/AI/MXNet/NDArray/Sparse.pm
 ##########
 @@ -0,0 +1,1342 @@
+# 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::NDArray::Sparse;
+use strict;
+use warnings;
+use AI::MXNet::Base;
+use AI::MXNet::Function::Parameters;
+use Mouse;
+extends 'AI::MXNet::NDArray';
+
+=head1 NAME
+
+    AI::MXNet::NDArray::Sparse - Sparse NDArray API of MXNet
+=cut
+
+=head1 DESCRIPTION
+
+    The base class of an NDArray stored in a sparse storage format.
+    See AI::MXNet::NDArray::CSR and AI::MXNet::NDArray::RowSparse for more 
details.
+=cut
+
+method _new_alloc_handle(
+    Stype                    $stype,
+    Shape                    $shape,
+    AI::MXNet::Context       $ctx,
+    Bool                     $delay_alloc,
+    Dtype                    $dtype,
+    AuxTypes                 $aux_types,
+    Maybe[ArrayRef[Shape]]   $aux_shapes=
+)
+{
+    confess("only int64 is supported for aux types")
+        if (grep { $_ ne 'int64' } @$aux_types);
+    my $aux_type_ids = [map { DTYPE_STR_TO_MX->{$_} } @$aux_types];
+    $aux_shapes //= [map { [0] } @$aux_types];
+    my $aux_shape_lens = [map { scalar(@$_) } @$aux_shapes];
+    @$aux_shapes = map { @$_ } @$aux_shapes;
+    my $num_aux = @{ $aux_types };
+    my $handle = check_call(
+        AI::MXNetCAPI::NDArrayCreateSparseEx(
+            STORAGE_TYPE_STR_TO_ID->{$stype},
+            $shape,
+            scalar(@$shape),
+            $ctx->device_type_id,
+            $ctx->device_id,
+            $delay_alloc,
+            DTYPE_STR_TO_MX->{$dtype},
+            scalar(@$aux_types),
+            $aux_type_ids,
+            $aux_shape_lens,
+            $aux_shapes
+        )
+    );
+}
+
+method _class_name()
+{
+    my $class = ref $self || $self;
+    $class;
+}
+
+sub not_implemented { confess "Not implemented" }
+use overload '""' => sub {
+                        my $self = shift;
+                        my $shape_info = join('x', @{ $self->shape });
+                        sprintf("\n<%s, %s @%s>", $self->_class_name, 
$shape_info, $self->context);
+                     },
+             '+=' => \&not_implemented,
+             '-=' => \&not_implemented,
+             '*=' => \&not_implemented,
+             '/=' => \&not_implemented;
+{
+    no warnings 'redefine';
+    *_sync_copyfrom = *_at = *_slice = *reshape = *size = \&not_implemented;
+}
+
+method _aux_type(Int $i)
+{
+    return DTYPE_MX_TO_STR->{
+        check_call(
+            AI::MXNetCAPI::NDArrayGetAuxType(
+                $self->handle, $i
+            )
+        )
+    }
+}
+
+method _num_aux()
+{
+    return scalar(@{ STORAGE_AUX_TYPES->{ $self->stype } });
+}
+
+method _aux_types()
+{
+    [map { $self->_aux_type($_) } 0..$self->_num_aux-1];
+}
+
+=head2 aspdl
+
+    Return a dense PDL object with value copied from this array
+=cut
+
+method aspdl()
+{
+    return $self->tostype('default')->aspdl;
+}
+
+=head2 astype
+
+        Returns a copy of the array after casting to a specified type.
+        Parameters
+        ----------
+        dtype : Dtype
+            The type of the returned array.
+        Examples
+        --------
+        >>> $x = mx->nd->sparse->zeros('row_sparse', [2,3], dtype=>'float32')
+        >>> $y = $x->astype('int32')
+        >>> $y->dtype
+        <type 'int32'>
+=cut
+
+method astype(Dtype $dtype)
+{
+    my $res = $self->zeros(
+        $self->stype, $self->shape, ctx => $self->context,
+        dtype => $dtype
+    );
+    $self->copyto($res);
+    return $res;
+}
+
+=head2 copyto
+
+        Copies the value of this array to another array.
+
+        Parameters
+        ----------
+        other : NDArray or NDArray::CSR or NDArray::RowSparse or Context
+            The destination array or context.
+
+        Returns
+        -------
+        NDArray or CSRNDArray::CSR or NDArray::RowSparse
+            The copied array.
+=cut
+
+method copyto(AI::MXNet::NDArray|AI::MXNet::Context $other)
+{
+    if($other->isa('AI::MXNet::NDArray'))
+    {
+        if($self->handle eq $other->handle)
+        {
+            Carp::cluck('You are attempting to copy an array to itself');
+            return;
+        }
+        else
+        {
+            return __PACKAGE__->_copyto($self, out => $other);
+        }
+    }
+    elsif($other->isa('AI::MXNet::Context'))
+    {
+        my $hret = __PACKAGE__->_ndarray_cls(
+            __PACKAGE__->_new_alloc_handle(
+                $self->stype, $self->shape, $other, 1, $self->dtype, 
$self->_aux_types
+            )
+        );
+        return __PACKAGE__->_copyto($self, out=>$hret)
+    }
+}
+
+=head2 check_format
+
+        Check whether the NDArray format is valid.
+
+        Parameters
+        ----------
+        full_check : bool, optional
+            If `True`, rigorous check, O(N) operations. Otherwise
+            basic check, O(1) operations (default True).
+=cut
+
+method check_format(Bool $full_check=1)
+{
+    scalar(check_call(AI::MXNetCAPI::NDArraySyncCheckFormat($self->handle, 
$full_check)));
+}
+
+=head2 _data
+
+        A deep copy NDArray of the data array associated with the 
BaseSparseNDArray.
+
+        This function blocks. Do not use it in performance critical code.
+=cut
+
+method _data()
+{
+    $self->wait_to_read;
+    my $handle = 
check_call(AI::MXNetCAPI::NDArrayGetDataNDArray($self->handle));
+    return AI::MXNet::NDArray->new(handle => $handle);
+}
+
+=head2 _aux_data
+
+        Get a deep copy NDArray of the i-th aux data array associated with the
+        AI::MXNet::NDArray::Sparse
+
+        This function blocks. Do not use it in performance critical code.
+=cut
+
+method _aux_data(Int $i)
+{
+    $self->wait_to_read;
+    my $handle = check_call(AI::MXNetCAPI::NDArrayGetAuxNDArray($self->handle, 
$i));
+    return AI::MXNet::NDArray->new(handle => $handle);
+}
+
+package AI::MXNet::NDArray::CSR;
+use AI::MXNet::Base;
+use Mouse;
+extends 'AI::MXNet::NDArray::Sparse';
+
+=head1 NAME
+
+    AI::MXNet::NDArray::CSR - A sparse representation of 2D NDArray in the 
Compressed Sparse Row format.
+=cut
+
+=head1 DESCRIPTION
+
+    A AI::MXNet::NDArray::CSR represents an AI::MXNet::NDArray as three 
separate arrays: `data`,
+    `indptr` and `indices`. It uses the CSR representation where the column 
indices for
+    row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their 
corresponding values are stored
+    in ``data[indptr[i]:indptr[i+1]]``.
+
+    The column indices for a given row are expected to be sorted in ascending 
order.
+    Duplicate column entries for the same row are not allowed.
+
+    Example
+    -------
+    >>> $a = mx->nd->array([[0, 1, 0], [2, 0, 0], [0, 0, 0], [0, 0, 3]]);
+    >>> $a = $a->tostype('csr');
+    >>> $a->data->aspdl;
+    [ 1  2  3]
+    >>> $a->indices->aspdl
+    [1 0 2]
+    >>> $a->indptr->aspdl
+    [0 1 2 2 3]
+
+    See Also
+    --------
+    csr_matrix: Several ways to construct a CSRNDArray
+=cut
+
+#    def __reduce__(self):
+#        return CSRNDArray, (None,), super(CSRNDArray, self).__getstate__()
+
+use overload '+=' => sub { ($_[0] + $_[1])->copyto($_[0]) },
+             '-=' => sub { ($_[0] - $_[1])->copyto($_[0]) },
+             '*=' => sub { ($_[0] * $_[1])->copyto($_[0]) },
+             '/=' => sub { ($_[0] / $_[1])->copyto($_[0]) };
+
+=head2 slice
+
+        Returns a sliced view of this array.
 
 Review comment:
   The documentation is wrong.. Should be a copy instead of a view. I'll post a 
PR to fix it (in both perl and python). 

----------------------------------------------------------------
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