|
|
|
@@ -24,9 +24,9 @@ from mindspore.ops import composite as C |
|
|
|
context.set_context(mode=context.GRAPH_MODE) |
|
|
|
|
|
|
|
|
|
|
|
class Net(nn.Cell): |
|
|
|
class FirstInputTupleNet(nn.Cell): |
|
|
|
def __init__(self): |
|
|
|
super(Net, self).__init__() |
|
|
|
super(FirstInputTupleNet, self).__init__() |
|
|
|
|
|
|
|
def construct(self, tuple_a, tensor_x, list_b, tensor_y, scalar, dict_c, flag): |
|
|
|
if flag: |
|
|
|
@@ -35,11 +35,11 @@ class Net(nn.Cell): |
|
|
|
|
|
|
|
|
|
|
|
class GradNet(nn.Cell): |
|
|
|
def __init__(self, net): |
|
|
|
def __init__(self, net, get_all): |
|
|
|
super(GradNet, self).__init__() |
|
|
|
self.forward_net = net |
|
|
|
self.sens = Tensor(np.ones((2, 2), np.float32) * 5) |
|
|
|
self.grad_all = C.GradOperation(get_all=True) |
|
|
|
self.grad_all = C.GradOperation(get_all=get_all) |
|
|
|
|
|
|
|
def construct(self, tuple_a, tensor_x, list_b, tensor_y, scalar, dict_c, flag): |
|
|
|
return self.grad_all(self.forward_net)(tuple_a, tensor_x, list_b, tensor_y, scalar, dict_c, flag) |
|
|
|
@@ -64,8 +64,8 @@ flag_1 = False |
|
|
|
p = Parameter(x, name="weight") |
|
|
|
a = np.ones((2, 2)) |
|
|
|
|
|
|
|
forward_net = Net() |
|
|
|
grad_net = GradNet(forward_net) |
|
|
|
forward_net = FirstInputTupleNet() |
|
|
|
grad_all_inputs_net = GradNet(forward_net, get_all=True) |
|
|
|
|
|
|
|
|
|
|
|
def test_outermost_net_inputs_including_non_tensor(): |
|
|
|
@@ -74,13 +74,31 @@ def test_outermost_net_inputs_including_non_tensor(): |
|
|
|
|
|
|
|
|
|
|
|
def test_grad_net_inputs_including_non_tensor(): |
|
|
|
grad_net(arg_t0, z, arg_l0, w, sl, args_d0, flag_0) |
|
|
|
grad_net(arg_t1, z, arg_l1, x, sl, args_d1, flag_1) |
|
|
|
assert len(grad_all_inputs_net(arg_t0, z, arg_l0, w, sl, args_d0, flag_0)) == 2 |
|
|
|
assert len(grad_all_inputs_net(arg_t1, z, arg_l1, x, sl, args_d1, flag_1)) == 2 |
|
|
|
|
|
|
|
|
|
|
|
def test_grad_first_input_net(): |
|
|
|
class FirstInputTensorNet(nn.Cell): |
|
|
|
def __init__(self): |
|
|
|
super(FirstInputTensorNet, self).__init__() |
|
|
|
|
|
|
|
def construct(self, tensor_x, tuple_a, list_b, tensor_y, scalar, dict_c, flag): |
|
|
|
if flag: |
|
|
|
return tensor_x - tuple_a[2] + list_b[1][1]["x"] - tensor_y + scalar - dict_c["x"] |
|
|
|
return tensor_x + tuple_a[2] - list_b[1][1]["y"] + tensor_y - scalar + dict_c["y"] |
|
|
|
|
|
|
|
grad_fist_input_tensor_net = GradNet(FirstInputTensorNet(), get_all=False) |
|
|
|
ret = grad_fist_input_tensor_net(z, arg_t0, arg_l0, w, sl, args_d0, flag_0) |
|
|
|
assert np.allclose(ret.asnumpy(), np.ones((2, 2), np.float32)) |
|
|
|
|
|
|
|
grad_fist_input_tuple_net = GradNet(forward_net, get_all=False) |
|
|
|
assert not grad_fist_input_tuple_net(arg_t0, z, arg_l0, w, sl, args_d0, flag_0) |
|
|
|
|
|
|
|
|
|
|
|
def test_net_inputs_including_str(): |
|
|
|
with pytest.raises(TypeError) as err: |
|
|
|
grad_net(arg_t0, s, arg_l0, w, sl, args_d0, flag_0) |
|
|
|
grad_all_inputs_net(arg_t0, s, arg_l0, w, sl, args_d0, flag_0) |
|
|
|
assert "The inputs types of the outermost network support bool, int, float, tensor, " \ |
|
|
|
"mstype.Number(mstype.bool, mstype.int, mstype.float, mstype.uint), " \ |
|
|
|
"and tuple or list containing only these types, and dict whose values are these types, " \ |
|
|
|
@@ -117,7 +135,7 @@ def test_outermost_net_pass_list_including_parameter(): |
|
|
|
|
|
|
|
def test_grad_net_pass_dict_including_parameter(): |
|
|
|
with pytest.raises(TypeError) as err: |
|
|
|
grad_net(arg_t0, z, arg_l0, {"x": z, "y": w, "z": p}, sl, args_d0, flag_0) |
|
|
|
grad_all_inputs_net(arg_t0, z, arg_l0, {"x": z, "y": w, "z": p}, sl, args_d0, flag_0) |
|
|
|
assert "The inputs types of the outermost network support bool, int, float, tensor, " \ |
|
|
|
"mstype.Number(mstype.bool, mstype.int, mstype.float, mstype.uint), " \ |
|
|
|
"and tuple or list containing only these types, and dict whose values are these types, " \ |
|
|
|
|