|
|
|
@@ -25,15 +25,14 @@ setitem = base.MultitypeFuncGraph('setitem') |
|
|
|
@setitem.register("List", "Number", "String") |
|
|
|
def _list_setitem_with_string(data, number_index, value): |
|
|
|
""" |
|
|
|
Assign value to list. |
|
|
|
Assigns value to list. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (list): Data of type lis. |
|
|
|
number_index (Number): Index of data. |
|
|
|
value (String): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
List, type is same as the element type of data. |
|
|
|
list, type is same as the element type of data. |
|
|
|
""" |
|
|
|
return F.list_setitem(data, number_index, value) |
|
|
|
|
|
|
|
@@ -41,7 +40,7 @@ def _list_setitem_with_string(data, number_index, value): |
|
|
|
@setitem.register("List", "Number", "Number") |
|
|
|
def _list_setitem_with_number(data, number_index, value): |
|
|
|
""" |
|
|
|
Assign value to list. |
|
|
|
Assigns value to list. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (list): Data of type lis. |
|
|
|
@@ -49,7 +48,7 @@ def _list_setitem_with_number(data, number_index, value): |
|
|
|
value (Number): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
List, type is same as the element type of data. |
|
|
|
list, type is same as the element type of data. |
|
|
|
""" |
|
|
|
return F.list_setitem(data, number_index, value) |
|
|
|
|
|
|
|
@@ -57,7 +56,7 @@ def _list_setitem_with_number(data, number_index, value): |
|
|
|
@setitem.register("List", "Number", "Tensor") |
|
|
|
def _list_setitem_with_Tensor(data, number_index, value): |
|
|
|
""" |
|
|
|
Assign value to list. |
|
|
|
Assigns value to list. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (list): Data of type lis. |
|
|
|
@@ -65,7 +64,7 @@ def _list_setitem_with_Tensor(data, number_index, value): |
|
|
|
value (Tensor): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
List, type is same as the element type of data. |
|
|
|
list, type is same as the element type of data. |
|
|
|
""" |
|
|
|
return F.list_setitem(data, number_index, value) |
|
|
|
|
|
|
|
@@ -73,15 +72,15 @@ def _list_setitem_with_Tensor(data, number_index, value): |
|
|
|
@setitem.register("List", "Number", "List") |
|
|
|
def _list_setitem_with_List(data, number_index, value): |
|
|
|
""" |
|
|
|
Assign value to list. |
|
|
|
Assigns value to list. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (list): Data of type lis. |
|
|
|
number_index (Number): Index of data. |
|
|
|
value (List): Value given. |
|
|
|
value (list): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
List, type is same as the element type of data. |
|
|
|
list, type is same as the element type of data. |
|
|
|
""" |
|
|
|
return F.list_setitem(data, number_index, value) |
|
|
|
|
|
|
|
@@ -89,15 +88,15 @@ def _list_setitem_with_List(data, number_index, value): |
|
|
|
@setitem.register("Dictionary", "String", "Tensor") |
|
|
|
def _dict_setitem_with_tensor(data, key, value): |
|
|
|
""" |
|
|
|
Assign value to dictionary. |
|
|
|
Assigns value to dictionary. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (Dictionary): Data of type dict. |
|
|
|
data (dict): Data of type dict. |
|
|
|
key (str): Key of the data. |
|
|
|
value (Tensor): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
Dict, type is as same as the element type of data. |
|
|
|
dict, type is as same as the element type of data. |
|
|
|
""" |
|
|
|
return F.dict_setitem(data, key, value) |
|
|
|
|
|
|
|
@@ -105,15 +104,15 @@ def _dict_setitem_with_tensor(data, key, value): |
|
|
|
@setitem.register("Dictionary", "String", "Number") |
|
|
|
def _dict_setitem_with_number(data, key, value): |
|
|
|
""" |
|
|
|
Assign value to dictionary. |
|
|
|
Assigns value to dictionary. |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (Dictionary): Data of type dict. |
|
|
|
data (dict): Data of type dict. |
|
|
|
key (str): Key of the data. |
|
|
|
value (Number): Value given. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
Dict, type is as same as the element type of data. |
|
|
|
dict, type is as same as the element type of data. |
|
|
|
""" |
|
|
|
return F.dict_setitem(data, key, value) |
|
|
|
|
|
|
|
@@ -219,14 +218,14 @@ def _tensor_setitem_with_slice_v4(data, input_slice, value): |
|
|
|
Tensor assignment. |
|
|
|
|
|
|
|
Note: |
|
|
|
Syntax support: A[Slice] = U |
|
|
|
Syntax support: A[tuple(Slice)] = U, and A[tuple(Number)] = U |
|
|
|
Restraint condition: A is a Tensor |
|
|
|
Slice like "1:3, ::, :4:-1" |
|
|
|
U is a Tensor(size=1) or Tensor(size>1) |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (Tensor): Assigned tensor. |
|
|
|
input_slice (Tuple(Slice)): Slice expression. |
|
|
|
input_slice (Union[tuple[Slice], tuple[Number]]): Slice expression. |
|
|
|
value (Number): Assignment value. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
@@ -236,39 +235,43 @@ def _tensor_setitem_with_slice_v4(data, input_slice, value): |
|
|
|
|
|
|
|
|
|
|
|
def _tensor_assgin_tensor(data, input_slice, value): |
|
|
|
"""Given a tensor value assign to tensor by slice""" |
|
|
|
# 1. condition |
|
|
|
"""Assigns a tensor value to the tensor by slice.""" |
|
|
|
result = None |
|
|
|
check_result = mult_util.check_tensor_setitem_index(input_slice) |
|
|
|
if check_result: |
|
|
|
data_shape = F.shape(data) |
|
|
|
data_size = F.size(data) |
|
|
|
data_dtype = F.dtype(data) |
|
|
|
indices = mult_util.slice2indices(input_slice, data_shape) |
|
|
|
indices_size = F.size(indices) |
|
|
|
indices_size = mult_util.check_indices(indices_size, input_slice) |
|
|
|
update = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
condition_1d = F.scatter_nd(indices, update, (data_size,)) |
|
|
|
condition_1d = F.cast(condition_1d, mstype.bool_) |
|
|
|
condition = F.reshape(condition_1d, data_shape) |
|
|
|
# 2. u |
|
|
|
value_fill = None |
|
|
|
value_size = F.size(value) |
|
|
|
|
|
|
|
value_size = mult_util.check_indices_value_size(indices_size, value_size) |
|
|
|
if value_size == 1: |
|
|
|
value_fill = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
value = F.cast(value, data_dtype) |
|
|
|
value_fill = F.tensor_mul(value_fill, value) |
|
|
|
elif value_size > 1: |
|
|
|
value_fill = F.reshape(value, (indices_size,)) |
|
|
|
value_1d = F.scatter_nd(indices, value_fill, (data_size,)) |
|
|
|
u = F.reshape(value_1d, data_shape) |
|
|
|
# A[slice]= u -> A[B]=U -> select(B, U, A) |
|
|
|
result = F.select(condition, u, data) |
|
|
|
is_tuple_int = mult_util.tuple_element_is_int(input_slice) |
|
|
|
if is_tuple_int: |
|
|
|
indices = mult_util.integer_to_indices(input_slice, data_shape) |
|
|
|
result = _tensor_indices_tensor(data, data_shape, input_slice, indices, value) |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def _tensor_indices_tensor(data, data_shape, index, indices, value): |
|
|
|
"""Assigns a tensor value to the tensor.""" |
|
|
|
data_size = F.size(data) |
|
|
|
data_dtype = F.dtype(data) |
|
|
|
indices_size = F.size(indices) |
|
|
|
indices_size = mult_util.check_indices(indices_size, index) |
|
|
|
update = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
condition_1d = F.scatter_nd(indices, update, (data_size,)) |
|
|
|
condition_1d = F.cast(condition_1d, mstype.bool_) |
|
|
|
condition = F.reshape(condition_1d, data_shape) |
|
|
|
value_fill = None |
|
|
|
value_size = F.size(value) |
|
|
|
|
|
|
|
value_size = mult_util.check_indices_value_size(indices_size, value_size) |
|
|
|
if value_size == 1: |
|
|
|
value_fill = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
value = F.cast(value, data_dtype) |
|
|
|
value_fill = F.tensor_mul(value_fill, value) |
|
|
|
elif value_size > 1: |
|
|
|
value_fill = F.reshape(value, (indices_size,)) |
|
|
|
value_1d = F.scatter_nd(indices, value_fill, (data_size,)) |
|
|
|
u = F.reshape(value_1d, data_shape) |
|
|
|
return F.select(condition, u, data) |
|
|
|
|
|
|
|
@setitem.register("Tensor", "Slice", "Number") |
|
|
|
def _tensor_setitem_with_slice_v1(data, input_slice, value): |
|
|
|
""" |
|
|
|
@@ -297,14 +300,14 @@ def _tensor_setitem_with_slice_v2(data, input_slice, value): |
|
|
|
Tensor assignment. |
|
|
|
|
|
|
|
Note: |
|
|
|
Syntax support: A[Slice] = u |
|
|
|
Syntax support: A[tuple(Slice)] = u, and A[tuple(Number)] = u |
|
|
|
Restraint condition: A is a Tensor. |
|
|
|
Slice like "1:3, ::, :4:-1" |
|
|
|
u is a scalar |
|
|
|
|
|
|
|
Inputs: |
|
|
|
data (Tensor): Assigned tensor. |
|
|
|
input_slice (Tuple(Slice)): slice expression. |
|
|
|
input_slice (Union[tuple[Slice], tuple[Number]]): slice expression. |
|
|
|
value (Number): Assignment value. |
|
|
|
|
|
|
|
Outputs: |
|
|
|
@@ -314,25 +317,46 @@ def _tensor_setitem_with_slice_v2(data, input_slice, value): |
|
|
|
|
|
|
|
|
|
|
|
def _tensor_assgin_number(data, input_slice, value): |
|
|
|
"""Given a scalar assign to tensor by slice""" |
|
|
|
# 1. condition |
|
|
|
"""Givens a scalar assign to tensor by slice""" |
|
|
|
check_result = mult_util.check_tensor_setitem_index(input_slice) |
|
|
|
result = None |
|
|
|
if check_result: |
|
|
|
data_shape = F.shape(data) |
|
|
|
data_size = F.size(data) |
|
|
|
data_dtype = F.dtype(data) |
|
|
|
indices = mult_util.slice2indices(input_slice, data_shape) |
|
|
|
indices_size = F.size(indices) |
|
|
|
indices_size = mult_util.check_indices(indices_size, input_slice) |
|
|
|
update = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
condition_1d = F.scatter_nd(indices, update, (data_size,)) |
|
|
|
condition_1d = F.cast(condition_1d, mstype.bool_) |
|
|
|
condition = F.reshape(condition_1d, data_shape) |
|
|
|
# 2. u |
|
|
|
value_fill = F.fill(data_dtype, (indices_size,), value) |
|
|
|
value_1d = F.scatter_nd(indices, value_fill, (data_size,)) |
|
|
|
u = F.reshape(value_1d, data_shape) |
|
|
|
# A[slice]= u -> A[B]=U -> select(B, U, A) |
|
|
|
result = F.select(condition, u, data) |
|
|
|
is_tuple_int = mult_util.tuple_element_is_int(input_slice) |
|
|
|
if is_tuple_int: |
|
|
|
indices = mult_util.integer_to_indices(input_slice, data_shape) |
|
|
|
result = _tensor_indices_number(data, data_shape, input_slice, indices, value) |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def _tensor_indices_number(data, data_shape, index, indices, value): |
|
|
|
"""Assigns a scalar value to the tensor.""" |
|
|
|
data_size = F.size(data) |
|
|
|
data_dtype = F.dtype(data) |
|
|
|
indices_size = F.size(indices) |
|
|
|
indices_size = mult_util.check_indices(indices_size, index) |
|
|
|
update = F.fill(data_dtype, (indices_size,), 1) |
|
|
|
condition_1d = F.scatter_nd(indices, update, (data_size,)) |
|
|
|
condition_1d = F.cast(condition_1d, mstype.bool_) |
|
|
|
condition = F.reshape(condition_1d, data_shape) |
|
|
|
value_fill = F.fill(data_dtype, (indices_size,), value) |
|
|
|
value_1d = F.scatter_nd(indices, value_fill, (data_size,)) |
|
|
|
u = F.reshape(value_1d, data_shape) |
|
|
|
return F.select(condition, u, data) |
|
|
|
|
|
|
|
|
|
|
|
@setitem.register("Tensor", "Number", "Number") |
|
|
|
def _tensor_setitem_with_int_v1(data, index, value): |
|
|
|
"""Syntax: A[1] = 3""" |
|
|
|
data_shape = F.shape(data) |
|
|
|
indices = mult_util.integer_to_indices(index, data_shape) |
|
|
|
return _tensor_indices_number(data, data_shape, index, indices, value) |
|
|
|
|
|
|
|
|
|
|
|
@setitem.register("Tensor", "Number", "Tensor") |
|
|
|
def _tensor_setitem_with_int_v2(data, index, value): |
|
|
|
"""Syntax: A[1] = Tensor""" |
|
|
|
data_shape = F.shape(data) |
|
|
|
indices = mult_util.integer_to_indices(index, data_shape) |
|
|
|
return _tensor_indices_tensor(data, data_shape, index, indices, value) |