Skip to content

Commit

Permalink
[mlir][OpDSL] Add default value to index attributes.
Browse files Browse the repository at this point in the history
Index attributes had no default value, which means the attribute values had to be set on the operation. This revision adds a default parameter to `IndexAttrDef`. After the change, every index attribute has to define a default value. For example, we may define the following strides attribute:
```

```
When using the operation the default stride is used if the strides attribute is not set. The mechanism is implemented using `DefaultValuedAttr`.

Additionally, the revision uses the naming index attribute instead of attribute more consistently, which is a preparation for follow up revisions that will introduce function attributes.

Depends On D119125

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D119126
  • Loading branch information
gysit committed Feb 14, 2022
1 parent 880e875 commit d50571a
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 429 deletions.
13 changes: 7 additions & 6 deletions mlir/docs/Dialects/Linalg/OpDSL.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ appear in the parameter list of the operation:
copy_and_scale(val, in_tensor, outs=[out_tensor])
```

## Attributes
## Index Attributes

Attributes are compile-time constant parameters only accessible in index
expressions. They can be used to parameterize the access pattern of a structured
Expand All @@ -118,7 +118,7 @@ The following example demonstrates the use of attributes:
@linalg_structured_op
def strided_copy(I=TensorDef(T, S.IH, S.IW),
O=TensorDef(T, S.OH, S.OW, output=True),
strides=IndexAttrDef(S.SH, S.SW)):
strides=IndexAttrDef(S.SH, S.SW, default=[1, 1])):
"""Copy a subset of the input tensor elements to the output tensor"""
O[D.oh, D.ow] = I[D.oh * S.SH, D.ow * S.SW]
```
Expand All @@ -129,11 +129,12 @@ the symbols `S.SH` and `S.SW`, which are used to index the input tensor `I`.
When instantiating the operation, the attribute is set using a named argument:

```python
strided_copy(in_tensor, outs=[out_tensor], strides=[1,2])
strided_copy(in_tensor, outs=[out_tensor], strides=[1, 2])
```

The `strides` vector elements substitute the symbols `S.SH` and `S.SW` in the
index expressions of the operation instance.
index expressions of the operation instance. If no strides are provided the
`default` vector elements are used instead.

Attributes are currently limited to integer vectors and only accessible in index
expressions. An operation may have multiple attributes all of them placed at the
Expand All @@ -157,8 +158,8 @@ def pooling_poly(
I=TensorDef(T1, S.N, S.H, S.W, S.C),
K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]),
O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True),
strides=IndexAttrDef(S.SH, S.SW),
dilations=IndexAttrDef(S.DH, S.DW)):
strides=IndexAttrDef(S.SH, S.SW, default=[1, 1]),
dilations=IndexAttrDef(S.DH, S.DW, default=[1, 1])):
O[D.n, D.oh, D.ow, D.c] += TypeFn.cast(U,
I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c])
```
Expand Down
Loading

0 comments on commit d50571a

Please sign in to comment.