mindspore/tests/ut/python/pynative_mode/test_sparse_pynative.py

82 lines
2.7 KiB
Python
Raw Normal View History

2020-07-31 11:47:31 +08:00
# 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.
# ============================================================================
"""
@File : test_sparse_pynative.py
@Author:
@Date : 2020-08-04
@Desc : test mindspore sparse pynative
"""
import pytest
2020-07-31 11:47:31 +08:00
import mindspore as ms
import mindspore.nn as nn
2022-10-28 14:47:54 +08:00
from mindspore import context, Tensor, COOTensor
from mindspore.common.sparse_tensor import RowTensorInner
2020-07-31 11:47:31 +08:00
from mindspore.ops import composite as C
@pytest.fixture(scope="module", autouse=True)
def setup_teardown():
context.set_context(mode=context.PYNATIVE_MODE)
yield
context.set_context(mode=context.GRAPH_MODE)
2020-07-31 11:47:31 +08:00
2020-08-25 20:16:08 +08:00
grad_all = C.GradOperation(get_all=True)
2020-07-31 11:47:31 +08:00
class GradWrap(nn.Cell):
def __init__(self, network):
super(GradWrap, self).__init__()
self.network = network
def construct(self, *args):
grad = grad_all(self.network)(*args)
return grad
2020-08-06 09:37:51 +08:00
def test_row_tensor_attr():
2021-12-23 19:11:33 +08:00
"""
Feature: Test RowTensor.
Description: Test RowTensor GetAttr.
Expectation: Success.
"""
2020-08-06 09:37:51 +08:00
class RowTensorGetAttr(nn.Cell):
2020-07-31 11:47:31 +08:00
def __init__(self, dense_shape):
2020-08-06 09:37:51 +08:00
super(RowTensorGetAttr, self).__init__()
2020-07-31 11:47:31 +08:00
self.dense_shape = dense_shape
def construct(self, indices, values):
2022-10-27 10:03:53 +08:00
x = RowTensorInner(indices, values, self.dense_shape)
2020-08-06 09:37:51 +08:00
return x.values, x.indices, x.dense_shape
2020-07-31 11:47:31 +08:00
indices = Tensor([0])
values = Tensor([[1, 2]], dtype=ms.float32)
2020-08-06 09:37:51 +08:00
RowTensorGetAttr((3, 2))(indices, values)
GradWrap(RowTensorGetAttr((3, 2)))(indices, values)
2020-07-31 11:47:31 +08:00
def test_sparse_tensor_attr():
2021-12-23 19:11:33 +08:00
"""
Feature: Test COOTensor.
Description: Test COOTensor GetAttr.
Expectation: Success.
"""
2020-07-31 11:47:31 +08:00
class SparseTensorGetAttr(nn.Cell):
def __init__(self):
super(SparseTensorGetAttr, self).__init__()
self.dense_shape = (3, 4)
def construct(self, indices, values):
2021-12-23 19:11:33 +08:00
x = COOTensor(indices, values, self.dense_shape)
return x.values, x.indices, x.shape
2020-07-31 11:47:31 +08:00
indices = Tensor([[0, 1], [1, 2]])
values = Tensor([1, 2], dtype=ms.float32)
SparseTensorGetAttr()(indices, values)
GradWrap(SparseTensorGetAttr())(indices, values)