upgrade Ascend software package 09 Sep 21
This commit is contained in:
parent
5c72e9d7c6
commit
0ced8775c4
|
@ -1 +1 @@
|
||||||
Subproject commit 6cc2d0293685edd22a1e3ef2a6af393779a0283d
|
Subproject commit 329eb8bbed9137a1ed6aca07ec68cdee6ae2975f
|
|
@ -1,108 +0,0 @@
|
||||||
# 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.
|
|
||||||
# ============================================================================
|
|
||||||
""" test lazy adam """
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import mindspore.nn as nn
|
|
||||||
from mindspore import Tensor, Parameter, context
|
|
||||||
from mindspore.common.api import _cell_graph_executor
|
|
||||||
from mindspore.nn import TrainOneStepCell, WithLossCell
|
|
||||||
from mindspore.nn.optim import LazyAdam
|
|
||||||
from mindspore.ops import operations as P
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module", autouse=True)
|
|
||||||
def setup_teardown():
|
|
||||||
context.set_context(enable_sparse=True)
|
|
||||||
yield
|
|
||||||
context.set_context(enable_sparse=False)
|
|
||||||
|
|
||||||
|
|
||||||
class Net(nn.Cell):
|
|
||||||
""" Net definition """
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super(Net, self).__init__()
|
|
||||||
self.weight = Parameter(Tensor(np.ones([64, 10]).astype(np.float32)), name="weight")
|
|
||||||
self.bias = Parameter(Tensor(np.ones([10]).astype((np.float32))), name="bias")
|
|
||||||
self.matmul = P.MatMul()
|
|
||||||
self.biasAdd = P.BiasAdd()
|
|
||||||
|
|
||||||
def construct(self, x):
|
|
||||||
x = self.biasAdd(self.matmul(x, self.weight), self.bias)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class NetWithSparseGatherV2(nn.Cell):
|
|
||||||
""" NetWithSparseGatherV2 definition """
|
|
||||||
def __init__(self):
|
|
||||||
super(NetWithSparseGatherV2, self).__init__()
|
|
||||||
self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1")
|
|
||||||
self.weight2 = Parameter(Tensor(np.ones([2, 1, 2]).astype((np.float32))), name="weight2")
|
|
||||||
self.axis = 0
|
|
||||||
self.gather = P.SparseGatherV2()
|
|
||||||
|
|
||||||
def construct(self, indices, label):
|
|
||||||
return self.gather(self.weight1, indices, self.axis) + self.weight2
|
|
||||||
|
|
||||||
|
|
||||||
def test_lazy_adam_compile():
|
|
||||||
""" test lazy adam compile """
|
|
||||||
inputs = Tensor(np.ones([1, 64]).astype(np.float32))
|
|
||||||
label = Tensor(np.zeros([1, 10]).astype(np.float32))
|
|
||||||
net = Net()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
loss = nn.SoftmaxCrossEntropyWithLogits()
|
|
||||||
optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
|
|
||||||
|
|
||||||
net_with_loss = WithLossCell(net, loss)
|
|
||||||
train_network = TrainOneStepCell(net_with_loss, optimizer)
|
|
||||||
_cell_graph_executor.compile(train_network, inputs, label)
|
|
||||||
|
|
||||||
|
|
||||||
def test_spares_lazy_adam_compile():
|
|
||||||
""" test sparse adam compile """
|
|
||||||
indices = Tensor(np.array([0, 1]).astype(np.int32))
|
|
||||||
label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
|
|
||||||
optimizer.target = 'CPU'
|
|
||||||
train_network = TrainOneStepCell(net, optimizer)
|
|
||||||
_cell_graph_executor.compile(train_network, indices, label)
|
|
||||||
|
|
||||||
|
|
||||||
def test_spares_lazy_adam():
|
|
||||||
""" test sparse adam"""
|
|
||||||
indices = Tensor(np.array([0, 1]).astype(np.int32))
|
|
||||||
label = Tensor(np.zeros([2, 1, 2]).astype(np.float32))
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
|
|
||||||
optimizer.target = 'Ascend'
|
|
||||||
train_network = TrainOneStepCell(net, optimizer)
|
|
||||||
_cell_graph_executor.compile(train_network, indices, label)
|
|
||||||
|
|
||||||
|
|
||||||
def test_lazy_adam_error():
|
|
||||||
net = Net()
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
LazyAdam(net.get_parameters(), learning_rate=-0.1)
|
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
|
||||||
LazyAdam(net.get_parameters(), learning_rate=0.1, beta1=2)
|
|
|
@ -1,80 +0,0 @@
|
||||||
# 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.
|
|
||||||
# ============================================================================
|
|
||||||
""" test lazy adam """
|
|
||||||
import pytest
|
|
||||||
import numpy as np
|
|
||||||
from mindspore.nn.optim import LazyAdam, FTRL, Adam, ProximalAdagrad
|
|
||||||
import mindspore.nn as nn
|
|
||||||
from mindspore import Tensor, Parameter, context
|
|
||||||
from mindspore.ops import operations as P
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module", autouse=True)
|
|
||||||
def setup_teardown():
|
|
||||||
context.set_context(enable_sparse=True)
|
|
||||||
yield
|
|
||||||
context.set_context(enable_sparse=False)
|
|
||||||
|
|
||||||
|
|
||||||
class NetWithSparseGatherV2(nn.Cell):
|
|
||||||
""" NetWithSparseGatherV2 definition """
|
|
||||||
def __init__(self):
|
|
||||||
super(NetWithSparseGatherV2, self).__init__()
|
|
||||||
self.weight1 = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="weight1")
|
|
||||||
self.weight2 = Parameter(Tensor(np.ones([2, 1, 2]).astype((np.float32))), name="weight2")
|
|
||||||
self.axis = 0
|
|
||||||
self.gather = P.SparseGatherV2()
|
|
||||||
|
|
||||||
def construct(self, indices, label):
|
|
||||||
return self.gather(self.weight1, indices, self.axis) + self.weight2
|
|
||||||
|
|
||||||
|
|
||||||
def test_ftrl_target():
|
|
||||||
""" test_ftrl_target """
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = FTRL(net.trainable_params(), weight_decay=0.9, loss_scale=2.0)
|
|
||||||
if optimizer.target not in ('CPU', 'Ascend'):
|
|
||||||
raise ValueError("The value must be 'CPU' or 'Ascend', but got value {}".format(optimizer.target))
|
|
||||||
|
|
||||||
|
|
||||||
def test_lazyadam_target():
|
|
||||||
""" test_lazyadam_target """
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = LazyAdam(net.trainable_params(), learning_rate=0.1, weight_decay=0.9, loss_scale=2.0)
|
|
||||||
if optimizer.target not in ('CPU', 'Ascend'):
|
|
||||||
raise ValueError("The value must be 'CPU' or 'Ascend', but got value {}".format(optimizer.target))
|
|
||||||
|
|
||||||
|
|
||||||
def test_adam_target():
|
|
||||||
""" test_adam_target """
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0, weight_decay=0.9)
|
|
||||||
if optimizer.target not in ('CPU', 'Ascend'):
|
|
||||||
raise ValueError("The value must be 'CPU' or 'Ascend', but got value {}".format(optimizer.target))
|
|
||||||
|
|
||||||
|
|
||||||
def test_proximal_target():
|
|
||||||
""" test_proximal_target """
|
|
||||||
net = NetWithSparseGatherV2()
|
|
||||||
net.set_train()
|
|
||||||
|
|
||||||
optimizer = ProximalAdagrad(net.trainable_params(), weight_decay=0.9, loss_scale=1024.0)
|
|
||||||
if optimizer.target not in ('CPU', 'Ascend'):
|
|
||||||
raise ValueError("The value must be 'CPU' or 'Ascend', but got value {}".format(optimizer.target))
|
|
|
@ -1,155 +0,0 @@
|
||||||
# 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.
|
|
||||||
# ============================================================================
|
|
||||||
"""
|
|
||||||
Test nn.probability.distribution.gumbel.
|
|
||||||
"""
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
import mindspore.nn as nn
|
|
||||||
import mindspore.nn.probability.distribution as msd
|
|
||||||
from mindspore import dtype
|
|
||||||
from mindspore import Tensor
|
|
||||||
|
|
||||||
def test_gumbel_shape_errpr():
|
|
||||||
"""
|
|
||||||
Invalid shapes.
|
|
||||||
"""
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
msd.Gumbel([[2.], [1.]], [[2.], [3.], [4.]], dtype=dtype.float32)
|
|
||||||
|
|
||||||
def test_type():
|
|
||||||
with pytest.raises(TypeError):
|
|
||||||
msd.Gumbel(0., 1., dtype=dtype.int32)
|
|
||||||
|
|
||||||
def test_name():
|
|
||||||
with pytest.raises(TypeError):
|
|
||||||
msd.Gumbel(0., 1., name=1.0)
|
|
||||||
|
|
||||||
def test_seed():
|
|
||||||
with pytest.raises(TypeError):
|
|
||||||
msd.Gumbel(0., 1., seed='seed')
|
|
||||||
|
|
||||||
def test_scale():
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
msd.Gumbel(0., 0.)
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
msd.Gumbel(0., -1.)
|
|
||||||
|
|
||||||
def test_arguments():
|
|
||||||
"""
|
|
||||||
args passing during initialization.
|
|
||||||
"""
|
|
||||||
l = msd.Gumbel([3.0], [4.0], dtype=dtype.float32)
|
|
||||||
assert isinstance(l, msd.Distribution)
|
|
||||||
|
|
||||||
|
|
||||||
class GumbelProb(nn.Cell):
|
|
||||||
"""
|
|
||||||
Gumbel distribution: initialize with loc/scale.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(GumbelProb, self).__init__()
|
|
||||||
self.gumbel = msd.Gumbel(3.0, 4.0, dtype=dtype.float32)
|
|
||||||
|
|
||||||
def construct(self, value):
|
|
||||||
prob = self.gumbel.prob(value)
|
|
||||||
log_prob = self.gumbel.log_prob(value)
|
|
||||||
cdf = self.gumbel.cdf(value)
|
|
||||||
log_cdf = self.gumbel.log_cdf(value)
|
|
||||||
sf = self.gumbel.survival_function(value)
|
|
||||||
log_sf = self.gumbel.log_survival(value)
|
|
||||||
return prob + log_prob + cdf + log_cdf + sf + log_sf
|
|
||||||
|
|
||||||
def test_gumbel_prob():
|
|
||||||
"""
|
|
||||||
Test probability functions: passing value through construct.
|
|
||||||
"""
|
|
||||||
net = GumbelProb()
|
|
||||||
value = Tensor([0.5, 1.0], dtype=dtype.float32)
|
|
||||||
ans = net(value)
|
|
||||||
assert isinstance(ans, Tensor)
|
|
||||||
|
|
||||||
class KL(nn.Cell):
|
|
||||||
"""
|
|
||||||
Test kl_loss.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(KL, self).__init__()
|
|
||||||
self.gumbel = msd.Gumbel(3.0, 4.0)
|
|
||||||
|
|
||||||
def construct(self, mu, s):
|
|
||||||
kl = self.gumbel.kl_loss('Gumbel', mu, s)
|
|
||||||
cross_entropy = self.gumbel.cross_entropy('Gumbel', mu, s)
|
|
||||||
return kl + cross_entropy
|
|
||||||
|
|
||||||
def test_kl_cross_entropy():
|
|
||||||
"""
|
|
||||||
Test kl_loss and cross_entropy.
|
|
||||||
"""
|
|
||||||
from mindspore import context
|
|
||||||
context.set_context(device_target="Ascend")
|
|
||||||
net = KL()
|
|
||||||
loc_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
|
|
||||||
scale_b = Tensor(np.array([1.0]).astype(np.float32), dtype=dtype.float32)
|
|
||||||
ans = net(loc_b, scale_b)
|
|
||||||
assert isinstance(ans, Tensor)
|
|
||||||
|
|
||||||
|
|
||||||
class GumbelBasics(nn.Cell):
|
|
||||||
"""
|
|
||||||
Test class: basic loc/scale function.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(GumbelBasics, self).__init__()
|
|
||||||
self.gumbel = msd.Gumbel(3.0, 4.0, dtype=dtype.float32)
|
|
||||||
|
|
||||||
def construct(self):
|
|
||||||
mean = self.gumbel.mean()
|
|
||||||
sd = self.gumbel.sd()
|
|
||||||
mode = self.gumbel.mode()
|
|
||||||
entropy = self.gumbel.entropy()
|
|
||||||
return mean + sd + mode + entropy
|
|
||||||
|
|
||||||
def test_bascis():
|
|
||||||
"""
|
|
||||||
Test mean/sd/mode/entropy functionality of Gumbel.
|
|
||||||
"""
|
|
||||||
net = GumbelBasics()
|
|
||||||
ans = net()
|
|
||||||
assert isinstance(ans, Tensor)
|
|
||||||
|
|
||||||
|
|
||||||
class GumbelConstruct(nn.Cell):
|
|
||||||
"""
|
|
||||||
Gumbel distribution: going through construct.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
super(GumbelConstruct, self).__init__()
|
|
||||||
self.gumbel = msd.Gumbel(3.0, 4.0)
|
|
||||||
|
|
||||||
|
|
||||||
def construct(self, value):
|
|
||||||
prob = self.gumbel('prob', value)
|
|
||||||
prob1 = self.gumbel.prob(value)
|
|
||||||
return prob + prob1
|
|
||||||
|
|
||||||
def test_gumbel_construct():
|
|
||||||
"""
|
|
||||||
Test probability function going through construct.
|
|
||||||
"""
|
|
||||||
net = GumbelConstruct()
|
|
||||||
value = Tensor([0.5, 1.0], dtype=dtype.float32)
|
|
||||||
ans = net(value)
|
|
||||||
assert isinstance(ans, Tensor)
|
|
|
@ -1,38 +0,0 @@
|
||||||
# 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.
|
|
||||||
# ============================================================================
|
|
||||||
""" Test Dropout """
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import mindspore.nn as nn
|
|
||||||
from mindspore import Tensor
|
|
||||||
from mindspore import context
|
|
||||||
|
|
||||||
context.set_context(device_target="Ascend")
|
|
||||||
|
|
||||||
|
|
||||||
def test_check_dropout_3():
|
|
||||||
Tensor(np.ones([20, 16, 50]).astype(np.int32))
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
nn.Dropout(3)
|
|
||||||
|
|
||||||
|
|
||||||
class Net_dropout(nn.Cell):
|
|
||||||
def __init__(self):
|
|
||||||
super(Net_dropout, self).__init__()
|
|
||||||
self.dropout = nn.Dropout(0.5)
|
|
||||||
|
|
||||||
def construct(self, x):
|
|
||||||
return self.dropout(x)
|
|
|
@ -1,62 +0,0 @@
|
||||||
# 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.
|
|
||||||
# ============================================================================
|
|
||||||
""" test image gradients """
|
|
||||||
import numpy as np
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import mindspore.common.dtype as mstype
|
|
||||||
import mindspore.context as context
|
|
||||||
import mindspore.nn as nn
|
|
||||||
from mindspore import Tensor
|
|
||||||
from mindspore.common.api import _cell_graph_executor
|
|
||||||
from mindspore.common.api import ms_function
|
|
||||||
|
|
||||||
context.set_context(device_target="Ascend")
|
|
||||||
|
|
||||||
|
|
||||||
class Net(nn.Cell):
|
|
||||||
def __init__(self):
|
|
||||||
super(Net, self).__init__()
|
|
||||||
self.image_gradients = nn.ImageGradients()
|
|
||||||
|
|
||||||
@ms_function
|
|
||||||
def construct(self, x):
|
|
||||||
return self.image_gradients(x)
|
|
||||||
|
|
||||||
|
|
||||||
def test_compile():
|
|
||||||
# input shape 1 x 1 x 2 x 2
|
|
||||||
image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mstype.int32)
|
|
||||||
net = Net()
|
|
||||||
_cell_graph_executor.compile(net, image)
|
|
||||||
|
|
||||||
|
|
||||||
def test_compile_multi_channel():
|
|
||||||
# input shape 4 x 2 x 2 x 2
|
|
||||||
dtype = mstype.int32
|
|
||||||
image = Tensor(np.array([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
|
|
||||||
[[[3, 5], [7, 9]], [[11, 13], [15, 17]]],
|
|
||||||
[[[5, 10], [15, 20]], [[25, 30], [35, 40]]],
|
|
||||||
[[[10, 20], [30, 40]], [[50, 60], [70, 80]]]]), dtype=dtype)
|
|
||||||
net = Net()
|
|
||||||
_cell_graph_executor.compile(net, image)
|
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_5d_input():
|
|
||||||
dtype = mstype.float32
|
|
||||||
image = Tensor(np.random.random([4, 1, 16, 16, 1]), dtype=dtype)
|
|
||||||
net = Net()
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_cell_graph_executor.compile(net, image)
|
|
Loading…
Reference in New Issue