!3537 add sparse operators
Merge pull request !3537 from riemann_penn/add_sparse_operatorpull/3537/MERGE
commit
f1e717554c
@ -0,0 +1,22 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed 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.
|
||||
# ============================================================================
|
||||
"""
|
||||
Sparse related transformation.
|
||||
"""
|
||||
from .sparse import SparseToDense
|
||||
|
||||
__all__ = [
|
||||
"SparseToDense",
|
||||
]
|
@ -0,0 +1,54 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed 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.
|
||||
# ============================================================================
|
||||
"""Sparse related tools."""
|
||||
from mindspore.ops import operations as P
|
||||
from ..cell import Cell
|
||||
|
||||
|
||||
class SparseToDense(Cell):
|
||||
"""
|
||||
Convert a sparse tensor into dense.
|
||||
|
||||
Not yet supported by any backend at the moment.
|
||||
|
||||
Args:
|
||||
sparse_tensor (SparseTensor): the sparse tensor to convert.
|
||||
|
||||
Returns:
|
||||
Tensor, the tensor converted.
|
||||
|
||||
Examples:
|
||||
>>> class SparseToDenseCell(nn.Cell):
|
||||
>>> def __init__(self, dense_shape):
|
||||
>>> super(SparseToDenseCell, self).__init__()
|
||||
>>> self.dense_shape = dense_shape
|
||||
>>> self.sparse_to_dense = nn.SparseToDense()
|
||||
>>> def construct(self, indices, values):
|
||||
>>> sparse = SparseTensor(indices, values, self.dense_shape)
|
||||
>>> return self.sparse_to_dense(sparse)
|
||||
>>>
|
||||
>>> indices = Tensor([[0, 1], [1, 2]])
|
||||
>>> values = Tensor([1, 2], dtype=ms.float32)
|
||||
>>> dense_shape = (3, 4)
|
||||
>>> SparseToDenseCell(dense_shape)(indices, values)
|
||||
"""
|
||||
def __init__(self):
|
||||
super(SparseToDense, self).__init__()
|
||||
self.sparse_to_dense = P.SparseToDense()
|
||||
|
||||
def construct(self, sparse_tensor):
|
||||
return self.sparse_to_dense(sparse_tensor.indices(),
|
||||
sparse_tensor.values(),
|
||||
sparse_tensor.dense_shape())
|
@ -0,0 +1,58 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed 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.
|
||||
# ============================================================================
|
||||
|
||||
"""bprop primitives"""
|
||||
from .. import functional as F
|
||||
from .. import operations as P
|
||||
from ..composite.multitype_ops.zeros_like_impl import zeros_like
|
||||
from .grad_base import bprops, bprop_getters
|
||||
|
||||
# Unused parameters are placeholders.
|
||||
|
||||
|
||||
@bprops.register("MakeSparseTensor")
|
||||
def bprop_make_sparse_tensor(indices, values, dense_shape, out, dout):
|
||||
"""Backpropagator for primitive `MakeSparseTensor`."""
|
||||
return zeros_like(indices), F.sparse_tensor_get_values(dout), ()
|
||||
|
||||
|
||||
@bprops.register("SparseTensorGetIndices")
|
||||
def bprop_sparse_tensor_get_indices(sparse_tensor, out, dout):
|
||||
"""Backpropagator for primitive `SparseTensorGetIndices`."""
|
||||
return (zeros_like(sparse_tensor),)
|
||||
|
||||
|
||||
@bprops.register("SparseTensorGetValues")
|
||||
def bprop_sparse_tensor_get_values(sparse_tensor, out, dout):
|
||||
"""Backpropagator for primitive `SparseTensorGetValues`."""
|
||||
return F.make_sparse_tensor(F.sparse_tensor_get_indices(sparse_tensor),
|
||||
dout,
|
||||
F.sparse_tensor_get_dense_shape(sparse_tensor))
|
||||
|
||||
|
||||
@bprops.register("SparseTensorGetDenseShape")
|
||||
def bprop_sparse_tensor_get_dense_shape(sparse_tensor, out, dout):
|
||||
"""Backpropagator for primitive `SparseTensorGetDenseShape`."""
|
||||
return (zeros_like(sparse_tensor),)
|
||||
|
||||
|
||||
@bprop_getters.register(P.SparseToDense)
|
||||
def get_bprop_sparse_to_dense(self):
|
||||
"""Generate bprop for SparseToDense"""
|
||||
|
||||
def bprop(indices, values, dense_shape, out, dout):
|
||||
return zeros_like(indices), dout, zeros_like(dense_shape)
|
||||
|
||||
return bprop
|
@ -0,0 +1,55 @@
|
||||
# coding: utf-8
|
||||
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed 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.
|
||||
# ============================================================================
|
||||
|
||||
"""Operators for sparse operators."""
|
||||
|
||||
from ..._checkparam import Validator as validator
|
||||
from ...common import dtype as mstype
|
||||
from ..primitive import PrimitiveWithInfer, prim_attr_register
|
||||
|
||||
class SparseToDense(PrimitiveWithInfer):
|
||||
"""
|
||||
Convert a sparse representation into a dense tensor.
|
||||
|
||||
Inputs:
|
||||
- **indices** (Tensor) - The indices of sparse representation.
|
||||
- **values** (Tensor) - Values corresponding to each row of indices.
|
||||
- **dense_shape** (tuple) - A int tuple which specifies the shape of dense tensor.
|
||||
|
||||
Returns:
|
||||
Tensor, the shape of tensor is dense_shape.
|
||||
|
||||
Examples:
|
||||
>>> indices = Tensor([[0, 1], [1, 2]])
|
||||
>>> values = Tensor([1, 2], dtype=ms.float32)
|
||||
>>> dense_shape = (3, 4)
|
||||
>>> out = P.SparseToDense()(indices, values, dense_shape)
|
||||
"""
|
||||
|
||||
@prim_attr_register
|
||||
def __init__(self):
|
||||
"""init index_select"""
|
||||
self.init_prim_io_names(inputs=['indices', 'values', 'dense_shape'], outputs=['output'])
|
||||
|
||||
def __infer__(self, indices, values, dense_shape):
|
||||
validator.check_subclass("indices", indices['dtype'], mstype.tensor, self.name)
|
||||
validator.check_subclass("values", values['dtype'], mstype.tensor, self.name)
|
||||
out = {'shape': dense_shape['value'],
|
||||
'dtype': values['dtype'],
|
||||
'value': None}
|
||||
return out
|
Loading…
Reference in new issue