| `[]` |The operation object type can be `list`, `tuple`, or `Tensor`. Accessed multiple subscripts of lists and dictionaries can be used as r-values instead of l-values. Only when the operation object type is `tuple(nn.Cell)`, the index type can be Tensor. `tuple(nn.Cell)` means all elements type of tuple are `nn.Cell`. For details about access constraints for the tuple and Tensor types, see the description of slicing operations.
| `[]` |The operation object type can be `list`, `tuple`, or `Tensor`. Accessed multiple subscripts of lists and dictionaries can be used as r-values instead of l-values. Only when the operation object type is tuple or list with element type `nn.Cell`, the index type can be Tensor. For details about access constraints for the tuple and Tensor types, see the description of slicing operations.
### Index operation
### Index operation
...
@@ -145,7 +145,7 @@ The index operation includes `tuple` and` Tensor`. The following focuses on the
...
@@ -145,7 +145,7 @@ The index operation includes `tuple` and` Tensor`. The following focuses on the
- Assignment: for example `tensor_x[..., ::, 1:] = u`.
- Assignment: for example `tensor_x[..., ::, 1:] = u`.
- Not supported in other situations
- Not supported in other situations
The slice value operations of the tuple type needs to focus on the slice value operation of the operation object type `tuple(nn.Cell)`. This operation is currently only supported by the GPU backend in Graph mode, and its syntax format is like `layers[index](*inputs)`, the example code is as follows:
The index value operation of tuple and list type, we need to focus on the index value operation of tuple or list whose element type is `nn.Cell`. This operation is currently only supported by the GPU backend in Graph mode, and its syntax format is like `layers[index](*inputs)`, the example code is as follows:
```python
```python
classNet(nn.Cell):
classNet(nn.Cell):
def__init__(self):
def__init__(self):
...
@@ -159,13 +159,42 @@ The slice value operations of the tuple type needs to focus on the slice value o
...
@@ -159,13 +159,42 @@ The slice value operations of the tuple type needs to focus on the slice value o
returnx
returnx
```
```
The grammar has the following constraints:
The grammar has the following constraints:
* Only supports slice value operation with operation object type `tuple(nn.Cell)`.
* Only the index value operation of tuple or list whose element type is `nn.Cell` is supported.
* The data type of the index value needs to be a Tensor scalar of type `int32`.
* The index is a scalar `Tensor` of type `int32`, with a value range of `[-n, n)`, where `n` is the size of the tuple, and the maximum supported tuple size is 1000.
* The value range of index value is `[-n, n)`, where `n` is the size of the tuple, and the maximum supported tuple size is 1000.
* The number, type and shape of the input data of the `Construct` function of each Cell element in the tuple are the same, and the number of data output after the `Construct` function runs, the type and shape are also the same.
* The number, type and shape of the input data of the `Construct` function of each Cell element in the tuple are the same, and the number of data output after the `Construct` function runs, the type and shape are also the same.
* Each element in the tuple needs to be defined before the tuple is defined.
* Each element in the tuple needs to be defined before the tuple is defined.
* This syntax does not support running branches as if, while, for and other control flow, except if the control condition of the control flow is constant. for example:
- Supported example:
```python
classNet(nn.Cell):
def__init__(self,flag=True):
super(Net,self).__init__()
self.flag=flag
self.relu=nn.ReLU()
self.softmax=nn.Softmax()
self.layers=(self.relu,self.softmax)
defconstruct(self,x,index):
ifself.flag:
x=self.layers[index](x)
returnx
```
- Unsupported example:
```python
classNet(nn.Cell):
def__init__(self):
super(Net,self).__init__()
self.relu=nn.ReLU()
self.softmax=nn.Softmax()
self.layers=(self.relu,self.softmax)
defconstruct(self,x,index,flag):
ifflag:
x=self.layers[index](x)
returnx
```
Other types of tuple also support slice value operations, but do not support index type as Tensor, support `tuple_x [start: stop: step]`, which has the same effect as Python, and will not be repeated here.
Tuple also support slice value operations, but do not support slice type as Tensor, support `tuple_x [start: stop: step]`, which has the same effect as Python, and will not be repeated here.