提交 94f5ddfd authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Rename SparseType to SparseTensorType

上级 b8c1c463
...@@ -167,7 +167,7 @@ def get_scalar_constant_value(v): ...@@ -167,7 +167,7 @@ def get_scalar_constant_value(v):
""" """
# Is it necessary to test for presence of aesara.sparse at runtime? # Is it necessary to test for presence of aesara.sparse at runtime?
sparse = globals().get("sparse") sparse = globals().get("sparse")
if sparse and isinstance(v.type, sparse.SparseType): if sparse and isinstance(v.type, sparse.SparseTensorType):
if v.owner is not None and isinstance(v.owner.op, sparse.CSM): if v.owner is not None and isinstance(v.owner.op, sparse.CSM):
data = v.owner.inputs[0] data = v.owner.inputs[0]
return tensor.get_scalar_constant_value(data) return tensor.get_scalar_constant_value(data)
......
...@@ -17,7 +17,7 @@ class CType(Type, CLinkerType): ...@@ -17,7 +17,7 @@ class CType(Type, CLinkerType):
- `TensorType`: for numpy.ndarray - `TensorType`: for numpy.ndarray
- `SparseType`: for scipy.sparse - `SparseTensorType`: for scipy.sparse
But you are encouraged to write your own, as described in WRITEME. But you are encouraged to write your own, as described in WRITEME.
......
...@@ -12,7 +12,7 @@ from aesara.tensor.type import TensorType ...@@ -12,7 +12,7 @@ from aesara.tensor.type import TensorType
try: try:
import scipy.sparse import scipy.sparse
from aesara.sparse.basic import SparseType from aesara.sparse.basic import SparseTensorType
def _is_sparse(a): def _is_sparse(a):
return scipy.sparse.issparse(a) return scipy.sparse.issparse(a)
...@@ -64,4 +64,4 @@ def may_share_memory(a, b, raise_other_type=True): ...@@ -64,4 +64,4 @@ def may_share_memory(a, b, raise_other_type=True):
if a_gpua or b_gpua: if a_gpua or b_gpua:
return False return False
return SparseType.may_share_memory(a, b) return SparseTensorType.may_share_memory(a, b)
...@@ -9,7 +9,7 @@ except ImportError: ...@@ -9,7 +9,7 @@ except ImportError:
enable_sparse = False enable_sparse = False
warn("SciPy can't be imported. Sparse matrix support is disabled.") warn("SciPy can't be imported. Sparse matrix support is disabled.")
from aesara.sparse.type import SparseType, _is_sparse from aesara.sparse.type import SparseTensorType, _is_sparse
if enable_sparse: if enable_sparse:
......
差异被折叠。
...@@ -7,7 +7,7 @@ from aesara.graph.basic import Apply ...@@ -7,7 +7,7 @@ from aesara.graph.basic import Apply
from aesara.graph.op import Op from aesara.graph.op import Op
from aesara.sparse.basic import ( from aesara.sparse.basic import (
Remove0, Remove0,
SparseType, SparseTensorType,
_is_sparse, _is_sparse,
as_sparse_variable, as_sparse_variable,
remove0, remove0,
...@@ -108,7 +108,9 @@ class Binomial(Op): ...@@ -108,7 +108,9 @@ class Binomial(Op):
assert shape.dtype in discrete_dtypes assert shape.dtype in discrete_dtypes
return Apply( return Apply(
self, [n, p, shape], [SparseType(dtype=self.dtype, format=self.format)()] self,
[n, p, shape],
[SparseTensorType(dtype=self.dtype, format=self.format)()],
) )
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
......
...@@ -3,7 +3,7 @@ import copy ...@@ -3,7 +3,7 @@ import copy
import scipy.sparse import scipy.sparse
from aesara.compile import SharedVariable, shared_constructor from aesara.compile import SharedVariable, shared_constructor
from aesara.sparse.basic import SparseType, _sparse_py_operators from aesara.sparse.basic import SparseTensorType, _sparse_py_operators
class SparseTensorSharedVariable(_sparse_py_operators, SharedVariable): class SparseTensorSharedVariable(_sparse_py_operators, SharedVariable):
...@@ -16,7 +16,7 @@ def sparse_constructor( ...@@ -16,7 +16,7 @@ def sparse_constructor(
value, name=None, strict=False, allow_downcast=None, borrow=False, format=None value, name=None, strict=False, allow_downcast=None, borrow=False, format=None
): ):
""" """
SharedVariable Constructor for SparseType. SharedVariable Constructor for SparseTensorType.
writeme writeme
...@@ -29,7 +29,7 @@ def sparse_constructor( ...@@ -29,7 +29,7 @@ def sparse_constructor(
if format is None: if format is None:
format = value.format format = value.format
type = SparseType(format=format, dtype=value.dtype) type = SparseTensorType(format=format, dtype=value.dtype)
if not borrow: if not borrow:
value = copy.deepcopy(value) value = copy.deepcopy(value)
return SparseTensorSharedVariable( return SparseTensorSharedVariable(
......
...@@ -25,9 +25,8 @@ def _is_sparse(x): ...@@ -25,9 +25,8 @@ def _is_sparse(x):
return isinstance(x, scipy.sparse.spmatrix) return isinstance(x, scipy.sparse.spmatrix)
class SparseType(TensorType, HasDataType): class SparseTensorType(TensorType, HasDataType):
""" """A `Type` for sparse tensors.
Fundamental way to create a sparse node.
Parameters Parameters
---------- ----------
...@@ -42,8 +41,7 @@ class SparseType(TensorType, HasDataType): ...@@ -42,8 +41,7 @@ class SparseType(TensorType, HasDataType):
Notes Notes
----- -----
As far as I can tell, L{scipy.sparse} objects must be matrices, i.e. Currently, sparse tensors can only be matrices (i.e. have two dimensions).
have dimension 2.
""" """
...@@ -126,15 +124,13 @@ class SparseType(TensorType, HasDataType): ...@@ -126,15 +124,13 @@ class SparseType(TensorType, HasDataType):
raise NotImplementedError() raise NotImplementedError()
return sp return sp
@staticmethod @classmethod
def may_share_memory(a, b): def may_share_memory(cls, a, b):
# This is Fred suggestion for a quick and dirty way of checking
# aliasing .. this can potentially be further refined (ticket #374)
if _is_sparse(a) and _is_sparse(b): if _is_sparse(a) and _is_sparse(b):
return ( return (
SparseType.may_share_memory(a, b.data) cls.may_share_memory(a, b.data)
or SparseType.may_share_memory(a, b.indices) or cls.may_share_memory(a, b.indices)
or SparseType.may_share_memory(a, b.indptr) or cls.may_share_memory(a, b.indptr)
) )
if _is_sparse(b) and isinstance(a, np.ndarray): if _is_sparse(b) and isinstance(a, np.ndarray):
a, b = b, a a, b = b, a
...@@ -151,7 +147,7 @@ class SparseType(TensorType, HasDataType): ...@@ -151,7 +147,7 @@ class SparseType(TensorType, HasDataType):
def convert_variable(self, var): def convert_variable(self, var):
res = super().convert_variable(var) res = super().convert_variable(var)
if res and not isinstance(res.type, SparseType): if res and not isinstance(res.type, type(self)):
# TODO: Convert to this sparse format # TODO: Convert to this sparse format
raise NotImplementedError() raise NotImplementedError()
...@@ -232,9 +228,8 @@ class SparseType(TensorType, HasDataType): ...@@ -232,9 +228,8 @@ class SparseType(TensorType, HasDataType):
return False return False
# Register SparseType's C code for ViewOp.
aesara.compile.register_view_op_c_code( aesara.compile.register_view_op_c_code(
SparseType, SparseTensorType,
""" """
Py_XDECREF(%(oname)s); Py_XDECREF(%(oname)s);
%(oname)s = %(iname)s; %(oname)s = %(iname)s;
...@@ -242,3 +237,6 @@ aesara.compile.register_view_op_c_code( ...@@ -242,3 +237,6 @@ aesara.compile.register_view_op_c_code(
""", """,
1, 1,
) )
# This is a deprecated alias used for (temporary) backward-compatibility
SparseType = SparseTensorType
...@@ -314,9 +314,9 @@ def get_scalar_constant_value( ...@@ -314,9 +314,9 @@ def get_scalar_constant_value(
except ValueError: except ValueError:
raise NotScalarConstantError() raise NotScalarConstantError()
from aesara.sparse.type import SparseType from aesara.sparse.type import SparseTensorType
if isinstance(v.type, SparseType): if isinstance(v.type, SparseTensorType):
raise NotScalarConstantError() raise NotScalarConstantError()
return data return data
......
...@@ -44,7 +44,7 @@ usual dense tensors. In particular, in the ...@@ -44,7 +44,7 @@ usual dense tensors. In particular, in the
instead of ``as_tensor_variable(x)``. instead of ``as_tensor_variable(x)``.
Another difference is that you need to use ``SparseVariable`` and Another difference is that you need to use ``SparseVariable`` and
``SparseType`` instead of ``TensorVariable`` and ``TensorType``. ``SparseTensorType`` instead of ``TensorVariable`` and ``TensorType``.
Do not forget that we support only sparse matrices (so only 2 dimensions) Do not forget that we support only sparse matrices (so only 2 dimensions)
and (like in SciPy) they do not support broadcasting operations by default and (like in SciPy) they do not support broadcasting operations by default
...@@ -55,7 +55,7 @@ you can create output variables like this: ...@@ -55,7 +55,7 @@ you can create output variables like this:
.. code-block:: python .. code-block:: python
out_format = inputs[0].format # or 'csr' or 'csc' if the output format is fixed out_format = inputs[0].format # or 'csr' or 'csc' if the output format is fixed
SparseType(dtype=inputs[0].dtype, format=out_format).make_variable() SparseTensorType(dtype=inputs[0].dtype, format=out_format).make_variable()
See the sparse :class:`Aesara.sparse.basic.Cast` `Op` code for a good example of See the sparse :class:`Aesara.sparse.basic.Cast` `Op` code for a good example of
a sparse `Op` with Python code. a sparse `Op` with Python code.
...@@ -226,7 +226,7 @@ along with pointers to the relevant documentation. ...@@ -226,7 +226,7 @@ along with pointers to the relevant documentation.
primitive type. The C type associated with this Aesara type is the primitive type. The C type associated with this Aesara type is the
represented C primitive itself. represented C primitive itself.
* :ref:`SparseType <sparse_ops>` : Aesara `Type` used to represent sparse * :ref:`SparseTensorType <sparse_ops>` : Aesara `Type` used to represent sparse
tensors. There is no equivalent C type for this Aesara `Type` but you tensors. There is no equivalent C type for this Aesara `Type` but you
can split a sparse variable into its parts as TensorVariables. Those can split a sparse variable into its parts as TensorVariables. Those
can then be used as inputs to an op with C code. can then be used as inputs to an op with C code.
......
...@@ -751,8 +751,8 @@ class TestAliasingRules: ...@@ -751,8 +751,8 @@ class TestAliasingRules:
# operations are used) and to break the elemwise composition # operations are used) and to break the elemwise composition
# with some non-elemwise op (here dot) # with some non-elemwise op (here dot)
x = sparse.SparseType("csc", dtype="float64")() x = sparse.SparseTensorType("csc", dtype="float64")()
y = sparse.SparseType("csc", dtype="float64")() y = sparse.SparseTensorType("csc", dtype="float64")()
f = function([In(x, mutable=True), In(y, mutable=True)], (x + y) + (x + y)) f = function([In(x, mutable=True), In(y, mutable=True)], (x + y) + (x + y))
# Test 1. If the same variable is given twice # Test 1. If the same variable is given twice
......
差异被折叠。
import pytest import pytest
from aesara.sparse import matrix as sp_matrix from aesara.sparse import matrix as sp_matrix
from aesara.sparse.type import SparseType from aesara.sparse.type import SparseTensorType
from aesara.tensor import dmatrix from aesara.tensor import dmatrix
def test_clone(): def test_clone():
st = SparseType("csr", "float64") st = SparseTensorType("csr", "float64")
assert st == st.clone() assert st == st.clone()
......
...@@ -7,7 +7,7 @@ from scipy.sparse.csr import csr_matrix ...@@ -7,7 +7,7 @@ from scipy.sparse.csr import csr_matrix
import aesara import aesara
import aesara.sparse as sparse import aesara.sparse as sparse
import aesara.tensor as at import aesara.tensor as at
from aesara.sparse.type import SparseType from aesara.sparse.type import SparseTensorType
from aesara.tensor.type import DenseTensorType from aesara.tensor.type import DenseTensorType
...@@ -16,7 +16,7 @@ class TestSparseVariable: ...@@ -16,7 +16,7 @@ class TestSparseVariable:
"method, exp_type, cm", "method, exp_type, cm",
[ [
("__abs__", DenseTensorType, None), ("__abs__", DenseTensorType, None),
("__neg__", SparseType, ExitStack()), ("__neg__", SparseTensorType, ExitStack()),
("__ceil__", DenseTensorType, None), ("__ceil__", DenseTensorType, None),
("__floor__", DenseTensorType, None), ("__floor__", DenseTensorType, None),
("__trunc__", DenseTensorType, None), ("__trunc__", DenseTensorType, None),
...@@ -65,7 +65,7 @@ class TestSparseVariable: ...@@ -65,7 +65,7 @@ class TestSparseVariable:
("conj", DenseTensorType, None), ("conj", DenseTensorType, None),
("round", DenseTensorType, None), ("round", DenseTensorType, None),
("trace", DenseTensorType, None), ("trace", DenseTensorType, None),
("zeros_like", SparseType, ExitStack()), ("zeros_like", SparseTensorType, ExitStack()),
("ones_like", DenseTensorType, ExitStack()), ("ones_like", DenseTensorType, ExitStack()),
("cumsum", DenseTensorType, None), ("cumsum", DenseTensorType, None),
("cumprod", DenseTensorType, None), ("cumprod", DenseTensorType, None),
...@@ -83,7 +83,7 @@ class TestSparseVariable: ...@@ -83,7 +83,7 @@ class TestSparseVariable:
if cm is None: if cm is None:
cm = pytest.warns(UserWarning, match=".*converted to dense.*") cm = pytest.warns(UserWarning, match=".*converted to dense.*")
if exp_type == SparseType: if exp_type == SparseTensorType:
exp_res_type = csr_matrix exp_res_type = csr_matrix
else: else:
exp_res_type = np.ndarray exp_res_type = np.ndarray
...@@ -112,16 +112,16 @@ class TestSparseVariable: ...@@ -112,16 +112,16 @@ class TestSparseVariable:
@pytest.mark.parametrize( @pytest.mark.parametrize(
"method, exp_type", "method, exp_type",
[ [
("__lt__", SparseType), ("__lt__", SparseTensorType),
("__le__", SparseType), ("__le__", SparseTensorType),
("__gt__", SparseType), ("__gt__", SparseTensorType),
("__ge__", SparseType), ("__ge__", SparseTensorType),
("__and__", DenseTensorType), ("__and__", DenseTensorType),
("__or__", DenseTensorType), ("__or__", DenseTensorType),
("__xor__", DenseTensorType), ("__xor__", DenseTensorType),
("__add__", SparseType), ("__add__", SparseTensorType),
("__sub__", SparseType), ("__sub__", SparseTensorType),
("__mul__", SparseType), ("__mul__", SparseTensorType),
("__pow__", DenseTensorType), ("__pow__", DenseTensorType),
("__mod__", DenseTensorType), ("__mod__", DenseTensorType),
("__divmod__", DenseTensorType), ("__divmod__", DenseTensorType),
...@@ -137,7 +137,7 @@ class TestSparseVariable: ...@@ -137,7 +137,7 @@ class TestSparseVariable:
method_to_call = getattr(x, method) method_to_call = getattr(x, method)
if exp_type == SparseType: if exp_type == SparseTensorType:
exp_res_type = csr_matrix exp_res_type = csr_matrix
cm = ExitStack() cm = ExitStack()
else: else:
...@@ -198,7 +198,7 @@ class TestSparseVariable: ...@@ -198,7 +198,7 @@ class TestSparseVariable:
x = sparse.csr_from_dense(x) x = sparse.csr_from_dense(x)
z = x[:, :2] z = x[:, :2]
assert isinstance(z.type, SparseType) assert isinstance(z.type, SparseTensorType)
f = aesara.function([x], z) f = aesara.function([x], z)
exp_res = f([[1.1, 0.0, 2.0], [-1.0, 0.0, 0.0]]) exp_res = f([[1.1, 0.0, 2.0], [-1.0, 0.0, 0.0]])
...@@ -211,7 +211,7 @@ class TestSparseVariable: ...@@ -211,7 +211,7 @@ class TestSparseVariable:
y = sparse.csr_from_dense(y) y = sparse.csr_from_dense(y)
z = x.__dot__(y) z = x.__dot__(y)
assert isinstance(z.type, SparseType) assert isinstance(z.type, SparseTensorType)
f = aesara.function([x, y], z) f = aesara.function([x, y], z)
exp_res = f( exp_res = f(
......
...@@ -451,7 +451,7 @@ class TestIndex: ...@@ -451,7 +451,7 @@ class TestIndex:
def test_sparse(self): def test_sparse(self):
sp = pytest.importorskip("scipy") sp = pytest.importorskip("scipy")
mySymbolicSparseList = TypedListType( mySymbolicSparseList = TypedListType(
sparse.SparseType("csr", aesara.config.floatX) sparse.SparseTensorType("csr", aesara.config.floatX)
)() )()
mySymbolicSparse = sparse.csr_matrix() mySymbolicSparse = sparse.csr_matrix()
...@@ -519,7 +519,7 @@ class TestCount: ...@@ -519,7 +519,7 @@ class TestCount:
def test_sparse(self): def test_sparse(self):
sp = pytest.importorskip("scipy") sp = pytest.importorskip("scipy")
mySymbolicSparseList = TypedListType( mySymbolicSparseList = TypedListType(
sparse.SparseType("csr", aesara.config.floatX) sparse.SparseTensorType("csr", aesara.config.floatX)
)() )()
mySymbolicSparse = sparse.csr_matrix() mySymbolicSparse = sparse.csr_matrix()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论