Skip to content

Commit

Permalink
Add spec for ConvertOp (#421)
Browse files Browse the repository at this point in the history
closes #418
  • Loading branch information
ghpvnist authored Dec 14, 2022
1 parent d607fcb commit 59b68c5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
71 changes: 71 additions & 0 deletions docs/spec_draft.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ syntax.
* [complex](#stablehlocomplex)
* [concatenate](#stablehloconcatenate)
* [constant](#stablehloconstant)
* [convert](#stablehloconvert)
* [convolution](#stablehloconvolution)
* [cosine](#stablehlocosine)
* [count_leading_zeros](#stablehlocount_leading_zeros)
Expand Down Expand Up @@ -1816,6 +1817,76 @@ Produces an `output` tensor from a constant `value`.

[Back to Ops](#index-of-ops)

## stablehlo.convert

### Semantics

Performs an element-wise conversion from one element type to another on
`operand` tensor and produces a `result` tensor.

For conversions involving **integer-to-integer**, if there is an unsigned/signed
overflow, the result is implementation-defined and one of the following:
* mathematical result modulo $2^n$, where n is the bit width of the result,
for unsigned overflow. For signed integer overflow, wraps the result around
the representable range $[-2^{n-1},\ 2^{n-1} - 1]$.
* saturation to $2^{n-1} - 1$ (or $-2^{n-1}$) for signed overflow and
saturation to $2^n - 1$ (or $0$) for unsigned overflow.

For conversions involving **floating-point-to-floating-point** or
**integer-to-floating-point**, if the source value can be exactly represented in
the destination type, the result value is that exact representation. Otherwise,
the behavior is TBD.

Conversion involving **complex-to-complex** follows the same behavior of
**floating-point-to-floating-point** conversions for converting real and
imaginary parts.

For conversions involving **floating-point-to-complex** or
**complex-to-floating-point**, the destination imaginary value is zeroed or the
source imaginary value is ignored, respectively. The conversion of the real part
follows the **floating-point-to-floating-point** conversion.

Conversions involving **integer-to-complex** follows the same behavior as
**integer-to-floating-point** conversion while converting the source integer to
destination real part. The destination imaginary part is zeroed.

For conversions involving **floating-point-to-integer**, the fractional part is
truncated. If the truncated value cannot be represented in the destination type,
the behavior is TBD. Conversions involving **complex-to-integer** follows the
same behavior while converting the source real part to destination integer. The
source imaginary part is ignored.

For **boolean-to-any-supported-type** conversions, the value `false` is
converted to zero, and the value `true` is converted to one. For
**any-supported-type-to-boolean** conversions, a zero value is converted to
`false` and any non-zero value is converted to `true`.

### Inputs

| Name | Type |
|-----------|------------------------------|
| `operand` | tensor of any supported type |

### Outputs

| Name | Type |
|----------|------------------------------|
| `result` | tensor of any supported type |

### Constraints

* (C1) `operand` and `result` have the same shape.

### Examples

```mlir
// %operand: [1, 2, 3]
%result = "stablehlo.convert"(%operand) : (tensor<3xi32>) -> tensor<3xcomplex<f32>>
// %result: [(1.0, 0.0), (2.0, 0.0), (3.0, 0.0)]
```

[Back to Ops](#index-of-ops)

## stablehlo.convolution

### Semantics
Expand Down
2 changes: 1 addition & 1 deletion docs/status.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ one of the following tracking labels.
| compute_reshape_shape | no | revisit | no | yes | no |
| concatenate | yes | yes | yes | yes | no |
| constant | yes | yes | yes | yes | yes |
| convert | no | yes* | infeasible | yes | no |
| convert | yes | yes | infeasible | yes | no |
| convolution | revisit | yes | revisit | revisit | no |
| cosine | yes | yes | yes | yes | yes |
| count_leading_zeros | yes | yes | yes | yes | no |
Expand Down
10 changes: 5 additions & 5 deletions stablehlo/dialect/StablehloOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ def StableHLO_ConvertOp : StableHLO_UnaryElementwiseOp<"convert",
[Pure, SameOperandsAndResultShape], HLO_Tensor> {
let summary = "Convert operator";
let description = [{
Performs element-wise conversion of values from one type to another, e.g.
float to int.
Performs an element-wise conversion from one element type to another on
`operand` tensor and produces a `result` tensor.

See https://www.tensorflow.org/xla/operation_semantics#convertelementtype.
See:
https://github.com/openxla/stablehlo/blob/main/docs/spec_draft.md#stablehloconvert

Example:

```mlir
%0 = stablehlo.convert %arg0 : (tensor<2xi32>) -> tensor<2xf32>
%result = stablehlo.convert %operand : (tensor<3xi32>) -> tensor<3xcomplex<f32>>
```
}];
let builders = [
Expand Down

0 comments on commit 59b68c5

Please sign in to comment.